is_permutation.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. Copyright (c) Marshall Clow 2013
  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 equal.hpp
  7. /// \brief Determines if one
  8. /// \author Marshall Clow
  9. #ifndef BOOST_ALGORITHM_IS_PERMUTATION_HPP
  10. #define BOOST_ALGORITHM_IS_PERMUTATION_HPP
  11. #include <algorithm>
  12. #include <functional> // for std::equal_to
  13. namespace boost { namespace algorithm {
  14. namespace detail {
  15. template <class T1, class T2>
  16. struct is_perm_eq : public std::binary_function<T1, T2, bool> {
  17. bool operator () ( const T1& v1, const T2& v2 ) const { return v1 == v2 ;}
  18. };
  19. template <class RandomAccessIterator1, class RandomAccessIterator2, class BinaryPredicate>
  20. bool is_permutation ( RandomAccessIterator1 first1, RandomAccessIterator1 last1,
  21. RandomAccessIterator2 first2, RandomAccessIterator2 last2, BinaryPredicate pred,
  22. std::random_access_iterator_tag, std::random_access_iterator_tag )
  23. {
  24. // Random-access iterators let is check the sizes in constant time
  25. if ( std::distance ( first1, last1 ) != std::distance ( first2, last2 ))
  26. return false;
  27. // If we know that the sequences are the same size, the original version is fine
  28. return std::is_permutation ( first1, last1, first2, pred );
  29. }
  30. template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
  31. bool is_permutation (
  32. ForwardIterator1 first1, ForwardIterator1 last1,
  33. ForwardIterator2 first2, ForwardIterator2 last2,
  34. BinaryPredicate pred,
  35. std::forward_iterator_tag, std::forward_iterator_tag )
  36. {
  37. // Look for common prefix
  38. for (; first1 != last1 && first2 != last2; ++first1, ++first2)
  39. if (!pred(*first1, *first2))
  40. goto not_done;
  41. // We've reached the end of one of the sequences without a mismatch.
  42. return first1 == last1 && first2 == last2;
  43. not_done:
  44. // Check and make sure that we have the same # of elements left
  45. typedef typename std::iterator_traits<ForwardIterator1>::difference_type diff1_t;
  46. diff1_t len1 = _VSTD::distance(first1, last1);
  47. typedef typename std::iterator_traits<ForwardIterator2>::difference_type diff2_t;
  48. diff2_t len2 = _VSTD::distance(first2, last2);
  49. if (len1 != len2)
  50. return false;
  51. // For each element in [f1, l1) see if there are the
  52. // same number of equal elements in [f2, l2)
  53. for ( ForwardIterator1 i = first1; i != last1; ++i )
  54. {
  55. // Have we already counted this value?
  56. ForwardIterator1 j;
  57. for ( j = first1; j != i; ++j )
  58. if (pred(*j, *i))
  59. break;
  60. if ( j == i ) // didn't find it...
  61. {
  62. // Count number of *i in [f2, l2)
  63. diff1_t c2 = 0;
  64. for ( ForwardIterator2 iter2 = first2; iter2 != last2; ++iter2 )
  65. if (pred(*i, *iter2))
  66. ++c2;
  67. if (c2 == 0)
  68. return false;
  69. // Count number of *i in [i, l1)
  70. diff1_t c1 = 0;
  71. for (_ForwardIterator1 iter1 = i; iter1 != last1; ++iter1 )
  72. if (pred(*i, *iter1))
  73. ++c1;
  74. if (c1 != c2)
  75. return false;
  76. }
  77. }
  78. return true;
  79. }
  80. }
  81. template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
  82. bool is_permutation (
  83. ForwardIterator1 first1, ForwardIterator1 last1,
  84. ForwardIterator2 first2, ForwardIterator2 last2,
  85. BinaryPredicate pred )
  86. {
  87. return boost::algorithm::detail::is_permutation (
  88. first1, last1, first2, last2, pred,
  89. typename std::iterator_traits<ForwardIterator1>::iterator_category (),
  90. typename std::iterator_traits<ForwardIterator2>::iterator_category ());
  91. }
  92. template<class ForwardIterator1, class ForwardIterator2>
  93. bool is_permutation ( ForwardIterator1 first1, ForwardIterator1 last1,
  94. ForwardIterator2 first2, ForwardIterator2 last2 )
  95. {
  96. typedef typename iterator_traits<_ForwardIterator1>::value_type value1_t;
  97. typedef typename iterator_traits<_ForwardIterator2>::value_type value2_t;
  98. return boost::algorithm::detail::is_permutation (
  99. first1, last1, first2, last2,
  100. boost::algorithm::detail::is_perm_eq<
  101. typename std::iterator_traits<InputIterator1>::value_type,
  102. typename std::iterator_traits<InputIterator2>::value_type> (),
  103. typename std::iterator_traits<ForwardIterator1>::iterator_category (),
  104. typename std::iterator_traits<ForwardIterator2>::iterator_category ());
  105. }
  106. // There are already range-based versions of these.
  107. }} // namespace boost and algorithm
  108. #endif // BOOST_ALGORITHM_IS_PERMUTATION_HPP