mirror of
https://github.com/bolero-MURAKAMI/Sprout.git
synced 2024-11-14 10:39:05 +00:00
Merge pull request #30 from kariya-mitsuru/equal_range
[algorithm.equal_range, algorithm.lower_bound, algorithm.upper_bound]
This commit is contained in:
commit
85ac03fa5f
5 changed files with 97 additions and 25 deletions
|
@ -13,10 +13,30 @@
|
|||
#include <sprout/config.hpp>
|
||||
#include <sprout/algorithm/lower_bound.hpp>
|
||||
#include <sprout/algorithm/upper_bound.hpp>
|
||||
#include <sprout/iterator/operation.hpp>
|
||||
#include <sprout/utility/pair/pair.hpp>
|
||||
#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT
|
||||
|
||||
namespace sprout {
|
||||
namespace detail {
|
||||
template<typename ForwardIterator, typename T, typename Compare>
|
||||
inline SPROUT_CONSTEXPR sprout::pair<ForwardIterator, ForwardIterator>
|
||||
equal_range(
|
||||
ForwardIterator first, typename std::iterator_traits<ForwardIterator>::difference_type len,
|
||||
T const& value, Compare comp
|
||||
)
|
||||
{
|
||||
return len == 0 ? sprout::pair<ForwardIterator, ForwardIterator>(first, first)
|
||||
: comp(*sprout::next(first, len / 2), value)
|
||||
? sprout::detail::equal_range(sprout::next(first, len / 2 + 1), len - (len / 2 + 1), value, comp)
|
||||
: comp(value, *sprout::next(first, len / 2))
|
||||
? sprout::detail::equal_range(first, len / 2, value, comp)
|
||||
: sprout::pair<ForwardIterator, ForwardIterator>(
|
||||
sprout::detail::lower_bound(first, len / 2, value, comp),
|
||||
sprout::detail::upper_bound(sprout::next(first, len / 2 + 1), len - (len / 2 + 1), value, comp)
|
||||
)
|
||||
;
|
||||
}
|
||||
} // namespace detail
|
||||
|
||||
// 25.4.3.3 equal_range
|
||||
//
|
||||
|
@ -26,19 +46,13 @@ namespace sprout {
|
|||
template<typename ForwardIterator, typename T, typename Compare>
|
||||
inline SPROUT_CONSTEXPR sprout::pair<ForwardIterator, ForwardIterator>
|
||||
equal_range(ForwardIterator first, ForwardIterator last, T const& value, Compare comp) {
|
||||
return sprout::pair<ForwardIterator, ForwardIterator>(
|
||||
sprout::lower_bound(first, last, value, comp),
|
||||
sprout::upper_bound(first, last, value, comp)
|
||||
);
|
||||
return sprout::detail::equal_range(first, sprout::distance(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>()
|
||||
);
|
||||
return sprout::equal_range(first, last, value, sprout::less<>());
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
|
|
|
@ -9,10 +9,10 @@
|
|||
#ifndef SPROUT_ALGORITHM_LOWER_BOUND_HPP
|
||||
#define SPROUT_ALGORITHM_LOWER_BOUND_HPP
|
||||
|
||||
#include <iterator>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/iterator/operation.hpp>
|
||||
#include <sprout/iterator/distance.hpp>
|
||||
#include <sprout/functional/less.hpp>
|
||||
#include <sprout/detail/algorithm/lower_bound.hpp>
|
||||
|
||||
namespace sprout {
|
||||
|
||||
|
@ -24,12 +24,7 @@ namespace sprout {
|
|||
template<typename ForwardIterator, typename T, typename Compare>
|
||||
inline SPROUT_CONSTEXPR ForwardIterator
|
||||
lower_bound(ForwardIterator first, ForwardIterator last, T const& value, Compare comp) {
|
||||
return first == last ? last
|
||||
: sprout::next(first) == last ? comp(*first, value) ? last : first
|
||||
: comp(*sprout::next(first, sprout::distance(first, last) / 2), value)
|
||||
? sprout::lower_bound(sprout::next(first, sprout::distance(first, last) / 2 + 1), last, value, comp)
|
||||
: sprout::lower_bound(first, sprout::next(first, sprout::distance(first, last) / 2), value, comp)
|
||||
;
|
||||
return sprout::detail::lower_bound(first, sprout::distance(first, last), value, comp);
|
||||
}
|
||||
|
||||
template<typename ForwardIterator, typename T>
|
||||
|
|
|
@ -9,10 +9,10 @@
|
|||
#ifndef SPROUT_ALGORITHM_UPPER_BOUND_HPP
|
||||
#define SPROUT_ALGORITHM_UPPER_BOUND_HPP
|
||||
|
||||
#include <iterator>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/iterator/operation.hpp>
|
||||
#include <sprout/iterator/distance.hpp>
|
||||
#include <sprout/functional/less.hpp>
|
||||
#include <sprout/detail/algorithm/upper_bound.hpp>
|
||||
|
||||
namespace sprout {
|
||||
|
||||
|
@ -24,12 +24,7 @@ namespace sprout {
|
|||
template<typename ForwardIterator, typename T, typename Compare>
|
||||
inline SPROUT_CONSTEXPR ForwardIterator
|
||||
upper_bound(ForwardIterator first, ForwardIterator last, T const& value, Compare comp) {
|
||||
return first == last ? last
|
||||
: sprout::next(first) == last ? !comp(value, *first) ? last : first
|
||||
: !comp(value, *sprout::next(first, sprout::distance(first, last) / 2))
|
||||
? sprout::upper_bound(sprout::next(first, sprout::distance(first, last) / 2 + 1), last, value, comp)
|
||||
: sprout::upper_bound(first, sprout::next(first, sprout::distance(first, last) / 2), value, comp)
|
||||
;
|
||||
return sprout::detail::upper_bound(first, sprout::distance(first, last), value, comp);
|
||||
}
|
||||
|
||||
template<typename ForwardIterator, typename T>
|
||||
|
|
34
sprout/detail/algorithm/lower_bound.hpp
Normal file
34
sprout/detail/algorithm/lower_bound.hpp
Normal file
|
@ -0,0 +1,34 @@
|
|||
/*=============================================================================
|
||||
Copyright (c) 2011-2013 Bolero MURAKAMI
|
||||
Copyright (C) 2011 RiSK (sscrisk)
|
||||
https://github.com/bolero-MURAKAMI/Sprout
|
||||
|
||||
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
=============================================================================*/
|
||||
#ifndef SPROUT_DETAIL_ALGORITHM_LOWER_BOUND_HPP
|
||||
#define SPROUT_DETAIL_ALGORITHM_LOWER_BOUND_HPP
|
||||
|
||||
#include <iterator>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/iterator/next.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace detail {
|
||||
template<typename ForwardIterator, typename T, typename Compare>
|
||||
inline SPROUT_CONSTEXPR ForwardIterator
|
||||
lower_bound(
|
||||
ForwardIterator first, typename std::iterator_traits<ForwardIterator>::difference_type len,
|
||||
T const& value, Compare comp
|
||||
)
|
||||
{
|
||||
return len == 0 ? first
|
||||
: comp(*sprout::next(first, len / 2), value)
|
||||
? sprout::detail::lower_bound(sprout::next(first, len / 2 + 1), len - (len / 2 + 1), value, comp)
|
||||
: sprout::detail::lower_bound(first, len / 2, value, comp)
|
||||
;
|
||||
}
|
||||
} // namespace detail
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_DETAIL_ALGORITHM_LOWER_BOUND_HPP
|
34
sprout/detail/algorithm/upper_bound.hpp
Normal file
34
sprout/detail/algorithm/upper_bound.hpp
Normal file
|
@ -0,0 +1,34 @@
|
|||
/*=============================================================================
|
||||
Copyright (c) 2011-2013 Bolero MURAKAMI
|
||||
Copyright (C) 2011 RiSK (sscrisk)
|
||||
https://github.com/bolero-MURAKAMI/Sprout
|
||||
|
||||
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
=============================================================================*/
|
||||
#ifndef SPROUT_DETAIL_ALGORITHM_UPPER_BOUND_HPP
|
||||
#define SPROUT_DETAIL_ALGORITHM_UPPER_BOUND_HPP
|
||||
|
||||
#include <iterator>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/iterator/next.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace detail {
|
||||
template<typename ForwardIterator, typename T, typename Compare>
|
||||
inline SPROUT_CONSTEXPR ForwardIterator
|
||||
upper_bound(
|
||||
ForwardIterator first, typename std::iterator_traits<ForwardIterator>::difference_type len,
|
||||
T const& value, Compare comp
|
||||
)
|
||||
{
|
||||
return len == 0 ? first
|
||||
: comp(value, *sprout::next(first, len / 2))
|
||||
? sprout::detail::upper_bound(first, len / 2, value, comp)
|
||||
: sprout::detail::upper_bound(sprout::next(first, len / 2 + 1), len - (len / 2 + 1), value, comp)
|
||||
;
|
||||
}
|
||||
} // namespace detail
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_DETAIL_ALGORITHM_UPPER_BOUND_HPP
|
Loading…
Reference in a new issue