Sprout/sprout/detail/algorithm/overlap_count.hpp

60 lines
2.1 KiB
C++
Raw Normal View History

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>
2011-09-01 02:48:32 +00:00
namespace sprout {
namespace detail {
2012-06-23 04:22:50 +00:00
template<typename InputIterator>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR typename std::iterator_traits<InputIterator>::difference_type
overlap_count_impl(
InputIterator first, InputIterator last,
2012-06-23 04:22:50 +00:00
typename std::iterator_traits<InputIterator>::value_type const& value,
typename std::iterator_traits<InputIterator>::difference_type current = 0
2011-09-01 02:48:32 +00:00
)
{
2012-10-05 15:58:56 +00:00
return first == last ? 0
: *first == value ? 1 + sprout::detail::overlap_count_impl(sprout::next(first), last, value)
2011-10-01 15:19:13 +00:00
: sprout::detail::overlap_count_impl(sprout::next(first), last, *first)
2011-09-01 02:48:32 +00:00
;
}
//
// overlap_count
//
2012-06-23 04:22:50 +00:00
template<typename InputIterator>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR typename std::iterator_traits<InputIterator>::difference_type
overlap_count(InputIterator first, InputIterator last) {
return first == last ? 0
2011-10-01 15:19:13 +00:00
: sprout::detail::overlap_count_impl(sprout::next(first), last, *first)
2011-09-01 02:48:32 +00:00
;
}
2012-06-23 04:22:50 +00:00
template<typename InputIterator, typename Predicate>
2012-10-05 15:58:56 +00:00
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
2011-09-01 02:48:32 +00:00
)
{
2012-10-05 15:58:56 +00:00
return first == last ? 0
: pred(*first, value) ? 1 + sprout::detail::overlap_count_impl(sprout::next(first), last, pred, value)
2011-10-01 15:19:13 +00:00
: sprout::detail::overlap_count_impl(sprout::next(first), last, pred, *first)
2011-09-01 02:48:32 +00:00
;
}
//
// overlap_count
//
2012-06-23 04:22:50 +00:00
template<typename InputIterator, typename Predicate>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR typename std::iterator_traits<InputIterator>::difference_type
overlap_count(InputIterator first, InputIterator last, Predicate pred) {
return first == last ? 0
2011-10-01 15:19:13 +00:00
: sprout::detail::overlap_count_impl(sprout::next(first), last, pred, *first)
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