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