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 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104
|
// Taken from
// https://github.com/skarupke/flat_hash_map/blob/2c4687431f978f02a3780e24b8b701d22aa32d9c/flat_hash_map.hpp
// with fixes applied:
// - https://github.com/skarupke/flat_hash_map/pull/25
// - https://github.com/skarupke/flat_hash_map/pull/26
// - replace size_t with uint64_t to fix it for 32bit
// - add "GCC diagnostic" pragma to ignore -Wshadow
// - make sherwood_v3_table::convertible_to_iterator public because GCC5 seems
// to have issues with it otherwise
// - fix compiler warnings in operator templated_iterator<const value_type>
// Copyright Malte Skarupke 2017.
// Distributed under the Boost Software License, Version 1.0.
// (See http://www.boost.org/LICENSE_1_0.txt)
#pragma once
#include <c10/macros/Macros.h>
#include <algorithm>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <iterator>
#include <stdexcept>
#include <type_traits>
#include <utility>
C10_CLANG_DIAGNOSTIC_PUSH()
#if C10_CLANG_HAS_WARNING("-Wimplicit-int-float-conversion")
C10_CLANG_DIAGNOSTIC_IGNORE("-Wimplicit-int-float-conversion")
#endif
#ifdef _MSC_VER
#define SKA_NOINLINE(...) __declspec(noinline) __VA_ARGS__
#else
#define SKA_NOINLINE(...) __VA_ARGS__ __attribute__((noinline))
#endif
namespace ska {
struct prime_number_hash_policy;
struct power_of_two_hash_policy;
struct fibonacci_hash_policy;
namespace detailv3 {
template <typename Result, typename Functor>
struct functor_storage : Functor {
functor_storage() = default;
functor_storage(const Functor& functor) : Functor(functor) {}
template <typename... Args>
Result operator()(Args&&... args) {
return static_cast<Functor&>(*this)(std::forward<Args>(args)...);
}
template <typename... Args>
Result operator()(Args&&... args) const {
return static_cast<const Functor&>(*this)(std::forward<Args>(args)...);
}
};
template <typename Result, typename... Args>
struct functor_storage<Result, Result (*)(Args...)> {
typedef Result (*function_ptr)(Args...);
function_ptr function;
functor_storage(function_ptr function) : function(function) {}
Result operator()(Args... args) const {
return function(std::forward<Args>(args)...);
}
operator function_ptr&() {
return function;
}
operator const function_ptr&() {
return function;
}
};
template <typename key_type, typename value_type, typename hasher>
struct KeyOrValueHasher : functor_storage<uint64_t, hasher> {
typedef functor_storage<uint64_t, hasher> hasher_storage;
KeyOrValueHasher() = default;
KeyOrValueHasher(const hasher& hash) : hasher_storage(hash) {}
uint64_t operator()(const key_type& key) {
return static_cast<hasher_storage&>(*this)(key);
}
uint64_t operator()(const key_type& key) const {
return static_cast<const hasher_storage&>(*this)(key);
}
uint64_t operator()(const value_type& value) {
return static_cast<hasher_storage&>(*this)(value.first);
}
uint64_t operator()(const value_type& value) const {
return static_cast<const hasher_storage&>(*this)(value.first);
}
template <typename F, typename S>
uint64_t operator()(const std::pair<F, S>& value) {
return static_cast<hasher_storage&>(*this)(value.first);
}
template <typename F, typename S>
uint64_t operator()(const std::pair<F, S>& value) const {
return static_cast<const hasher_storage&>(*this)(value.first);
}
};
template <typename key_type, typename value_type, typename key_equal>
struct KeyOrValueEquality : functor_storage<bool, key_equal> {
typedef functor_storage<bool, key_equal> equality_storage;
KeyOrValueEquality() = default;
KeyOrValueEquality(const key_equal& equality) : equality_storage(equality) {}
bool operator()(const key_type& lhs, const key_type& rhs) {
return static_cast<equality_storage&>(*this)(lhs, rhs);
}
bool operator()(const key_type& lhs, const value_type& rhs) {
return static_cast<equality_storage&>(*this)(lhs, rhs.first);
}
bool operator()(const value_type& lhs, const key_type& rhs) {
return static_cast<equality_storage&>(*this)(lhs.first, rhs);
}
bool operator()(const value_type& lhs, const value_type& rhs) {
return static_cast<equality_storage&>(*this)(lhs.first, rhs.first);
}
template <typename F, typename S>
bool operator()(const key_type& lhs, const std::pair<F, S>& rhs) {
return static_cast<equality_storage&>(*this)(lhs, rhs.first);
}
template <typename F, typename S>
bool operator()(const std::pair<F, S>& lhs, const key_type& rhs) {
return static_cast<equality_storage&>(*this)(lhs.first, rhs);
}
template <typename F, typename S>
bool operator()(const value_type& lhs, const std::pair<F, S>& rhs) {
return static_cast<equality_storage&>(*this)(lhs.first, rhs.first);
}
template <typename F, typename S>
bool operator()(const std::pair<F, S>& lhs, const value_type& rhs) {
return static_cast<equality_storage&>(*this)(lhs.first, rhs.first);
}
template <typename FL, typename SL, typename FR, typename SR>
bool operator()(const std::pair<FL, SL>& lhs, const std::pair<FR, SR>& rhs) {
return static_cast<equality_storage&>(*this)(lhs.first, rhs.first);
}
};
static constexpr int8_t min_lookups = 4;
template <typename T>
struct sherwood_v3_entry {
sherwood_v3_entry() {}
sherwood_v3_entry(int8_t distance_from_desired)
: distance_from_desired(distance_from_desired) {}
~sherwood_v3_entry() {}
bool has_value() const {
return distance_from_desired >= 0;
}
bool is_empty() const {
return distance_from_desired < 0;
}
bool is_at_desired_position() const {
return distance_from_desired <= 0;
}
template <typename... Args>
void emplace(int8_t distance, Args&&... args) {
new (std::addressof(value)) T(std::forward<Args>(args)...);
distance_from_desired = distance;
}
void destroy_value() {
value.~T();
distance_from_desired = -1;
}
int8_t distance_from_desired = -1;
static constexpr int8_t special_end_value = 0;
union {
T value;
};
};
inline int8_t log2(uint64_t value) {
static constexpr int8_t table[64] = {
63, 0, 58, 1, 59, 47, 53, 2, 60, 39, 48, 27, 54, 33, 42, 3,
61, 51, 37, 40, 49, 18, 28, 20, 55, 30, 34, 11, 43, 14, 22, 4,
62, 57, 46, 52, 38, 26, 32, 41, 50, 36, 17, 19, 29, 10, 13, 21,
56, 45, 25, 31, 35, 16, 9, 12, 44, 24, 15, 8, 23, 7, 6, 5};
value |= value >> 1;
value |= value >> 2;
value |= value >> 4;
value |= value >> 8;
value |= value >> 16;
value |= value >> 32;
return table[((value - (value >> 1)) * 0x07EDD5E59A4E28C2) >> 58];
}
template <typename T, bool>
struct AssignIfTrue {
void operator()(T& lhs, const T& rhs) {
lhs = rhs;
}
void operator()(T& lhs, T&& rhs) {
lhs = std::move(rhs);
}
};
template <typename T>
struct AssignIfTrue<T, false> {
void operator()(T&, const T&) {}
void operator()(T&, T&&) {}
};
inline uint64_t next_power_of_two(uint64_t i) {
--i;
i |= i >> 1;
i |= i >> 2;
i |= i >> 4;
i |= i >> 8;
i |= i >> 16;
i |= i >> 32;
++i;
return i;
}
// Implementation taken from http://en.cppreference.com/w/cpp/types/void_t
// (it takes CWG1558 into account and also works for older compilers)
template <typename... Ts>
struct make_void {
typedef void type;
};
template <typename... Ts>
using void_t = typename make_void<Ts...>::type;
template <typename T, typename = void>
struct HashPolicySelector {
typedef fibonacci_hash_policy type;
};
template <typename T>
struct HashPolicySelector<T, void_t<typename T::hash_policy>> {
typedef typename T::hash_policy type;
};
template <
typename T,
typename FindKey,
typename ArgumentHash,
typename Hasher,
typename ArgumentEqual,
typename Equal,
typename ArgumentAlloc,
typename EntryAlloc>
class sherwood_v3_table : private EntryAlloc, private Hasher, private Equal {
using Entry = detailv3::sherwood_v3_entry<T>;
using AllocatorTraits = std::allocator_traits<EntryAlloc>;
using EntryPointer = typename AllocatorTraits::pointer;
public:
struct convertible_to_iterator;
using value_type = T;
using size_type = uint64_t;
using difference_type = std::ptrdiff_t;
using hasher = ArgumentHash;
using key_equal = ArgumentEqual;
using allocator_type = EntryAlloc;
using reference = value_type&;
using const_reference = const value_type&;
using pointer = value_type*;
using const_pointer = const value_type*;
sherwood_v3_table() {}
explicit sherwood_v3_table(
size_type bucket_count,
const ArgumentHash& hash = ArgumentHash(),
const ArgumentEqual& equal = ArgumentEqual(),
const ArgumentAlloc& alloc = ArgumentAlloc())
: EntryAlloc(alloc), Hasher(hash), Equal(equal) {
rehash(bucket_count);
}
sherwood_v3_table(size_type bucket_count, const ArgumentAlloc& alloc)
: sherwood_v3_table(
bucket_count,
ArgumentHash(),
ArgumentEqual(),
alloc) {}
sherwood_v3_table(
size_type bucket_count,
const ArgumentHash& hash,
const ArgumentAlloc& alloc)
: sherwood_v3_table(bucket_count, hash, ArgumentEqual(), alloc) {}
explicit sherwood_v3_table(const ArgumentAlloc& alloc) : EntryAlloc(alloc) {}
template <typename It>
sherwood_v3_table(
It first,
It last,
size_type bucket_count = 0,
const ArgumentHash& hash = ArgumentHash(),
const ArgumentEqual& equal = ArgumentEqual(),
const ArgumentAlloc& alloc = ArgumentAlloc())
: sherwood_v3_table(bucket_count, hash, equal, alloc) {
insert(first, last);
}
template <typename It>
sherwood_v3_table(
It first,
It last,
size_type bucket_count,
const ArgumentAlloc& alloc)
: sherwood_v3_table(
first,
last,
bucket_count,
ArgumentHash(),
ArgumentEqual(),
alloc) {}
template <typename It>
sherwood_v3_table(
It first,
It last,
size_type bucket_count,
const ArgumentHash& hash,
const ArgumentAlloc& alloc)
: sherwood_v3_table(
first,
last,
bucket_count,
hash,
ArgumentEqual(),
alloc) {}
sherwood_v3_table(
std::initializer_list<T> il,
size_type bucket_count = 0,
const ArgumentHash& hash = ArgumentHash(),
const ArgumentEqual& equal = ArgumentEqual(),
const ArgumentAlloc& alloc = ArgumentAlloc())
: sherwood_v3_table(bucket_count, hash, equal, alloc) {
if (bucket_count == 0)
rehash(il.size());
insert(il.begin(), il.end());
}
sherwood_v3_table(
std::initializer_list<T> il,
size_type bucket_count,
const ArgumentAlloc& alloc)
: sherwood_v3_table(
il,
bucket_count,
ArgumentHash(),
ArgumentEqual(),
alloc) {}
sherwood_v3_table(
std::initializer_list<T> il,
size_type bucket_count,
const ArgumentHash& hash,
const ArgumentAlloc& alloc)
: sherwood_v3_table(il, bucket_count, hash, ArgumentEqual(), alloc) {}
sherwood_v3_table(const sherwood_v3_table& other)
: sherwood_v3_table(
other,
AllocatorTraits::select_on_container_copy_construction(
other.get_allocator())) {}
sherwood_v3_table(const sherwood_v3_table& other, const ArgumentAlloc& alloc)
: EntryAlloc(alloc),
Hasher(other),
Equal(other),
_max_load_factor(other._max_load_factor) {
rehash_for_other_container(other);
try {
insert(other.begin(), other.end());
} catch (...) {
clear();
deallocate_data(entries, num_slots_minus_one, max_lookups);
throw;
}
}
sherwood_v3_table(sherwood_v3_table&& other) noexcept
: EntryAlloc(std::move(other)),
Hasher(std::move(other)),
Equal(std::move(other)) {
swap_pointers(other);
}
sherwood_v3_table(
sherwood_v3_table&& other,
const ArgumentAlloc& alloc) noexcept
: EntryAlloc(alloc), Hasher(std::move(other)), Equal(std::move(other)) {
swap_pointers(other);
}
sherwood_v3_table& operator=(const sherwood_v3_table& other) {
if (this == std::addressof(other))
return *this;
clear();
if (AllocatorTraits::propagate_on_container_copy_assignment::value) {
if (static_cast<EntryAlloc&>(*this) !=
static_cast<const EntryAlloc&>(other)) {
reset_to_empty_state();
}
AssignIfTrue<
EntryAlloc,
AllocatorTraits::propagate_on_container_copy_assignment::value>()(
*this, other);
}
_max_load_factor = other._max_load_factor;
static_cast<Hasher&>(*this) = other;
static_cast<Equal&>(*this) = other;
rehash_for_other_container(other);
insert(other.begin(), other.end());
return *this;
}
sherwood_v3_table& operator=(sherwood_v3_table&& other) noexcept {
if (this == std::addressof(other))
return *this;
else if (AllocatorTraits::propagate_on_container_move_assignment::value) {
clear();
reset_to_empty_state();
AssignIfTrue<
EntryAlloc,
AllocatorTraits::propagate_on_container_move_assignment::value>()(
*this, std::move(other));
swap_pointers(other);
} else if (
static_cast<EntryAlloc&>(*this) == static_cast<EntryAlloc&>(other)) {
swap_pointers(other);
} else {
clear();
_max_load_factor = other._max_load_factor;
rehash_for_other_container(other);
for (T& elem : other)
emplace(std::move(elem));
other.clear();
}
static_cast<Hasher&>(*this) = std::move(other);
static_cast<Equal&>(*this) = std::move(other);
return *this;
}
~sherwood_v3_table() {
clear();
deallocate_data(entries, num_slots_minus_one, max_lookups);
}
const allocator_type& get_allocator() const {
return static_cast<const allocator_type&>(*this);
}
const ArgumentEqual& key_eq() const {
return static_cast<const ArgumentEqual&>(*this);
}
const ArgumentHash& hash_function() const {
return static_cast<const ArgumentHash&>(*this);
}
template <typename ValueType>
struct templated_iterator {
templated_iterator() = default;
templated_iterator(EntryPointer current) : current(current) {}
EntryPointer current = EntryPointer();
using iterator_category = std::forward_iterator_tag;
using value_type = ValueType;
using difference_type = ptrdiff_t;
using pointer = ValueType*;
using reference = ValueType&;
friend bool operator==(
const templated_iterator& lhs,
const templated_iterator& rhs) {
return lhs.current == rhs.current;
}
friend bool operator!=(
const templated_iterator& lhs,
const templated_iterator& rhs) {
return !(lhs == rhs);
}
templated_iterator& operator++() {
do {
++current;
} while (current->is_empty());
return *this;
}
templated_iterator operator++(int) {
templated_iterator copy(*this);
++*this;
return copy;
}
ValueType& operator*() const {
return current->value;
}
ValueType* operator->() const {
return std::addressof(current->value);
}
// the template automatically disables the operator when value_type is
// already const, because that would cause a lot of compiler warnings
// otherwise.
template <
class target_type = const value_type,
class = typename std::enable_if<
std::is_same<target_type, const value_type>::value &&
!std::is_same<target_type, value_type>::value>::type>
operator templated_iterator<target_type>() const {
return {current};
}
};
using iterator = templated_iterator<value_type>;
using const_iterator = templated_iterator<const value_type>;
iterator begin() {
for (EntryPointer it = entries;; ++it) {
if (it->has_value())
return {it};
}
}
const_iterator begin() const {
for (EntryPointer it = entries;; ++it) {
if (it->has_value())
return {it};
}
}
const_iterator cbegin() const {
return begin();
}
iterator end() {
return {
entries + static_cast<ptrdiff_t>(num_slots_minus_one + max_lookups)};
}
const_iterator end() const {
return {
entries + static_cast<ptrdiff_t>(num_slots_minus_one + max_lookups)};
}
const_iterator cend() const {
return end();
}
iterator find(const FindKey& key) {
uint64_t index =
hash_policy.index_for_hash(hash_object(key), num_slots_minus_one);
EntryPointer it = entries + ptrdiff_t(index);
for (int8_t distance = 0; it->distance_from_desired >= distance;
++distance, ++it) {
if (compares_equal(key, it->value))
return {it};
}
return end();
}
const_iterator find(const FindKey& key) const {
return const_cast<sherwood_v3_table*>(this)->find(key);
}
uint64_t count(const FindKey& key) const {
return find(key) == end() ? 0 : 1;
}
std::pair<iterator, iterator> equal_range(const FindKey& key) {
iterator found = find(key);
if (found == end())
return {found, found};
else
return {found, std::next(found)};
}
std::pair<const_iterator, const_iterator> equal_range(
const FindKey& key) const {
const_iterator found = find(key);
if (found == end())
return {found, found};
else
return {found, std::next(found)};
}
template <typename Key, typename... Args>
std::pair<iterator, bool> emplace(Key&& key, Args&&... args) {
uint64_t index =
hash_policy.index_for_hash(hash_object(key), num_slots_minus_one);
EntryPointer current_entry = entries + ptrdiff_t(index);
int8_t distance_from_desired = 0;
for (; current_entry->distance_from_desired >= distance_from_desired;
++current_entry, ++distance_from_desired) {
if (compares_equal(key, current_entry->value))
return {{current_entry}, false};
}
return emplace_new_key(
distance_from_desired,
current_entry,
std::forward<Key>(key),
std::forward<Args>(args)...);
}
std::pair<iterator, bool> insert(const value_type& value) {
return emplace(value);
}
std::pair<iterator, bool> insert(value_type&& value) {
return emplace(std::move(value));
}
template <typename... Args>
iterator emplace_hint(const_iterator, Args&&... args) {
return emplace(std::forward<Args>(args)...).first;
}
iterator insert(const_iterator, const value_type& value) {
return emplace(value).first;
}
iterator insert(const_iterator, value_type&& value) {
return emplace(std::move(value)).first;
}
template <typename It>
void insert(It begin, It end) {
for (; begin != end; ++begin) {
emplace(*begin);
}
}
void insert(std::initializer_list<value_type> il) {
insert(il.begin(), il.end());
}
void rehash(uint64_t num_buckets) {
num_buckets = std::max(
num_buckets,
static_cast<uint64_t>(
std::ceil(num_elements / static_cast<double>(_max_load_factor))));
if (num_buckets == 0) {
reset_to_empty_state();
return;
}
auto new_prime_index = hash_policy.next_size_over(num_buckets);
if (num_buckets == bucket_count())
return;
int8_t new_max_lookups = compute_max_lookups(num_buckets);
EntryPointer new_buckets(
AllocatorTraits::allocate(*this, num_buckets + new_max_lookups));
EntryPointer special_end_item =
new_buckets + static_cast<ptrdiff_t>(num_buckets + new_max_lookups - 1);
for (EntryPointer it = new_buckets; it != special_end_item; ++it)
it->distance_from_desired = -1;
special_end_item->distance_from_desired = Entry::special_end_value;
std::swap(entries, new_buckets);
std::swap(num_slots_minus_one, num_buckets);
--num_slots_minus_one;
hash_policy.commit(new_prime_index);
int8_t old_max_lookups = max_lookups;
max_lookups = new_max_lookups;
num_elements = 0;
for (EntryPointer
it = new_buckets,
end = it + static_cast<ptrdiff_t>(num_buckets + old_max_lookups);
it != end;
++it) {
if (it->has_value()) {
emplace(std::move(it->value));
it->destroy_value();
}
}
deallocate_data(new_buckets, num_buckets, old_max_lookups);
}
void reserve(uint64_t num_elements_) {
uint64_t required_buckets = num_buckets_for_reserve(num_elements_);
if (required_buckets > bucket_count())
rehash(required_buckets);
}
// the return value is a type that can be converted to an iterator
// the reason for doing this is that it's not free to find the
// iterator pointing at the next element. if you care about the
// next iterator, turn the return value into an iterator
convertible_to_iterator erase(const_iterator to_erase) {
EntryPointer current = to_erase.current;
current->destroy_value();
--num_elements;
for (EntryPointer next = current + ptrdiff_t(1);
!next->is_at_desired_position();
++current, ++next) {
current->emplace(next->distance_from_desired - 1, std::move(next->value));
next->destroy_value();
}
return {to_erase.current};
}
iterator erase(const_iterator begin_it, const_iterator end_it) {
if (begin_it == end_it)
return {begin_it.current};
for (EntryPointer it = begin_it.current, end = end_it.current; it != end;
++it) {
if (it->has_value()) {
it->destroy_value();
--num_elements;
}
}
if (end_it == this->end())
return this->end();
ptrdiff_t num_to_move = std::min(
static_cast<ptrdiff_t>(end_it.current->distance_from_desired),
end_it.current - begin_it.current);
EntryPointer to_return = end_it.current - num_to_move;
for (EntryPointer it = end_it.current; !it->is_at_desired_position();) {
EntryPointer target = it - num_to_move;
target->emplace(
it->distance_from_desired - num_to_move, std::move(it->value));
it->destroy_value();
++it;
num_to_move = std::min(
static_cast<ptrdiff_t>(it->distance_from_desired), num_to_move);
}
return {to_return};
}
uint64_t erase(const FindKey& key) {
auto found = find(key);
if (found == end())
return 0;
else {
erase(found);
return 1;
}
}
void clear() {
for (EntryPointer it = entries,
end = it +
static_cast<ptrdiff_t>(num_slots_minus_one + max_lookups);
it != end;
++it) {
if (it->has_value())
it->destroy_value();
}
num_elements = 0;
}
void shrink_to_fit() {
rehash_for_other_container(*this);
}
void swap(sherwood_v3_table& other) {
using std::swap;
swap_pointers(other);
swap(static_cast<ArgumentHash&>(*this), static_cast<ArgumentHash&>(other));
swap(
static_cast<ArgumentEqual&>(*this), static_cast<ArgumentEqual&>(other));
if (AllocatorTraits::propagate_on_container_swap::value)
swap(static_cast<EntryAlloc&>(*this), static_cast<EntryAlloc&>(other));
}
uint64_t size() const {
return num_elements;
}
uint64_t max_size() const {
return (AllocatorTraits::max_size(*this)) / sizeof(Entry);
}
uint64_t bucket_count() const {
return num_slots_minus_one ? num_slots_minus_one + 1 : 0;
}
size_type max_bucket_count() const {
return (AllocatorTraits::max_size(*this) - min_lookups) / sizeof(Entry);
}
uint64_t bucket(const FindKey& key) const {
return hash_policy.index_for_hash(hash_object(key), num_slots_minus_one);
}
float load_factor() const {
uint64_t buckets = bucket_count();
if (buckets)
return static_cast<float>(num_elements) / bucket_count();
else
return 0;
}
void max_load_factor(float value) {
_max_load_factor = value;
}
float max_load_factor() const {
return _max_load_factor;
}
bool empty() const {
return num_elements == 0;
}
private:
EntryPointer entries = empty_default_table();
uint64_t num_slots_minus_one = 0;
typename HashPolicySelector<ArgumentHash>::type hash_policy;
int8_t max_lookups = detailv3::min_lookups - 1;
float _max_load_factor = 0.5f;
uint64_t num_elements = 0;
EntryPointer empty_default_table() {
EntryPointer result =
AllocatorTraits::allocate(*this, detailv3::min_lookups);
EntryPointer special_end_item =
result + static_cast<ptrdiff_t>(detailv3::min_lookups - 1);
for (EntryPointer it = result; it != special_end_item; ++it)
it->distance_from_desired = -1;
special_end_item->distance_from_desired = Entry::special_end_value;
return result;
}
static int8_t compute_max_lookups(uint64_t num_buckets) {
int8_t desired = detailv3::log2(num_buckets);
return std::max(detailv3::min_lookups, desired);
}
uint64_t num_buckets_for_reserve(uint64_t num_elements_) const {
return static_cast<uint64_t>(std::ceil(
num_elements_ / std::min(0.5, static_cast<double>(_max_load_factor))));
}
void rehash_for_other_container(const sherwood_v3_table& other) {
rehash(
std::min(num_buckets_for_reserve(other.size()), other.bucket_count()));
}
void swap_pointers(sherwood_v3_table& other) {
using std::swap;
swap(hash_policy, other.hash_policy);
swap(entries, other.entries);
swap(num_slots_minus_one, other.num_slots_minus_one);
swap(num_elements, other.num_elements);
swap(max_lookups, other.max_lookups);
swap(_max_load_factor, other._max_load_factor);
}
template <typename Key, typename... Args>
SKA_NOINLINE(std::pair<iterator, bool>)
emplace_new_key(
int8_t distance_from_desired,
EntryPointer current_entry,
Key&& key,
Args&&... args) {
using std::swap;
if (num_slots_minus_one == 0 || distance_from_desired == max_lookups ||
num_elements + 1 >
(num_slots_minus_one + 1) * static_cast<double>(_max_load_factor)) {
grow();
return emplace(std::forward<Key>(key), std::forward<Args>(args)...);
} else if (current_entry->is_empty()) {
current_entry->emplace(
distance_from_desired,
std::forward<Key>(key),
std::forward<Args>(args)...);
++num_elements;
return {{current_entry}, true};
}
value_type to_insert(std::forward<Key>(key), std::forward<Args>(args)...);
swap(distance_from_desired, current_entry->distance_from_desired);
swap(to_insert, current_entry->value);
iterator result = {current_entry};
for (++distance_from_desired, ++current_entry;; ++current_entry) {
if (current_entry->is_empty()) {
current_entry->emplace(distance_from_desired, std::move(to_insert));
++num_elements;
return {result, true};
} else if (current_entry->distance_from_desired < distance_from_desired) {
swap(distance_from_desired, current_entry->distance_from_desired);
swap(to_insert, current_entry->value);
++distance_from_desired;
} else {
++distance_from_desired;
if (distance_from_desired == max_lookups) {
swap(to_insert, result.current->value);
grow();
return emplace(std::move(to_insert));
}
}
}
}
void grow() {
rehash(std::max(uint64_t(4), 2 * bucket_count()));
}
void deallocate_data(
EntryPointer begin,
uint64_t num_slots_minus_one_,
int8_t max_lookups_) {
AllocatorTraits::deallocate(
*this, begin, num_slots_minus_one_ + max_lookups_ + 1);
}
void reset_to_empty_state() {
deallocate_data(entries, num_slots_minus_one, max_lookups);
entries = empty_default_table();
num_slots_minus_one = 0;
hash_policy.reset();
max_lookups = detailv3::min_lookups - 1;
}
template <typename U>
uint64_t hash_object(const U& key) {
return static_cast<Hasher&>(*this)(key);
}
template <typename U>
uint64_t hash_object(const U& key) const {
return static_cast<const Hasher&>(*this)(key);
}
template <typename L, typename R>
bool compares_equal(const L& lhs, const R& rhs) {
return static_cast<Equal&>(*this)(lhs, rhs);
}
public:
struct convertible_to_iterator {
EntryPointer it;
operator iterator() {
if (it->has_value())
return {it};
else
return ++iterator{it};
}
operator const_iterator() {
if (it->has_value())
return {it};
else
return ++const_iterator{it};
}
};
};
} // namespace detailv3
struct prime_number_hash_policy {
static uint64_t mod0(uint64_t) {
return 0llu;
}
static uint64_t mod2(uint64_t hash) {
return hash % 2llu;
}
static uint64_t mod3(uint64_t hash) {
return hash % 3llu;
}
static uint64_t mod5(uint64_t hash) {
return hash % 5llu;
}
static uint64_t mod7(uint64_t hash) {
return hash % 7llu;
}
static uint64_t mod11(uint64_t hash) {
return hash % 11llu;
}
static uint64_t mod13(uint64_t hash) {
return hash % 13llu;
}
static uint64_t mod17(uint64_t hash) {
return hash % 17llu;
}
static uint64_t mod23(uint64_t hash) {
return hash % 23llu;
}
static uint64_t mod29(uint64_t hash) {
return hash % 29llu;
}
static uint64_t mod37(uint64_t hash) {
return hash % 37llu;
}
static uint64_t mod47(uint64_t hash) {
return hash % 47llu;
}
static uint64_t mod59(uint64_t hash) {
return hash % 59llu;
}
static uint64_t mod73(uint64_t hash) {
return hash % 73llu;
}
static uint64_t mod97(uint64_t hash) {
return hash % 97llu;
}
static uint64_t mod127(uint64_t hash) {
return hash % 127llu;
}
static uint64_t mod151(uint64_t hash) {
return hash % 151llu;
}
static uint64_t mod197(uint64_t hash) {
return hash % 197llu;
}
static uint64_t mod251(uint64_t hash) {
return hash % 251llu;
}
static uint64_t mod313(uint64_t hash) {
return hash % 313llu;
}
static uint64_t mod397(uint64_t hash) {
return hash % 397llu;
}
static uint64_t mod499(uint64_t hash) {
return hash % 499llu;
}
static uint64_t mod631(uint64_t hash) {
return hash % 631llu;
}
static uint64_t mod797(uint64_t hash) {
return hash % 797llu;
}
static uint64_t mod1009(uint64_t hash) {
return hash % 1009llu;
}
static uint64_t mod1259(uint64_t hash) {
return hash % 1259llu;
}
static uint64_t mod1597(uint64_t hash) {
return hash % 1597llu;
}
static uint64_t mod2011(uint64_t hash) {
return hash % 2011llu;
}
static uint64_t mod2539(uint64_t hash) {
return hash % 2539llu;
}
static uint64_t mod3203(uint64_t hash) {
return hash % 3203llu;
}
static uint64_t mod4027(uint64_t hash) {
return hash % 4027llu;
}
static uint64_t mod5087(uint64_t hash) {
return hash % 5087llu;
}
static uint64_t mod6421(uint64_t hash) {
return hash % 6421llu;
}
static uint64_t mod8089(uint64_t hash) {
return hash % 8089llu;
}
static uint64_t mod10193(uint64_t hash) {
return hash % 10193llu;
}
static uint64_t mod12853(uint64_t hash) {
return hash % 12853llu;
}
static uint64_t mod16193(uint64_t hash) {
return hash % 16193llu;
}
static uint64_t mod20399(uint64_t hash) {
return hash % 20399llu;
}
static uint64_t mod25717(uint64_t hash) {
return hash % 25717llu;
}
static uint64_t mod32401(uint64_t hash) {
return hash % 32401llu;
}
static uint64_t mod40823(uint64_t hash) {
return hash % 40823llu;
}
static uint64_t mod51437(uint64_t hash) {
return hash % 51437llu;
}
static uint64_t mod64811(uint64_t hash) {
return hash % 64811llu;
}
static uint64_t mod81649(uint64_t hash) {
return hash % 81649llu;
}
static uint64_t mod102877(uint64_t hash) {
return hash % 102877llu;
}
static uint64_t mod129607(uint64_t hash) {
return hash % 129607llu;
}
static uint64_t mod163307(uint64_t hash) {
return hash % 163307llu;
}
static uint64_t mod205759(uint64_t hash) {
return hash % 205759llu;
}
static uint64_t mod259229(uint64_t hash) {
return hash % 259229llu;
}
static uint64_t mod326617(uint64_t hash) {
return hash % 326617llu;
}
static uint64_t mod411527(uint64_t hash) {
return hash % 411527llu;
}
static uint64_t mod518509(uint64_t hash) {
return hash % 518509llu;
}
static uint64_t mod653267(uint64_t hash) {
return hash % 653267llu;
}
static uint64_t mod823117(uint64_t hash) {
return hash % 823117llu;
}
static uint64_t mod1037059(uint64_t hash) {
return hash % 1037059llu;
}
static uint64_t mod1306601(uint64_t hash) {
return hash % 1306601llu;
}
static uint64_t mod1646237(uint64_t hash) {
return hash % 1646237llu;
}
static uint64_t mod2074129(uint64_t hash) {
return hash % 2074129llu;
}
static uint64_t mod2613229(uint64_t hash) {
return hash % 2613229llu;
}
static uint64_t mod3292489(uint64_t hash) {
return hash % 3292489llu;
}
static uint64_t mod4148279(uint64_t hash) {
return hash % 4148279llu;
}
static uint64_t mod5226491(uint64_t hash) {
return hash % 5226491llu;
}
static uint64_t mod6584983(uint64_t hash) {
return hash % 6584983llu;
}
static uint64_t mod8296553(uint64_t hash) {
return hash % 8296553llu;
}
static uint64_t mod10453007(uint64_t hash) {
return hash % 10453007llu;
}
static uint64_t mod13169977(uint64_t hash) {
return hash % 13169977llu;
}
static uint64_t mod16593127(uint64_t hash) {
return hash % 16593127llu;
}
static uint64_t mod20906033(uint64_t hash) {
return hash % 20906033llu;
}
static uint64_t mod26339969(uint64_t hash) {
return hash % 26339969llu;
}
static uint64_t mod33186281(uint64_t hash) {
return hash % 33186281llu;
}
static uint64_t mod41812097(uint64_t hash) {
return hash % 41812097llu;
}
static uint64_t mod52679969(uint64_t hash) {
return hash % 52679969llu;
}
static uint64_t mod66372617(uint64_t hash) {
return hash % 66372617llu;
}
static uint64_t mod83624237(uint64_t hash) {
return hash % 83624237llu;
}
static uint64_t mod105359939(uint64_t hash) {
return hash % 105359939llu;
}
static uint64_t mod132745199(uint64_t hash) {
return hash % 132745199llu;
}
static uint64_t mod167248483(uint64_t hash) {
return hash % 167248483llu;
}
static uint64_t mod210719881(uint64_t hash) {
return hash % 210719881llu;
}
static uint64_t mod265490441(uint64_t hash) {
return hash % 265490441llu;
}
static uint64_t mod334496971(uint64_t hash) {
return hash % 334496971llu;
}
static uint64_t mod421439783(uint64_t hash) {
return hash % 421439783llu;
}
static uint64_t mod530980861(uint64_t hash) {
return hash % 530980861llu;
}
static uint64_t mod668993977(uint64_t hash) {
return hash % 668993977llu;
}
static uint64_t mod842879579(uint64_t hash) {
return hash % 842879579llu;
}
static uint64_t mod1061961721(uint64_t hash) {
return hash % 1061961721llu;
}
static uint64_t mod1337987929(uint64_t hash) {
return hash % 1337987929llu;
}
static uint64_t mod1685759167(uint64_t hash) {
return hash % 1685759167llu;
}
static uint64_t mod2123923447(uint64_t hash) {
return hash % 2123923447llu;
}
static uint64_t mod2675975881(uint64_t hash) {
return hash % 2675975881llu;
}
static uint64_t mod3371518343(uint64_t hash) {
return hash % 3371518343llu;
}
static uint64_t mod4247846927(uint64_t hash) {
return hash % 4247846927llu;
}
static uint64_t mod5351951779(uint64_t hash) {
return hash % 5351951779llu;
}
static uint64_t mod6743036717(uint64_t hash) {
return hash % 6743036717llu;
}
static uint64_t mod8495693897(uint64_t hash) {
return hash % 8495693897llu;
}
static uint64_t mod10703903591(uint64_t hash) {
return hash % 10703903591llu;
}
static uint64_t mod13486073473(uint64_t hash) {
return hash % 13486073473llu;
}
static uint64_t mod16991387857(uint64_t hash) {
return hash % 16991387857llu;
}
static uint64_t mod21407807219(uint64_t hash) {
return hash % 21407807219llu;
}
static uint64_t mod26972146961(uint64_t hash) {
return hash % 26972146961llu;
}
static uint64_t mod33982775741(uint64_t hash) {
return hash % 33982775741llu;
}
static uint64_t mod42815614441(uint64_t hash) {
return hash % 42815614441llu;
}
static uint64_t mod53944293929(uint64_t hash) {
return hash % 53944293929llu;
}
static uint64_t mod67965551447(uint64_t hash) {
return hash % 67965551447llu;
}
static uint64_t mod85631228929(uint64_t hash) {
return hash % 85631228929llu;
}
static uint64_t mod107888587883(uint64_t hash) {
return hash % 107888587883llu;
}
static uint64_t mod135931102921(uint64_t hash) {
return hash % 135931102921llu;
}
static uint64_t mod171262457903(uint64_t hash) {
return hash % 171262457903llu;
}
static uint64_t mod215777175787(uint64_t hash) {
return hash % 215777175787llu;
}
static uint64_t mod271862205833(uint64_t hash) {
return hash % 271862205833llu;
}
static uint64_t mod342524915839(uint64_t hash) {
return hash % 342524915839llu;
}
static uint64_t mod431554351609(uint64_t hash) {
return hash % 431554351609llu;
}
static uint64_t mod543724411781(uint64_t hash) {
return hash % 543724411781llu;
}
static uint64_t mod685049831731(uint64_t hash) {
return hash % 685049831731llu;
}
static uint64_t mod863108703229(uint64_t hash) {
return hash % 863108703229llu;
}
static uint64_t mod1087448823553(uint64_t hash) {
return hash % 1087448823553llu;
}
static uint64_t mod1370099663459(uint64_t hash) {
return hash % 1370099663459llu;
}
static uint64_t mod1726217406467(uint64_t hash) {
return hash % 1726217406467llu;
}
static uint64_t mod2174897647073(uint64_t hash) {
return hash % 2174897647073llu;
}
static uint64_t mod2740199326961(uint64_t hash) {
return hash % 2740199326961llu;
}
static uint64_t mod3452434812973(uint64_t hash) {
return hash % 3452434812973llu;
}
static uint64_t mod4349795294267(uint64_t hash) {
return hash % 4349795294267llu;
}
static uint64_t mod5480398654009(uint64_t hash) {
return hash % 5480398654009llu;
}
static uint64_t mod6904869625999(uint64_t hash) {
return hash % 6904869625999llu;
}
static uint64_t mod8699590588571(uint64_t hash) {
return hash % 8699590588571llu;
}
static uint64_t mod10960797308051(uint64_t hash) {
return hash % 10960797308051llu;
}
static uint64_t mod13809739252051(uint64_t hash) {
return hash % 13809739252051llu;
}
static uint64_t mod17399181177241(uint64_t hash) {
return hash % 17399181177241llu;
}
static uint64_t mod21921594616111(uint64_t hash) {
return hash % 21921594616111llu;
}
static uint64_t mod27619478504183(uint64_t hash) {
return hash % 27619478504183llu;
}
static uint64_t mod34798362354533(uint64_t hash) {
return hash % 34798362354533llu;
}
static uint64_t mod43843189232363(uint64_t hash) {
return hash % 43843189232363llu;
}
static uint64_t mod55238957008387(uint64_t hash) {
return hash % 55238957008387llu;
}
static uint64_t mod69596724709081(uint64_t hash) {
return hash % 69596724709081llu;
}
static uint64_t mod87686378464759(uint64_t hash) {
return hash % 87686378464759llu;
}
static uint64_t mod110477914016779(uint64_t hash) {
return hash % 110477914016779llu;
}
static uint64_t mod139193449418173(uint64_t hash) {
return hash % 139193449418173llu;
}
static uint64_t mod175372756929481(uint64_t hash) {
return hash % 175372756929481llu;
}
static uint64_t mod220955828033581(uint64_t hash) {
return hash % 220955828033581llu;
}
static uint64_t mod278386898836457(uint64_t hash) {
return hash % 278386898836457llu;
}
static uint64_t mod350745513859007(uint64_t hash) {
return hash % 350745513859007llu;
}
static uint64_t mod441911656067171(uint64_t hash) {
return hash % 441911656067171llu;
}
static uint64_t mod556773797672909(uint64_t hash) {
return hash % 556773797672909llu;
}
static uint64_t mod701491027718027(uint64_t hash) {
return hash % 701491027718027llu;
}
static uint64_t mod883823312134381(uint64_t hash) {
return hash % 883823312134381llu;
}
static uint64_t mod1113547595345903(uint64_t hash) {
return hash % 1113547595345903llu;
}
static uint64_t mod1402982055436147(uint64_t hash) {
return hash % 1402982055436147llu;
}
static uint64_t mod1767646624268779(uint64_t hash) {
return hash % 1767646624268779llu;
}
static uint64_t mod2227095190691797(uint64_t hash) {
return hash % 2227095190691797llu;
}
static uint64_t mod2805964110872297(uint64_t hash) {
return hash % 2805964110872297llu;
}
static uint64_t mod3535293248537579(uint64_t hash) {
return hash % 3535293248537579llu;
}
static uint64_t mod4454190381383713(uint64_t hash) {
return hash % 4454190381383713llu;
}
static uint64_t mod5611928221744609(uint64_t hash) {
return hash % 5611928221744609llu;
}
static uint64_t mod7070586497075177(uint64_t hash) {
return hash % 7070586497075177llu;
}
static uint64_t mod8908380762767489(uint64_t hash) {
return hash % 8908380762767489llu;
}
static uint64_t mod11223856443489329(uint64_t hash) {
return hash % 11223856443489329llu;
}
static uint64_t mod14141172994150357(uint64_t hash) {
return hash % 14141172994150357llu;
}
static uint64_t mod17816761525534927(uint64_t hash) {
return hash % 17816761525534927llu;
}
static uint64_t mod22447712886978529(uint64_t hash) {
return hash % 22447712886978529llu;
}
static uint64_t mod28282345988300791(uint64_t hash) {
return hash % 28282345988300791llu;
}
static uint64_t mod35633523051069991(uint64_t hash) {
return hash % 35633523051069991llu;
}
static uint64_t mod44895425773957261(uint64_t hash) {
return hash % 44895425773957261llu;
}
static uint64_t mod56564691976601587(uint64_t hash) {
return hash % 56564691976601587llu;
}
static uint64_t mod71267046102139967(uint64_t hash) {
return hash % 71267046102139967llu;
}
static uint64_t mod89790851547914507(uint64_t hash) {
return hash % 89790851547914507llu;
}
static uint64_t mod113129383953203213(uint64_t hash) {
return hash % 113129383953203213llu;
}
static uint64_t mod142534092204280003(uint64_t hash) {
return hash % 142534092204280003llu;
}
static uint64_t mod179581703095829107(uint64_t hash) {
return hash % 179581703095829107llu;
}
static uint64_t mod226258767906406483(uint64_t hash) {
return hash % 226258767906406483llu;
}
static uint64_t mod285068184408560057(uint64_t hash) {
return hash % 285068184408560057llu;
}
static uint64_t mod359163406191658253(uint64_t hash) {
return hash % 359163406191658253llu;
}
static uint64_t mod452517535812813007(uint64_t hash) {
return hash % 452517535812813007llu;
}
static uint64_t mod570136368817120201(uint64_t hash) {
return hash % 570136368817120201llu;
}
static uint64_t mod718326812383316683(uint64_t hash) {
return hash % 718326812383316683llu;
}
static uint64_t mod905035071625626043(uint64_t hash) {
return hash % 905035071625626043llu;
}
static uint64_t mod1140272737634240411(uint64_t hash) {
return hash % 1140272737634240411llu;
}
static uint64_t mod1436653624766633509(uint64_t hash) {
return hash % 1436653624766633509llu;
}
static uint64_t mod1810070143251252131(uint64_t hash) {
return hash % 1810070143251252131llu;
}
static uint64_t mod2280545475268481167(uint64_t hash) {
return hash % 2280545475268481167llu;
}
static uint64_t mod2873307249533267101(uint64_t hash) {
return hash % 2873307249533267101llu;
}
static uint64_t mod3620140286502504283(uint64_t hash) {
return hash % 3620140286502504283llu;
}
static uint64_t mod4561090950536962147(uint64_t hash) {
return hash % 4561090950536962147llu;
}
static uint64_t mod5746614499066534157(uint64_t hash) {
return hash % 5746614499066534157llu;
}
static uint64_t mod7240280573005008577(uint64_t hash) {
return hash % 7240280573005008577llu;
}
static uint64_t mod9122181901073924329(uint64_t hash) {
return hash % 9122181901073924329llu;
}
static uint64_t mod11493228998133068689(uint64_t hash) {
return hash % 11493228998133068689llu;
}
static uint64_t mod14480561146010017169(uint64_t hash) {
return hash % 14480561146010017169llu;
}
static uint64_t mod18446744073709551557(uint64_t hash) {
return hash % 18446744073709551557llu;
}
using mod_function = uint64_t (*)(uint64_t);
mod_function next_size_over(uint64_t& size) const {
// prime numbers generated by the following method:
// 1. start with a prime p = 2
// 2. go to wolfram alpha and get p = NextPrime(2 * p)
// 3. repeat 2. until you overflow 64 bits
// you now have large gaps which you would hit if somebody called reserve()
// with an unlucky number.
// 4. to fill the gaps for every prime p go to wolfram alpha and get
// ClosestPrime(p * 2^(1/3)) and ClosestPrime(p * 2^(2/3)) and put those in
// the gaps
// 5. get PrevPrime(2^64) and put it at the end
static constexpr const uint64_t prime_list[] = {
2llu,
3llu,
5llu,
7llu,
11llu,
13llu,
17llu,
23llu,
29llu,
37llu,
47llu,
59llu,
73llu,
97llu,
127llu,
151llu,
197llu,
251llu,
313llu,
397llu,
499llu,
631llu,
797llu,
1009llu,
1259llu,
1597llu,
2011llu,
2539llu,
3203llu,
4027llu,
5087llu,
6421llu,
8089llu,
10193llu,
12853llu,
16193llu,
20399llu,
25717llu,
32401llu,
40823llu,
51437llu,
64811llu,
81649llu,
102877llu,
129607llu,
163307llu,
205759llu,
259229llu,
326617llu,
411527llu,
518509llu,
653267llu,
823117llu,
1037059llu,
1306601llu,
1646237llu,
2074129llu,
2613229llu,
3292489llu,
4148279llu,
5226491llu,
6584983llu,
8296553llu,
10453007llu,
13169977llu,
16593127llu,
20906033llu,
26339969llu,
33186281llu,
41812097llu,
52679969llu,
66372617llu,
83624237llu,
105359939llu,
132745199llu,
167248483llu,
210719881llu,
265490441llu,
334496971llu,
421439783llu,
530980861llu,
668993977llu,
842879579llu,
1061961721llu,
1337987929llu,
1685759167llu,
2123923447llu,
2675975881llu,
3371518343llu,
4247846927llu,
5351951779llu,
6743036717llu,
8495693897llu,
10703903591llu,
13486073473llu,
16991387857llu,
21407807219llu,
26972146961llu,
33982775741llu,
42815614441llu,
53944293929llu,
67965551447llu,
85631228929llu,
107888587883llu,
135931102921llu,
171262457903llu,
215777175787llu,
271862205833llu,
342524915839llu,
431554351609llu,
543724411781llu,
685049831731llu,
863108703229llu,
1087448823553llu,
1370099663459llu,
1726217406467llu,
2174897647073llu,
2740199326961llu,
3452434812973llu,
4349795294267llu,
5480398654009llu,
6904869625999llu,
8699590588571llu,
10960797308051llu,
13809739252051llu,
17399181177241llu,
21921594616111llu,
27619478504183llu,
34798362354533llu,
43843189232363llu,
55238957008387llu,
69596724709081llu,
87686378464759llu,
110477914016779llu,
139193449418173llu,
175372756929481llu,
220955828033581llu,
278386898836457llu,
350745513859007llu,
441911656067171llu,
556773797672909llu,
701491027718027llu,
883823312134381llu,
1113547595345903llu,
1402982055436147llu,
1767646624268779llu,
2227095190691797llu,
2805964110872297llu,
3535293248537579llu,
4454190381383713llu,
5611928221744609llu,
7070586497075177llu,
8908380762767489llu,
11223856443489329llu,
14141172994150357llu,
17816761525534927llu,
22447712886978529llu,
28282345988300791llu,
35633523051069991llu,
44895425773957261llu,
56564691976601587llu,
71267046102139967llu,
89790851547914507llu,
113129383953203213llu,
142534092204280003llu,
179581703095829107llu,
226258767906406483llu,
285068184408560057llu,
359163406191658253llu,
452517535812813007llu,
570136368817120201llu,
718326812383316683llu,
905035071625626043llu,
1140272737634240411llu,
1436653624766633509llu,
1810070143251252131llu,
2280545475268481167llu,
2873307249533267101llu,
3620140286502504283llu,
4561090950536962147llu,
5746614499066534157llu,
7240280573005008577llu,
9122181901073924329llu,
11493228998133068689llu,
14480561146010017169llu,
18446744073709551557llu};
static constexpr uint64_t (*const mod_functions[])(uint64_t) = {
&mod0,
&mod2,
&mod3,
&mod5,
&mod7,
&mod11,
&mod13,
&mod17,
&mod23,
&mod29,
&mod37,
&mod47,
&mod59,
&mod73,
&mod97,
&mod127,
&mod151,
&mod197,
&mod251,
&mod313,
&mod397,
&mod499,
&mod631,
&mod797,
&mod1009,
&mod1259,
&mod1597,
&mod2011,
&mod2539,
&mod3203,
&mod4027,
&mod5087,
&mod6421,
&mod8089,
&mod10193,
&mod12853,
&mod16193,
&mod20399,
&mod25717,
&mod32401,
&mod40823,
&mod51437,
&mod64811,
&mod81649,
&mod102877,
&mod129607,
&mod163307,
&mod205759,
&mod259229,
&mod326617,
&mod411527,
&mod518509,
&mod653267,
&mod823117,
&mod1037059,
&mod1306601,
&mod1646237,
&mod2074129,
&mod2613229,
&mod3292489,
&mod4148279,
&mod5226491,
&mod6584983,
&mod8296553,
&mod10453007,
&mod13169977,
&mod16593127,
&mod20906033,
&mod26339969,
&mod33186281,
&mod41812097,
&mod52679969,
&mod66372617,
&mod83624237,
&mod105359939,
&mod132745199,
&mod167248483,
&mod210719881,
&mod265490441,
&mod334496971,
&mod421439783,
&mod530980861,
&mod668993977,
&mod842879579,
&mod1061961721,
&mod1337987929,
&mod1685759167,
&mod2123923447,
&mod2675975881,
&mod3371518343,
&mod4247846927,
&mod5351951779,
&mod6743036717,
&mod8495693897,
&mod10703903591,
&mod13486073473,
&mod16991387857,
&mod21407807219,
&mod26972146961,
&mod33982775741,
&mod42815614441,
&mod53944293929,
&mod67965551447,
&mod85631228929,
&mod107888587883,
&mod135931102921,
&mod171262457903,
&mod215777175787,
&mod271862205833,
&mod342524915839,
&mod431554351609,
&mod543724411781,
&mod685049831731,
&mod863108703229,
&mod1087448823553,
&mod1370099663459,
&mod1726217406467,
&mod2174897647073,
&mod2740199326961,
&mod3452434812973,
&mod4349795294267,
&mod5480398654009,
&mod6904869625999,
&mod8699590588571,
&mod10960797308051,
&mod13809739252051,
&mod17399181177241,
&mod21921594616111,
&mod27619478504183,
&mod34798362354533,
&mod43843189232363,
&mod55238957008387,
&mod69596724709081,
&mod87686378464759,
&mod110477914016779,
&mod139193449418173,
&mod175372756929481,
&mod220955828033581,
&mod278386898836457,
&mod350745513859007,
&mod441911656067171,
&mod556773797672909,
&mod701491027718027,
&mod883823312134381,
&mod1113547595345903,
&mod1402982055436147,
&mod1767646624268779,
&mod2227095190691797,
&mod2805964110872297,
&mod3535293248537579,
&mod4454190381383713,
&mod5611928221744609,
&mod7070586497075177,
&mod8908380762767489,
&mod11223856443489329,
&mod14141172994150357,
&mod17816761525534927,
&mod22447712886978529,
&mod28282345988300791,
&mod35633523051069991,
&mod44895425773957261,
&mod56564691976601587,
&mod71267046102139967,
&mod89790851547914507,
&mod113129383953203213,
&mod142534092204280003,
&mod179581703095829107,
&mod226258767906406483,
&mod285068184408560057,
&mod359163406191658253,
&mod452517535812813007,
&mod570136368817120201,
&mod718326812383316683,
&mod905035071625626043,
&mod1140272737634240411,
&mod1436653624766633509,
&mod1810070143251252131,
&mod2280545475268481167,
&mod2873307249533267101,
&mod3620140286502504283,
&mod4561090950536962147,
&mod5746614499066534157,
&mod7240280573005008577,
&mod9122181901073924329,
&mod11493228998133068689,
&mod14480561146010017169,
&mod18446744073709551557};
const uint64_t* found = std::lower_bound(
std::begin(prime_list), std::end(prime_list) - 1, size);
size = *found;
return mod_functions[1 + found - prime_list];
}
void commit(mod_function new_mod_function) {
current_mod_function = new_mod_function;
}
void reset() {
current_mod_function = &mod0;
}
uint64_t index_for_hash(uint64_t hash, uint64_t /*num_slots_minus_one*/)
const {
return current_mod_function(hash);
}
uint64_t keep_in_range(uint64_t index, uint64_t num_slots_minus_one) const {
return index > num_slots_minus_one ? current_mod_function(index) : index;
}
private:
mod_function current_mod_function = &mod0;
};
struct power_of_two_hash_policy {
uint64_t index_for_hash(uint64_t hash, uint64_t num_slots_minus_one) const {
return hash & num_slots_minus_one;
}
uint64_t keep_in_range(uint64_t index, uint64_t num_slots_minus_one) const {
return index_for_hash(index, num_slots_minus_one);
}
int8_t next_size_over(uint64_t& size) const {
size = detailv3::next_power_of_two(size);
return 0;
}
void commit(int8_t) {}
void reset() {}
};
struct fibonacci_hash_policy {
uint64_t index_for_hash(uint64_t hash, uint64_t /*num_slots_minus_one*/)
const {
return (11400714819323198485ull * hash) >> shift;
}
uint64_t keep_in_range(uint64_t index, uint64_t num_slots_minus_one) const {
return index & num_slots_minus_one;
}
int8_t next_size_over(uint64_t& size) const {
size = std::max(uint64_t(2), detailv3::next_power_of_two(size));
return 64 - detailv3::log2(size);
}
void commit(int8_t shift_) {
shift = shift_;
}
void reset() {
shift = 63;
}
private:
int8_t shift = 63;
};
template <
typename K,
typename V,
typename H = std::hash<K>,
typename E = std::equal_to<K>,
typename A = std::allocator<std::pair<K, V>>>
class flat_hash_map
: public detailv3::sherwood_v3_table<
std::pair<K, V>,
K,
H,
detailv3::KeyOrValueHasher<K, std::pair<K, V>, H>,
E,
detailv3::KeyOrValueEquality<K, std::pair<K, V>, E>,
A,
typename std::allocator_traits<A>::template rebind_alloc<
detailv3::sherwood_v3_entry<std::pair<K, V>>>> {
using Table = detailv3::sherwood_v3_table<
std::pair<K, V>,
K,
H,
detailv3::KeyOrValueHasher<K, std::pair<K, V>, H>,
E,
detailv3::KeyOrValueEquality<K, std::pair<K, V>, E>,
A,
typename std::allocator_traits<A>::template rebind_alloc<
detailv3::sherwood_v3_entry<std::pair<K, V>>>>;
public:
using key_type = K;
using mapped_type = V;
using Table::Table;
flat_hash_map() {}
inline V& operator[](const K& key) {
return emplace(key, convertible_to_value()).first->second;
}
inline V& operator[](K&& key) {
return emplace(std::move(key), convertible_to_value()).first->second;
}
V& at(const K& key) {
auto found = this->find(key);
if (found == this->end())
throw std::out_of_range("Argument passed to at() was not in the map.");
return found->second;
}
const V& at(const K& key) const {
auto found = this->find(key);
if (found == this->end())
throw std::out_of_range("Argument passed to at() was not in the map.");
return found->second;
}
using Table::emplace;
std::pair<typename Table::iterator, bool> emplace() {
return emplace(key_type(), convertible_to_value());
}
template <typename M>
std::pair<typename Table::iterator, bool> insert_or_assign(
const key_type& key,
M&& m) {
auto emplace_result = emplace(key, std::forward<M>(m));
if (!emplace_result.second)
emplace_result.first->second = std::forward<M>(m);
return emplace_result;
}
template <typename M>
std::pair<typename Table::iterator, bool> insert_or_assign(
key_type&& key,
M&& m) {
auto emplace_result = emplace(std::move(key), std::forward<M>(m));
if (!emplace_result.second)
emplace_result.first->second = std::forward<M>(m);
return emplace_result;
}
template <typename M>
typename Table::iterator insert_or_assign(
typename Table::const_iterator,
const key_type& key,
M&& m) {
return insert_or_assign(key, std::forward<M>(m)).first;
}
template <typename M>
typename Table::iterator insert_or_assign(
typename Table::const_iterator,
key_type&& key,
M&& m) {
return insert_or_assign(std::move(key), std::forward<M>(m)).first;
}
friend bool operator==(const flat_hash_map& lhs, const flat_hash_map& rhs) {
if (lhs.size() != rhs.size())
return false;
for (const typename Table::value_type& value : lhs) {
auto found = rhs.find(value.first);
if (found == rhs.end())
return false;
else if (value.second != found->second)
return false;
}
return true;
}
friend bool operator!=(const flat_hash_map& lhs, const flat_hash_map& rhs) {
return !(lhs == rhs);
}
private:
struct convertible_to_value {
operator V() const {
return V();
}
};
};
template <
typename T,
typename H = std::hash<T>,
typename E = std::equal_to<T>,
typename A = std::allocator<T>>
class flat_hash_set
: public detailv3::sherwood_v3_table<
T,
T,
H,
detailv3::functor_storage<uint64_t, H>,
E,
detailv3::functor_storage<bool, E>,
A,
typename std::allocator_traits<A>::template rebind_alloc<
detailv3::sherwood_v3_entry<T>>> {
using Table = detailv3::sherwood_v3_table<
T,
T,
H,
detailv3::functor_storage<uint64_t, H>,
E,
detailv3::functor_storage<bool, E>,
A,
typename std::allocator_traits<A>::template rebind_alloc<
detailv3::sherwood_v3_entry<T>>>;
public:
using key_type = T;
using Table::Table;
flat_hash_set() {}
template <typename... Args>
std::pair<typename Table::iterator, bool> emplace(Args&&... args) {
return Table::emplace(T(std::forward<Args>(args)...));
}
std::pair<typename Table::iterator, bool> emplace(const key_type& arg) {
return Table::emplace(arg);
}
std::pair<typename Table::iterator, bool> emplace(key_type& arg) {
return Table::emplace(arg);
}
std::pair<typename Table::iterator, bool> emplace(const key_type&& arg) {
return Table::emplace(std::move(arg));
}
std::pair<typename Table::iterator, bool> emplace(key_type&& arg) {
return Table::emplace(std::move(arg));
}
friend bool operator==(const flat_hash_set& lhs, const flat_hash_set& rhs) {
if (lhs.size() != rhs.size())
return false;
for (const T& value : lhs) {
if (rhs.find(value) == rhs.end())
return false;
}
return true;
}
friend bool operator!=(const flat_hash_set& lhs, const flat_hash_set& rhs) {
return !(lhs == rhs);
}
};
template <typename T>
struct power_of_two_std_hash : std::hash<T> {
typedef ska::power_of_two_hash_policy hash_policy;
};
} // end namespace ska
C10_CLANG_DIAGNOSTIC_POP()
|