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