From 5b13f3b8a24bf40772703cbd5be515783a31b05d Mon Sep 17 00:00:00 2001 From: Mitsuru Kariya Date: Sun, 1 Sep 2013 00:13:23 +0900 Subject: [PATCH] [algorithm.equal_range, etc] Change recursive function parameters from "Iter, Iter" to "Iter, length." --- sprout/algorithm/equal_range.hpp | 32 ++++++++++++++++------- sprout/algorithm/lower_bound.hpp | 11 +++----- sprout/algorithm/upper_bound.hpp | 11 +++----- sprout/detail/algorithm/lower_bound.hpp | 34 +++++++++++++++++++++++++ sprout/detail/algorithm/upper_bound.hpp | 34 +++++++++++++++++++++++++ 5 files changed, 97 insertions(+), 25 deletions(-) create mode 100644 sprout/detail/algorithm/lower_bound.hpp create mode 100644 sprout/detail/algorithm/upper_bound.hpp diff --git a/sprout/algorithm/equal_range.hpp b/sprout/algorithm/equal_range.hpp index fc891363..590c9a7d 100644 --- a/sprout/algorithm/equal_range.hpp +++ b/sprout/algorithm/equal_range.hpp @@ -13,10 +13,30 @@ #include #include #include +#include #include -#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT namespace sprout { + namespace detail { + template + inline SPROUT_CONSTEXPR sprout::pair + equal_range( + ForwardIterator first, typename std::iterator_traits::difference_type len, + T const& value, Compare comp + ) + { + return len == 0 ? sprout::pair(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( + 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 inline SPROUT_CONSTEXPR sprout::pair equal_range(ForwardIterator first, ForwardIterator last, T const& value, Compare comp) { - return sprout::pair( - 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 inline SPROUT_CONSTEXPR sprout::pair equal_range(ForwardIterator first, ForwardIterator last, T const& value) { - return sprout::equal_range( - first, last, value, - NS_SSCRISK_CEL_OR_SPROUT::less::value_type>() - ); + return sprout::equal_range(first, last, value, sprout::less<>()); } } // namespace sprout diff --git a/sprout/algorithm/lower_bound.hpp b/sprout/algorithm/lower_bound.hpp index eff2721e..ed15284e 100644 --- a/sprout/algorithm/lower_bound.hpp +++ b/sprout/algorithm/lower_bound.hpp @@ -9,10 +9,10 @@ #ifndef SPROUT_ALGORITHM_LOWER_BOUND_HPP #define SPROUT_ALGORITHM_LOWER_BOUND_HPP -#include #include -#include +#include #include +#include namespace sprout { @@ -24,12 +24,7 @@ namespace sprout { template 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 diff --git a/sprout/algorithm/upper_bound.hpp b/sprout/algorithm/upper_bound.hpp index 56532f7c..9e96f589 100644 --- a/sprout/algorithm/upper_bound.hpp +++ b/sprout/algorithm/upper_bound.hpp @@ -9,10 +9,10 @@ #ifndef SPROUT_ALGORITHM_UPPER_BOUND_HPP #define SPROUT_ALGORITHM_UPPER_BOUND_HPP -#include #include -#include +#include #include +#include namespace sprout { @@ -24,12 +24,7 @@ namespace sprout { template 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 diff --git a/sprout/detail/algorithm/lower_bound.hpp b/sprout/detail/algorithm/lower_bound.hpp new file mode 100644 index 00000000..2f43c7be --- /dev/null +++ b/sprout/detail/algorithm/lower_bound.hpp @@ -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 +#include +#include + +namespace sprout { + namespace detail { + template + inline SPROUT_CONSTEXPR ForwardIterator + lower_bound( + ForwardIterator first, typename std::iterator_traits::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 diff --git a/sprout/detail/algorithm/upper_bound.hpp b/sprout/detail/algorithm/upper_bound.hpp new file mode 100644 index 00000000..7b688cbd --- /dev/null +++ b/sprout/detail/algorithm/upper_bound.hpp @@ -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 +#include +#include + +namespace sprout { + namespace detail { + template + inline SPROUT_CONSTEXPR ForwardIterator + upper_bound( + ForwardIterator first, typename std::iterator_traits::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