add while_loop, until_loop utilities

This commit is contained in:
bolero-MURAKAMI 2015-04-13 11:40:49 +09:00
parent f78e5d49a0
commit 8d19fdc676
7 changed files with 320 additions and 0 deletions

View file

@ -0,0 +1,70 @@
/*=============================================================================
Copyright (c) 2011-2015 Bolero MURAKAMI
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_ALGORITHM_FOLD_UNTIL_HPP
#define SPROUT_ALGORITHM_FOLD_UNTIL_HPP
#include <iterator>
#include <sprout/config.hpp>
#include <sprout/iterator/operation.hpp>
#include <sprout/utility/pair/pair.hpp>
namespace sprout {
namespace detail {
template<typename InputIterator, typename T, typename BinaryOperation, typename Predicate>
inline SPROUT_CONSTEXPR sprout::pair<InputIterator, T>
fold_until_impl_1(
sprout::pair<InputIterator, T> const& current,
InputIterator last, BinaryOperation binary_op, Predicate pred, typename std::iterator_traits<InputIterator>::difference_type n
)
{
typedef sprout::pair<InputIterator, T> type;
return current.first == last || pred(current.second) ? current
: n == 1 ? type(sprout::next(current.first), binary_op(current.second, *current.first))
: sprout::detail::fold_until_impl_1(
sprout::detail::fold_until_impl_1(
current,
last, binary_op, pred, n / 2
),
last, binary_op, pred, n - n / 2
)
;
}
template<typename InputIterator, typename T, typename BinaryOperation, typename Predicate>
inline SPROUT_CONSTEXPR sprout::pair<InputIterator, T>
fold_until_impl(
sprout::pair<InputIterator, T> const& current,
InputIterator last, BinaryOperation binary_op, Predicate pred, typename std::iterator_traits<InputIterator>::difference_type n
)
{
return current.first == last || pred(current.second) ? current
: sprout::detail::fold_until_impl(
sprout::detail::fold_until_impl_1(
current,
last, binary_op, pred, n
),
last, binary_op, pred, n * 2
)
;
}
} // namespace detail
//
// fold_until
//
// recursion depth:
// O(log N)
//
template<typename InputIterator, typename T, typename BinaryOperation, typename Predicate>
inline SPROUT_CONSTEXPR T
fold_until(InputIterator first, InputIterator last, T init, BinaryOperation binary_op, Predicate pred) {
typedef sprout::pair<InputIterator, T> type;
return sprout::detail::fold_until_impl(type(first, init), last, binary_op, pred, 1).second;
}
} // namespace sprout
#endif // #ifndef SPROUT_ALGORITHM_FOLD_UNTIL_HPP

View file

@ -0,0 +1,70 @@
/*=============================================================================
Copyright (c) 2011-2015 Bolero MURAKAMI
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_ALGORITHM_FOLD_WHILE_HPP
#define SPROUT_ALGORITHM_FOLD_WHILE_HPP
#include <iterator>
#include <sprout/config.hpp>
#include <sprout/iterator/operation.hpp>
#include <sprout/utility/pair/pair.hpp>
namespace sprout {
namespace detail {
template<typename InputIterator, typename T, typename BinaryOperation, typename Predicate>
inline SPROUT_CONSTEXPR sprout::pair<InputIterator, T>
fold_while_impl_1(
sprout::pair<InputIterator, T> const& current,
InputIterator last, BinaryOperation binary_op, Predicate pred, typename std::iterator_traits<InputIterator>::difference_type n
)
{
typedef sprout::pair<InputIterator, T> type;
return current.first == last || !pred(current.second) ? current
: n == 1 ? type(sprout::next(current.first), binary_op(current.second, *current.first))
: sprout::detail::fold_while_impl_1(
sprout::detail::fold_while_impl_1(
current,
last, binary_op, pred, n / 2
),
last, binary_op, pred, n - n / 2
)
;
}
template<typename InputIterator, typename T, typename BinaryOperation, typename Predicate>
inline SPROUT_CONSTEXPR sprout::pair<InputIterator, T>
fold_while_impl(
sprout::pair<InputIterator, T> const& current,
InputIterator last, BinaryOperation binary_op, Predicate pred, typename std::iterator_traits<InputIterator>::difference_type n
)
{
return current.first == last || !pred(current.second) ? current
: sprout::detail::fold_while_impl(
sprout::detail::fold_while_impl_1(
current,
last, binary_op, pred, n
),
last, binary_op, pred, n * 2
)
;
}
} // namespace detail
//
// fold_while
//
// recursion depth:
// O(log N)
//
template<typename InputIterator, typename T, typename BinaryOperation, typename Predicate>
inline SPROUT_CONSTEXPR T
fold_while(InputIterator first, InputIterator last, T init, BinaryOperation binary_op, Predicate pred) {
typedef sprout::pair<InputIterator, T> type;
return sprout::detail::fold_while_impl(type(first, init), last, binary_op, pred, 1).second;
}
} // namespace sprout
#endif // #ifndef SPROUT_ALGORITHM_FOLD_WHILE_HPP

View file

@ -61,6 +61,8 @@
#include <sprout/algorithm/next_difference.hpp> #include <sprout/algorithm/next_difference.hpp>
#include <sprout/algorithm/next_symmetric_difference.hpp> #include <sprout/algorithm/next_symmetric_difference.hpp>
#include <sprout/algorithm/clamp.hpp> #include <sprout/algorithm/clamp.hpp>
#include <sprout/algorithm/fold_while.hpp>
#include <sprout/algorithm/fold_until.hpp>
#include <sprout/algorithm/abs_diff.hpp> #include <sprout/algorithm/abs_diff.hpp>
#endif // #ifndef SPROUT_ALGORITHM_NON_MODIFYIING_HPP #endif // #ifndef SPROUT_ALGORITHM_NON_MODIFYIING_HPP

View file

@ -21,5 +21,6 @@
#include <sprout/utility/string_view.hpp> #include <sprout/utility/string_view.hpp>
#include <sprout/utility/any_convertible.hpp> #include <sprout/utility/any_convertible.hpp>
#include <sprout/utility/use_default.hpp> #include <sprout/utility/use_default.hpp>
#include <sprout/utility/loop.hpp>
#endif // #ifndef SPROUT_UTILITY_HPP #endif // #ifndef SPROUT_UTILITY_HPP

15
sprout/utility/loop.hpp Normal file
View file

@ -0,0 +1,15 @@
/*=============================================================================
Copyright (c) 2011-2015 Bolero MURAKAMI
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_LOOP_HPP
#define SPROUT_UTILITY_LOOP_HPP
#include <sprout/config.hpp>
#include <sprout/utility/while_loop.hpp>
#include <sprout/utility/until_loop.hpp>
#endif // #ifndef SPROUT_UTILITY_LOOP_HPP

View file

@ -0,0 +1,81 @@
/*=============================================================================
Copyright (c) 2011-2015 Bolero MURAKAMI
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 {
#ifdef SPROUT_CONFIG_DISABLE_CXX14_CONSTEXPR
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));
}
template<typename T, typename UnaryOperation, typename Predicate>
inline SPROUT_CONSTEXPR sprout::pair<T, bool>
until_loop_impl_1(sprout::pair<T, bool> const& current, UnaryOperation unary_op, Predicate pred, std::size_t n) {
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,
unary_op, pred, n / 2
),
unary_op, pred, n - n / 2
)
;
}
template<typename T, typename UnaryOperation, typename Predicate>
inline SPROUT_CONSTEXPR sprout::pair<T, bool>
until_loop_impl(sprout::pair<T, bool> const& current, UnaryOperation unary_op, Predicate pred, std::size_t n) {
return current.second || pred(current.first) ? current
: sprout::detail::until_loop_impl(
sprout::detail::until_loop_impl_1(
current,
unary_op, pred, n
),
unary_op, pred, n * 2
)
;
}
} // namespace detail
//
// until_loop
//
// recursion depth:
// O(log N)
//
template<typename T, typename UnaryOperation, typename Predicate>
inline SPROUT_CONSTEXPR T
until_loop(T init, UnaryOperation unary_op, Predicate pred) {
typedef sprout::pair<T, bool> type;
return sprout::detail::until_loop_impl(type(init, false), unary_op, pred, 1).second;
}
#else
//
// until_loop
//
// recursion depth:
// 0
//
template<typename T, typename UnaryOperation, typename Predicate>
inline SPROUT_CXX14_CONSTEXPR T
until_loop(T init, UnaryOperation unary_op, Predicate pred) {
while (!pred(init)) {
init = unary_op(init);
}
return init;
}
#endif
} // namespace sprout
#endif // #ifndef SPROUT_UTILITY_UNTIL_LOOP_HPP

View file

@ -0,0 +1,81 @@
/*=============================================================================
Copyright (c) 2011-2015 Bolero MURAKAMI
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));
}
template<typename T, typename UnaryOperation, typename Predicate>
inline SPROUT_CONSTEXPR sprout::pair<T, bool>
while_loop_impl_1(sprout::pair<T, bool> const& current, UnaryOperation unary_op, Predicate pred, std::size_t n) {
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,
unary_op, pred, n / 2
),
unary_op, pred, n - n / 2
)
;
}
template<typename T, typename UnaryOperation, typename Predicate>
inline SPROUT_CONSTEXPR sprout::pair<T, bool>
while_loop_impl(sprout::pair<T, bool> const& current, UnaryOperation unary_op, Predicate pred, std::size_t n) {
return current.second || !pred(current.first) ? current
: sprout::detail::while_loop_impl(
sprout::detail::while_loop_impl_1(
current,
unary_op, pred, n
),
unary_op, pred, n * 2
)
;
}
} // namespace detail
//
// while_loop
//
// recursion depth:
// O(log N)
//
template<typename T, typename UnaryOperation, typename Predicate>
inline SPROUT_CONSTEXPR T
while_loop(T init, UnaryOperation unary_op, Predicate pred) {
typedef sprout::pair<T, bool> type;
return sprout::detail::while_loop_impl(type(init, false), unary_op, pred, 1).second;
}
#else
//
// while_loop
//
// recursion depth:
// 0
//
template<typename T, typename UnaryOperation, typename Predicate>
inline SPROUT_CXX14_CONSTEXPR T
while_loop(T init, UnaryOperation unary_op, Predicate pred) {
while (pred(init)) {
init = unary_op(init);
}
return init;
}
#endif
} // namespace sprout
#endif // #ifndef SPROUT_UTILITY_WHILE_LOOP_HPP