Chris@16: /* Chris@16: Copyright (c) Marshall Clow 2008-2012. Chris@16: Chris@16: Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: */ Chris@16: Chris@16: /// \file equal.hpp Chris@16: /// \brief Test ranges to if they are equal Chris@16: /// \author Marshall Clow Chris@16: Chris@16: #ifndef BOOST_ALGORITHM_EQUAL_HPP Chris@16: #define BOOST_ALGORITHM_EQUAL_HPP Chris@16: Chris@16: #include // for std::equal Chris@16: #include // for std::equal_to Chris@16: Chris@16: namespace boost { namespace algorithm { Chris@16: Chris@16: namespace detail { Chris@16: Chris@16: template Chris@16: struct eq : public std::binary_function { Chris@16: bool operator () ( const T1& v1, const T2& v2 ) const { return v1 == v2 ;} Chris@16: }; Chris@16: Chris@16: template Chris@16: bool equal ( RandomAccessIterator1 first1, RandomAccessIterator1 last1, Chris@16: RandomAccessIterator2 first2, RandomAccessIterator2 last2, BinaryPredicate pred, Chris@16: std::random_access_iterator_tag, std::random_access_iterator_tag ) Chris@16: { Chris@16: // Random-access iterators let is check the sizes in constant time Chris@16: if ( std::distance ( first1, last1 ) != std::distance ( first2, last2 )) Chris@16: return false; Chris@16: // If we know that the sequences are the same size, the original version is fine Chris@16: return std::equal ( first1, last1, first2, pred ); Chris@16: } Chris@16: Chris@16: template Chris@16: bool equal ( InputIterator1 first1, InputIterator1 last1, Chris@16: InputIterator2 first2, InputIterator2 last2, BinaryPredicate pred, Chris@16: std::input_iterator_tag, std::input_iterator_tag ) Chris@16: { Chris@16: for (; first1 != last1 && first2 != last2; ++first1, ++first2 ) Chris@16: if ( !pred(*first1, *first2 )) Chris@16: return false; Chris@16: Chris@16: return first1 == last1 && first2 == last2; Chris@16: } Chris@16: } Chris@16: Chris@16: /// \fn equal ( InputIterator1 first1, InputIterator1 last1, Chris@16: /// InputIterator2 first2, InputIterator2 last2, Chris@16: /// BinaryPredicate pred ) Chris@16: /// \return true if all elements in the two ranges are equal Chris@16: /// Chris@16: /// \param first1 The start of the first range. Chris@16: /// \param last1 One past the end of the first range. Chris@16: /// \param first2 The start of the second range. Chris@16: /// \param last2 One past the end of the second range. Chris@16: /// \param pred A predicate for comparing the elements of the ranges Chris@16: template Chris@16: bool equal ( InputIterator1 first1, InputIterator1 last1, Chris@16: InputIterator2 first2, InputIterator2 last2, BinaryPredicate pred ) Chris@16: { Chris@16: return boost::algorithm::detail::equal ( Chris@16: first1, last1, first2, last2, pred, Chris@16: typename std::iterator_traits::iterator_category (), Chris@16: typename std::iterator_traits::iterator_category ()); Chris@16: } Chris@16: Chris@16: /// \fn equal ( InputIterator1 first1, InputIterator1 last1, Chris@16: /// InputIterator2 first2, InputIterator2 last2 ) Chris@16: /// \return true if all elements in the two ranges are equal Chris@16: /// Chris@16: /// \param first1 The start of the first range. Chris@16: /// \param last1 One past the end of the first range. Chris@16: /// \param first2 The start of the second range. Chris@16: /// \param last2 One past the end of the second range. Chris@16: template Chris@16: bool equal ( InputIterator1 first1, InputIterator1 last1, Chris@16: InputIterator2 first2, InputIterator2 last2 ) Chris@16: { Chris@16: return boost::algorithm::detail::equal ( Chris@16: first1, last1, first2, last2, Chris@16: boost::algorithm::detail::eq< Chris@16: typename std::iterator_traits::value_type, Chris@16: typename std::iterator_traits::value_type> (), Chris@16: typename std::iterator_traits::iterator_category (), Chris@16: typename std::iterator_traits::iterator_category ()); Chris@16: } Chris@16: Chris@16: // There are already range-based versions of these. Chris@16: Chris@16: }} // namespace boost and algorithm Chris@16: Chris@16: #endif // BOOST_ALGORITHM_EQUAL_HPP