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