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 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269
|
{%MainUnit generics.collections.pas}
{
This file is part of the Free Pascal run time library.
Copyright (c) 2014 by Maciej Izak (hnb)
member of the Free Sparta development team (http://freesparta.com)
Copyright(c) 2004-2014 DaThoX
It contains the Free Pascal generics library
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Acknowledgment
Thanks to Sphere 10 Software (http://sphere10.com) for sponsoring
many new types and major refactoring of entire library
Thanks to mORMot (http://synopse.info) project for the best implementations
of hashing functions like crc32c and xxHash32 :)
**********************************************************************}
{ TPair<TKey,TValue> }
class function TPair<TKey, TValue>.Create(AKey: TKey;
AValue: TValue): TPair<TKey, TValue>;
begin
Result.Key := AKey;
Result.Value := AValue;
end;
{ TCustomDictionary<CUSTOM_DICTIONARY_CONSTRAINTS> }
procedure TCustomDictionary<CUSTOM_DICTIONARY_CONSTRAINTS>.PairNotify(constref APair: TDictionaryPair;
ACollectionNotification: TCollectionNotification);
begin
KeyNotify(APair.Key, ACollectionNotification);
ValueNotify(APair.Value, ACollectionNotification);
end;
procedure TCustomDictionary<CUSTOM_DICTIONARY_CONSTRAINTS>.KeyNotify(constref AKey: TKey;
ACollectionNotification: TCollectionNotification);
begin
if Assigned(FOnKeyNotify) then
FOnKeyNotify(Self, AKey, ACollectionNotification);
end;
procedure TCustomDictionary<CUSTOM_DICTIONARY_CONSTRAINTS>.SetValue(var AValue: TValue; constref ANewValue: TValue);
var
LOldValue: TValue;
begin
LOldValue := AValue;
AValue := ANewValue;
ValueNotify(LOldValue, cnRemoved);
ValueNotify(ANewValue, cnAdded);
end;
procedure TCustomDictionary<CUSTOM_DICTIONARY_CONSTRAINTS>.ValueNotify(constref AValue: TValue;
ACollectionNotification: TCollectionNotification);
begin
if Assigned(FOnValueNotify) then
FOnValueNotify(Self, AValue, ACollectionNotification);
end;
constructor TCustomDictionary<CUSTOM_DICTIONARY_CONSTRAINTS>.Create;
begin
Create(0);
end;
constructor TCustomDictionary<CUSTOM_DICTIONARY_CONSTRAINTS>.Create(ACapacity: SizeInt); overload;
begin
Create(ACapacity, TEqualityComparer<TKey>.Default(THashFactory));
end;
constructor TCustomDictionary<CUSTOM_DICTIONARY_CONSTRAINTS>.Create(ACapacity: SizeInt;
const AComparer: IEqualityComparer<TKey>);
begin
FEqualityComparer := AComparer;
SetCapacity(ACapacity);
end;
constructor TCustomDictionary<CUSTOM_DICTIONARY_CONSTRAINTS>.Create(const AComparer: IEqualityComparer<TKey>);
begin
Create(0, AComparer);
end;
constructor TCustomDictionary<CUSTOM_DICTIONARY_CONSTRAINTS>.Create(ACollection: TEnumerable<TDictionaryPair>);
begin
Create(ACollection, TEqualityComparer<TKey>.Default(THashFactory));
end;
{$IFDEF ENABLE_METHODS_WITH_TEnumerableWithPointers}
constructor TCustomDictionary<CUSTOM_DICTIONARY_CONSTRAINTS>.Create(ACollection: TEnumerableWithPointers<TDictionaryPair>);
begin
Create(ACollection, TEqualityComparer<TKey>.Default(THashFactory));
end;
{$ENDIF}
constructor TCustomDictionary<CUSTOM_DICTIONARY_CONSTRAINTS>.Create(ACollection: TEnumerable<TDictionaryPair>;
const AComparer: IEqualityComparer<TKey>); overload;
var
LItem: TDictionaryPair;
begin
Create(AComparer);
for LItem in ACollection do
Add(LItem);
end;
{$IFDEF ENABLE_METHODS_WITH_TEnumerableWithPointers}
constructor TCustomDictionary<CUSTOM_DICTIONARY_CONSTRAINTS>.Create(ACollection: TEnumerableWithPointers<TDictionaryPair>;
const AComparer: IEqualityComparer<TKey>); overload;
var
LItem: PDictionaryPair;
begin
Create(AComparer);
for LItem in ACollection.Ptr^ do
Add(LItem^);
end;
{$ENDIF}
destructor TCustomDictionary<CUSTOM_DICTIONARY_CONSTRAINTS>.Destroy;
begin
Clear;
FKeys.Free;
FValues.Free;
inherited;
end;
function TCustomDictionary<CUSTOM_DICTIONARY_CONSTRAINTS>.ToArray(ACount: SizeInt): TArray<TDictionaryPair>;
var
i: SizeInt;
LEnumerator: TEnumerator<TDictionaryPair>;
begin
SetLength(Result, ACount);
LEnumerator := DoGetEnumerator;
i := 0;
while LEnumerator.MoveNext do
begin
Result[i] := LEnumerator.Current;
Inc(i);
end;
LEnumerator.Free;
end;
function TCustomDictionary<CUSTOM_DICTIONARY_CONSTRAINTS>.ToArray: TArray<TDictionaryPair>;
begin
Result := ToArray(Count);
end;
{ TCustomDictionaryEnumerator<T, CUSTOM_DICTIONARY_CONSTRAINTS> }
constructor TCustomDictionaryEnumerator<T, CUSTOM_DICTIONARY_CONSTRAINTS>.Create(
ADictionary: TCustomDictionary<CUSTOM_DICTIONARY_CONSTRAINTS>);
begin
inherited Create;
FIndex := -1;
FDictionary := ADictionary;
end;
function TCustomDictionaryEnumerator<T, CUSTOM_DICTIONARY_CONSTRAINTS>.DoGetCurrent: T;
begin
Result := GetCurrent;
end;
{ TDictionaryEnumerable<TDictionaryEnumerator, TDictionaryPointersEnumerator, T, CUSTOM_DICTIONARY_CONSTRAINTS> }
function TDictionaryEnumerable<TDictionaryEnumerator, TDictionaryPointersEnumerator, T, CUSTOM_DICTIONARY_CONSTRAINTS>.GetPtrEnumerator: TEnumerator<PT>;
begin
Result := TDictionaryPointersEnumerator.Create(FDictionary);
end;
constructor TDictionaryEnumerable<TDictionaryEnumerator, TDictionaryPointersEnumerator, T, CUSTOM_DICTIONARY_CONSTRAINTS>.Create(
ADictionary: TCustomDictionary<CUSTOM_DICTIONARY_CONSTRAINTS>);
begin
FDictionary := ADictionary;
end;
function TDictionaryEnumerable<TDictionaryEnumerator, TDictionaryPointersEnumerator, T, CUSTOM_DICTIONARY_CONSTRAINTS>.
DoGetEnumerator: TDictionaryEnumerator;
begin
Result := TDictionaryEnumerator(TDictionaryEnumerator.NewInstance);
TCustomDictionaryEnumerator<T, CUSTOM_DICTIONARY_CONSTRAINTS>(Result).Create(FDictionary);
end;
function TDictionaryEnumerable<TDictionaryEnumerator, TDictionaryPointersEnumerator, T, CUSTOM_DICTIONARY_CONSTRAINTS>.GetCount: SizeInt;
begin
Result := TCustomDictionary<CUSTOM_DICTIONARY_CONSTRAINTS>(FDictionary).Count;
end;
function TDictionaryEnumerable<TDictionaryEnumerator, TDictionaryPointersEnumerator, T, CUSTOM_DICTIONARY_CONSTRAINTS>.ToArray: TArray<T>;
begin
Result := ToArrayImpl(FDictionary.Count);
end;
{ TOpenAddressingEnumerator<T, DICTIONARY_CONSTRAINTS> }
function TOpenAddressingEnumerator<T, OPEN_ADDRESSING_CONSTRAINTS>.DoMoveNext: Boolean;
var
LLength: SizeInt;
begin
Inc(FIndex);
LLength := Length(TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>(FDictionary).FItems);
if FIndex >= LLength then
Exit(False);
// maybe related to bug #24098
// compiler error for (TDictionary<DICTIONARY_CONSTRAINTS>(FDictionary).FItems[FIndex].Hash and UInt32.GetSignMask) = 0
while ((TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>(FDictionary).FItems[FIndex].Hash) and UInt32.GetSignMask) = 0 do
begin
Inc(FIndex);
if FIndex = LLength then
Exit(False);
end;
Result := True;
end;
{ TOpenAddressingPointersEnumerator<TItem, PDictionaryPair> }
function TOpenAddressingPointersEnumerator<TItem, PDictionaryPair>.DoMoveNext: boolean;
var
LLength: SizeInt;
begin
Inc(FIndex);
LLength := Length(FItems^);
if FIndex >= LLength then
Exit(False);
// maybe related to bug #24098
// compiler error for (TDictionary<DICTIONARY_CONSTRAINTS>(FDictionary).FItems[FIndex].Hash and UInt32.GetSignMask) = 0
while (FItems^[FIndex].Hash and UInt32.GetSignMask) = 0 do
begin
Inc(FIndex);
if FIndex = LLength then
Exit(False);
end;
Result := True;
end;
function TOpenAddressingPointersEnumerator<TItem, PDictionaryPair>.DoGetCurrent: PDictionaryPair;
begin
Result := GetCurrent;
end;
function TOpenAddressingPointersEnumerator<TItem, PDictionaryPair>.GetCurrent: PDictionaryPair;
begin
Result := @FItems^[FIndex].Pair;
end;
constructor TOpenAddressingPointersEnumerator<TItem, PDictionaryPair>.Create(var AItems);
begin
FIndex := -1;
FItems := @AItems;
end;
{ TOpenAddressingPointersCollection<TPointersEnumerator, TItem, PDictionaryPair> }
function TOpenAddressingPointersCollection<TPointersEnumerator, TItem, PDictionaryPair>.Items: PArray;
begin
Result := PArray(@((@Self)^));
end;
function TOpenAddressingPointersCollection<TPointersEnumerator, TItem, PDictionaryPair>.GetCount: SizeInt;
begin
Result := PSizeInt(PByte(@((@Self)^))-SizeOf(SizeInt))^;
end;
function TOpenAddressingPointersCollection<TPointersEnumerator, TItem, PDictionaryPair>.GetEnumerator: TPointersEnumerator;
begin
Result := TPointersEnumerator(TPointersEnumerator.NewInstance);
TPointersEnumerator(Result).Create(Items^);
end;
function TOpenAddressingPointersCollection<TPointersEnumerator, TItem, PDictionaryPair>.ToArray: TArray<PDictionaryPair>;
{begin
Result := ToArrayImpl(FList.Count);
end;}
var
i: SizeInt;
LEnumerator: TPointersEnumerator;
begin
SetLength(Result, GetCount);
try
LEnumerator := GetEnumerator;
i := 0;
while LEnumerator.MoveNext do
begin
Result[i] := LEnumerator.Current;
Inc(i);
end;
finally
LEnumerator.Free;
end;
end;
{ TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS> }
constructor TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.Create(ACapacity: SizeInt;
const AComparer: IEqualityComparer<TKey>);
begin
inherited Create(ACapacity, AComparer);
FMaxLoadFactor := TProbeSequence.DEFAULT_LOAD_FACTOR;
end;
function TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.GetKeys: TKeyCollection;
begin
if not Assigned(FKeys) then
FKeys := TKeyCollection.Create(Self);
Result := TKeyCollection(FKeys);
end;
function TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.GetValues: TValueCollection;
begin
if not Assigned(FValues) then
FValues := TValueCollection.Create(Self);
Result := TValueCollection(FValues);
end;
function TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.FindBucketIndex(constref AKey: TKey): SizeInt;
var
LHash: UInt32;
begin
Result := FindBucketIndex(FItems, AKey, LHash);
end;
procedure TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.PrepareAddingItem;
begin
if RealItemsLength > FItemsThreshold then
Rehash(Length(FItems) shl 1)
else if FItemsThreshold = 0 then
begin
SetLength(FItems, 8);
UpdateItemsThreshold(8);
end
else if FItemsLength = $40000001 then // High(TIndex) ... Error: Type mismatch
OutOfMemoryError;
end;
procedure TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.UpdateItemsThreshold(ASize: SizeInt);
begin
if ASize = $40000000 then
FItemsThreshold := $40000001
else
FItemsThreshold := Pred(Round(ASize * FMaxLoadFactor));
end;
procedure TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.AddItem(var AItem: TItem; constref AKey: TKey;
constref AValue: TValue; const AHash: UInt32);
begin
AItem.Hash := AHash;
AItem.Pair.Key := AKey;
AItem.Pair.Value := AValue;
// ! very important. FItemsLength must be increased after above code (because constref has meaning)
Inc(FItemsLength);
PairNotify(AItem.Pair, cnAdded);
end;
function TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.GetPointers: PPointersCollection;
begin
Result := PPointersCollection(@FItems);
end;
procedure TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.Add(constref AKey: TKey; constref AValue: TValue);
begin
DoAdd(AKey, AValue);
end;
procedure TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.Add(constref APair: TPair<TKey, TValue>);
begin
DoAdd(APair.Key, APair.Value);
end;
function TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.DoAdd(constref AKey: TKey; constref AValue: TValue): SizeInt;
var
LHash: UInt32;
begin
PrepareAddingItem;
Result := FindBucketIndex(FItems, AKey, LHash);
if Result >= 0 then
raise EListError.CreateRes(@SDuplicatesNotAllowed);
Result := not Result;
AddItem(FItems[Result], AKey, AValue, LHash);
end;
function TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.DoRemove(AIndex: SizeInt;
ACollectionNotification: TCollectionNotification): TValue;
var
LItem: PItem;
LPair: TPair<TKey, TValue>;
begin
LItem := @FItems[AIndex];
LItem.Hash := 0;
Result := LItem.Pair.Value;
LPair := LItem.Pair;
LItem.Pair := Default(TPair<TKey, TValue>);
Dec(FItemsLength);
PairNotify(LPair, ACollectionNotification);
end;
procedure TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.Remove(constref AKey: TKey);
var
LIndex: SizeInt;
begin
LIndex := FindBucketIndex(AKey);
if LIndex < 0 then
Exit;
DoRemove(LIndex, cnRemoved);
end;
function TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.ExtractPair(constref AKey: TKey): TPair<TKey, TValue>;
var
LIndex: SizeInt;
begin
LIndex := FindBucketIndex(AKey);
if LIndex < 0 then
Exit(Default(TPair<TKey, TValue>));
Result.Key := AKey;
Result.Value := DoRemove(LIndex, cnExtracted);
end;
procedure TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.Clear;
var
LItem: PItem;
i: SizeInt;
LOldItems: array of TItem;
begin
FItemsLength := 0;
FItemsThreshold := 0;
// ClearTombstones;
LOldItems := FItems;
FItems := nil;
for i := 0 to High(LOldItems) do
begin
LItem := @LOldItems[i];
if (LItem.Hash and UInt32.GetSignMask = 0) then
Continue;
PairNotify(LItem.Pair, cnRemoved);
end;
end;
function TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.RealItemsLength: SizeInt;
begin
Result := FItemsLength;
end;
function TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.Rehash(ASizePow2: SizeInt; AForce: Boolean): Boolean;
var
LNewItems: TArray<TItem>;
LHash: UInt32;
LIndex: SizeInt;
i: SizeInt;
LItem, LNewItem: PItem;
begin
if (ASizePow2 = Length(FItems)) and not AForce then
Exit(False);
if ASizePow2 < 0 then
OutOfMemoryError;
SetLength(LNewItems, ASizePow2);
UpdateItemsThreshold(ASizePow2);
for i := 0 to High(FItems) do
begin
LItem := @FItems[i];
if (LItem.Hash and UInt32.GetSignMask) <> 0 then
begin
LIndex := FindBucketIndex(LNewItems, LItem.Pair.Key, LHash);
LIndex := not LIndex;
LNewItem := @LNewItems[LIndex];
LNewItem.Hash := LHash;
LNewItem.Pair := LItem.Pair;
end;
end;
FItems := LNewItems;
Result := True;
end;
function TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.DoGetEnumerator: TEnumerator<TDictionaryPair>;
begin
Result := GetEnumerator;
end;
procedure TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.SetCapacity(ACapacity: SizeInt);
begin
if ACapacity < FItemsLength then
raise EArgumentOutOfRangeException.CreateRes(@SArgumentOutOfRange);
Resize(ACapacity);
end;
procedure TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.SetMaxLoadFactor(AValue: single);
var
LItemsLength: SizeInt;
begin
if (AValue > TProbeSequence.MAX_LOAD_FACTOR) or (AValue <= 0) then
raise EArgumentOutOfRangeException.CreateRes(@SArgumentOutOfRange);
FMaxLoadFactor := AValue;
repeat
LItemsLength := Length(FItems);
UpdateItemsThreshold(LItemsLength);
if RealItemsLength > FItemsThreshold then
Rehash(LItemsLength shl 1);
until RealItemsLength <= FItemsThreshold;
end;
function TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.GetLoadFactor: single;
begin
Result := FItemsLength / Length(FItems);
end;
function TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.GetCapacity: SizeInt;
begin
Result := Length(FItems);
end;
procedure TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.Resize(ANewSize: SizeInt);
var
LNewSize: SizeInt;
begin
if ANewSize < 0 then
raise EArgumentOutOfRangeException.CreateRes(@SArgumentOutOfRange);
LNewSize := 0;
if ANewSize > 0 then
begin
LNewSize := 8;
while LNewSize < ANewSize do
LNewSize := LNewSize shl 1;
end;
Rehash(LNewSize);
end;
function TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.GetEnumerator: TPairEnumerator;
begin
Result := TPairEnumerator.Create(Self);
end;
function TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.GetItem(const AKey: TKey): TValue;
var
LIndex: SizeInt;
begin
LIndex := FindBucketIndex(AKey);
if LIndex < 0 then
raise EListError.CreateRes(@SDictionaryKeyDoesNotExist);
Result := FItems[LIndex].Pair.Value;
end;
procedure TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.TrimExcess;
begin
SetCapacity(Succ(FItemsLength));
end;
procedure TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.SetItem(const AKey: TKey; const AValue: TValue);
var
LIndex: SizeInt;
begin
LIndex := FindBucketIndex(AKey);
if LIndex < 0 then
raise EListError.CreateRes(@SItemNotFound);
SetValue(FItems[LIndex].Pair.Value, AValue);
end;
function TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.TryGetValue(constref AKey: TKey; out AValue: TValue): Boolean;
var
LIndex: SizeInt;
begin
LIndex := FindBucketIndex(AKey);
Result := LIndex >= 0;
if Result then
AValue := FItems[LIndex].Pair.Value
else
AValue := Default(TValue);
end;
procedure TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.AddOrSetValue(constref AKey: TKey; constref AValue: TValue);
var
LIndex: SizeInt;
LHash: UInt32;
begin
LIndex := FindBucketIndex(FItems, AKey, LHash);
if LIndex < 0 then
DoAdd(AKey, AValue)
else
SetValue(FItems[LIndex].Pair.Value, AValue);
end;
function TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.ContainsKey(constref AKey: TKey): Boolean;
var
LIndex: SizeInt;
begin
LIndex := FindBucketIndex(AKey);
Result := LIndex >= 0;
end;
function TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.ContainsValue(constref AValue: TValue): Boolean;
begin
Result := ContainsValue(AValue, TEqualityComparer<TValue>.Default(THashFactory));
end;
function TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.ContainsValue(constref AValue: TValue;
const AEqualityComparer: IEqualityComparer<TValue>): Boolean;
var
i: SizeInt;
LItem: PItem;
begin
if Length(FItems) = 0 then
Exit(False);
for i := 0 to High(FItems) do
begin
LItem := @FItems[i];
if (LItem.Hash and UInt32.GetSignMask) = 0 then
Continue;
if AEqualityComparer.Equals(AValue, LItem.Pair.Value) then
Exit(True);
end;
Result := False;
end;
procedure TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.GetMemoryLayout(
const AOnGetMemoryLayoutKeyPosition: TOnGetMemoryLayoutKeyPosition);
var
i: SizeInt;
begin
for i := 0 to High(FItems) do
if (FItems[i].Hash and UInt32.GetSignMask) <> 0 then
AOnGetMemoryLayoutKeyPosition(Self, i);
end;
{ TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.TPairEnumerator }
function TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.TPairEnumerator.GetCurrent: TPair<TKey, TValue>;
begin
Result := TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>(FDictionary).FItems[FIndex].Pair;
end;
{ TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.TValueEnumerator }
function TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.TValueEnumerator.GetCurrent: TValue;
begin
Result := TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>(FDictionary).FItems[FIndex].Pair.Value;
end;
{ TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.TPValueEnumerator }
function TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.TPValueEnumerator.GetCurrent: PValue;
begin
Result := @(TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>(FDictionary).FItems[FIndex].Pair.Value);
end;
{ TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.TKeyEnumerator }
function TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.TKeyEnumerator.GetCurrent: TKey;
begin
Result := TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>(FDictionary).FItems[FIndex].Pair.Key;
end;
{ TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.TPKeyEnumerator }
function TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.TPKeyEnumerator.GetCurrent: PKey;
begin
Result := @(TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>(FDictionary).FItems[FIndex].Pair.Key);
end;
{ TOpenAddressingLP<DICTIONARY_CONSTRAINTS> }
procedure TOpenAddressingLP<OPEN_ADDRESSING_CONSTRAINTS>.NotifyIndexChange(AFrom, ATo: SizeInt);
begin
end;
function TOpenAddressingLP<OPEN_ADDRESSING_CONSTRAINTS>.DoRemove(AIndex: SizeInt;
ACollectionNotification: TCollectionNotification): TValue;
var
LItem: PItem;
LPair: TPair<TKey, TValue>;
LLengthMask: SizeInt;
i, LIndex, LGapIndex: SizeInt;
LHash, LBucket: UInt32;
begin
LItem := @FItems[AIndex];
LPair := LItem.Pair;
// try fill gap
LHash := LItem.Hash;
LItem.Hash := 0; // prevents an infinite searching loop
LLengthMask := Length(FItems) - 1;
i := Succ(AIndex - (LHash and LLengthMask));
LGapIndex := AIndex;
repeat
LIndex := TProbeSequence.Probe(i, LHash) and LLengthMask;
LItem := @FItems[LIndex];
// Empty position
if (LItem.Hash and UInt32.GetSignMask) = 0 then
Break; // breaking bad!
LBucket := LItem.Hash and LLengthMask;
if not InCircularRange(LGapIndex, LBucket, LIndex) then
begin
NotifyIndexChange(LIndex, LGapIndex);
FItems[LGapIndex] := LItem^;
LItem.Hash := 0; // new gap
LGapIndex := LIndex;
end;
Inc(i);
until false;
LItem := @FItems[LGapIndex];
LItem.Hash := 0;
LItem.Pair := Default(TPair<TKey, TValue>);
Dec(FItemsLength);
Result := LPair.Value;
PairNotify(LPair, ACollectionNotification);
end;
function TOpenAddressingLP<OPEN_ADDRESSING_CONSTRAINTS>.FindBucketIndex(constref AItems: TArray<TItem>;
constref AKey: TKey; out AHash: UInt32): SizeInt;
var
LItem: {TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.}_TItem; // for workaround Lazarus bug #25613
LLengthMask: SizeInt;
i, m: SizeInt;
LHash: UInt32;
begin
m := Length(AItems);
LLengthMask := m - 1;
LHash := FEqualityComparer.GetHashCode(AKey);
i := 0;
AHash := LHash or UInt32.GetSignMask;
if m = 0 then
Exit(-1);
Result := AHash and LLengthMask;
repeat
LItem := _TItem(AItems[Result]);
// Empty position
if (LItem.Hash and UInt32.GetSignMask) = 0 then
Exit(not Result); // insert!
// Same position?
if LItem.Hash = AHash then
if FEqualityComparer.Equals(AKey, LItem.Pair.Key) then
Exit;
Inc(i);
Result := TProbeSequence.Probe(i, AHash) and LLengthMask;
until false;
end;
{ TOpenAddressingTombstones<OPEN_ADDRESSING_CONSTRAINTS> }
function TOpenAddressingTombstones<OPEN_ADDRESSING_CONSTRAINTS>.Rehash(ASizePow2: SizeInt; AForce: Boolean): Boolean;
begin
if inherited then
FTombstonesCount := 0;
end;
function TOpenAddressingTombstones<OPEN_ADDRESSING_CONSTRAINTS>.RealItemsLength: SizeInt;
begin
Result := FItemsLength + FTombstonesCount
end;
procedure TOpenAddressingTombstones<OPEN_ADDRESSING_CONSTRAINTS>.ClearTombstones;
begin
Rehash(Length(FItems), True);
end;
procedure TOpenAddressingTombstones<OPEN_ADDRESSING_CONSTRAINTS>.Clear;
begin
FTombstonesCount := 0;
inherited;
end;
function TOpenAddressingTombstones<OPEN_ADDRESSING_CONSTRAINTS>.DoRemove(AIndex: SizeInt;
ACollectionNotification: TCollectionNotification): TValue;
begin
Result := inherited;
FItems[AIndex].Hash := 1;
Inc(FTombstonesCount);
end;
function TOpenAddressingTombstones<OPEN_ADDRESSING_CONSTRAINTS>.DoAdd(constref AKey: TKey;
constref AValue: TValue): SizeInt;
var
LHash: UInt32;
begin
PrepareAddingItem;
Result := FindBucketIndexOrTombstone(FItems, AKey, LHash);
if Result >= 0 then
raise EListError.CreateRes(@SDuplicatesNotAllowed);
Result := not Result;
// Can't ovverride because we lost info about old hash
if FItems[Result].Hash <> 0 then
Dec(FTombstonesCount);
AddItem(FItems[Result], AKey, AValue, LHash);
end;
{ TOpenAddressingSH<OPEN_ADDRESSING_CONSTRAINTS> }
function TOpenAddressingSH<OPEN_ADDRESSING_CONSTRAINTS>.FindBucketIndex(constref AItems: TArray<TItem>;
constref AKey: TKey; out AHash: UInt32): SizeInt;
var
LItem: {TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.}_TItem; // for workaround Lazarus bug #25613
LLengthMask: SizeInt;
i, m: SizeInt;
LHash: UInt32;
begin
m := Length(AItems);
LLengthMask := m - 1;
LHash := FEqualityComparer.GetHashCode(AKey);
i := 0;
AHash := LHash or UInt32.GetSignMask;
if m = 0 then
Exit(-1);
Result := AHash and LLengthMask;
repeat
LItem := _TItem(AItems[Result]);
// Empty position
if LItem.Hash = 0 then
Exit(not Result); // insert!
// Same position?
if LItem.Hash = AHash then
if FEqualityComparer.Equals(AKey, LItem.Pair.Key) then
Exit;
Inc(i);
Result := TProbeSequence.Probe(i, AHash) and LLengthMask;
until false;
end;
function TOpenAddressingSH<OPEN_ADDRESSING_CONSTRAINTS>.FindBucketIndexOrTombstone(constref AItems: TArray<TItem>;
constref AKey: TKey; out AHash: UInt32): SizeInt;
var
LItem: {TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.}_TItem; // for workaround Lazarus bug #25613
LLengthMask: SizeInt;
i, m: SizeInt;
LHash: UInt32;
begin
m := Length(AItems);
LLengthMask := m - 1;
LHash := FEqualityComparer.GetHashCode(AKey);
i := 0;
AHash := LHash or UInt32.GetSignMask;
if m = 0 then
Exit(-1);
Result := AHash and LLengthMask;
repeat
LItem := _TItem(AItems[Result]);
// Empty position or tombstone
if LItem.Hash and UInt32.GetSignMask = 0 then
Exit(not Result); // insert!
// Same position?
if LItem.Hash = AHash then
if FEqualityComparer.Equals(AKey, LItem.Pair.Key) then
Exit;
Inc(i);
Result := TProbeSequence.Probe(i, AHash) and LLengthMask;
until false;
end;
{ TOpenAddressingQP<OPEN_ADDRESSING_CONSTRAINTS> }
procedure TOpenAddressingQP<OPEN_ADDRESSING_CONSTRAINTS>.UpdateItemsThreshold(ASize: SizeInt);
begin
if ASize = $40000000 then
FItemsThreshold := $40000001
else
begin
FPrimaryNumberAsSizeApproximation := PrimaryNumbersJustLessThanPowerOfTwo[
MultiplyDeBruijnBitPosition[UInt32(((ASize and -ASize) * $077CB531)) shr 27]];
FItemsThreshold := Pred(Round(FPrimaryNumberAsSizeApproximation * FMaxLoadFactor));
end;
end;
function TOpenAddressingQP<OPEN_ADDRESSING_CONSTRAINTS>.FindBucketIndex(constref AItems: TArray<TItem>;
constref AKey: TKey; out AHash: UInt32): SizeInt;
var
LItem: {TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.}_TItem; // for workaround Lazarus bug #25613
i: SizeInt;
LHash: UInt32;
begin
LHash := FEqualityComparer.GetHashCode(AKey);
i := 0;
AHash := LHash or UInt32.GetSignMask;
if Length(AItems) = 0 then
Exit(-1);
for i := 0 to FPrimaryNumberAsSizeApproximation - 1 do
begin
Result := TProbeSequence.Probe(i, AHash) mod FPrimaryNumberAsSizeApproximation;
LItem := _TItem(AItems[Result]);
// Empty position
if LItem.Hash = 0 then
Exit(not Result); // insert!
// Same position?
if LItem.Hash = AHash then
if FEqualityComparer.Equals(AKey, LItem.Pair.Key) then
Exit;
end;
Result := -1;
end;
function TOpenAddressingQP<OPEN_ADDRESSING_CONSTRAINTS>.FindBucketIndexOrTombstone(constref AItems: TArray<TItem>;
constref AKey: TKey; out AHash: UInt32): SizeInt;
var
LItem: {TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.}_TItem; // for workaround Lazarus bug #25613
i: SizeInt;
LHash: UInt32;
begin
LHash := FEqualityComparer.GetHashCode(AKey);
i := 0;
AHash := LHash or UInt32.GetSignMask;
if Length(AItems) = 0 then
Exit(-1);
for i := 0 to FPrimaryNumberAsSizeApproximation - 1 do
begin
Result := TProbeSequence.Probe(i, AHash) mod FPrimaryNumberAsSizeApproximation;
LItem := _TItem(AItems[Result]);
// Empty position or tombstone
if LItem.Hash and UInt32.GetSignMask = 0 then
Exit(not Result); // insert!
// Same position?
if LItem.Hash = AHash then
if FEqualityComparer.Equals(AKey, LItem.Pair.Key) then
Exit;
end;
Result := -1;
end;
{ TOpenAddressingDH<OPEN_ADDRESSING_CONSTRAINTS> }
constructor TOpenAddressingDH<OPEN_ADDRESSING_CONSTRAINTS>.Create(ACapacity: SizeInt;
const AComparer: IEqualityComparer<TKey>);
begin
end;
constructor TOpenAddressingDH<OPEN_ADDRESSING_CONSTRAINTS>.Create(const AComparer: IEqualityComparer<TKey>);
begin
end;
constructor TOpenAddressingDH<OPEN_ADDRESSING_CONSTRAINTS>.Create(ACollection: TEnumerable<TDictionaryPair>;
const AComparer: IEqualityComparer<TKey>);
begin
end;
{$IFDEF ENABLE_METHODS_WITH_TEnumerableWithPointers}
constructor TOpenAddressingDH<OPEN_ADDRESSING_CONSTRAINTS>.Create(ACollection: TEnumerableWithPointers<TDictionaryPair>;
const AComparer: IEqualityComparer<TKey>);
begin
end;
{$ENDIF}
constructor TOpenAddressingDH<OPEN_ADDRESSING_CONSTRAINTS>.Create(ACapacity: SizeInt);
begin
Create(ACapacity, TExtendedEqualityComparer<TKey>.Default(THashFactory));
end;
constructor TOpenAddressingDH<OPEN_ADDRESSING_CONSTRAINTS>.Create(ACollection: TEnumerable<TDictionaryPair>);
begin
Create(ACollection, TExtendedEqualityComparer<TKey>.Default(THashFactory));
end;
{$IFDEF ENABLE_METHODS_WITH_TEnumerableWithPointers}
constructor TOpenAddressingDH<OPEN_ADDRESSING_CONSTRAINTS>.Create(ACollection: TEnumerableWithPointers<TDictionaryPair>);
begin
Create(ACollection, TExtendedEqualityComparer<TKey>.Default(THashFactory));
end;
{$ENDIF}
constructor TOpenAddressingDH<OPEN_ADDRESSING_CONSTRAINTS>.Create(ACapacity: SizeInt;
const AComparer: IExtendedEqualityComparer<TKey>);
begin
FMaxLoadFactor := TProbeSequence.DEFAULT_LOAD_FACTOR;
FEqualityComparer := AComparer;
SetCapacity(ACapacity);
end;
constructor TOpenAddressingDH<OPEN_ADDRESSING_CONSTRAINTS>.Create(const AComparer: IExtendedEqualityComparer<TKey>);
begin
Create(0, AComparer);
end;
constructor TOpenAddressingDH<OPEN_ADDRESSING_CONSTRAINTS>.Create(ACollection: TEnumerable<TDictionaryPair>;
const AComparer: IExtendedEqualityComparer<TKey>);
var
LItem: TDictionaryPair;
begin
Create(AComparer);
for LItem in ACollection do
Add(LItem);
end;
{$IFDEF ENABLE_METHODS_WITH_TEnumerableWithPointers}
constructor TOpenAddressingDH<OPEN_ADDRESSING_CONSTRAINTS>.Create(ACollection: TEnumerableWithPointers<TDictionaryPair>;
const AComparer: IExtendedEqualityComparer<TKey>);
var
LItem: PDictionaryPair;
begin
Create(AComparer);
for LItem in ACollection.Ptr^ do
Add(LItem^);
end;
{$ENDIF}
procedure TOpenAddressingDH<OPEN_ADDRESSING_CONSTRAINTS>.UpdateItemsThreshold(ASize: SizeInt);
begin
inherited;
R :=
PrimaryNumbersJustLessThanPowerOfTwo[
MultiplyDeBruijnBitPosition[UInt32(((ASize and -ASize) * $077CB531)) shr 27]]
end;
function TOpenAddressingDH<OPEN_ADDRESSING_CONSTRAINTS>.FindBucketIndex(constref AItems: TArray<TItem>;
constref AKey: TKey; out AHash: UInt32): SizeInt;
var
LItem: {TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.}_TItem; // for workaround Lazarus bug #25613
LLengthMask: SizeInt;
i, m: SizeInt;
LHash: array[-1..1] of UInt32;
LHash1: UInt32 absolute LHash[0];
LHash2: UInt32 absolute LHash[1];
begin
m := Length(AItems);
LLengthMask := m - 1;
LHash[-1] := 2; // number of hashes
IExtendedEqualityComparer<TKey>(FEqualityComparer).GetHashList(AKey, @LHash[-1]);
i := 0;
AHash := LHash1 or UInt32.GetSignMask;
if m = 0 then
Exit(-1);
Result := LHash1 and LLengthMask;
// second hash function must be special
LHash2 := (R - (LHash2 mod R)) or 1;
repeat
LItem := _TItem(AItems[Result]);
// Empty position
if LItem.Hash = 0 then
Exit(not Result);
// Same position?
if LItem.Hash = AHash then
if FEqualityComparer.Equals(AKey, LItem.Pair.Key) then
Exit;
Inc(i);
Result := TProbeSequence.Probe(i, AHash, LHash2) and LLengthMask;
until false;
end;
function TOpenAddressingDH<OPEN_ADDRESSING_CONSTRAINTS>.FindBucketIndexOrTombstone(constref AItems: TArray<TItem>;
constref AKey: TKey; out AHash: UInt32): SizeInt;
var
LItem: {TOpenAddressing<OPEN_ADDRESSING_CONSTRAINTS>.}_TItem; // for workaround Lazarus bug #25613
LLengthMask: SizeInt;
i, m: SizeInt;
LHash: array[-1..1] of UInt32;
LHash1: UInt32 absolute LHash[0];
LHash2: UInt32 absolute LHash[1];
begin
m := Length(AItems);
LLengthMask := m - 1;
LHash[-1] := 2; // number of hashes
IExtendedEqualityComparer<TKey>(FEqualityComparer).GetHashList(AKey, @LHash[-1]);
i := 0;
AHash := LHash1 or UInt32.GetSignMask;
if m = 0 then
Exit(-1);
Result := LHash1 and LLengthMask;
// second hash function must be special
LHash2 := (R - (LHash2 mod R)) or 1;
repeat
LItem := _TItem(AItems[Result]);
// Empty position or tombstone
if LItem.Hash and UInt32.GetSignMask = 0 then
Exit(not Result);
// Same position?
if LItem.Hash = AHash then
if FEqualityComparer.Equals(AKey, LItem.Pair.Key) then
Exit;
Inc(i);
Result := TProbeSequence.Probe(i, AHash, LHash2) and LLengthMask;
until false;
end;
{ TDeamortizedDArrayCuckooMapEnumerator<T, CUCKOO_CONSTRAINTS> }
constructor TDeamortizedDArrayCuckooMapEnumerator<T, CUCKOO_CONSTRAINTS>.Create(
ADictionary: TCustomDictionary<CUSTOM_DICTIONARY_CONSTRAINTS>);
begin
inherited;
if ADictionary.Count = 0 then
FMainIndex := TCuckooCfg.D
else
FMainIndex := 0;
end;
function TDeamortizedDArrayCuckooMapEnumerator<T, CUCKOO_CONSTRAINTS>.DoMoveNext: Boolean;
var
LLength: SizeInt;
LArray: TItemsArray;
begin
Inc(FIndex);
if (FMainIndex = TCuckooCfg.D) then // queue
begin
LLength := Length(TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>(FDictionary).FQueue.FItems);
if FIndex >= LLength then
Exit(False);
while ((TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>(FDictionary).FQueue.FItems[FIndex].Hash)
and UInt32.GetSignMask) = 0 do
begin
Inc(FIndex);
if FIndex = LLength then
Exit(False);
end;
end
else // d-array
begin
LArray := TItemsArray(TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>(FDictionary).FItems[FMainIndex]);
LLength := Length(LArray);
if FIndex >= LLength then
begin
Inc(FMainIndex);
FIndex := -1;
Exit(DoMoveNext);
end;
while ((LArray[FIndex].Hash) and UInt32.GetSignMask) = 0 do
begin
Inc(FIndex);
if FIndex = LLength then
begin
Inc(FMainIndex);
FIndex := -1;
Exit(DoMoveNext);
end;
end;
end;
Result := True;
end;
{ TDeamortizedDArrayPointersEnumerator<TCuckooCfg, TItemsArray, TItemsDArray, TQueueDictionary, PDictionaryPair> }
function TDeamortizedDArrayPointersEnumerator<TCuckooCfg, TItemsArray, TItemsDArray, TQueueDictionary, PDictionaryPair>.DoMoveNext: boolean;
var
LLength: SizeInt;
LArray: TItemsArray;
begin
Inc(FIndex);
if (FMainIndex = TCuckooCfg.D) then // queue
begin
LLength := Length(FQueue.FItems);
if FIndex >= LLength then
Exit(False);
while ((FQueue.FItems[FIndex].Hash)
and UInt32.GetSignMask) = 0 do
begin
Inc(FIndex);
if FIndex = LLength then
Exit(False);
end;
end
else // d-array
begin
LArray := FItems^[FMainIndex];
LLength := Length(LArray);
if FIndex >= LLength then
begin
Inc(FMainIndex);
FIndex := -1;
Exit(DoMoveNext);
end;
while (((LArray[FIndex]).Hash) and UInt32.GetSignMask) = 0 do
begin
Inc(FIndex);
if FIndex = LLength then
begin
Inc(FMainIndex);
FIndex := -1;
Exit(DoMoveNext);
end;
end;
end;
Result := True;
end;
function TDeamortizedDArrayPointersEnumerator<TCuckooCfg, TItemsArray, TItemsDArray, TQueueDictionary, PDictionaryPair>.DoGetCurrent: PDictionaryPair;
begin
Result := GetCurrent;
end;
function TDeamortizedDArrayPointersEnumerator<TCuckooCfg, TItemsArray, TItemsDArray, TQueueDictionary, PDictionaryPair>.GetCurrent: PDictionaryPair;
begin
if FMainIndex = TCuckooCfg.D then
Result := @(FQueue.FItems[FIndex].Pair.Value.Pair)
else
Result := @((FItems^[FMainIndex])[FIndex].Pair);
end;
constructor TDeamortizedDArrayPointersEnumerator<TCuckooCfg, TItemsArray, TItemsDArray, TQueueDictionary, PDictionaryPair>.Create(var AItems; AQueue: TQueueDictionary; ACount: SizeInt);
begin
FIndex := -1;
if ACount = 0 then
FMainIndex := TCuckooCfg.D
else
FMainIndex := 0;
FQueue := AQueue;
FItems := @AItems;
end;
{ TDeamortizedDArrayPointersCollection<TPointersEnumerator, TItem, TQueueDictionary, PDictionaryPair> }
function TDeamortizedDArrayPointersCollection<TPointersEnumerator, TItemsDArray, TQueueDictionary, PDictionaryPair>.Items: PArray;
begin
Result := PArray(@((@Self)^));
end;
function TDeamortizedDArrayPointersCollection<TPointersEnumerator, TItemsDArray, TQueueDictionary, PDictionaryPair>.GetCount: SizeInt;
begin
Result := SizeInt((@PByte(@((@Self)^))[-SizeOf(SizeInt)])^);
end;
function TDeamortizedDArrayPointersCollection<TPointersEnumerator, TItemsDArray, TQueueDictionary, PDictionaryPair>.GetQueue: TQueueDictionary;
begin
Result := TQueueDictionary((@PByte(@((@Self)^))[SizeOf(TItemsDArray)])^);
end;
function TDeamortizedDArrayPointersCollection<TPointersEnumerator, TItemsDArray, TQueueDictionary, PDictionaryPair>.GetEnumerator: TPointersEnumerator;
begin
Result := TPointersEnumerator(TPointersEnumerator.NewInstance);
TPointersEnumerator(Result).Create(Items^, GetQueue, GetCount);
end;
function TDeamortizedDArrayPointersCollection<TPointersEnumerator, TItemsDArray, TQueueDictionary, PDictionaryPair>.ToArray: TArray<PDictionaryPair>;
{begin
Result := ToArrayImpl(FList.Count);
end;}
var
i: SizeInt;
LEnumerator: TPointersEnumerator;
begin
SetLength(Result, GetCount);
try
LEnumerator := GetEnumerator;
i := 0;
while LEnumerator.MoveNext do
begin
Result[i] := LEnumerator.Current;
Inc(i);
end;
finally
LEnumerator.Free;
end;
end;
{ TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS> }
function TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.TQueueDictionary.Rehash(ASizePow2: SizeInt;
AForce: boolean): Boolean;
var
FOldIdx: array of TKey;
i: SizeInt;
begin
SetLength(FOldIdx, FIdx.Count);
for i := 0 to FIdx.Count - 1 do
FOldIdx[i] := FItems[FIdx[i]].Pair.Key;
Result := inherited Rehash(ASizePow2, AForce);
for i := 0 to FIdx.Count - 1 do
FIdx[i] := FindBucketIndex(FOldIdx[i]);
end;
procedure TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.TQueueDictionary.NotifyIndexChange(AFrom, ATo: SizeInt);
var
i: SizeInt;
begin
// notify change position
for i := 0 to FIdx.Count-1 do
if FIdx[i] = AFrom then
begin
FIdx[i] := ATo;
Exit;
end;
end;
procedure TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.TQueueDictionary.InsertIntoBack(AItem: Pointer);
//var
// LItem: TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.PItem; absolute AItem; !!! bug #25917
var
LItem: TQueueDictionary.PValue absolute AItem;
LIndex: SizeInt;
begin
LIndex := DoAdd(LItem.Pair.Key, LItem^);
FIdx.Insert(0, LIndex);
end;
procedure TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.TQueueDictionary.InsertIntoHead(AItem: Pointer);
//var
// LItem: TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.PItem absolute AItem; !!! bug #25917
var
LItem: TQueueDictionary.PValue absolute AItem;
LIndex: SizeInt;
begin
LIndex := DoAdd(LItem.Pair.Key, LItem^);
FIdx.Add(LIndex);
end;
function TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.TQueueDictionary.IsEmpty: Boolean;
begin
Result := FIdx.Count = 0;
end;
function TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.TQueueDictionary.Pop: Pointer;
var
AIndex: SizeInt;
//LResult: TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.TItem; !!!bug #25917
begin
AIndex := FIdx.DoRemove(FIdx.Count - 1, cnExtracted);
Result := New(TQueueDictionary.PValue);
TQueueDictionary.PValue(Result)^ := DoRemove(AIndex, cnExtracted);
end;
constructor TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.TQueueDictionary.Create(ACapacity: SizeInt;
const AComparer: IEqualityComparer<TKey>);
begin
FIdx := TList<UInt32>.Create;
inherited Create(ACapacity, AComparer);
end;
destructor TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.TQueueDictionary.Destroy;
begin
FIdx.Free;
end;
function TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.GetQueueCount: SizeInt;
begin
Result := FQueue.Count;
end;
constructor TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.Create(ACapacity: SizeInt;
const AComparer: IEqualityComparer<TKey>);
begin
end;
constructor TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.Create(const AComparer: IEqualityComparer<TKey>);
begin
end;
constructor TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.Create(ACollection: TEnumerable<TDictionaryPair>;
const AComparer: IEqualityComparer<TKey>);
begin
end;
{$IFDEF ENABLE_METHODS_WITH_TEnumerableWithPointers}
constructor TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.Create(ACollection: TEnumerableWithPointers<TDictionaryPair>;
const AComparer: IEqualityComparer<TKey>);
begin
end;
{$ENDIF}
constructor TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.Create;
begin
Create(0);
end;
constructor TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.Create(ACapacity: SizeInt);
begin
Create(ACapacity, TExtendedEqualityComparer<TKey>.Default(THashFactory));
end;
constructor TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.Create(ACollection: TEnumerable<TDictionaryPair>);
begin
Create(ACollection, TExtendedEqualityComparer<TKey>.Default(THashFactory));
end;
{$IFDEF ENABLE_METHODS_WITH_TEnumerableWithPointers}
constructor TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.Create(ACollection: TEnumerableWithPointers<TDictionaryPair>);
begin
Create(ACollection, TExtendedEqualityComparer<TKey>.Default(THashFactory));
end;
{$ENDIF}
constructor TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.Create(ACapacity: SizeInt;
const AComparer: IExtendedEqualityComparer<TKey>);
begin
FMaxLoadFactor := TCuckooCfg.MAX_LOAD_FACTOR;
FQueue := TQueueDictionary.Create;
FCDM := TCDM.Create;
// to do - check constraint consts
if TCuckooCfg.D > THashFactory.MAX_HASHLIST_COUNT then
raise EArgumentOutOfRangeException.CreateRes(@SArgumentOutOfRange);
// should be moved to class constructor, but bug #24848
CUCKOO_SIGN := UInt32.GetSizedSignMask(THashFactory.HASH_FUNCTIONS_MASK_SIZE + 1);
CUCKOO_INDEX_SIZE := UInt32.GetBitsLength - (THashFactory.HASH_FUNCTIONS_MASK_SIZE + 1);
CUCKOO_HASH_SIGN := THashFactory.HASH_FUNCTIONS_MASK shl CUCKOO_INDEX_SIZE;
FEqualityComparer := AComparer;
SetCapacity(ACapacity);
end;
constructor TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.Create(const AComparer: IExtendedEqualityComparer<TKey>);
begin
Create(0, AComparer);
end;
constructor TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.Create(ACollection: TEnumerable<TDictionaryPair>;
const AComparer: IExtendedEqualityComparer<TKey>);
var
LItem: TDictionaryPair;
begin
Create(AComparer);
for LItem in ACollection do
Add(LItem);
end;
{$IFDEF ENABLE_METHODS_WITH_TEnumerableWithPointers}
constructor TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.Create(ACollection: TEnumerableWithPointers<TDictionaryPair>;
const AComparer: IExtendedEqualityComparer<TKey>);
var
LItem: PDictionaryPair;
begin
Create(AComparer);
for LItem in ACollection.Ptr^ do
Add(LItem^);
end;
{$ENDIF}
destructor TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.Destroy;
begin
inherited;
FQueue.Free;
FCDM.Free;
end;
function TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.GetKeys: TKeyCollection;
begin
if not Assigned(FKeys) then
FKeys := TKeyCollection.Create(Self);
Result := TKeyCollection(FKeys);
end;
function TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.GetValues: TValueCollection;
begin
if not Assigned(FValues) then
FValues := TValueCollection.Create(Self);
Result := TValueCollection(FValues);
end;
function TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.GetPointers: PPointersCollection;
begin
Result := PPointersCollection(@FItems);
end;
function TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.Lookup(constref AKey: TKey;
var AHashListOrIndex: PUInt32): SizeInt;
begin
Result := Lookup(FItems, AKey, AHashListOrIndex);
end;
function TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.Lookup(constref AItems: TItemsDArray; constref AKey: TKey;
var AHashListOrIndex: PUInt32): SizeInt;
var
LLengthMask: SizeInt;
i, j, k: SizeInt;
AHashList: PUInt32 absolute AHashListOrIndex;
AHashListParams: PUInt16 absolute AHashListOrIndex;
AIndex: PtrInt absolute AHashListOrIndex;
// LBloomFilter: UInt32; // to rethink. now is useless
begin
if Length(AItems[0]) = 0 then
Exit(LR_NIL);
LLengthMask := Length(AItems[0]) - 1;
AHashListParams[0] := TCuckooCfg.D; // number of hashes
i := 1; // ineks iteracji iteracji haszy
k := 1; // indeks iteracji haszy
// LBloomFilter := 0;
repeat
AHashListParams[1] := i; // iteration
IExtendedEqualityComparer<TKey>(FEqualityComparer).GetHashList(AKey, AHashList);
for j := 0 to THashFactory.HASHLIST_COUNT_PER_FUNCTION[i] - 1 do
begin
AHashList[k] := AHashList[k] or CUCKOO_SIGN;
// LBloomFilter := LBloomFilter or AHashList[k];
with AItems[k-1][AHashList[k] and LLengthMask] do
if (Hash and UInt32.GetSignMask) <> 0 then
if (AHashList[k] = Hash or CUCKOO_SIGN) and FEqualityComparer.Equals(AKey, Pair.Key) then
Exit(k-1);
Inc(k);
end;
Inc(i);
until k > TCuckooCfg.D;
i := FQueue.FindBucketIndex(AKey);
if i >= 0 then
begin
AIndex := i;
Exit(LR_QUEUE);
end;
{ LBloomFilter := not LBloomFilter;
for i := 0 to FDicQueueList.Count - 1 do
// with FQueue[i] do
if LBloomFilter and FQueue[i].Hash = 0 then
for j := 1 to TCuckooCfg.D do
if (FQueue[i].Hash or CUCKOO_SIGN = AHashList[j]) then
if FEqualityComparer.Equals(AKey, FQueue[i].Pair.Key) then
begin
AIndex := i;
Exit(LR_QUEUE);
end; }
Result := LR_NIL;
end;
procedure TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.PrepareAddingItem;
var
i: SizeInt;
begin
if FItemsLength > FItemsThreshold then
Rehash(Length(FItems[0]) shl 1)
else if FItemsThreshold = 0 then
begin
for i := 0 to TCuckooCfg.D - 1 do
SetLength(FItems[i], 4);
UpdateItemsThreshold(4);
end
else if FItemsLength = $40000001 then // High(TIndex) ... Error: Type mismatch
OutOfMemoryError;
end;
procedure TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.UpdateItemsThreshold(ASize: SizeInt);
var
LLength: SizeInt;
begin
LLength := ASize*TCuckooCfg.D;
if LLength = $40000000 then
FItemsThreshold := $40000001
else
FItemsThreshold := Pred(Round(LLength * FMaxLoadFactor));
end;
procedure TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.AddItem(constref AItems: TItemsDArray; constref AKey: TKey;
constref AValue: TValue; const AHashList: PUInt32);
var
LNewItem: TItem;
LPNewItem: PItem;
y: boolean = false;
b: UInt32;
LIndex: UInt32;
i, LLengthMask: SizeInt;
LTempItem: TItem;
LHashList: array[0..1] of UInt32;
LHashListParams: array[0..3] of UInt16 absolute LHashList;
begin
LLengthMask := Length(AItems[0]) - 1;
LNewItem.Pair.Key := AKey;
LNewItem.Pair.Value := AValue;
// by concept already sign bit is set
LNewItem.Hash := ((not CUCKOO_HASH_SIGN) and AHashList[1]) or UInt32.GetSignMask; // start at array [0]
FQueue.InsertIntoBack(@LNewItem);
for i := 0 to TCuckooCfg.L - 1 do
begin
if not y then
if FQueue.IsEmpty then
Exit
else
begin
LPNewItem := FQueue.Pop; // bug #25917 workaround
LNewItem := LPNewItem^;
Dispose(LPNewItem);
b := (LNewItem.Hash and CUCKOO_HASH_SIGN) shr CUCKOO_INDEX_SIZE;
y := true;
end;
LIndex := LNewItem.Hash and LLengthMask;
if (AItems[b][LIndex].Hash and UInt32.GetSignMask) = 0 then // insert!
begin
AItems[b][LIndex] := LNewItem;
FCDM.Clear;
y := false;
end
else
begin
if FCDM.ContainsKey(LNewItem.Pair.Key) then // found second cycle
begin
FQueue.InsertIntoBack(@LNewItem);
FCDM.Clear;
y := false;
end
else
begin
LTempItem := AItems[b][LIndex];
AItems[b][LIndex] := LNewItem;
LNewItem.Hash := LNewItem.Hash or CUCKOO_SIGN;
FCDM.AddOrSetValue(LNewItem.Pair.Key, EmptyRecord);
LNewItem := LTempItem;
b := b + 1;
if b >= TCuckooCfg.D then
b := 0;
LHashListParams[0] := -Succ(b);
IExtendedEqualityComparer<TKey>(FEqualityComparer).GetHashList(LNewItem.Pair.Key, @LHashList[0]);
LNewItem.Hash := (LHashList[1] and not CUCKOO_SIGN) or (b shl CUCKOO_INDEX_SIZE) or UInt32.GetSignMask;
// y := True; // always true in this place
end;
end;
end;
if y then
FQueue.InsertIntoHead(@LNewItem);
end;
procedure TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.DoAdd(const AKey: TKey; const AValue: TValue;
const AHashList: PUInt32);
begin
AddItem(FItems, AKey, AValue, AHashList);
Inc(FItemsLength);
KeyNotify(AKey, cnAdded);
ValueNotify(AValue, cnAdded);
end;
procedure TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.Add(constref AKey: TKey; constref AValue: TValue);
var
LHashList: array[0..TCuckooCfg.D] of UInt32;
LHashListOrIndex: PUint32;
begin
PrepareAddingItem;
LHashListOrIndex := @LHashList[0];
if Lookup(AKey, LHashListOrIndex) <> LR_NIL then
raise EListError.CreateRes(@SDuplicatesNotAllowed);
DoAdd(AKey, AValue, LHashListOrIndex);
end;
procedure TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.Add(constref APair: TPair<TKey, TValue>);
begin
Add(APair.Key, APair.Value);
end;
function TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.DoRemove(const AHashListOrIndex: PUInt32;
ALookupResult: SizeInt; ACollectionNotification: TCollectionNotification): TValue;
var
LItem: PItem;
LIndex: UInt32;
LQueueIndex: SizeInt absolute AHashListOrIndex;
LPair: TPair<TKey, TValue>;
begin
case ALookupResult of
LR_QUEUE:
LPair := FQueue.FItems[LQueueIndex].Pair.Value.Pair;
LR_NIL:
raise ERangeError.Create(SItemNotFound);
else
LIndex := AHashListOrIndex[ALookupResult + 1] and (Length(FItems[0]) - 1);
LItem := @FItems[ALookupResult][LIndex];
LItem.Hash := 0;
LPair := LItem.Pair;
LItem.Pair := Default(TPair<TKey, TValue>);
end;
Result := LPair.Value;
Dec(FItemsLength);
if ALookupResult = LR_QUEUE then
begin
FQueue.FIdx.Remove(LQueueIndex);
FQueue.DoRemove(LQueueIndex, cnRemoved);
end;
FCDM.Remove(LPair.Key); // item can exist in CDM
PairNotify(LPair, ACollectionNotification);
end;
procedure TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.Remove(constref AKey: TKey);
var
LHashList: array[0..TCuckooCfg.D] of UInt32;
LHashListOrIndex: PUint32;
LLookupResult: SizeInt;
begin
LHashListOrIndex := @LHashList[0];
LLookupResult := Lookup(AKey, LHashListOrIndex);
if LLookupResult = LR_NIL then
Exit;
DoRemove(LHashListOrIndex, LLookupResult, cnRemoved);
end;
function TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.ExtractPair(constref AKey: TKey): TPair<TKey, TValue>;
var
LHashList: array[0..TCuckooCfg.D] of UInt32;
LHashListOrIndex: PUint32;
LLookupResult: SizeInt;
begin
LHashListOrIndex := @LHashList[0];
LLookupResult := Lookup(AKey, LHashListOrIndex);
if LLookupResult = LR_NIL then
Exit(Default(TPair<TKey, TValue>));
Result.Key := AKey;
Result.Value := DoRemove(LHashListOrIndex, LLookupResult, cnExtracted);
end;
procedure TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.Clear;
var
LItem: PItem;
i, j: SizeInt;
LOldItems: TItemsDArray;
LOldQueueItems: TQueueDictionary.TItemsArray;
LQueueItem: TQueueDictionary._TItem;
begin
FItemsLength := 0;
FItemsThreshold := 0;
LOldItems := FItems;
for i := 0 to TCuckooCfg.D - 1 do
FItems[i] := nil;
for i := 0 to TCuckooCfg.D - 1 do
begin
for j := 0 to High(LOldItems[0]) do
begin
LItem := @LOldItems[i][j];
if (LItem.Hash and UInt32.GetSignMask <> 0) then
PairNotify(LItem.Pair, cnRemoved);
end;
end;
FCDM.Clear;
// queue
FQueue.FItemsLength := 0;
FQueue.FItemsThreshold := 0;
LOldQueueItems := FQueue.FItems;
FQueue.FItems := nil;
for i := 0 to High(LOldQueueItems) do
begin
LQueueItem := TQueueDictionary._TItem(LOldQueueItems[i]);
if (LQueueItem.Hash and UInt32.GetSignMask = 0) then
Continue;
PairNotify(LQueueItem.Pair.Value.Pair, cnRemoved);
end;
end;
procedure TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.Rehash(ASizePow2: SizeInt);
var
LNewItems: TItemsDArray;
i, j: SizeInt;
LItem: PItem;
LOldQueue: TQueueDictionary;
var
LHashList: array[0..1] of UInt32;
LHashListParams: array[0..3] of Int16 absolute LHashList;
begin
if ASizePow2 = Length(FItems[0]) then
Exit;
if ASizePow2 < 0 then
OutOfMemoryError;
for i := 0 to TCuckooCfg.D - 1 do
SetLength(LNewItems[i], ASizePow2);
LHashListParams[0] := -1;
// opportunity to clear the queue
LOldQueue := FQueue;
FCDM.Clear;
FQueue := TQueueDictionary.Create;
for i := 0 to LOldQueue.FIdx.Count - 1 do
begin
LItem := @LOldQueue.FItems[LOldQueue.FIdx[i]].Pair.Value;
LHashList[1] := FEqualityComparer.GetHashCode(LItem.Pair.Key);
AddItem(LNewItems, LItem.Pair.Key, LItem.Pair.Value, @LHashList[0]);
end;
LOldQueue.Free;
// copy the old elements
for i := 0 to TCuckooCfg.D - 1 do
for j := 0 to High(FItems[0]) do
begin
LItem := @FItems[i][j];
if (LItem.Hash and UInt32.GetSignMask) = 0 then
Continue;
// small optimization. most of items exist in table 0
if LItem.Hash and CUCKOO_HASH_SIGN = 0 then
begin
LHashList[1] := LItem.Hash;
AddItem(LNewItems, LItem.Pair.Key, LItem.Pair.Value, @LHashList[0]);
end
else
begin
LHashList[1] := FEqualityComparer.GetHashCode(LItem.Pair.Key);
AddItem(LNewItems, LItem.Pair.Key, LItem.Pair.Value, @LHashList[0]);
end;
end;
FItems := LNewItems;
UpdateItemsThreshold(ASizePow2);
end;
function TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.DoGetEnumerator: TEnumerator<TDictionaryPair>;
begin
Result := GetEnumerator;
end;
procedure TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.SetCapacity(ACapacity: SizeInt);
begin
if ACapacity < FItemsLength then
raise EArgumentOutOfRangeException.CreateRes(@SArgumentOutOfRange);
Resize(ACapacity);
end;
procedure TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.SetMaxLoadFactor(AValue: single);
var
LItemsLength: SizeInt;
begin
if (AValue > TCuckooCfg.MAX_LOAD_FACTOR) or (AValue <= 0) then
raise EArgumentOutOfRangeException.CreateRes(@SArgumentOutOfRange);
FMaxLoadFactor := AValue;
repeat
LItemsLength := Length(FItems[0]);
UpdateItemsThreshold(LItemsLength);
if FItemsLength > FItemsThreshold then
Rehash(LItemsLength shl 1);
until FItemsLength <= FItemsThreshold;
end;
function TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.GetLoadFactor: single;
begin
Result := FItemsLength / (Length(FItems[0]) * TCuckooCfg.D);
end;
function TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.GetCapacity: SizeInt;
begin
Result := Length(FItems[0]) * TCuckooCfg.D;
end;
procedure TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.Resize(ANewSize: SizeInt);
var
LNewSize: SizeInt;
begin
if ANewSize < 0 then
raise EArgumentOutOfRangeException.CreateRes(@SArgumentOutOfRange);
LNewSize := 0;
if ANewSize > 0 then
begin
LNewSize := 4;
while LNewSize * TCuckooCfg.D < ANewSize do
LNewSize := LNewSize shl 1;
end;
Rehash(LNewSize);
end;
function TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.GetEnumerator: TPairEnumerator;
begin
Result := TPairEnumerator.Create(Self);
end;
function TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.GetItem(const AKey: TKey): TValue;
var
LHashList: array[0..TCuckooCfg.D] of UInt32;
LHashListOrIndex: PUint32;
LLookupResult: SizeInt;
LIndex: UInt32;
begin
LHashListOrIndex := @LHashList[0];
LLookupResult := Lookup(AKey, LHashListOrIndex);
case LLookupResult of
LR_QUEUE:
Result := FQueue.FItems[PtrInt(LHashListOrIndex)].Pair.Value.Pair.Value;
LR_NIL:
raise EListError.CreateRes(@SDictionaryKeyDoesNotExist);
else
LIndex := LHashListOrIndex[LLookupResult + 1] and (Length(FItems[0]) - 1);
Result := FItems[LLookupResult][LIndex].Pair.Value;
end;
end;
procedure TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.TrimExcess;
begin
SetCapacity(Succ(FItemsLength));
FQueue.TrimExcess;
FQueue.FIdx.TrimExcess;
end;
procedure TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.SetItem(constref AValue: TValue;
const AHashListOrIndex: PUInt32; ALookupResult: SizeInt);
var
LIndex: UInt32;
begin
case ALookupResult of
LR_QUEUE:
SetValue(FQueue.FItems[PtrInt(AHashListOrIndex)].Pair.Value.Pair.Value, AValue);
LR_NIL:
raise EListError.CreateRes(@SItemNotFound);
else
LIndex := AHashListOrIndex[ALookupResult + 1] and (Length(FItems[0]) - 1);
SetValue(FItems[ALookupResult][LIndex].Pair.Value, AValue);
end;
end;
procedure TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.SetItem(const AKey: TKey; const AValue: TValue);
var
LHashList: array[0..TCuckooCfg.D] of UInt32;
LHashListOrIndex: PUint32;
LLookupResult: SizeInt;
begin
LHashListOrIndex := @LHashList[0];
LLookupResult := Lookup(AKey, LHashListOrIndex);
SetItem(AValue, LHashListOrIndex, LLookupResult);
end;
function TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.TryGetValue(constref AKey: TKey; out AValue: TValue): Boolean;
var
LHashList: array[0..TCuckooCfg.D] of UInt32;
LHashListOrIndex: PUint32;
LLookupResult: SizeInt;
LIndex: UInt32;
begin
LHashListOrIndex := @LHashList[0];
LLookupResult := Lookup(AKey, LHashListOrIndex);
Result := LLookupResult <> LR_NIL;
case LLookupResult of
LR_QUEUE:
AValue := FQueue.FItems[PtrInt(LHashListOrIndex)].Pair.Value.Pair.Value;
LR_NIL:
AValue := Default(TValue);
else
LIndex := LHashListOrIndex[LLookupResult + 1] and (Length(FItems[0]) - 1);
AValue := FItems[LLookupResult][LIndex].Pair.Value;
end;
end;
procedure TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.AddOrSetValue(constref AKey: TKey; constref AValue: TValue);
var
LHashList: array[0..TCuckooCfg.D] of UInt32;
LHashListOrIndex: PUint32;
LLookupResult: SizeInt;
begin
LHashListOrIndex := @LHashList[0];
LLookupResult := Lookup(AKey, LHashListOrIndex);
if LLookupResult = LR_NIL then
Add(AKey, AValue)
// more optimal version for AddOrSetValue has some bug : see Test_CuckooD2_Notification
//begin
// PrepareAddingItem;
// DoAdd(AKey, AValue, LHashListOrIndex);
//end
else
SetItem(AValue, LHashListOrIndex, LLookupResult);
end;
function TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.ContainsKey(constref AKey: TKey): Boolean;
var
LHashList: array[0..TCuckooCfg.D] of UInt32;
LHashListOrIndex: PUint32;
begin
LHashListOrIndex := @LHashList[0];
Result := Lookup(AKey, LHashListOrIndex) <> LR_NIL;
end;
function TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.ContainsValue(constref AValue: TValue): Boolean;
begin
Result := ContainsValue(AValue, TEqualityComparer<TValue>.Default(THashFactory));
end;
function TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.ContainsValue(constref AValue: TValue;
const AEqualityComparer: IEqualityComparer<TValue>): Boolean;
var
i, j: SizeInt;
LItem: PItem;
begin
if Length(FItems[0]) = 0 then
Exit(False);
for i := 0 to TCuckooCfg.D - 1 do
for j := 0 to High(FItems[0]) do
begin
LItem := @FItems[i][j];
if (LItem.Hash and UInt32.GetSignMask) = 0 then
Continue;
if AEqualityComparer.Equals(AValue, LItem.Pair.Value) then
Exit(True);
end;
Result := False;
end;
procedure TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.GetMemoryLayout(
const AOnGetMemoryLayoutKeyPosition: TOnGetMemoryLayoutKeyPosition);
var
i, j, k: SizeInt;
begin
k := 0;
for i := 0 to TCuckooCfg.D - 1 do
for j := 0 to High(FItems[0]) do
begin
if FItems[i][j].Hash and UInt32.GetSignMask <> 0 then
AOnGetMemoryLayoutKeyPosition(Self, k);
inc(k);
end;
end;
{ TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.TPairEnumerator }
function TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.TPairEnumerator.GetCurrent: TPair<TKey, TValue>;
begin
if FMainIndex = TCuckooCfg.D then
Result := TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>(FDictionary).FQueue.FItems[FIndex].Pair.Value.Pair
else
Result := TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>(FDictionary).FItems[FMainIndex][FIndex].Pair;
end;
{ TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.TValueEnumerator }
function TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.TValueEnumerator.GetCurrent: TValue;
begin
if FMainIndex = TCuckooCfg.D then
Result := TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>(FDictionary).FQueue.FItems[FIndex].Pair.Value.Pair.Value
else
Result := TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>(FDictionary).FItems[FMainIndex][FIndex].Pair.Value;
end;
{ TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.TPValueEnumerator }
function TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.TPValueEnumerator.GetCurrent: PValue;
begin
if FMainIndex = TCuckooCfg.D then
Result := @(TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>(FDictionary).FQueue.FItems[FIndex].Pair.Value.Pair.Value)
else
Result := @(TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>(FDictionary).FItems[FMainIndex][FIndex].Pair.Value);
end;
{ TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.TKeyEnumerator }
function TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.TKeyEnumerator.GetCurrent: TKey;
begin
if FMainIndex = TCuckooCfg.D then
Result := TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>(FDictionary).FQueue.FItems[FIndex].Pair.Value.Pair.Key
else
Result := TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>(FDictionary).FItems[FMainIndex][FIndex].Pair.Key;
end;
{ TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.TPKeyEnumerator }
function TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.TPKeyEnumerator.GetCurrent: TKey;
begin
if FMainIndex = TCuckooCfg.D then
Result := @(TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>(FDictionary).FQueue.FItems[FIndex].Pair.Value.Pair.Key)
else
Result := @(TDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>(FDictionary).FItems[FMainIndex][FIndex].Pair.Key);
end;
{ TObjectDictionary<DICTIONARY_CONSTRAINTS> }
procedure TObjectDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.KeyNotify(
constref AKey: TKey; ACollectionNotification: TCollectionNotification);
begin
inherited;
if (doOwnsKeys in FOwnerships) and (ACollectionNotification = cnRemoved) then
TObject((@AKey)^).Free;
end;
procedure TObjectDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.ValueNotify(constref AValue: TValue;
ACollectionNotification: TCollectionNotification);
begin
inherited;
if (doOwnsValues in FOwnerships) and (ACollectionNotification = cnRemoved) then
TObject((@AValue)^).Free;
end;
constructor TObjectDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.Create(
AOwnerships: TDictionaryOwnerships);
begin
Create(AOwnerships, 0);
end;
constructor TObjectDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.Create(
AOwnerships: TDictionaryOwnerships; ACapacity: SizeInt);
begin
inherited Create(ACapacity);
FOwnerships := AOwnerships;
end;
constructor TObjectDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.Create(
AOwnerships: TDictionaryOwnerships; const AComparer: IExtendedEqualityComparer<TKey>);
begin
inherited Create(AComparer);
FOwnerships := AOwnerships;
end;
constructor TObjectDeamortizedDArrayCuckooMap<CUCKOO_CONSTRAINTS>.Create(
AOwnerships: TDictionaryOwnerships; ACapacity: SizeInt; const AComparer: IExtendedEqualityComparer<TKey>);
begin
inherited Create(ACapacity, AComparer);
FOwnerships := AOwnerships;
end;
procedure TObjectOpenAddressingLP<OPEN_ADDRESSING_CONSTRAINTS>.KeyNotify(
constref AKey: TKey; ACollectionNotification: TCollectionNotification);
begin
inherited;
if (doOwnsKeys in FOwnerships) and (ACollectionNotification = cnRemoved) then
TObject((@AKey)^).Free;
end;
procedure TObjectOpenAddressingLP<OPEN_ADDRESSING_CONSTRAINTS>.ValueNotify(
constref AValue: TValue; ACollectionNotification: TCollectionNotification);
begin
inherited;
if (doOwnsValues in FOwnerships) and (ACollectionNotification = cnRemoved) then
TObject((@AValue)^).Free;
end;
constructor TObjectOpenAddressingLP<OPEN_ADDRESSING_CONSTRAINTS>.Create(AOwnerships: TDictionaryOwnerships);
begin
Create(AOwnerships, 0);
end;
constructor TObjectOpenAddressingLP<OPEN_ADDRESSING_CONSTRAINTS>.Create(AOwnerships: TDictionaryOwnerships;
ACapacity: SizeInt);
begin
inherited Create(ACapacity);
FOwnerships := AOwnerships;
end;
constructor TObjectOpenAddressingLP<OPEN_ADDRESSING_CONSTRAINTS>.Create(AOwnerships: TDictionaryOwnerships;
const AComparer: IEqualityComparer<TKey>);
begin
inherited Create(AComparer);
FOwnerships := AOwnerships;
end;
constructor TObjectOpenAddressingLP<OPEN_ADDRESSING_CONSTRAINTS>.Create(AOwnerships: TDictionaryOwnerships;
ACapacity: SizeInt; const AComparer: IEqualityComparer<TKey>);
begin
inherited Create(ACapacity, AComparer);
FOwnerships := AOwnerships;
end;
|