2 Copyright (c) Marshall Clow 2008-2012.
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)
9 /// \brief Test ranges to if they are equal
10 /// \author Marshall Clow
12 #ifndef BOOST_ALGORITHM_EQUAL_HPP
13 #define BOOST_ALGORITHM_EQUAL_HPP
15 #include <algorithm> // for std::equal
16 #include <functional> // for std::binary_function
19 namespace boost { namespace algorithm {
23 template <class T1, class T2>
24 struct eq : public std::binary_function<T1, T2, bool> {
25 bool operator () ( const T1& v1, const T2& v2 ) const { return v1 == v2 ;}
28 template <class RandomAccessIterator1, class RandomAccessIterator2, class BinaryPredicate>
29 bool equal ( RandomAccessIterator1 first1, RandomAccessIterator1 last1,
30 RandomAccessIterator2 first2, RandomAccessIterator2 last2, BinaryPredicate pred,
31 std::random_access_iterator_tag, std::random_access_iterator_tag )
33 // Random-access iterators let is check the sizes in constant time
34 if ( std::distance ( first1, last1 ) != std::distance ( first2, last2 ))
36 // If we know that the sequences are the same size, the original version is fine
37 return std::equal ( first1, last1, first2, pred );
40 template <class InputIterator1, class InputIterator2, class BinaryPredicate>
41 bool equal ( InputIterator1 first1, InputIterator1 last1,
42 InputIterator2 first2, InputIterator2 last2, BinaryPredicate pred,
43 std::input_iterator_tag, std::input_iterator_tag )
45 for (; first1 != last1 && first2 != last2; ++first1, ++first2 )
46 if ( !pred(*first1, *first2 ))
49 return first1 == last1 && first2 == last2;
53 /// \fn equal ( InputIterator1 first1, InputIterator1 last1,
54 /// InputIterator2 first2, InputIterator2 last2,
55 /// BinaryPredicate pred )
56 /// \return true if all elements in the two ranges are equal
58 /// \param first1 The start of the first range.
59 /// \param last1 One past the end of the first range.
60 /// \param first2 The start of the second range.
61 /// \param last2 One past the end of the second range.
62 /// \param pred A predicate for comparing the elements of the ranges
63 template <class InputIterator1, class InputIterator2, class BinaryPredicate>
64 bool equal ( InputIterator1 first1, InputIterator1 last1,
65 InputIterator2 first2, InputIterator2 last2, BinaryPredicate pred )
67 return boost::algorithm::detail::equal (
68 first1, last1, first2, last2, pred,
69 typename std::iterator_traits<InputIterator1>::iterator_category (),
70 typename std::iterator_traits<InputIterator2>::iterator_category ());
73 /// \fn equal ( InputIterator1 first1, InputIterator1 last1,
74 /// InputIterator2 first2, InputIterator2 last2 )
75 /// \return true if all elements in the two ranges are equal
77 /// \param first1 The start of the first range.
78 /// \param last1 One past the end of the first range.
79 /// \param first2 The start of the second range.
80 /// \param last2 One past the end of the second range.
81 template <class InputIterator1, class InputIterator2>
82 bool equal ( InputIterator1 first1, InputIterator1 last1,
83 InputIterator2 first2, InputIterator2 last2 )
85 return boost::algorithm::detail::equal (
86 first1, last1, first2, last2,
87 boost::algorithm::detail::eq<
88 typename std::iterator_traits<InputIterator1>::value_type,
89 typename std::iterator_traits<InputIterator2>::value_type> (),
90 typename std::iterator_traits<InputIterator1>::iterator_category (),
91 typename std::iterator_traits<InputIterator2>::iterator_category ());
94 // There are already range-based versions of these.
96 }} // namespace boost and algorithm
98 #endif // BOOST_ALGORITHM_EQUAL_HPP