equal.hpp 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 <algorithm> // for std::equal
  12. #include <functional> // for std::equal_to
  13. namespace boost { namespace algorithm {
  14. namespace detail {
  15. template <class T1, class T2>
  16. struct eq : public std::binary_function<T1, T2, bool> {
  17. bool operator () ( const T1& v1, const T2& v2 ) const { return v1 == v2 ;}
  18. };
  19. template <class RandomAccessIterator1, class RandomAccessIterator2, class BinaryPredicate>
  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. // If we know that the sequences are the same size, the original version is fine
  28. return std::equal ( first1, last1, first2, pred );
  29. }
  30. template <class InputIterator1, class InputIterator2, class BinaryPredicate>
  31. bool equal ( InputIterator1 first1, InputIterator1 last1,
  32. InputIterator2 first2, InputIterator2 last2, BinaryPredicate pred,
  33. std::input_iterator_tag, std::input_iterator_tag )
  34. {
  35. for (; first1 != last1 && first2 != last2; ++first1, ++first2 )
  36. if ( !pred(*first1, *first2 ))
  37. return false;
  38. return first1 == last1 && first2 == last2;
  39. }
  40. }
  41. /// \fn equal ( InputIterator1 first1, InputIterator1 last1,
  42. /// InputIterator2 first2, InputIterator2 last2,
  43. /// BinaryPredicate pred )
  44. /// \return true if all elements in the two ranges are equal
  45. ///
  46. /// \param first1 The start of the first range.
  47. /// \param last1 One past the end of the first range.
  48. /// \param first2 The start of the second range.
  49. /// \param last2 One past the end of the second range.
  50. /// \param pred A predicate for comparing the elements of the ranges
  51. template <class InputIterator1, class InputIterator2, class BinaryPredicate>
  52. bool equal ( InputIterator1 first1, InputIterator1 last1,
  53. InputIterator2 first2, InputIterator2 last2, BinaryPredicate pred )
  54. {
  55. return boost::algorithm::detail::equal (
  56. first1, last1, first2, last2, pred,
  57. typename std::iterator_traits<InputIterator1>::iterator_category (),
  58. typename std::iterator_traits<InputIterator2>::iterator_category ());
  59. }
  60. /// \fn equal ( InputIterator1 first1, InputIterator1 last1,
  61. /// InputIterator2 first2, InputIterator2 last2 )
  62. /// \return true if all elements in the two ranges are equal
  63. ///
  64. /// \param first1 The start of the first range.
  65. /// \param last1 One past the end of the first range.
  66. /// \param first2 The start of the second range.
  67. /// \param last2 One past the end of the second range.
  68. template <class InputIterator1, class InputIterator2>
  69. bool equal ( InputIterator1 first1, InputIterator1 last1,
  70. InputIterator2 first2, InputIterator2 last2 )
  71. {
  72. return boost::algorithm::detail::equal (
  73. first1, last1, first2, last2,
  74. boost::algorithm::detail::eq<
  75. typename std::iterator_traits<InputIterator1>::value_type,
  76. typename std::iterator_traits<InputIterator2>::value_type> (),
  77. typename std::iterator_traits<InputIterator1>::iterator_category (),
  78. typename std::iterator_traits<InputIterator2>::iterator_category ());
  79. }
  80. // There are already range-based versions of these.
  81. }} // namespace boost and algorithm
  82. #endif // BOOST_ALGORITHM_EQUAL_HPP