search.hpp 5.3 KB

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