1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2024-11-12 21:09:01 +00:00
Sprout/sprout/utility/until_loop.hpp

79 lines
2.5 KiB
C++
Raw Permalink Normal View History

2015-04-13 02:40:49 +00:00
/*=============================================================================
Copyright (c) 2011-2019 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_UNTIL_LOOP_HPP
#define SPROUT_UTILITY_UNTIL_LOOP_HPP
#include <sprout/config.hpp>
#include <sprout/utility/pair/pair.hpp>
namespace sprout {
namespace detail {
template<typename T, typename Predicate>
inline SPROUT_CONSTEXPR sprout::pair<T, bool>
until_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
until_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::until_loop_check<T>(unary_op(current.first), pred)
: sprout::detail::until_loop_impl_1(
sprout::detail::until_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
until_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::until_loop_impl(
sprout::detail::until_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
//
// until_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
until_loop(T init, Predicate pred, UnaryOperation unary_op) {
2015-04-13 02:40:49 +00:00
typedef sprout::pair<T, bool> type;
2019-06-15 09:17:17 +00:00
return sprout::detail::until_loop_impl(type(init, false), pred, unary_op, 1).first;
2015-04-13 02:40:49 +00:00
}
2015-04-13 02:40:49 +00:00
//
// flat_until_loop
2015-04-13 02:40:49 +00:00
//
// 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
flat_until_loop(T init, Predicate pred, UnaryOperation unary_op) {
2015-04-15 08:48:31 +00:00
for (; !pred(init); init = unary_op(init))
;
2015-04-13 02:40:49 +00:00
return init;
}
} // namespace sprout
#endif // #ifndef SPROUT_UTILITY_UNTIL_LOOP_HPP