-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathinteger_sequence.h
61 lines (46 loc) · 1.46 KB
/
integer_sequence.h
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
58
59
60
#ifndef I_SEQ_2908SDGF375
#define I_SEQ_2908SDGF375
#include <type_traits>
namespace cti
{ // compile time integers
// a sequence container ---------------------------------------------
template<int ...>
struct ints { };
namespace detail
{
// integer sequence ---------------------------------------------
template<int N, int... Is>
struct int_seq : int_seq < N - 1, N, Is... > { };
template<int... Is>
struct int_seq<0, Is...>
{
using type = ints < 0, Is... > ;
};
// reverse integer sequence -------------------------------------
template<int C, int N, int... Is>
struct rev_seq : rev_seq<C - 1, N, N - C, Is...> { };
template<int N, int... Is>
struct rev_seq<0, N, Is...>
{
using type = ints<N, Is...> ;
};
// integer range ------------------------------------------------
template<int C, int N0, int N, int... Is>
struct int_range :
int_range<C, N0, (N - C > N0 ? N - C : N0), N, Is...> { };
template<int C, int N0, int... Is>
struct int_range<C, N0, N0, Is...>
{
using type = ints<N0, Is...>;
};
}
// convenience aliases ----------------------------------------------
template<int N>
using sequence = typename detail::int_seq<N>::type;
template<int N>
using reverse_sequence = typename detail::rev_seq<N, N>::type;
template<int N0, int N,
typename std::enable_if<(N0 <= N), int>::type C = 1>
using range = typename detail::int_range<C, N0, N - ((N-N0)%C)>::type;
} // ~compile time integers
#endif