Debian Codegraph @

3rd_party/range-v3/range/v3/algorithm/max.hpp

raw
1 /// \file
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
19 #include <range/v3/range_fwd.hpp>
20
21 #include <range/v3/functional/comparisons.hpp>
22 #include <range/v3/functional/identity.hpp>
23 #include <range/v3/functional/invoke.hpp>
24 #include <range/v3/iterator/concepts.hpp>
25 #include <range/v3/iterator/traits.hpp>
26 #include <range/v3/range/access.hpp>
27 #include <range/v3/range/concepts.hpp>
28 #include <range/v3/range/traits.hpp>
29 #include <range/v3/utility/static_const.hpp>
30
31 #include <range/v3/detail/prologue.hpp>
32
33 namespace ranges
34 {
35 /// \addtogroup group-algorithms
36 /// @{
37 RANGES_FUNC_BEGIN(max)
38
39 /// \brief function template \c max
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
48 /// \overload
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
69 /// \overload
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 }
85 /// @}
86 } // namespace ranges
87
88 #include <range/v3/detail/epilogue.hpp>
89
90 #endif