From 9b0d1a79333faed2c8c2bc755ffac0dde67756c4 Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Thu, 13 Dec 2012 13:03:18 +0900 Subject: [PATCH] fix recursion depth: find_end --- libs/algorithm/test/find_end.cpp | 12 +++---- sprout/algorithm/find_end.hpp | 59 ++++++++++++++++++++++++++++---- 2 files changed, 59 insertions(+), 12 deletions(-) diff --git a/libs/algorithm/test/find_end.cpp b/libs/algorithm/test/find_end.cpp index a3145232..da2f9ca0 100644 --- a/libs/algorithm/test/find_end.cpp +++ b/libs/algorithm/test/find_end.cpp @@ -10,8 +10,8 @@ namespace testspr { static void algorithm_find_end_test() { using namespace sprout; { - SPROUT_STATIC_CONSTEXPR auto arr1 = array{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}; - SPROUT_STATIC_CONSTEXPR auto arr2 = array{{4, 5, 6}}; + SPROUT_STATIC_CONSTEXPR auto arr1 = array{{1, 2, 3, 4, 5, 6, 7, 3, 4, 5}}; + SPROUT_STATIC_CONSTEXPR auto arr2 = array{{3, 4, 5}}; SPROUT_STATIC_CONSTEXPR auto arr3 = array{{6, 5, 4}}; { @@ -21,7 +21,7 @@ namespace testspr { sprout::begin(arr2), sprout::end(arr2) ); - TESTSPR_BOTH_ASSERT(found == sprout::begin(arr1) + 3); + TESTSPR_BOTH_ASSERT(found == sprout::begin(arr1) + 7); } { SPROUT_STATIC_CONSTEXPR auto found = sprout::find_end( @@ -39,7 +39,7 @@ namespace testspr { sprout::begin(arr2), sprout::end(arr2) ); - TESTSPR_BOTH_ASSERT(found == sprout::begin(arr1) + 5); + TESTSPR_BOTH_ASSERT(found == sprout::begin(arr1) + 2); } { SPROUT_STATIC_CONSTEXPR auto found = sprout::find_end( @@ -59,7 +59,7 @@ namespace testspr { sprout::end(arr2), testspr::equal_to() ); - TESTSPR_BOTH_ASSERT(found == sprout::begin(arr1) + 3); + TESTSPR_BOTH_ASSERT(found == sprout::begin(arr1) + 7); } { SPROUT_STATIC_CONSTEXPR auto found = sprout::find_end( @@ -79,7 +79,7 @@ namespace testspr { sprout::end(arr2), testspr::equal_to() ); - TESTSPR_BOTH_ASSERT(found == sprout::begin(arr1) + 5); + TESTSPR_BOTH_ASSERT(found == sprout::begin(arr1) + 2); } { SPROUT_STATIC_CONSTEXPR auto found = sprout::find_end( diff --git a/sprout/algorithm/find_end.hpp b/sprout/algorithm/find_end.hpp index 87e03b2c..95fa98c5 100644 --- a/sprout/algorithm/find_end.hpp +++ b/sprout/algorithm/find_end.hpp @@ -5,9 +5,60 @@ #include #include #include +#include namespace sprout { namespace detail { + template + inline SPROUT_CONSTEXPR RandomAccessIterator1 + find_end_impl_ra( + RandomAccessIterator1 first1, RandomAccessIterator1 last1, + ForwardIterator2 first2, ForwardIterator2 last2, + BinaryPredicate pred, + typename std::iterator_traits::difference_type pivot, RandomAccessIterator1 last1_, RandomAccessIterator1 result, + RandomAccessIterator1 searched + ) + { + return searched < first1 ? sprout::detail::find_end_impl_ra( + sprout::next(first1, pivot), last1, first2, last2, pred, + (NS_SSCRISK_CEL_OR_SPROUT::distance(first1, last1) - pivot) / 2, last1_, searched, + sprout::detail::find_end_impl_ra( + first1, sprout::next(first1, pivot), first2, last2, pred, + pivot / 2, last1_, searched, + first1 + ) + ) + : searched == last1_ ? result + : pivot == 0 ? sprout::detail::search_one(first1, last1_, first2, last2, pred) + : sprout::detail::find_end_impl_ra( + sprout::next(first1, pivot), last1, first2, last2, pred, + (NS_SSCRISK_CEL_OR_SPROUT::distance(first1, last1) - pivot) / 2, last1_, result, + sprout::detail::find_end_impl_ra( + first1, sprout::next(first1, pivot), first2, last2, pred, + pivot / 2, last1_, result, + first1 + ) + ) + ; + } + template + inline SPROUT_CONSTEXPR RandomAccessIterator1 + find_end( + RandomAccessIterator1 first1, RandomAccessIterator1 last1, + ForwardIterator2 first2, ForwardIterator2 last2, + BinaryPredicate pred, + std::random_access_iterator_tag* + ) + { + return first1 == last1 ? last1 + : sprout::detail::find_end_impl_ra( + first1, last1, first2, last2, pred, + NS_SSCRISK_CEL_OR_SPROUT::distance(first1, last1) / 2, last1, last1, + first1 + ) + ; + } + template inline SPROUT_CONSTEXPR ForwardIterator1 find_end_impl( @@ -20,7 +71,7 @@ namespace sprout { return first1 == last1 ? result : sprout::detail::find_end_impl( sprout::search(sprout::next(first1), last1, first2, last2, pred), last1, first2, last2, pred, - first1 + first1 ) ; } @@ -62,11 +113,7 @@ namespace sprout { ForwardIterator2 first2, ForwardIterator2 last2 ) { - return sprout::find_end( - first1, last1, - first2, last2, - sprout::equal_to<>() - ); + return sprout::find_end(first1, last1, first2, last2, sprout::equal_to<>()); } } // namespace sprout