mirror of
https://github.com/bolero-MURAKAMI/Sprout.git
synced 2025-07-14 15:04:09 +00:00
fix recursion depth: count, count_if, mismatch, equal, is_permutation
This commit is contained in:
parent
b5bdde21c4
commit
75228465fe
14 changed files with 716 additions and 87 deletions
|
@ -1,65 +1,92 @@
|
|||
#ifndef SPROUT_ALGORITHM_IS_PERMUTATION_HPP
|
||||
#define SPROUT_ALGORITHM_IS_PERMUTATION_HPP
|
||||
|
||||
#include <iterator>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/iterator/operation.hpp>
|
||||
#include <sprout/algorithm/count.hpp>
|
||||
#include <sprout/functional/equal_to.hpp>
|
||||
#include <sprout/functional/bind2nd.hpp>
|
||||
#include <sprout/algorithm/count_if.hpp>
|
||||
#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT
|
||||
#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT
|
||||
|
||||
namespace sprout {
|
||||
// Copyright (C) 2011 RiSK (sscrisk)
|
||||
|
||||
namespace detail {
|
||||
template<typename ForwardIterator1, typename ForwardIterator2>
|
||||
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename BinaryPredicate>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
is_permutation_impl(
|
||||
ForwardIterator1 first1, ForwardIterator1 last1,
|
||||
ForwardIterator2 first2, ForwardIterator1 first1_
|
||||
is_permutation_impl_ra(
|
||||
RandomAccessIterator1 first1, RandomAccessIterator1 last1,
|
||||
RandomAccessIterator2 first2, RandomAccessIterator2 last2,
|
||||
BinaryPredicate pred,
|
||||
typename std::iterator_traits<RandomAccessIterator1>::difference_type pivot
|
||||
)
|
||||
{
|
||||
return first1_ == last1 ? true
|
||||
: sprout::count(first1, last1, *first1_)
|
||||
== sprout::count(first2, sprout::next(first2, NS_SSCRISK_CEL_OR_SPROUT::distance(first1, last1)), *first1_)
|
||||
&& sprout::detail::is_permutation_impl(first1, last1, first2, sprout::next(first1_))
|
||||
? true
|
||||
: false
|
||||
return pivot == 0 ? sprout::count_if(first1, last1, sprout::bind2nd(pred, *first1))
|
||||
<= sprout::count_if(first2, last2, sprout::bind2nd(pred, *first1))
|
||||
: sprout::detail::is_permutation_impl_ra(
|
||||
first1, last1, first2, last2,
|
||||
pred, pivot / 2
|
||||
)
|
||||
&& sprout::detail::is_permutation_impl_ra(
|
||||
sprout::next(first1, pivot), last1, first2, last2,
|
||||
pred, (NS_SSCRISK_CEL_OR_SPROUT::distance(first1, last1) - pivot) / 2
|
||||
)
|
||||
;
|
||||
}
|
||||
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*
|
||||
)
|
||||
{
|
||||
return first1 == last1 ? true
|
||||
: sprout::detail::is_permutation_impl_ra(
|
||||
first1, last1, first2, sprout::next(first2, NS_SSCRISK_CEL_OR_SPROUT::distance(first1, last1)),
|
||||
pred, NS_SSCRISK_CEL_OR_SPROUT::distance(first1, last1) / 2
|
||||
)
|
||||
;
|
||||
}
|
||||
|
||||
// Copyright (C) 2011 RiSK (sscrisk)
|
||||
template<typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
is_permutation_impl(
|
||||
ForwardIterator1 first1, ForwardIterator1 last1,
|
||||
ForwardIterator2 first2, ForwardIterator1 first1_,
|
||||
ForwardIterator2 first2, ForwardIterator2 last2,
|
||||
BinaryPredicate pred
|
||||
)
|
||||
{
|
||||
return first1_ == last1 ? true
|
||||
: sprout::count_if(first1, last1, NS_SSCRISK_CEL_OR_SPROUT::bind2nd(pred, *first1_))
|
||||
== sprout::count_if(
|
||||
first2, sprout::next(first2, NS_SSCRISK_CEL_OR_SPROUT::distance(first1, last1)),
|
||||
NS_SSCRISK_CEL_OR_SPROUT::bind2nd(pred, *first1_)
|
||||
)
|
||||
&& sprout::detail::is_permutation_impl(first1, last1, first2, sprout::next(first1_), pred)
|
||||
? true
|
||||
: false
|
||||
return first1 == last1 ? true
|
||||
: sprout::count_if(first1, last1, sprout::bind2nd(pred, *first1))
|
||||
<= sprout::count_if(first2, last2, sprout::bind2nd(pred, *first1))
|
||||
&& sprout::detail::is_permutation_impl(sprout::next(first1), last1, first2, last2, pred)
|
||||
;
|
||||
}
|
||||
template<typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
is_permutation(
|
||||
ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, BinaryPredicate pred,
|
||||
void*
|
||||
)
|
||||
{
|
||||
return sprout::detail::is_permutation_impl(
|
||||
first1, last1, first2, sprout::next(first2, NS_SSCRISK_CEL_OR_SPROUT::distance(first1, last1)), pred
|
||||
);
|
||||
}
|
||||
} // namespace detail
|
||||
|
||||
// 25.2.12 Is permutation
|
||||
template<typename ForwardIterator1, typename ForwardIterator2>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
is_permutation(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2) {
|
||||
return sprout::detail::is_permutation_impl(first1, last1, first2, first1);
|
||||
}
|
||||
|
||||
template<typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
is_permutation(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, BinaryPredicate pred) {
|
||||
return sprout::detail::is_permutation_impl(first1, last1, first2, first1, pred);
|
||||
typedef typename sprout::common_iterator_category<ForwardIterator1, ForwardIterator2>::type* category;
|
||||
return sprout::detail::is_permutation(first1, last1, first2, pred, category());
|
||||
}
|
||||
|
||||
template<typename ForwardIterator1, typename ForwardIterator2>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
is_permutation(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2) {
|
||||
return sprout::is_permutation(first1, last1, first2, sprout::equal_to<>());
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue