-
Notifications
You must be signed in to change notification settings - Fork 36
/
hash_table6.hpp
1836 lines (1606 loc) · 57.2 KB
/
hash_table6.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// emhash6::HashMap for C++11/14/17
// version 1.7.2
// https://github.com/ktprime/emhash/blob/master/hash_table6.hpp
//
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
// SPDX-License-Identifier: MIT
// Copyright (c) 2019-2024 Huang Yuanbing & bailuzhou AT 163.com
//
// 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 <cstring>
#include <string>
#include <cmath>
#include <cstdlib>
#include <type_traits>
#include <cassert>
#include <utility>
#include <cstdint>
#include <functional>
#include <iterator>
#include <algorithm>
#if EMH_WY_HASH
#include "wyhash.h"
#endif
#ifdef EMH_KEY
#undef EMH_KEY
#undef EMH_VAL
#undef EMH_PKV
#undef EMH_NEW
#undef EMH_SET
#undef EMH_BUCKET
#undef EMH_EMPTY
#undef EMH_MASK
#endif
// likely/unlikely
#if (__GNUC__ >= 4 || __clang__)
# define EMH_LIKELY(condition) __builtin_expect(condition, 1)
# define EMH_UNLIKELY(condition) __builtin_expect(condition, 0)
#else
# define EMH_LIKELY(condition) condition
# define EMH_UNLIKELY(condition) condition
#endif
#ifndef EMH_BUCKET_INDEX
#define EMH_BUCKET_INDEX 1
#endif
#if EMH_BUCKET_INDEX == 0
#define EMH_KEY(p,n) p[n].second.first
#define EMH_VAL(p,n) p[n].second.second
#define EMH_BUCKET(p,n) p[n].first / 2
#define EMH_ADDR(p,n) p[n].first
#define EMH_EMPTY(p,n) ((int)p[n].first < 0)
#define EMH_PKV(p,n) p[n].second
#define EMH_NEW(key, val, bucket, next) new(_pairs + bucket) PairT(next, value_type(key, val)), _num_filled ++; EMH_SET(bucket)
#elif EMH_BUCKET_INDEX == 2
#define EMH_KEY(p,n) p[n].first.first
#define EMH_VAL(p,n) p[n].first.second
#define EMH_BUCKET(p,n) p[n].second / 2
#define EMH_ADDR(p,n) p[n].second
#define EMH_EMPTY(p,n) ((int)p[n].second < 0)
#define EMH_PKV(p,n) p[n].first
#define EMH_NEW(key, val, bucket, next) new(_pairs + bucket) PairT(value_type(key, val), next), _num_filled ++; EMH_SET(bucket)
#else
#define EMH_KEY(p,n) p[n].first
#define EMH_VAL(p,n) p[n].second
#define EMH_BUCKET(p,n) p[n].bucket / 2
#define EMH_ADDR(p,n) p[n].bucket
#define EMH_EMPTY(p,n) (0 > (int)p[n].bucket)
#define EMH_PKV(p,n) p[n]
#define EMH_NEW(key, val, bucket, next) new(_pairs + bucket) PairT(key, val, next), _num_filled ++; EMH_SET(bucket)
#endif
#define EMH_MASK(n) uint8_t(1 << (n % MASK_BIT))
#define EMH_SET(n) _bitmask[n / MASK_BIT] &= ~(EMH_MASK(n))
#define EMH_CLS(n) _bitmask[n / MASK_BIT] |= EMH_MASK(n)
#if _WIN32
#include <intrin.h>
#if _WIN64
#pragma intrinsic(_umul128)
#endif
#endif
namespace emhash6 {
#ifdef EMH_SIZE_TYPE_16BIT
typedef uint16_t size_type;
static constexpr size_type INACTIVE = 0xFFFF;
#elif EMH_SIZE_TYPE_64BIT
typedef uint64_t size_type;
static constexpr size_type INACTIVE = 0 - 0x1ull;
#else
typedef uint32_t size_type;
static constexpr size_type INACTIVE = 0 - 0x1u;
#endif
static_assert((int)INACTIVE < 0, "INACTIVE must be even and < 0(to int)");
//https://gist.github.com/jtbr/1896790eb6ad50506d5f042991906c30
static inline size_type CTZ(size_t n)
{
#if defined(__x86_64__) || defined(_WIN32) || (__BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
#elif __BIG_ENDIAN__ || (__BYTE_ORDER__ && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
n = __builtin_bswap64(n);
#else
static uint32_t endianness = 0x12345678;
const auto is_big = *(const char *)&endianness == 0x12;
if (is_big)
n = __builtin_bswap64(n);
#endif
#if _WIN32
unsigned long index;
#if defined(_WIN64)
_BitScanForward64(&index, n);
#else
_BitScanForward(&index, n);
#endif
#elif defined (__LP64__) || (SIZE_MAX == UINT64_MAX) || defined (__x86_64__)
auto index = __builtin_ctzll(n);
#elif 1
auto index = __builtin_ctzl(n);
#else
#if defined (__LP64__) || (SIZE_MAX == UINT64_MAX) || defined (__x86_64__)
size_type index;
__asm__("bsfq %1, %0\n" : "=r" (index) : "rm" (n) : "cc");
#else
size_type index;
__asm__("bsf %1, %0\n" : "=r" (index) : "rm" (n) : "cc");
#endif
#endif
return (size_type)index;
}
template <typename First, typename Second>
struct entry {
using first_type = First;
using second_type = Second;
entry(const First& key, const Second& val, size_type ibucket) :second(val), first(key) { bucket = ibucket; }
entry(First&& key, Second&& val, size_type ibucket) :second(std::move(val)), first(std::move(key)) { bucket = ibucket; }
entry(const std::pair<First, Second>& pair) :second(pair.second), first(pair.first) { bucket = INACTIVE; }
entry(std::pair<First, Second>&& pair) :second(std::move(pair.second)), first(std::move(pair.first)) { bucket = INACTIVE; }
entry(const entry& pairT) :second(pairT.second), first(pairT.first) { bucket = pairT.bucket; }
entry(entry&& pairT) noexcept :second(std::move(pairT.second)), first(std::move(pairT.first)) { bucket = pairT.bucket; }
template<typename K, typename V>
entry(K&& key, V&& val, size_type ibucket)
:second(std::forward<V>(val)), first(std::forward<K>(key))
{
bucket = ibucket;
}
entry& operator = (entry&& pairT) noexcept
{
second = std::move(pairT.second);
bucket = pairT.bucket;
first = std::move(pairT.first);
return *this;
}
entry& operator = (const entry& o)
{
second = o.second;
bucket = o.bucket;
first = o.first;
return *this;
}
bool operator == (const std::pair<First, Second>& p) const
{
return first == p.first && second == p.second;
}
bool operator == (const entry<First, Second>& p) const
{
return first == p.first && second == p.second;
}
void swap(entry<First, Second>& o)
{
std::swap(second, o.second);
std::swap(first, o.first);
}
#if EMH_ORDER_KV || EMH_SIZE_TYPE_64BIT
First first;
size_type bucket;
Second second;
#else
Second second;
size_type bucket;
First first;
#endif
};
/// A cache-friendly hash table with open addressing, linear/qua probing and power-of-two capacity
template <typename KeyT, typename ValueT, typename HashT = std::hash<KeyT>, typename EqT = std::equal_to<KeyT>>
class HashMap
{
#ifndef EMH_DEFAULT_LOAD_FACTOR
constexpr static float EMH_DEFAULT_LOAD_FACTOR = 0.80f;
#endif
constexpr static float EMH_MIN_LOAD_FACTOR = 0.25f;
public:
typedef HashMap<KeyT, ValueT, HashT, EqT> htype;
typedef std::pair<KeyT, ValueT> value_type;
#if EMH_BUCKET_INDEX == 0
typedef value_type value_pair;
typedef std::pair<size_type, value_type> PairT;
#elif EMH_BUCKET_INDEX == 2
typedef value_type value_pair;
typedef std::pair<value_type, size_type> PairT;
#else
typedef entry<KeyT, ValueT> value_pair;
typedef entry<KeyT, ValueT> PairT;
#endif
typedef KeyT key_type;
typedef ValueT val_type;
typedef ValueT mapped_type;
typedef HashT hasher;
typedef EqT key_equal;
typedef PairT& reference;
typedef const PairT& const_reference;
class const_iterator;
class iterator
{
public:
typedef std::forward_iterator_tag iterator_category;
typedef std::ptrdiff_t difference_type;
typedef value_pair value_type;
typedef value_pair* pointer;
typedef value_pair& reference;
iterator() = default;
iterator(const const_iterator& it) : _map(it._map), _bucket(it._bucket), _from(it._from), _bmask(it._bmask) { }
iterator(const htype* hash_map, size_type bucket, bool) : _map(hash_map), _bucket(bucket) { init(); }
#if EMH_ITER_SAFE
iterator(const htype* hash_map, size_type bucket) : _map(hash_map), _bucket(bucket) { init(); }
#else
iterator(const htype* hash_map, size_type bucket) : _map(hash_map), _bucket(bucket) { _from = (size_type)-1; }
#endif
void init()
{
_from = (_bucket / SIZE_BIT) * SIZE_BIT;
if (_bucket < _map->bucket_count()) {
_bmask = *(size_t*)((size_t*)_map->_bitmask + _from / SIZE_BIT);
_bmask |= (1ull << _bucket % SIZE_BIT) - 1;
_bmask = ~_bmask;
} else {
_bmask = 0;
}
}
size_type bucket() const
{
return _bucket;
}
void clear(size_type bucket)
{
if (_bucket / SIZE_BIT == bucket / SIZE_BIT)
_bmask &= ~(1ull << (bucket % SIZE_BIT));
}
iterator& next()
{
goto_next_element();
return *this;
}
iterator& operator++()
{
#ifndef EMH_ITER_SAFE
if (_from == (size_type)-1) init();
#endif
_bmask &= _bmask - 1;
goto_next_element();
return *this;
}
iterator operator++(int)
{
#ifndef EMH_ITER_SAFE
if (_from == (size_type)-1) init();
#endif
iterator old = *this;
_bmask &= _bmask - 1;
goto_next_element();
return old;
}
reference operator*() const
{
return _map->EMH_PKV(_pairs, _bucket);
}
pointer operator->() const
{
return &(_map->EMH_PKV(_pairs, _bucket));
}
bool operator==(const iterator& rhs) const { return _bucket == rhs._bucket; }
bool operator!=(const iterator& rhs) const { return _bucket != rhs._bucket; }
bool operator==(const const_iterator& rhs) const { return _bucket == rhs._bucket; }
bool operator!=(const const_iterator& rhs) const { return _bucket != rhs._bucket; }
private:
void goto_next_element()
{
if (_bmask != 0) {
_bucket = _from + CTZ(_bmask);
return;
}
do {
_bmask = ~*(size_t*)((size_t*)_map->_bitmask + (_from += SIZE_BIT) / SIZE_BIT);
} while (_bmask == 0);
_bucket = _from + CTZ(_bmask);
}
public:
const htype* _map;
size_type _bucket;
size_type _from;
size_t _bmask;
};
class const_iterator
{
public:
typedef std::forward_iterator_tag iterator_category;
typedef std::ptrdiff_t difference_type;
typedef value_pair value_type;
typedef const value_pair* pointer;
typedef const value_pair& reference;
const_iterator(const iterator& it) : _map(it._map), _bucket(it._bucket), _from(it._from), _bmask(it._bmask) { }
//const_iterator(const htype* hash_map, size_type bucket, bool) : _map(hash_map), _bucket(bucket) { init(); }
#if EMH_ITER_SAFE
const_iterator(const htype* hash_map, size_type bucket) : _map(hash_map), _bucket(bucket) { init(); }
#else
const_iterator(const htype* hash_map, size_type bucket) : _map(hash_map), _bucket(bucket) { _from = (size_type)-1; }
#endif
void init()
{
_from = (_bucket / SIZE_BIT) * SIZE_BIT;
if (_bucket < _map->bucket_count()) {
_bmask = *(size_t*)((size_t*)_map->_bitmask + _from / SIZE_BIT);
_bmask |= (1ull << _bucket % SIZE_BIT) - 1;
_bmask = ~_bmask;
} else {
_bmask = 0;
}
}
size_type bucket() const
{
return _bucket;
}
const_iterator& operator++()
{
#ifndef EMH_ITER_SAFE
if (_from == (size_type)-1) init();
#endif
goto_next_element();
return *this;
}
const_iterator operator++(int)
{
#ifndef EMH_ITER_SAFE
if (_from == (size_type)-1) init();
#endif
const_iterator old(*this);
goto_next_element();
return old;
}
reference operator*() const
{
return _map->EMH_PKV(_pairs, _bucket);
}
pointer operator->() const
{
return &(_map->EMH_PKV(_pairs, _bucket));
}
bool operator==(const const_iterator& rhs) const { return _bucket == rhs._bucket; }
bool operator!=(const const_iterator& rhs) const { return _bucket != rhs._bucket; }
private:
void goto_next_element()
{
_bmask &= _bmask - 1;
if (_bmask != 0) {
_bucket = _from + CTZ(_bmask);
return;
}
do {
_bmask = ~*(size_t*)((size_t*)_map->_bitmask + (_from += SIZE_BIT) / SIZE_BIT);
} while (_bmask == 0);
_bucket = _from + CTZ(_bmask);
}
public:
const htype* _map;
size_type _bucket;
size_type _from;
size_t _bmask;
};
void init(size_type bucket, float lf = EMH_DEFAULT_LOAD_FACTOR)
{
#if EMH_SAFE_HASH
_num_main = _hash_inter = 0;
#endif
_mask = 0;
_pairs = nullptr;
_bitmask = nullptr;
_num_filled = 0;
_mlf = (uint32_t)((1 << 27) / EMH_DEFAULT_LOAD_FACTOR);
max_load_factor(lf);
rehash(bucket);
}
HashMap(size_type bucket = 4, float lf = EMH_DEFAULT_LOAD_FACTOR)
{
init(bucket, lf);
}
size_t AllocSize(uint64_t num_buckets) const
{
return (num_buckets + PACK_SIZE) * sizeof(PairT) + (num_buckets + 7) / 8 + BIT_PACK;
}
HashMap(const HashMap& rhs)
{
if (rhs.load_factor() > EMH_MIN_LOAD_FACTOR) {
_pairs = (PairT*)malloc(AllocSize(rhs._mask + 1));
clone(rhs);
} else {
init(rhs._num_filled + 2, rhs.max_load_factor());
for (auto it = rhs.begin(); it != rhs.end(); ++it)
insert_unique(it->first, it->second);
}
}
HashMap(HashMap&& rhs) noexcept
{
#ifndef EMH_ZERO_MOVE
init(4);
#else
_mask = _num_filled = 0;
_pairs = nullptr;
#endif
swap(rhs);
}
HashMap(std::initializer_list<value_type> ilist)
{
init((size_type)ilist.size());
for (auto it = ilist.begin(); it != ilist.end(); ++it)
do_insert(*it);
}
template<class InputIt>
HashMap(InputIt first, InputIt last, size_type bucket_count=4)
{
init(std::distance(first, last) + bucket_count);
for (; first != last; ++first)
emplace(*first);
}
HashMap& operator= (const HashMap& rhs) noexcept
{
if (this == &rhs)
return *this;
if (rhs.load_factor() < EMH_MIN_LOAD_FACTOR) {
clear(); free(_pairs); _pairs = nullptr;
rehash(rhs._num_filled + 2);
for (auto it = rhs.begin(); it != rhs.end(); ++it)
insert_unique(it->first, it->second);
return *this;
}
if (is_triviall_destructable())
clearkv();
if (_mask != rhs._mask) {
free(_pairs);
_pairs = (PairT*)malloc(AllocSize(1 + rhs._mask));
}
clone(rhs);
return *this;
}
HashMap& operator= (HashMap&& rhs) noexcept
{
if (this != &rhs) {
swap(rhs);
rhs.clear();
}
return *this;
}
template<typename Con>
bool operator == (const Con& rhs) const
{
if (size() != rhs.size())
return false;
for (auto it = begin(), last = end(); it != last; ++it) {
auto oi = rhs.find(it->first);
if (oi == rhs.end() || it->second != oi->second)
return false;
}
return true;
}
template<typename Con>
bool operator != (const Con& rhs) const { return !(*this == rhs); }
~HashMap() noexcept
{
if (is_triviall_destructable()) {
for (auto it = cbegin(); _num_filled; ++it) {
_num_filled --;
it->~value_pair();
}
}
free(_pairs);
_pairs = nullptr;
}
void clone(const HashMap& rhs)
{
_hasher = rhs._hasher;
// _eq = rhs._eq;
#if EMH_SAFE_HASH
_num_main = rhs._num_main;
_hash_inter = rhs._hash_inter;
#endif
_num_filled = rhs._num_filled;
_mask = rhs._mask;
_mlf = rhs._mlf;
_bitmask = decltype(_bitmask)((char*)_pairs + ((char*)rhs._bitmask - (char*)rhs._pairs));
auto opairs = rhs._pairs;
auto _num_buckets = _mask + 1;
if (is_copy_trivially())
memcpy((char*)_pairs, opairs, _num_buckets * sizeof(PairT));
else {
for (size_type bucket = 0; bucket < _num_buckets; bucket++) {
auto next_bucket = EMH_ADDR(_pairs, bucket) = EMH_ADDR(opairs, bucket);
if ((int)next_bucket >= 0)
new(_pairs + bucket) PairT(opairs[bucket]);
}
}
memcpy((char*)(_pairs + _num_buckets), opairs + _num_buckets, PACK_SIZE * sizeof(PairT) + _num_buckets / 8 + BIT_PACK);
}
void swap(HashMap& rhs)
{
std::swap(_hasher, rhs._hasher);
// std::swap(_eq, rhs._eq);
std::swap(_pairs, rhs._pairs);
#if EMH_SAFE_HASH
std::swap(_num_main, rhs._num_main);
std::swap(_hash_inter, rhs._hash_inter);
#endif
std::swap(_num_filled, rhs._num_filled);
std::swap(_mask, rhs._mask);
std::swap(_mlf, rhs._mlf);
std::swap(_bitmask, rhs._bitmask);
//std::swap(EMH_ADDR(_pairs, _mask + 1), EMH_ADDR(rhs._pairs, rhs._mask + 1));
}
// -------------------------------------------------------------
iterator begin() noexcept
{
#ifdef EMH_ZERO_MOVE
if (0 == _num_filled)
return {this, _mask + 1};
#endif
const auto bmask = ~(*(size_t*)_bitmask);
if (bmask != 0)
return {this, CTZ(bmask)};
iterator it(this, sizeof(bmask) * 8 - 1);
it.init();
return it.next();
}
const_iterator cbegin() const noexcept
{
#ifdef EMH_ZERO_MOVE
if (0 == _num_filled)
return {this, _mask + 1};
#endif
const auto bmask = ~(*(size_t*)_bitmask);
if (bmask != 0)
return {this, CTZ(bmask)};
iterator it(this, sizeof(bmask) * 8 - 1);
it.init();
return it.next();
}
iterator last() const
{
if (_num_filled == 0)
return end();
auto bucket = _mask;
while (EMH_EMPTY(_pairs, bucket)) bucket--;
return {this, bucket, true};
}
const_iterator begin() const noexcept { return cbegin(); }
iterator end() noexcept { return {this, _mask + 1}; }
const_iterator cend() const { return {this, _mask + 1}; }
const_iterator end() const { return {this, _mask + 1}; }
size_type size() const { return _num_filled; }
bool empty() const { return _num_filled == 0; }
size_type bucket_count() const { return _mask + 1; }
float load_factor() const { return static_cast<float>(_num_filled) / ((float)_mask + 1.0f); }
HashT& hash_function() const { return _hasher; }
EqT& key_eq() const { return _eq; }
void max_load_factor(float mlf)
{
if (mlf <= 0.999f && mlf > EMH_MIN_LOAD_FACTOR)
_mlf = decltype(_mlf)((1 << 27) / mlf);
}
constexpr float max_load_factor() const { return (1 << 27) / (float)_mlf; }
constexpr size_type max_size() const { return 1ull << ((sizeof(size_type) * 8) - 1); }
constexpr size_type max_bucket_count() const { return max_size(); }
#if EMH_STATIS
//Returns the bucket number where the element with key k is located.
size_type bucket(const KeyT& key) const
{
const auto bucket = hash_key(key) & _mask;
const auto next_bucket = EMH_ADDR(_pairs, bucket);
if ((int)next_bucket < 0)
return 0;
else if (bucket == next_bucket * 2)
return bucket + 1;
return hash_main(bucket);
}
//Returns the number of elements in bucket n.
size_type bucket_size(const size_type bucket) const
{
auto next_bucket = EMH_ADDR(_pairs, bucket);
if ((int)next_bucket < 0)
return 0;
const auto& bucket_key = EMH_KEY(_pairs, bucket);
next_bucket = hash_key(bucket_key) & _mask;
size_type bucket_size = 1;
//iterator each item in current main bucket
while (true) {
const auto nbucket = EMH_BUCKET(_pairs, next_bucket);
if (nbucket == next_bucket) {
break;
}
bucket_size++;
next_bucket = nbucket;
}
return bucket_size;
}
size_type get_main_bucket(const size_type bucket) const
{
if (EMH_EMPTY(_pairs, bucket))
return -1u;
return hash_main(bucket);
}
int get_cache_info(size_type bucket, size_type next_bucket) const
{
auto pbucket = reinterpret_cast<std::ptrdiff_t>(&_pairs[bucket]);
auto pnext = reinterpret_cast<std::ptrdiff_t>(&_pairs[next_bucket]);
if (pbucket / 64 == pnext / 64)
return 0;
auto diff = pbucket > pnext ? (pbucket - pnext) : pnext - pbucket;
if (diff < 127 * 64)
return diff / 64 + 1;
return 127;
}
int get_bucket_info(const size_type bucket, size_type steps[], const size_type slots) const
{
auto next_bucket = EMH_ADDR(_pairs, bucket);
if ((int)next_bucket < 0)
return -1;
const auto main_bucket = hash_main(bucket);
if (main_bucket != bucket)
return 0;
else if (next_bucket == bucket)
return 1;
steps[get_cache_info(bucket, next_bucket) % slots] ++;
size_type ibucket_size = 2;
//find a new empty and linked it to tail
while (true) {
const auto nbucket = EMH_BUCKET(_pairs, next_bucket);
if (nbucket == next_bucket)
break;
steps[get_cache_info(nbucket, next_bucket) % slots] ++;
ibucket_size ++;
next_bucket = nbucket;
}
return ibucket_size;
}
void dump_statics() const
{
size_type buckets[129] = {0};
size_type steps[129] = {0};
for (size_type bucket = 0; bucket <= _mask; ++bucket) {
auto bsize = get_bucket_info(bucket, steps, 128);
if (bsize > 0)
buckets[bsize] ++;
}
size_type sumb = 0, collision = 0, sumc = 0, finds = 0, sumn = 0;
puts("============== buckets size ration ========");
for (size_type i = 0; i < sizeof(buckets) / sizeof(buckets[0]); i++) {
const auto bucketsi = buckets[i];
if (bucketsi == 0)
continue;
sumb += bucketsi;
sumn += bucketsi * i;
collision += bucketsi * (i - 1);
finds += bucketsi * i * (i + 1) / 2;
printf(" %2u %8u %0.8lf %2.3lf\n", i, bucketsi, bucketsi * 1.0 * i / _num_filled, sumn * 100.0 / _num_filled);
}
puts("========== collision miss ration ===========");
for (size_type i = 0; i < sizeof(steps) / sizeof(steps[0]); i++) {
sumc += steps[i];
if (steps[i] <= 2)
continue;
printf(" %2u %8u %0.2lf %.2lf\n", i, steps[i], steps[i] * 100.0 / collision, sumc * 100.0 / collision);
}
if (sumb == 0) return;
printf(" _num_filled/aver_size/packed collision/cache_miss/hit_find = %u/%.2lf/%zd/ %.2lf%%/%.2lf%%/%.2lf\n",
_num_filled, _num_filled * 1.0 / sumb, sizeof(PairT), (collision * 100.0 / _num_filled), (collision - steps[0]) * 100.0 / _num_filled, finds * 1.0 / _num_filled);
assert(sumn == _num_filled);
assert(sumc == collision);
puts("============== buckets size end =============");
}
#endif
// ------------------------------------------------------------
template<typename Key = KeyT>
inline iterator find(const Key& key, size_t key_hash) noexcept
{
return {this, find_filled_hash(key, key_hash)};
}
template<typename Key = KeyT>
inline const_iterator find(const Key& key, size_t key_hash) const noexcept
{
return {this, find_filled_hash(key, key_hash)};
}
template<typename Key=KeyT>
inline iterator find(const Key& key) noexcept
{
return {this, find_filled_bucket(key)};
}
template<typename Key = KeyT>
inline const_iterator find(const Key& key) const noexcept
{
return {this, find_filled_bucket(key)};
}
template<typename Key = KeyT>
inline ValueT& at(const KeyT& key)
{
const auto bucket = find_filled_bucket(key);
//throw
return EMH_VAL(_pairs, bucket);
}
template<typename Key = KeyT>
inline const ValueT& at(const KeyT& key) const
{
const auto bucket = find_filled_bucket(key);
//throw
return EMH_VAL(_pairs, bucket);
}
template<typename Key = KeyT>
inline bool contains(const Key& key) const noexcept
{
return find_filled_bucket(key) <= _mask;
}
template<typename Key = KeyT>
inline size_type count(const Key& key) const noexcept
{
return find_filled_bucket(key) <= _mask ? 1 : 0;
}
template<typename Key = KeyT>
std::pair<iterator, iterator> equal_range(const Key& key) const noexcept
{
const auto found = find(key);
if (found.bucket() > _mask)
return { found, found };
else
return { found, std::next(found) };
}
template<typename K=KeyT>
std::pair<const_iterator, const_iterator> equal_range(const K& key) const
{
const auto found = find(key);
if (found.bucket() > _mask)
return { found, found };
else
return { found, std::next(found) };
}
void merge(HashMap& rhs)
{
if (empty()) {
*this = std::move(rhs);
return;
}
for (auto rit = rhs.begin(); rit != rhs.end(); ) {
auto fit = find(rit->first);
if (fit.bucket() > _mask) {
insert_unique(rit->first, std::move(rit->second));
rit = rhs.erase(rit);
} else {
++rit;
}
}
}
#ifdef EMH_EXT
bool try_get(const KeyT& key, ValueT& val) const noexcept
{
const auto bucket = find_filled_bucket(key);
const auto found = bucket <= _mask;
if (found) {
val = EMH_VAL(_pairs, bucket);
}
return found;
}
/// Returns the matching ValueT or nullptr if k isn't found.
ValueT* try_get(const KeyT& key) noexcept
{
const auto bucket = find_filled_bucket(key);
return bucket <= _mask ? &EMH_VAL(_pairs, bucket) : nullptr;
}
/// Const version of the above
ValueT* try_get(const KeyT& key) const noexcept
{
const auto bucket = find_filled_bucket(key);
return bucket <= _mask ? &EMH_VAL(_pairs, bucket) : nullptr;
}
/// Convenience function.
ValueT get_or_return_default(const KeyT& key) const noexcept
{
const auto bucket = find_filled_bucket(key);
return bucket <= _mask ? EMH_VAL(_pairs, bucket) : ValueT();
}
#endif
// -----------------------------------------------------
/// Returns a pair consisting of an iterator to the inserted element
/// (or to the element that prevented the insertion)
/// and a bool denoting whether the insertion took place.
std::pair<iterator, bool> do_insert(const value_type& value)
{
const auto bucket = find_or_allocate(value.first);
const auto next = bucket / 2;
const auto found = EMH_EMPTY(_pairs, next);
if (found) {
EMH_NEW(value.first, value.second, next, bucket);
}
return { {this, next}, found };
}
std::pair<iterator, bool> do_insert(value_type&& value)
{
const auto bucket = find_or_allocate(value.first);
const auto next = bucket / 2;
const auto found = EMH_EMPTY(_pairs, next);
if (found) {
EMH_NEW(std::move(value.first), std::move(value.second), next, bucket);
}
return { {this, next}, found };
}
template<typename K, typename V>
std::pair<iterator, bool> do_insert(K&& key, V&& val)
{
const auto bucket = find_or_allocate(key);
const auto next = bucket / 2;
const auto found = EMH_EMPTY(_pairs, next);
if (found) {
EMH_NEW(std::forward<K>(key), std::forward<V>(val), next, bucket);
}
return { {this, next}, found };
}
template<typename K, typename V>
std::pair<iterator, bool> do_assign(K&& key, V&& val)
{
check_expand_need();
const auto bucket = find_or_allocate(key);
const auto next = bucket / 2;
const auto found = EMH_EMPTY(_pairs, next);
if (found) {
EMH_NEW(std::forward<K>(key), std::forward<V>(val), next, bucket);
} else {
EMH_VAL(_pairs, next) = std::move(val);
}
return { {this, next}, found };
}
std::pair<iterator, bool> insert(const value_type& value)
{
check_expand_need();
return do_insert(value);
}
std::pair<iterator, bool> insert(value_type&& value)
{
check_expand_need();
return do_insert(std::move(value));
}