fastremap 1.16.0__cp39-cp39-win32.whl → 1.17.0__cp39-cp39-win32.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1572 @@
1
+ // Copyright Malte Skarupke 2017.
2
+ // Distributed under the Boost Software License, Version 1.0.
3
+ // (See http://www.boost.org/LICENSE_1_0.txt)
4
+
5
+ // Boost Software License - Version 1.0 - August 17th, 2003
6
+
7
+ // Permission is hereby granted, free of charge, to any person or organization
8
+ // obtaining a copy of the software and accompanying documentation covered by
9
+ // this license (the "Software") to use, reproduce, display, distribute,
10
+ // execute, and transmit the Software, and to prepare derivative works of the
11
+ // Software, and to permit third-parties to whom the Software is furnished to
12
+ // do so, all subject to the following:
13
+
14
+ // The copyright notices in the Software and this entire statement, including
15
+ // the above license grant, this restriction and the following disclaimer,
16
+ // must be included in all copies of the Software, in whole or in part, and
17
+ // all derivative works of the Software, unless such copies or derivative
18
+ // works are solely in the form of machine-executable object code generated by
19
+ // a source language processor.
20
+
21
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
+ // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24
+ // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25
+ // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26
+ // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27
+ // DEALINGS IN THE SOFTWARE.
28
+
29
+ #pragma once
30
+
31
+ // Check windows
32
+ #if _WIN32 || _WIN64
33
+ #if _WIN64
34
+ #define ENV64BIT
35
+ #else
36
+ #define ENV32BIT
37
+ #endif
38
+ #endif
39
+
40
+ // Check GCC
41
+ #if __GNUC__
42
+ #if __x86_64__ || __ppc64__ || __aarch64__ || __LP64__
43
+ #define ENV64BIT
44
+ #else
45
+ #define ENV32BIT
46
+ #endif
47
+ #endif
48
+
49
+ #include <cstdint>
50
+ #include <cstddef>
51
+ #include <functional>
52
+ #include <cmath>
53
+ #include <algorithm>
54
+ #include <iterator>
55
+ #include <utility>
56
+ #include <type_traits>
57
+
58
+ #ifdef _MSC_VER
59
+ #define SKA_NOINLINE(...) __declspec(noinline) __VA_ARGS__
60
+ #else
61
+ #define SKA_NOINLINE(...) __VA_ARGS__ __attribute__((noinline))
62
+ #endif
63
+
64
+ namespace ska
65
+ {
66
+ struct prime_number_hash_policy;
67
+ struct power_of_two_hash_policy;
68
+ struct fibonacci_hash_policy;
69
+
70
+ namespace detailv3
71
+ {
72
+ template<typename Result, typename Functor>
73
+ struct functor_storage : Functor
74
+ {
75
+ functor_storage() = default;
76
+ functor_storage(const Functor & functor)
77
+ : Functor(functor)
78
+ {
79
+ }
80
+ template<typename... Args>
81
+ Result operator()(Args &&... args)
82
+ {
83
+ return static_cast<Functor &>(*this)(std::forward<Args>(args)...);
84
+ }
85
+ template<typename... Args>
86
+ Result operator()(Args &&... args) const
87
+ {
88
+ return static_cast<const Functor &>(*this)(std::forward<Args>(args)...);
89
+ }
90
+ };
91
+ template<typename Result, typename... Args>
92
+ struct functor_storage<Result, Result (*)(Args...)>
93
+ {
94
+ typedef Result (*function_ptr)(Args...);
95
+ function_ptr function;
96
+ functor_storage(function_ptr function)
97
+ : function(function)
98
+ {
99
+ }
100
+ Result operator()(Args... args) const
101
+ {
102
+ return function(std::forward<Args>(args)...);
103
+ }
104
+ operator function_ptr &()
105
+ {
106
+ return function;
107
+ }
108
+ operator const function_ptr &()
109
+ {
110
+ return function;
111
+ }
112
+ };
113
+ template<typename key_type, typename value_type, typename hasher>
114
+ struct KeyOrValueHasher : functor_storage<size_t, hasher>
115
+ {
116
+ typedef functor_storage<size_t, hasher> hasher_storage;
117
+ KeyOrValueHasher() = default;
118
+ KeyOrValueHasher(const hasher & hash)
119
+ : hasher_storage(hash)
120
+ {
121
+ }
122
+ size_t operator()(const key_type & key)
123
+ {
124
+ return static_cast<hasher_storage &>(*this)(key);
125
+ }
126
+ size_t operator()(const key_type & key) const
127
+ {
128
+ return static_cast<const hasher_storage &>(*this)(key);
129
+ }
130
+ size_t operator()(const value_type & value)
131
+ {
132
+ return static_cast<hasher_storage &>(*this)(value.first);
133
+ }
134
+ size_t operator()(const value_type & value) const
135
+ {
136
+ return static_cast<const hasher_storage &>(*this)(value.first);
137
+ }
138
+ template<typename F, typename S>
139
+ size_t operator()(const std::pair<F, S> & value)
140
+ {
141
+ return static_cast<hasher_storage &>(*this)(value.first);
142
+ }
143
+ template<typename F, typename S>
144
+ size_t operator()(const std::pair<F, S> & value) const
145
+ {
146
+ return static_cast<const hasher_storage &>(*this)(value.first);
147
+ }
148
+ };
149
+ template<typename key_type, typename value_type, typename key_equal>
150
+ struct KeyOrValueEquality : functor_storage<bool, key_equal>
151
+ {
152
+ typedef functor_storage<bool, key_equal> equality_storage;
153
+ KeyOrValueEquality() = default;
154
+ KeyOrValueEquality(const key_equal & equality)
155
+ : equality_storage(equality)
156
+ {
157
+ }
158
+ bool operator()(const key_type & lhs, const key_type & rhs)
159
+ {
160
+ return static_cast<equality_storage &>(*this)(lhs, rhs);
161
+ }
162
+ bool operator()(const key_type & lhs, const value_type & rhs)
163
+ {
164
+ return static_cast<equality_storage &>(*this)(lhs, rhs.first);
165
+ }
166
+ bool operator()(const value_type & lhs, const key_type & rhs)
167
+ {
168
+ return static_cast<equality_storage &>(*this)(lhs.first, rhs);
169
+ }
170
+ bool operator()(const value_type & lhs, const value_type & rhs)
171
+ {
172
+ return static_cast<equality_storage &>(*this)(lhs.first, rhs.first);
173
+ }
174
+ template<typename F, typename S>
175
+ bool operator()(const key_type & lhs, const std::pair<F, S> & rhs)
176
+ {
177
+ return static_cast<equality_storage &>(*this)(lhs, rhs.first);
178
+ }
179
+ template<typename F, typename S>
180
+ bool operator()(const std::pair<F, S> & lhs, const key_type & rhs)
181
+ {
182
+ return static_cast<equality_storage &>(*this)(lhs.first, rhs);
183
+ }
184
+ template<typename F, typename S>
185
+ bool operator()(const value_type & lhs, const std::pair<F, S> & rhs)
186
+ {
187
+ return static_cast<equality_storage &>(*this)(lhs.first, rhs.first);
188
+ }
189
+ template<typename F, typename S>
190
+ bool operator()(const std::pair<F, S> & lhs, const value_type & rhs)
191
+ {
192
+ return static_cast<equality_storage &>(*this)(lhs.first, rhs.first);
193
+ }
194
+ template<typename FL, typename SL, typename FR, typename SR>
195
+ bool operator()(const std::pair<FL, SL> & lhs, const std::pair<FR, SR> & rhs)
196
+ {
197
+ return static_cast<equality_storage &>(*this)(lhs.first, rhs.first);
198
+ }
199
+ };
200
+ static constexpr int8_t min_lookups = 4;
201
+ template<typename T>
202
+ struct sherwood_v3_entry
203
+ {
204
+ sherwood_v3_entry()
205
+ {
206
+ }
207
+ sherwood_v3_entry(int8_t distance_from_desired)
208
+ : distance_from_desired(distance_from_desired)
209
+ {
210
+ }
211
+ ~sherwood_v3_entry()
212
+ {
213
+ }
214
+
215
+ bool has_value() const
216
+ {
217
+ return distance_from_desired >= 0;
218
+ }
219
+ bool is_empty() const
220
+ {
221
+ return distance_from_desired < 0;
222
+ }
223
+ bool is_at_desired_position() const
224
+ {
225
+ return distance_from_desired <= 0;
226
+ }
227
+ template<typename... Args>
228
+ void emplace(int8_t distance, Args &&... args)
229
+ {
230
+ new (std::addressof(value)) T(std::forward<Args>(args)...);
231
+ distance_from_desired = distance;
232
+ }
233
+
234
+ void destroy_value()
235
+ {
236
+ value.~T();
237
+ distance_from_desired = -1;
238
+ }
239
+
240
+ int8_t distance_from_desired = -1;
241
+ static constexpr int8_t special_end_value = 0;
242
+ union { T value; };
243
+ };
244
+
245
+ inline int8_t log2(size_t value)
246
+ {
247
+ #ifdef ENV64BIT
248
+ static constexpr int8_t table[64] =
249
+ {
250
+ 63, 0, 58, 1, 59, 47, 53, 2,
251
+ 60, 39, 48, 27, 54, 33, 42, 3,
252
+ 61, 51, 37, 40, 49, 18, 28, 20,
253
+ 55, 30, 34, 11, 43, 14, 22, 4,
254
+ 62, 57, 46, 52, 38, 26, 32, 41,
255
+ 50, 36, 17, 19, 29, 10, 13, 21,
256
+ 56, 45, 25, 31, 35, 16, 9, 12,
257
+ 44, 24, 15, 8, 23, 7, 6, 5
258
+ };
259
+ value |= value >> 1;
260
+ value |= value >> 2;
261
+ value |= value >> 4;
262
+ value |= value >> 8;
263
+ value |= value >> 16;
264
+ value |= value >> 32;
265
+ return table[((value - (value >> 1)) * 0x07EDD5E59A4E28C2) >> 58];
266
+ #endif
267
+ #ifdef ENV32BIT
268
+ static constexpr int8_t table[32] =
269
+ {
270
+ 0, 9, 1, 10, 13, 21, 2, 29,
271
+ 11, 14, 16, 18, 22, 25, 3, 30,
272
+ 8, 12, 20, 28, 15, 17, 24, 7,
273
+ 19, 27, 23, 6, 26, 5, 4, 31
274
+ };
275
+
276
+ value |= value >> 1;
277
+ value |= value >> 2;
278
+ value |= value >> 4;
279
+ value |= value >> 8;
280
+ value |= value >> 16;
281
+ return table[(value*0x07C4ACDD) >> 27];
282
+ #endif
283
+ }
284
+
285
+ template<typename T, bool>
286
+ struct AssignIfTrue
287
+ {
288
+ void operator()(T & lhs, const T & rhs)
289
+ {
290
+ lhs = rhs;
291
+ }
292
+ void operator()(T & lhs, T && rhs)
293
+ {
294
+ lhs = std::move(rhs);
295
+ }
296
+ };
297
+ template<typename T>
298
+ struct AssignIfTrue<T, false>
299
+ {
300
+ void operator()(T &, const T &)
301
+ {
302
+ }
303
+ void operator()(T &, T &&)
304
+ {
305
+ }
306
+ };
307
+
308
+ inline size_t next_power_of_two(size_t i)
309
+ {
310
+ --i;
311
+ i |= i >> 1;
312
+ i |= i >> 2;
313
+ i |= i >> 4;
314
+ i |= i >> 8;
315
+ i |= i >> 16;
316
+ #ifdef ENV64BIT
317
+ i |= i >> 32;
318
+ #endif
319
+ ++i;
320
+ return i;
321
+ }
322
+
323
+ // Implementation taken from http://en.cppreference.com/w/cpp/types/void_t
324
+ // (it takes CWG1558 into account and also works for older compilers)
325
+ template<typename... Ts> struct make_void { typedef void type;};
326
+ template<typename... Ts> using void_t = typename make_void<Ts...>::type;
327
+
328
+ template<typename T, typename = void>
329
+ struct HashPolicySelector
330
+ {
331
+ typedef fibonacci_hash_policy type;
332
+ };
333
+ template<typename T>
334
+ struct HashPolicySelector<T, void_t<typename T::hash_policy>>
335
+ {
336
+ typedef typename T::hash_policy type;
337
+ };
338
+
339
+ template<typename T, typename FindKey, typename ArgumentHash, typename Hasher, typename ArgumentEqual, typename Equal, typename ArgumentAlloc, typename EntryAlloc>
340
+ class sherwood_v3_table : private EntryAlloc, private Hasher, private Equal
341
+ {
342
+ using Entry = detailv3::sherwood_v3_entry<T>;
343
+ using AllocatorTraits = std::allocator_traits<EntryAlloc>;
344
+ using EntryPointer = typename AllocatorTraits::pointer;
345
+ struct convertible_to_iterator;
346
+
347
+ public:
348
+
349
+ using value_type = T;
350
+ using size_type = size_t;
351
+ using difference_type = std::ptrdiff_t;
352
+ using hasher = ArgumentHash;
353
+ using key_equal = ArgumentEqual;
354
+ using allocator_type = EntryAlloc;
355
+ using reference = value_type &;
356
+ using const_reference = const value_type &;
357
+ using pointer = value_type *;
358
+ using const_pointer = const value_type *;
359
+
360
+ sherwood_v3_table()
361
+ {
362
+ }
363
+ explicit sherwood_v3_table(size_type bucket_count, const ArgumentHash & hash = ArgumentHash(), const ArgumentEqual & equal = ArgumentEqual(), const ArgumentAlloc & alloc = ArgumentAlloc())
364
+ : EntryAlloc(alloc), Hasher(hash), Equal(equal)
365
+ {
366
+ rehash(bucket_count);
367
+ }
368
+ sherwood_v3_table(size_type bucket_count, const ArgumentAlloc & alloc)
369
+ : sherwood_v3_table(bucket_count, ArgumentHash(), ArgumentEqual(), alloc)
370
+ {
371
+ }
372
+ sherwood_v3_table(size_type bucket_count, const ArgumentHash & hash, const ArgumentAlloc & alloc)
373
+ : sherwood_v3_table(bucket_count, hash, ArgumentEqual(), alloc)
374
+ {
375
+ }
376
+ explicit sherwood_v3_table(const ArgumentAlloc & alloc)
377
+ : EntryAlloc(alloc)
378
+ {
379
+ }
380
+ template<typename It>
381
+ sherwood_v3_table(It first, It last, size_type bucket_count = 0, const ArgumentHash & hash = ArgumentHash(), const ArgumentEqual & equal = ArgumentEqual(), const ArgumentAlloc & alloc = ArgumentAlloc())
382
+ : sherwood_v3_table(bucket_count, hash, equal, alloc)
383
+ {
384
+ insert(first, last);
385
+ }
386
+ template<typename It>
387
+ sherwood_v3_table(It first, It last, size_type bucket_count, const ArgumentAlloc & alloc)
388
+ : sherwood_v3_table(first, last, bucket_count, ArgumentHash(), ArgumentEqual(), alloc)
389
+ {
390
+ }
391
+ template<typename It>
392
+ sherwood_v3_table(It first, It last, size_type bucket_count, const ArgumentHash & hash, const ArgumentAlloc & alloc)
393
+ : sherwood_v3_table(first, last, bucket_count, hash, ArgumentEqual(), alloc)
394
+ {
395
+ }
396
+ sherwood_v3_table(std::initializer_list<T> il, size_type bucket_count = 0, const ArgumentHash & hash = ArgumentHash(), const ArgumentEqual & equal = ArgumentEqual(), const ArgumentAlloc & alloc = ArgumentAlloc())
397
+ : sherwood_v3_table(bucket_count, hash, equal, alloc)
398
+ {
399
+ if (bucket_count == 0)
400
+ rehash(il.size());
401
+ insert(il.begin(), il.end());
402
+ }
403
+ sherwood_v3_table(std::initializer_list<T> il, size_type bucket_count, const ArgumentAlloc & alloc)
404
+ : sherwood_v3_table(il, bucket_count, ArgumentHash(), ArgumentEqual(), alloc)
405
+ {
406
+ }
407
+ sherwood_v3_table(std::initializer_list<T> il, size_type bucket_count, const ArgumentHash & hash, const ArgumentAlloc & alloc)
408
+ : sherwood_v3_table(il, bucket_count, hash, ArgumentEqual(), alloc)
409
+ {
410
+ }
411
+ sherwood_v3_table(const sherwood_v3_table & other)
412
+ : sherwood_v3_table(other, AllocatorTraits::select_on_container_copy_construction(other.get_allocator()))
413
+ {
414
+ }
415
+ sherwood_v3_table(const sherwood_v3_table & other, const ArgumentAlloc & alloc)
416
+ : EntryAlloc(alloc), Hasher(other), Equal(other), _max_load_factor(other._max_load_factor)
417
+ {
418
+ rehash_for_other_container(other);
419
+ try
420
+ {
421
+ insert(other.begin(), other.end());
422
+ }
423
+ catch(...)
424
+ {
425
+ clear();
426
+ deallocate_data(entries, num_slots_minus_one, max_lookups);
427
+ throw;
428
+ }
429
+ }
430
+ sherwood_v3_table(sherwood_v3_table && other) noexcept
431
+ : EntryAlloc(std::move(other)), Hasher(std::move(other)), Equal(std::move(other))
432
+ {
433
+ swap_pointers(other);
434
+ }
435
+ sherwood_v3_table(sherwood_v3_table && other, const ArgumentAlloc & alloc) noexcept
436
+ : EntryAlloc(alloc), Hasher(std::move(other)), Equal(std::move(other))
437
+ {
438
+ swap_pointers(other);
439
+ }
440
+ sherwood_v3_table & operator=(const sherwood_v3_table & other)
441
+ {
442
+ if (this == std::addressof(other))
443
+ return *this;
444
+
445
+ clear();
446
+ if (AllocatorTraits::propagate_on_container_copy_assignment::value)
447
+ {
448
+ if (static_cast<EntryAlloc &>(*this) != static_cast<const EntryAlloc &>(other))
449
+ {
450
+ reset_to_empty_state();
451
+ }
452
+ AssignIfTrue<EntryAlloc, AllocatorTraits::propagate_on_container_copy_assignment::value>()(*this, other);
453
+ }
454
+ _max_load_factor = other._max_load_factor;
455
+ static_cast<Hasher &>(*this) = other;
456
+ static_cast<Equal &>(*this) = other;
457
+ rehash_for_other_container(other);
458
+ insert(other.begin(), other.end());
459
+ return *this;
460
+ }
461
+ sherwood_v3_table & operator=(sherwood_v3_table && other) noexcept
462
+ {
463
+ if (this == std::addressof(other))
464
+ return *this;
465
+ else if (AllocatorTraits::propagate_on_container_move_assignment::value)
466
+ {
467
+ clear();
468
+ reset_to_empty_state();
469
+ AssignIfTrue<EntryAlloc, AllocatorTraits::propagate_on_container_move_assignment::value>()(*this, std::move(other));
470
+ swap_pointers(other);
471
+ }
472
+ else if (static_cast<EntryAlloc &>(*this) == static_cast<EntryAlloc &>(other))
473
+ {
474
+ swap_pointers(other);
475
+ }
476
+ else
477
+ {
478
+ clear();
479
+ _max_load_factor = other._max_load_factor;
480
+ rehash_for_other_container(other);
481
+ for (T & elem : other)
482
+ emplace(std::move(elem));
483
+ other.clear();
484
+ }
485
+ static_cast<Hasher &>(*this) = std::move(other);
486
+ static_cast<Equal &>(*this) = std::move(other);
487
+ return *this;
488
+ }
489
+ ~sherwood_v3_table()
490
+ {
491
+ clear();
492
+ deallocate_data(entries, num_slots_minus_one, max_lookups);
493
+ }
494
+
495
+ const allocator_type & get_allocator() const
496
+ {
497
+ return static_cast<const allocator_type &>(*this);
498
+ }
499
+ const ArgumentEqual & key_eq() const
500
+ {
501
+ return static_cast<const ArgumentEqual &>(*this);
502
+ }
503
+ const ArgumentHash & hash_function() const
504
+ {
505
+ return static_cast<const ArgumentHash &>(*this);
506
+ }
507
+
508
+ template<typename ValueType>
509
+ struct templated_iterator
510
+ {
511
+ templated_iterator() = default;
512
+ templated_iterator(EntryPointer current)
513
+ : current(current)
514
+ {
515
+ }
516
+ EntryPointer current = EntryPointer();
517
+
518
+ using iterator_category = std::forward_iterator_tag;
519
+ using value_type = ValueType;
520
+ using difference_type = ptrdiff_t;
521
+ using pointer = ValueType *;
522
+ using reference = ValueType &;
523
+
524
+ friend bool operator==(const templated_iterator & lhs, const templated_iterator & rhs)
525
+ {
526
+ return lhs.current == rhs.current;
527
+ }
528
+ friend bool operator!=(const templated_iterator & lhs, const templated_iterator & rhs)
529
+ {
530
+ return !(lhs == rhs);
531
+ }
532
+
533
+ templated_iterator & operator++()
534
+ {
535
+ do
536
+ {
537
+ ++current;
538
+ }
539
+ while(current->is_empty());
540
+ return *this;
541
+ }
542
+ templated_iterator operator++(int)
543
+ {
544
+ templated_iterator copy(*this);
545
+ ++*this;
546
+ return copy;
547
+ }
548
+
549
+ ValueType & operator*() const
550
+ {
551
+ return current->value;
552
+ }
553
+ ValueType * operator->() const
554
+ {
555
+ return std::addressof(current->value);
556
+ }
557
+
558
+ operator templated_iterator<const value_type>() const
559
+ {
560
+ return { current };
561
+ }
562
+ };
563
+ using iterator = templated_iterator<value_type>;
564
+ using const_iterator = templated_iterator<const value_type>;
565
+
566
+ iterator begin()
567
+ {
568
+ for (EntryPointer it = entries;; ++it)
569
+ {
570
+ if (it->has_value())
571
+ return { it };
572
+ }
573
+ }
574
+ const_iterator begin() const
575
+ {
576
+ for (EntryPointer it = entries;; ++it)
577
+ {
578
+ if (it->has_value())
579
+ return { it };
580
+ }
581
+ }
582
+ const_iterator cbegin() const
583
+ {
584
+ return begin();
585
+ }
586
+ iterator end()
587
+ {
588
+ return { entries + static_cast<ptrdiff_t>(num_slots_minus_one + max_lookups) };
589
+ }
590
+ const_iterator end() const
591
+ {
592
+ return { entries + static_cast<ptrdiff_t>(num_slots_minus_one + max_lookups) };
593
+ }
594
+ const_iterator cend() const
595
+ {
596
+ return end();
597
+ }
598
+
599
+ iterator find(const FindKey & key)
600
+ {
601
+ size_t index = hash_policy.index_for_hash(hash_object(key), num_slots_minus_one);
602
+ EntryPointer it = entries + ptrdiff_t(index);
603
+ for (int8_t distance = 0; it->distance_from_desired >= distance; ++distance, ++it)
604
+ {
605
+ if (compares_equal(key, it->value))
606
+ return { it };
607
+ }
608
+ return end();
609
+ }
610
+ const_iterator find(const FindKey & key) const
611
+ {
612
+ return const_cast<sherwood_v3_table *>(this)->find(key);
613
+ }
614
+ size_t count(const FindKey & key) const
615
+ {
616
+ return find(key) == end() ? 0 : 1;
617
+ }
618
+ std::pair<iterator, iterator> equal_range(const FindKey & key)
619
+ {
620
+ iterator found = find(key);
621
+ if (found == end())
622
+ return { found, found };
623
+ else
624
+ return { found, std::next(found) };
625
+ }
626
+ std::pair<const_iterator, const_iterator> equal_range(const FindKey & key) const
627
+ {
628
+ const_iterator found = find(key);
629
+ if (found == end())
630
+ return { found, found };
631
+ else
632
+ return { found, std::next(found) };
633
+ }
634
+
635
+ template<typename Key, typename... Args>
636
+ std::pair<iterator, bool> emplace(Key && key, Args &&... args)
637
+ {
638
+ size_t index = hash_policy.index_for_hash(hash_object(key), num_slots_minus_one);
639
+ EntryPointer current_entry = entries + ptrdiff_t(index);
640
+ int8_t distance_from_desired = 0;
641
+ for (; current_entry->distance_from_desired >= distance_from_desired; ++current_entry, ++distance_from_desired)
642
+ {
643
+ if (compares_equal(key, current_entry->value))
644
+ return { { current_entry }, false };
645
+ }
646
+ return emplace_new_key(distance_from_desired, current_entry, std::forward<Key>(key), std::forward<Args>(args)...);
647
+ }
648
+
649
+ std::pair<iterator, bool> insert(const value_type & value)
650
+ {
651
+ return emplace(value);
652
+ }
653
+ std::pair<iterator, bool> insert(value_type && value)
654
+ {
655
+ return emplace(std::move(value));
656
+ }
657
+ template<typename... Args>
658
+ iterator emplace_hint(const_iterator, Args &&... args)
659
+ {
660
+ return emplace(std::forward<Args>(args)...).first;
661
+ }
662
+ iterator insert(const_iterator, const value_type & value)
663
+ {
664
+ return emplace(value).first;
665
+ }
666
+ iterator insert(const_iterator, value_type && value)
667
+ {
668
+ return emplace(std::move(value)).first;
669
+ }
670
+
671
+ template<typename It>
672
+ void insert(It begin, It end)
673
+ {
674
+ for (; begin != end; ++begin)
675
+ {
676
+ emplace(*begin);
677
+ }
678
+ }
679
+ void insert(std::initializer_list<value_type> il)
680
+ {
681
+ insert(il.begin(), il.end());
682
+ }
683
+
684
+ void rehash(size_t num_buckets)
685
+ {
686
+ num_buckets = std::max(num_buckets, static_cast<size_t>(std::ceil(num_elements / static_cast<double>(_max_load_factor))));
687
+ if (num_buckets == 0)
688
+ {
689
+ reset_to_empty_state();
690
+ return;
691
+ }
692
+ auto new_prime_index = hash_policy.next_size_over(num_buckets);
693
+ if (num_buckets == bucket_count())
694
+ return;
695
+ int8_t new_max_lookups = compute_max_lookups(num_buckets);
696
+ EntryPointer new_buckets(AllocatorTraits::allocate(*this, num_buckets + new_max_lookups));
697
+ EntryPointer special_end_item = new_buckets + static_cast<ptrdiff_t>(num_buckets + new_max_lookups - 1);
698
+ for (EntryPointer it = new_buckets; it != special_end_item; ++it)
699
+ it->distance_from_desired = -1;
700
+ special_end_item->distance_from_desired = Entry::special_end_value;
701
+ std::swap(entries, new_buckets);
702
+ std::swap(num_slots_minus_one, num_buckets);
703
+ --num_slots_minus_one;
704
+ hash_policy.commit(new_prime_index);
705
+ int8_t old_max_lookups = max_lookups;
706
+ max_lookups = new_max_lookups;
707
+ num_elements = 0;
708
+ for (EntryPointer it = new_buckets, end = it + static_cast<ptrdiff_t>(num_buckets + old_max_lookups); it != end; ++it)
709
+ {
710
+ if (it->has_value())
711
+ {
712
+ emplace(std::move(it->value));
713
+ it->destroy_value();
714
+ }
715
+ }
716
+ deallocate_data(new_buckets, num_buckets, old_max_lookups);
717
+ }
718
+
719
+ void reserve(size_t num_elements)
720
+ {
721
+ size_t required_buckets = num_buckets_for_reserve(num_elements);
722
+ if (required_buckets > bucket_count())
723
+ rehash(required_buckets);
724
+ }
725
+
726
+ // the return value is a type that can be converted to an iterator
727
+ // the reason for doing this is that it's not free to find the
728
+ // iterator pointing at the next element. if you care about the
729
+ // next iterator, turn the return value into an iterator
730
+ convertible_to_iterator erase(const_iterator to_erase)
731
+ {
732
+ EntryPointer current = to_erase.current;
733
+ current->destroy_value();
734
+ --num_elements;
735
+ for (EntryPointer next = current + ptrdiff_t(1); !next->is_at_desired_position(); ++current, ++next)
736
+ {
737
+ current->emplace(next->distance_from_desired - 1, std::move(next->value));
738
+ next->destroy_value();
739
+ }
740
+ return { to_erase.current };
741
+ }
742
+
743
+ iterator erase(const_iterator begin_it, const_iterator end_it)
744
+ {
745
+ if (begin_it == end_it)
746
+ return { begin_it.current };
747
+ for (EntryPointer it = begin_it.current, end = end_it.current; it != end; ++it)
748
+ {
749
+ if (it->has_value())
750
+ {
751
+ it->destroy_value();
752
+ --num_elements;
753
+ }
754
+ }
755
+ if (end_it == this->end())
756
+ return this->end();
757
+ ptrdiff_t num_to_move = std::min(static_cast<ptrdiff_t>(end_it.current->distance_from_desired), end_it.current - begin_it.current);
758
+ EntryPointer to_return = end_it.current - num_to_move;
759
+ for (EntryPointer it = end_it.current; !it->is_at_desired_position();)
760
+ {
761
+ EntryPointer target = it - num_to_move;
762
+ target->emplace(it->distance_from_desired - num_to_move, std::move(it->value));
763
+ it->destroy_value();
764
+ ++it;
765
+ num_to_move = std::min(static_cast<ptrdiff_t>(it->distance_from_desired), num_to_move);
766
+ }
767
+ return { to_return };
768
+ }
769
+
770
+ size_t erase(const FindKey & key)
771
+ {
772
+ auto found = find(key);
773
+ if (found == end())
774
+ return 0;
775
+ else
776
+ {
777
+ erase(found);
778
+ return 1;
779
+ }
780
+ }
781
+
782
+ void clear()
783
+ {
784
+ for (EntryPointer it = entries, end = it + static_cast<ptrdiff_t>(num_slots_minus_one + max_lookups); it != end; ++it)
785
+ {
786
+ if (it->has_value())
787
+ it->destroy_value();
788
+ }
789
+ num_elements = 0;
790
+ }
791
+
792
+ void shrink_to_fit()
793
+ {
794
+ rehash_for_other_container(*this);
795
+ }
796
+
797
+ void swap(sherwood_v3_table & other)
798
+ {
799
+ using std::swap;
800
+ swap_pointers(other);
801
+ swap(static_cast<ArgumentHash &>(*this), static_cast<ArgumentHash &>(other));
802
+ swap(static_cast<ArgumentEqual &>(*this), static_cast<ArgumentEqual &>(other));
803
+ if (AllocatorTraits::propagate_on_container_swap::value)
804
+ swap(static_cast<EntryAlloc &>(*this), static_cast<EntryAlloc &>(other));
805
+ }
806
+
807
+ size_t size() const
808
+ {
809
+ return num_elements;
810
+ }
811
+ size_t max_size() const
812
+ {
813
+ return (AllocatorTraits::max_size(*this)) / sizeof(Entry);
814
+ }
815
+ size_t bucket_count() const
816
+ {
817
+ return num_slots_minus_one ? num_slots_minus_one + 1 : 0;
818
+ }
819
+ size_type max_bucket_count() const
820
+ {
821
+ return (AllocatorTraits::max_size(*this) - min_lookups) / sizeof(Entry);
822
+ }
823
+ size_t bucket(const FindKey & key) const
824
+ {
825
+ return hash_policy.index_for_hash(hash_object(key), num_slots_minus_one);
826
+ }
827
+ float load_factor() const
828
+ {
829
+ size_t buckets = bucket_count();
830
+ if (buckets)
831
+ return static_cast<float>(num_elements) / bucket_count();
832
+ else
833
+ return 0;
834
+ }
835
+ void max_load_factor(float value)
836
+ {
837
+ _max_load_factor = value;
838
+ }
839
+ float max_load_factor() const
840
+ {
841
+ return _max_load_factor;
842
+ }
843
+
844
+ bool empty() const
845
+ {
846
+ return num_elements == 0;
847
+ }
848
+
849
+ private:
850
+ EntryPointer entries = empty_default_table();
851
+ size_t num_slots_minus_one = 0;
852
+ typename HashPolicySelector<ArgumentHash>::type hash_policy;
853
+ int8_t max_lookups = detailv3::min_lookups - 1;
854
+ float _max_load_factor = 0.5f;
855
+ size_t num_elements = 0;
856
+
857
+ EntryPointer empty_default_table()
858
+ {
859
+ EntryPointer result = AllocatorTraits::allocate(*this, detailv3::min_lookups);
860
+ EntryPointer special_end_item = result + static_cast<ptrdiff_t>(detailv3::min_lookups - 1);
861
+ for (EntryPointer it = result; it != special_end_item; ++it)
862
+ it->distance_from_desired = -1;
863
+ special_end_item->distance_from_desired = Entry::special_end_value;
864
+ return result;
865
+ }
866
+
867
+ static int8_t compute_max_lookups(size_t num_buckets)
868
+ {
869
+ int8_t desired = detailv3::log2(num_buckets);
870
+ return std::max(detailv3::min_lookups, desired);
871
+ }
872
+
873
+ size_t num_buckets_for_reserve(size_t num_elements) const
874
+ {
875
+ return static_cast<size_t>(std::ceil(num_elements / std::min(0.5, static_cast<double>(_max_load_factor))));
876
+ }
877
+ void rehash_for_other_container(const sherwood_v3_table & other)
878
+ {
879
+ rehash(std::min(num_buckets_for_reserve(other.size()), other.bucket_count()));
880
+ }
881
+
882
+ void swap_pointers(sherwood_v3_table & other)
883
+ {
884
+ using std::swap;
885
+ swap(hash_policy, other.hash_policy);
886
+ swap(entries, other.entries);
887
+ swap(num_slots_minus_one, other.num_slots_minus_one);
888
+ swap(num_elements, other.num_elements);
889
+ swap(max_lookups, other.max_lookups);
890
+ swap(_max_load_factor, other._max_load_factor);
891
+ }
892
+
893
+ template<typename Key, typename... Args>
894
+ SKA_NOINLINE(std::pair<iterator, bool>) emplace_new_key(int8_t distance_from_desired, EntryPointer current_entry, Key && key, Args &&... args)
895
+ {
896
+ using std::swap;
897
+ if (num_slots_minus_one == 0 || distance_from_desired == max_lookups || num_elements + 1 > (num_slots_minus_one + 1) * static_cast<double>(_max_load_factor))
898
+ {
899
+ grow();
900
+ return emplace(std::forward<Key>(key), std::forward<Args>(args)...);
901
+ }
902
+ else if (current_entry->is_empty())
903
+ {
904
+ current_entry->emplace(distance_from_desired, std::forward<Key>(key), std::forward<Args>(args)...);
905
+ ++num_elements;
906
+ return { { current_entry }, true };
907
+ }
908
+ value_type to_insert(std::forward<Key>(key), std::forward<Args>(args)...);
909
+ swap(distance_from_desired, current_entry->distance_from_desired);
910
+ swap(to_insert, current_entry->value);
911
+ iterator result = { current_entry };
912
+ for (++distance_from_desired, ++current_entry;; ++current_entry)
913
+ {
914
+ if (current_entry->is_empty())
915
+ {
916
+ current_entry->emplace(distance_from_desired, std::move(to_insert));
917
+ ++num_elements;
918
+ return { result, true };
919
+ }
920
+ else if (current_entry->distance_from_desired < distance_from_desired)
921
+ {
922
+ swap(distance_from_desired, current_entry->distance_from_desired);
923
+ swap(to_insert, current_entry->value);
924
+ ++distance_from_desired;
925
+ }
926
+ else
927
+ {
928
+ ++distance_from_desired;
929
+ if (distance_from_desired == max_lookups)
930
+ {
931
+ swap(to_insert, result.current->value);
932
+ grow();
933
+ return emplace(std::move(to_insert));
934
+ }
935
+ }
936
+ }
937
+ }
938
+
939
+ void grow()
940
+ {
941
+ rehash(std::max(size_t(4), 2 * bucket_count()));
942
+ }
943
+
944
+ void deallocate_data(EntryPointer begin, size_t num_slots_minus_one, int8_t max_lookups)
945
+ {
946
+ AllocatorTraits::deallocate(*this, begin, num_slots_minus_one + max_lookups + 1);
947
+ }
948
+
949
+ void reset_to_empty_state()
950
+ {
951
+ deallocate_data(entries, num_slots_minus_one, max_lookups);
952
+ entries = empty_default_table();
953
+ num_slots_minus_one = 0;
954
+ hash_policy.reset();
955
+ max_lookups = detailv3::min_lookups - 1;
956
+ }
957
+
958
+ template<typename U>
959
+ size_t hash_object(const U & key)
960
+ {
961
+ return static_cast<Hasher &>(*this)(key);
962
+ }
963
+ template<typename U>
964
+ size_t hash_object(const U & key) const
965
+ {
966
+ return static_cast<const Hasher &>(*this)(key);
967
+ }
968
+ template<typename L, typename R>
969
+ bool compares_equal(const L & lhs, const R & rhs)
970
+ {
971
+ return static_cast<Equal &>(*this)(lhs, rhs);
972
+ }
973
+
974
+ struct convertible_to_iterator
975
+ {
976
+ EntryPointer it;
977
+
978
+ operator iterator()
979
+ {
980
+ if (it->has_value())
981
+ return { it };
982
+ else
983
+ return ++iterator{it};
984
+ }
985
+ operator const_iterator()
986
+ {
987
+ if (it->has_value())
988
+ return { it };
989
+ else
990
+ return ++const_iterator{it};
991
+ }
992
+ };
993
+
994
+ };
995
+ }
996
+
997
+ struct prime_number_hash_policy
998
+ {
999
+ static size_t mod0(size_t) { return 0u; }
1000
+ static size_t mod2(size_t hash) { return hash % 2u; }
1001
+ static size_t mod3(size_t hash) { return hash % 3u; }
1002
+ static size_t mod5(size_t hash) { return hash % 5u; }
1003
+ static size_t mod7(size_t hash) { return hash % 7u; }
1004
+ static size_t mod11(size_t hash) { return hash % 11u; }
1005
+ static size_t mod13(size_t hash) { return hash % 13u; }
1006
+ static size_t mod17(size_t hash) { return hash % 17u; }
1007
+ static size_t mod23(size_t hash) { return hash % 23u; }
1008
+ static size_t mod29(size_t hash) { return hash % 29u; }
1009
+ static size_t mod37(size_t hash) { return hash % 37u; }
1010
+ static size_t mod47(size_t hash) { return hash % 47u; }
1011
+ static size_t mod59(size_t hash) { return hash % 59u; }
1012
+ static size_t mod73(size_t hash) { return hash % 73u; }
1013
+ static size_t mod97(size_t hash) { return hash % 97u; }
1014
+ static size_t mod127(size_t hash) { return hash % 127u; }
1015
+ static size_t mod151(size_t hash) { return hash % 151u; }
1016
+ static size_t mod197(size_t hash) { return hash % 197u; }
1017
+ static size_t mod251(size_t hash) { return hash % 251u; }
1018
+ static size_t mod313(size_t hash) { return hash % 313u; }
1019
+ static size_t mod397(size_t hash) { return hash % 397u; }
1020
+ static size_t mod499(size_t hash) { return hash % 499u; }
1021
+ static size_t mod631(size_t hash) { return hash % 631u; }
1022
+ static size_t mod797(size_t hash) { return hash % 797u; }
1023
+ static size_t mod1009(size_t hash) { return hash % 1009u; }
1024
+ static size_t mod1259(size_t hash) { return hash % 1259u; }
1025
+ static size_t mod1597(size_t hash) { return hash % 1597u; }
1026
+ static size_t mod2011(size_t hash) { return hash % 2011u; }
1027
+ static size_t mod2539(size_t hash) { return hash % 2539u; }
1028
+ static size_t mod3203(size_t hash) { return hash % 3203u; }
1029
+ static size_t mod4027(size_t hash) { return hash % 4027u; }
1030
+ static size_t mod5087(size_t hash) { return hash % 5087u; }
1031
+ static size_t mod6421(size_t hash) { return hash % 6421u; }
1032
+ static size_t mod8089(size_t hash) { return hash % 8089u; }
1033
+ static size_t mod10193(size_t hash) { return hash % 10193u; }
1034
+ static size_t mod12853(size_t hash) { return hash % 12853u; }
1035
+ static size_t mod16193(size_t hash) { return hash % 16193u; }
1036
+ static size_t mod20399(size_t hash) { return hash % 20399u; }
1037
+ static size_t mod25717(size_t hash) { return hash % 25717u; }
1038
+ static size_t mod32401(size_t hash) { return hash % 32401u; }
1039
+ static size_t mod40823(size_t hash) { return hash % 40823u; }
1040
+ static size_t mod51437(size_t hash) { return hash % 51437u; }
1041
+ static size_t mod64811(size_t hash) { return hash % 64811u; }
1042
+ static size_t mod81649(size_t hash) { return hash % 81649u; }
1043
+ static size_t mod102877(size_t hash) { return hash % 102877u; }
1044
+ static size_t mod129607(size_t hash) { return hash % 129607u; }
1045
+ static size_t mod163307(size_t hash) { return hash % 163307u; }
1046
+ static size_t mod205759(size_t hash) { return hash % 205759u; }
1047
+ static size_t mod259229(size_t hash) { return hash % 259229u; }
1048
+ static size_t mod326617(size_t hash) { return hash % 326617u; }
1049
+ static size_t mod411527(size_t hash) { return hash % 411527u; }
1050
+ static size_t mod518509(size_t hash) { return hash % 518509u; }
1051
+ static size_t mod653267(size_t hash) { return hash % 653267u; }
1052
+ static size_t mod823117(size_t hash) { return hash % 823117u; }
1053
+ static size_t mod1037059(size_t hash) { return hash % 1037059u; }
1054
+ static size_t mod1306601(size_t hash) { return hash % 1306601u; }
1055
+ static size_t mod1646237(size_t hash) { return hash % 1646237u; }
1056
+ static size_t mod2074129(size_t hash) { return hash % 2074129u; }
1057
+ static size_t mod2613229(size_t hash) { return hash % 2613229u; }
1058
+ static size_t mod3292489(size_t hash) { return hash % 3292489u; }
1059
+ static size_t mod4148279(size_t hash) { return hash % 4148279u; }
1060
+ static size_t mod5226491(size_t hash) { return hash % 5226491u; }
1061
+ static size_t mod6584983(size_t hash) { return hash % 6584983u; }
1062
+ static size_t mod8296553(size_t hash) { return hash % 8296553u; }
1063
+ static size_t mod10453007(size_t hash) { return hash % 10453007u; }
1064
+ static size_t mod13169977(size_t hash) { return hash % 13169977u; }
1065
+ static size_t mod16593127(size_t hash) { return hash % 16593127u; }
1066
+ static size_t mod20906033(size_t hash) { return hash % 20906033u; }
1067
+ static size_t mod26339969(size_t hash) { return hash % 26339969u; }
1068
+ static size_t mod33186281(size_t hash) { return hash % 33186281u; }
1069
+ static size_t mod41812097(size_t hash) { return hash % 41812097u; }
1070
+ static size_t mod52679969(size_t hash) { return hash % 52679969u; }
1071
+ static size_t mod66372617(size_t hash) { return hash % 66372617u; }
1072
+ static size_t mod83624237(size_t hash) { return hash % 83624237u; }
1073
+ static size_t mod105359939(size_t hash) { return hash % 105359939u; }
1074
+ static size_t mod132745199(size_t hash) { return hash % 132745199u; }
1075
+ static size_t mod167248483(size_t hash) { return hash % 167248483u; }
1076
+ static size_t mod210719881(size_t hash) { return hash % 210719881u; }
1077
+ static size_t mod265490441(size_t hash) { return hash % 265490441u; }
1078
+ static size_t mod334496971(size_t hash) { return hash % 334496971u; }
1079
+ static size_t mod421439783(size_t hash) { return hash % 421439783u; }
1080
+ static size_t mod530980861(size_t hash) { return hash % 530980861u; }
1081
+ static size_t mod668993977(size_t hash) { return hash % 668993977u; }
1082
+ static size_t mod842879579(size_t hash) { return hash % 842879579u; }
1083
+ static size_t mod1061961721(size_t hash) { return hash % 1061961721u; }
1084
+ static size_t mod1337987929(size_t hash) { return hash % 1337987929u; }
1085
+ static size_t mod1685759167(size_t hash) { return hash % 1685759167u; }
1086
+ static size_t mod2123923447(size_t hash) { return hash % 2123923447u; }
1087
+ static size_t mod2675975881(size_t hash) { return hash % 2675975881u; }
1088
+ static size_t mod3371518343(size_t hash) { return hash % 3371518343u; }
1089
+ static size_t mod4247846927(size_t hash) { return hash % 4247846927u; }
1090
+
1091
+ #ifdef ENV64BIT
1092
+ static size_t mod5351951779(size_t hash) { return hash % 5351951779llu; }
1093
+ static size_t mod6743036717(size_t hash) { return hash % 6743036717llu; }
1094
+ static size_t mod8495693897(size_t hash) { return hash % 8495693897llu; }
1095
+ static size_t mod10703903591(size_t hash) { return hash % 10703903591llu; }
1096
+ static size_t mod13486073473(size_t hash) { return hash % 13486073473llu; }
1097
+ static size_t mod16991387857(size_t hash) { return hash % 16991387857llu; }
1098
+ static size_t mod21407807219(size_t hash) { return hash % 21407807219llu; }
1099
+ static size_t mod26972146961(size_t hash) { return hash % 26972146961llu; }
1100
+ static size_t mod33982775741(size_t hash) { return hash % 33982775741llu; }
1101
+ static size_t mod42815614441(size_t hash) { return hash % 42815614441llu; }
1102
+ static size_t mod53944293929(size_t hash) { return hash % 53944293929llu; }
1103
+ static size_t mod67965551447(size_t hash) { return hash % 67965551447llu; }
1104
+ static size_t mod85631228929(size_t hash) { return hash % 85631228929llu; }
1105
+ static size_t mod107888587883(size_t hash) { return hash % 107888587883llu; }
1106
+ static size_t mod135931102921(size_t hash) { return hash % 135931102921llu; }
1107
+ static size_t mod171262457903(size_t hash) { return hash % 171262457903llu; }
1108
+ static size_t mod215777175787(size_t hash) { return hash % 215777175787llu; }
1109
+ static size_t mod271862205833(size_t hash) { return hash % 271862205833llu; }
1110
+ static size_t mod342524915839(size_t hash) { return hash % 342524915839llu; }
1111
+ static size_t mod431554351609(size_t hash) { return hash % 431554351609llu; }
1112
+ static size_t mod543724411781(size_t hash) { return hash % 543724411781llu; }
1113
+ static size_t mod685049831731(size_t hash) { return hash % 685049831731llu; }
1114
+ static size_t mod863108703229(size_t hash) { return hash % 863108703229llu; }
1115
+ static size_t mod1087448823553(size_t hash) { return hash % 1087448823553llu; }
1116
+ static size_t mod1370099663459(size_t hash) { return hash % 1370099663459llu; }
1117
+ static size_t mod1726217406467(size_t hash) { return hash % 1726217406467llu; }
1118
+ static size_t mod2174897647073(size_t hash) { return hash % 2174897647073llu; }
1119
+ static size_t mod2740199326961(size_t hash) { return hash % 2740199326961llu; }
1120
+ static size_t mod3452434812973(size_t hash) { return hash % 3452434812973llu; }
1121
+ static size_t mod4349795294267(size_t hash) { return hash % 4349795294267llu; }
1122
+ static size_t mod5480398654009(size_t hash) { return hash % 5480398654009llu; }
1123
+ static size_t mod6904869625999(size_t hash) { return hash % 6904869625999llu; }
1124
+ static size_t mod8699590588571(size_t hash) { return hash % 8699590588571llu; }
1125
+ static size_t mod10960797308051(size_t hash) { return hash % 10960797308051llu; }
1126
+ static size_t mod13809739252051(size_t hash) { return hash % 13809739252051llu; }
1127
+ static size_t mod17399181177241(size_t hash) { return hash % 17399181177241llu; }
1128
+ static size_t mod21921594616111(size_t hash) { return hash % 21921594616111llu; }
1129
+ static size_t mod27619478504183(size_t hash) { return hash % 27619478504183llu; }
1130
+ static size_t mod34798362354533(size_t hash) { return hash % 34798362354533llu; }
1131
+ static size_t mod43843189232363(size_t hash) { return hash % 43843189232363llu; }
1132
+ static size_t mod55238957008387(size_t hash) { return hash % 55238957008387llu; }
1133
+ static size_t mod69596724709081(size_t hash) { return hash % 69596724709081llu; }
1134
+ static size_t mod87686378464759(size_t hash) { return hash % 87686378464759llu; }
1135
+ static size_t mod110477914016779(size_t hash) { return hash % 110477914016779llu; }
1136
+ static size_t mod139193449418173(size_t hash) { return hash % 139193449418173llu; }
1137
+ static size_t mod175372756929481(size_t hash) { return hash % 175372756929481llu; }
1138
+ static size_t mod220955828033581(size_t hash) { return hash % 220955828033581llu; }
1139
+ static size_t mod278386898836457(size_t hash) { return hash % 278386898836457llu; }
1140
+ static size_t mod350745513859007(size_t hash) { return hash % 350745513859007llu; }
1141
+ static size_t mod441911656067171(size_t hash) { return hash % 441911656067171llu; }
1142
+ static size_t mod556773797672909(size_t hash) { return hash % 556773797672909llu; }
1143
+ static size_t mod701491027718027(size_t hash) { return hash % 701491027718027llu; }
1144
+ static size_t mod883823312134381(size_t hash) { return hash % 883823312134381llu; }
1145
+ static size_t mod1113547595345903(size_t hash) { return hash % 1113547595345903llu; }
1146
+ static size_t mod1402982055436147(size_t hash) { return hash % 1402982055436147llu; }
1147
+ static size_t mod1767646624268779(size_t hash) { return hash % 1767646624268779llu; }
1148
+ static size_t mod2227095190691797(size_t hash) { return hash % 2227095190691797llu; }
1149
+ static size_t mod2805964110872297(size_t hash) { return hash % 2805964110872297llu; }
1150
+ static size_t mod3535293248537579(size_t hash) { return hash % 3535293248537579llu; }
1151
+ static size_t mod4454190381383713(size_t hash) { return hash % 4454190381383713llu; }
1152
+ static size_t mod5611928221744609(size_t hash) { return hash % 5611928221744609llu; }
1153
+ static size_t mod7070586497075177(size_t hash) { return hash % 7070586497075177llu; }
1154
+ static size_t mod8908380762767489(size_t hash) { return hash % 8908380762767489llu; }
1155
+ static size_t mod11223856443489329(size_t hash) { return hash % 11223856443489329llu; }
1156
+ static size_t mod14141172994150357(size_t hash) { return hash % 14141172994150357llu; }
1157
+ static size_t mod17816761525534927(size_t hash) { return hash % 17816761525534927llu; }
1158
+ static size_t mod22447712886978529(size_t hash) { return hash % 22447712886978529llu; }
1159
+ static size_t mod28282345988300791(size_t hash) { return hash % 28282345988300791llu; }
1160
+ static size_t mod35633523051069991(size_t hash) { return hash % 35633523051069991llu; }
1161
+ static size_t mod44895425773957261(size_t hash) { return hash % 44895425773957261llu; }
1162
+ static size_t mod56564691976601587(size_t hash) { return hash % 56564691976601587llu; }
1163
+ static size_t mod71267046102139967(size_t hash) { return hash % 71267046102139967llu; }
1164
+ static size_t mod89790851547914507(size_t hash) { return hash % 89790851547914507llu; }
1165
+ static size_t mod113129383953203213(size_t hash) { return hash % 113129383953203213llu; }
1166
+ static size_t mod142534092204280003(size_t hash) { return hash % 142534092204280003llu; }
1167
+ static size_t mod179581703095829107(size_t hash) { return hash % 179581703095829107llu; }
1168
+ static size_t mod226258767906406483(size_t hash) { return hash % 226258767906406483llu; }
1169
+ static size_t mod285068184408560057(size_t hash) { return hash % 285068184408560057llu; }
1170
+ static size_t mod359163406191658253(size_t hash) { return hash % 359163406191658253llu; }
1171
+ static size_t mod452517535812813007(size_t hash) { return hash % 452517535812813007llu; }
1172
+ static size_t mod570136368817120201(size_t hash) { return hash % 570136368817120201llu; }
1173
+ static size_t mod718326812383316683(size_t hash) { return hash % 718326812383316683llu; }
1174
+ static size_t mod905035071625626043(size_t hash) { return hash % 905035071625626043llu; }
1175
+ static size_t mod1140272737634240411(size_t hash) { return hash % 1140272737634240411llu; }
1176
+ static size_t mod1436653624766633509(size_t hash) { return hash % 1436653624766633509llu; }
1177
+ static size_t mod1810070143251252131(size_t hash) { return hash % 1810070143251252131llu; }
1178
+ static size_t mod2280545475268481167(size_t hash) { return hash % 2280545475268481167llu; }
1179
+ static size_t mod2873307249533267101(size_t hash) { return hash % 2873307249533267101llu; }
1180
+ static size_t mod3620140286502504283(size_t hash) { return hash % 3620140286502504283llu; }
1181
+ static size_t mod4561090950536962147(size_t hash) { return hash % 4561090950536962147llu; }
1182
+ static size_t mod5746614499066534157(size_t hash) { return hash % 5746614499066534157llu; }
1183
+ static size_t mod7240280573005008577(size_t hash) { return hash % 7240280573005008577llu; }
1184
+ static size_t mod9122181901073924329(size_t hash) { return hash % 9122181901073924329llu; }
1185
+ static size_t mod11493228998133068689(size_t hash) { return hash % 11493228998133068689llu; }
1186
+ static size_t mod14480561146010017169(size_t hash) { return hash % 14480561146010017169llu; }
1187
+ static size_t mod18446744073709551557(size_t hash) { return hash % 18446744073709551557llu; }
1188
+ #endif
1189
+
1190
+ using mod_function = size_t (*)(size_t);
1191
+
1192
+ mod_function next_size_over(size_t & size) const
1193
+ {
1194
+ // prime numbers generated by the following method:
1195
+ // 1. start with a prime p = 2
1196
+ // 2. go to wolfram alpha and get p = NextPrime(2 * p)
1197
+ // 3. repeat 2. until you overflow 64 bits
1198
+ // you now have large gaps which you would hit if somebody called reserve() with an unlucky number.
1199
+ // 4. to fill the gaps for every prime p go to wolfram alpha and get ClosestPrime(p * 2^(1/3)) and ClosestPrime(p * 2^(2/3)) and put those in the gaps
1200
+ // 5. get PrevPrime(2^64) and put it at the end
1201
+ static constexpr const size_t prime_list[] =
1202
+ {
1203
+ 2u, 3u, 5u, 7u, 11u, 13u, 17u, 23u, 29u, 37u, 47u,
1204
+ 59u, 73u, 97u, 127u, 151u, 197u, 251u, 313u, 397u,
1205
+ 499u, 631u, 797u, 1009u, 1259u, 1597u, 2011u, 2539u,
1206
+ 3203u, 4027u, 5087u, 6421u, 8089u, 10193u, 12853u, 16193u,
1207
+ 20399u, 25717u, 32401u, 40823u, 51437u, 64811u, 81649u,
1208
+ 102877u, 129607u, 163307u, 205759u, 259229u, 326617u,
1209
+ 411527u, 518509u, 653267u, 823117u, 1037059u, 1306601u,
1210
+ 1646237u, 2074129u, 2613229u, 3292489u, 4148279u, 5226491u,
1211
+ 6584983u, 8296553u, 10453007u, 13169977u, 16593127u, 20906033u,
1212
+ 26339969u, 33186281u, 41812097u, 52679969u, 66372617u,
1213
+ 83624237u, 105359939u, 132745199u, 167248483u, 210719881u,
1214
+ 265490441u, 334496971u, 421439783u, 530980861u, 668993977u,
1215
+ 842879579u, 1061961721u, 1337987929u, 1685759167u, 2123923447u,
1216
+ 2675975881u, 3371518343u, 4247846927u
1217
+ #ifdef ENV64BIT
1218
+ , 5351951779llu, 6743036717llu,
1219
+ 8495693897llu, 10703903591llu, 13486073473llu, 16991387857llu,
1220
+ 21407807219llu, 26972146961llu, 33982775741llu, 42815614441llu,
1221
+ 53944293929llu, 67965551447llu, 85631228929llu, 107888587883llu,
1222
+ 135931102921llu, 171262457903llu, 215777175787llu, 271862205833llu,
1223
+ 342524915839llu, 431554351609llu, 543724411781llu, 685049831731llu,
1224
+ 863108703229llu, 1087448823553llu, 1370099663459llu, 1726217406467llu,
1225
+ 2174897647073llu, 2740199326961llu, 3452434812973llu, 4349795294267llu,
1226
+ 5480398654009llu, 6904869625999llu, 8699590588571llu, 10960797308051llu,
1227
+ 13809739252051llu, 17399181177241llu, 21921594616111llu, 27619478504183llu,
1228
+ 34798362354533llu, 43843189232363llu, 55238957008387llu, 69596724709081llu,
1229
+ 87686378464759llu, 110477914016779llu, 139193449418173llu,
1230
+ 175372756929481llu, 220955828033581llu, 278386898836457llu,
1231
+ 350745513859007llu, 441911656067171llu, 556773797672909llu,
1232
+ 701491027718027llu, 883823312134381llu, 1113547595345903llu,
1233
+ 1402982055436147llu, 1767646624268779llu, 2227095190691797llu,
1234
+ 2805964110872297llu, 3535293248537579llu, 4454190381383713llu,
1235
+ 5611928221744609llu, 7070586497075177llu, 8908380762767489llu,
1236
+ 11223856443489329llu, 14141172994150357llu, 17816761525534927llu,
1237
+ 22447712886978529llu, 28282345988300791llu, 35633523051069991llu,
1238
+ 44895425773957261llu, 56564691976601587llu, 71267046102139967llu,
1239
+ 89790851547914507llu, 113129383953203213llu, 142534092204280003llu,
1240
+ 179581703095829107llu, 226258767906406483llu, 285068184408560057llu,
1241
+ 359163406191658253llu, 452517535812813007llu, 570136368817120201llu,
1242
+ 718326812383316683llu, 905035071625626043llu, 1140272737634240411llu,
1243
+ 1436653624766633509llu, 1810070143251252131llu, 2280545475268481167llu,
1244
+ 2873307249533267101llu, 3620140286502504283llu, 4561090950536962147llu,
1245
+ 5746614499066534157llu, 7240280573005008577llu, 9122181901073924329llu,
1246
+ 11493228998133068689llu, 14480561146010017169llu, 18446744073709551557llu
1247
+ #endif
1248
+ };
1249
+ static constexpr size_t (* const mod_functions[])(size_t) =
1250
+ {
1251
+ &mod0, &mod2, &mod3, &mod5, &mod7, &mod11, &mod13, &mod17, &mod23, &mod29, &mod37,
1252
+ &mod47, &mod59, &mod73, &mod97, &mod127, &mod151, &mod197, &mod251, &mod313, &mod397,
1253
+ &mod499, &mod631, &mod797, &mod1009, &mod1259, &mod1597, &mod2011, &mod2539, &mod3203,
1254
+ &mod4027, &mod5087, &mod6421, &mod8089, &mod10193, &mod12853, &mod16193, &mod20399,
1255
+ &mod25717, &mod32401, &mod40823, &mod51437, &mod64811, &mod81649, &mod102877,
1256
+ &mod129607, &mod163307, &mod205759, &mod259229, &mod326617, &mod411527, &mod518509,
1257
+ &mod653267, &mod823117, &mod1037059, &mod1306601, &mod1646237, &mod2074129,
1258
+ &mod2613229, &mod3292489, &mod4148279, &mod5226491, &mod6584983, &mod8296553,
1259
+ &mod10453007, &mod13169977, &mod16593127, &mod20906033, &mod26339969, &mod33186281,
1260
+ &mod41812097, &mod52679969, &mod66372617, &mod83624237, &mod105359939, &mod132745199,
1261
+ &mod167248483, &mod210719881, &mod265490441, &mod334496971, &mod421439783,
1262
+ &mod530980861, &mod668993977, &mod842879579, &mod1061961721, &mod1337987929,
1263
+ &mod1685759167, &mod2123923447, &mod2675975881, &mod3371518343, &mod4247846927
1264
+
1265
+ #ifdef ENV64BIT
1266
+ , &mod5351951779, &mod6743036717, &mod8495693897, &mod10703903591, &mod13486073473,
1267
+ &mod16991387857, &mod21407807219, &mod26972146961, &mod33982775741, &mod42815614441,
1268
+ &mod53944293929, &mod67965551447, &mod85631228929, &mod107888587883, &mod135931102921,
1269
+ &mod171262457903, &mod215777175787, &mod271862205833, &mod342524915839,
1270
+ &mod431554351609, &mod543724411781, &mod685049831731, &mod863108703229,
1271
+ &mod1087448823553, &mod1370099663459, &mod1726217406467, &mod2174897647073,
1272
+ &mod2740199326961, &mod3452434812973, &mod4349795294267, &mod5480398654009,
1273
+ &mod6904869625999, &mod8699590588571, &mod10960797308051, &mod13809739252051,
1274
+ &mod17399181177241, &mod21921594616111, &mod27619478504183, &mod34798362354533,
1275
+ &mod43843189232363, &mod55238957008387, &mod69596724709081, &mod87686378464759,
1276
+ &mod110477914016779, &mod139193449418173, &mod175372756929481, &mod220955828033581,
1277
+ &mod278386898836457, &mod350745513859007, &mod441911656067171, &mod556773797672909,
1278
+ &mod701491027718027, &mod883823312134381, &mod1113547595345903, &mod1402982055436147,
1279
+ &mod1767646624268779, &mod2227095190691797, &mod2805964110872297, &mod3535293248537579,
1280
+ &mod4454190381383713, &mod5611928221744609, &mod7070586497075177, &mod8908380762767489,
1281
+ &mod11223856443489329, &mod14141172994150357, &mod17816761525534927,
1282
+ &mod22447712886978529, &mod28282345988300791, &mod35633523051069991,
1283
+ &mod44895425773957261, &mod56564691976601587, &mod71267046102139967,
1284
+ &mod89790851547914507, &mod113129383953203213, &mod142534092204280003,
1285
+ &mod179581703095829107, &mod226258767906406483, &mod285068184408560057,
1286
+ &mod359163406191658253, &mod452517535812813007, &mod570136368817120201,
1287
+ &mod718326812383316683, &mod905035071625626043, &mod1140272737634240411,
1288
+ &mod1436653624766633509, &mod1810070143251252131, &mod2280545475268481167,
1289
+ &mod2873307249533267101, &mod3620140286502504283, &mod4561090950536962147,
1290
+ &mod5746614499066534157, &mod7240280573005008577, &mod9122181901073924329,
1291
+ &mod11493228998133068689, &mod14480561146010017169, &mod18446744073709551557
1292
+ #endif
1293
+ };
1294
+ const size_t * found = std::lower_bound(std::begin(prime_list), std::end(prime_list) - 1, size);
1295
+ size = *found;
1296
+ return mod_functions[1 + found - prime_list];
1297
+ }
1298
+ void commit(mod_function new_mod_function)
1299
+ {
1300
+ current_mod_function = new_mod_function;
1301
+ }
1302
+ void reset()
1303
+ {
1304
+ current_mod_function = &mod0;
1305
+ }
1306
+
1307
+ size_t index_for_hash(size_t hash, size_t /*num_slots_minus_one*/) const
1308
+ {
1309
+ return current_mod_function(hash);
1310
+ }
1311
+ size_t keep_in_range(size_t index, size_t num_slots_minus_one) const
1312
+ {
1313
+ return index > num_slots_minus_one ? current_mod_function(index) : index;
1314
+ }
1315
+
1316
+ private:
1317
+ mod_function current_mod_function = &mod0;
1318
+ };
1319
+
1320
+ struct power_of_two_hash_policy
1321
+ {
1322
+ size_t index_for_hash(size_t hash, size_t num_slots_minus_one) const
1323
+ {
1324
+ return hash & num_slots_minus_one;
1325
+ }
1326
+ size_t keep_in_range(size_t index, size_t num_slots_minus_one) const
1327
+ {
1328
+ return index_for_hash(index, num_slots_minus_one);
1329
+ }
1330
+ int8_t next_size_over(size_t & size) const
1331
+ {
1332
+ size = detailv3::next_power_of_two(size);
1333
+ return 0;
1334
+ }
1335
+ void commit(int8_t)
1336
+ {
1337
+ }
1338
+ void reset()
1339
+ {
1340
+ }
1341
+
1342
+ };
1343
+
1344
+ struct fibonacci_hash_policy
1345
+ {
1346
+ size_t index_for_hash(size_t hash, size_t /*num_slots_minus_one*/) const
1347
+ {
1348
+ return (11400714819323198485ull * hash) >> shift;
1349
+ }
1350
+ size_t keep_in_range(size_t index, size_t num_slots_minus_one) const
1351
+ {
1352
+ return index & num_slots_minus_one;
1353
+ }
1354
+
1355
+ int8_t next_size_over(size_t & size) const
1356
+ {
1357
+ size = std::max(size_t(2), detailv3::next_power_of_two(size));
1358
+ return 64 - detailv3::log2(size);
1359
+ }
1360
+ void commit(int8_t shift)
1361
+ {
1362
+ this->shift = shift;
1363
+ }
1364
+ void reset()
1365
+ {
1366
+ shift = 63;
1367
+ }
1368
+
1369
+ private:
1370
+ int8_t shift = 63;
1371
+ };
1372
+
1373
+ template<typename K, typename V, typename H = std::hash<K>, typename E = std::equal_to<K>, typename A = std::allocator<std::pair<K, V> > >
1374
+ class flat_hash_map
1375
+ : public detailv3::sherwood_v3_table
1376
+ <
1377
+ std::pair<K, V>,
1378
+ K,
1379
+ H,
1380
+ detailv3::KeyOrValueHasher<K, std::pair<K, V>, H>,
1381
+ E,
1382
+ detailv3::KeyOrValueEquality<K, std::pair<K, V>, E>,
1383
+ A,
1384
+ typename std::allocator_traits<A>::template rebind_alloc<detailv3::sherwood_v3_entry<std::pair<K, V>>>
1385
+ >
1386
+ {
1387
+ using Table = detailv3::sherwood_v3_table
1388
+ <
1389
+ std::pair<K, V>,
1390
+ K,
1391
+ H,
1392
+ detailv3::KeyOrValueHasher<K, std::pair<K, V>, H>,
1393
+ E,
1394
+ detailv3::KeyOrValueEquality<K, std::pair<K, V>, E>,
1395
+ A,
1396
+ typename std::allocator_traits<A>::template rebind_alloc<detailv3::sherwood_v3_entry<std::pair<K, V>>>
1397
+ >;
1398
+ public:
1399
+
1400
+ using key_type = K;
1401
+ using mapped_type = V;
1402
+
1403
+ using Table::Table;
1404
+ flat_hash_map()
1405
+ {
1406
+ }
1407
+
1408
+ inline V & operator[](const K & key)
1409
+ {
1410
+ return emplace(key, convertible_to_value()).first->second;
1411
+ }
1412
+ inline V & operator[](K && key)
1413
+ {
1414
+ return emplace(std::move(key), convertible_to_value()).first->second;
1415
+ }
1416
+ V & at(const K & key)
1417
+ {
1418
+ auto found = this->find(key);
1419
+ if (found == this->end())
1420
+ throw std::out_of_range("Argument passed to at() was not in the map.");
1421
+ return found->second;
1422
+ }
1423
+ const V & at(const K & key) const
1424
+ {
1425
+ auto found = this->find(key);
1426
+ if (found == this->end())
1427
+ throw std::out_of_range("Argument passed to at() was not in the map.");
1428
+ return found->second;
1429
+ }
1430
+
1431
+ using Table::emplace;
1432
+ std::pair<typename Table::iterator, bool> emplace()
1433
+ {
1434
+ return emplace(key_type(), convertible_to_value());
1435
+ }
1436
+ template<typename M>
1437
+ std::pair<typename Table::iterator, bool> insert_or_assign(const key_type & key, M && m)
1438
+ {
1439
+ auto emplace_result = emplace(key, std::forward<M>(m));
1440
+ if (!emplace_result.second)
1441
+ emplace_result.first->second = std::forward<M>(m);
1442
+ return emplace_result;
1443
+ }
1444
+ template<typename M>
1445
+ std::pair<typename Table::iterator, bool> insert_or_assign(key_type && key, M && m)
1446
+ {
1447
+ auto emplace_result = emplace(std::move(key), std::forward<M>(m));
1448
+ if (!emplace_result.second)
1449
+ emplace_result.first->second = std::forward<M>(m);
1450
+ return emplace_result;
1451
+ }
1452
+ template<typename M>
1453
+ typename Table::iterator insert_or_assign(typename Table::const_iterator, const key_type & key, M && m)
1454
+ {
1455
+ return insert_or_assign(key, std::forward<M>(m)).first;
1456
+ }
1457
+ template<typename M>
1458
+ typename Table::iterator insert_or_assign(typename Table::const_iterator, key_type && key, M && m)
1459
+ {
1460
+ return insert_or_assign(std::move(key), std::forward<M>(m)).first;
1461
+ }
1462
+
1463
+ friend bool operator==(const flat_hash_map & lhs, const flat_hash_map & rhs)
1464
+ {
1465
+ if (lhs.size() != rhs.size())
1466
+ return false;
1467
+ for (const typename Table::value_type & value : lhs)
1468
+ {
1469
+ auto found = rhs.find(value.first);
1470
+ if (found == rhs.end())
1471
+ return false;
1472
+ else if (value.second != found->second)
1473
+ return false;
1474
+ }
1475
+ return true;
1476
+ }
1477
+ friend bool operator!=(const flat_hash_map & lhs, const flat_hash_map & rhs)
1478
+ {
1479
+ return !(lhs == rhs);
1480
+ }
1481
+
1482
+ private:
1483
+ struct convertible_to_value
1484
+ {
1485
+ operator V() const
1486
+ {
1487
+ return V();
1488
+ }
1489
+ };
1490
+ };
1491
+
1492
+ template<typename T, typename H = std::hash<T>, typename E = std::equal_to<T>, typename A = std::allocator<T> >
1493
+ class flat_hash_set
1494
+ : public detailv3::sherwood_v3_table
1495
+ <
1496
+ T,
1497
+ T,
1498
+ H,
1499
+ detailv3::functor_storage<size_t, H>,
1500
+ E,
1501
+ detailv3::functor_storage<bool, E>,
1502
+ A,
1503
+ typename std::allocator_traits<A>::template rebind_alloc<detailv3::sherwood_v3_entry<T>>
1504
+ >
1505
+ {
1506
+ using Table = detailv3::sherwood_v3_table
1507
+ <
1508
+ T,
1509
+ T,
1510
+ H,
1511
+ detailv3::functor_storage<size_t, H>,
1512
+ E,
1513
+ detailv3::functor_storage<bool, E>,
1514
+ A,
1515
+ typename std::allocator_traits<A>::template rebind_alloc<detailv3::sherwood_v3_entry<T>>
1516
+ >;
1517
+ public:
1518
+
1519
+ using key_type = T;
1520
+
1521
+ using Table::Table;
1522
+ flat_hash_set()
1523
+ {
1524
+ }
1525
+
1526
+ template<typename... Args>
1527
+ std::pair<typename Table::iterator, bool> emplace(Args &&... args)
1528
+ {
1529
+ return Table::emplace(T(std::forward<Args>(args)...));
1530
+ }
1531
+ std::pair<typename Table::iterator, bool> emplace(const key_type & arg)
1532
+ {
1533
+ return Table::emplace(arg);
1534
+ }
1535
+ std::pair<typename Table::iterator, bool> emplace(key_type & arg)
1536
+ {
1537
+ return Table::emplace(arg);
1538
+ }
1539
+ std::pair<typename Table::iterator, bool> emplace(const key_type && arg)
1540
+ {
1541
+ return Table::emplace(std::move(arg));
1542
+ }
1543
+ std::pair<typename Table::iterator, bool> emplace(key_type && arg)
1544
+ {
1545
+ return Table::emplace(std::move(arg));
1546
+ }
1547
+
1548
+ friend bool operator==(const flat_hash_set & lhs, const flat_hash_set & rhs)
1549
+ {
1550
+ if (lhs.size() != rhs.size())
1551
+ return false;
1552
+ for (const T & value : lhs)
1553
+ {
1554
+ if (rhs.find(value) == rhs.end())
1555
+ return false;
1556
+ }
1557
+ return true;
1558
+ }
1559
+ friend bool operator!=(const flat_hash_set & lhs, const flat_hash_set & rhs)
1560
+ {
1561
+ return !(lhs == rhs);
1562
+ }
1563
+ };
1564
+
1565
+
1566
+ template<typename T>
1567
+ struct power_of_two_std_hash : std::hash<T>
1568
+ {
1569
+ typedef ska::power_of_two_hash_policy hash_policy;
1570
+ };
1571
+
1572
+ } // end namespace ska