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