-
Notifications
You must be signed in to change notification settings - Fork 0
/
exp_repeat.hpp
230 lines (193 loc) · 6.22 KB
/
exp_repeat.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
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
#pragma once
#include"select_type.hpp"
#include"exp_function_binder.hpp"
#include <type_traits>
#include <memory>
namespace exp_repeat
{
template<class F, class ...L>
struct Meta_Invoke {
using type = F::template apply<L...>;
};
template<class F, class ...L>
using meta_invoke = typename Meta_Invoke<F, L...>::type;
struct below_zero {
static constexpr size_t value = 0;
};
template<size_t _I>
struct Idx {
static const size_t value = _I;
};
template<class _Idx, size_t _I>
struct Add_Idx {};
template<size_t I> struct Add_Idx<below_zero, I>
{
using type = Idx<0>;
};
template<template<size_t> class _Idx, size_t _I_in_Idx, size_t _I>
struct Add_Idx<_Idx<_I_in_Idx>, _I>
{
using type = _Idx<_I_in_Idx + _I>;
};
template<class _Idx, size_t _I>
using add_idx_t = typename Add_Idx<_Idx, _I>::type;
template<class _Idx>
using inc_idx_t = add_idx_t<_Idx, 1>;
template<size_t _stride_length>
struct _Stride {};
template<bool _Condition, class _Meta_Object, class F, class Arg_t>
struct looper
{
using _current_type = meta_invoke<_Meta_Object, meta_invoke<F, Arg_t>>;
template<class _Bool_Fn>
struct apply
{
using type = typename looper<
meta_invoke<_Bool_Fn, _Meta_Object>::value, _current_type,
F, meta_invoke<F, Arg_t>
>::template apply<_Bool_Fn>::type;
};
};
template<class _Meta_Object, class F, class Arg_t>
struct looper<false, _Meta_Object, F, Arg_t>
{
template<class _Bool_Fn> struct apply{ using type = _Meta_Object; };
};
template<size_t _Limit>
struct _Length_Less_Than
{
template<class TL> struct apply{ static const bool value = (TL::length < _Limit); };
};
template<class TL>
struct _Appendable_Meta_List
{
static const size_t length = size_of_type_list<TL>::value;
using type = TL;
template<class _Extent>
using apply = _Appendable_Meta_List<typename exp_join_impl<TL, _Extent>::type>;
};
struct _Increase_Idx
{
template<class _Idx> using apply = inc_idx_t <_Idx>;
};
template<size_t NUM>
using meta_itoa = typename looper<(NUM != 0), _Appendable_Meta_List<exp_list<exp_repeat::Idx<0>>>,
_Increase_Idx, exp_repeat::Idx<0>>::template apply<_Length_Less_Than<NUM>>::type::type;
template<size_t ..._NUM>
struct meta_array
{
using cv_typelist = exp_list<Idx<_NUM>...>;
template<template<size_t...I> class integer_array_type>
using to = integer_array_type<_NUM...>;
template<size_t N> using at = exp_select<N, cv_typelist>;
};
template<class TL>
struct _Meta_To_Array{};
template<template<class...> class TL, size_t... _I>
struct _Meta_To_Array<TL<Idx<_I>...>>
{
using type = meta_array<_I...>;
};
template<class TL>
using meta_to_array =typename _Meta_To_Array<TL>::type;
namespace invoke
{
template<size_t _Idx, class _Node>
exp_select<_Idx, typename _Node::element_type_list> get_from_node(_Node& node)
{
exp_select<_Idx, typename _Node::element_type_list> val{};
return fetch_value(val, exp_iterator<_Node>(node)[_Idx]);
}
template<size_t _Idx, class _Node>
exp_select<_Idx, typename _Node::element_type_list>& ref_from_node(_Node& node)
{
using ref_type = exp_select<_Idx, typename _Node::element_type_list>&;
exp_reference_pointer<ref_type> erp{};
auto get_ref = [&erp]<class vt>(vt& value) {
if constexpr (requires(vt& v){erp = v; })
erp = value;
};
do_at(node, get_ref, _Idx);
return (ref_type)erp;
}
template<template<size_t ...> class _Meta_Array, class F, class _Node, size_t ...idxs>
auto node_invoke(_Meta_Array<idxs...> metas, _Node& node, F&& f)
{
return f((get_from_node<idxs>(node))...);
}
template<class _Iota_Type>
struct _Itoa_To_Array {};
template<size_t ..._Nums>
struct _Itoa_To_Array<exp_list<Idx<_Nums>...>>
{
using type = meta_array<_Nums...>;
};
template<size_t _Max>
using meta_ordered_array_c = typename _Itoa_To_Array<meta_itoa<_Max>>::type;
template<class _Node, class F>
auto node_invoke(_Node& node, F&& f)
{
return node_invoke(meta_ordered_array_c<max_type_list_index<typename _Node::element_type_list>::value>(), node, f);
}
#define VERIFY_MUTABLE_ARGMENT(NAME) if constexpr(sizeof...(NAME)!=0)
template<class _Node, class ...L>
void push_invoke(_Node& node, L...l)
{
VERIFY_MUTABLE_ARGMENT(l)
(push_back(node, l, shared_constructor()), ...);
}
template<size_t N> struct idx_invoke {
using idx_sequence = meta_to_array<meta_itoa<N>>;
template<class F, size_t ...I, class ...Args> auto constexpr invoke_impl(meta_array<I...>, F&& f, Args ...args) const
{
return f.template operator() < I... > (std::forward<Args>(args)...);
}
template<class F, class ...Args> auto constexpr operator()(F&& f, Args&&...args) const {
return invoke_impl(idx_sequence{}, std::forward<F>(f), std::forward<Args>(args)...);
}
};
}
template<auto fn>
using argument_node = element_node<0,
typename decltype(
exp_bind::bind(fn)
)::argument_list_type, std::shared_ptr>;
template<class ...>
struct Meta_Expr {};
template<class _Idx1, class _OP, class _Idx2>
struct Meta_Expr<_Idx1, _OP, _Idx2> {
using type = meta_invoke<_OP, _Idx1, _Idx2>;
};
namespace operators{
template<const char _O> struct Meta_Op {};
template<> struct Meta_Op<'+'> {
template<class X, class Y>
using apply = Idx<X::value + Y::value>;
};
template<> struct Meta_Op<'-'> {
template<class X, class Y>
using apply = Idx<X::value - Y::value>;
};
template<> struct Meta_Op<'*'> {
template<class X, class Y>
using apply = Idx<X::value * Y::value>;
};
template<> struct Meta_Op<'>'> {
template<class X, class Y>
struct apply { static const bool value = (X::value > Y::value); };
};
template<> struct Meta_Op<'='> {
template<class X, class Y>
struct apply { static const bool value = (X::value == Y::value); };
};
using plus = Meta_Op<'+'>;
using sub = Meta_Op<'-'>;
using mul = Meta_Op<'*'>;
using greater = Meta_Op<'>'>;
using equal = Meta_Op<'='>;
}
template<class ...L>
using meta_expr = typename Meta_Expr<L...>::type;
template<class ...L>
using meta_expr_string = Meta_Expr<L...>;
}