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 //
7 // For more information, see http://www.boost.org/libs/range/
8 //
9 #ifndef BOOST_RANGE_ALGORITHM_LOWER_BOUND_HPP_INCLUDED
10 #define BOOST_RANGE_ALGORITHM_LOWER_BOUND_HPP_INCLUDED
11
12 #include <boost/concept_check.hpp>
13 #include <boost/range/begin.hpp>
14 #include <boost/range/end.hpp>
15 #include <boost/range/concepts.hpp>
16 #include <boost/range/detail/range_return.hpp>
17 #include <algorithm>
18
19 namespace boost
20 {
21 namespace range
22 {
23
24 /// \brief template function lower_bound
25 ///
26 /// range-based version of the lower_bound std algorithm
27 ///
28 /// \pre ForwardRange is a model of the ForwardRangeConcept
29 template< class ForwardRange, class Value >
30 inline BOOST_DEDUCED_TYPENAME disable_if<
31 is_const<ForwardRange>,
32 BOOST_DEDUCED_TYPENAME range_iterator<ForwardRange>::type
33 >::type
lower_bound(ForwardRange & rng,const Value & val)34 lower_bound( ForwardRange& rng, const Value& val )
35 {
36 BOOST_RANGE_CONCEPT_ASSERT(( ForwardRangeConcept<ForwardRange> ));
37 return std::lower_bound(boost::begin(rng), boost::end(rng), val);
38 }
39
40 /// \overload
41 template< class ForwardRange, class Value >
42 inline BOOST_DEDUCED_TYPENAME range_iterator<const ForwardRange>::type
lower_bound(const ForwardRange & rng,const Value & val)43 lower_bound( const ForwardRange& rng, const Value& val )
44 {
45 BOOST_RANGE_CONCEPT_ASSERT(( ForwardRangeConcept<const ForwardRange> ));
46 return std::lower_bound(boost::begin(rng), boost::end(rng), val);
47 }
48
49 /// \overload
50 template< class ForwardRange, class Value, class SortPredicate >
51 inline BOOST_DEDUCED_TYPENAME disable_if<
52 is_const<ForwardRange>,
53 BOOST_DEDUCED_TYPENAME range_iterator<ForwardRange>::type
54 >::type
lower_bound(ForwardRange & rng,const Value & val,SortPredicate pred)55 lower_bound( ForwardRange& rng, const Value& val, SortPredicate pred )
56 {
57 BOOST_RANGE_CONCEPT_ASSERT(( ForwardRangeConcept<ForwardRange> ));
58 return std::lower_bound(boost::begin(rng), boost::end(rng), val, pred);
59 }
60
61 /// \overload
62 template< class ForwardRange, class Value, class SortPredicate >
63 inline BOOST_DEDUCED_TYPENAME range_iterator<const ForwardRange>::type
lower_bound(const ForwardRange & rng,const Value & val,SortPredicate pred)64 lower_bound( const ForwardRange& rng, const Value& val, SortPredicate pred )
65 {
66 BOOST_RANGE_CONCEPT_ASSERT(( ForwardRangeConcept<const ForwardRange> ));
67 return std::lower_bound(boost::begin(rng), boost::end(rng), val, pred);
68 }
69
70 /// \overload
71 template< range_return_value re, class ForwardRange, class Value >
72 inline BOOST_DEDUCED_TYPENAME disable_if<
73 is_const<ForwardRange>,
74 BOOST_DEDUCED_TYPENAME range_return<ForwardRange,re>::type
75 >::type
lower_bound(ForwardRange & rng,const Value & val)76 lower_bound( ForwardRange& rng, const Value& val )
77 {
78 BOOST_RANGE_CONCEPT_ASSERT(( ForwardRangeConcept<ForwardRange> ));
79 return range_return<ForwardRange,re>::
80 pack(std::lower_bound(boost::begin(rng), boost::end(rng), val),
81 rng);
82 }
83
84 /// \overload
85 template< range_return_value re, class ForwardRange, class Value >
86 inline BOOST_DEDUCED_TYPENAME range_return<const ForwardRange,re>::type
lower_bound(const ForwardRange & rng,const Value & val)87 lower_bound( const ForwardRange& rng, const Value& val )
88 {
89 BOOST_RANGE_CONCEPT_ASSERT(( ForwardRangeConcept<const ForwardRange> ));
90 return range_return<const ForwardRange,re>::
91 pack(std::lower_bound(boost::begin(rng), boost::end(rng), val),
92 rng);
93 }
94
95 /// \overload
96 template< range_return_value re, class ForwardRange, class Value, class SortPredicate >
97 inline BOOST_DEDUCED_TYPENAME disable_if<
98 is_const<ForwardRange>,
99 BOOST_DEDUCED_TYPENAME range_return<ForwardRange,re>::type
100 >::type
lower_bound(ForwardRange & rng,const Value & val,SortPredicate pred)101 lower_bound( ForwardRange& rng, const Value& val, SortPredicate pred )
102 {
103 BOOST_RANGE_CONCEPT_ASSERT(( ForwardRangeConcept<ForwardRange> ));
104 return range_return<ForwardRange,re>::
105 pack(std::lower_bound(boost::begin(rng), boost::end(rng), val, pred),
106 rng);
107 }
108
109 /// \overload
110 template< range_return_value re, class ForwardRange, class Value, class SortPredicate >
111 inline BOOST_DEDUCED_TYPENAME range_return<const ForwardRange,re>::type
lower_bound(const ForwardRange & rng,const Value & val,SortPredicate pred)112 lower_bound( const ForwardRange& rng, const Value& val, SortPredicate pred )
113 {
114 BOOST_RANGE_CONCEPT_ASSERT(( ForwardRangeConcept<const ForwardRange> ));
115 return range_return<const ForwardRange,re>::
116 pack(std::lower_bound(boost::begin(rng), boost::end(rng), val, pred),
117 rng);
118 }
119
120 } // namespace range
121 using range::lower_bound;
122 } // namespace boost
123
124 #endif // include guard
125