mirror of
https://github.com/bolero-MURAKAMI/Sprout.git
synced 2025-07-02 14:04:20 +00:00
fix recursion depth: includes
This commit is contained in:
parent
eea1c2bbc1
commit
0201107eec
14 changed files with 442 additions and 71 deletions
|
@ -2,37 +2,37 @@
|
|||
#define SPROUT_ALGORITHM_BINARY_SEARCH_HPP
|
||||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/iterator/operation.hpp>
|
||||
#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT
|
||||
#include <sprout/algorithm/lower_bound.hpp>
|
||||
#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT
|
||||
|
||||
namespace sprout {
|
||||
// Copyright (C) 2011 RiSK (sscrisk)
|
||||
namespace detail {
|
||||
template<typename ForwardIterator, typename T, typename Compare>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
binary_search_impl(ForwardIterator first, ForwardIterator last, T const& value, Compare comp) {
|
||||
return (first != last && !comp(value, *first));
|
||||
}
|
||||
} // namespace detail
|
||||
|
||||
// 25.4.3.4 binary_search
|
||||
template<typename ForwardIterator, typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
binary_search(ForwardIterator first, ForwardIterator last, T const& value) {
|
||||
return first == last ? false
|
||||
: sprout::next(first) == last ? !(*first < value) && !(value < *first) ? true : false
|
||||
: *sprout::next(first, NS_SSCRISK_CEL_OR_SPROUT::distance(first, last) / 2) < value
|
||||
? sprout::binary_search(sprout::next(first, NS_SSCRISK_CEL_OR_SPROUT::distance(first, last) / 2), last, value)
|
||||
: value < *sprout::next(first, NS_SSCRISK_CEL_OR_SPROUT::distance(first, last)) / 2
|
||||
? sprout::binary_search(first, sprout::next(first, NS_SSCRISK_CEL_OR_SPROUT::distance(first, last) / 2), value)
|
||||
: true
|
||||
;
|
||||
}
|
||||
|
||||
//
|
||||
// recursion depth:
|
||||
// [first, last) is RandomAccessIterator -> O(log N)
|
||||
// otherwise -> O(N)
|
||||
//
|
||||
template<typename ForwardIterator, typename T, typename Compare>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
binary_search(ForwardIterator first, ForwardIterator last, T const& value, Compare comp) {
|
||||
return first == last ? false
|
||||
: sprout::next(first) == last ? !comp(*first, value) && !comp(value, *first) ? true : false
|
||||
: comp(*sprout::next(first, NS_SSCRISK_CEL_OR_SPROUT::distance(first, last) / 2), value)
|
||||
? sprout::binary_search(sprout::next(first, NS_SSCRISK_CEL_OR_SPROUT::distance(first, last) / 2), last, value)
|
||||
: comp(value, *sprout::next(first, NS_SSCRISK_CEL_OR_SPROUT::distance(first, last)) / 2)
|
||||
? sprout::binary_search(first, sprout::next(first, NS_SSCRISK_CEL_OR_SPROUT::distance(first, last) / 2), value)
|
||||
: true
|
||||
;
|
||||
return sprout::detail::binary_search_impl(sprout::lower_bound(first, last, value), last, value, comp);
|
||||
}
|
||||
|
||||
template<typename ForwardIterator, typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
binary_search(ForwardIterator first, ForwardIterator last, T const& value) {
|
||||
return sprout::binary_search(
|
||||
first, last, value,
|
||||
NS_SSCRISK_CEL_OR_SPROUT::less<typename std::iterator_traits<ForwardIterator>::value_type>()
|
||||
);
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
|
|
|
@ -5,22 +5,31 @@
|
|||
#include <sprout/algorithm/lower_bound.hpp>
|
||||
#include <sprout/algorithm/upper_bound.hpp>
|
||||
#include <sprout/utility/pair.hpp>
|
||||
#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT
|
||||
|
||||
namespace sprout {
|
||||
// Copyright (C) 2011 RiSK (sscrisk)
|
||||
|
||||
// 25.4.3.3 equal_range
|
||||
template<typename ForwardIterator, typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::pair<ForwardIterator, ForwardIterator>
|
||||
equal_range(ForwardIterator first, ForwardIterator last, T const& value) {
|
||||
return {sprout::lower_bound(first, last, value), sprout::upper_bound(first, last, value)};
|
||||
}
|
||||
|
||||
//
|
||||
// recursion depth:
|
||||
// [first, last) is RandomAccessIterator -> O(log N)
|
||||
// otherwise -> O(N)
|
||||
//
|
||||
template<typename ForwardIterator, typename T, typename Compare>
|
||||
inline SPROUT_CONSTEXPR pair<ForwardIterator, ForwardIterator>
|
||||
equal_range(ForwardIterator first, ForwardIterator last, T const& value, Compare comp) {
|
||||
return {sprout::lower_bound(first, last, value, comp), sprout::upper_bound(first, last, value, comp)};
|
||||
}
|
||||
|
||||
template<typename ForwardIterator, typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::pair<ForwardIterator, ForwardIterator>
|
||||
equal_range(ForwardIterator first, ForwardIterator last, T const& value) {
|
||||
return sprout::equal_range(
|
||||
first, last, value,
|
||||
NS_SSCRISK_CEL_OR_SPROUT::less<typename std::iterator_traits<ForwardIterator>::value_type>()
|
||||
);
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_ALGORITHM_EQUAL_RANGE_HPP
|
||||
|
|
|
@ -1,33 +1,94 @@
|
|||
#ifndef SPROUT_ALGORITHM_INCLUDES_HPP
|
||||
#define SPROUT_ALGORITHM_INCLUDES_HPP
|
||||
|
||||
#include <iterator>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/iterator/operation.hpp>
|
||||
#include <sprout/functional/less.hpp>
|
||||
#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT
|
||||
|
||||
namespace sprout {
|
||||
// Copyright (C) 2011 RiSK (sscrisk)
|
||||
namespace detail {
|
||||
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename Compare>
|
||||
inline SPROUT_CONSTEXPR RandomAccessIterator2
|
||||
includes_impl_ra(
|
||||
RandomAccessIterator1 first1, RandomAccessIterator1 last1, RandomAccessIterator2 first2, RandomAccessIterator2 last2, Compare comp,
|
||||
typename std::iterator_traits<RandomAccessIterator1>::difference_type pivot
|
||||
)
|
||||
{
|
||||
return first2 == last2 ? first2
|
||||
: pivot == 0 ? (!comp(*first1, *first2) && !comp(*first2, *first1) ? sprout::next(first2) : first2)
|
||||
: sprout::detail::includes_impl_ra(
|
||||
sprout::next(first1, pivot), last1,
|
||||
sprout::detail::includes_impl_ra(
|
||||
first1, sprout::next(first1, pivot),
|
||||
first2,
|
||||
last2, comp,
|
||||
pivot / 2
|
||||
),
|
||||
last2, comp,
|
||||
(NS_SSCRISK_CEL_OR_SPROUT::distance(first1, last1) - pivot) / 2
|
||||
)
|
||||
;
|
||||
}
|
||||
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename Compare>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
includes(
|
||||
RandomAccessIterator1 first1, RandomAccessIterator1 last1, RandomAccessIterator2 first2, RandomAccessIterator2 last2, Compare comp,
|
||||
std::random_access_iterator_tag*
|
||||
)
|
||||
{
|
||||
return first1 == last1 ? first2 == last2
|
||||
: sprout::detail::includes_impl_ra(
|
||||
first1, last1, first2, last2, comp,
|
||||
NS_SSCRISK_CEL_OR_SPROUT::distance(first1, last1) / 2
|
||||
)
|
||||
== last2
|
||||
;
|
||||
}
|
||||
|
||||
// Copyright (C) 2011 RiSK (sscrisk)
|
||||
template<typename InputIterator1, typename InputIterator2, typename Compare>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
includes_impl(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp) {
|
||||
return first2 == last2 ? true
|
||||
: first1 == last1 ? false
|
||||
: !comp(*first1, *first2) && !comp(*first2, *first1)
|
||||
? sprout::detail::includes_impl(sprout::next(first1), last1, sprout::next(first2), last2)
|
||||
: sprout::detail::includes_impl(sprout::next(first1), last1, first2, last2)
|
||||
;
|
||||
}
|
||||
template<typename InputIterator1, typename InputIterator2, typename Compare>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
includes(
|
||||
InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp,
|
||||
void*
|
||||
)
|
||||
{
|
||||
return sprout::detail::includes_impl(first1, last1, first2, comp);
|
||||
}
|
||||
} //namespace detail
|
||||
|
||||
// 25.4.5.1 includes
|
||||
template<typename InputIterator1, typename InputIterator2>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2) {
|
||||
return first2 == last2 ? true
|
||||
: first1 == last1 ? false
|
||||
: !(*first1 < *first2) && !(*first2 < *first1)
|
||||
? sprout::includes(sprout::next(first1), last1, sprout::next(first2), last2)
|
||||
: sprout::includes(sprout::next(first1), last1, first2, last2)
|
||||
;
|
||||
}
|
||||
|
||||
//
|
||||
// recursion depth:
|
||||
// [first, last) is RandomAccessIterator -> O(log N)
|
||||
// otherwise -> O(N)
|
||||
//
|
||||
template<typename InputIterator1, typename InputIterator2, typename Compare>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp) {
|
||||
return first2 == last2 ? true
|
||||
: first1 == last1 ? false
|
||||
: !comp(*first1, *first2) && !comp(*first2, *first1)
|
||||
? sprout::includes(sprout::next(first1), last1, sprout::next(first2), last2)
|
||||
: sprout::includes(sprout::next(first1), last1, first2, last2)
|
||||
;
|
||||
typedef typename std::iterator_traits<InputIterator1>::iterator_category* category;
|
||||
return sprout::detail::includes(first1, last1, first2, last2, comp, category());
|
||||
}
|
||||
|
||||
template<typename InputIterator1, typename InputIterator2>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2) {
|
||||
return sprout::includes(
|
||||
first1, last1, first2, last2,
|
||||
sprout::less<>()
|
||||
);
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
|
|
|
@ -56,6 +56,11 @@ namespace sprout {
|
|||
} //namespace detail
|
||||
|
||||
// 25.4.1.5 is_sorted
|
||||
//
|
||||
// recursion depth:
|
||||
// [first, last) is RandomAccessIterator -> O(log N)
|
||||
// otherwise -> O(N)
|
||||
//
|
||||
template<typename ForwardIterator, typename Compare>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
is_sorted(ForwardIterator first, ForwardIterator last, Compare comp) {
|
||||
|
|
|
@ -69,6 +69,11 @@ namespace sprout {
|
|||
} //namespace detail
|
||||
|
||||
// 25.4.1.5 is_sorted
|
||||
//
|
||||
// recursion depth:
|
||||
// [first, last) is RandomAccessIterator -> O(log N)
|
||||
// otherwise -> O(N)
|
||||
//
|
||||
template<typename ForwardIterator, typename Compare>
|
||||
inline SPROUT_CONSTEXPR ForwardIterator
|
||||
is_sorted_until(ForwardIterator first, ForwardIterator last, Compare comp) {
|
||||
|
|
|
@ -1,25 +1,21 @@
|
|||
#ifndef SPROUT_ALGORITHM_LOWER_BOUND_HPP
|
||||
#define SPROUT_ALGORITHM_LOWER_BOUND_HPP
|
||||
|
||||
#include <iterator>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/iterator/operation.hpp>
|
||||
#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT
|
||||
#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT
|
||||
|
||||
namespace sprout {
|
||||
// Copyright (C) 2011 RiSK (sscrisk)
|
||||
|
||||
// 25.4.3.1 lower_bound
|
||||
template<typename ForwardIterator, typename T>
|
||||
inline SPROUT_CONSTEXPR ForwardIterator
|
||||
lower_bound(ForwardIterator first, ForwardIterator last, T const& value) {
|
||||
return first == last ? last
|
||||
: sprout::next(first) == last ? *first < value ? last : first
|
||||
: *sprout::next(first, NS_SSCRISK_CEL_OR_SPROUT::distance(first, last) / 2) < value
|
||||
? sprout::lower_bound(sprout::next(first, NS_SSCRISK_CEL_OR_SPROUT::distance(first, last) / 2), last, value)
|
||||
: sprout::lower_bound(first, sprout::next(first, NS_SSCRISK_CEL_OR_SPROUT::distance(first, last) / 2), value)
|
||||
;
|
||||
}
|
||||
|
||||
//
|
||||
// recursion depth:
|
||||
// [first, last) is RandomAccessIterator -> O(log N)
|
||||
// otherwise -> O(N)
|
||||
//
|
||||
template<typename ForwardIterator, typename T, typename Compare>
|
||||
inline SPROUT_CONSTEXPR ForwardIterator
|
||||
lower_bound(ForwardIterator first, ForwardIterator last, T const& value, Compare comp) {
|
||||
|
@ -30,6 +26,15 @@ namespace sprout {
|
|||
: sprout::lower_bound(first, sprout::next(first, NS_SSCRISK_CEL_OR_SPROUT::distance(first, last) / 2), value, comp)
|
||||
;
|
||||
}
|
||||
|
||||
template<typename ForwardIterator, typename T>
|
||||
inline SPROUT_CONSTEXPR ForwardIterator
|
||||
lower_bound(ForwardIterator first, ForwardIterator last, T const& value) {
|
||||
return sprout::lower_bound(
|
||||
first, last, value,
|
||||
NS_SSCRISK_CEL_OR_SPROUT::less<typename std::iterator_traits<ForwardIterator>::value_type>()
|
||||
);
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_ALGORITHM_LOWER_BOUND_HPP
|
||||
|
|
|
@ -1,25 +1,21 @@
|
|||
#ifndef SPROUT_ALGORITHM_UPPER_BOUND_HPP
|
||||
#define SPROUT_ALGORITHM_UPPER_BOUND_HPP
|
||||
|
||||
#include <iterator>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/iterator/operation.hpp>
|
||||
#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT
|
||||
#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT
|
||||
|
||||
namespace sprout {
|
||||
// Copyright (C) 2011 RiSK (sscrisk)
|
||||
|
||||
// 25.4.3.2 upper_bound
|
||||
template<typename ForwardIterator, typename T>
|
||||
inline SPROUT_CONSTEXPR ForwardIterator
|
||||
upper_bound(ForwardIterator first, ForwardIterator last, T const& value) {
|
||||
return first == last ? last
|
||||
: sprout::next(first) == last ? !(value < *first) ? last : first
|
||||
: !(value < *sprout::next(first, NS_SSCRISK_CEL_OR_SPROUT::distance(first, last) / 2))
|
||||
? upper_bound(sprout::next(first, NS_SSCRISK_CEL_OR_SPROUT::distance(first, last) / 2), last, value)
|
||||
: sprout::upper_bound(first, sprout::next(first, NS_SSCRISK_CEL_OR_SPROUT::distance(first, last) / 2), value)
|
||||
;
|
||||
}
|
||||
|
||||
//
|
||||
// recursion depth:
|
||||
// [first, last) is RandomAccessIterator -> O(log N)
|
||||
// otherwise -> O(N)
|
||||
//
|
||||
template<typename ForwardIterator, typename T, typename Compare>
|
||||
inline SPROUT_CONSTEXPR ForwardIterator
|
||||
upper_bound(ForwardIterator first, ForwardIterator last, T const& value, Compare comp) {
|
||||
|
@ -30,6 +26,15 @@ namespace sprout {
|
|||
: sprout::upper_bound(first, sprout::next(first, NS_SSCRISK_CEL_OR_SPROUT::distance(first, last) / 2), value, comp)
|
||||
;
|
||||
}
|
||||
|
||||
template<typename ForwardIterator, typename T>
|
||||
inline SPROUT_CONSTEXPR ForwardIterator
|
||||
upper_bound(ForwardIterator first, ForwardIterator last, T const& value) {
|
||||
return sprout::upper_bound(
|
||||
first, last, value,
|
||||
NS_SSCRISK_CEL_OR_SPROUT::less<typename std::iterator_traits<ForwardIterator>::value_type>()
|
||||
);
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_ALGORITHM_UPPER_BOUND_HPP
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue