1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
// The Art of C++ / Sequences
// Copyright (c) 2015 Daniel Frey
#ifndef TAOCPP_SEQUENCES_INCLUDE_FOLD_HPP
#define TAOCPP_SEQUENCES_INCLUDE_FOLD_HPP
#include <type_traits>
#include "values.hpp"
#include "integer_sequence.hpp"
#include "make_integer_sequence.hpp"
namespace tao
{
namespace seq
{
namespace impl
{
template< template< typename U, U, U > class, typename, bool, typename T, T... >
struct folder;
template< template< typename U, U, U > class OP, std::size_t... Is, typename T, T... Ns >
struct folder< OP, index_sequence< Is... >, false, T, Ns... >
{
using values = seq::values< T, Ns... >;
using type = integer_sequence< T, OP< T, values::data[ 2 * Is ], values::data[ 2 * Is + 1 ] >::value... >;
};
template< template< typename U, U, U > class OP, std::size_t... Is, typename T, T N, T... Ns >
struct folder< OP, index_sequence< Is... >, true, T, N, Ns... >
{
using values = seq::values< T, Ns... >;
using type = integer_sequence< T, N, OP< T, values::data[ 2 * Is ], values::data[ 2 * Is + 1 ] >::value... >;
};
}
template< template< typename U, U, U > class, typename T, T... >
struct fold;
template< template< typename U, U, U > class OP, typename T, T N >
struct fold< OP, T, N >
: std::integral_constant< T, N >
{};
template< template< typename U, U, U > class OP, typename T, T... Ns >
struct fold
: fold< OP, typename impl::folder< OP, make_index_sequence< sizeof...( Ns ) / 2 >, sizeof...( Ns ) % 2 == 1, T, Ns... >::type >
{};
template< template< typename U, U, U > class OP, typename T, T... Ns >
struct fold< OP, integer_sequence< T, Ns... > >
: fold< OP, T, Ns... >
{};
}
}
#endif // TAOCPP_SEQUENCES_INCLUDE_FOLD_HPP
|