upper_bound.hpp 4.2 KB

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