1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2024-11-12 21:09:01 +00:00

fix recursion depth: search

This commit is contained in:
bolero-MURAKAMI 2012-12-13 12:18:19 +09:00
parent cf9f6670b6
commit 90890d2336
5 changed files with 281 additions and 24 deletions

View file

@ -21,6 +21,7 @@
#include "./mismatch.cpp" #include "./mismatch.cpp"
#include "./equal.cpp" #include "./equal.cpp"
#include "./is_permutation.cpp" #include "./is_permutation.cpp"
#include "./search.cpp"
#ifdef TESTSPR_CPP_INCLUDE_DISABLE_SPROUT_LIBS_ALGORITHM_TEST_NON_MODIFYIING_CPP #ifdef TESTSPR_CPP_INCLUDE_DISABLE_SPROUT_LIBS_ALGORITHM_TEST_NON_MODIFYIING_CPP
# undef TESTSPR_CPP_INCLUDE # undef TESTSPR_CPP_INCLUDE
@ -43,6 +44,7 @@ namespace testspr {
testspr::algorithm_mismatch_test(); testspr::algorithm_mismatch_test();
testspr::algorithm_equal_test(); testspr::algorithm_equal_test();
testspr::algorithm_is_permutation_test(); testspr::algorithm_is_permutation_test();
testspr::algorithm_search_test();
} }
} // namespace testspr } // namespace testspr

View file

@ -0,0 +1,103 @@
#ifndef SPROUT_LIBS_ALGORITHM_TEST_SEARCH_CPP
#define SPROUT_LIBS_ALGORITHM_TEST_SEARCH_CPP
#include <sprout/algorithm/search.hpp>
#include <sprout/array.hpp>
#include <sprout/container.hpp>
#include <testspr/tools.hpp>
namespace testspr {
static void algorithm_search_test() {
using namespace sprout;
{
SPROUT_STATIC_CONSTEXPR auto arr1 = array<int, 10>{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}};
SPROUT_STATIC_CONSTEXPR auto arr2 = array<int, 3>{{4, 5, 6}};
SPROUT_STATIC_CONSTEXPR auto arr3 = array<int, 3>{{6, 5, 4}};
{
SPROUT_STATIC_CONSTEXPR auto found = sprout::search(
sprout::begin(arr1),
sprout::end(arr1),
sprout::begin(arr2),
sprout::end(arr2)
);
TESTSPR_BOTH_ASSERT(found == sprout::begin(arr1) + 3);
}
{
SPROUT_STATIC_CONSTEXPR auto found = sprout::search(
sprout::begin(arr1),
sprout::end(arr1),
sprout::begin(arr3),
sprout::end(arr3)
);
TESTSPR_BOTH_ASSERT(found == sprout::end(arr1));
}
{
SPROUT_STATIC_CONSTEXPR auto found = sprout::search(
sprout::begin(arr1),
sprout::begin(arr1) + 5,
sprout::begin(arr2),
sprout::end(arr2)
);
TESTSPR_BOTH_ASSERT(found == sprout::begin(arr1) + 5);
}
{
SPROUT_STATIC_CONSTEXPR auto found = sprout::search(
sprout::begin(arr1),
sprout::begin(arr1) + 5,
sprout::begin(arr3),
sprout::end(arr3)
);
TESTSPR_BOTH_ASSERT(found == sprout::begin(arr1) + 5);
}
{
SPROUT_STATIC_CONSTEXPR auto found = sprout::search(
sprout::begin(arr1),
sprout::end(arr1),
sprout::begin(arr2),
sprout::end(arr2),
testspr::equal_to<int>()
);
TESTSPR_BOTH_ASSERT(found == sprout::begin(arr1) + 3);
}
{
SPROUT_STATIC_CONSTEXPR auto found = sprout::search(
sprout::begin(arr1),
sprout::end(arr1),
sprout::begin(arr3),
sprout::end(arr3),
testspr::equal_to<int>()
);
TESTSPR_BOTH_ASSERT(found == sprout::end(arr1));
}
{
SPROUT_STATIC_CONSTEXPR auto found = sprout::search(
sprout::begin(arr1),
sprout::begin(arr1) + 5,
sprout::begin(arr2),
sprout::end(arr2),
testspr::equal_to<int>()
);
TESTSPR_BOTH_ASSERT(found == sprout::begin(arr1) + 5);
}
{
SPROUT_STATIC_CONSTEXPR auto found = sprout::search(
sprout::begin(arr1),
sprout::begin(arr1) + 5,
sprout::begin(arr3),
sprout::end(arr3),
testspr::equal_to<int>()
);
TESTSPR_BOTH_ASSERT(found == sprout::begin(arr1) + 5);
}
}
}
} // namespace testspr
#ifndef TESTSPR_CPP_INCLUDE
# define TESTSPR_TEST_FUNCTION testspr::algorithm_search_test
# include <testspr/include_main.hpp>
#endif
#endif // #ifndef SPROUT_LIBS_ALGORITHM_TEST_SEARCH_CPP

