2012-06-23 23:22:12 +00:00
|
|
|
#ifndef SPROUT_DETAIL_ALGORITHM_OVERLAP_COUNT_HPP
|
|
|
|
#define SPROUT_DETAIL_ALGORITHM_OVERLAP_COUNT_HPP
|
2011-09-01 02:48:32 +00:00
|
|
|
|
|
|
|
#include <iterator>
|
|
|
|
#include <sprout/config.hpp>
|
2011-10-01 15:19:13 +00:00
|
|
|
#include <sprout/iterator/operation.hpp>
|
2013-01-03 08:01:50 +00:00
|
|
|
#include <sprout/iterator/type_traits/is_iterator.hpp>
|
|
|
|
#include <sprout/tuple/tuple.hpp>
|
2012-12-16 17:14:07 +00:00
|
|
|
#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT
|
2011-09-01 02:48:32 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
namespace detail {
|
2012-12-17 11:10:17 +00:00
|
|
|
template<typename RandomAccessIterator, typename BinaryPredicate>
|
|
|
|
inline SPROUT_CONSTEXPR typename std::iterator_traits<RandomAccessIterator>::difference_type
|
|
|
|
overlap_count_impl_ra(
|
|
|
|
RandomAccessIterator first, RandomAccessIterator last, BinaryPredicate pred,
|
|
|
|
typename std::iterator_traits<RandomAccessIterator>::difference_type pivot
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return pivot == 0 ? (pred(*first, *last) ? 1 : 0)
|
|
|
|
: sprout::detail::overlap_count_impl_ra(
|
|
|
|
first, sprout::next(first, pivot), pred,
|
|
|
|
pivot / 2
|
|
|
|
)
|
|
|
|
+ sprout::detail::overlap_count_impl_ra(
|
|
|
|
sprout::next(first, pivot), last, pred,
|
2013-01-03 08:01:50 +00:00
|
|
|
(sprout::distance(first, last) - pivot) / 2
|
2012-12-17 11:10:17 +00:00
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename RandomAccessIterator, typename BinaryPredicate>
|
2013-01-03 08:01:50 +00:00
|
|
|
inline SPROUT_CONSTEXPR typename std::enable_if<
|
|
|
|
sprout::is_constant_distance_iterator<RandomAccessIterator>::value,
|
|
|
|
typename std::iterator_traits<RandomAccessIterator>::difference_type
|
|
|
|
>::type
|
2012-12-17 11:10:17 +00:00
|
|
|
overlap_count(
|
|
|
|
RandomAccessIterator first, RandomAccessIterator last, BinaryPredicate pred,
|
|
|
|
std::random_access_iterator_tag*
|
|
|
|
)
|
|
|
|
{
|
2013-01-03 08:01:50 +00:00
|
|
|
return first == last || sprout::distance(first, last) == 1 ? 0
|
2012-12-17 11:10:17 +00:00
|
|
|
: sprout::detail::overlap_count_impl_ra(
|
2013-01-03 08:01:50 +00:00
|
|
|
first, sprout::next(first, sprout::distance(first, last) - 1), pred,
|
|
|
|
(sprout::distance(first, last) - 1) / 2
|
2012-12-17 11:10:17 +00:00
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2012-12-16 17:14:07 +00:00
|
|
|
template<typename InputIterator, typename BinaryPredicate>
|
2013-01-03 08:01:50 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::tuples::tuple<InputIterator, typename std::iterator_traits<InputIterator>::value_type, typename std::iterator_traits<InputIterator>::difference_type>
|
|
|
|
overlap_count_impl_1(
|
|
|
|
sprout::tuples::tuple<InputIterator, typename std::iterator_traits<InputIterator>::value_type, typename std::iterator_traits<InputIterator>::difference_type> const& current,
|
|
|
|
InputIterator last, BinaryPredicate pred, typename std::iterator_traits<InputIterator>::difference_type n
|
|
|
|
)
|
|
|
|
{
|
|
|
|
typedef sprout::tuples::tuple<InputIterator, typename std::iterator_traits<InputIterator>::value_type, typename std::iterator_traits<InputIterator>::difference_type> type;
|
|
|
|
return sprout::tuples::get<0>(current) == last ? current
|
|
|
|
: n == 1 ? type(
|
|
|
|
sprout::next(sprout::tuples::get<0>(current)), *sprout::tuples::get<0>(current),
|
|
|
|
sprout::tuples::get<2>(current) + (pred(*sprout::tuples::get<0>(current), sprout::tuples::get<1>(current)) ? 1 : 0)
|
|
|
|
)
|
|
|
|
: sprout::detail::overlap_count_impl_1(
|
|
|
|
sprout::detail::overlap_count_impl_1(
|
|
|
|
current,
|
|
|
|
last, pred, n / 2
|
|
|
|
),
|
|
|
|
last, pred, n - n / 2
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename InputIterator, typename BinaryPredicate>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::tuples::tuple<InputIterator, typename std::iterator_traits<InputIterator>::value_type, typename std::iterator_traits<InputIterator>::difference_type>
|
2012-10-05 15:58:56 +00:00
|
|
|
overlap_count_impl(
|
2013-01-03 08:01:50 +00:00
|
|
|
sprout::tuples::tuple<InputIterator, typename std::iterator_traits<InputIterator>::value_type, typename std::iterator_traits<InputIterator>::difference_type> const& current,
|
|
|
|
InputIterator last, BinaryPredicate pred, typename std::iterator_traits<InputIterator>::difference_type n
|
2011-09-01 02:48:32 +00:00
|
|
|
)
|
|
|
|
{
|
2013-01-03 08:01:50 +00:00
|
|
|
typedef sprout::tuples::tuple<InputIterator, typename std::iterator_traits<InputIterator>::value_type, typename std::iterator_traits<InputIterator>::difference_type> type;
|
|
|
|
return sprout::tuples::get<0>(current) == last ? current
|
|
|
|
: sprout::detail::overlap_count_impl(
|
|
|
|
sprout::detail::overlap_count_impl_1(
|
|
|
|
current,
|
|
|
|
last, pred, n
|
|
|
|
),
|
|
|
|
last, pred, n * 2
|
|
|
|
)
|
2011-09-01 02:48:32 +00:00
|
|
|
;
|
|
|
|
}
|
2012-12-17 11:10:17 +00:00
|
|
|
template<typename InputIterator, typename BinaryPredicate>
|
|
|
|
inline SPROUT_CONSTEXPR typename std::iterator_traits<InputIterator>::difference_type
|
2013-01-03 08:01:50 +00:00
|
|
|
overlap_count(
|
2012-12-17 11:10:17 +00:00
|
|
|
InputIterator first, InputIterator last, BinaryPredicate pred,
|
|
|
|
void*
|
|
|
|
)
|
|
|
|
{
|
2013-01-03 08:01:50 +00:00
|
|
|
typedef sprout::tuples::tuple<InputIterator, typename std::iterator_traits<InputIterator>::value_type, typename std::iterator_traits<InputIterator>::difference_type> type;
|
2012-12-17 11:10:17 +00:00
|
|
|
return first == last ? 0
|
2013-01-03 08:01:50 +00:00
|
|
|
: sprout::tuples::get<2>(
|
|
|
|
sprout::detail::overlap_count_impl(type(sprout::next(first), *first, 0), last, pred, 1)
|
|
|
|
)
|
2012-12-17 11:10:17 +00:00
|
|
|
;
|
|
|
|
}
|
2012-12-16 17:14:07 +00:00
|
|
|
|
2011-09-01 02:48:32 +00:00
|
|
|
//
|
|
|
|
// overlap_count
|
|
|
|
//
|
2013-01-03 08:01:50 +00:00
|
|
|
// recursion depth:
|
|
|
|
// O(log N)
|
|
|
|
//
|
2012-12-16 17:14:07 +00:00
|
|
|
template<typename InputIterator, typename BinaryPredicate>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR typename std::iterator_traits<InputIterator>::difference_type
|
2012-12-16 17:14:07 +00:00
|
|
|
overlap_count(InputIterator first, InputIterator last, BinaryPredicate pred) {
|
2012-12-17 11:10:17 +00:00
|
|
|
typedef typename std::iterator_traits<InputIterator>::iterator_category* category;
|
|
|
|
return sprout::detail::overlap_count(first, last, pred, category());
|
2011-09-01 02:48:32 +00:00
|
|
|
}
|
2012-12-16 17:14:07 +00:00
|
|
|
|
|
|
|
template<typename InputIterator>
|
|
|
|
inline SPROUT_CONSTEXPR typename std::iterator_traits<InputIterator>::difference_type
|
|
|
|
overlap_count(InputIterator first, InputIterator last) {
|
|
|
|
return sprout::detail::overlap_count(
|
|
|
|
first, last,
|
|
|
|
NS_SSCRISK_CEL_OR_SPROUT::equal_to<typename std::iterator_traits<InputIterator>::value_type>()
|
|
|
|
);
|
|
|
|
}
|
2011-09-01 02:48:32 +00:00
|
|
|
} // namespace detail
|
|
|
|
} // namespace sprout
|
|
|
|
|
2012-06-23 23:22:12 +00:00
|
|
|
#endif // #ifndef SPROUT_DETAIL_ALGORITHM_OVERLAP_COUNT_HPP
|