is_permutation.hpp 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. Copyright (c) Marshall Clow 2014.
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. */
  6. /// \file is_permutation.hpp
  7. /// \brief Is a sequence a permutation of another sequence (four iterator versions)
  8. /// \author Marshall Clow
  9. #ifndef BOOST_ALGORITHM_IS_PERMUTATION14_HPP
  10. #define BOOST_ALGORITHM_IS_PERMUTATION14_HPP
  11. #include <utility> // for std::pair
  12. #include <functional> // for std::equal_to
  13. #include <iterator>
  14. #include <boost/algorithm/cxx11/is_permutation.hpp>
  15. #include <boost/algorithm/cxx14/mismatch.hpp>
  16. namespace boost { namespace algorithm {
  17. /// \fn is_permutation ( ForwardIterator1 first, ForwardIterator1 last,
  18. /// ForwardIterator2 first2, ForwardIterator2 last2 )
  19. /// \brief Tests to see if the sequence [first,last) is a permutation of the sequence starting at first2
  20. ///
  21. /// \param first1 The start of the input sequence
  22. /// \param last2 One past the end of the input sequence
  23. /// \param first2 The start of the second sequence
  24. /// \param last1 One past the end of the second sequence
  25. /// \note This function is part of the C++2014 standard library.
  26. template< class ForwardIterator1, class ForwardIterator2 >
  27. bool is_permutation ( ForwardIterator1 first1, ForwardIterator1 last1,
  28. ForwardIterator2 first2, ForwardIterator2 last2 )
  29. {
  30. // How should I deal with the idea that ForwardIterator1::value_type
  31. // and ForwardIterator2::value_type could be different? Define my own comparison predicate?
  32. std::pair<ForwardIterator1, ForwardIterator2> eq = boost::algorithm::mismatch
  33. ( first1, last1, first2, last2 );
  34. if ( eq.first == last1 && eq.second == last2)
  35. return true;
  36. return boost::algorithm::detail::is_permutation_tag (
  37. eq.first, last1, eq.second, last2,
  38. std::equal_to<typename std::iterator_traits<ForwardIterator1>::value_type> (),
  39. typename std::iterator_traits<ForwardIterator1>::iterator_category (),
  40. typename std::iterator_traits<ForwardIterator2>::iterator_category ());
  41. }
  42. /// \fn is_permutation ( ForwardIterator1 first, ForwardIterator1 last,
  43. /// ForwardIterator2 first2, ForwardIterator2 last2,
  44. /// BinaryPredicate p )
  45. /// \brief Tests to see if the sequence [first,last) is a permutation of the sequence starting at first2
  46. ///
  47. /// \param first1 The start of the input sequence
  48. /// \param last1 One past the end of the input sequence
  49. /// \param first2 The start of the second sequence
  50. /// \param last2 One past the end of the second sequence
  51. /// \param pred The predicate to compare elements with
  52. ///
  53. /// \note This function is part of the C++2014 standard library.
  54. template< class ForwardIterator1, class ForwardIterator2, class BinaryPredicate >
  55. bool is_permutation ( ForwardIterator1 first1, ForwardIterator1 last1,
  56. ForwardIterator2 first2, ForwardIterator2 last2,
  57. BinaryPredicate pred )
  58. {
  59. std::pair<ForwardIterator1, ForwardIterator2> eq = boost::algorithm::mismatch
  60. ( first1, last1, first2, last2, pred );
  61. if ( eq.first == last1 && eq.second == last2)
  62. return true;
  63. return boost::algorithm::detail::is_permutation_tag (
  64. first1, last1, first2, last2, pred,
  65. typename std::iterator_traits<ForwardIterator1>::iterator_category (),
  66. typename std::iterator_traits<ForwardIterator2>::iterator_category ());
  67. }
  68. }}
  69. #endif // BOOST_ALGORITHM_IS_PERMUTATION14_HPP