-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMapping.mpp
More file actions
380 lines (354 loc) · 12 KB
/
Mapping.mpp
File metadata and controls
380 lines (354 loc) · 12 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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
export module CppUtils.Type.Mapping;
import std;
import CppUtils.String;
import CppUtils.Type.Hash;
import CppUtils.Type.Utility;
export namespace CppUtils::Type
{
template<auto Lhs, auto... Rhs>
struct Pair final
{
static constexpr auto lhs = Lhs;
static constexpr auto rhs = std::tuple{Rhs...};
};
template<class... Pairs>
struct Mapping final
{
using Values = std::tuple<Pairs...>;
Mapping() = delete;
// Complexité : O(n) à la compilation seulement
template<auto Lhs>
[[nodiscard]] static inline consteval auto toRhs() noexcept -> auto
{
if constexpr (sizeof...(Pairs) == 0)
static_assert(Type::FalseValue<Lhs>, "Lhs not found");
else
return []<auto Target, class First, class... Rest>(this auto&& self) consteval {
if constexpr (First::lhs == Target)
return First::rhs;
else if constexpr (sizeof...(Rest) == 0)
static_assert(Type::FalseValue<Target>, "Lhs not found");
else
return self.template operator()<Target, Rest...>();
}.template operator()<Lhs, Pairs...>();
}
// Complexité : O(n) à la compilation seulement
template<auto Rhs>
[[nodiscard]] static inline consteval auto toLhs() noexcept -> auto
{
if constexpr (sizeof...(Pairs) == 0)
static_assert(Type::FalseValue<Rhs>, "Rhs not found");
else
return []<auto Target, class First, class... Rest>(this auto&& self) consteval {
if constexpr (contains<Target>(First::rhs))
return First::lhs;
else if constexpr (sizeof...(Rest) == 0)
static_assert(Type::FalseValue<Target>, "Rhs not found");
else
return self.template operator()<Target, Rest...>();
}.template operator()<Rhs, Pairs...>();
}
// Complexité : O(n) à la compilation seulement
template<auto Lhs>
[[nodiscard]] static inline consteval auto toSingleRhs() noexcept -> auto
{
constexpr auto tuple = toRhs<Lhs>();
static_assert(std::tuple_size_v<decltype(tuple)> == 1);
return std::get<0>(tuple);
}
// Complexité : O(n) à la compilation seulement
template<auto Lhs>
[[nodiscard]] static inline consteval auto containsLhs() noexcept -> bool
{
return ((Pairs::lhs == Lhs) or ...);
}
// Complexité : O(n) à la compilation seulement
template<auto Rhs>
[[nodiscard]] static inline consteval auto containsRhs() noexcept -> bool
{
return (contains<Rhs>(Pairs::rhs) or ...);
}
// Complexité : O(n) à la compilation seulement
template<auto Lhs>
[[nodiscard]] static inline consteval auto hasSingleRhs() noexcept -> bool
{
if constexpr (containsLhs<Lhs>())
return std::tuple_size_v<decltype(toRhs<Lhs>())> == 1;
else
return false;
}
private:
// Complexité : O(n) à la compilation seulement
template<auto Value, class Tuple>
[[nodiscard]] static inline consteval auto contains(const Tuple& tuple) noexcept -> bool
{
return []<std::size_t... I>(const Tuple& tuple, std::index_sequence<I...>) constexpr noexcept -> bool {
return ((std::get<I>(tuple) == Value) or ...);
}(tuple, std::make_index_sequence<std::tuple_size_v<Tuple>>{});
}
struct HashParameters final
{
std::size_t seed;
std::size_t size;
};
template<class LhsType>
static inline consteval auto getLhsEntries()
{
struct Entry final
{
LhsType key;
std::size_t index;
};
auto result = []<std::size_t... I>(std::index_sequence<I...>) {
return std::array{Entry{Pairs::lhs, I}...};
}(std::make_index_sequence<sizeof...(Pairs)>{});
std::sort(std::begin(result), std::end(result), [](const auto& lhs, const auto& rhs) { return lhs.key < rhs.key; });
return result;
}
template<class LhsType>
static inline consteval auto getLhsParameters()
{
constexpr auto entries = getLhsEntries<LhsType>();
for (auto size = std::bit_ceil(sizeof...(Pairs) * 2uz); size <= 1'024; size <<= 1)
{
for (auto seed = 0uz; seed < 256; ++seed)
{
auto occupied = std::array<bool, 1'024>{};
for (auto i = 0uz; i < size; ++i)
occupied[i] = false;
auto collision = false;
for (const auto& entry : entries)
{
const auto index = (hash(entry.key) ^ seed) % size;
if (occupied[index])
{
collision = true;
break;
}
occupied[index] = true;
}
if (not collision)
return HashParameters{seed, size};
}
}
return HashParameters{0, 0};
}
template<class RhsType>
static inline consteval auto getRhsEntries()
{
struct Entry final
{
RhsType key;
std::size_t pairIndex;
};
constexpr auto totalRhs = (std::tuple_size_v<decltype(Pairs::rhs)> + ...);
auto result = std::array<Entry, totalRhs>{};
auto i = 0uz;
auto pairIdx = 0uz;
([&](auto&& pair) {
std::apply([&](auto&&... values) {
((result[i++] = {static_cast<RhsType>(values), pairIdx}), ...);
}, pair.rhs);
++pairIdx;
}(Pairs{}), ...);
std::sort(std::begin(result), std::end(result), [](const auto& lhs, const auto& rhs) { return lhs.key < rhs.key; });
return result;
}
template<class RhsType>
static inline consteval auto getRhsParameters()
{
constexpr auto entries = getRhsEntries<RhsType>();
for (auto size = std::bit_ceil(std::size(entries) * 2uz); size <= 1'024; size <<= 1)
{
for (auto seed = 0uz; seed < 256; ++seed)
{
auto occupied = std::array<bool, 1'024>{};
for (auto i = 0uz; i < size; ++i)
occupied[i] = false;
auto collision = false;
for (const auto& entry : entries)
{
const auto index = (hash(entry.key) ^ seed) % size;
if (occupied[index])
{
collision = true;
break;
}
occupied[index] = true;
}
if (not collision)
return HashParameters{seed, size};
}
}
return HashParameters{0, 0};
}
[[nodiscard]] static inline constexpr auto findLhs(auto lhs) noexcept -> std::optional<std::size_t>
{
using LhsType = decltype(lhs);
constexpr auto entries = getLhsEntries<LhsType>();
if constexpr (std::is_integral_v<LhsType> or std::is_enum_v<LhsType>)
{
constexpr auto minKey = std::min({static_cast<long long>(Pairs::lhs)...});
constexpr auto maxKey = std::max({static_cast<long long>(Pairs::lhs)...});
constexpr auto range = static_cast<std::size_t>(maxKey - minKey + 1);
if constexpr (range < std::max(2'048uz, sizeof...(Pairs) * 2))
{
static constexpr auto lookupTable = []() {
constexpr auto entries = getLhsEntries<LhsType>();
auto result = std::array<int, range>{};
result.fill(-1);
for (auto i = 0uz; i < std::size(entries); ++i)
result[static_cast<std::size_t>(static_cast<long long>(entries[i].key) - minKey)] = static_cast<int>(i);
return result;
}();
if (const auto key = static_cast<long long>(lhs); key >= minKey and key <= maxKey)
if (const auto index = lookupTable[static_cast<std::size_t>(key - minKey)]; index != -1)
return entries[static_cast<std::size_t>(index)].index;
return std::nullopt;
}
}
if (constexpr auto parameters = getLhsParameters<LhsType>(); parameters.size > 0)
{
static constexpr auto perfectHashTable = []() {
constexpr auto entries = getLhsEntries<LhsType>();
auto result = std::array<int, parameters.size>{};
result.fill(-1);
for (auto i = 0uz; i < std::size(entries); ++i)
result[(Type::hash(entries[i].key) ^ parameters.seed) % parameters.size] = static_cast<int>(i);
return result;
}();
if (const auto index = perfectHashTable[(Type::hash(lhs) ^ parameters.seed) % parameters.size]; index != -1)
if (const auto entry = entries[static_cast<std::size_t>(index)]; entry.key == lhs)
return entry.index;
}
else
{
if (auto it = std::lower_bound(std::begin(entries), std::end(entries), lhs, [](const auto& lhs, const auto& rhs) { return lhs.key < rhs; });
it != std::end(entries) and it->key == lhs)
return it->index;
}
return std::nullopt;
}
[[nodiscard]] static inline constexpr auto findRhs(auto rhs) noexcept -> std::optional<std::size_t>
{
using RhsType = decltype(rhs);
constexpr auto entries = getRhsEntries<RhsType>();
if (constexpr auto parameters = getRhsParameters<RhsType>(); parameters.size > 0)
{
static constexpr auto perfectHashTable = []() {
constexpr auto entries = getRhsEntries<RhsType>();
auto result = std::array<int, parameters.size>{};
result.fill(-1);
for (auto i = 0uz; i < std::size(entries); ++i)
result[(hash(entries[i].key) ^ parameters.seed) % parameters.size] = static_cast<int>(i);
return result;
}();
if (const auto index = perfectHashTable[(hash(rhs) ^ parameters.seed) % parameters.size]; index != -1)
if (const auto entry = entries[static_cast<std::size_t>(index)]; entry.key == rhs)
return entry.pairIndex;
}
else
{
if (auto it = std::lower_bound(std::begin(entries), std::end(entries), rhs, [](const auto& lhs, const auto& rhs) { return lhs.key < rhs; });
it != std::end(entries) and it->key == rhs)
return it->pairIndex;
}
return std::nullopt;
}
public:
// Complexité : O(1) au runtime
[[nodiscard]] static inline constexpr auto containsLhs(auto lhs) noexcept -> bool
{
return findLhs(lhs).has_value();
}
// Complexité : O(1) au runtime
[[nodiscard]] static inline constexpr auto containsRhs(auto rhs) noexcept -> bool
{
return findRhs(rhs).has_value();
}
// Complexité : O(1) au runtime
[[nodiscard]] static inline constexpr auto toRhs(auto lhs) noexcept
{
using UnifiedType = std::remove_cvref_t<CommonType<decltype(std::get<0>(Pairs::rhs))...>>;
return toRhs(lhs, std::optional<UnifiedType>{std::nullopt});
}
// Complexité : O(1) au runtime (ou O(n) pour l'extraction si hétérogène)
[[nodiscard]] static inline constexpr auto toRhs(auto lhs, auto defaultValue) -> decltype(defaultValue)
{
using DefaultType = decltype(defaultValue);
if (const auto index = findLhs(lhs))
{
using UnifiedType = std::remove_cvref_t<CommonType<decltype(std::get<0>(Pairs::rhs))...>>;
if constexpr (std::convertible_to<UnifiedType, DefaultType>)
{
static constexpr auto values = std::array{static_cast<DefaultType>(std::get<0>(Pairs::rhs))...};
if constexpr (std::is_same_v<DefaultType, std::nullptr_t>)
std::unreachable();
else
return static_cast<DefaultType>(values[*index]);
}
else
{
auto result = defaultValue;
auto i = 0uz;
std::apply([&](auto&&... pairs) {
(([&](auto&& pair) {
if (i++ == *index)
{
if constexpr (std::is_same_v<DefaultType, std::nullptr_t>)
std::unreachable();
else
result = static_cast<DefaultType>(std::get<0>(pair.rhs));
}
}(pairs)),
...);
}, Values{});
return result;
}
}
return defaultValue;
}
// Complexité : O(1) au runtime
[[nodiscard]] static inline constexpr auto toLhs(auto rhs) noexcept
{
using UnifiedType = std::remove_cvref_t<CommonType<decltype(Pairs::lhs)...>>;
return toLhs(rhs, std::optional<UnifiedType>{std::nullopt});
}
// Complexité : O(1) au runtime (ou O(n) pour l'extraction si hétérogène)
[[nodiscard]] static inline constexpr auto toLhs(auto rhs, auto defaultValue) -> decltype(defaultValue)
{
using DefaultType = decltype(defaultValue);
if (const auto pairIndex = findRhs(rhs))
{
using UnifiedType = std::remove_cvref_t<CommonType<decltype(Pairs::lhs)...>>;
if constexpr (std::convertible_to<UnifiedType, DefaultType>)
{
static constexpr auto values = std::array{static_cast<DefaultType>(Pairs::lhs)...};
if constexpr (std::is_same_v<DefaultType, std::nullptr_t>)
std::unreachable();
else
return static_cast<DefaultType>(values[*pairIndex]);
}
else
{
auto result = defaultValue;
auto i = 0uz;
std::apply([&](auto&&... pairs) {
(([&](auto&& pair) {
if (i++ == *pairIndex)
{
if constexpr (std::is_same_v<DefaultType, std::nullptr_t>)
std::unreachable();
else
result = static_cast<DefaultType>(pair.lhs);
}
}(pairs)),
...);
}, Values{});
return result;
}
}
return defaultValue;
}
};
}