Horizon
Loading...
Searching...
No Matches
max.hpp
Go to the documentation of this file.
1
2// Range v3 library
3//
4// Copyright Eric Niebler 2014-present
5// Copyright Casey Carter 2015
6//
7// Use, modification and distribution is subject to the
8// Boost Software License, Version 1.0. (See accompanying
9// file LICENSE_1_0.txt or copy at
10// http://www.boost.org/LICENSE_1_0.txt)
11//
12// Project home: https://github.com/ericniebler/range-v3
13//
14#ifndef RANGES_V3_ALGORITHM_MAX_HPP
15#define RANGES_V3_ALGORITHM_MAX_HPP
16
17#include <initializer_list>
18
20
29#include <range/v3/utility/static_const.hpp>
30
31#include <range/v3/detail/prologue.hpp>
32
33namespace ranges
34{
37 RANGES_FUNC_BEGIN(max)
38
39
40 template(typename T, typename C = less, typename P = identity)(
41 requires indirect_strict_weak_order<C, projected<T const *, P>>)
42 constexpr T const & RANGES_FUNC(max)(
43 T const & a, T const & b, C pred = C{}, P proj = P{}) //
44 {
45 return invoke(pred, invoke(proj, b), invoke(proj, a)) ? a : b;
46 }
47
49 template(typename Rng, typename C = less, typename P = identity)(
50 requires input_range<Rng> AND
51 indirect_strict_weak_order<C, projected<iterator_t<Rng>, P>> AND
52 indirectly_copyable_storable<iterator_t<Rng>, range_value_t<Rng> *>)
53 constexpr range_value_t<Rng> //
54 RANGES_FUNC(max)(Rng && rng, C pred = C{}, P proj = P{}) //
55 {
56 auto first = ranges::begin(rng);
57 auto last = ranges::end(rng);
58 RANGES_EXPECT(first != last);
59 range_value_t<Rng> result = *first;
60 while(++first != last)
61 {
62 auto && tmp = *first;
63 if(invoke(pred, invoke(proj, result), invoke(proj, tmp)))
64 result = (decltype(tmp) &&)tmp;
65 }
66 return result;
67 }
68
70 template(typename T, typename C = less, typename P = identity)(
71 requires copyable<T> AND
72 indirect_strict_weak_order<C, projected<T const *, P>>)
73 constexpr T RANGES_FUNC(max)(
74 std::initializer_list<T> const && rng, C pred = C{}, P proj = P{}) //
75 {
76 return (*this)(rng, std::move(pred), std::move(proj));
77 }
78
79 RANGES_FUNC_END(max)
80
81 namespace cpp20
82 {
83 using ranges::max;
84 }
86} // namespace ranges
87
88#include <range/v3/detail/epilogue.hpp>
89
90#endif
The indirect_strict_weak_order concept.
typename Fn::template invoke< Args... > invoke
Evaluate the invocable Fn with the arguments Args.
Definition meta.hpp:541
front< Pair > first
Retrieve the first element of the pair Pair.
Definition meta.hpp:2251
bool_<(T::type::value< U::type::value)> less
A Boolean integral constant wrapper around true if T::type::value is less than U::type::value; false,...
Definition meta.hpp:255
Definition identity.hpp:25
Definition comparisons.hpp:50