-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinternals.h
335 lines (284 loc) · 10.7 KB
/
internals.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#pragma once
#include <type_traits>
#include <array>
/*! Internal meta-programming tools
*
* See especially
* - https://codereview.stackexchange.com/questions/107877/simple-multi-dimensional-array-class-in-c11
* - https://github.com/maddouri/hyper_array/
*
*/
namespace corgi {
namespace internals {
/// shorthand for the enable_if syntax
/// @see http://en.cppreference.com/w/cpp/types/enable_if#Helper_types
//
// TODO: move to c++-14 where this is in std::
template <bool b, typename T=void>
using enable_if_t = typename std::enable_if<b, T>::type;
/// building block of a neat trick for checking multiple types against a given trait
template <bool...>
struct bool_pack
{};
/// neat trick for checking multiple types against a given trait
/// https://codereview.stackexchange.com/a/107903/86688
template <bool... bs>
using are_all_true = std::is_same<bool_pack<true, bs...>,
bool_pack<bs..., true>>;
/// checks that all the template arguments are integral types
/// @note `T&` where `std::is_integral<T>::value==true` is considered integral
/// by removing any reference then using `std::is_integral`
template <typename... Ts>
using are_integral = are_all_true<
std::is_integral<
typename std::remove_reference<Ts>::type
>::value...
>;
/// compile-time sum
template <typename T>
constexpr T ct_plus(const T x, const T y) { return x + y; }
/// compile-time product
template <typename T>
constexpr T ct_prod(const T x, const T y) { return x * y; }
/// compile-time equivalent to `std::accumulate()`
template <
typename T, ///< result type
std::size_t N, ///< length of the array
typename O ///< type of the binary operation
>
constexpr
T ct_accumulate(const ::std::array<T, N>& arr, ///< accumulate from this array
const size_t first, ///< starting from this position
const size_t length, ///< accumulate this number of elements
const T initial_value, ///< let this be the accumulator's initial value
const O& op ///< use this binary operation
)
{
// https://stackoverflow.com/a/33158265/865719
//return (first < (first + length))
// ? op(arr[first],
// ct_accumulate(arr,
// first + 1,
// length - 1,
// initial_value,
// op))
// : initial_value;
// TODO this one is allowed in >c++14
T ret = initial_value;
for(size_t i=0; i<length; i++){
ret = op(ret, arr[first + i]);
}
return ret;
}
/// compile-time equivalent to `std::inner_product()`
template <
typename T, ///< the result type
typename T_1, ///< first array's type
size_t N_1, ///< length of the first array
typename T_2, ///< second array's type
size_t N_2, ///< length of the second array
typename O_SUM, ///< summation operation's type
typename O_PROD ///< multiplication operation's type
>
constexpr
T ct_inner_product(const ::std::array<T_1, N_1>& arr_1, ///< calc the inner product of this array
const size_t first_1, ///< from this position
const ::std::array<T_2, N_2>& arr_2, ///< with this array
const size_t first_2, ///< from this position
const size_t length, ///< using this many elements from both arrays
const T initial_value, ///< let this be the summation's initial value
const O_SUM& op_sum, ///< use this as the summation operator
const O_PROD& op_prod ///< use this as the multiplication operator
)
{
// same logic as `ct_accumulate()`
//return (first_1 < (first_1 + length))
// ? op_sum(op_prod(arr_1[first_1],
// arr_2[first_2]),
// ct_inner_product(arr_1, first_1 + 1,
// arr_2, first_2 + 1,
// length - 1,
// initial_value,
// op_sum, op_prod))
// : initial_value;
// TODO this one is allowed in >c++14
T ret = initial_value;
for(size_t i=0; i<length; i++){
ret = op_sum(ret, op_prod(arr_1[first_1 + i], arr_2[first_2 + i]));
}
return ret;
}
/*! computes the index coefficients assuming row-major order
*
*
* what we compute:
* \f[
* \begin{cases}
* C_i = \prod_{j=i+1}^{n-1} L_j
* \\
* \begin{cases}
* i &\in [0, \text{Dimensions - 1}] \\
* C_i &: \text{\_coeffs[i]} \\
* L_j &: \text{\_lengths[j]}
* \end{cases}
* \end{cases}
* \f]
*/
//template <typename size_type, std::size_t D>
//::std::array<size_type, D>
//compute_index_coeffs(const ::std::array<size_type, D>& dimension_lengths) noexcept
//{
// ::std::array<size_type, D> coeffs;
// for (size_type i = 0; i < D; ++i)
// {
// coeffs[i] = ct_accumulate(dimension_lengths,
// i + 1,
// D - i - 1,
// static_cast<size_type>(1),
// ct_prod<size_type>);
// }
// return coeffs;
//}
//--------------------------------------------------
// tyepdef shortcut for checking indicies
//
// TODO: does not work; complains about <anonymous> type?
//
// template<std::size_t Dim, typename... Args>
// using check_index_length_t =
// typename corgi::internals::enable_if_t<(sizeof...(Args) == Dim) &&
// corgi::internals::are_integral<Args...>::value, void>;
template<std::size_t Dim, typename... Args>
using check_index_length_t =
typename enable_if_t<(sizeof...(Args) == Dim), void>::type;
//--------------------------------------------------
// N-length tuple of type T, i.e., tuple_of<3, int> = tuple<int, int, int>
// see:
// - https://stackoverflow.com/questions/38885406/produce-stdtuple-of-same-type-in-compile-time-given-its-length-by-a-template-a
template <size_t I,typename T>
struct tuple_n{
template< typename...Args> using type = typename tuple_n<I-1, T>::template type<T, Args...>;
};
template <typename T>
struct tuple_n<0, T> {
template<typename...Args> using type = std::tuple<Args...>;
};
template <size_t I,typename T> using tuple_of = typename tuple_n<I,T>::template type<>;
//--------------------------------------------------
// implementations of std::apply (from c++-17)
// See:
// - https://stackoverflow.com/questions/7858817/unpacking-a-tuple-to-call-a-matching-function-pointer
//
// ver1
/*
template<typename Function, typename Tuple, size_t ... I>
auto apply(Function f, Tuple t, std::index_sequence<I ...>)
{
return f(std::get<I>(t) ...);
}
template<typename Function, typename Tuple>
auto apply(Function f, Tuple t)
{
static constexpr auto size = std::tuple_size<Tuple>::value;
return apply(f, t, std::make_index_sequence<size>{});
}
*/
//--------------------------------------------------
// ver2
template<std::size_t...Is>
auto index_over(std::index_sequence<Is...> /*unused*/){
return [](auto&&f)->decltype(auto){
return decltype(f)(f)( std::integral_constant<std::size_t, Is>{}... );
};
}
template<std::size_t N>
auto index_upto(std::integral_constant<std::size_t, N> /*unused*/={}){
return index_over( std::make_index_sequence<N>{} );
}
//template< class T >
//constexpr std::size_t tuple_size_v = tuple_size<T>::value;
//typename std::tuple_size<std::remove_reference_t<Tuple>::value>()
template<class T>
constexpr auto tuple_size_v = std::tuple_size<T>::value;
template<class F, class Tuple>
decltype(auto) apply( F&& f, Tuple&& tup ) {
auto indexer = index_upto<
tuple_size_v<std::remove_reference_t<Tuple>>
>();
return indexer(
[&](auto...Is)->decltype(auto) {
return std::forward<F>(f)(
std::get<Is>(std::forward<Tuple>(tup))...
);
}
);
}
// C++-14 index_sequence (maybe?); toggle to get C++-11 compatibility
// used together with tuple unpacking in corgi::Grid::id()
//template <size_t... Is>
//struct index_sequence;
// See also:
// - https://stackoverflow.com/questions/17424477/implementation-c14-make-integer-sequence/17426611#17426611
// - http://aherrmann.github.io/programming/2016/02/28/unpacking-tuples-in-cpp14/
//--------------------------------------------------
// array into tuple conversion
//
// See:
// - https://stackoverflow.com/questions/37029886/how-to-construct-a-tuple-from-an-array
//
// try1
//template <class... Formats, size_t N, size_t... Is>
//std::tuple<Formats...> as_tuple(std::array<char*, N> const& arr,
// std::index_sequence<Is...>)
//{
// return std::make_tuple(Formats{arr[Is]}...);
//}
//
//template <class... Formats, size_t N,
// class = enable_if_t<(N == sizeof...(Formats))>>
//std::tuple<Formats...> as_tuple(std::array<char*, N> const& arr)
//{
// return as_tuple<Formats...>(arr, std::make_index_sequence<N>{});
//}
// try2
// See:
// - https://stackoverflow.com/questions/41207774/how-do-i-create-a-tuple-of-n-ts-from-an-array-of-t
template<std::size_t... I, typename U>
constexpr auto into_tuple(const U &arr, std::index_sequence<I...> /*unused*/) {
return std::make_tuple(arr[I]...);
}
template<typename T, std::size_t N>
constexpr auto into_tuple(const T (&arr)[N]) {
return into_tuple(arr, std::make_index_sequence<N>{});
}
template<typename T, std::size_t N>
constexpr auto into_tuple(const std::array<T, N> &arr) {
return into_tuple(arr, std::make_index_sequence<N>{});
}
//--------------------------------------------------
// tuple into array
// See:
// - https://stackoverflow.com/questions/10604794/convert-stdtuple-to-stdarray-c11
//template<typename First, typename... Rem>
//std::array<First, 1+sizeof...(Rem)>
//into_array(const std::tuple<First, Rem...>& t) {
// std::array<First, 1+sizeof...(Rem)> arr;
// Array_filler<First, decltype(t), 1+sizeof...(Rem)>::into_array(t, arr);
// return arr;
//}
// Convert tuple into a array implementation
template<typename T, std::size_t N, typename Tuple, std::size_t... I>
constexpr decltype(auto) into_array_impl(const Tuple& a, std::index_sequence<I...> /*unused*/)
{
return std::array<T,N>{std::get<I>(a)...};
}
// Convert tuple into a array
template<typename Head, typename... T>
constexpr decltype(auto) into_array(const std::tuple<Head, T...>& a)
{
using Tuple = std::tuple<Head, T...>;
constexpr auto N = sizeof...(T) + 1;
return into_array_impl<Head, N, Tuple>(a, std::make_index_sequence<N>());
}
} // end of namespace internals
} // end of corgi