Sprout/sprout/algorithm/is_permutation.hpp

100 lines
3.8 KiB
C++
Raw Normal View History

2012-04-01 13:15:09 +00:00
#ifndef SPROUT_ALGORITHM_IS_PERMUTATION_HPP
#define SPROUT_ALGORITHM_IS_PERMUTATION_HPP
#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>
#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>
#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT
namespace sprout {
namespace detail {
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename BinaryPredicate>
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR bool
is_permutation_impl_ra(
RandomAccessIterator1 first1, RandomAccessIterator1 last1,
RandomAccessIterator2 first2, RandomAccessIterator2 last2,
BinaryPredicate pred,
typename std::iterator_traits<RandomAccessIterator1>::difference_type pivot
)
{
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*
2012-04-01 13:15:09 +00:00
)
{
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
)
2012-04-01 13:15:09 +00:00
;
}
// Copyright (C) 2011 RiSK (sscrisk)
2012-04-01 13:15:09 +00:00
template<typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate>
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR bool
is_permutation_impl(
ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2,
2012-04-01 13:15:09 +00:00
BinaryPredicate pred
)
{
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)
2012-04-01 13:15:09 +00:00
;
}
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
);
}
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:
// [first1, last1), first2 are RandomAccessIterator -> O(log N)
// otherwise -> O(N^2)
//
template<typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate>
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR bool
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
}
template<typename ForwardIterator1, typename ForwardIterator2>
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR bool
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