mismatch.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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_MISMATCH_HPP_INCLUDED
  10. #define BOOST_RANGE_ALGORITHM_MISMATCH_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/difference_type.hpp>
  16. #include <algorithm>
  17. namespace boost
  18. {
  19. namespace range_detail
  20. {
  21. template< class SinglePassTraversalReadableIterator1,
  22. class SinglePassTraversalReadableIterator2 >
  23. inline std::pair<SinglePassTraversalReadableIterator1,
  24. SinglePassTraversalReadableIterator2>
  25. mismatch_impl(SinglePassTraversalReadableIterator1 first1,
  26. SinglePassTraversalReadableIterator1 last1,
  27. SinglePassTraversalReadableIterator2 first2,
  28. SinglePassTraversalReadableIterator2 last2)
  29. {
  30. while (first1 != last1 && first2 != last2 && *first1 == *first2)
  31. {
  32. ++first1;
  33. ++first2;
  34. }
  35. return std::pair<SinglePassTraversalReadableIterator1,
  36. SinglePassTraversalReadableIterator2>(first1, first2);
  37. }
  38. template< class SinglePassTraversalReadableIterator1,
  39. class SinglePassTraversalReadableIterator2,
  40. class BinaryPredicate >
  41. inline std::pair<SinglePassTraversalReadableIterator1,
  42. SinglePassTraversalReadableIterator2>
  43. mismatch_impl(SinglePassTraversalReadableIterator1 first1,
  44. SinglePassTraversalReadableIterator1 last1,
  45. SinglePassTraversalReadableIterator2 first2,
  46. SinglePassTraversalReadableIterator2 last2,
  47. BinaryPredicate pred)
  48. {
  49. while (first1 != last1 && first2 != last2 && pred(*first1, *first2))
  50. {
  51. ++first1;
  52. ++first2;
  53. }
  54. return std::pair<SinglePassTraversalReadableIterator1,
  55. SinglePassTraversalReadableIterator2>(first1, first2);
  56. }
  57. } // namespace range_detail
  58. namespace range
  59. {
  60. /// \brief template function mismatch
  61. ///
  62. /// range-based version of the mismatch std algorithm
  63. ///
  64. /// \pre SinglePassRange1 is a model of the SinglePassRangeConcept
  65. /// \pre SinglePassRange2 is a model of the SinglePassRangeConcept
  66. /// \pre BinaryPredicate is a model of the BinaryPredicateConcept
  67. template< class SinglePassRange1, class SinglePassRange2 >
  68. inline std::pair<
  69. BOOST_DEDUCED_TYPENAME range_iterator<SinglePassRange1>::type,
  70. BOOST_DEDUCED_TYPENAME range_iterator<const SinglePassRange2>::type >
  71. mismatch(SinglePassRange1& rng1, const SinglePassRange2 & rng2)
  72. {
  73. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<SinglePassRange1> ));
  74. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange2> ));
  75. return ::boost::range_detail::mismatch_impl(
  76. ::boost::begin(rng1), ::boost::end(rng1),
  77. ::boost::begin(rng2), ::boost::end(rng2));
  78. }
  79. /// \overload
  80. template< class SinglePassRange1, class SinglePassRange2 >
  81. inline std::pair<
  82. BOOST_DEDUCED_TYPENAME range_iterator<const SinglePassRange1>::type,
  83. BOOST_DEDUCED_TYPENAME range_iterator<const SinglePassRange2>::type >
  84. mismatch(const SinglePassRange1& rng1, const SinglePassRange2& rng2)
  85. {
  86. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange1> ));
  87. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange2> ));
  88. return ::boost::range_detail::mismatch_impl(
  89. ::boost::begin(rng1), ::boost::end(rng1),
  90. ::boost::begin(rng2), ::boost::end(rng2));
  91. }
  92. /// \overload
  93. template< class SinglePassRange1, class SinglePassRange2 >
  94. inline std::pair<
  95. BOOST_DEDUCED_TYPENAME range_iterator<SinglePassRange1>::type,
  96. BOOST_DEDUCED_TYPENAME range_iterator<SinglePassRange2>::type >
  97. mismatch(SinglePassRange1& rng1, SinglePassRange2 & rng2)
  98. {
  99. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<SinglePassRange1> ));
  100. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<SinglePassRange2> ));
  101. return ::boost::range_detail::mismatch_impl(
  102. ::boost::begin(rng1), ::boost::end(rng1),
  103. ::boost::begin(rng2), ::boost::end(rng2));
  104. }
  105. /// \overload
  106. template< class SinglePassRange1, class SinglePassRange2 >
  107. inline std::pair<
  108. BOOST_DEDUCED_TYPENAME range_iterator<const SinglePassRange1>::type,
  109. BOOST_DEDUCED_TYPENAME range_iterator<SinglePassRange2>::type >
  110. mismatch(const SinglePassRange1& rng1, SinglePassRange2& rng2)
  111. {
  112. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange1> ));
  113. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<SinglePassRange2> ));
  114. return ::boost::range_detail::mismatch_impl(
  115. ::boost::begin(rng1), ::boost::end(rng1),
  116. ::boost::begin(rng2), ::boost::end(rng2));
  117. }
  118. /// \overload
  119. template< class SinglePassRange1, class SinglePassRange2, class BinaryPredicate >
  120. inline std::pair<
  121. BOOST_DEDUCED_TYPENAME range_iterator<SinglePassRange1>::type,
  122. BOOST_DEDUCED_TYPENAME range_iterator<const SinglePassRange2>::type >
  123. mismatch(SinglePassRange1& rng1, const SinglePassRange2& rng2, BinaryPredicate pred)
  124. {
  125. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<SinglePassRange1> ));
  126. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange2> ));
  127. return ::boost::range_detail::mismatch_impl(
  128. ::boost::begin(rng1), ::boost::end(rng1),
  129. ::boost::begin(rng2), ::boost::end(rng2), pred);
  130. }
  131. /// \overload
  132. template< class SinglePassRange1, class SinglePassRange2, class BinaryPredicate >
  133. inline std::pair<
  134. BOOST_DEDUCED_TYPENAME range_iterator<const SinglePassRange1>::type,
  135. BOOST_DEDUCED_TYPENAME range_iterator<const SinglePassRange2>::type >
  136. mismatch(const SinglePassRange1& rng1, const SinglePassRange2& rng2, BinaryPredicate pred)
  137. {
  138. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange1> ));
  139. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange2> ));
  140. return ::boost::range_detail::mismatch_impl(
  141. ::boost::begin(rng1), ::boost::end(rng1),
  142. ::boost::begin(rng2), ::boost::end(rng2), pred);
  143. }
  144. /// \overload
  145. template< class SinglePassRange1, class SinglePassRange2, class BinaryPredicate >
  146. inline std::pair<
  147. BOOST_DEDUCED_TYPENAME range_iterator<SinglePassRange1>::type,
  148. BOOST_DEDUCED_TYPENAME range_iterator<SinglePassRange2>::type >
  149. mismatch(SinglePassRange1& rng1, SinglePassRange2& rng2, BinaryPredicate pred)
  150. {
  151. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<SinglePassRange1> ));
  152. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<SinglePassRange2> ));
  153. return ::boost::range_detail::mismatch_impl(
  154. ::boost::begin(rng1), ::boost::end(rng1),
  155. ::boost::begin(rng2), ::boost::end(rng2), pred);
  156. }
  157. /// \overload
  158. template< class SinglePassRange1, class SinglePassRange2, class BinaryPredicate >
  159. inline std::pair<
  160. BOOST_DEDUCED_TYPENAME range_iterator<const SinglePassRange1>::type,
  161. BOOST_DEDUCED_TYPENAME range_iterator<SinglePassRange2>::type >
  162. mismatch(const SinglePassRange1& rng1, SinglePassRange2& rng2, BinaryPredicate pred)
  163. {
  164. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<const SinglePassRange1> ));
  165. BOOST_RANGE_CONCEPT_ASSERT(( SinglePassRangeConcept<SinglePassRange2> ));
  166. return ::boost::range_detail::mismatch_impl(
  167. ::boost::begin(rng1), ::boost::end(rng1),
  168. ::boost::begin(rng2), ::boost::end(rng2), pred);
  169. }
  170. } // namespace range
  171. using range::mismatch;
  172. } // namespace boost
  173. #endif // include guard