lower_bound.hpp 4.2 KB

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