2013-08-08 09:54:33 +00:00
|
|
|
/*=============================================================================
|
2016-02-25 09:48:28 +00:00
|
|
|
Copyright (c) 2011-2016 Bolero MURAKAMI
|
2013-08-08 09:54:33 +00:00
|
|
|
https://github.com/bolero-MURAKAMI/Sprout
|
|
|
|
|
|
|
|
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)
|
|
|
|
=============================================================================*/
|
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>
|
2013-01-03 08:38:45 +00:00
|
|
|
#include <type_traits>
|
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-02-07 14:12:57 +00:00
|
|
|
#include <sprout/utility/pair/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-09-03 13:04:03 +00:00
|
|
|
#include <sprout/algorithm/find_if.hpp>
|
|
|
|
#include <sprout/algorithm/mismatch.hpp>
|
2012-04-01 13:15:09 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
namespace detail {
|
2013-09-03 13:04:03 +00:00
|
|
|
template<typename Difference, typename ForwardIterator, typename BinaryPredicate>
|
|
|
|
inline SPROUT_CONSTEXPR bool
|
|
|
|
is_permutation_impl_check(Difference count, ForwardIterator first, ForwardIterator last, BinaryPredicate pred)
|
|
|
|
{
|
|
|
|
return count != 0 && sprout::count_if(sprout::next(first), last, sprout::bind2nd(pred, *first)) + 1 == count;
|
|
|
|
}
|
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-05-16 10:03:27 +00:00
|
|
|
RandomAccessIterator2 first2, RandomAccessIterator2 last2,
|
2013-09-03 13:04:03 +00:00
|
|
|
BinaryPredicate pred, RandomAccessIterator1 current,
|
|
|
|
typename std::iterator_traits<RandomAccessIterator1>::difference_type size
|
2012-12-11 16:47:14 +00:00
|
|
|
)
|
|
|
|
{
|
2013-09-03 13:04:03 +00:00
|
|
|
return size == 1 ? sprout::find_if(first1, current, sprout::bind2nd(pred, *current)) != current
|
|
|
|
|| sprout::detail::is_permutation_impl_check(
|
|
|
|
sprout::count_if(first2, last2, sprout::bind2nd(pred, *current)),
|
|
|
|
current, last1, pred
|
|
|
|
)
|
2012-12-11 16:47:14 +00:00
|
|
|
: sprout::detail::is_permutation_impl_ra(
|
2013-05-16 10:03:27 +00:00
|
|
|
first1, last1, first2, last2,
|
2013-09-03 13:04:03 +00:00
|
|
|
pred, current, size / 2
|
2012-12-11 16:47:14 +00:00
|
|
|
)
|
|
|
|
&& sprout::detail::is_permutation_impl_ra(
|
2013-09-03 13:04:03 +00:00
|
|
|
first1, last1, first2, last2,
|
|
|
|
pred, sprout::next(current, size / 2), size - size / 2
|
2012-12-11 16:47:14 +00:00
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename BinaryPredicate>
|
2013-01-03 08:38:45 +00:00
|
|
|
inline SPROUT_CONSTEXPR typename std::enable_if<
|
|
|
|
sprout::is_constant_distance_iterator<RandomAccessIterator1>::value && sprout::is_constant_distance_iterator<RandomAccessIterator2>::value,
|
|
|
|
bool
|
|
|
|
>::type
|
2012-12-11 16:47:14 +00:00
|
|
|
is_permutation(
|
2013-09-03 13:04:03 +00:00
|
|
|
sprout::pair<RandomAccessIterator1, RandomAccessIterator2> first, RandomAccessIterator1 last1, BinaryPredicate pred,
|
2012-12-11 16:47:14 +00:00
|
|
|
std::random_access_iterator_tag*
|
2012-04-01 13:15:09 +00:00
|
|
|
)
|
|
|
|
{
|
2013-09-03 13:04:03 +00:00
|
|
|
return first.first == last1
|
|
|
|
|| sprout::detail::is_permutation_impl_ra(
|
|
|
|
first.first, last1, first.second, sprout::next(first.second, sprout::distance(first.first, last1)),
|
|
|
|
pred, first.first, sprout::distance(first.first, last1)
|
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,
|
2013-09-03 13:04:03 +00:00
|
|
|
ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2,
|
2013-01-03 08:01:50 +00:00
|
|
|
BinaryPredicate pred, typename std::iterator_traits<ForwardIterator1>::difference_type n
|
|
|
|
)
|
|
|
|
{
|
|
|
|
typedef sprout::pair<ForwardIterator1, bool> type;
|
|
|
|
return !current.second || current.first == last1 ? current
|
2013-09-03 13:04:03 +00:00
|
|
|
: n == 1 ? sprout::find_if(first1, current.first, sprout::bind2nd(pred, *current.first)) != current.first
|
|
|
|
|| sprout::detail::is_permutation_impl_check(
|
|
|
|
sprout::count_if(first2, last2, sprout::bind2nd(pred, *current.first)),
|
|
|
|
current.first, last1, pred
|
|
|
|
)
|
2013-01-03 08:01:50 +00:00
|
|
|
? type(sprout::next(current.first), true)
|
|
|
|
: type(current.first, false)
|
|
|
|
: sprout::detail::is_permutation_impl_1(
|
|
|
|
sprout::detail::is_permutation_impl_1(
|
|
|
|
current,
|
2013-09-03 13:04:03 +00:00
|
|
|
first1, last1, first2, last2, pred, n / 2
|
2013-01-03 08:01:50 +00:00
|
|
|
),
|
2013-09-03 13:04:03 +00:00
|
|
|
first1, last1, first2, last2, pred, n - n / 2
|
2013-01-03 08:01:50 +00:00
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate>
|
2013-09-03 13:04:03 +00:00
|
|
|
inline SPROUT_CONSTEXPR bool
|
2012-10-06 04:53:07 +00:00
|
|
|
is_permutation_impl(
|
2013-09-03 13:04:03 +00:00
|
|
|
ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2,
|
|
|
|
BinaryPredicate pred, typename std::iterator_traits<ForwardIterator1>::difference_type size
|
2012-04-01 13:15:09 +00:00
|
|
|
)
|
|
|
|
{
|
2013-09-03 13:04:03 +00:00
|
|
|
typedef sprout::pair<ForwardIterator1, bool> type;
|
|
|
|
return sprout::detail::is_permutation_impl_1(
|
|
|
|
type(first1, true),
|
|
|
|
first1, last1, first2, sprout::next(first2, size),
|
|
|
|
pred, size
|
|
|
|
).second
|
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(
|
2013-09-03 13:04:03 +00:00
|
|
|
sprout::pair<ForwardIterator1, ForwardIterator2> first, ForwardIterator1 last1, BinaryPredicate pred,
|
2013-02-23 06:21:27 +00:00
|
|
|
std::forward_iterator_tag*
|
2012-12-11 16:47:14 +00:00
|
|
|
)
|
|
|
|
{
|
2013-09-03 13:04:03 +00:00
|
|
|
return first.first == last1
|
|
|
|
|| sprout::detail::is_permutation_impl(
|
|
|
|
first.first, last1, first.second, pred, sprout::distance(first.first, last1)
|
|
|
|
)
|
|
|
|
;
|
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;
|
2013-09-03 13:04:03 +00:00
|
|
|
return sprout::detail::is_permutation(sprout::mismatch(first1, last1, first2, pred), last1, 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
|
|
|
}
|
2013-05-16 10:03:27 +00:00
|
|
|
|
|
|
|
namespace detail {
|
2013-09-03 13:04:03 +00:00
|
|
|
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename BinaryPredicate>
|
|
|
|
inline SPROUT_CONSTEXPR bool
|
|
|
|
is_permutation_impl_ra_1(
|
|
|
|
sprout::pair<RandomAccessIterator1, RandomAccessIterator2> first,
|
|
|
|
RandomAccessIterator1 last1, RandomAccessIterator2 last2, BinaryPredicate pred
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return first.first == last1
|
|
|
|
|| sprout::detail::is_permutation_impl_ra(
|
|
|
|
first.first, last1, first.second, last2,
|
|
|
|
pred, first.first, sprout::distance(first.first, last1)
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
2013-05-16 10:03:27 +00:00
|
|
|
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename BinaryPredicate>
|
|
|
|
inline SPROUT_CONSTEXPR typename std::enable_if<
|
|
|
|
sprout::is_constant_distance_iterator<RandomAccessIterator1>::value && sprout::is_constant_distance_iterator<RandomAccessIterator2>::value,
|
|
|
|
bool
|
|
|
|
>::type
|
|
|
|
is_permutation(
|
|
|
|
RandomAccessIterator1 first1, RandomAccessIterator1 last1, RandomAccessIterator2 first2, RandomAccessIterator2 last2, BinaryPredicate pred,
|
|
|
|
std::random_access_iterator_tag*
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::distance(first1, last1) == sprout::distance(first2, last2)
|
2013-09-03 13:04:03 +00:00
|
|
|
&& sprout::detail::is_permutation_impl_ra_1(
|
|
|
|
sprout::mismatch(first1, last1, first2, last2, pred),
|
|
|
|
last1, last2, pred)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate, typename Difference1, typename Difference2>
|
|
|
|
inline SPROUT_CONSTEXPR bool
|
|
|
|
is_permutation_impl_3(
|
|
|
|
ForwardIterator1 first1, ForwardIterator1 last1,
|
|
|
|
ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred,
|
|
|
|
Difference1 size1, Difference2 size2
|
|
|
|
)
|
|
|
|
{
|
|
|
|
typedef sprout::pair<ForwardIterator1, bool> type;
|
|
|
|
return size1 == size2
|
|
|
|
&& sprout::detail::is_permutation_impl_1(
|
|
|
|
type(first1, true), first1, last1, first2, last2,
|
|
|
|
pred, size1
|
|
|
|
).second
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate>
|
|
|
|
inline SPROUT_CONSTEXPR bool
|
|
|
|
is_permutation_impl_2(
|
|
|
|
sprout::pair<ForwardIterator1, ForwardIterator2> first,
|
|
|
|
ForwardIterator1 last1, ForwardIterator2 last2, BinaryPredicate pred
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::detail::is_permutation_impl_3(
|
|
|
|
first.first, last1, first.second, last2, pred,
|
|
|
|
sprout::distance(first.first, last1), sprout::distance(first.second, last2)
|
|
|
|
)
|
2013-05-16 10:03:27 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate>
|
|
|
|
inline SPROUT_CONSTEXPR bool
|
|
|
|
is_permutation(
|
|
|
|
ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred,
|
|
|
|
std::forward_iterator_tag*
|
|
|
|
)
|
|
|
|
{
|
2013-09-03 13:04:03 +00:00
|
|
|
return sprout::detail::is_permutation_impl_2(
|
|
|
|
sprout::mismatch(first1, last1, first2, last2, pred),
|
|
|
|
last1, last2, pred);
|
2013-05-16 10:03:27 +00:00
|
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
// 25.2.12 Is permutation
|
|
|
|
//
|
|
|
|
// recursion depth:
|
|
|
|
// O(log N)
|
|
|
|
//
|
|
|
|
template<typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate>
|
|
|
|
inline SPROUT_CONSTEXPR bool
|
|
|
|
is_permutation(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred) {
|
|
|
|
typedef typename sprout::common_iterator_category<ForwardIterator1, ForwardIterator2>::type* category;
|
|
|
|
return sprout::detail::is_permutation(first1, last1, first2, last2, pred, category());
|
|
|
|
}
|
|
|
|
template<typename ForwardIterator1, typename ForwardIterator2>
|
|
|
|
inline SPROUT_CONSTEXPR bool
|
|
|
|
is_permutation(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2) {
|
|
|
|
return sprout::is_permutation(first1, last1, first2, last2, sprout::equal_to<>());
|
|
|
|
}
|
2012-04-01 13:15:09 +00:00
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_ALGORITHM_IS_PERMUTATION_HPP
|