1 // Copyright Neil Groves 2009. Use, modification and
2 // distribution is subject to the Boost Software License, Version
3 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
5 //
6 // Copyright 2019 Glen Joseph Fernandes (glenjofe@gmail.com)
7 //
8 // For more information, see http://www.boost.org/libs/range/
9 //
10 #ifndef BOOST_RANGE_ALGORITHM_MAX_ELEMENT_HPP_INCLUDED
11 #define BOOST_RANGE_ALGORITHM_MAX_ELEMENT_HPP_INCLUDED
12
13 #include <boost/concept_check.hpp>
14 #include <boost/range/begin.hpp>
15 #include <boost/range/end.hpp>
16 #include <boost/range/concepts.hpp>
17 #include <boost/range/detail/less.hpp>
18 #include <boost/range/detail/range_return.hpp>
19
20 namespace boost
21 {
22 namespace range
23 {
24
25 namespace detail
26 {
27
28 template<typename Iterator, class Predicate>
29 inline Iterator
max_element(Iterator first,Iterator last,Predicate comp)30 max_element(Iterator first, Iterator last, Predicate comp)
31 {
32 if (first == last) {
33 return last;
34 }
35 Iterator result = first;
36 while (++first != last) {
37 if (comp(*result, *first)) {
38 result = first;
39 }
40 }
41 return result;
42 }
43
44 } // namespace detail
45
46 /// \brief template function max_element
47 ///
48 /// range-based version of the max_element std algorithm
49 ///
50 /// \pre ForwardRange is a model of the ForwardRangeConcept
51 /// \pre BinaryPredicate is a model of the BinaryPredicateConcept
52 template<class ForwardRange>
53 inline BOOST_DEDUCED_TYPENAME range_iterator<ForwardRange>::type
max_element(ForwardRange & rng)54 max_element(ForwardRange& rng)
55 {
56 BOOST_RANGE_CONCEPT_ASSERT(( ForwardRangeConcept<ForwardRange> ));
57 return detail::max_element(boost::begin(rng), boost::end(rng), detail::less());
58 }
59
60 /// \overload
61 template<class ForwardRange>
62 inline BOOST_DEDUCED_TYPENAME range_iterator<const ForwardRange>::type
max_element(const ForwardRange & rng)63 max_element(const ForwardRange& rng)
64 {
65 BOOST_RANGE_CONCEPT_ASSERT(( ForwardRangeConcept<const ForwardRange> ));
66 return detail::max_element(boost::begin(rng), boost::end(rng), detail::less());
67 }
68
69 /// \overload
70 template<class ForwardRange, class BinaryPredicate>
71 inline BOOST_DEDUCED_TYPENAME range_iterator<ForwardRange>::type
max_element(ForwardRange & rng,BinaryPredicate pred)72 max_element(ForwardRange& rng, BinaryPredicate pred)
73 {
74 BOOST_RANGE_CONCEPT_ASSERT(( ForwardRangeConcept<ForwardRange> ));
75 return detail::max_element(boost::begin(rng), boost::end(rng), pred);
76 }
77
78 /// \overload
79 template<class ForwardRange, class BinaryPredicate>
80 inline BOOST_DEDUCED_TYPENAME range_iterator<const ForwardRange>::type
max_element(const ForwardRange & rng,BinaryPredicate pred)81 max_element(const ForwardRange& rng, BinaryPredicate pred)
82 {
83 BOOST_RANGE_CONCEPT_ASSERT(( ForwardRangeConcept<const ForwardRange> ));
84 return detail::max_element(boost::begin(rng), boost::end(rng), pred);
85 }
86
87 // range_return overloads
88
89 /// \overload
90 template<range_return_value re, class ForwardRange>
91 inline BOOST_DEDUCED_TYPENAME range_return<ForwardRange,re>::type
max_element(ForwardRange & rng)92 max_element(ForwardRange& rng)
93 {
94 BOOST_RANGE_CONCEPT_ASSERT(( ForwardRangeConcept<ForwardRange> ));
95 return range_return<ForwardRange,re>::pack(
96 detail::max_element(boost::begin(rng), boost::end(rng), detail::less()),
97 rng);
98 }
99
100 /// \overload
101 template<range_return_value re, class ForwardRange>
102 inline BOOST_DEDUCED_TYPENAME range_return<const ForwardRange,re>::type
max_element(const ForwardRange & rng)103 max_element(const ForwardRange& rng)
104 {
105 BOOST_RANGE_CONCEPT_ASSERT(( ForwardRangeConcept<const ForwardRange> ));
106 return range_return<const ForwardRange,re>::pack(
107 detail::max_element(boost::begin(rng), boost::end(rng), detail::less()),
108 rng);
109 }
110
111 /// \overload
112 template<range_return_value re, class ForwardRange, class BinaryPredicate>
113 inline BOOST_DEDUCED_TYPENAME range_return<ForwardRange,re>::type
max_element(ForwardRange & rng,BinaryPredicate pred)114 max_element(ForwardRange& rng, BinaryPredicate pred)
115 {
116 BOOST_RANGE_CONCEPT_ASSERT(( ForwardRangeConcept<ForwardRange> ));
117 return range_return<ForwardRange,re>::pack(
118 detail::max_element(boost::begin(rng), boost::end(rng), pred),
119 rng);
120 }
121
122 /// \overload
123 template<range_return_value re, class ForwardRange, class BinaryPredicate>
124 inline BOOST_DEDUCED_TYPENAME range_return<const ForwardRange,re>::type
max_element(const ForwardRange & rng,BinaryPredicate pred)125 max_element(const ForwardRange& rng, BinaryPredicate pred)
126 {
127 BOOST_RANGE_CONCEPT_ASSERT(( ForwardRangeConcept<const ForwardRange> ));
128 return range_return<const ForwardRange,re>::pack(
129 detail::max_element(boost::begin(rng), boost::end(rng), pred),
130 rng);
131 }
132
133 } // namespace range
134 using range::max_element;
135 } // namespace boost
136
137 #endif // include guard
138