]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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::binary_function | |
17 | #include <iterator> | |
18 | ||
19 | namespace boost { namespace algorithm { | |
20 | ||
21 | namespace detail { | |
22 | ||
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 ;} | |
26 | }; | |
27 | ||
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 ) | |
32 | { | |
33 | // Random-access iterators let is check the sizes in constant time | |
34 | if ( std::distance ( first1, last1 ) != std::distance ( first2, last2 )) | |
35 | return false; | |
36 | // If we know that the sequences are the same size, the original version is fine | |
37 | return std::equal ( first1, last1, first2, pred ); | |
38 | } | |
39 | ||
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 ) | |
44 | { | |
45 | for (; first1 != last1 && first2 != last2; ++first1, ++first2 ) | |
46 | if ( !pred(*first1, *first2 )) | |
47 | return false; | |
48 | ||
49 | return first1 == last1 && first2 == last2; | |
50 | } | |
51 | } | |
52 | ||
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 | |
57 | /// | |
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 ) | |
66 | { | |
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 ()); | |
71 | } | |
72 | ||
73 | /// \fn equal ( InputIterator1 first1, InputIterator1 last1, | |
74 | /// InputIterator2 first2, InputIterator2 last2 ) | |
75 | /// \return true if all elements in the two ranges are equal | |
76 | /// | |
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 ) | |
84 | { | |
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 ()); | |
92 | } | |
93 | ||
94 | // There are already range-based versions of these. | |
95 | ||
96 | }} // namespace boost and algorithm | |
97 | ||
98 | #endif // BOOST_ALGORITHM_EQUAL_HPP |