-
Notifications
You must be signed in to change notification settings - Fork 0
/
indices.hpp
59 lines (35 loc) · 1.22 KB
/
indices.hpp
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
#ifndef INDICES_HPP
#define INDICES_HPP
#include <cstddef>
// a compile-time index sequence similar to c++14 std::index_sequence
template<std::size_t ... N>
struct indices { };
namespace detail {
template<class s1, class s2>
struct merge_indices_type;
template<std::size_t ... I1, std::size_t ... I2>
struct merge_indices_type< indices<I1...>, indices<I2...> >{
using type = indices<I1..., I2...>;
};
template<std::size_t S, std::size_t E, std::size_t D = E - S>
struct make_indices_type {
static_assert(E >= S, "bounds must be increasing");
static constexpr std::size_t pivot = (E + S) / 2;
using lhs_type = typename make_indices_type<S, pivot>::type;
using rhs_type = typename make_indices_type<pivot, E>::type;
using type = typename merge_indices_type< lhs_type, rhs_type>::type;
};
template<std::size_t I>
struct make_indices_type<I, I, 0> {
using type = indices<>;
};
template<std::size_t I, std::size_t J>
struct make_indices_type<I, J, 1> {
using type = indices<I>;
};
}
template<std::size_t N>
using range_indices = typename detail::make_indices_type<0, N>::type;
template<class ... T>
using type_indices = range_indices< sizeof...(T) >;
#endif