-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.hpp
285 lines (274 loc) · 16.7 KB
/
function.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
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
#pragma once
#ifndef FUNCTIONAL_HPP
#define FUNCTIONAL_HPP 1
#include "basics.hpp"
namespace Mathlab {
template <typename _R, typename... _Args> class Function {
struct vtable_t {
_R(*invoke)(uintptr_t&, _Args...);
void (*copy)(uintptr_t&, const uintptr_t&) noexcept;
void (*move)(uintptr_t&, uintptr_t&&) noexcept;
void (*swap)(uintptr_t&, uintptr_t&) noexcept;
void (*destroy)(uintptr_t&) noexcept;
::std::type_info const& (*type)() noexcept;
bool reqAlloc;
};
template<typename _T, bool B = true>
struct vtable_funcs {
static constexpr _R invoke(uintptr_t& storage, _Args... args) _NOEXCEPT_AS_(_declval(_T)(args...)) {
return (B ? *reinterpret_cast<_T*>(storage) : reinterpret_cast<_T&>(storage))(args...);
}
static constexpr void copy(uintptr_t& dest, const uintptr_t& src) noexcept {
if constexpr (B) dest = reinterpret_cast<uintptr_t>(new _T(*reinterpret_cast<_T*>(src)));
else dest = src;
}
static constexpr void move(uintptr_t& dest, uintptr_t&& src) noexcept {
dest = src, src = 0;
}
static constexpr void swap(uintptr_t& lhs, uintptr_t& rhs) noexcept {
uintptr_t u = lhs;
lhs = rhs, rhs = u;
}
static constexpr void destroy(uintptr_t& storage) noexcept {
if constexpr (B) delete reinterpret_cast<_T*>(storage);
else reinterpret_cast<_T*>(&storage)->~_T();
}
static constexpr ::std::type_info const& type() noexcept {
return typeid(_T);
}
static constexpr bool reqAlloc = B;
};
template<typename _T> static constexpr vtable_t* vtable() {
vtable_funcs<_T, (alignof(_T) > alignof(uintptr_t) || sizeof(_T) > sizeof(uintptr_t) ||
!std::is_trivially_copyable_v<_T>) > v{};
static vtable_t vtable{v.invoke, v.copy, v.move, v.swap, v.destroy, v.type, v.reqAlloc};
return &vtable;
}
mutable uintptr_t _storage = 0;
vtable_t* _vtable;
public:
typedef _R ResultType, (*PointerType)(_Args...);
constexpr Function(std::nullptr_t n = nullptr) noexcept : _storage(0), _vtable(n) {}
template <class _S, class... _T> constexpr Function(const Function<_S, _T...>& other) noexcept : _storage(0), _vtable(other._vtable) {
if (_vtable) _vtable->copy(_storage, other._storage);
}
template <class _S, class... _T> constexpr Function(Function<_S, _T...>&& other) noexcept : _storage(other._storage), _vtable(other._vtable) {}
constexpr Function(PointerType f) noexcept : _vtable(vtable<PointerType>()), _storage(reinterpret_cast<uintptr_t>(f)) {}
//Other than PointerType
template<FunctionObject _Fn> constexpr Function(_Fn&& f) noexcept : _storage(0), _vtable(vtable<_Fn>()) {
if (_vtable->reqAlloc) _storage = new _Fn(f);
else new(&_storage) _Fn(f);
}
constexpr Function& operator=(Function const& other) noexcept {
if (_vtable && _storage != other._storage) _vtable->destroy(_storage);
if (_vtable = other._vtable) _vtable->copy(_storage, other._storage);
return *this;
}
constexpr Function& operator=(Function&& other) noexcept {
if (_vtable && _storage != other._storage) _vtable->destroy(_storage);
if (_vtable = other._vtable) _vtable->move(_storage, other._storage), other._vtable = nullptr;
}
template<FunctionObject _Fn> constexpr Function& operator=(_Fn&& f) {
Function(f).swap(*this);
return *this;
}
constexpr ~Function() noexcept {
if (_vtable) _vtable->destroy(_storage);
}
constexpr void swap(Function& other) noexcept {
if (_vtable == other._vtable) {
if (_vtable) _vtable->swap(_storage, other._storage);
return;
}
Function tmp(other);
if (other._vtable = _vtable) other._vtable->move(other._storage, _storage);
if (_vtable = tmp._vtable) _vtable->move(_storage, tmp._storage);
}
constexpr _R operator()(_Args... args) const {
return _vtable ? _vtable->invoke(_storage, args...) : throw Error(this);
}
constexpr ::std::type_info const& target_type() const noexcept {
return _vtable ? _vtable->type() : typeid(void);
}
template<typename _T = void> constexpr _T* target() noexcept {
return typeid(_T) == _vtable->type() || typeid(_T) == typeid(void) ? reinterpret_cast<_T*>(_storage) : nullptr;
}
template<typename _T = void> constexpr const _T* target() const noexcept {
return typeid(_T) == _vtable->type() || typeid(_T) == typeid(void) ? reinterpret_cast<const _T*>(_storage) : nullptr;
}
constexpr operator void* () const noexcept {
return _vtable;
}
constexpr size_t hash() const noexcept {
return _storage;
}
constexpr operator bool() const noexcept {
return _vtable;
}
constexpr bool operator==(nullptr_t) const noexcept {
return _vtable == nullptr;
}
constexpr bool operator==(const Function& other) const noexcept {
return _vtable == other._vtable && (_storage == other._storage || _storage == other._storage);
}
};
template <class... _Args> using Predicate = Function<bool, _Args...>;
template <class... _Args> using Operation = Function<void, _Args...>;
template <class _R, class... _Args> constexpr Function<_R, _Args...> makeFunction(_R(*f)(_Args...)) {
return f;
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<Plus<_T, _S>, _Args...> operator+(const Function<_T, _Args...>& f, const Function<_S, _Args...>& g) {
return [=](_Args... args) {return f(args...) + g(args...); };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<Plus<_T, _S>, _Args...> operator+(_T(*f)(_Args...), const Function<_S, _Args...>& g) {
return [=](_Args... args) {return f(args...) + g(args...); };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<Plus<_T, _S>, _Args...> operator+(const Function<_T, _Args...>& f, _S(*g)(_Args...)) {
return [=](_Args... args) {return f(args...) + g(args...); };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<Plus<_T, _S>, _Args...> operator+(const _T& t, const Function<_S, _Args...>& g) {
return [=](_Args... args) {return t + g(args...); };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<Plus<_T, _S>, _Args...> operator+(const Function<_T, _Args...>& f, const _S& s) {
return [=](_Args... args) {return f(args...) + s; };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<Minus<_T, _S>, _Args...> operator-(const Function<_T, _Args...>& f, const Function<_S, _Args...>& g) {
return [=](_Args... args) {return f(args...) - g(args...); };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<Minus<_T, _S>, _Args...> operator-(_T(*f)(_Args...), const Function<_S, _Args...>& g) {
return [=](_Args... args) {return f(args...) - g(args...); };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<Minus<_T, _S>, _Args...> operator-(const Function<_T, _Args...>& f, _S(*g)(_Args...)) {
return [=](_Args... args) {return f(args...) - g(args...); };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<Minus<_T, _S>, _Args...> operator-(const _T& t, const Function<_S, _Args...>& g) {
return [=](_Args... args) {return t - g(args...); };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<Minus<_T, _S>, _Args...> operator-(const Function<_T, _Args...>& f, const _S& s) {
return [=](_Args... args) {return f(args...) - s; };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<Multiplies<_T, _S>, _Args...> operator*(const Function<_T, _Args...>& f, const Function<_S, _Args...>& g) {
return [=](_Args... args) {return f(args...) * g(args...); };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<Multiplies<_T, _S>, _Args...> operator*(_T(*f)(_Args...), const Function<_S, _Args...>& g) {
return [=](_Args... args) {return f(args...) * g(args...); };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<Multiplies<_T, _S>, _Args...> operator*(const Function<_T, _Args...>& f, _S(*g)(_Args...)) {
return [=](_Args... args) {return f(args...) * g(args...); };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<Multiplies<_T, _S>, _Args...> operator*(const _T& t, const Function<_S, _Args...>& g) {
return [=](_Args... args) {return t * g(args...); };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<Multiplies<_T, _S>, _Args...> operator*(const Function<_T, _Args...>& f, const _S& s) {
return [=](_Args... args) {return f(args...) * s; };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<Divides<_T, _S>, _Args...> operator/(const Function<_T, _Args...>& f, const Function<_S, _Args...>& g) {
return [=](_Args... args) {return f(args...) / g(args...); };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<Divides<_T, _S>, _Args...> operator/(_T(*f)(_Args...), const Function<_S, _Args...>& g) {
return [=](_Args... args) {return f(args...) / g(args...); };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<Divides<_T, _S>, _Args...> operator/(const Function<_T, _Args...>& f, _S(*g)(_Args...)) {
return [=](_Args... args) {return f(args...) / g(args...); };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<Divides<_T, _S>, _Args...> operator/(const _T& t, const Function<_S, _Args...>& g) {
return [=](_Args... args) {return t / g(args...); };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<Divides<_T, _S>, _Args...> operator/(const Function<_T, _Args...>& f, const _S& s) {
return [=](_Args... args) {return f(args...) / s; };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<Modulus<_T, _S>, _Args...> operator%(const Function<_T, _Args...>& f, const Function<_S, _Args...>& g) {
return [=](_Args... args) {return f(args...) % g(args...); };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<Modulus<_T, _S>, _Args...> operator%(_T(*f)(_Args...), const Function<_S, _Args...>& g) {
return [=](_Args... args) {return f(args...) % g(args...); };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<Modulus<_T, _S>, _Args...> operator%(const Function<_T, _Args...>& f, _S(*g)(_Args...)) {
return [=](_Args... args) {return f(args...) % g(args...); };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<Modulus<_T, _S>, _Args...> operator%(const _T& t, const Function<_S, _Args...>& g) {
return [=](_Args... args) {return t % g(args...); };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<Modulus<_T, _S>, _Args...> operator%(const Function<_T, _Args...>& f, const _S& s) {
return [=](_Args... args) {return f(args...) % s; };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<BitAnd<_T, _S>, _Args...> operator&(const Function<_T, _Args...>& f, const Function<_S, _Args...>& g) {
return [=](_Args... args) {return f(args...) & g(args...); };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<BitAnd<_T, _S>, _Args...> operator&(_T(*f)(_Args...), const Function<_S, _Args...>& g) {
return [=](_Args... args) {return f(args...) & g(args...); };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<BitAnd<_T, _S>, _Args...> operator&(const Function<_T, _Args...>& f, _S(*g)(_Args...)) {
return [=](_Args... args) {return f(args...) & g(args...); };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<BitAnd<_T, _S>, _Args...> operator&(const _T& t, const Function<_S, _Args...>& g) {
return [=](_Args... args) {return t & g(args...); };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<BitAnd<_T, _S>, _Args...> operator&(const Function<_T, _Args...>& f, const _S& s) {
return [=](_Args... args) {return f(args...) & s; };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<BitOr<_T, _S>, _Args...> operator|(const Function<_T, _Args...>& f, const Function<_S, _Args...>& g) {
return [=](_Args... args) {return f(args...) | g(args...); };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<BitOr<_T, _S>, _Args...> operator|(_T(*f)(_Args...), const Function<_S, _Args...>& g) {
return [=](_Args... args) {return f(args...) | g(args...); };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<BitOr<_T, _S>, _Args...> operator|(const Function<_T, _Args...>& f, _S(*g)(_Args...)) {
return [=](_Args... args) {return f(args...) | g(args...); };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<BitOr<_T, _S>, _Args...> operator|(const _T& t, const Function<_S, _Args...>& g) {
return [=](_Args... args) {return t | g(args...); };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<BitOr<_T, _S>, _Args...> operator|(const Function<_T, _Args...>& f, const _S& s) {
return [=](_Args... args) {return f(args...) | s; };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<BitXor<_T, _S>, _Args...> operator^(const Function<_T, _Args...>& f, const Function<_S, _Args...>& g) {
return [=](_Args... args) {return f(args...) ^ g(args...); };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<BitXor<_T, _S>, _Args...> operator^(_T(*f)(_Args...), const Function<_S, _Args...>& g) {
return [=](_Args... args) {return f(args...) ^ g(args...); };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<BitXor<_T, _S>, _Args...> operator^(const Function<_T, _Args...>& f, _S(*g)(_Args...)) {
return [=](_Args... args) {return f(args...) ^ g(args...); };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<BitXor<_T, _S>, _Args...> operator^(const _T& t, const Function<_S, _Args...>& g) {
return [=](_Args... args) {return t ^ g(args...); };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<BitXor<_T, _S>, _Args...> operator^(const Function<_T, _Args...>& f, const _S& s) {
return [=](_Args... args) {return f(args...) ^ s; };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<ShiftLeft<_T, _S>, _Args...> operator<<(const Function<_T, _Args...>& f, const Function<_S, _Args...>& g) {
return [=](_Args... args) {return f(args...) << g(args...); };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<ShiftLeft<_T, _S>, _Args...> operator<<(_T(*f)(_Args...), const Function<_S, _Args...>& g) {
return [=](_Args... args) {return f(args...) << g(args...); };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<ShiftLeft<_T, _S>, _Args...> operator<<(const Function<_T, _Args...>& f, _S(*g)(_Args...)) {
return [=](_Args... args) {return f(args...) << g(args...); };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<ShiftLeft<_T, _S>, _Args...> operator<<(const _T& t, const Function<_S, _Args...>& g) {
return [=](_Args... args) {return t << g(args...); };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<ShiftLeft<_T, _S>, _Args...> operator<<(const Function<_T, _Args...>& f, const _S& s) {
return [=](_Args... args) {return f(args...) << s; };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<ShiftRight<_T, _S>, _Args...> operator>>(const Function<_T, _Args...>& f, const Function<_S, _Args...>& g) {
return [=](_Args... args) {return f(args...) >> g(args...); };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<ShiftRight<_T, _S>, _Args...> operator>>(_T(*f)(_Args...), const Function<_S, _Args...>& g) {
return [=](_Args... args) {return f(args...) >> g(args...); };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<ShiftRight<_T, _S>, _Args...> operator>>(const Function<_T, _Args...>& f, _S(*g)(_Args...)) {
return [=](_Args... args) {return f(args...) >> g(args...); };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<ShiftRight<_T, _S>, _Args...> operator>>(const _T& t, const Function<_S, _Args...>& g) {
return [=](_Args... args) {return t >> g(args...); };
}
template <Arithmetic _T, Arithmetic _S, class... _Args> constexpr Function<ShiftRight<_T, _S>, _Args...> operator>>(const Function<_T, _Args...>& f, const _S& s) {
return [=](_Args... args) {return f(args...) >> s; };
}
template <class _T, class _S, class... _Args> constexpr Function<_T, _Args...> operator->*(const Function<_T, _S>& f, const Function<_S, _Args...>& g) {
return [=](_Args... args) {return f(g(args...)); };
}
}
#endif