-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyy_flat_map.h
539 lines (446 loc) · 15.5 KB
/
yy_flat_map.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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/*
MIT License
Copyright (c) 2024-2025 Yafiyogi
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#include <cstddef>
#include <algorithm>
#include <initializer_list>
#include <tuple>
#include <type_traits>
#include "yy_assert.h"
#include "yy_clear_action.h"
#include "yy_find_iter_util.hpp"
#include "yy_ref_traits.h"
#include "yy_type_traits.h"
#include "yy_types.hpp"
#include "yy_vector.h"
namespace yafiyogi::yy_data {
namespace flat_map_detail {
template<typename Key,
typename Value,
ClearAction KeyClearAction,
ClearAction ValueClearAction>
struct traits_type final
{
using key_type = yy_traits::remove_cvr_t<Key>;
using key_l_value_ref = typename yy_traits::ref_traits<key_type>::l_value_ref;
using key_r_value_ref = typename yy_traits::ref_traits<key_type>::r_value_ref;
using key_vector = yy_quad::simple_vector<key_type, KeyClearAction>;
using key_ptr = key_vector::value_ptr;
using const_key_ptr = key_vector::const_value_ptr;
using value_type = yy_traits::remove_cvr_t<Value>;
using value_l_value_ref = typename yy_traits::ref_traits<value_type>::l_value_ref;
using value_r_value_ref = typename yy_traits::ref_traits<value_type>::r_value_ref;
using value_vector = yy_quad::simple_vector<value_type, ValueClearAction>;
using value_ptr = value_vector::value_ptr;
using const_value_ptr = value_vector::const_value_ptr;
};
} // namespace flat_map_detail
template<typename Key,
typename Value,
ClearAction KeyClearAction = default_clear_action_v<Key>,
ClearAction ValueClearAction = default_clear_action_v<Value>,
std::size_t SearchSizeThreshold = ((yy_data::find_util_detail::size_threshold_cache_line_size * 2) / sizeof(Key)) + 1>
class flat_map final
{
public:
using traits = flat_map_detail::traits_type<Key, Value, KeyClearAction, ValueClearAction>;
using key_type = typename traits::key_type;
using key_ptr = typename traits::key_ptr;
using const_key_ptr = typename traits::const_key_ptr;
using key_l_value_ref = typename traits::key_l_value_ref;
using key_r_value_ref = typename traits::key_r_value_ref;
using value_type = typename traits::value_type;
using value_ptr = typename traits::value_ptr;
using const_value_ptr = typename traits::const_value_ptr;
using value_l_value_ref = typename traits::value_l_value_ref;
using value_r_value_ref = typename traits::value_r_value_ref;
using key_vector = typename traits::key_vector;
using key_iterator = key_vector::iterator;
using value_vector = typename traits::value_vector;
using value_iterator = value_vector::iterator;
using difference_type = std::ptrdiff_t;
template<typename KeyParamType>
using key_param_traits = yy_traits::ref_traits<KeyParamType>;
template<typename KeyParamType>
using key_param_l_value_ref = typename key_param_traits<KeyParamType>::l_value_ref;
template<typename key_type,
typename value_type>
constexpr flat_map(std::initializer_list<std::tuple<key_type, value_type>> init)
{
reserve(init.size());
for(auto & [key, value] : init)
{
emplace(std::move(key), std::move(value));
}
}
constexpr flat_map() noexcept = default;
constexpr flat_map(const flat_map &) noexcept = default;
constexpr flat_map(flat_map &&) noexcept = default;
constexpr ~flat_map() noexcept = default;
constexpr flat_map & operator=(const flat_map &) noexcept = default;
constexpr flat_map & operator=(flat_map &&) noexcept = default;
template<typename KeyParamType>
[[nodiscard]]
constexpr pos_end_type lower_bound_pos(const KeyParamType & p_key) const noexcept
{
return lower_bound_iter_pos(m_keys, p_key);
}
template<typename KeyParamType,
typename Visitor>
[[nodiscard]]
constexpr bool lower_bound(Visitor && visitor,
const KeyParamType & p_key) noexcept
{
auto [pos, is_end] = lower_bound_iter_pos(m_keys, p_key);
if(!is_end)
{
visitor(key(pos), value(pos), pos);
}
return is_end;
}
template<typename KeyParamType,
typename Visitor>
[[nodiscard]]
constexpr bool lower_bound(Visitor && visitor,
const KeyParamType & p_key) const noexcept
{
auto [pos, is_end] = lower_bound_iter_pos(m_keys, p_key);
if(!is_end)
{
visitor(key(pos), value(pos), pos);
}
return is_end;
}
struct key_value_pos_type final
{
key_ptr key = nullptr;
value_ptr value = nullptr;
size_type pos = 0;
};
template<typename KeyParamType>
[[nodiscard]]
constexpr key_value_pos_type find(const KeyParamType & p_key) noexcept
{
auto [pos, found] = find_iter_pos<SearchSizeThreshold>(m_keys, p_key);
if(found)
{
return key_value_pos_type{key(pos), value(pos), pos};
}
return key_value_pos_type{nullptr, nullptr, pos};
}
struct const_key_value_pos_type final
{
const_key_ptr key = nullptr;
const_value_ptr value = nullptr;
size_type pos = 0;
};
template<typename KeyParamType>
[[nodiscard]]
constexpr const_key_value_pos_type find(const KeyParamType & p_key) const noexcept
{
auto [pos, found] = find_iter_pos<SearchSizeThreshold>(m_keys, p_key);
if(found)
{
return const_key_value_pos_type{key(pos), value(pos), pos};
}
return const_key_value_pos_type{nullptr, nullptr, pos};
}
template<typename KeyParamType,
typename Visitor>
[[nodiscard]]
constexpr pos_found_type find_value(Visitor && visitor,
const KeyParamType & p_key) noexcept
{
pos_found_type pos_found{find_iter_pos<SearchSizeThreshold>(m_keys, p_key)};
if(pos_found.found)
{
visitor(value(pos_found.pos), pos_found.pos);
}
return pos_found;
}
template<typename KeyParamType,
typename Visitor>
[[nodiscard]]
constexpr pos_found_type find_value(Visitor && visitor,
const KeyParamType & p_key) const noexcept
{
pos_found_type pos_found{find_iter_pos<SearchSizeThreshold>(m_keys, p_key)};
if(pos_found.found)
{
visitor(value(pos_found.pos), pos_found.pos);
}
return pos_found;
}
template<typename KeyParamType>
[[nodiscard]]
constexpr pos_found_type find_pos(const KeyParamType & p_key) const noexcept
{
return find_iter_pos<SearchSizeThreshold>(m_keys, p_key);
}
struct ref_type final
{
key_type & key;
value_type & value;
};
[[nodiscard]]
constexpr ref_type operator[](size_type pos) noexcept
{
return ref_type{*key(pos), *value(pos)};
}
struct const_ref_type final
{
const key_type & key;
const value_type & value;
};
[[nodiscard]]
constexpr const_ref_type operator[](size_type pos) const noexcept
{
return const_ref_type{*key(pos), *value(pos)};
}
struct pos_inserted_type final
{
size_type pos = 0;
bool inserted = false;
};
template<typename InputKeyType,
typename InputValueType>
constexpr pos_inserted_type emplace(InputKeyType && p_key,
InputValueType && p_value)
{
static_assert(std::is_convertible_v<yy_traits::remove_cvr_t<InputKeyType>, key_type>
|| (std::is_pointer_v<InputKeyType> && std::is_base_of_v<key_type, yy_traits::remove_cvr_t<std::remove_pointer<InputKeyType>>>),
"p_key is of an incompatible type.");
static_assert(std::is_convertible_v<yy_traits::remove_cvr_t<InputValueType>, value_type>
|| (std::is_pointer_v<InputValueType> && std::is_base_of_v<value_type, yy_traits::remove_cvr_t<std::remove_pointer<InputValueType>>>),
"p_value is of an incompatible type.");
auto keys_begin{m_keys.begin()};
auto [key_iter, found] = find_iter(m_keys, p_key);
auto pos = static_cast<size_type>(key_iter - keys_begin);
if(!found)
{
auto [key, value] = add_empty(key_iter);
pos = static_cast<size_type>(key - m_keys.begin());
*key = std::forward<InputKeyType>(p_key);
*value = std::forward<InputValueType>(p_value);
}
return pos_inserted_type{pos, !found};
}
template<typename InputKeyType,
typename InputValueType>
constexpr size_type emplace(size_type p_pos,
InputKeyType && p_key,
InputValueType && p_value)
{
static_assert(std::is_convertible_v<yy_traits::remove_cvr_t<InputKeyType>, key_type>
|| (std::is_pointer_v<InputKeyType> && std::is_base_of_v<key_type, yy_traits::remove_cvr_t<std::remove_pointer<InputKeyType>>>),
"p_key is of an incompatible type.");
static_assert(std::is_convertible_v<yy_traits::remove_cvr_t<InputValueType>, value_type>
|| (std::is_pointer_v<InputValueType> && std::is_base_of_v<value_type, yy_traits::remove_cvr_t<std::remove_pointer<InputValueType>>>),
"p_value is of an incompatible type.");
auto [key, value] = add_empty(m_keys.begin() + static_cast<ssize_type>(p_pos));
*key = std::forward<InputKeyType>(p_key);
*value = std::forward<InputValueType>(p_value);
return static_cast<size_type>(key - m_keys.begin());
}
template<typename InputKeyType,
typename InputValueType>
constexpr pos_inserted_type emplace_or_assign(InputKeyType && p_key,
InputValueType && p_value)
{
static_assert(std::is_convertible_v<yy_traits::remove_cvr_t<InputValueType>, value_type>
|| (std::is_pointer_v<InputValueType> && std::is_base_of_v<value_type, yy_traits::remove_cvr_t<std::remove_pointer<InputValueType>>>),
"p_value is of an incompatible type.");
auto [key_iter, found] = find_iter(m_keys, p_key);
auto pos = (key_iter - m_keys.begin());
auto value_iter = m_values.begin() + pos;
if(!found)
{
std::tie(key_iter, value_iter) = add_empty(key_iter);
*key_iter = std::forward<InputKeyType>(p_key);
}
*value_iter = std::forward<InputValueType>(p_value);
return pos_inserted_type{static_cast<size_type>(pos), !found};
}
template<typename InputKeyType>
constexpr void erase(InputKeyType && p_key)
{
if(auto [key_iter, found] = find_iter(m_keys, p_key);
found)
{
m_keys.erase(key_iter);
ssize_type pos = key_iter - m_keys.begin();
m_values.erase(m_values.begin() + pos);
}
}
constexpr void swap(flat_map & other) noexcept
{
if(this != &other)
{
std::swap(m_keys, other.m_keys);
std::swap(m_values, other.m_values);
}
}
constexpr void swap(flat_map && other) noexcept
{
if(this != &other)
{
m_keys = std::move(other.m_keys);
m_values = std::move(other.m_values);
}
}
[[nodiscard]]
constexpr size_type size() const noexcept
{
YY_ASSERT(m_keys.size() == m_values.size());
return m_keys.size();
}
constexpr void reserve(size_type size)
{
m_keys.reserve(size);
m_values.reserve(size);
}
void clear() noexcept
{
m_keys.clear();
m_values.clear();
}
[[nodiscard]]
constexpr bool empty() const noexcept
{
return m_keys.empty();
}
[[nodiscard]]
constexpr bool operator<(const flat_map & other) const noexcept
{
if(empty())
{
return !other.empty();
}
if(!empty() && other.empty())
{
return false;
}
const auto max = std::min(size(), other.size());
for(size_type idx = 0; idx < max; ++idx)
{
if(m_keys[idx] < other.m_keys[idx])
{
return true;
}
if(!(m_keys[idx] == other.m_keys[idx]))
{
return false;
}
if(m_values[idx] < other.m_values[idx])
{
return true;
}
if(!(m_values[idx] == other.m_values[idx]))
{
return false;
}
}
return size() < other.size();
}
[[nodiscard]]
constexpr bool operator==(const flat_map & other) const noexcept
{
if(empty())
{
return other.empty();
}
if(!empty() && other.empty())
{
return false;
}
const auto max = std::min(size(), other.size());
for(size_type idx = 0; idx < max; ++idx)
{
if(!(m_keys[idx] == other.m_keys[idx]))
{
return false;
}
if(!(m_values[idx] == other.m_values[idx]))
{
return false;
}
}
return size() == other.size();
}
template<typename Visitor>
void visit(Visitor && visitor)
{
auto value_iter{m_values.begin()};
for(auto & key : m_keys)
{
visitor(key, *value_iter);
++value_iter;
}
}
template<typename Visitor>
void visit(Visitor && visitor) const
{
auto value_iter{m_values.begin()};
for(const auto & key : m_keys)
{
visitor(key, *value_iter);
++value_iter;
}
}
private:
[[nodiscard]]
constexpr std::tuple<key_iterator, value_iterator> add_empty(key_iterator p_pos)
{
auto [key_pos, key_inserted] = m_keys.add_empty(p_pos);
if(!key_inserted)
{
throw std::runtime_error("flat_map::add_empty() key add_empty() failed!");
}
auto [value_pos, value_inserted] = m_values.add_empty(m_values.begin() + (key_pos - m_keys.begin()));
if(!value_inserted)
{
throw std::runtime_error("flat_map::add_empty() value add_empty() failed!");
}
return std::make_tuple(key_pos, value_pos);
}
[[nodiscard]]
constexpr key_ptr key(size_type idx) noexcept
{
return m_keys.data() + idx;
}
[[nodiscard]]
constexpr const_key_ptr key(size_type idx) const noexcept
{
return m_keys.data() + idx;
}
[[nodiscard]]
constexpr value_ptr value(size_type idx) noexcept
{
return m_values.data() + idx;
}
[[nodiscard]]
constexpr const_value_ptr value(size_type idx) const noexcept
{
return m_values.data() + idx;
}
key_vector m_keys;
value_vector m_values;
};
} // namespace yafiyogi::yy_data