diff --git a/sprout/algorithm/fit/partition_copy.hpp b/sprout/algorithm/fit/partition_copy.hpp index c908a15c..9659569f 100644 --- a/sprout/algorithm/fit/partition_copy.hpp +++ b/sprout/algorithm/fit/partition_copy.hpp @@ -8,6 +8,7 @@ #include #include #include +#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT #include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT #include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT diff --git a/sprout/detail/algorithm/count_n.hpp b/sprout/detail/algorithm/count_n.hpp index 60910216..9288078c 100644 --- a/sprout/detail/algorithm/count_n.hpp +++ b/sprout/detail/algorithm/count_n.hpp @@ -4,18 +4,45 @@ #include #include #include +#include namespace sprout { namespace detail { + template + inline SPROUT_CONSTEXPR typename std::iterator_traits::difference_type + count_n( + InputIterator first, Size n, T const& value, + std::random_access_iterator_tag* + ) + { + return sprout::count(first, sprout::next(first, n), value); + } + + template + inline SPROUT_CONSTEXPR typename std::iterator_traits::difference_type + count_n_impl(InputIterator first, Size n, T const& value) { + return n == 0 ? 0 + : (*first == value ? 1 : 0) + sprout::detail::count_n_impl(sprout::next(first), n - 1, value) + ; + } + template + inline SPROUT_CONSTEXPR typename std::iterator_traits::difference_type + count_n( + InputIterator first, Size n, T const& value, + void* + ) + { + return sprout::detail::count_n_impl(first, n, value); + } + // // count_n // template inline SPROUT_CONSTEXPR typename std::iterator_traits::difference_type count_n(InputIterator first, Size n, T const& value) { - return n == 0 ? 0 - : (*first == value ? 1 : 0) + sprout::detail::count_n(sprout::next(first), n - 1, value) - ; + typedef typename std::iterator_traits::iterator_category* category; + return sprout::detail::count_n(first, n, value, category()); } } // namespace detail } // namespace sprout diff --git a/sprout/detail/algorithm/count_n_if.hpp b/sprout/detail/algorithm/count_n_if.hpp index a1e86469..78003a88 100644 --- a/sprout/detail/algorithm/count_n_if.hpp +++ b/sprout/detail/algorithm/count_n_if.hpp @@ -4,18 +4,46 @@ #include #include #include +#include namespace sprout { namespace detail { + template + inline SPROUT_CONSTEXPR typename std::iterator_traits::difference_type + count_n_if( + InputIterator first, Size n, Predicate pred, + std::random_access_iterator_tag* + ) + { + return sprout::count_if(first, sprout::next(first, n), pred); + } + + template + inline SPROUT_CONSTEXPR typename std::iterator_traits::difference_type + count_n_if_impl(InputIterator first, Size n, Predicate pred) { + return n == 0 ? 0 + : (pred(*first) ? 1 : 0) + sprout::detail::count_n_if(sprout::next(first), n - 1, pred) + ; + } + template + inline SPROUT_CONSTEXPR typename std::iterator_traits::difference_type + count_n_if( + InputIterator first, Size n, Predicate pred, + void* + ) + { + return sprout::detail::count_n_if_impl(first, n, pred); + } + // // count_n_if // template inline SPROUT_CONSTEXPR typename std::iterator_traits::difference_type count_n_if(InputIterator first, Size n, Predicate pred) { - return n == 0 ? 0 - : (pred(*first) ? 1 : 0) + sprout::detail::count_n_if(sprout::next(first), n - 1, pred) - ; + typedef typename std::iterator_traits::iterator_category* category; + return sprout::detail::count_n_if(first, n, pred, category()); + } } // namespace detail } // namespace sprout diff --git a/sprout/detail/algorithm/overlap_count.hpp b/sprout/detail/algorithm/overlap_count.hpp index 58e16269..16115031 100644 --- a/sprout/detail/algorithm/overlap_count.hpp +++ b/sprout/detail/algorithm/overlap_count.hpp @@ -4,38 +4,15 @@ #include #include #include +#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT namespace sprout { namespace detail { - template + template inline SPROUT_CONSTEXPR typename std::iterator_traits::difference_type overlap_count_impl( InputIterator first, InputIterator last, - typename std::iterator_traits::value_type const& value, - typename std::iterator_traits::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 - inline SPROUT_CONSTEXPR typename std::iterator_traits::difference_type - overlap_count(InputIterator first, InputIterator last) { - return first == last ? 0 - : sprout::detail::overlap_count_impl(sprout::next(first), last, *first) - ; - } - - template - inline SPROUT_CONSTEXPR typename std::iterator_traits::difference_type - overlap_count_impl( - InputIterator first, InputIterator last, - Predicate pred, typename std::iterator_traits::value_type const& value + BinaryPredicate pred, typename std::iterator_traits::value_type const& value ) { return first == last ? 0 @@ -43,16 +20,26 @@ namespace sprout { : sprout::detail::overlap_count_impl(sprout::next(first), last, pred, *first) ; } + // // overlap_count // - template + template inline SPROUT_CONSTEXPR typename std::iterator_traits::difference_type - overlap_count(InputIterator first, InputIterator last, Predicate pred) { + overlap_count(InputIterator first, InputIterator last, BinaryPredicate pred) { return first == last ? 0 : sprout::detail::overlap_count_impl(sprout::next(first), last, pred, *first) ; } + + template + inline SPROUT_CONSTEXPR typename std::iterator_traits::difference_type + overlap_count(InputIterator first, InputIterator last) { + return sprout::detail::overlap_count( + first, last, + NS_SSCRISK_CEL_OR_SPROUT::equal_to::value_type>() + ); + } } // namespace detail } // namespace sprout diff --git a/sprout/detail/algorithm/set_overlap_count.hpp b/sprout/detail/algorithm/set_overlap_count.hpp index 3c731902..b4f5a8e6 100644 --- a/sprout/detail/algorithm/set_overlap_count.hpp +++ b/sprout/detail/algorithm/set_overlap_count.hpp @@ -4,6 +4,7 @@ #include #include #include +#include namespace sprout { namespace detail { @@ -27,9 +28,7 @@ namespace sprout { : 0 ; } - // - // set_overlap_count - // + template inline SPROUT_CONSTEXPR typename std::iterator_traits::difference_type set_overlap_count( @@ -37,14 +36,7 @@ namespace sprout { InputIterator2 first2, InputIterator2 last2 ) { - return first1 != last1 && first2 != last2 - ? *first1 < *first2 - ? sprout::detail::set_overlap_count(sprout::next(first1), last1, first2, last2) - : *first2 < *first1 - ? sprout::detail::set_overlap_count(first1, last1, sprout::next(first2), last2) - : 1 + sprout::detail::set_overlap_count(sprout::next(first1), last1, sprout::next(first2), last2) - : 0 - ; + return sprout::detail::set_overlap_count(first1, last1, first2, last2, sprout::less<>()); } } // namespace detail } // namespace sprout