View file

@ -3,28 +3,91 @@
#include <sprout/config.hpp> #include <sprout/config.hpp>
#include <sprout/iterator/operation.hpp> #include <sprout/iterator/operation.hpp>
#include <sprout/functional/equal_to.hpp>
#include <sprout/detail/algorithm/search_one.hpp>
#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT #include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT
namespace sprout { namespace sprout {
// Copyright (C) 2011 RiSK (sscrisk) namespace detail {
template<typename RandomAccessIterator1, typename ForwardIterator2, typename BinaryPredicate>
// 25.2.13 Search inline SPROUT_CONSTEXPR RandomAccessIterator1
template<typename ForwardIterator1, typename ForwardIterator2> search_impl_ra(
inline SPROUT_CONSTEXPR ForwardIterator1 RandomAccessIterator1 first1, RandomAccessIterator1 last1,
search( ForwardIterator2 first2, ForwardIterator2 last2,
ForwardIterator1 first1, ForwardIterator1 last1, BinaryPredicate pred,
ForwardIterator2 first2, ForwardIterator2 last2 typename std::iterator_traits<RandomAccessIterator1>::difference_type pivot, RandomAccessIterator1 last1_, RandomAccessIterator1 searched
) )
{ {
return first1 == last1 || first2 == last2 ? first1 return searched < first1 || searched == last1_ ? searched
: NS_SSCRISK_CEL_OR_SPROUT::distance(first1, last1) < NS_SSCRISK_CEL_OR_SPROUT::distance(first2, last2) ? last1 : pivot == 0 ? sprout::detail::search_one(first1, last1_, first2, last2, pred)
: *first1 == *first2 : sprout::detail::search_impl_ra(
&& sprout::search(sprout::next(first1), last1, sprout::next(first2), last2) == sprout::next(first1) sprout::next(first1, pivot), last1, first2, last2, pred,
? first1 (NS_SSCRISK_CEL_OR_SPROUT::distance(first1, last1) - pivot) / 2, last1_,
: sprout::search(sprout::next(first1), last1, first2, last2) sprout::detail::search_impl_ra(
first1, sprout::next(first1, pivot), first2, last2, pred,
pivot / 2, last1_,
first1
)
)
;
}
template<typename RandomAccessIterator1, typename ForwardIterator2, typename BinaryPredicate>
inline SPROUT_CONSTEXPR RandomAccessIterator1
search(
RandomAccessIterator1 first1, RandomAccessIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2,
BinaryPredicate pred,
std::random_access_iterator_tag*
)
{
return first1 == last1 ? last1
: sprout::detail::search_impl_ra(
first1, last1, first2, last2, pred,
NS_SSCRISK_CEL_OR_SPROUT::distance(first1, last1) / 2, last1, first1
)
; ;
} }
template<typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate>
inline SPROUT_CONSTEXPR ForwardIterator1
search_impl(
ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2,
BinaryPredicate pred,
ForwardIterator1 searched
)
{
return searched == first1 || searched == last1 ? searched
: sprout::detail::search_impl(
sprout::next(first1), last1, first2, last2, pred,
sprout::detail::search_one(sprout::next(first1), last1, first2, last2, pred)
)
;
}
template<typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate>
inline SPROUT_CONSTEXPR ForwardIterator1
search(
ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2,
BinaryPredicate pred,
void*
)
{
return sprout::detail::search_impl(
first1, last1, first2, last2, pred,
sprout::detail::search_one(first1, last1, first2, last2, pred)
);
}
} //namespace detail
// 25.2.13 Search
//
// recursion depth:
// [first1, last1) is RandomAccessIterator -> O(log N1)
// otherwise -> O(N1)
// [first2, last2) is RandomAccessIterator -> O(log N2)
// otherwise -> O(N2)
//
template<typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate> template<typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate>
inline SPROUT_CONSTEXPR ForwardIterator1 inline SPROUT_CONSTEXPR ForwardIterator1
search( search(
@ -33,13 +96,18 @@ namespace sprout {
BinaryPredicate pred BinaryPredicate pred
) )
{ {
return first1 == last1 || first2 == last2 ? first1 typedef typename std::iterator_traits<ForwardIterator1>::iterator_category* category;
: NS_SSCRISK_CEL_OR_SPROUT::distance(first1, last1) < NS_SSCRISK_CEL_OR_SPROUT::distance(first2, last2) ? last1 return sprout::detail::search(first1, last1, first2, last2, pred, category());
: *first1 == *first2 }
&& sprout::search(sprout::next(first1), last1, sprout::next(first2), last2, pred) == sprout::next(first1)
? first1 template<typename ForwardIterator1, typename ForwardIterator2>
: sprout::search(sprout::next(first1), last1, first2, last2, pred) inline SPROUT_CONSTEXPR ForwardIterator1
; search(
ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2
)
{
return sprout::search(first1, last1, first2, last2, sprout::equal_to<>());
} }
} // namespace sprout } // namespace sprout

View file

@ -0,0 +1,83 @@
#ifndef SPROUT_DETAIL_ALGORITHM_SEARCH_ONE_HPP
#define SPROUT_DETAIL_ALGORITHM_SEARCH_ONE_HPP
#include <iterator>
#include <sprout/config.hpp>
#include <sprout/iterator/operation.hpp>
#include <sprout/functional/equal_to.hpp>
#include <sprout/algorithm/mismatch.hpp>
#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT
namespace sprout {
namespace detail {
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename BinaryPredicate>
inline SPROUT_CONSTEXPR RandomAccessIterator1
search_one_impl_ra(
RandomAccessIterator1 first1, RandomAccessIterator1 last1, RandomAccessIterator2 first2, RandomAccessIterator2 last2, BinaryPredicate pred
)
{
return sprout::detail::mismatch_impl_ra(
first1, last1, first2, pred,
NS_SSCRISK_CEL_OR_SPROUT::distance(first1, last1) / 2, first1
)
== last1
? first1
: last1
;
}
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename BinaryPredicate>
inline SPROUT_CONSTEXPR RandomAccessIterator1
search_one(
RandomAccessIterator1 first1, RandomAccessIterator1 last1, RandomAccessIterator2 first2, RandomAccessIterator2 last2, BinaryPredicate pred,
std::random_access_iterator_tag*
)
{
return NS_SSCRISK_CEL_OR_SPROUT::distance(first1, last1) < NS_SSCRISK_CEL_OR_SPROUT::distance(first2, last2) ? last1
: first1 == last1 ? first1
: sprout::detail::search_one_impl_ra(
first1, sprout::next(first1, NS_SSCRISK_CEL_OR_SPROUT::distance(first2, last2)), first2, last2, pred
)
;
}
template<typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate>
inline SPROUT_CONSTEXPR ForwardIterator1
search_one_impl(
ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred,
ForwardIterator1 first1_
)
{
return first2 == last2 ? first1_
: first1 == last1 ? last1
: !pred(*first1, *first2) ? first1
: sprout::detail::search_one_impl(sprout::next(first1), last1, sprout::next(first2), last2, pred, first1_)
;
}
template<typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate>
inline SPROUT_CONSTEXPR ForwardIterator1
search_one(
ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred,
void*
)
{
return sprout::detail::search_one_impl(first1, last1, first2, last2, pred, first1);
}
//
// search_one
//
template<typename ForwardIterator1, typename ForwardIterator2, typename BinaryPredicate>
inline SPROUT_CONSTEXPR ForwardIterator1
search_one(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, BinaryPredicate pred) {
typedef typename sprout::common_iterator_category<ForwardIterator1, ForwardIterator2>::type* category;
return sprout::detail::search_one(first1, last1, first2, last2, pred, category());
}
template<typename ForwardIterator1, typename ForwardIterator2>
inline SPROUT_CONSTEXPR ForwardIterator1
search_one(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2) {
return sprout::detail::search_one(first1, last1, first2, last2, sprout::equal_to<>());
}
} //namespace detail
} // namespace sprout
#endif // #ifndef SPROUT_DETAIL_ALGORITHM_SEARCH_ONE_HPP

View file

@ -8,6 +8,7 @@
#include <sprout/type_traits/inherit_if_xxx.hpp> #include <sprout/type_traits/inherit_if_xxx.hpp>
#include <sprout/functional/base.hpp> #include <sprout/functional/base.hpp>
#include <sprout/functional/type_traits/weak_result_type.hpp> #include <sprout/functional/type_traits/weak_result_type.hpp>
#include <sprout/functional/type_traits/has_type.hpp>
namespace sprout { namespace sprout {
// 20.8.3 reference_wrapper // 20.8.3 reference_wrapper