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/config.hpp>
  15. #include <boost/algorithm/cxx11/is_permutation.hpp>
  16. #include <boost/algorithm/cxx14/mismatch.hpp>
  17. namespace boost { namespace algorithm {
  18. /// \fn is_permutation ( ForwardIterator1 first, ForwardIterator1 last,
  19. /// ForwardIterator2 first2, ForwardIterator2 last2 )
  20. /// \brief Tests to see if the sequence [first,last) is a permutation of the sequence starting at first2
  21. ///
  22. /// \param first1 The start of the input sequence
  23. /// \param last2 One past the end of the input sequence
  24. /// \param first2 The start of the second sequence
  25. /// \param last1 One past the end of the second sequence
  26. /// \note This function is part of the C++2014 standard library.
  27. template< class ForwardIterator1, class ForwardIterator2 >
  28. bool is_permutation ( ForwardIterator1 first1, ForwardIterator1 last1,
  29. ForwardIterator2 first2, ForwardIterator2 last2 )
  30. {
  31. // How should I deal with the idea that ForwardIterator1::value_type
  32. // and ForwardIterator2::value_type could be different? Define my own comparison predicate?
  33. std::pair<ForwardIterator1, ForwardIterator2> eq = boost::algorithm::mismatch
  34. ( first1, last1, first2, last2 );
  35. if ( eq.first == last1 && eq.second == last2)
  36. return true;
  37. return boost::algorithm::detail::is_permutation_tag (
  38. eq.first, last1, eq.second, last2,
  39. std::equal_to<typename std::iterator_traits<ForwardIterator1>::value_type> (),
  40. typename std::iterator_traits<ForwardIterator1>::iterator_category (),
  41. typename std::iterator_traits<ForwardIterator2>::iterator_category ());
  42. }
  43. /// \fn is_permutation ( ForwardIterator1 first, ForwardIterator1 last,
  44. /// ForwardIterator2 first2, ForwardIterator2 last2,
  45. /// BinaryPredicate p )
  46. /// \brief Tests to see if the sequence [first,last) is a permutation of the sequence starting at first2
  47. ///
  48. /// \param first1 The start of the input sequence
  49. /// \param last1 One past the end of the input sequence
  50. /// \param first2 The start of the second sequence
  51. /// \param last2 One past the end of the second sequence
  52. /// \param pred The predicate to compare elements with
  53. ///
  54. /// \note This function is part of the C++2014 standard library.
  55. template< class ForwardIterator1, class ForwardIterator2, class BinaryPredicate >
  56. bool is_permutation ( ForwardIterator1 first1, ForwardIterator1 last1,
  57. ForwardIterator2 first2, ForwardIterator2 last2,
  58. BinaryPredicate pred )
  59. {
  60. std::pair<ForwardIterator1, ForwardIterator2> eq = boost::algorithm::mismatch
  61. ( first1, last1, first2, last2, pred );
  62. if ( eq.first == last1 && eq.second == last2)
  63. return true;
  64. return boost::algorithm::detail::is_permutation_tag (
  65. first1, last1, first2, last2, pred,
  66. typename std::iterator_traits<ForwardIterator1>::iterator_category (),
  67. typename std::iterator_traits<ForwardIterator2>::iterator_category ());
  68. }
  69. }}
  70. #endif // BOOST_ALGORITHM_IS_PERMUTATION14_HPP