find_first_of.hpp 6.0 KB

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