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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
|
///////////////////////// ankerl::unordered_dense::{map, set} /////////////////////////
// A fast & densely stored hashmap and hashset based on robin-hood backward shift deletion.
// Version 4.4.0
// https://github.com/martinus/unordered_dense
//
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
// SPDX-License-Identifier: MIT
// Copyright (c) 2022-2023 Martin Leitner-Ankerl <martin.ankerl@gmail.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.
#ifndef ANKERL_UNORDERED_DENSE_H
#define ANKERL_UNORDERED_DENSE_H
// see https://semver.org/spec/v2.0.0.html
#define ANKERL_UNORDERED_DENSE_VERSION_MAJOR 4 // NOLINT(cppcoreguidelines-macro-usage) incompatible API changes
#define ANKERL_UNORDERED_DENSE_VERSION_MINOR 4 // NOLINT(cppcoreguidelines-macro-usage) backwards compatible functionality
#define ANKERL_UNORDERED_DENSE_VERSION_PATCH 0 // NOLINT(cppcoreguidelines-macro-usage) backwards compatible bug fixes
// API versioning with inline namespace, see https://www.foonathan.net/2018/11/inline-namespaces/
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
#define ANKERL_UNORDERED_DENSE_VERSION_CONCAT1(major, minor, patch) v##major##_##minor##_##patch
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
#define ANKERL_UNORDERED_DENSE_VERSION_CONCAT(major, minor, patch) ANKERL_UNORDERED_DENSE_VERSION_CONCAT1(major, minor, patch)
#define ANKERL_UNORDERED_DENSE_NAMESPACE \
ANKERL_UNORDERED_DENSE_VERSION_CONCAT( \
ANKERL_UNORDERED_DENSE_VERSION_MAJOR, ANKERL_UNORDERED_DENSE_VERSION_MINOR, ANKERL_UNORDERED_DENSE_VERSION_PATCH)
#if defined(_MSVC_LANG)
# define ANKERL_UNORDERED_DENSE_CPP_VERSION _MSVC_LANG
#else
# define ANKERL_UNORDERED_DENSE_CPP_VERSION __cplusplus
#endif
#if defined(__GNUC__)
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
# define ANKERL_UNORDERED_DENSE_PACK(decl) decl __attribute__((__packed__))
#elif defined(_MSC_VER)
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
# define ANKERL_UNORDERED_DENSE_PACK(decl) __pragma(pack(push, 1)) decl __pragma(pack(pop))
#endif
// exceptions
#if defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)
# define ANKERL_UNORDERED_DENSE_HAS_EXCEPTIONS() 1 // NOLINT(cppcoreguidelines-macro-usage)
#else
# define ANKERL_UNORDERED_DENSE_HAS_EXCEPTIONS() 0 // NOLINT(cppcoreguidelines-macro-usage)
#endif
#ifdef _MSC_VER
# define ANKERL_UNORDERED_DENSE_NOINLINE __declspec(noinline)
#else
# define ANKERL_UNORDERED_DENSE_NOINLINE __attribute__((noinline))
#endif
// defined in unordered_dense.cpp
#if !defined(ANKERL_UNORDERED_DENSE_EXPORT)
# define ANKERL_UNORDERED_DENSE_EXPORT
#endif
#if ANKERL_UNORDERED_DENSE_CPP_VERSION < 201703L
# error ankerl::unordered_dense requires C++17 or higher
#else
# include <array> // for array
# include <cstdint> // for uint64_t, uint32_t, uint8_t, UINT64_C
# include <cstring> // for size_t, memcpy, memset
# include <functional> // for equal_to, hash
# include <initializer_list> // for initializer_list
# include <iterator> // for pair, distance
# include <limits> // for numeric_limits
# include <memory> // for allocator, allocator_traits, shared_ptr
# include <optional> // for optional
# include <stdexcept> // for out_of_range
# include <string> // for basic_string
# include <string_view> // for basic_string_view, hash
# include <tuple> // for forward_as_tuple
# include <type_traits> // for enable_if_t, declval, conditional_t, ena...
# include <utility> // for forward, exchange, pair, as_const, piece...
# include <vector> // for vector
# if ANKERL_UNORDERED_DENSE_HAS_EXCEPTIONS() == 0
# include <cstdlib> // for abort
# endif
# if defined(__has_include)
# if __has_include(<memory_resource>)
# define ANKERL_UNORDERED_DENSE_PMR std::pmr // NOLINT(cppcoreguidelines-macro-usage)
# include <memory_resource> // for polymorphic_allocator
# elif __has_include(<experimental/memory_resource>)
# define ANKERL_UNORDERED_DENSE_PMR std::experimental::pmr // NOLINT(cppcoreguidelines-macro-usage)
# include <experimental/memory_resource> // for polymorphic_allocator
# endif
# endif
# if defined(_MSC_VER) && defined(_M_X64)
# include <intrin.h>
# pragma intrinsic(_umul128)
# endif
# if defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__)
# define ANKERL_UNORDERED_DENSE_LIKELY(x) __builtin_expect(x, 1) // NOLINT(cppcoreguidelines-macro-usage)
# define ANKERL_UNORDERED_DENSE_UNLIKELY(x) __builtin_expect(x, 0) // NOLINT(cppcoreguidelines-macro-usage)
# else
# define ANKERL_UNORDERED_DENSE_LIKELY(x) (x) // NOLINT(cppcoreguidelines-macro-usage)
# define ANKERL_UNORDERED_DENSE_UNLIKELY(x) (x) // NOLINT(cppcoreguidelines-macro-usage)
# endif
namespace ankerl::unordered_dense {
inline namespace ANKERL_UNORDERED_DENSE_NAMESPACE {
namespace detail {
# if ANKERL_UNORDERED_DENSE_HAS_EXCEPTIONS()
// make sure this is not inlined as it is slow and dramatically enlarges code, thus making other
// inlinings more difficult. Throws are also generally the slow path.
[[noreturn]] inline ANKERL_UNORDERED_DENSE_NOINLINE void on_error_key_not_found() {
throw std::out_of_range("ankerl::unordered_dense::map::at(): key not found");
}
[[noreturn]] inline ANKERL_UNORDERED_DENSE_NOINLINE void on_error_bucket_overflow() {
throw std::overflow_error("ankerl::unordered_dense: reached max bucket size, cannot increase size");
}
[[noreturn]] inline ANKERL_UNORDERED_DENSE_NOINLINE void on_error_too_many_elements() {
throw std::out_of_range("ankerl::unordered_dense::map::replace(): too many elements");
}
# else
[[noreturn]] inline void on_error_key_not_found() {
abort();
}
[[noreturn]] inline void on_error_bucket_overflow() {
abort();
}
[[noreturn]] inline void on_error_too_many_elements() {
abort();
}
# endif
} // namespace detail
// hash ///////////////////////////////////////////////////////////////////////
// This is a stripped-down implementation of wyhash: https://github.com/wangyi-fudan/wyhash
// No big-endian support (because different values on different machines don't matter),
// hardcodes seed and the secret, reformats the code, and clang-tidy fixes.
namespace detail::wyhash {
inline void mum(uint64_t* a, uint64_t* b) {
# if defined(__SIZEOF_INT128__)
__uint128_t r = *a;
r *= *b;
*a = static_cast<uint64_t>(r);
*b = static_cast<uint64_t>(r >> 64U);
# elif defined(_MSC_VER) && defined(_M_X64)
*a = _umul128(*a, *b, b);
# else
uint64_t ha = *a >> 32U;
uint64_t hb = *b >> 32U;
uint64_t la = static_cast<uint32_t>(*a);
uint64_t lb = static_cast<uint32_t>(*b);
uint64_t hi{};
uint64_t lo{};
uint64_t rh = ha * hb;
uint64_t rm0 = ha * lb;
uint64_t rm1 = hb * la;
uint64_t rl = la * lb;
uint64_t t = rl + (rm0 << 32U);
auto c = static_cast<uint64_t>(t < rl);
lo = t + (rm1 << 32U);
c += static_cast<uint64_t>(lo < t);
hi = rh + (rm0 >> 32U) + (rm1 >> 32U) + c;
*a = lo;
*b = hi;
# endif
}
// multiply and xor mix function, aka MUM
[[nodiscard]] inline auto mix(uint64_t a, uint64_t b) -> uint64_t {
mum(&a, &b);
return a ^ b;
}
// read functions. WARNING: we don't care about endianness, so results are different on big endian!
[[nodiscard]] inline auto r8(const uint8_t* p) -> uint64_t {
uint64_t v{};
std::memcpy(&v, p, 8U);
return v;
}
[[nodiscard]] inline auto r4(const uint8_t* p) -> uint64_t {
uint32_t v{};
std::memcpy(&v, p, 4);
return v;
}
// reads 1, 2, or 3 bytes
[[nodiscard]] inline auto r3(const uint8_t* p, size_t k) -> uint64_t {
return (static_cast<uint64_t>(p[0]) << 16U) | (static_cast<uint64_t>(p[k >> 1U]) << 8U) | p[k - 1];
}
[[maybe_unused]] [[nodiscard]] inline auto hash(void const* key, size_t len) -> uint64_t {
static constexpr auto secret = std::array{UINT64_C(0xa0761d6478bd642f),
UINT64_C(0xe7037ed1a0b428db),
UINT64_C(0x8ebc6af09c88c6e3),
UINT64_C(0x589965cc75374cc3)};
auto const* p = static_cast<uint8_t const*>(key);
uint64_t seed = secret[0];
uint64_t a{};
uint64_t b{};
if (ANKERL_UNORDERED_DENSE_LIKELY(len <= 16)) {
if (ANKERL_UNORDERED_DENSE_LIKELY(len >= 4)) {
a = (r4(p) << 32U) | r4(p + ((len >> 3U) << 2U));
b = (r4(p + len - 4) << 32U) | r4(p + len - 4 - ((len >> 3U) << 2U));
} else if (ANKERL_UNORDERED_DENSE_LIKELY(len > 0)) {
a = r3(p, len);
b = 0;
} else {
a = 0;
b = 0;
}
} else {
size_t i = len;
if (ANKERL_UNORDERED_DENSE_UNLIKELY(i > 48)) {
uint64_t see1 = seed;
uint64_t see2 = seed;
do {
seed = mix(r8(p) ^ secret[1], r8(p + 8) ^ seed);
see1 = mix(r8(p + 16) ^ secret[2], r8(p + 24) ^ see1);
see2 = mix(r8(p + 32) ^ secret[3], r8(p + 40) ^ see2);
p += 48;
i -= 48;
} while (ANKERL_UNORDERED_DENSE_LIKELY(i > 48));
seed ^= see1 ^ see2;
}
while (ANKERL_UNORDERED_DENSE_UNLIKELY(i > 16)) {
seed = mix(r8(p) ^ secret[1], r8(p + 8) ^ seed);
i -= 16;
p += 16;
}
a = r8(p + i - 16);
b = r8(p + i - 8);
}
return mix(secret[1] ^ len, mix(a ^ secret[1], b ^ seed));
}
[[nodiscard]] inline auto hash(uint64_t x) -> uint64_t {
return detail::wyhash::mix(x, UINT64_C(0x9E3779B97F4A7C15));
}
} // namespace detail::wyhash
ANKERL_UNORDERED_DENSE_EXPORT template <typename T, typename Enable = void>
struct hash {
auto operator()(T const& obj) const noexcept(noexcept(std::declval<std::hash<T>>().operator()(std::declval<T const&>())))
-> uint64_t {
return std::hash<T>{}(obj);
}
};
template <typename CharT>
struct hash<std::basic_string<CharT>> {
using is_avalanching = void;
auto operator()(std::basic_string<CharT> const& str) const noexcept -> uint64_t {
return detail::wyhash::hash(str.data(), sizeof(CharT) * str.size());
}
};
template <typename CharT>
struct hash<std::basic_string_view<CharT>> {
using is_avalanching = void;
auto operator()(std::basic_string_view<CharT> const& sv) const noexcept -> uint64_t {
return detail::wyhash::hash(sv.data(), sizeof(CharT) * sv.size());
}
};
template <class T>
struct hash<T*> {
using is_avalanching = void;
auto operator()(T* ptr) const noexcept -> uint64_t {
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
return detail::wyhash::hash(reinterpret_cast<uintptr_t>(ptr));
}
};
template <class T>
struct hash<std::unique_ptr<T>> {
using is_avalanching = void;
auto operator()(std::unique_ptr<T> const& ptr) const noexcept -> uint64_t {
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
return detail::wyhash::hash(reinterpret_cast<uintptr_t>(ptr.get()));
}
};
template <class T>
struct hash<std::shared_ptr<T>> {
using is_avalanching = void;
auto operator()(std::shared_ptr<T> const& ptr) const noexcept -> uint64_t {
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
return detail::wyhash::hash(reinterpret_cast<uintptr_t>(ptr.get()));
}
};
template <typename Enum>
struct hash<Enum, typename std::enable_if<std::is_enum<Enum>::value>::type> {
using is_avalanching = void;
auto operator()(Enum e) const noexcept -> uint64_t {
using underlying = typename std::underlying_type_t<Enum>;
return detail::wyhash::hash(static_cast<underlying>(e));
}
};
template <typename... Args>
struct tuple_hash_helper {
// Converts the value into 64bit. If it is an integral type, just cast it. Mixing is doing the rest.
// If it isn't an integral we need to hash it.
template <typename Arg>
[[nodiscard]] constexpr static auto to64(Arg const& arg) -> uint64_t {
if constexpr (std::is_integral_v<Arg> || std::is_enum_v<Arg>) {
return static_cast<uint64_t>(arg);
} else {
return hash<Arg>{}(arg);
}
}
[[nodiscard]] static auto mix64(uint64_t state, uint64_t v) -> uint64_t {
return detail::wyhash::mix(state + v, uint64_t{0x9ddfea08eb382d69});
}
// Creates a buffer that holds all the data from each element of the tuple. If possible we memcpy the data directly. If
// not, we hash the object and use this for the array. Size of the array is known at compile time, and memcpy is optimized
// away, so filling the buffer is highly efficient. Finally, call wyhash with this buffer.
template <typename T, std::size_t... Idx>
[[nodiscard]] static auto calc_hash(T const& t, std::index_sequence<Idx...>) noexcept -> uint64_t {
auto h = uint64_t{};
((h = mix64(h, to64(std::get<Idx>(t)))), ...);
return h;
}
};
template <typename... Args>
struct hash<std::tuple<Args...>> : tuple_hash_helper<Args...> {
using is_avalanching = void;
auto operator()(std::tuple<Args...> const& t) const noexcept -> uint64_t {
return tuple_hash_helper<Args...>::calc_hash(t, std::index_sequence_for<Args...>{});
}
};
template <typename A, typename B>
struct hash<std::pair<A, B>> : tuple_hash_helper<A, B> {
using is_avalanching = void;
auto operator()(std::pair<A, B> const& t) const noexcept -> uint64_t {
return tuple_hash_helper<A, B>::calc_hash(t, std::index_sequence_for<A, B>{});
}
};
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
# define ANKERL_UNORDERED_DENSE_HASH_STATICCAST(T) \
template <> \
struct hash<T> { \
using is_avalanching = void; \
auto operator()(T const& obj) const noexcept -> uint64_t { \
return detail::wyhash::hash(static_cast<uint64_t>(obj)); \
} \
}
# if defined(__GNUC__) && !defined(__clang__)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wuseless-cast"
# endif
// see https://en.cppreference.com/w/cpp/utility/hash
ANKERL_UNORDERED_DENSE_HASH_STATICCAST(bool);
ANKERL_UNORDERED_DENSE_HASH_STATICCAST(char);
ANKERL_UNORDERED_DENSE_HASH_STATICCAST(signed char);
ANKERL_UNORDERED_DENSE_HASH_STATICCAST(unsigned char);
# if ANKERL_UNORDERED_DENSE_CPP_VERSION >= 202002L && defined(__cpp_char8_t)
ANKERL_UNORDERED_DENSE_HASH_STATICCAST(char8_t);
# endif
ANKERL_UNORDERED_DENSE_HASH_STATICCAST(char16_t);
ANKERL_UNORDERED_DENSE_HASH_STATICCAST(char32_t);
ANKERL_UNORDERED_DENSE_HASH_STATICCAST(wchar_t);
ANKERL_UNORDERED_DENSE_HASH_STATICCAST(short);
ANKERL_UNORDERED_DENSE_HASH_STATICCAST(unsigned short);
ANKERL_UNORDERED_DENSE_HASH_STATICCAST(int);
ANKERL_UNORDERED_DENSE_HASH_STATICCAST(unsigned int);
ANKERL_UNORDERED_DENSE_HASH_STATICCAST(long);
ANKERL_UNORDERED_DENSE_HASH_STATICCAST(long long);
ANKERL_UNORDERED_DENSE_HASH_STATICCAST(unsigned long);
ANKERL_UNORDERED_DENSE_HASH_STATICCAST(unsigned long long);
# if defined(__GNUC__) && !defined(__clang__)
# pragma GCC diagnostic pop
# endif
// bucket_type //////////////////////////////////////////////////////////
namespace bucket_type {
struct standard {
static constexpr uint32_t dist_inc = 1U << 8U; // skip 1 byte fingerprint
static constexpr uint32_t fingerprint_mask = dist_inc - 1; // mask for 1 byte of fingerprint
uint32_t m_dist_and_fingerprint; // upper 3 byte: distance to original bucket. lower byte: fingerprint from hash
uint32_t m_value_idx; // index into the m_values vector.
};
ANKERL_UNORDERED_DENSE_PACK(struct big {
static constexpr uint32_t dist_inc = 1U << 8U; // skip 1 byte fingerprint
static constexpr uint32_t fingerprint_mask = dist_inc - 1; // mask for 1 byte of fingerprint
uint32_t m_dist_and_fingerprint; // upper 3 byte: distance to original bucket. lower byte: fingerprint from hash
size_t m_value_idx; // index into the m_values vector.
});
} // namespace bucket_type
namespace detail {
struct nonesuch {};
template <class Default, class AlwaysVoid, template <class...> class Op, class... Args>
struct detector {
using value_t = std::false_type;
using type = Default;
};
template <class Default, template <class...> class Op, class... Args>
struct detector<Default, std::void_t<Op<Args...>>, Op, Args...> {
using value_t = std::true_type;
using type = Op<Args...>;
};
template <template <class...> class Op, class... Args>
using is_detected = typename detail::detector<detail::nonesuch, void, Op, Args...>::value_t;
template <template <class...> class Op, class... Args>
constexpr bool is_detected_v = is_detected<Op, Args...>::value;
template <typename T>
using detect_avalanching = typename T::is_avalanching;
template <typename T>
using detect_is_transparent = typename T::is_transparent;
template <typename T>
using detect_iterator = typename T::iterator;
template <typename T>
using detect_reserve = decltype(std::declval<T&>().reserve(size_t{}));
// enable_if helpers
template <typename Mapped>
constexpr bool is_map_v = !std::is_void_v<Mapped>;
// clang-format off
template <typename Hash, typename KeyEqual>
constexpr bool is_transparent_v = is_detected_v<detect_is_transparent, Hash> && is_detected_v<detect_is_transparent, KeyEqual>;
// clang-format on
template <typename From, typename To1, typename To2>
constexpr bool is_neither_convertible_v = !std::is_convertible_v<From, To1> && !std::is_convertible_v<From, To2>;
template <typename T>
constexpr bool has_reserve = is_detected_v<detect_reserve, T>;
// base type for map has mapped_type
template <class T>
struct base_table_type_map {
using mapped_type = T;
};
// base type for set doesn't have mapped_type
struct base_table_type_set {};
} // namespace detail
// Very much like std::deque, but faster for indexing (in most cases). As of now this doesn't implement the full std::vector
// API, but merely what's necessary to work as an underlying container for ankerl::unordered_dense::{map, set}.
// It allocates blocks of equal size and puts them into the m_blocks vector. That means it can grow simply by adding a new
// block to the back of m_blocks, and doesn't double its size like an std::vector. The disadvantage is that memory is not
// linear and thus there is one more indirection necessary for indexing.
template <typename T, typename Allocator = std::allocator<T>, size_t MaxSegmentSizeBytes = 4096>
class segmented_vector {
template <bool IsConst>
class iter_t;
public:
using allocator_type = Allocator;
using pointer = typename std::allocator_traits<allocator_type>::pointer;
using const_pointer = typename std::allocator_traits<allocator_type>::const_pointer;
using difference_type = typename std::allocator_traits<allocator_type>::difference_type;
using value_type = T;
using size_type = std::size_t;
using reference = T&;
using const_reference = T const&;
using iterator = iter_t<false>;
using const_iterator = iter_t<true>;
private:
using vec_alloc = typename std::allocator_traits<Allocator>::template rebind_alloc<pointer>;
std::vector<pointer, vec_alloc> m_blocks{};
size_t m_size{};
// Calculates the maximum number for x in (s << x) <= max_val
static constexpr auto num_bits_closest(size_t max_val, size_t s) -> size_t {
auto f = size_t{0};
while (s << (f + 1) <= max_val) {
++f;
}
return f;
}
using self_t = segmented_vector<T, Allocator, MaxSegmentSizeBytes>;
static constexpr auto num_bits = num_bits_closest(MaxSegmentSizeBytes, sizeof(T));
static constexpr auto num_elements_in_block = 1U << num_bits;
static constexpr auto mask = num_elements_in_block - 1U;
/**
* Iterator class doubles as const_iterator and iterator
*/
template <bool IsConst>
class iter_t {
using ptr_t = typename std::conditional_t<IsConst, segmented_vector::const_pointer const*, segmented_vector::pointer*>;
ptr_t m_data{};
size_t m_idx{};
template <bool B>
friend class iter_t;
public:
using difference_type = segmented_vector::difference_type;
using value_type = T;
using reference = typename std::conditional_t<IsConst, value_type const&, value_type&>;
using pointer = typename std::conditional_t<IsConst, segmented_vector::const_pointer, segmented_vector::pointer>;
using iterator_category = std::forward_iterator_tag;
iter_t() noexcept = default;
template <bool OtherIsConst, typename = typename std::enable_if<IsConst && !OtherIsConst>::type>
// NOLINTNEXTLINE(google-explicit-constructor,hicpp-explicit-conversions)
constexpr iter_t(iter_t<OtherIsConst> const& other) noexcept
: m_data(other.m_data)
, m_idx(other.m_idx) {}
constexpr iter_t(ptr_t data, size_t idx) noexcept
: m_data(data)
, m_idx(idx) {}
template <bool OtherIsConst, typename = typename std::enable_if<IsConst && !OtherIsConst>::type>
constexpr auto operator=(iter_t<OtherIsConst> const& other) noexcept -> iter_t& {
m_data = other.m_data;
m_idx = other.m_idx;
return *this;
}
constexpr auto operator++() noexcept -> iter_t& {
++m_idx;
return *this;
}
constexpr auto operator+(difference_type diff) noexcept -> iter_t {
return {m_data, static_cast<size_t>(static_cast<difference_type>(m_idx) + diff)};
}
template <bool OtherIsConst>
constexpr auto operator-(iter_t<OtherIsConst> const& other) noexcept -> difference_type {
return static_cast<difference_type>(m_idx) - static_cast<difference_type>(other.m_idx);
}
constexpr auto operator*() const noexcept -> reference {
return m_data[m_idx >> num_bits][m_idx & mask];
}
constexpr auto operator->() const noexcept -> pointer {
return &m_data[m_idx >> num_bits][m_idx & mask];
}
template <bool O>
constexpr auto operator==(iter_t<O> const& o) const noexcept -> bool {
return m_idx == o.m_idx;
}
template <bool O>
constexpr auto operator!=(iter_t<O> const& o) const noexcept -> bool {
return !(*this == o);
}
};
// slow path: need to allocate a new segment every once in a while
void increase_capacity() {
auto ba = Allocator(m_blocks.get_allocator());
pointer block = std::allocator_traits<Allocator>::allocate(ba, num_elements_in_block);
m_blocks.push_back(block);
}
// Moves everything from other
void append_everything_from(segmented_vector&& other) {
reserve(size() + other.size());
for (auto&& o : other) {
emplace_back(std::move(o));
}
}
// Copies everything from other
void append_everything_from(segmented_vector const& other) {
reserve(size() + other.size());
for (auto const& o : other) {
emplace_back(o);
}
}
void dealloc() {
auto ba = Allocator(m_blocks.get_allocator());
for (auto ptr : m_blocks) {
std::allocator_traits<Allocator>::deallocate(ba, ptr, num_elements_in_block);
}
}
[[nodiscard]] static constexpr auto calc_num_blocks_for_capacity(size_t capacity) {
return (capacity + num_elements_in_block - 1U) / num_elements_in_block;
}
public:
segmented_vector() = default;
// NOLINTNEXTLINE(google-explicit-constructor,hicpp-explicit-conversions)
segmented_vector(Allocator alloc)
: m_blocks(vec_alloc(alloc)) {}
segmented_vector(segmented_vector&& other, Allocator alloc)
: segmented_vector(alloc) {
*this = std::move(other);
}
segmented_vector(segmented_vector const& other, Allocator alloc)
: m_blocks(vec_alloc(alloc)) {
append_everything_from(other);
}
segmented_vector(segmented_vector&& other) noexcept
: segmented_vector(std::move(other), get_allocator()) {}
segmented_vector(segmented_vector const& other) {
append_everything_from(other);
}
auto operator=(segmented_vector const& other) -> segmented_vector& {
if (this == &other) {
return *this;
}
clear();
append_everything_from(other);
return *this;
}
auto operator=(segmented_vector&& other) noexcept -> segmented_vector& {
clear();
dealloc();
if (other.get_allocator() == get_allocator()) {
m_blocks = std::move(other.m_blocks);
m_size = std::exchange(other.m_size, {});
} else {
// make sure to construct with other's allocator!
m_blocks = std::vector<pointer, vec_alloc>(vec_alloc(other.get_allocator()));
append_everything_from(std::move(other));
}
return *this;
}
~segmented_vector() {
clear();
dealloc();
}
[[nodiscard]] constexpr auto size() const -> size_t {
return m_size;
}
[[nodiscard]] constexpr auto capacity() const -> size_t {
return m_blocks.size() * num_elements_in_block;
}
// Indexing is highly performance critical
[[nodiscard]] constexpr auto operator[](size_t i) const noexcept -> T const& {
return m_blocks[i >> num_bits][i & mask];
}
[[nodiscard]] constexpr auto operator[](size_t i) noexcept -> T& {
return m_blocks[i >> num_bits][i & mask];
}
[[nodiscard]] constexpr auto begin() -> iterator {
return {m_blocks.data(), 0U};
}
[[nodiscard]] constexpr auto begin() const -> const_iterator {
return {m_blocks.data(), 0U};
}
[[nodiscard]] constexpr auto cbegin() const -> const_iterator {
return {m_blocks.data(), 0U};
}
[[nodiscard]] constexpr auto end() -> iterator {
return {m_blocks.data(), m_size};
}
[[nodiscard]] constexpr auto end() const -> const_iterator {
return {m_blocks.data(), m_size};
}
[[nodiscard]] constexpr auto cend() const -> const_iterator {
return {m_blocks.data(), m_size};
}
[[nodiscard]] constexpr auto back() -> reference {
return operator[](m_size - 1);
}
[[nodiscard]] constexpr auto back() const -> const_reference {
return operator[](m_size - 1);
}
void pop_back() {
back().~T();
--m_size;
}
[[nodiscard]] auto empty() const {
return 0 == m_size;
}
void reserve(size_t new_capacity) {
m_blocks.reserve(calc_num_blocks_for_capacity(new_capacity));
while (new_capacity > capacity()) {
increase_capacity();
}
}
[[nodiscard]] auto get_allocator() const -> allocator_type {
return allocator_type{m_blocks.get_allocator()};
}
template <class... Args>
auto emplace_back(Args&&... args) -> reference {
if (m_size == capacity()) {
increase_capacity();
}
auto* ptr = static_cast<void*>(&operator[](m_size));
auto& ref = *new (ptr) T(std::forward<Args>(args)...);
++m_size;
return ref;
}
void clear() {
if constexpr (!std::is_trivially_destructible_v<T>) {
for (size_t i = 0, s = size(); i < s; ++i) {
operator[](i).~T();
}
}
m_size = 0;
}
void shrink_to_fit() {
auto ba = Allocator(m_blocks.get_allocator());
auto num_blocks_required = calc_num_blocks_for_capacity(m_size);
while (m_blocks.size() > num_blocks_required) {
std::allocator_traits<Allocator>::deallocate(ba, m_blocks.back(), num_elements_in_block);
m_blocks.pop_back();
}
m_blocks.shrink_to_fit();
}
};
namespace detail {
// This is it, the table. Doubles as map and set, and uses `void` for T when its used as a set.
template <class Key,
class T, // when void, treat it as a set.
class Hash,
class KeyEqual,
class AllocatorOrContainer,
class Bucket,
bool IsSegmented>
class table : public std::conditional_t<is_map_v<T>, base_table_type_map<T>, base_table_type_set> {
using underlying_value_type = typename std::conditional_t<is_map_v<T>, std::pair<Key, T>, Key>;
using underlying_container_type = std::conditional_t<IsSegmented,
segmented_vector<underlying_value_type, AllocatorOrContainer>,
std::vector<underlying_value_type, AllocatorOrContainer>>;
public:
using value_container_type = std::
conditional_t<is_detected_v<detect_iterator, AllocatorOrContainer>, AllocatorOrContainer, underlying_container_type>;
private:
using bucket_alloc =
typename std::allocator_traits<typename value_container_type::allocator_type>::template rebind_alloc<Bucket>;
using bucket_alloc_traits = std::allocator_traits<bucket_alloc>;
static constexpr uint8_t initial_shifts = 64 - 2; // 2^(64-m_shift) number of buckets
static constexpr float default_max_load_factor = 0.8F;
public:
using key_type = Key;
using value_type = typename value_container_type::value_type;
using size_type = typename value_container_type::size_type;
using difference_type = typename value_container_type::difference_type;
using hasher = Hash;
using key_equal = KeyEqual;
using allocator_type = typename value_container_type::allocator_type;
using reference = typename value_container_type::reference;
using const_reference = typename value_container_type::const_reference;
using pointer = typename value_container_type::pointer;
using const_pointer = typename value_container_type::const_pointer;
using const_iterator = typename value_container_type::const_iterator;
using iterator = std::conditional_t<is_map_v<T>, typename value_container_type::iterator, const_iterator>;
using bucket_type = Bucket;
private:
using value_idx_type = decltype(Bucket::m_value_idx);
using dist_and_fingerprint_type = decltype(Bucket::m_dist_and_fingerprint);
static_assert(std::is_trivially_destructible_v<Bucket>, "assert there's no need to call destructor / std::destroy");
static_assert(std::is_trivially_copyable_v<Bucket>, "assert we can just memset / memcpy");
value_container_type m_values{}; // Contains all the key-value pairs in one densely stored container. No holes.
using bucket_pointer = typename std::allocator_traits<bucket_alloc>::pointer;
bucket_pointer m_buckets{};
size_t m_num_buckets = 0;
size_t m_max_bucket_capacity = 0;
float m_max_load_factor = default_max_load_factor;
Hash m_hash{};
KeyEqual m_equal{};
uint8_t m_shifts = initial_shifts;
[[nodiscard]] auto next(value_idx_type bucket_idx) const -> value_idx_type {
return ANKERL_UNORDERED_DENSE_UNLIKELY(bucket_idx + 1U == m_num_buckets)
? 0
: static_cast<value_idx_type>(bucket_idx + 1U);
}
// Helper to access bucket through pointer types
[[nodiscard]] static constexpr auto at(bucket_pointer bucket_ptr, size_t offset) -> Bucket& {
return *(bucket_ptr + static_cast<typename std::allocator_traits<bucket_alloc>::difference_type>(offset));
}
// use the dist_inc and dist_dec functions so that uint16_t types work without warning
[[nodiscard]] static constexpr auto dist_inc(dist_and_fingerprint_type x) -> dist_and_fingerprint_type {
return static_cast<dist_and_fingerprint_type>(x + Bucket::dist_inc);
}
[[nodiscard]] static constexpr auto dist_dec(dist_and_fingerprint_type x) -> dist_and_fingerprint_type {
return static_cast<dist_and_fingerprint_type>(x - Bucket::dist_inc);
}
// The goal of mixed_hash is to always produce a high quality 64bit hash.
template <typename K>
[[nodiscard]] constexpr auto mixed_hash(K const& key) const -> uint64_t {
if constexpr (is_detected_v<detect_avalanching, Hash>) {
// we know that the hash is good because is_avalanching.
if constexpr (sizeof(decltype(m_hash(key))) < sizeof(uint64_t)) {
// 32bit hash and is_avalanching => multiply with a constant to avalanche bits upwards
return m_hash(key) * UINT64_C(0x9ddfea08eb382d69);
} else {
// 64bit and is_avalanching => only use the hash itself.
return m_hash(key);
}
} else {
// not is_avalanching => apply wyhash
return wyhash::hash(m_hash(key));
}
}
[[nodiscard]] constexpr auto dist_and_fingerprint_from_hash(uint64_t hash) const -> dist_and_fingerprint_type {
return Bucket::dist_inc | (static_cast<dist_and_fingerprint_type>(hash) & Bucket::fingerprint_mask);
}
[[nodiscard]] constexpr auto bucket_idx_from_hash(uint64_t hash) const -> value_idx_type {
return static_cast<value_idx_type>(hash >> m_shifts);
}
[[nodiscard]] static constexpr auto get_key(value_type const& vt) -> key_type const& {
if constexpr (is_map_v<T>) {
return vt.first;
} else {
return vt;
}
}
template <typename K>
[[nodiscard]] auto next_while_less(K const& key) const -> Bucket {
auto hash = mixed_hash(key);
auto dist_and_fingerprint = dist_and_fingerprint_from_hash(hash);
auto bucket_idx = bucket_idx_from_hash(hash);
while (dist_and_fingerprint < at(m_buckets, bucket_idx).m_dist_and_fingerprint) {
dist_and_fingerprint = dist_inc(dist_and_fingerprint);
bucket_idx = next(bucket_idx);
}
return {dist_and_fingerprint, bucket_idx};
}
void place_and_shift_up(Bucket bucket, value_idx_type place) {
while (0 != at(m_buckets, place).m_dist_and_fingerprint) {
bucket = std::exchange(at(m_buckets, place), bucket);
bucket.m_dist_and_fingerprint = dist_inc(bucket.m_dist_and_fingerprint);
place = next(place);
}
at(m_buckets, place) = bucket;
}
[[nodiscard]] static constexpr auto calc_num_buckets(uint8_t shifts) -> size_t {
return (std::min)(max_bucket_count(), size_t{1} << (64U - shifts));
}
[[nodiscard]] constexpr auto calc_shifts_for_size(size_t s) const -> uint8_t {
auto shifts = initial_shifts;
while (shifts > 0 && static_cast<size_t>(static_cast<float>(calc_num_buckets(shifts)) * max_load_factor()) < s) {
--shifts;
}
return shifts;
}
// assumes m_values has data, m_buckets=m_buckets_end=nullptr, m_shifts is INITIAL_SHIFTS
void copy_buckets(table const& other) {
// assumes m_values has already the correct data copied over.
if (empty()) {
// when empty, at least allocate an initial buckets and clear them.
allocate_buckets_from_shift();
clear_buckets();
} else {
m_shifts = other.m_shifts;
allocate_buckets_from_shift();
std::memcpy(m_buckets, other.m_buckets, sizeof(Bucket) * bucket_count());
}
}
/**
* True when no element can be added any more without increasing the size
*/
[[nodiscard]] auto is_full() const -> bool {
return size() > m_max_bucket_capacity;
}
void deallocate_buckets() {
auto ba = bucket_alloc(m_values.get_allocator());
if (nullptr != m_buckets) {
bucket_alloc_traits::deallocate(ba, m_buckets, bucket_count());
m_buckets = nullptr;
}
m_num_buckets = 0;
m_max_bucket_capacity = 0;
}
void allocate_buckets_from_shift() {
auto ba = bucket_alloc(m_values.get_allocator());
m_num_buckets = calc_num_buckets(m_shifts);
m_buckets = bucket_alloc_traits::allocate(ba, m_num_buckets);
if (m_num_buckets == max_bucket_count()) {
// reached the maximum, make sure we can use each bucket
m_max_bucket_capacity = max_bucket_count();
} else {
m_max_bucket_capacity = static_cast<value_idx_type>(static_cast<float>(m_num_buckets) * max_load_factor());
}
}
void clear_buckets() {
if (m_buckets != nullptr) {
std::memset(&*m_buckets, 0, sizeof(Bucket) * bucket_count());
}
}
void clear_and_fill_buckets_from_values() {
clear_buckets();
for (value_idx_type value_idx = 0, end_idx = static_cast<value_idx_type>(m_values.size()); value_idx < end_idx;
++value_idx) {
auto const& key = get_key(m_values[value_idx]);
auto [dist_and_fingerprint, bucket] = next_while_less(key);
// we know for certain that key has not yet been inserted, so no need to check it.
place_and_shift_up({dist_and_fingerprint, value_idx}, bucket);
}
}
void increase_size() {
if (m_max_bucket_capacity == max_bucket_count()) {
// remove the value again, we can't add it!
m_values.pop_back();
on_error_bucket_overflow();
}
--m_shifts;
deallocate_buckets();
allocate_buckets_from_shift();
clear_and_fill_buckets_from_values();
}
template <typename Op>
void do_erase(value_idx_type bucket_idx, Op handle_erased_value) {
auto const value_idx_to_remove = at(m_buckets, bucket_idx).m_value_idx;
// shift down until either empty or an element with correct spot is found
auto next_bucket_idx = next(bucket_idx);
while (at(m_buckets, next_bucket_idx).m_dist_and_fingerprint >= Bucket::dist_inc * 2) {
at(m_buckets, bucket_idx) = {dist_dec(at(m_buckets, next_bucket_idx).m_dist_and_fingerprint),
at(m_buckets, next_bucket_idx).m_value_idx};
bucket_idx = std::exchange(next_bucket_idx, next(next_bucket_idx));
}
at(m_buckets, bucket_idx) = {};
handle_erased_value(std::move(m_values[value_idx_to_remove]));
// update m_values
if (value_idx_to_remove != m_values.size() - 1) {
// no luck, we'll have to replace the value with the last one and update the index accordingly
auto& val = m_values[value_idx_to_remove];
val = std::move(m_values.back());
// update the values_idx of the moved entry. No need to play the info game, just look until we find the values_idx
auto mh = mixed_hash(get_key(val));
bucket_idx = bucket_idx_from_hash(mh);
auto const values_idx_back = static_cast<value_idx_type>(m_values.size() - 1);
while (values_idx_back != at(m_buckets, bucket_idx).m_value_idx) {
bucket_idx = next(bucket_idx);
}
at(m_buckets, bucket_idx).m_value_idx = value_idx_to_remove;
}
m_values.pop_back();
}
template <typename K, typename Op>
auto do_erase_key(K&& key, Op handle_erased_value) -> size_t {
if (empty()) {
return 0;
}
auto [dist_and_fingerprint, bucket_idx] = next_while_less(key);
while (dist_and_fingerprint == at(m_buckets, bucket_idx).m_dist_and_fingerprint &&
!m_equal(key, get_key(m_values[at(m_buckets, bucket_idx).m_value_idx]))) {
dist_and_fingerprint = dist_inc(dist_and_fingerprint);
bucket_idx = next(bucket_idx);
}
if (dist_and_fingerprint != at(m_buckets, bucket_idx).m_dist_and_fingerprint) {
return 0;
}
do_erase(bucket_idx, handle_erased_value);
return 1;
}
template <class K, class M>
auto do_insert_or_assign(K&& key, M&& mapped) -> std::pair<iterator, bool> {
auto it_isinserted = try_emplace(std::forward<K>(key), std::forward<M>(mapped));
if (!it_isinserted.second) {
it_isinserted.first->second = std::forward<M>(mapped);
}
return it_isinserted;
}
template <typename... Args>
auto do_place_element(dist_and_fingerprint_type dist_and_fingerprint, value_idx_type bucket_idx, Args&&... args)
-> std::pair<iterator, bool> {
// emplace the new value. If that throws an exception, no harm done; index is still in a valid state
m_values.emplace_back(std::forward<Args>(args)...);
auto value_idx = static_cast<value_idx_type>(m_values.size() - 1);
if (ANKERL_UNORDERED_DENSE_UNLIKELY(is_full())) {
increase_size();
} else {
place_and_shift_up({dist_and_fingerprint, value_idx}, bucket_idx);
}
// place element and shift up until we find an empty spot
return {begin() + static_cast<difference_type>(value_idx), true};
}
template <typename K, typename... Args>
auto do_try_emplace(K&& key, Args&&... args) -> std::pair<iterator, bool> {
auto hash = mixed_hash(key);
auto dist_and_fingerprint = dist_and_fingerprint_from_hash(hash);
auto bucket_idx = bucket_idx_from_hash(hash);
while (true) {
auto* bucket = &at(m_buckets, bucket_idx);
if (dist_and_fingerprint == bucket->m_dist_and_fingerprint) {
if (m_equal(key, get_key(m_values[bucket->m_value_idx]))) {
return {begin() + static_cast<difference_type>(bucket->m_value_idx), false};
}
} else if (dist_and_fingerprint > bucket->m_dist_and_fingerprint) {
return do_place_element(dist_and_fingerprint,
bucket_idx,
std::piecewise_construct,
std::forward_as_tuple(std::forward<K>(key)),
std::forward_as_tuple(std::forward<Args>(args)...));
}
dist_and_fingerprint = dist_inc(dist_and_fingerprint);
bucket_idx = next(bucket_idx);
}
}
template <typename K>
auto do_find(K const& key) -> iterator {
if (ANKERL_UNORDERED_DENSE_UNLIKELY(empty())) {
return end();
}
auto mh = mixed_hash(key);
auto dist_and_fingerprint = dist_and_fingerprint_from_hash(mh);
auto bucket_idx = bucket_idx_from_hash(mh);
auto* bucket = &at(m_buckets, bucket_idx);
// unrolled loop. *Always* check a few directly, then enter the loop. This is faster.
if (dist_and_fingerprint == bucket->m_dist_and_fingerprint && m_equal(key, get_key(m_values[bucket->m_value_idx]))) {
return begin() + static_cast<difference_type>(bucket->m_value_idx);
}
dist_and_fingerprint = dist_inc(dist_and_fingerprint);
bucket_idx = next(bucket_idx);
bucket = &at(m_buckets, bucket_idx);
if (dist_and_fingerprint == bucket->m_dist_and_fingerprint && m_equal(key, get_key(m_values[bucket->m_value_idx]))) {
return begin() + static_cast<difference_type>(bucket->m_value_idx);
}
dist_and_fingerprint = dist_inc(dist_and_fingerprint);
bucket_idx = next(bucket_idx);
bucket = &at(m_buckets, bucket_idx);
while (true) {
if (dist_and_fingerprint == bucket->m_dist_and_fingerprint) {
if (m_equal(key, get_key(m_values[bucket->m_value_idx]))) {
return begin() + static_cast<difference_type>(bucket->m_value_idx);
}
} else if (dist_and_fingerprint > bucket->m_dist_and_fingerprint) {
return end();
}
dist_and_fingerprint = dist_inc(dist_and_fingerprint);
bucket_idx = next(bucket_idx);
bucket = &at(m_buckets, bucket_idx);
}
}
template <typename K>
auto do_find(K const& key) const -> const_iterator {
return const_cast<table*>(this)->do_find(key); // NOLINT(cppcoreguidelines-pro-type-const-cast)
}
template <typename K, typename Q = T, std::enable_if_t<is_map_v<Q>, bool> = true>
auto do_at(K const& key) -> Q& {
if (auto it = find(key); ANKERL_UNORDERED_DENSE_LIKELY(end() != it)) {
return it->second;
}
on_error_key_not_found();
}
template <typename K, typename Q = T, std::enable_if_t<is_map_v<Q>, bool> = true>
auto do_at(K const& key) const -> Q const& {
return const_cast<table*>(this)->at(key); // NOLINT(cppcoreguidelines-pro-type-const-cast)
}
public:
explicit table(size_t bucket_count,
Hash const& hash = Hash(),
KeyEqual const& equal = KeyEqual(),
allocator_type const& alloc_or_container = allocator_type())
: m_values(alloc_or_container)
, m_hash(hash)
, m_equal(equal) {
if (0 != bucket_count) {
reserve(bucket_count);
} else {
allocate_buckets_from_shift();
clear_buckets();
}
}
table()
: table(0) {}
table(size_t bucket_count, allocator_type const& alloc)
: table(bucket_count, Hash(), KeyEqual(), alloc) {}
table(size_t bucket_count, Hash const& hash, allocator_type const& alloc)
: table(bucket_count, hash, KeyEqual(), alloc) {}
explicit table(allocator_type const& alloc)
: table(0, Hash(), KeyEqual(), alloc) {}
template <class InputIt>
table(InputIt first,
InputIt last,
size_type bucket_count = 0,
Hash const& hash = Hash(),
KeyEqual const& equal = KeyEqual(),
allocator_type const& alloc = allocator_type())
: table(bucket_count, hash, equal, alloc) {
insert(first, last);
}
template <class InputIt>
table(InputIt first, InputIt last, size_type bucket_count, allocator_type const& alloc)
: table(first, last, bucket_count, Hash(), KeyEqual(), alloc) {}
template <class InputIt>
table(InputIt first, InputIt last, size_type bucket_count, Hash const& hash, allocator_type const& alloc)
: table(first, last, bucket_count, hash, KeyEqual(), alloc) {}
table(table const& other)
: table(other, other.m_values.get_allocator()) {}
table(table const& other, allocator_type const& alloc)
: m_values(other.m_values, alloc)
, m_max_load_factor(other.m_max_load_factor)
, m_hash(other.m_hash)
, m_equal(other.m_equal) {
copy_buckets(other);
}
table(table&& other) noexcept
: table(std::move(other), other.m_values.get_allocator()) {}
table(table&& other, allocator_type const& alloc) noexcept
: m_values(alloc) {
*this = std::move(other);
}
table(std::initializer_list<value_type> ilist,
size_t bucket_count = 0,
Hash const& hash = Hash(),
KeyEqual const& equal = KeyEqual(),
allocator_type const& alloc = allocator_type())
: table(bucket_count, hash, equal, alloc) {
insert(ilist);
}
table(std::initializer_list<value_type> ilist, size_type bucket_count, allocator_type const& alloc)
: table(ilist, bucket_count, Hash(), KeyEqual(), alloc) {}
table(std::initializer_list<value_type> init, size_type bucket_count, Hash const& hash, allocator_type const& alloc)
: table(init, bucket_count, hash, KeyEqual(), alloc) {}
~table() {
if (nullptr != m_buckets) {
auto ba = bucket_alloc(m_values.get_allocator());
bucket_alloc_traits::deallocate(ba, m_buckets, bucket_count());
}
}
auto operator=(table const& other) -> table& {
if (&other != this) {
deallocate_buckets(); // deallocate before m_values is set (might have another allocator)
m_values = other.m_values;
m_max_load_factor = other.m_max_load_factor;
m_hash = other.m_hash;
m_equal = other.m_equal;
m_shifts = initial_shifts;
copy_buckets(other);
}
return *this;
}
auto operator=(table&& other) noexcept(noexcept(std::is_nothrow_move_assignable_v<value_container_type> &&
std::is_nothrow_move_assignable_v<Hash> &&
std::is_nothrow_move_assignable_v<KeyEqual>)) -> table& {
if (&other != this) {
deallocate_buckets(); // deallocate before m_values is set (might have another allocator)
m_values = std::move(other.m_values);
other.m_values.clear();
// we can only reuse m_buckets when both maps have the same allocator!
if (get_allocator() == other.get_allocator()) {
m_buckets = std::exchange(other.m_buckets, nullptr);
m_num_buckets = std::exchange(other.m_num_buckets, 0);
m_max_bucket_capacity = std::exchange(other.m_max_bucket_capacity, 0);
m_shifts = std::exchange(other.m_shifts, initial_shifts);
m_max_load_factor = std::exchange(other.m_max_load_factor, default_max_load_factor);
m_hash = std::exchange(other.m_hash, {});
m_equal = std::exchange(other.m_equal, {});
other.allocate_buckets_from_shift();
other.clear_buckets();
} else {
// set max_load_factor *before* copying the other's buckets, so we have the same
// behavior
m_max_load_factor = other.m_max_load_factor;
// copy_buckets sets m_buckets, m_num_buckets, m_max_bucket_capacity, m_shifts
copy_buckets(other);
// clear's the other's buckets so other is now already usable.
other.clear_buckets();
m_hash = other.m_hash;
m_equal = other.m_equal;
}
// map "other" is now already usable, it's empty.
}
return *this;
}
auto operator=(std::initializer_list<value_type> ilist) -> table& {
clear();
insert(ilist);
return *this;
}
auto get_allocator() const noexcept -> allocator_type {
return m_values.get_allocator();
}
// iterators //////////////////////////////////////////////////////////////
auto begin() noexcept -> iterator {
return m_values.begin();
}
auto begin() const noexcept -> const_iterator {
return m_values.begin();
}
auto cbegin() const noexcept -> const_iterator {
return m_values.cbegin();
}
auto end() noexcept -> iterator {
return m_values.end();
}
auto cend() const noexcept -> const_iterator {
return m_values.cend();
}
auto end() const noexcept -> const_iterator {
return m_values.end();
}
// capacity ///////////////////////////////////////////////////////////////
[[nodiscard]] auto empty() const noexcept -> bool {
return m_values.empty();
}
[[nodiscard]] auto size() const noexcept -> size_t {
return m_values.size();
}
[[nodiscard]] static constexpr auto max_size() noexcept -> size_t {
if constexpr ((std::numeric_limits<value_idx_type>::max)() == (std::numeric_limits<size_t>::max)()) {
return size_t{1} << (sizeof(value_idx_type) * 8 - 1);
} else {
return size_t{1} << (sizeof(value_idx_type) * 8);
}
}
// modifiers //////////////////////////////////////////////////////////////
void clear() {
m_values.clear();
clear_buckets();
}
auto insert(value_type const& value) -> std::pair<iterator, bool> {
return emplace(value);
}
auto insert(value_type&& value) -> std::pair<iterator, bool> {
return emplace(std::move(value));
}
template <class P, std::enable_if_t<std::is_constructible_v<value_type, P&&>, bool> = true>
auto insert(P&& value) -> std::pair<iterator, bool> {
return emplace(std::forward<P>(value));
}
auto insert(const_iterator /*hint*/, value_type const& value) -> iterator {
return insert(value).first;
}
auto insert(const_iterator /*hint*/, value_type&& value) -> iterator {
return insert(std::move(value)).first;
}
template <class P, std::enable_if_t<std::is_constructible_v<value_type, P&&>, bool> = true>
auto insert(const_iterator /*hint*/, P&& value) -> iterator {
return insert(std::forward<P>(value)).first;
}
template <class InputIt>
void insert(InputIt first, InputIt last) {
while (first != last) {
insert(*first);
++first;
}
}
void insert(std::initializer_list<value_type> ilist) {
insert(ilist.begin(), ilist.end());
}
// nonstandard API: *this is emptied.
// Also see "A Standard flat_map" https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p0429r9.pdf
auto extract() && -> value_container_type {
return std::move(m_values);
}
// nonstandard API:
// Discards the internally held container and replaces it with the one passed. Erases non-unique elements.
auto replace(value_container_type&& container) {
if (ANKERL_UNORDERED_DENSE_UNLIKELY(container.size() > max_size())) {
on_error_too_many_elements();
}
auto shifts = calc_shifts_for_size(container.size());
if (0 == m_num_buckets || shifts < m_shifts || container.get_allocator() != m_values.get_allocator()) {
m_shifts = shifts;
deallocate_buckets();
allocate_buckets_from_shift();
}
clear_buckets();
m_values = std::move(container);
// can't use clear_and_fill_buckets_from_values() because container elements might not be unique
auto value_idx = value_idx_type{};
// loop until we reach the end of the container. duplicated entries will be replaced with back().
while (value_idx != static_cast<value_idx_type>(m_values.size())) {
auto const& key = get_key(m_values[value_idx]);
auto hash = mixed_hash(key);
auto dist_and_fingerprint = dist_and_fingerprint_from_hash(hash);
auto bucket_idx = bucket_idx_from_hash(hash);
bool key_found = false;
while (true) {
auto const& bucket = at(m_buckets, bucket_idx);
if (dist_and_fingerprint > bucket.m_dist_and_fingerprint) {
break;
}
if (dist_and_fingerprint == bucket.m_dist_and_fingerprint &&
m_equal(key, get_key(m_values[bucket.m_value_idx]))) {
key_found = true;
break;
}
dist_and_fingerprint = dist_inc(dist_and_fingerprint);
bucket_idx = next(bucket_idx);
}
if (key_found) {
if (value_idx != static_cast<value_idx_type>(m_values.size() - 1)) {
m_values[value_idx] = std::move(m_values.back());
}
m_values.pop_back();
} else {
place_and_shift_up({dist_and_fingerprint, value_idx}, bucket_idx);
++value_idx;
}
}
}
template <class M, typename Q = T, std::enable_if_t<is_map_v<Q>, bool> = true>
auto insert_or_assign(Key const& key, M&& mapped) -> std::pair<iterator, bool> {
return do_insert_or_assign(key, std::forward<M>(mapped));
}
template <class M, typename Q = T, std::enable_if_t<is_map_v<Q>, bool> = true>
auto insert_or_assign(Key&& key, M&& mapped) -> std::pair<iterator, bool> {
return do_insert_or_assign(std::move(key), std::forward<M>(mapped));
}
template <typename K,
typename M,
typename Q = T,
typename H = Hash,
typename KE = KeyEqual,
std::enable_if_t<is_map_v<Q> && is_transparent_v<H, KE>, bool> = true>
auto insert_or_assign(K&& key, M&& mapped) -> std::pair<iterator, bool> {
return do_insert_or_assign(std::forward<K>(key), std::forward<M>(mapped));
}
template <class M, typename Q = T, std::enable_if_t<is_map_v<Q>, bool> = true>
auto insert_or_assign(const_iterator /*hint*/, Key const& key, M&& mapped) -> iterator {
return do_insert_or_assign(key, std::forward<M>(mapped)).first;
}
template <class M, typename Q = T, std::enable_if_t<is_map_v<Q>, bool> = true>
auto insert_or_assign(const_iterator /*hint*/, Key&& key, M&& mapped) -> iterator {
return do_insert_or_assign(std::move(key), std::forward<M>(mapped)).first;
}
template <typename K,
typename M,
typename Q = T,
typename H = Hash,
typename KE = KeyEqual,
std::enable_if_t<is_map_v<Q> && is_transparent_v<H, KE>, bool> = true>
auto insert_or_assign(const_iterator /*hint*/, K&& key, M&& mapped) -> iterator {
return do_insert_or_assign(std::forward<K>(key), std::forward<M>(mapped)).first;
}
// Single arguments for unordered_set can be used without having to construct the value_type
template <class K,
typename Q = T,
typename H = Hash,
typename KE = KeyEqual,
std::enable_if_t<!is_map_v<Q> && is_transparent_v<H, KE>, bool> = true>
auto emplace(K&& key) -> std::pair<iterator, bool> {
auto hash = mixed_hash(key);
auto dist_and_fingerprint = dist_and_fingerprint_from_hash(hash);
auto bucket_idx = bucket_idx_from_hash(hash);
while (dist_and_fingerprint <= at(m_buckets, bucket_idx).m_dist_and_fingerprint) {
if (dist_and_fingerprint == at(m_buckets, bucket_idx).m_dist_and_fingerprint &&
m_equal(key, m_values[at(m_buckets, bucket_idx).m_value_idx])) {
// found it, return without ever actually creating anything
return {begin() + static_cast<difference_type>(at(m_buckets, bucket_idx).m_value_idx), false};
}
dist_and_fingerprint = dist_inc(dist_and_fingerprint);
bucket_idx = next(bucket_idx);
}
// value is new, insert element first, so when exception happens we are in a valid state
return do_place_element(dist_and_fingerprint, bucket_idx, std::forward<K>(key));
}
template <class... Args>
auto emplace(Args&&... args) -> std::pair<iterator, bool> {
// we have to instantiate the value_type to be able to access the key.
// 1. emplace_back the object so it is constructed. 2. If the key is already there, pop it later in the loop.
auto& key = get_key(m_values.emplace_back(std::forward<Args>(args)...));
auto hash = mixed_hash(key);
auto dist_and_fingerprint = dist_and_fingerprint_from_hash(hash);
auto bucket_idx = bucket_idx_from_hash(hash);
while (dist_and_fingerprint <= at(m_buckets, bucket_idx).m_dist_and_fingerprint) {
if (dist_and_fingerprint == at(m_buckets, bucket_idx).m_dist_and_fingerprint &&
m_equal(key, get_key(m_values[at(m_buckets, bucket_idx).m_value_idx]))) {
m_values.pop_back(); // value was already there, so get rid of it
return {begin() + static_cast<difference_type>(at(m_buckets, bucket_idx).m_value_idx), false};
}
dist_and_fingerprint = dist_inc(dist_and_fingerprint);
bucket_idx = next(bucket_idx);
}
// value is new, place the bucket and shift up until we find an empty spot
auto value_idx = static_cast<value_idx_type>(m_values.size() - 1);
if (ANKERL_UNORDERED_DENSE_UNLIKELY(is_full())) {
// increase_size just rehashes all the data we have in m_values
increase_size();
} else {
// place element and shift up until we find an empty spot
place_and_shift_up({dist_and_fingerprint, value_idx}, bucket_idx);
}
return {begin() + static_cast<difference_type>(value_idx), true};
}
template <class... Args>
auto emplace_hint(const_iterator /*hint*/, Args&&... args) -> iterator {
return emplace(std::forward<Args>(args)...).first;
}
template <class... Args, typename Q = T, std::enable_if_t<is_map_v<Q>, bool> = true>
auto try_emplace(Key const& key, Args&&... args) -> std::pair<iterator, bool> {
return do_try_emplace(key, std::forward<Args>(args)...);
}
template <class... Args, typename Q = T, std::enable_if_t<is_map_v<Q>, bool> = true>
auto try_emplace(Key&& key, Args&&... args) -> std::pair<iterator, bool> {
return do_try_emplace(std::move(key), std::forward<Args>(args)...);
}
template <class... Args, typename Q = T, std::enable_if_t<is_map_v<Q>, bool> = true>
auto try_emplace(const_iterator /*hint*/, Key const& key, Args&&... args) -> iterator {
return do_try_emplace(key, std::forward<Args>(args)...).first;
}
template <class... Args, typename Q = T, std::enable_if_t<is_map_v<Q>, bool> = true>
auto try_emplace(const_iterator /*hint*/, Key&& key, Args&&... args) -> iterator {
return do_try_emplace(std::move(key), std::forward<Args>(args)...).first;
}
template <
typename K,
typename... Args,
typename Q = T,
typename H = Hash,
typename KE = KeyEqual,
std::enable_if_t<is_map_v<Q> && is_transparent_v<H, KE> && is_neither_convertible_v<K&&, iterator, const_iterator>,
bool> = true>
auto try_emplace(K&& key, Args&&... args) -> std::pair<iterator, bool> {
return do_try_emplace(std::forward<K>(key), std::forward<Args>(args)...);
}
template <
typename K,
typename... Args,
typename Q = T,
typename H = Hash,
typename KE = KeyEqual,
std::enable_if_t<is_map_v<Q> && is_transparent_v<H, KE> && is_neither_convertible_v<K&&, iterator, const_iterator>,
bool> = true>
auto try_emplace(const_iterator /*hint*/, K&& key, Args&&... args) -> iterator {
return do_try_emplace(std::forward<K>(key), std::forward<Args>(args)...).first;
}
auto erase(iterator it) -> iterator {
auto hash = mixed_hash(get_key(*it));
auto bucket_idx = bucket_idx_from_hash(hash);
auto const value_idx_to_remove = static_cast<value_idx_type>(it - cbegin());
while (at(m_buckets, bucket_idx).m_value_idx != value_idx_to_remove) {
bucket_idx = next(bucket_idx);
}
do_erase(bucket_idx, [](value_type&& /*unused*/) {
});
return begin() + static_cast<difference_type>(value_idx_to_remove);
}
auto extract(iterator it) -> value_type {
auto hash = mixed_hash(get_key(*it));
auto bucket_idx = bucket_idx_from_hash(hash);
auto const value_idx_to_remove = static_cast<value_idx_type>(it - cbegin());
while (at(m_buckets, bucket_idx).m_value_idx != value_idx_to_remove) {
bucket_idx = next(bucket_idx);
}
auto tmp = std::optional<value_type>{};
do_erase(bucket_idx, [&tmp](value_type&& val) {
tmp = std::move(val);
});
return std::move(tmp).value();
}
template <typename Q = T, std::enable_if_t<is_map_v<Q>, bool> = true>
auto erase(const_iterator it) -> iterator {
return erase(begin() + (it - cbegin()));
}
template <typename Q = T, std::enable_if_t<is_map_v<Q>, bool> = true>
auto extract(const_iterator it) -> value_type {
return extract(begin() + (it - cbegin()));
}
auto erase(const_iterator first, const_iterator last) -> iterator {
auto const idx_first = first - cbegin();
auto const idx_last = last - cbegin();
auto const first_to_last = std::distance(first, last);
auto const last_to_end = std::distance(last, cend());
// remove elements from left to right which moves elements from the end back
auto const mid = idx_first + (std::min)(first_to_last, last_to_end);
auto idx = idx_first;
while (idx != mid) {
erase(begin() + idx);
++idx;
}
// all elements from the right are moved, now remove the last element until all done
idx = idx_last;
while (idx != mid) {
--idx;
erase(begin() + idx);
}
return begin() + idx_first;
}
auto erase(Key const& key) -> size_t {
return do_erase_key(key, [](value_type&& /*unused*/) {
});
}
auto extract(Key const& key) -> std::optional<value_type> {
auto tmp = std::optional<value_type>{};
do_erase_key(key, [&tmp](value_type&& val) {
tmp = std::move(val);
});
return tmp;
}
template <class K, class H = Hash, class KE = KeyEqual, std::enable_if_t<is_transparent_v<H, KE>, bool> = true>
auto erase(K&& key) -> size_t {
return do_erase_key(std::forward<K>(key), [](value_type&& /*unused*/) {
});
}
template <class K, class H = Hash, class KE = KeyEqual, std::enable_if_t<is_transparent_v<H, KE>, bool> = true>
auto extract(K&& key) -> std::optional<value_type> {
auto tmp = std::optional<value_type>{};
do_erase_key(std::forward<K>(key), [&tmp](value_type&& val) {
tmp = std::move(val);
});
return tmp;
}
void swap(table& other) noexcept(noexcept(std::is_nothrow_swappable_v<value_container_type> &&
std::is_nothrow_swappable_v<Hash> && std::is_nothrow_swappable_v<KeyEqual>)) {
using std::swap;
swap(other, *this);
}
// lookup /////////////////////////////////////////////////////////////////
template <typename Q = T, std::enable_if_t<is_map_v<Q>, bool> = true>
auto at(key_type const& key) -> Q& {
return do_at(key);
}
template <typename K,
typename Q = T,
typename H = Hash,
typename KE = KeyEqual,
std::enable_if_t<is_map_v<Q> && is_transparent_v<H, KE>, bool> = true>
auto at(K const& key) -> Q& {
return do_at(key);
}
template <typename Q = T, std::enable_if_t<is_map_v<Q>, bool> = true>
auto at(key_type const& key) const -> Q const& {
return do_at(key);
}
template <typename K,
typename Q = T,
typename H = Hash,
typename KE = KeyEqual,
std::enable_if_t<is_map_v<Q> && is_transparent_v<H, KE>, bool> = true>
auto at(K const& key) const -> Q const& {
return do_at(key);
}
template <typename Q = T, std::enable_if_t<is_map_v<Q>, bool> = true>
auto operator[](Key const& key) -> Q& {
return try_emplace(key).first->second;
}
template <typename Q = T, std::enable_if_t<is_map_v<Q>, bool> = true>
auto operator[](Key&& key) -> Q& {
return try_emplace(std::move(key)).first->second;
}
template <typename K,
typename Q = T,
typename H = Hash,
typename KE = KeyEqual,
std::enable_if_t<is_map_v<Q> && is_transparent_v<H, KE>, bool> = true>
auto operator[](K&& key) -> Q& {
return try_emplace(std::forward<K>(key)).first->second;
}
auto count(Key const& key) const -> size_t {
return find(key) == end() ? 0 : 1;
}
template <class K, class H = Hash, class KE = KeyEqual, std::enable_if_t<is_transparent_v<H, KE>, bool> = true>
auto count(K const& key) const -> size_t {
return find(key) == end() ? 0 : 1;
}
auto find(Key const& key) -> iterator {
return do_find(key);
}
auto find(Key const& key) const -> const_iterator {
return do_find(key);
}
template <class K, class H = Hash, class KE = KeyEqual, std::enable_if_t<is_transparent_v<H, KE>, bool> = true>
auto find(K const& key) -> iterator {
return do_find(key);
}
template <class K, class H = Hash, class KE = KeyEqual, std::enable_if_t<is_transparent_v<H, KE>, bool> = true>
auto find(K const& key) const -> const_iterator {
return do_find(key);
}
auto contains(Key const& key) const -> bool {
return find(key) != end();
}
template <class K, class H = Hash, class KE = KeyEqual, std::enable_if_t<is_transparent_v<H, KE>, bool> = true>
auto contains(K const& key) const -> bool {
return find(key) != end();
}
auto equal_range(Key const& key) -> std::pair<iterator, iterator> {
auto it = do_find(key);
return {it, it == end() ? end() : it + 1};
}
auto equal_range(const Key& key) const -> std::pair<const_iterator, const_iterator> {
auto it = do_find(key);
return {it, it == end() ? end() : it + 1};
}
template <class K, class H = Hash, class KE = KeyEqual, std::enable_if_t<is_transparent_v<H, KE>, bool> = true>
auto equal_range(K const& key) -> std::pair<iterator, iterator> {
auto it = do_find(key);
return {it, it == end() ? end() : it + 1};
}
template <class K, class H = Hash, class KE = KeyEqual, std::enable_if_t<is_transparent_v<H, KE>, bool> = true>
auto equal_range(K const& key) const -> std::pair<const_iterator, const_iterator> {
auto it = do_find(key);
return {it, it == end() ? end() : it + 1};
}
// bucket interface ///////////////////////////////////////////////////////
auto bucket_count() const noexcept -> size_t { // NOLINT(modernize-use-nodiscard)
return m_num_buckets;
}
static constexpr auto max_bucket_count() noexcept -> size_t { // NOLINT(modernize-use-nodiscard)
return max_size();
}
// hash policy ////////////////////////////////////////////////////////////
[[nodiscard]] auto load_factor() const -> float {
return bucket_count() ? static_cast<float>(size()) / static_cast<float>(bucket_count()) : 0.0F;
}
[[nodiscard]] auto max_load_factor() const -> float {
return m_max_load_factor;
}
void max_load_factor(float ml) {
m_max_load_factor = ml;
if (m_num_buckets != max_bucket_count()) {
m_max_bucket_capacity = static_cast<value_idx_type>(static_cast<float>(bucket_count()) * max_load_factor());
}
}
void rehash(size_t count) {
count = (std::min)(count, max_size());
auto shifts = calc_shifts_for_size((std::max)(count, size()));
if (shifts != m_shifts) {
m_shifts = shifts;
deallocate_buckets();
m_values.shrink_to_fit();
allocate_buckets_from_shift();
clear_and_fill_buckets_from_values();
}
}
void reserve(size_t capa) {
capa = (std::min)(capa, max_size());
if constexpr (has_reserve<value_container_type>) {
// std::deque doesn't have reserve(). Make sure we only call when available
m_values.reserve(capa);
}
auto shifts = calc_shifts_for_size((std::max)(capa, size()));
if (0 == m_num_buckets || shifts < m_shifts) {
m_shifts = shifts;
deallocate_buckets();
allocate_buckets_from_shift();
clear_and_fill_buckets_from_values();
}
}
// observers //////////////////////////////////////////////////////////////
auto hash_function() const -> hasher {
return m_hash;
}
auto key_eq() const -> key_equal {
return m_equal;
}
// nonstandard API: expose the underlying values container
[[nodiscard]] auto values() const noexcept -> value_container_type const& {
return m_values;
}
// non-member functions ///////////////////////////////////////////////////
friend auto operator==(table const& a, table const& b) -> bool {
if (&a == &b) {
return true;
}
if (a.size() != b.size()) {
return false;
}
for (auto const& b_entry : b) {
auto it = a.find(get_key(b_entry));
if constexpr (is_map_v<T>) {
// map: check that key is here, then also check that value is the same
if (a.end() == it || !(b_entry.second == it->second)) {
return false;
}
} else {
// set: only check that the key is here
if (a.end() == it) {
return false;
}
}
}
return true;
}
friend auto operator!=(table const& a, table const& b) -> bool {
return !(a == b);
}
};
} // namespace detail
ANKERL_UNORDERED_DENSE_EXPORT template <class Key,
class T,
class Hash = hash<Key>,
class KeyEqual = std::equal_to<Key>,
class AllocatorOrContainer = std::allocator<std::pair<Key, T>>,
class Bucket = bucket_type::standard>
using map = detail::table<Key, T, Hash, KeyEqual, AllocatorOrContainer, Bucket, false>;
ANKERL_UNORDERED_DENSE_EXPORT template <class Key,
class T,
class Hash = hash<Key>,
class KeyEqual = std::equal_to<Key>,
class AllocatorOrContainer = std::allocator<std::pair<Key, T>>,
class Bucket = bucket_type::standard>
using segmented_map = detail::table<Key, T, Hash, KeyEqual, AllocatorOrContainer, Bucket, true>;
ANKERL_UNORDERED_DENSE_EXPORT template <class Key,
class Hash = hash<Key>,
class KeyEqual = std::equal_to<Key>,
class AllocatorOrContainer = std::allocator<Key>,
class Bucket = bucket_type::standard>
using set = detail::table<Key, void, Hash, KeyEqual, AllocatorOrContainer, Bucket, false>;
ANKERL_UNORDERED_DENSE_EXPORT template <class Key,
class Hash = hash<Key>,
class KeyEqual = std::equal_to<Key>,
class AllocatorOrContainer = std::allocator<Key>,
class Bucket = bucket_type::standard>
using segmented_set = detail::table<Key, void, Hash, KeyEqual, AllocatorOrContainer, Bucket, true>;
# if defined(ANKERL_UNORDERED_DENSE_PMR)
namespace pmr {
ANKERL_UNORDERED_DENSE_EXPORT template <class Key,
class T,
class Hash = hash<Key>,
class KeyEqual = std::equal_to<Key>,
class Bucket = bucket_type::standard>
using map =
detail::table<Key, T, Hash, KeyEqual, ANKERL_UNORDERED_DENSE_PMR::polymorphic_allocator<std::pair<Key, T>>, Bucket, false>;
ANKERL_UNORDERED_DENSE_EXPORT template <class Key,
class T,
class Hash = hash<Key>,
class KeyEqual = std::equal_to<Key>,
class Bucket = bucket_type::standard>
using segmented_map =
detail::table<Key, T, Hash, KeyEqual, ANKERL_UNORDERED_DENSE_PMR::polymorphic_allocator<std::pair<Key, T>>, Bucket, true>;
ANKERL_UNORDERED_DENSE_EXPORT template <class Key,
class Hash = hash<Key>,
class KeyEqual = std::equal_to<Key>,
class Bucket = bucket_type::standard>
using set = detail::table<Key, void, Hash, KeyEqual, ANKERL_UNORDERED_DENSE_PMR::polymorphic_allocator<Key>, Bucket, false>;
ANKERL_UNORDERED_DENSE_EXPORT template <class Key,
class Hash = hash<Key>,
class KeyEqual = std::equal_to<Key>,
class Bucket = bucket_type::standard>
using segmented_set =
detail::table<Key, void, Hash, KeyEqual, ANKERL_UNORDERED_DENSE_PMR::polymorphic_allocator<Key>, Bucket, true>;
} // namespace pmr
# endif
// deduction guides ///////////////////////////////////////////////////////////
// deduction guides for alias templates are only possible since C++20
// see https://en.cppreference.com/w/cpp/language/class_template_argument_deduction
} // namespace ANKERL_UNORDERED_DENSE_NAMESPACE
} // namespace ankerl::unordered_dense
// std extensions /////////////////////////////////////////////////////////////
namespace std { // NOLINT(cert-dcl58-cpp)
ANKERL_UNORDERED_DENSE_EXPORT template <class Key,
class T,
class Hash,
class KeyEqual,
class AllocatorOrContainer,
class Bucket,
class Pred,
bool IsSegmented>
// NOLINTNEXTLINE(cert-dcl58-cpp)
auto erase_if(ankerl::unordered_dense::detail::table<Key, T, Hash, KeyEqual, AllocatorOrContainer, Bucket, IsSegmented>& map,
Pred pred) -> size_t {
using map_t = ankerl::unordered_dense::detail::table<Key, T, Hash, KeyEqual, AllocatorOrContainer, Bucket, IsSegmented>;
// going back to front because erase() invalidates the end iterator
auto const old_size = map.size();
auto idx = old_size;
while (idx) {
--idx;
auto it = map.begin() + static_cast<typename map_t::difference_type>(idx);
if (pred(*it)) {
map.erase(it);
}
}
return old_size - map.size();
}
} // namespace std
#endif
#endif
|