mirror of
https://github.com/bolero-MURAKAMI/Sprout.git
synced 2025-07-04 14:14:09 +00:00
add while_loop, until_loop utilities
This commit is contained in:
parent
f78e5d49a0
commit
8d19fdc676
7 changed files with 320 additions and 0 deletions
70
sprout/algorithm/fold_until.hpp
Normal file
70
sprout/algorithm/fold_until.hpp
Normal 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
|
70
sprout/algorithm/fold_while.hpp
Normal file
70
sprout/algorithm/fold_while.hpp
Normal 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
|
|
@ -61,6 +61,8 @@
|
|||
#include <sprout/algorithm/next_difference.hpp>
|
||||
#include <sprout/algorithm/next_symmetric_difference.hpp>
|
||||
#include <sprout/algorithm/clamp.hpp>
|
||||
#include <sprout/algorithm/fold_while.hpp>
|
||||
#include <sprout/algorithm/fold_until.hpp>
|
||||
#include <sprout/algorithm/abs_diff.hpp>
|
||||
|
||||
#endif // #ifndef SPROUT_ALGORITHM_NON_MODIFYIING_HPP
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue