From 0f638838189fdc4ea01967a8ecbb27302195b279 Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Sun, 23 Mar 2014 14:56:48 -0700 Subject: [PATCH] Rework the file layout and implementation of 'is_permutation' to match the various versions of the C++ standard. In particular, move the four iterator versions into the cxx14/ directory. --- .../boost/algorithm/cxx11/is_permutation.hpp | 58 +------------ .../boost/algorithm/cxx14/is_permutation.hpp | 86 +++++++++++++++++++ test/is_permutation_test1.cpp | 1 + 3 files changed, 90 insertions(+), 55 deletions(-) create mode 100644 include/boost/algorithm/cxx14/is_permutation.hpp diff --git a/include/boost/algorithm/cxx11/is_permutation.hpp b/include/boost/algorithm/cxx11/is_permutation.hpp index ac3eb82..9b3bc22 100644 --- a/include/boost/algorithm/cxx11/is_permutation.hpp +++ b/include/boost/algorithm/cxx11/is_permutation.hpp @@ -9,8 +9,8 @@ /// \brief Is a sequence a permutation of another sequence /// \author Marshall Clow -#ifndef BOOST_ALGORITHM_IS_PERMUTATION_HPP -#define BOOST_ALGORITHM_IS_PERMUTATION_HPP +#ifndef BOOST_ALGORITHM_IS_PERMUTATION11_HPP +#define BOOST_ALGORITHM_IS_PERMUTATION11_HPP #include // for std::less, tie, mismatch and is_permutation (if available) #include // for std::make_pair @@ -163,58 +163,6 @@ bool is_permutation ( ForwardIterator1 first1, ForwardIterator1 last1, ForwardIt #endif -#if __cplusplus <= 201103L -/// \fn is_permutation ( ForwardIterator1 first, ForwardIterator1 last, -/// ForwardIterator2 first2, ForwardIterator2 last2 ) -/// \brief Tests to see if the sequence [first,last) is a permutation of the sequence starting at first2 -/// -/// \param first1 The start of the input sequence -/// \param last2 One past the end of the input sequence -/// \param first2 The start of the second sequence -/// \param last1 One past the end of the second sequence -/// \note This function is part of the C++2014 standard library. -/// We will use the standard one if it is available, -/// otherwise we have our own implementation. -template< class ForwardIterator1, class ForwardIterator2 > -bool is_permutation ( ForwardIterator1 first1, ForwardIterator1 last1, - ForwardIterator2 first2, ForwardIterator2 last2 ) -{ -// How should I deal with the idea that ForwardIterator1::value_type -// and ForwardIterator2::value_type could be different? Define my own comparison predicate? - return boost::algorithm::detail::is_permutation_tag ( - first1, last1, first2, last2, - std::equal_to::value_type> (), - typename std::iterator_traits::iterator_category (), - typename std::iterator_traits::iterator_category ()); -} - -/// \fn is_permutation ( ForwardIterator1 first, ForwardIterator1 last, -/// ForwardIterator2 first2, ForwardIterator2 last2, -/// BinaryPredicate p ) -/// \brief Tests to see if the sequence [first,last) is a permutation of the sequence starting at first2 -/// -/// \param first1 The start of the input sequence -/// \param last1 One past the end of the input sequence -/// \param first2 The start of the second sequence -/// \param last2 One past the end of the second sequence -/// \param pred The predicate to compare elements with -/// -/// \note This function is part of the C++2014 standard library. -/// We will use the standard one if it is available, -/// otherwise we have our own implementation. -template< class ForwardIterator1, class ForwardIterator2, class BinaryPredicate > -bool is_permutation ( ForwardIterator1 first1, ForwardIterator1 last1, - ForwardIterator2 first2, ForwardIterator2 last2, - BinaryPredicate pred ) -{ - return boost::algorithm::detail::is_permutation_tag ( - first1, last1, first2, last2, pred, - typename std::iterator_traits::iterator_category (), - typename std::iterator_traits::iterator_category ()); -} -#endif - - /// \fn is_permutation ( const Range &r, ForwardIterator first2 ) /// \brief Tests to see if the sequence [first,last) is a permutation of the sequence starting at first2 @@ -245,4 +193,4 @@ is_permutation ( const Range &r, ForwardIterator first2, BinaryPredicate pred ) }} -#endif // BOOST_ALGORITHM_IS_PERMUTATION_HPP +#endif // BOOST_ALGORITHM_IS_PERMUTATION11_HPP diff --git a/include/boost/algorithm/cxx14/is_permutation.hpp b/include/boost/algorithm/cxx14/is_permutation.hpp new file mode 100644 index 0000000..d8002cb --- /dev/null +++ b/include/boost/algorithm/cxx14/is_permutation.hpp @@ -0,0 +1,86 @@ +/* + Copyright (c) Marshall Clow 2014. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +*/ + +/// \file is_permutation.hpp +/// \brief Is a sequence a permutation of another sequence (four iterator versions) +/// \author Marshall Clow + +#ifndef BOOST_ALGORITHM_IS_PERMUTATION14_HPP +#define BOOST_ALGORITHM_IS_PERMUTATION14_HPP + +#include // for std::less, tie, mismatch and is_permutation (if available) +#include // for std::make_pair +#include // for std::equal_to +#include + +#include +#include + +namespace boost { namespace algorithm { + +#if __cplusplus <= 201103L +/// \fn is_permutation ( ForwardIterator1 first, ForwardIterator1 last, +/// ForwardIterator2 first2, ForwardIterator2 last2 ) +/// \brief Tests to see if the sequence [first,last) is a permutation of the sequence starting at first2 +/// +/// \param first1 The start of the input sequence +/// \param last2 One past the end of the input sequence +/// \param first2 The start of the second sequence +/// \param last1 One past the end of the second sequence +/// \note This function is part of the C++2014 standard library. +/// We will use the standard one if it is available, +/// otherwise we have our own implementation. +template< class ForwardIterator1, class ForwardIterator2 > +bool is_permutation ( ForwardIterator1 first1, ForwardIterator1 last1, + ForwardIterator2 first2, ForwardIterator2 last2 ) +{ +// How should I deal with the idea that ForwardIterator1::value_type +// and ForwardIterator2::value_type could be different? Define my own comparison predicate? + std::pair eq = boost::algorithm::mismatch + ( first1, last1, first2, last2 ); + if ( eq.first == last1 && eq.second == last2) + return true; + return boost::algorithm::detail::is_permutation_tag ( + eq.first, last1, eq.second, last2, + std::equal_to::value_type> (), + typename std::iterator_traits::iterator_category (), + typename std::iterator_traits::iterator_category ()); +} + +/// \fn is_permutation ( ForwardIterator1 first, ForwardIterator1 last, +/// ForwardIterator2 first2, ForwardIterator2 last2, +/// BinaryPredicate p ) +/// \brief Tests to see if the sequence [first,last) is a permutation of the sequence starting at first2 +/// +/// \param first1 The start of the input sequence +/// \param last1 One past the end of the input sequence +/// \param first2 The start of the second sequence +/// \param last2 One past the end of the second sequence +/// \param pred The predicate to compare elements with +/// +/// \note This function is part of the C++2014 standard library. +/// We will use the standard one if it is available, +/// otherwise we have our own implementation. +template< class ForwardIterator1, class ForwardIterator2, class BinaryPredicate > +bool is_permutation ( ForwardIterator1 first1, ForwardIterator1 last1, + ForwardIterator2 first2, ForwardIterator2 last2, + BinaryPredicate pred ) +{ + std::pair eq = boost::algorithm::mismatch + ( first1, last1, first2, last2, pred ); + if ( eq.first == last1 && eq.second == last2) + return true; + return boost::algorithm::detail::is_permutation_tag ( + first1, last1, first2, last2, pred, + typename std::iterator_traits::iterator_category (), + typename std::iterator_traits::iterator_category ()); +} +#endif + +}} + +#endif // BOOST_ALGORITHM_IS_PERMUTATION14_HPP diff --git a/test/is_permutation_test1.cpp b/test/is_permutation_test1.cpp index e819643..2e1a12f 100644 --- a/test/is_permutation_test1.cpp +++ b/test/is_permutation_test1.cpp @@ -11,6 +11,7 @@ #include #include +#include #define BOOST_TEST_MAIN #include