equal.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. namespace boost { namespace algorithm {
  13. namespace detail {
  14. template <class T1, class T2>
  15. struct eq {
  16. BOOST_CONSTEXPR bool operator () ( const T1& v1, const T2& v2 ) const { return v1 == v2 ;}
  17. };
  18. template <class RandomAccessIterator1, class RandomAccessIterator2, class BinaryPredicate>
  19. BOOST_CXX14_CONSTEXPR
  20. bool equal ( RandomAccessIterator1 first1, RandomAccessIterator1 last1,
  21. RandomAccessIterator2 first2, RandomAccessIterator2 last2, BinaryPredicate pred,
  22. std::random_access_iterator_tag, std::random_access_iterator_tag )
  23. {
  24. // Random-access iterators let is check the sizes in constant time
  25. if ( std::distance ( first1, last1 ) != std::distance ( first2, last2 ))
  26. return false;
  27. // std::equal
  28. for (; first1 != last1; ++first1, ++first2)
  29. if (!pred(*first1, *first2))
  30. return false;
  31. return true;
  32. }
  33. template <class InputIterator1, class InputIterator2, class BinaryPredicate>
  34. BOOST_CXX14_CONSTEXPR
  35. bool equal ( InputIterator1 first1, InputIterator1 last1,
  36. InputIterator2 first2, InputIterator2 last2, BinaryPredicate pred,
  37. std::input_iterator_tag, std::input_iterator_tag )
  38. {
  39. for (; first1 != last1 && first2 != last2; ++first1, ++first2 )
  40. if ( !pred(*first1, *first2 ))
  41. return false;
  42. return first1 == last1 && first2 == last2;
  43. }
  44. }
  45. /// \fn equal ( InputIterator1 first1, InputIterator1 last1,
  46. /// InputIterator2 first2, InputIterator2 last2,
  47. /// BinaryPredicate pred )
  48. /// \return true if all elements in the two ranges are equal
  49. ///
  50. /// \param first1 The start of the first range.
  51. /// \param last1 One past the end of the first range.
  52. /// \param first2 The start of the second range.
  53. /// \param last2 One past the end of the second range.
  54. /// \param pred A predicate for comparing the elements of the ranges
  55. template <class InputIterator1, class InputIterator2, class BinaryPredicate>
  56. BOOST_CXX14_CONSTEXPR
  57. bool equal ( InputIterator1 first1, InputIterator1 last1,
  58. InputIterator2 first2, InputIterator2 last2, BinaryPredicate pred )
  59. {
  60. return boost::algorithm::detail::equal (
  61. first1, last1, first2, last2, pred,
  62. typename std::iterator_traits<InputIterator1>::iterator_category (),
  63. typename std::iterator_traits<InputIterator2>::iterator_category ());
  64. }
  65. /// \fn equal ( InputIterator1 first1, InputIterator1 last1,
  66. /// InputIterator2 first2, InputIterator2 last2 )
  67. /// \return true if all elements in the two ranges are equal
  68. ///
  69. /// \param first1 The start of the first range.
  70. /// \param last1 One past the end of the first range.
  71. /// \param first2 The start of the second range.
  72. /// \param last2 One past the end of the second range.
  73. template <class InputIterator1, class InputIterator2>
  74. BOOST_CXX14_CONSTEXPR
  75. bool equal ( InputIterator1 first1, InputIterator1 last1,
  76. InputIterator2 first2, InputIterator2 last2 )
  77. {
  78. return boost::algorithm::detail::equal (
  79. first1, last1, first2, last2,
  80. boost::algorithm::detail::eq<
  81. typename std::iterator_traits<InputIterator1>::value_type,
  82. typename std::iterator_traits<InputIterator2>::value_type> (),
  83. typename std::iterator_traits<InputIterator1>::iterator_category (),
  84. typename std::iterator_traits<InputIterator2>::iterator_category ());
  85. }
  86. // There are already range-based versions of these.
  87. }} // namespace boost and algorithm
  88. #endif // BOOST_ALGORITHM_EQUAL_HPP