2015-04-13 02:40:49 +00:00
|
|
|
/*=============================================================================
|
2016-02-25 09:48:28 +00:00
|
|
|
Copyright (c) 2011-2016 Bolero MURAKAMI
|
2015-04-13 02:40:49 +00:00
|
|
|
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_UTILITY_WHILE_LOOP_HPP
|
|
|
|
#define SPROUT_UTILITY_WHILE_LOOP_HPP
|
|
|
|
|
|
|
|
#include <sprout/config.hpp>
|
|
|
|
#include <sprout/utility/pair/pair.hpp>
|
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
#ifdef SPROUT_CONFIG_DISABLE_CXX14_CONSTEXPR
|
|
|
|
namespace detail {
|
|
|
|
template<typename T, typename Predicate>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::pair<T, bool>
|
|
|
|
while_loop_check(T const& init, Predicate pred) {
|
|
|
|
return sprout::pair<T, bool>(init, !pred(init));
|
|
|
|
}
|
2015-04-15 08:48:31 +00:00
|
|
|
template<typename T, typename Predicate, typename UnaryOperation>
|
2015-04-13 02:40:49 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::pair<T, bool>
|
2015-04-15 08:48:31 +00:00
|
|
|
while_loop_impl_1(sprout::pair<T, bool> const& current, Predicate pred, UnaryOperation unary_op, std::size_t n) {
|
2015-04-13 02:40:49 +00:00
|
|
|
return current.second || !pred(current.first) ? current
|
|
|
|
: n == 1 ? sprout::detail::while_loop_check<T>(unary_op(current.first), pred)
|
|
|
|
: sprout::detail::while_loop_impl_1(
|
|
|
|
sprout::detail::while_loop_impl_1(
|
|
|
|
current,
|
2015-04-15 08:48:31 +00:00
|
|
|
pred, unary_op, n / 2
|
2015-04-13 02:40:49 +00:00
|
|
|
),
|
2015-04-15 08:48:31 +00:00
|
|
|
pred, unary_op, n - n / 2
|
2015-04-13 02:40:49 +00:00
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
2015-04-15 08:48:31 +00:00
|
|
|
template<typename T, typename Predicate, typename UnaryOperation>
|
2015-04-13 02:40:49 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::pair<T, bool>
|
2015-04-15 08:48:31 +00:00
|
|
|
while_loop_impl(sprout::pair<T, bool> const& current, Predicate pred, UnaryOperation unary_op, std::size_t n) {
|
2015-04-13 02:40:49 +00:00
|
|
|
return current.second || !pred(current.first) ? current
|
|
|
|
: sprout::detail::while_loop_impl(
|
|
|
|
sprout::detail::while_loop_impl_1(
|
|
|
|
current,
|
2015-04-15 08:48:31 +00:00
|
|
|
pred, unary_op, n
|
2015-04-13 02:40:49 +00:00
|
|
|
),
|
2015-04-15 08:48:31 +00:00
|
|
|
pred, unary_op, n * 2
|
2015-04-13 02:40:49 +00:00
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
//
|
|
|
|
// while_loop
|
|
|
|
//
|
|
|
|
// recursion depth:
|
|
|
|
// O(log N)
|
|
|
|
//
|
2015-04-15 08:48:31 +00:00
|
|
|
template<typename T, typename Predicate, typename UnaryOperation>
|
2015-04-13 02:40:49 +00:00
|
|
|
inline SPROUT_CONSTEXPR T
|
2015-04-15 08:48:31 +00:00
|
|
|
while_loop(T init, Predicate pred, UnaryOperation unary_op) {
|
2015-04-13 02:40:49 +00:00
|
|
|
typedef sprout::pair<T, bool> type;
|
2015-04-15 08:48:31 +00:00
|
|
|
return sprout::detail::while_loop_impl(type(init, false), pred, unary_op, 1).second;
|
2015-04-13 02:40:49 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
//
|
|
|
|
// while_loop
|
|
|
|
//
|
|
|
|
// recursion depth:
|
|
|
|
// 0
|
|
|
|
//
|
2015-04-15 08:48:31 +00:00
|
|
|
template<typename T, typename Predicate, typename UnaryOperation>
|
2015-04-13 02:40:49 +00:00
|
|
|
inline SPROUT_CXX14_CONSTEXPR T
|
2015-04-15 08:48:31 +00:00
|
|
|
while_loop(T init, Predicate pred, UnaryOperation unary_op) {
|
|
|
|
for (; pred(init); init = unary_op(init))
|
|
|
|
;
|
2015-04-13 02:40:49 +00:00
|
|
|
return init;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_UTILITY_WHILE_LOOP_HPP
|