[algorithm.equal_range, etc] Change recursive function parameters from "Iter, Iter" to "Iter, length."

This commit is contained in:
Mitsuru Kariya 2013-09-01 00:13:23 +09:00
parent bbeb2f1b4b
commit 5b13f3b8a2
5 changed files with 97 additions and 25 deletions

View file

@ -13,10 +13,30 @@
#include <sprout/config.hpp> #include <sprout/config.hpp>
#include <sprout/algorithm/lower_bound.hpp> #include <sprout/algorithm/lower_bound.hpp>
#include <sprout/algorithm/upper_bound.hpp> #include <sprout/algorithm/upper_bound.hpp>
#include <sprout/iterator/operation.hpp>
#include <sprout/utility/pair/pair.hpp> #include <sprout/utility/pair/pair.hpp>
#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT
namespace 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 // 25.4.3.3 equal_range
// //
@ -26,19 +46,13 @@ namespace sprout {
template<typename ForwardIterator, typename T, typename Compare> template<typename ForwardIterator, typename T, typename Compare>
inline SPROUT_CONSTEXPR sprout::pair<ForwardIterator, ForwardIterator> inline SPROUT_CONSTEXPR sprout::pair<ForwardIterator, ForwardIterator>
equal_range(ForwardIterator first, ForwardIterator last, T const& value, Compare comp) { equal_range(ForwardIterator first, ForwardIterator last, T const& value, Compare comp) {
return sprout::pair<ForwardIterator, ForwardIterator>( return sprout::detail::equal_range(first, sprout::distance(first, last), value, comp);
sprout::lower_bound(first, last, value, comp),
sprout::upper_bound(first, last, value, comp)
);
} }
template<typename ForwardIterator, typename T> template<typename ForwardIterator, typename T>
inline SPROUT_CONSTEXPR sprout::pair<ForwardIterator, ForwardIterator> inline SPROUT_CONSTEXPR sprout::pair<ForwardIterator, ForwardIterator>
equal_range(ForwardIterator first, ForwardIterator last, T const& value) { equal_range(ForwardIterator first, ForwardIterator last, T const& value) {
return sprout::equal_range( return sprout::equal_range(first, last, value, sprout::less<>());
first, last, value,
NS_SSCRISK_CEL_OR_SPROUT::less<typename std::iterator_traits<ForwardIterator>::value_type>()
);
} }
} // namespace sprout } // namespace sprout

View file

@ -9,10 +9,10 @@
#ifndef SPROUT_ALGORITHM_LOWER_BOUND_HPP #ifndef SPROUT_ALGORITHM_LOWER_BOUND_HPP
#define SPROUT_ALGORITHM_LOWER_BOUND_HPP #define SPROUT_ALGORITHM_LOWER_BOUND_HPP
#include <iterator>
#include <sprout/config.hpp> #include <sprout/config.hpp>
#include <sprout/iterator/operation.hpp> #include <sprout/iterator/distance.hpp>
#include <sprout/functional/less.hpp> #include <sprout/functional/less.hpp>
#include <sprout/detail/algorithm/lower_bound.hpp>
namespace sprout { namespace sprout {
@ -24,12 +24,7 @@ namespace sprout {
template<typename ForwardIterator, typename T, typename Compare> template<typename ForwardIterator, typename T, typename Compare>
inline SPROUT_CONSTEXPR ForwardIterator inline SPROUT_CONSTEXPR ForwardIterator
lower_bound(ForwardIterator first, ForwardIterator last, T const& value, Compare comp) { lower_bound(ForwardIterator first, ForwardIterator last, T const& value, Compare comp) {
return first == last ? last return sprout::detail::lower_bound(first, sprout::distance(first, last), value, comp);
: 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)
;
} }
template<typename ForwardIterator, typename T> template<typename ForwardIterator, typename T>

View file

@ -9,10 +9,10 @@
#ifndef SPROUT_ALGORITHM_UPPER_BOUND_HPP #ifndef SPROUT_ALGORITHM_UPPER_BOUND_HPP
#define SPROUT_ALGORITHM_UPPER_BOUND_HPP #define SPROUT_ALGORITHM_UPPER_BOUND_HPP
#include <iterator>
#include <sprout/config.hpp> #include <sprout/config.hpp>
#include <sprout/iterator/operation.hpp> #include <sprout/iterator/distance.hpp>
#include <sprout/functional/less.hpp> #include <sprout/functional/less.hpp>
#include <sprout/detail/algorithm/upper_bound.hpp>
namespace sprout { namespace sprout {
@ -24,12 +24,7 @@ namespace sprout {
template<typename ForwardIterator, typename T, typename Compare> template<typename ForwardIterator, typename T, typename Compare>
inline SPROUT_CONSTEXPR ForwardIterator inline SPROUT_CONSTEXPR ForwardIterator
upper_bound(ForwardIterator first, ForwardIterator last, T const& value, Compare comp) { upper_bound(ForwardIterator first, ForwardIterator last, T const& value, Compare comp) {
return first == last ? last return sprout::detail::upper_bound(first, sprout::distance(first, last), value, comp);
: 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)
;
} }
template<typename ForwardIterator, typename T> template<typename ForwardIterator, typename T>

View 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

View 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