-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathutility.hpp
More file actions
254 lines (217 loc) · 9.43 KB
/
utility.hpp
File metadata and controls
254 lines (217 loc) · 9.43 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
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
#pragma once
#include <stdint.h>
#include <algorithm>
#include <functional>
#include <new>
#include <vector>
#include <type_traits>
#include <tuple>
#include <memory>
#ifdef _MSC_VER
#pragma warning(disable: 4482) // nonstandard extension used enum Name::Val, standard in C++11
#define NO_RETURN __declspec(noreturn)
#else
#define NO_RETURN __attribute__((noreturn))
#endif
#define MAX_NUM_ARRAY_ELEMENTS (1024*1024)
#define MAX_SIZE_OF_BYTE_ARRAYS (20*1024*1024)
//namespace std {
// typedef decltype(sizeof(int)) size_t;
// typedef decltype(nullptr) nullptr_t;
//}
namespace fc {
using std::size_t;
typedef decltype(nullptr) nullptr_t;
template<typename T> struct remove_reference { typedef T type; };
template<typename T> struct remove_reference<T&> { typedef T type; };
template<typename T> struct remove_reference<T&&> { typedef T type; };
template<typename T> struct deduce { typedef T type; };
template<typename T> struct deduce<T&> { typedef T type; };
template<typename T> struct deduce<const T&> { typedef T type; };
template<typename T> struct deduce<T&&> { typedef T type; };
template<typename T> struct deduce<const T&&>{ typedef T type; };
using std::move;
using std::forward;
struct true_type { enum _value { value = 1 }; };
struct false_type { enum _value { value = 0 }; };
namespace detail {
template<typename T> fc::true_type is_class_helper(void(T::*)());
template<typename T> fc::false_type is_class_helper(...);
template<typename T, typename A, typename... Args>
struct supports_allocator {
public:
static constexpr bool value = std::uses_allocator<T, A>::value;
static constexpr bool leading_allocator = std::is_constructible< T, std::allocator_arg_t, A, Args... >::value;
static constexpr bool trailing_allocator = std::is_constructible< T, Args..., A>::value;
};
template<typename T, typename A, typename... Args>
auto construct_maybe_with_allocator( A&& allocator, Args&&... args )
-> std::enable_if_t<supports_allocator<T, A>::value && supports_allocator<T, A>::leading_allocator, T>
{
return T( std::allocator_arg, std::forward<A>(allocator), std::forward<Args>(args)... );
}
template<typename T, typename A, typename... Args>
auto construct_maybe_with_allocator( A&& allocator, Args&&... args )
-> std::enable_if_t<supports_allocator<T, A>::value && !supports_allocator<T, A>::leading_allocator, T>
{
static_assert( supports_allocator<T, A>::trailing_allocator, "type supposedly supports allocators but cannot be constructed by either the leading- or trailing-allocator convention" );
return T( std::forward<Args>(args)..., std::forward<A>(allocator) );
}
template<typename T, typename A, typename... Args>
auto construct_maybe_with_allocator( A&&, Args&&... args )
-> std::enable_if_t<!supports_allocator<T, A>::value , T>
{
return T( std::forward<Args>(args)... );
}
template<typename T, typename A, typename... Args>
auto maybe_augment_constructor_arguments_with_allocator(
A&& allocator, std::tuple<Args&&...> args,
std::enable_if_t<supports_allocator<T, A>::value && supports_allocator<T, A>::leading_allocator, int> = 0
) {
return std::tuple_cat( std::forward_as_tuple<std::allocator_arg_t, A>( std::allocator_arg, allocator ), args );
}
template<typename T, typename A, typename... Args>
auto maybe_augment_constructor_arguments_with_allocator(
A&& allocator, std::tuple<>,
std::enable_if_t<supports_allocator<T, A>::value && supports_allocator<T, A>::leading_allocator, int> = 0
) {
return std::forward_as_tuple<std::allocator_arg_t, A>( std::allocator_arg, allocator );
}
template<typename T, typename A, typename... Args>
auto maybe_augment_constructor_arguments_with_allocator(
A&& allocator, std::tuple<Args&&...> args,
std::enable_if_t<supports_allocator<T, A>::value && !supports_allocator<T, A>::leading_allocator, int> = 0
) {
static_assert( supports_allocator<T, A>::trailing_allocator, "type supposedly supports allocators but cannot be constructed by either the leading- or trailing-allocator convention" );
return std::tuple_cat( args, std::forward_as_tuple<A>( allocator ) );
}
template<typename T, typename A, typename... Args>
auto maybe_augment_constructor_arguments_with_allocator(
A&& allocator, std::tuple<>,
std::enable_if_t<supports_allocator<T, A>::value && !supports_allocator<T, A>::leading_allocator, int> = 0
) {
static_assert( supports_allocator<T, A>::trailing_allocator, "type supposedly supports allocators but cannot be constructed by either the leading- or trailing-allocator convention" );
return std::forward_as_tuple<A>( allocator );
}
template<typename T, typename A, typename... Args>
auto maybe_augment_constructor_arguments_with_allocator(
A&&, std::tuple<Args&&...> args,
std::enable_if_t<!supports_allocator<T, A>::value, int> = 0
) {
return args;
}
template<typename T1, typename T2, typename A>
std::pair<T1,T2> default_construct_pair_maybe_with_allocator( A&& allocator )
{
return std::pair<T1,T2>(
std::piecewise_construct,
maybe_augment_constructor_arguments_with_allocator<T1>( allocator, std::make_tuple() ),
maybe_augment_constructor_arguments_with_allocator<T2>( allocator, std::make_tuple() )
);
}
}
template<typename T>
struct is_class { typedef decltype(detail::is_class_helper<T>(0)) type; enum value_enum { value = type::value }; };
#ifdef min
#undef min
#endif
template<typename T>
const T& min( const T& a, const T& b ) { return a < b ? a: b; }
constexpr size_t const_strlen(const char* str) {
int i = 0;
while(*(str+i) != '\0')
i++;
return i;
}
template<typename Container>
void move_append(Container& dest, Container&& src ) {
if (src.empty()) {
return;
} else if (dest.empty()) {
dest = std::move(src);
} else {
dest.insert(std::end(dest), std::make_move_iterator(std::begin(src)), std::make_move_iterator(std::end(src)));
}
}
template<typename Container>
void copy_append(Container& dest, const Container& src ) {
if (src.empty()) {
return;
} else {
dest.insert(std::end(dest), std::begin(src), std::end(src));
}
}
template<typename Container>
void deduplicate( Container& entries ) {
if (entries.size() > 1) {
std::sort( entries.begin(), entries.end() );
auto itr = std::unique( entries.begin(), entries.end() );
entries.erase( itr, entries.end() );
}
}
/**
* std::function type that verifies std::function is set before invocation
*/
template<typename Signature>
class optional_delegate;
template<typename R, typename ...Args>
class optional_delegate<R(Args...)> : private std::function<R(Args...)> {
public:
using std::function<R(Args...)>::function;
auto operator()( Args... args ) const -> R {
if (static_cast<bool>(*this)) {
if constexpr( std::is_move_constructible_v<R> ) {
return std::function<R(Args...)>::operator()(std::move(args)...);
} else {
return std::function<R(Args...)>::operator()(args...);
}
} else {
if constexpr( !std::is_void_v<R> ) {
return {};
}
}
}
};
using yield_function_t = optional_delegate<void()>;
//TODO: these should really be consteval, but they're getting pulled in by a few of our c++17 compiled files
namespace size_literals {
namespace detail {
template <unsigned ShiftAmount>
constexpr unsigned long long to_bytes(const unsigned long long val) {
constexpr unsigned long long max_ull = std::numeric_limits<unsigned long long>::max();
constexpr unsigned long long multiplier = 1ull << ShiftAmount;
if(val > max_ull/multiplier)
throw std::invalid_argument("literal too large");
return val * multiplier;
}
}
constexpr unsigned long long operator""_KiB(const unsigned long long val) {
return detail::to_bytes<10>(val);
}
constexpr unsigned long long operator""_MiB(const unsigned long long val) {
return detail::to_bytes<20>(val);
}
constexpr unsigned long long operator""_GiB(const unsigned long long val) {
return detail::to_bytes<30>(val);
}
constexpr unsigned long long operator""_TiB(const unsigned long long val) {
return detail::to_bytes<40>(val);
}
constexpr unsigned long long operator""_pow2(const unsigned long long n) {
if(n >= std::numeric_limits<unsigned long long>::digits)
throw std::invalid_argument("exponent for _pow2 too large");
return 1ull << n;
}
static_assert(4_MiB == 4*1024*1024);
static_assert(16_pow2 == 64*1024);
}
}
// outside of namespace fc becuase of VC++ conflict with std::swap
template<typename T>
void fc_swap( T& a, T& b ) {
T tmp = std::move(a);
a = std::move(b);
b = std::move(tmp);
}
#define LLCONST(constant) static_cast<int64_t>(constant##ll)
#define ULLCONST(constant) static_cast<uint64_t>(constant##ull)