-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalue_tuple.hpp
More file actions
173 lines (135 loc) · 4.87 KB
/
value_tuple.hpp
File metadata and controls
173 lines (135 loc) · 4.87 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#pragma once
#include <iostream>
//solution to exercice 1
#include "static_if.hpp"
namespace detail_ {
template <int Index, typename Type>
struct position{
static_assert((Index > 0), "Index should be > 0");
using value_type = Type;
constexpr position(Type const& val) : value{val}
{}
static const int index=Index;
Type value;
};
template <int N, typename First>
constexpr auto initialize( )
{
return First{};
}
template <int N, typename First, typename X>
constexpr auto initialize( X const& x )
{
//note: the ? construct does not allow x.value and 0 to have
//different types
//This prevents us from defining a generic tuple. How would
// you solve this problem? return (X::index==N? x.value : 0);
//solution exercice 1:
return static_if<X::index==N>::apply(x.value, First{});
}
template <int N, typename First, typename X, typename ... Rest>
constexpr auto initialize(X const& x, Rest const& ... rest )
{
//note: the ? construct does not allow x.value and
//initialize<N>(rest...) to have different types This prevents
//us from defining a generic tuple. How would you solve this
//problem? return X::index==N? x.value :
//initialize<N>(rest...);
//solution exercice 1:
return static_if<X::index==N>::apply( x.value, initialize<N, First>(rest...));
}
template<typename T, int Idx>
struct access{
using type = typename std::conditional<Idx==1, T, typename access<typename T::super, Idx-1>::type >::type;
};
template<typename T>
struct access<T, 1>{
using type = T;
};
template<int NDim, typename ... Types >
struct sized_value_tuple;
template<int NDim, typename First, typename ... Types >
struct sized_value_tuple<NDim, First, Types ...>
: public sized_value_tuple<NDim, Types ...>
{
public:
static const int n_dim=NDim;
typedef sized_value_tuple<NDim, Types ...> super;
private:
static const int s_index=NDim - sizeof...(Types);
template <int Limit>
static constexpr bool check_bounds() {
return true;
};
template <int Limit, typename _First, typename ...Elems>
static constexpr bool check_bounds() {
return (_First::index <= Limit) && check_bounds<Limit, Elems...>();
};
public:
template <int Idx, typename Type, typename... GenericElements>
constexpr sized_value_tuple ( position<Idx, Type> & t,
GenericElements & ... x):
super( t, x... ),
m_offset(initialize<s_index, First>(t, x...))
{
static_assert(check_bounds<n_dim,
position<Idx, Type>,
GenericElements...>(), "Out of bound");
}
template <int Idx, typename Type, typename... GenericElements>
constexpr sized_value_tuple ( position<Idx, Type> && t,
GenericElements && ... x):
sized_value_tuple(t, x...)
{
}
constexpr sized_value_tuple ():
super(),
m_offset(initialize<s_index, First>())
{ }
template<int Idx>
constexpr auto get() const {
static_assert(Idx <= NDim, "error");
return static_if<Idx==s_index>
::apply(m_offset, super::template get<Idx>());
}
template<int Idx, typename T>
void set(T const& arg_) {
access<sized_value_tuple,Idx>::type::m_offset = arg_;
}
template<int Idx>
constexpr bool is_present() const {
return static_if<Idx==s_index>::
apply(true, super::template is_present<Idx>());
}
protected:
First m_offset;
};
//recursion anchor
template< int NDim >
struct sized_value_tuple<NDim>
{
static const int n_dim=NDim;
template <typename... GenericElements>
constexpr sized_value_tuple ( GenericElements const& ... ) {}
constexpr sized_value_tuple ( ) {}
static const int s_index=NDim+1;
//never called
template<int Idx>
constexpr int get() const {
static_assert((Idx<=n_dim),
"sized_value_tuple out of bound access");
return 0; }
template<int Idx, typename T>
void set(T const& arg_) {
}
template<int Idx>
constexpr bool is_present() const {return false;}
};
} //namespace detail_
template<int Index, typename Type>
auto constexpr pos(Type value){
static_assert((Index > 0), "Index should be > 0");
return detail_::position<Index, Type>(value);
}
template<typename ... T>
using value_tuple=detail_::sized_value_tuple<sizeof...(T), T...>;