1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2024-12-03 21:15:42 +00:00
Sprout/sprout/detail/algorithm/overlap_count.hpp
2012-06-24 08:22:12 +09:00

73 lines
2.2 KiB
C++

#ifndef SPROUT_DETAIL_ALGORITHM_OVERLAP_COUNT_HPP
#define SPROUT_DETAIL_ALGORITHM_OVERLAP_COUNT_HPP
#include <iterator>
#include <sprout/config.hpp>
#include <sprout/iterator/operation.hpp>
namespace sprout {
namespace detail {
template<typename InputIterator>
inline SPROUT_CONSTEXPR typename std::iterator_traits<InputIterator>::difference_type overlap_count_impl(
InputIterator first,
InputIterator last,
typename std::iterator_traits<InputIterator>::value_type const& value,
typename std::iterator_traits<InputIterator>::difference_type current = 0
)
{
return first == last
? 0
: *first == value
? 1 + sprout::detail::overlap_count_impl(sprout::next(first), last, value)
: sprout::detail::overlap_count_impl(sprout::next(first), last, *first)
;
}
//
// overlap_count
//
template<typename InputIterator>
inline SPROUT_CONSTEXPR typename std::iterator_traits<InputIterator>::difference_type overlap_count(
InputIterator first,
InputIterator last
)
{
return first == last
? 0
: sprout::detail::overlap_count_impl(sprout::next(first), last, *first)
;
}
template<typename InputIterator, typename Predicate>
inline SPROUT_CONSTEXPR typename std::iterator_traits<InputIterator>::difference_type overlap_count_impl(
InputIterator first,
InputIterator last,
Predicate pred,
typename std::iterator_traits<InputIterator>::value_type const& value
)
{
return first == last
? 0
: pred(*first, value)
? 1 + sprout::detail::overlap_count_impl(sprout::next(first), last, pred, value)
: sprout::detail::overlap_count_impl(sprout::next(first), last, pred, *first)
;
}
//
// overlap_count
//
template<typename InputIterator, typename Predicate>
inline SPROUT_CONSTEXPR typename std::iterator_traits<InputIterator>::difference_type overlap_count(
InputIterator first,
InputIterator last,
Predicate pred
)
{
return first == last
? 0
: sprout::detail::overlap_count_impl(sprout::next(first), last, pred, *first)
;
}
} // namespace detail
} // namespace sprout
#endif // #ifndef SPROUT_DETAIL_ALGORITHM_OVERLAP_COUNT_HPP