equal.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. Copyright (c) Marshall Clow 2008-2012.
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. */
  6. /// \file equal.hpp
  7. /// \brief Test ranges to if they are equal
  8. /// \author Marshall Clow
  9. #ifndef BOOST_ALGORITHM_EQUAL_HPP
  10. #define BOOST_ALGORITHM_EQUAL_HPP
  11. #include <iterator>
  12. #include <boost/config.hpp>
  13. namespace boost { namespace algorithm {
  14. namespace detail {
  15. template <class T1, class T2>
  16. struct eq {
  17. BOOST_CONSTEXPR bool operator () ( const T1& v1, const T2& v2 ) const { return v1 == v2 ;}
  18. };
  19. template <class RandomAccessIterator1, class RandomAccessIterator2, class BinaryPredicate>
  20. BOOST_CXX14_CONSTEXPR
  21. bool equal ( RandomAccessIterator1 first1, RandomAccessIterator1 last1,
  22. RandomAccessIterator2 first2, RandomAccessIterator2 last2, BinaryPredicate pred,
  23. std::random_access_iterator_tag, std::random_access_iterator_tag )
  24. {
  25. // Random-access iterators let is check the sizes in constant time
  26. if ( std::distance ( first1, last1 ) != std::distance ( first2, last2 ))
  27. return false;
  28. // std::equal
  29. for (; first1 != last1; ++first1, ++first2)
  30. if (!pred(*first1, *first2))
  31. return false;
  32. return true;
  33. }
  34. template <class InputIterator1, class InputIterator2, class BinaryPredicate>
  35. BOOST_CXX14_CONSTEXPR
  36. bool equal ( InputIterator1 first1, InputIterator1 last1,
  37. InputIterator2 first2, InputIterator2 last2, BinaryPredicate pred,
  38. std::input_iterator_tag, std::input_iterator_tag )
  39. {
  40. for (; first1 != last1 && first2 != last2; ++first1, ++first2 )
  41. if ( !pred(*first1, *first2 ))
  42. return false;
  43. return first1 == last1 && first2 == last2;
  44. }
  45. }
  46. /// \fn equal ( InputIterator1 first1, InputIterator1 last1,
  47. /// InputIterator2 first2, InputIterator2 last2,
  48. /// BinaryPredicate pred )
  49. /// \return true if all elements in the two ranges are equal
  50. ///
  51. /// \param first1 The start of the first range.
  52. /// \param last1 One past the end of the first range.
  53. /// \param first2 The start of the second range.
  54. /// \param last2 One past the end of the second range.
  55. /// \param pred A predicate for comparing the elements of the ranges
  56. template <class InputIterator1, class InputIterator2, class BinaryPredicate>
  57. BOOST_CXX14_CONSTEXPR
  58. bool equal ( InputIterator1 first1, InputIterator1 last1,
  59. InputIterator2 first2, InputIterator2 last2, BinaryPredicate pred )
  60. {
  61. return boost::algorithm::detail::equal (
  62. first1, last1, first2, last2, pred,
  63. typename std::iterator_traits<InputIterator1>::iterator_category (),
  64. typename std::iterator_traits<InputIterator2>::iterator_category ());
  65. }
  66. /// \fn equal ( InputIterator1 first1, InputIterator1 last1,
  67. /// InputIterator2 first2, InputIterator2 last2 )
  68. /// \return true if all elements in the two ranges are equal
  69. ///
  70. /// \param first1 The start of the first range.
  71. /// \param last1 One past the end of the first range.
  72. /// \param first2 The start of the second range.
  73. /// \param last2 One past the end of the second range.
  74. template <class InputIterator1, class InputIterator2>
  75. BOOST_CXX14_CONSTEXPR
  76. bool equal ( InputIterator1 first1, InputIterator1 last1,
  77. InputIterator2 first2, InputIterator2 last2 )
  78. {
  79. return boost::algorithm::detail::equal (
  80. first1, last1, first2, last2,
  81. boost::algorithm::detail::eq<
  82. typename std::iterator_traits<InputIterator1>::value_type,
  83. typename std::iterator_traits<InputIterator2>::value_type> (),
  84. typename std::iterator_traits<InputIterator1>::iterator_category (),
  85. typename std::iterator_traits<InputIterator2>::iterator_category ());
  86. }
  87. // There are already range-based versions of these.
  88. }} // namespace boost and algorithm
  89. #endif // BOOST_ALGORITHM_EQUAL_HPP