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

reimplementation types::fold

This commit is contained in:
bolero-MURAKAMI 2015-04-11 12:19:12 +09:00
parent 84b6d5d994
commit 78e954aa58

View file

@ -8,7 +8,6 @@
#ifndef SPROUT_TYPE_ALGORITHM_FOLD_HPP
#define SPROUT_TYPE_ALGORITHM_FOLD_HPP
#include <sprout/config.hpp>
#include <sprout/workaround/std/cstddef.hpp>
#include <sprout/type/apply.hpp>
#include <sprout/type/tuple.hpp>
@ -18,30 +17,52 @@ namespace sprout {
namespace types {
namespace detail {
template<
typename Tuple, typename T, typename BinaryOp, std::size_t I,
bool Valid = (I != sprout::types::tuple_size<Tuple>::value)
typename Tuple, typename T, typename BinaryOp, std::size_t First, std::size_t Last,
std::size_t Pivot,
bool C0 = (Pivot == 0)
>
struct fold_impl;
template<typename Tuple, typename T, typename BinaryOp, std::size_t I>
struct fold_impl<Tuple, T, BinaryOp, I, false>
: public sprout::identity<T>
template<
typename Tuple, typename T, typename BinaryOp, std::size_t First, std::size_t Last,
std::size_t Pivot
>
struct fold_impl<Tuple, T, BinaryOp, First, Last, Pivot, true>
: public sprout::types::apply<BinaryOp, T, typename sprout::types::tuple_element<First, Tuple>::type>
{};
template<typename Tuple, typename T, typename BinaryOp, std::size_t I>
struct fold_impl<Tuple, T, BinaryOp, I, true>
template<
typename Tuple, typename T, typename BinaryOp, std::size_t First, std::size_t Last,
std::size_t Pivot
>
struct fold_impl<Tuple, T, BinaryOp, First, Last, Pivot, false>
: public sprout::types::detail::fold_impl<
Tuple,
typename sprout::types::apply<BinaryOp, T, typename sprout::types::tuple_element<I, Tuple>::type>::type,
typename sprout::types::detail::fold_impl<
Tuple,
T,
BinaryOp,
First, First + Pivot,
Pivot / 2
>::type,
BinaryOp,
I + 1
First + Pivot, Last,
(Last - First - Pivot) / 2
>
{};
template<typename Tuple, typename T, typename BinaryOp, std::size_t Size = sprout::types::tuple_size<Tuple>::value>
struct fold
: public sprout::types::detail::fold_impl<Tuple, T, BinaryOp, 0, Size, Size / 2>
{};
template<typename Tuple, typename T, typename BinaryOp>
struct fold<Tuple, T, BinaryOp, 0>
: public sprout::identity<T>
{};
} // namespace detail
//
// fold
//
template<typename Tuple, typename T, typename BinaryOp>
struct fold
: public sprout::types::detail::fold_impl<Tuple, T, BinaryOp, 0>
: public sprout::types::detail::fold<Tuple, T, BinaryOp>
{};
#if SPROUT_USE_TEMPLATE_ALIASES