2012-04-01 13:15:09 +00:00
|
|
|
#ifndef SPROUT_ALGORITHM_IS_PERMUTATION_HPP
|
|
|
|
#define SPROUT_ALGORITHM_IS_PERMUTATION_HPP
|
|
|
|
|
2012-12-11 16:47:14 +00:00
|
|
|
#include <iterator>
|
2012-04-01 13:15:09 +00:00
|
|
|
#include <sprout/config.hpp>
|
|
|
|
#include <sprout/iterator/operation.hpp>
|
2012-12-21 14:12:54 +00:00
|
|
|
#include <sprout/iterator/type_traits/common.hpp>
|
2013-01-03 08:01:50 +00:00
|
|
|
#include <sprout/utility/pair.hpp>
|
2012-12-11 16:47:14 +00:00
|
|
|
#include <sprout/functional/equal_to.hpp>
|
|
|
|
#include <sprout/functional/bind2nd.hpp>
|
2012-04-01 13:15:09 +00:00
|
|
|
#include <sprout/algorithm/count_if.hpp>
|
2013-01-03 08:01:50 +00:00
|
|
|
#include <sprout/detail/algorithm/count_n_if.hpp>
|
2012-04-01 13:15:09 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
namespace detail {
|
2012-12-11 16:47:14 +00:00
|
|
|
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename BinaryPredicate>
|
2012-10-06 04:53:07 +00:00
|
|
|
inline SPROUT_CONSTEXPR bool
|
2012-12-11 16:47:14 +00:00
|
|
|
is_permutation_impl_ra(
|
|
|
|
RandomAccessIterator1 first1, RandomAccessIterator1 last1,
|
2013-01-03 08:01:50 +00:00
|
|
|
RandomAccessIterator2 first2, typename std::iterator_traits<RandomAccessIterator2>::difference_type d2,
|
2012-12-11 16:47:14 +00:00
|
|
|
BinaryPredicate pred,
|
|
|
|
typename std::iterator_traits<RandomAccessIterator1>::difference_type pivot
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return pivot == 0 ? sprout::count_if(first1, last1, sprout::bind2nd(pred, *first1))
|
2013-01-03 08:01:50 +00:00
|
|
|
<= sprout::detail::count_n_if(first2, d2, sprout::bind2nd(pred, *first1))
|
2012-12-11 16:47:14 +00:00
|
|
|
: sprout::detail::is_permutation_impl_ra(
|
2013-01-03 08:01:50 +00:00
|
|
|
first1, last1, first2, d2,
|
2012-12-11 16:47:14 +00:00
|
|
|
pred, pivot / 2
|
|
|
|
)
|
|
|
|
&& sprout::detail::is_permutation_impl_ra(
|
2013-01-03 08:01:50 +00:00
|
|
|
sprout::next(first1, pivot), last1, first2, d2,
|
|
|
|
pred, (sprout::distance(first1, last1) - pivot) / 2
|
2012-12-11 16:47:14 +00:00
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename BinaryPredicate>
|
|
|
|
inline SPROUT_CONSTEXPR bool
|
|
|
|
is_permutation(
|
|
|
|
RandomAccessIterator1 first1, RandomAccessIterator1 last1, RandomAccessIterator2 first2, BinaryPredicate pred,
|
|
|
|
std::random_access_iterator_tag*
|
2012-04-01 13:15:09 +00:00
|
|
|
)
|
|
|
|
{
|
2012-12-11 16:47:14 +00:00
|
|
|
return first1 == last1 ? true
|
|
|
|
: sprout::detail::is_permutation_impl_ra(
|
2013-01-03 08:01:50 +00:00
|
|
|
first1, last1, first2, sprout::distance(first1, last1),
|
|
|
|
pred, sprout::distance(first1, last1) / 2
|
2012-12-11 16:47:14 +00:00
|
|
|
)
|
2012-04-01 13:15:09 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate>
|
2013-01-03 08:01:50 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::pair<ForwardIterator1, bool>
|
|
|
|
is_permutation_impl_1(
|
|
|
|
sprout::pair<ForwardIterator1, bool> const& current,
|
|
|
|
ForwardIterator1 last1, ForwardIterator2 first2, typename std::iterator_traits<ForwardIterator2>::difference_type d2,
|
|
|
|
BinaryPredicate pred, typename std::iterator_traits<ForwardIterator1>::difference_type n
|
|
|
|
)
|
|
|
|
{
|
|
|
|
typedef sprout::pair<ForwardIterator1, bool> type;
|
|
|
|
return !current.second || current.first == last1 ? current
|
|
|
|
: n == 1 ? sprout::count_if(current.first, last1, sprout::bind2nd(pred, *current.first))
|
|
|
|
<= sprout::detail::count_n_if(first2, d2, sprout::bind2nd(pred, *current.first))
|
|
|
|
? type(sprout::next(current.first), true)
|
|
|
|
: type(current.first, false)
|
|
|
|
: sprout::detail::is_permutation_impl_1(
|
|
|
|
sprout::detail::is_permutation_impl_1(
|
|
|
|
current,
|
|
|
|
last1, first2, d2, pred, n / 2
|
|
|
|
),
|
|
|
|
last1, first2, d2, pred, n - n / 2
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::pair<ForwardIterator1, bool>
|
2012-10-06 04:53:07 +00:00
|
|
|
is_permutation_impl(
|
2013-01-03 08:01:50 +00:00
|
|
|
sprout::pair<ForwardIterator1, bool> const& current,
|
|
|
|
ForwardIterator1 last1, ForwardIterator2 first2, typename std::iterator_traits<ForwardIterator2>::difference_type d2,
|
|
|
|
BinaryPredicate pred, typename std::iterator_traits<ForwardIterator1>::difference_type n
|
2012-04-01 13:15:09 +00:00
|
|
|
)
|
|
|
|
{
|
2013-01-03 08:01:50 +00:00
|
|
|
typedef sprout::pair<ForwardIterator1, bool> type;
|
|
|
|
return !current.second || current.first == last1 ? current
|
|
|
|
: sprout::detail::is_permutation_impl(
|
|
|
|
sprout::detail::is_permutation_impl_1(
|
|
|
|
current,
|
|
|
|
last1, first2, d2, pred, n
|
|
|
|
),
|
|
|
|
last1, first2, d2, pred, n * 2
|
|
|
|
)
|
2012-04-01 13:15:09 +00:00
|
|
|
;
|
|
|
|
}
|
2012-12-11 16:47:14 +00:00
|
|
|
template<typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate>
|
|
|
|
inline SPROUT_CONSTEXPR bool
|
|
|
|
is_permutation(
|
|
|
|
ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, BinaryPredicate pred,
|
|
|
|
void*
|
|
|
|
)
|
|
|
|
{
|
2013-01-03 08:01:50 +00:00
|
|
|
typedef sprout::pair<ForwardIterator1, bool> type;
|
|
|
|
return sprout::detail::is_permutation_impl(type(first1, true), last1, first2, sprout::distance(first1, last1), pred, 1).second;
|
2012-12-11 16:47:14 +00:00
|
|
|
}
|
2012-04-01 13:15:09 +00:00
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
// 25.2.12 Is permutation
|
2012-12-12 08:46:36 +00:00
|
|
|
//
|
|
|
|
// recursion depth:
|
2013-01-03 08:01:50 +00:00
|
|
|
// O(log N)
|
2012-12-12 08:46:36 +00:00
|
|
|
//
|
2012-12-11 16:47:14 +00:00
|
|
|
template<typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate>
|
2012-10-06 04:53:07 +00:00
|
|
|
inline SPROUT_CONSTEXPR bool
|
2012-12-11 16:47:14 +00:00
|
|
|
is_permutation(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, BinaryPredicate pred) {
|
|
|
|
typedef typename sprout::common_iterator_category<ForwardIterator1, ForwardIterator2>::type* category;
|
|
|
|
return sprout::detail::is_permutation(first1, last1, first2, pred, category());
|
2012-04-01 13:15:09 +00:00
|
|
|
}
|
|
|
|
|
2012-12-11 16:47:14 +00:00
|
|
|
template<typename ForwardIterator1, typename ForwardIterator2>
|
2012-10-06 04:53:07 +00:00
|
|
|
inline SPROUT_CONSTEXPR bool
|
2012-12-11 16:47:14 +00:00
|
|
|
is_permutation(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2) {
|
|
|
|
return sprout::is_permutation(first1, last1, first2, sprout::equal_to<>());
|
2012-04-01 13:15:09 +00:00
|
|
|
}
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_ALGORITHM_IS_PERMUTATION_HPP
|