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
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
// SPDX-FileCopyrightText: 2015 - 2025 Kohei Yoshida
//
// SPDX-License-Identifier: MIT
#include "mdds/global.hpp"
#include <cassert>
#include <cctype>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <ostream>
#include <iostream>
#include <iomanip>
#include <type_traits>
#include <cstring>
#include <functional>
#define HAS_STATIC_CONSTEXPR_MEMBER(MEMBER) \
template<typename T> \
class has_##MEMBER \
{ \
using y_type = char; \
using n_type = long; \
\
template<typename U> \
static y_type test(decltype(U::MEMBER)); \
template<typename U> \
static n_type test(...); \
\
public: \
static constexpr bool value = sizeof(test<T>(0)) == sizeof(y_type); \
};
namespace mdds {
namespace trie { namespace detail {
HAS_STATIC_CONSTEXPR_MEMBER(dump_packed_construction_state)
template<typename PackedT, typename HandlerT>
struct traverse_packed_buffer
{
using packed_type = PackedT;
void operator()(const packed_type& packed, HandlerT hdl) const
{
size_t n = packed.size();
size_t i = 0;
hdl.root_offset(i, packed[i]);
++i;
while (i < n)
{
hdl.node_value(i, packed[i]);
++i;
hdl.node_index_size(i, packed[i]);
size_t index_size = packed[i];
++i;
index_size /= 2;
for (size_t j = 0; j < index_size; ++j)
{
hdl.node_child_key(i, packed[i]);
++i;
hdl.node_child_offset(i, packed[i]);
++i;
}
}
}
};
template<typename TrieT, typename PackedT>
struct dump_packed_buffer
{
using packed_type = PackedT;
using key_type = typename TrieT::key_type;
using key_unit_type = typename key_type::value_type;
using value_type = typename TrieT::value_type;
using pack_value_type = typename TrieT::traits_type::pack_value_type;
static constexpr auto null_value = TrieT::null_value;
void operator()(std::ostream& os, const packed_type& packed)
{
os << std::setfill('0') << "---\n"
<< "# packed buffer layout\n"
<< "buffer size: " << packed.size() << "\n"
<< "layout:" << std::endl;
int width = std::floor(std::log10(packed.size())) + 1;
struct _handler
{
std::ostream& m_os;
const int m_width = 0;
std::size_t m_this_node_offset = 0;
_handler(std::ostream& _os, int width) : m_os(_os), m_width(width)
{}
/** first element in the buffer. */
void root_offset(size_t i, pack_value_type v) const
{
m_os << " " << std::setw(m_width) << i << ": root node offset (" << v << ")" << std::endl;
}
/** first element in each node section. */
void node_value(size_t i, pack_value_type v)
{
m_this_node_offset = i;
m_os << " " << std::setw(m_width) << i << ": node value pos (";
if (v == null_value)
m_os << "null";
else
m_os << +v;
m_os << ")" << std::endl;
}
/**
* second element in each node section that stores the size of
* the child data sub-section.
*/
void node_index_size(size_t i, pack_value_type v) const
{
m_os << " " << std::setw(m_width) << i << ": index size (" << size_t(v) << ")" << std::endl;
}
/** element that stores the key value for child node. */
void node_child_key(size_t i, pack_value_type v) const
{
key_unit_type key = v;
m_os << " " << std::setw(m_width) << i << ": key (" << key << ")" << std::endl;
}
/** element that stores the relative offset of the child node. */
void node_child_offset(size_t i, pack_value_type v) const
{
size_t offset = v;
m_os << " " << std::setw(m_width) << i << ": offset (rel=" << offset
<< "; abs=" << (m_this_node_offset - offset) << ")" << std::endl;
}
} handler(os, width);
traverse_packed_buffer<packed_type, _handler>{}(packed, handler);
}
};
template<typename TrieT, typename PackedT, typename NodeT, typename = void>
struct dump_packed_construction_state
{
void operator()(std::ostream&, const PackedT&, const NodeT&)
{}
};
template<typename TrieT, typename PackedT, typename NodeT>
struct dump_packed_construction_state<
TrieT, PackedT, NodeT,
typename std::enable_if_t<has_dump_packed_construction_state<typename TrieT::traits_type>::value>>
{
using trie_node = NodeT;
void operator()(std::ostream& os, const PackedT& packed, const NodeT& root)
{
if constexpr (!TrieT::traits_type::dump_packed_construction_state)
return;
os << "---\n"
<< "# input entries" << std::endl;
typename TrieT::key_type buffer;
traverse_node(os, buffer, root);
dump_packed_buffer<TrieT, PackedT>{}(os, packed);
}
private:
void traverse_node(std::ostream& os, typename TrieT::key_type& buffer, const NodeT& node)
{
if (node.value)
{
// This node has value.
os << "- key: " << buffer << "\n"
<< " value: " << *node.value << std::endl;
}
for (const trie_node* p : node.children)
{
const trie_node& this_node = *p;
buffer.push_back(this_node.key);
traverse_node(os, buffer, this_node);
buffer.pop_back();
}
}
};
template<typename PackedT>
void verify_packed_position(const PackedT& packed, const typename PackedT::value_type* pos)
{
const auto* end_pos = packed.data() + packed.size();
if (packed.data() <= pos && pos < end_pos)
return;
std::ostringstream os;
os << "stored position is outside the valid data region (pos=" << pos << "; packed-begin=" << packed.data()
<< "; packed-end=" << end_pos;
throw integrity_error(os.str());
}
template<typename PackValueT, typename KeyUnitT, typename SizeT>
const PackValueT* find_child_pos(const PackValueT* first_child, SizeT n_child_nodes, KeyUnitT c)
{
using size_type = SizeT;
using key_unit_type = KeyUnitT;
for (size_type low = 0, high = n_child_nodes - 1; low <= high;)
{
size_type i = (low + high) / 2; // take the mid position
// each slot contains the key for the child and offset to its index position
const auto* child_pos = first_child + i * 2;
const key_unit_type node_key = *child_pos;
if (c == node_key)
{
// Match found!
return child_pos;
}
if (low == high)
// No more child node key to test. Bail out.
break;
if (high - low == 1)
{
// Only two more child keys left.
if (i == low)
low = high;
else
{
assert(i == high);
high = low;
}
}
else if (c < node_key)
// Move on to the lower sub-group.
high = i;
else
// Move on to the higher sub-group.
low = i;
}
// no match found
return nullptr;
}
template<typename PackValueT, typename KeyUnitT, typename SizeT>
const PackValueT* find_prefix_node(
const PackValueT* p, const KeyUnitT* prefix, const KeyUnitT* prefix_end,
std::function<void(const PackValueT*, const PackValueT*, const PackValueT*)> node_func)
{
using size_type = SizeT;
if (prefix == prefix_end)
{
// target node found!
size_t index_size = *(p + 1);
const auto* child_pos = p + 2;
const auto* child_end = child_pos + index_size;
node_func(p, child_pos, child_end);
return p;
}
const auto* p0 = p; // store the head offset position of this node.
// Find the child node with a matching key character.
++p;
size_type index_size = *p;
size_type n = index_size / 2; // number of child nodes
++p;
if (!n)
{
// This is a leaf node - no more child nodes to test
node_func(nullptr, nullptr, nullptr);
return nullptr;
}
const auto* child_end = p + index_size;
if (const auto* child_pos = find_child_pos(p, n, *prefix); child_pos)
{
// Match found!
size_type offset = *(child_pos + 1);
node_func(p0, child_pos, child_end);
const auto* p_child = p0 - offset;
++prefix;
return find_prefix_node<PackValueT, KeyUnitT, SizeT>(p_child, prefix, prefix_end, std::move(node_func));
}
// no matching node found - search failed
node_func(nullptr, nullptr, nullptr);
return nullptr;
}
inline const char* value_type_size_name(bool variable_size)
{
return variable_size ? "varaible size" : "fixed size";
}
union bin_value
{
char buffer[8];
uint8_t ui8;
uint16_t ui16;
uint32_t ui32;
uint64_t ui64;
};
using value_addrs_type = std::map<const void*, size_t>;
template<typename FuncT, typename ValueT>
struct write_variable_size_values_to_ostream
{
void operator()(std::ostream& os, const std::deque<ValueT>& value_store) const
{
bin_value bv;
size_t pos = 0;
for (const ValueT& v : value_store)
{
auto sp_size = os.tellp(); // position to come back to to write the size.
bv.ui32 = 0;
os.write(bv.buffer, 4); // write 0 as a placeholder.
auto sp_start = os.tellp();
FuncT::write(os, v);
auto sp_end = os.tellp();
bv.ui32 = sp_end - sp_start; // bytes written
// go back and write the actual bytes written.
os.seekp(sp_size);
os.write(bv.buffer, 4);
os.seekp(sp_end);
++pos;
}
}
};
template<typename FuncT, typename ValueT>
struct write_fixed_size_values_to_ostream
{
void operator()(std::ostream& os, const std::deque<ValueT>& value_store) const
{
bin_value bv;
// Write the size of constant-size values.
bv.ui32 = FuncT::value_size;
os.write(bv.buffer, 4);
size_t pos = 0;
for (const ValueT& v : value_store)
{
auto sp_start = os.tellp();
FuncT::write(os, v);
auto sp_end = os.tellp();
size_t bytes_written = sp_end - sp_start;
if (bytes_written != FuncT::value_size)
{
std::ostringstream msg;
msg << "bytes written (" << bytes_written << ") does not equal the value size (" << FuncT::value_size
<< ")";
throw size_error(msg.str());
}
++pos;
}
}
};
template<typename FuncT, typename ValueT, typename SizeTrait>
struct write_values_to_ostream;
template<typename FuncT, typename ValueT>
struct write_values_to_ostream<FuncT, ValueT, std::true_type> : write_variable_size_values_to_ostream<FuncT, ValueT>
{
};
template<typename FuncT, typename ValueT>
struct write_values_to_ostream<FuncT, ValueT, std::false_type> : write_fixed_size_values_to_ostream<FuncT, ValueT>
{
};
template<typename FuncT, typename ValueT>
struct read_fixed_size_values_from_istream
{
using value_store_type = std::deque<ValueT>;
value_store_type operator()(std::istream& is, uint32_t value_count) const
{
value_store_type value_store;
bin_value bv;
// read the size of the value.
is.read(bv.buffer, 4);
size_t size = bv.ui32;
if (size != FuncT::value_size)
{
std::ostringstream os;
os << "wrong size of fixed value type (expected: " << FuncT::value_size << "; actual: " << size << ")";
throw std::invalid_argument(os.str());
}
for (uint32_t i = 0; i < value_count; ++i)
{
value_store.emplace_back();
FuncT::read(is, size, value_store.back());
}
return value_store;
}
};
template<typename FuncT, typename ValueT>
struct read_variable_size_values_from_istream
{
using value_store_type = std::deque<ValueT>;
value_store_type operator()(std::istream& is, uint32_t value_count) const
{
value_store_type value_store;
bin_value bv;
for (uint32_t i = 0; i < value_count; ++i)
{
is.read(bv.buffer, 4);
size_t size = bv.ui32;
ValueT v;
FuncT::read(is, size, v);
value_store.push_back(std::move(v));
}
return value_store;
}
};
template<typename FuncT, typename ValueT, typename SizeTrait>
struct read_values_from_istream;
template<typename FuncT, typename ValueT>
struct read_values_from_istream<FuncT, ValueT, std::true_type> : read_variable_size_values_from_istream<FuncT, ValueT>
{
};
template<typename FuncT, typename ValueT>
struct read_values_from_istream<FuncT, ValueT, std::false_type> : read_fixed_size_values_from_istream<FuncT, ValueT>
{
};
}} // namespace trie::detail
namespace trie {
template<typename T>
void numeric_value_serializer<T>::write(std::ostream& os, const T& v)
{
static_assert(std::is_arithmetic<T>::value, "not a numeric type.");
constexpr size_t s = sizeof(T);
const char* p = reinterpret_cast<const char*>(&v);
os.write(p, s);
}
template<typename T>
void numeric_value_serializer<T>::read(std::istream& is, size_t n, T& v)
{
static_assert(std::is_arithmetic<T>::value, "not a numeric type.");
constexpr size_t s = sizeof(T);
assert(s == n);
union
{
char buffer[s];
T v;
} buf;
char* p = buf.buffer;
while (n)
{
is.read(p, s);
auto size_read = is.gcount();
n -= size_read;
p += size_read;
}
v = buf.v;
}
template<typename T>
void numeric_sequence_value_serializer<T>::write(std::ostream& os, const T& v)
{
static_assert(
std::is_arithmetic<typename T::value_type>::value, "value type of this vector is not a numeric type.");
for (const auto& elem : v)
element_serializer::write(os, elem);
}
template<typename T>
void numeric_sequence_value_serializer<T>::read(std::istream& is, size_t n, T& v)
{
using elem_type = typename T::value_type;
static_assert(
std::is_arithmetic<typename T::value_type>::value, "value type of this vector is not a numeric type.");
constexpr size_t elem_size = element_serializer::value_size;
assert(n % elem_size == 0);
size_t elem_count = n / elem_size;
for (size_t i = 0; i < elem_count; ++i)
{
elem_type elem;
element_serializer::read(is, elem_size, elem);
v.push_back(elem);
}
}
template<>
inline void variable_value_serializer<std::string>::write(std::ostream& os, const std::string& v)
{
os.write(v.data(), v.size());
}
template<>
inline void variable_value_serializer<std::string>::read(std::istream& is, size_t n, std::string& v)
{
v.resize(n);
char* p = const_cast<char*>(v.data());
while (n)
{
is.read(p, n);
auto size_read = is.gcount();
n -= size_read;
p += size_read;
}
}
} // namespace trie
template<typename KeyT, typename ValueT, typename TraitsT>
trie_map<KeyT, ValueT, TraitsT>::trie_map::trie_node::trie_node() : value{}, has_value(false)
{}
template<typename KeyT, typename ValueT, typename TraitsT>
trie_map<KeyT, ValueT, TraitsT>::trie_map::trie_node::trie_node(const trie_node& other)
: children(other.children), value(other.value), has_value(other.has_value)
{}
template<typename KeyT, typename ValueT, typename TraitsT>
trie_map<KeyT, ValueT, TraitsT>::trie_map::trie_node::trie_node(trie_node&& other)
: children(std::move(other.children)), value(std::move(other.value)), has_value(std::move(other.has_value))
{}
template<typename KeyT, typename ValueT, typename TraitsT>
void trie_map<KeyT, ValueT, TraitsT>::trie_map::trie_node::swap(trie_node& other)
{
children.swap(other.children);
std::swap(value, other.value);
std::swap(has_value, other.has_value);
}
template<typename KeyT, typename ValueT, typename TraitsT>
trie_map<KeyT, ValueT, TraitsT>::const_node_type::const_node_type(const trie_node* ref_node) : m_ref_node(ref_node)
{}
template<typename KeyT, typename ValueT, typename TraitsT>
trie_map<KeyT, ValueT, TraitsT>::const_node_type::const_node_type()
{}
template<typename KeyT, typename ValueT, typename TraitsT>
trie_map<KeyT, ValueT, TraitsT>::const_node_type::const_node_type(const const_node_type& other)
: m_ref_node(other.m_ref_node)
{}
template<typename KeyT, typename ValueT, typename TraitsT>
auto trie_map<KeyT, ValueT, TraitsT>::const_node_type::operator=(const const_node_type& other) -> const_node_type&
{
m_ref_node = other.m_ref_node;
return *this;
}
template<typename KeyT, typename ValueT, typename TraitsT>
bool trie_map<KeyT, ValueT, TraitsT>::const_node_type::valid() const
{
return m_ref_node != nullptr;
}
template<typename KeyT, typename ValueT, typename TraitsT>
bool trie_map<KeyT, ValueT, TraitsT>::const_node_type::has_child() const
{
if (!m_ref_node)
return false;
return !m_ref_node->children.empty();
}
template<typename KeyT, typename ValueT, typename TraitsT>
bool trie_map<KeyT, ValueT, TraitsT>::const_node_type::has_value() const
{
if (!m_ref_node)
return false;
return m_ref_node->has_value;
}
template<typename KeyT, typename ValueT, typename TraitsT>
auto trie_map<KeyT, ValueT, TraitsT>::const_node_type::value() const -> const value_type&
{
return m_ref_node->value;
}
template<typename KeyT, typename ValueT, typename TraitsT>
auto trie_map<KeyT, ValueT, TraitsT>::const_node_type::child(key_unit_type c) const -> const_node_type
{
if (!m_ref_node)
return const_node_type();
auto it = m_ref_node->children.find(c);
if (it == m_ref_node->children.end())
return const_node_type();
return const_node_type(&it->second);
}
template<typename KeyT, typename ValueT, typename TraitsT>
trie_map<KeyT, ValueT, TraitsT>::trie_map()
{}
template<typename KeyT, typename ValueT, typename TraitsT>
trie_map<KeyT, ValueT, TraitsT>::trie_map(const trie_map& other) : m_root(other.m_root)
{}
template<typename KeyT, typename ValueT, typename TraitsT>
trie_map<KeyT, ValueT, TraitsT>::trie_map(trie_map&& other) : m_root(std::move(other.m_root))
{}
template<typename KeyT, typename ValueT, typename TraitsT>
typename trie_map<KeyT, ValueT, TraitsT>::const_iterator trie_map<KeyT, ValueT, TraitsT>::begin() const
{
if (m_root.children.empty())
// empty container
return end();
// Push the root node.
key_type buf;
const_node_stack_type node_stack;
node_stack.emplace_back(&m_root, m_root.children.begin());
// Push root's first child node.
auto it = node_stack.back().child_pos;
const_iterator::push_child_node_to_stack(node_stack, buf, it);
// In theory there should always be at least one value node along the
// left-most branch.
while (!node_stack.back().node->has_value)
{
auto this_it = node_stack.back().child_pos;
const_iterator::push_child_node_to_stack(node_stack, buf, this_it);
}
return const_iterator(std::move(node_stack), std::move(buf), trie::detail::iterator_type::normal);
}
template<typename KeyT, typename ValueT, typename TraitsT>
typename trie_map<KeyT, ValueT, TraitsT>::iterator trie_map<KeyT, ValueT, TraitsT>::begin()
{
if (m_root.children.empty())
// empty container
return end();
// Push the root node.
key_type buf;
node_stack_type node_stack;
node_stack.emplace_back(&m_root, m_root.children.begin());
// Push root's first child node.
auto it = node_stack.back().child_pos;
iterator::push_child_node_to_stack(node_stack, buf, it);
// In theory there should always be at least one value node along the
// left-most branch.
while (!node_stack.back().node->has_value)
{
auto this_it = node_stack.back().child_pos;
iterator::push_child_node_to_stack(node_stack, buf, this_it);
}
return iterator(std::move(node_stack), std::move(buf), trie::detail::iterator_type::normal);
}
template<typename KeyT, typename ValueT, typename TraitsT>
typename trie_map<KeyT, ValueT, TraitsT>::const_iterator trie_map<KeyT, ValueT, TraitsT>::end() const
{
const_node_stack_type node_stack;
node_stack.emplace_back(&m_root, m_root.children.end());
return const_iterator(std::move(node_stack), key_type(), trie::detail::iterator_type::end);
}
template<typename KeyT, typename ValueT, typename TraitsT>
typename trie_map<KeyT, ValueT, TraitsT>::iterator trie_map<KeyT, ValueT, TraitsT>::end()
{
node_stack_type node_stack;
node_stack.emplace_back(&m_root, m_root.children.end());
return iterator(std::move(node_stack), key_type(), trie::detail::iterator_type::end);
}
template<typename KeyT, typename ValueT, typename TraitsT>
trie_map<KeyT, ValueT, TraitsT>& trie_map<KeyT, ValueT, TraitsT>::operator=(trie_map other)
{
trie_map tmp(std::move(other));
tmp.swap(*this);
return *this;
}
template<typename KeyT, typename ValueT, typename TraitsT>
bool trie_map<KeyT, ValueT, TraitsT>::operator==(const trie_map& other) const
{
return descend_for_equality(m_root, other.m_root);
}
template<typename KeyT, typename ValueT, typename TraitsT>
bool trie_map<KeyT, ValueT, TraitsT>::operator!=(const trie_map& other) const
{
return !operator==(other);
}
template<typename KeyT, typename ValueT, typename TraitsT>
void trie_map<KeyT, ValueT, TraitsT>::swap(trie_map& other)
{
m_root.swap(other.m_root);
}
template<typename KeyT, typename ValueT, typename TraitsT>
auto trie_map<KeyT, ValueT, TraitsT>::root_node() const -> const_node_type
{
return const_node_type(&m_root);
}
template<typename KeyT, typename ValueT, typename TraitsT>
void trie_map<KeyT, ValueT, TraitsT>::insert(const key_type& key, value_type value)
{
const key_unit_type* p = key.data();
size_t n = key.size();
const key_unit_type* p_end = p + n;
insert_into_tree(m_root, p, p_end, std::move(value));
}
template<typename KeyT, typename ValueT, typename TraitsT>
void trie_map<KeyT, ValueT, TraitsT>::insert(const key_unit_type* key, size_type len, value_type value)
{
const key_unit_type* key_end = key + len;
insert_into_tree(m_root, key, key_end, std::move(value));
}
template<typename KeyT, typename ValueT, typename TraitsT>
bool trie_map<KeyT, ValueT, TraitsT>::erase(const key_unit_type* key, size_type len)
{
const key_unit_type* key_end = key + len;
const_node_stack_type node_stack;
find_prefix_node_with_stack(node_stack, m_root, key, key_end);
if (node_stack.empty() || !node_stack.back().node->has_value)
// Nothing is erased.
return false;
const trie_node* node = node_stack.back().node;
trie_node* node_mod = const_cast<trie_node*>(node);
node_mod->has_value = false;
// If this is a leaf node, remove it, and keep removing its parents until
// we reach a parent node that still has at least one child node.
while (!node->has_value && node->children.empty() && node_stack.size() > 1)
{
node_stack.pop_back();
auto& si = node_stack.back();
const_cast<trie_node*>(si.node)->children.erase(si.child_pos);
node = si.node;
}
return true;
}
template<typename KeyT, typename ValueT, typename TraitsT>
void trie_map<KeyT, ValueT, TraitsT>::insert_into_tree(
trie_node& node, const key_unit_type* key, const key_unit_type* key_end, value_type value)
{
if (key == key_end)
{
node.value = std::move(value);
node.has_value = true;
return;
}
key_unit_type c = *key;
auto it = node.children.lower_bound(c);
if (it == node.children.end() || node.children.key_comp()(c, it->first))
{
// Insert a new node.
it = node.children.insert(it, typename trie_node::children_type::value_type(c, trie_node()));
}
++key;
insert_into_tree(it->second, key, key_end, std::move(value));
}
template<typename KeyT, typename ValueT, typename TraitsT>
const typename trie_map<KeyT, ValueT, TraitsT>::trie_node* trie_map<KeyT, ValueT, TraitsT>::find_prefix_node(
const trie_node& node, const key_unit_type* prefix, const key_unit_type* prefix_end) const
{
if (prefix == prefix_end)
// Right node is found.
return &node;
auto it = node.children.find(*prefix);
if (it == node.children.end())
return nullptr;
++prefix;
return find_prefix_node(it->second, prefix, prefix_end);
}
template<typename KeyT, typename ValueT, typename TraitsT>
template<bool IsConst>
void trie_map<KeyT, ValueT, TraitsT>::find_prefix_node_with_stack(
std::vector<stack_item<IsConst>>& node_stack, mdds::detail::const_t<trie_node, IsConst>& node,
const key_unit_type* prefix, const key_unit_type* prefix_end) const
{
if (prefix == prefix_end)
{
// Right node is found.
node_stack.emplace_back(&node, node.children.begin());
return;
}
auto it = node.children.find(*prefix);
if (it == node.children.end())
return;
node_stack.emplace_back(&node, it);
++prefix;
find_prefix_node_with_stack(node_stack, it->second, prefix, prefix_end);
}
template<typename KeyT, typename ValueT, typename TraitsT>
template<bool IsConst>
typename trie_map<KeyT, ValueT, TraitsT>::key_type trie_map<KeyT, ValueT, TraitsT>::build_key_buffer_from_node_stack(
const std::vector<stack_item<IsConst>>& node_stack) const
{
// Build the key value from the stack.
key_type buf;
auto end = node_stack.end();
--end; // Skip the node with value which doesn't store a key element.
std::for_each(node_stack.begin(), end, [&](const stack_item<IsConst>& si) { buf.push_back(si.child_pos->first); });
return buf;
}
template<typename KeyT, typename ValueT, typename TraitsT>
void trie_map<KeyT, ValueT, TraitsT>::count_values(size_type& n, const trie_node& node) const
{
if (node.has_value)
++n;
std::for_each(
node.children.begin(), node.children.end(),
[&](const typename trie_node::children_type::value_type& v) { count_values(n, v.second); });
}
template<typename KeyT, typename ValueT, typename TraitsT>
bool trie_map<KeyT, ValueT, TraitsT>::descend_for_equality(const trie_node& left, const trie_node& right) const
{
if (left.has_value != right.has_value)
return false;
if (left.has_value && left.value != right.value)
return false;
if (left.children.size() != right.children.size())
return false;
auto it_lhs = left.children.cbegin();
auto it_rhs = right.children.cbegin();
for (; it_lhs != left.children.cend(); ++it_lhs, ++it_rhs)
{
if (it_lhs->first != it_rhs->first)
return false;
if (!descend_for_equality(it_lhs->second, it_rhs->second))
return false;
}
return true;
}
template<typename KeyT, typename ValueT, typename TraitsT>
typename trie_map<KeyT, ValueT, TraitsT>::const_iterator trie_map<KeyT, ValueT, TraitsT>::find(
const key_type& key) const
{
return find(key.data(), key.size());
}
template<typename KeyT, typename ValueT, typename TraitsT>
typename trie_map<KeyT, ValueT, TraitsT>::const_iterator trie_map<KeyT, ValueT, TraitsT>::find(
const key_unit_type* input, size_type len) const
{
const key_unit_type* input_end = input + len;
const_node_stack_type node_stack;
find_prefix_node_with_stack(node_stack, m_root, input, input_end);
if (node_stack.empty() || !node_stack.back().node->has_value)
// Specified key doesn't exist.
return end();
key_type buf = build_key_buffer_from_node_stack(node_stack);
return const_iterator(std::move(node_stack), std::move(buf), trie::detail::iterator_type::normal);
}
template<typename KeyT, typename ValueT, typename TraitsT>
typename trie_map<KeyT, ValueT, TraitsT>::iterator trie_map<KeyT, ValueT, TraitsT>::find(const key_type& key)
{
return find(key.data(), key.size());
}
template<typename KeyT, typename ValueT, typename TraitsT>
typename trie_map<KeyT, ValueT, TraitsT>::iterator trie_map<KeyT, ValueT, TraitsT>::find(
const key_unit_type* input, size_type len)
{
const key_unit_type* input_end = input + len;
node_stack_type node_stack;
find_prefix_node_with_stack(node_stack, m_root, input, input_end);
if (node_stack.empty() || !node_stack.back().node->has_value)
// Specified key doesn't exist.
return end();
key_type buf = build_key_buffer_from_node_stack(node_stack);
return iterator(std::move(node_stack), std::move(buf), trie::detail::iterator_type::normal);
}
template<typename KeyT, typename ValueT, typename TraitsT>
typename trie_map<KeyT, ValueT, TraitsT>::search_results trie_map<KeyT, ValueT, TraitsT>::prefix_search(
const key_type& key) const
{
return prefix_search(key.data(), key.size());
}
template<typename KeyT, typename ValueT, typename TraitsT>
typename trie_map<KeyT, ValueT, TraitsT>::search_results trie_map<KeyT, ValueT, TraitsT>::prefix_search(
const key_unit_type* prefix, size_type len) const
{
const key_unit_type* prefix_end = prefix + len;
const trie_node* node = find_prefix_node(m_root, prefix, prefix_end);
return search_results(node, key_type(prefix, len));
}
template<typename KeyT, typename ValueT, typename TraitsT>
typename trie_map<KeyT, ValueT, TraitsT>::size_type trie_map<KeyT, ValueT, TraitsT>::size() const
{
size_type n = 0;
count_values(n, m_root);
return n;
}
template<typename KeyT, typename ValueT, typename TraitsT>
bool trie_map<KeyT, ValueT, TraitsT>::empty() const noexcept
{
return m_root.children.empty() && !m_root.has_value;
}
template<typename KeyT, typename ValueT, typename TraitsT>
void trie_map<KeyT, ValueT, TraitsT>::clear()
{
m_root.children.clear();
m_root.has_value = false;
}
template<typename KeyT, typename ValueT, typename TraitsT>
typename trie_map<KeyT, ValueT, TraitsT>::packed_type trie_map<KeyT, ValueT, TraitsT>::pack()
{
return packed_type(trie::detail::move_to_pack{}, *this);
}
template<typename KeyT, typename ValueT, typename TraitsT>
void packed_trie_map<KeyT, ValueT, TraitsT>::traverse_range(
trie_node& root, node_pool_type& node_pool, const typename packed_trie_map<KeyT, ValueT, TraitsT>::entry* start,
const typename packed_trie_map<KeyT, ValueT, TraitsT>::entry* end, size_type pos)
{
const entry* p = start;
const entry* range_start = start;
const entry* range_end = nullptr;
key_unit_type range_char = 0;
size_type range_count = 0;
for (; p != end; ++p)
{
if (pos > p->keylen)
continue;
if (pos == p->keylen)
{
root.value = &p->value;
continue;
}
++range_count;
key_unit_type c = p->key[pos];
if (!range_char)
range_char = c;
else if (range_char != c)
{
// End of current character range.
range_end = p;
node_pool.emplace_back(range_char);
root.children.push_back(&node_pool.back());
traverse_range(*root.children.back(), node_pool, range_start, range_end, pos + 1);
range_start = range_end;
range_char = range_start->key[pos];
range_end = nullptr;
range_count = 1;
}
}
if (range_count)
{
assert(range_char);
node_pool.emplace_back(range_char);
root.children.push_back(&node_pool.back());
traverse_range(*root.children.back(), node_pool, range_start, end, pos + 1);
}
}
template<typename KeyT, typename ValueT, typename TraitsT>
typename packed_trie_map<KeyT, ValueT, TraitsT>::size_type packed_trie_map<KeyT, ValueT, TraitsT>::compact_node(
const trie_node& node)
{
std::vector<std::tuple<size_t, key_unit_type>> child_offsets;
child_offsets.reserve(node.children.size());
// Process child nodes first.
std::for_each(node.children.begin(), node.children.end(), [&](const trie_node* p) {
const trie_node& child_node = *p;
size_type child_offset = compact_node(child_node);
child_offsets.emplace_back(child_offset, child_node.key);
});
// Process this node.
size_type offset = m_packed.size();
if (node.value)
{
auto pos = push_value_to_store(trie::detail::copy_to_pack{}, *node.value);
m_packed.push_back(pack_value_type(pos));
}
else
m_packed.push_back(null_value);
push_child_offsets(offset, child_offsets);
return offset;
}
template<typename KeyT, typename ValueT, typename TraitsT>
template<typename ModeT, typename NodeT>
auto packed_trie_map<KeyT, ValueT, TraitsT>::compact_node(ModeT, NodeT& node) -> size_type
{
std::vector<std::tuple<size_t, key_unit_type>> child_offsets;
child_offsets.reserve(node.children.size());
// Process child nodes first.
std::for_each(node.children.begin(), node.children.end(), [&](auto& v) {
key_unit_type key = v.first;
size_type child_offset = compact_node(ModeT{}, v.second);
child_offsets.emplace_back(child_offset, key);
});
// Process this node.
size_type offset = m_packed.size();
if (node.has_value)
{
auto pos = push_value_to_store(ModeT{}, node.value);
m_packed.push_back(pack_value_type(pos));
}
else
m_packed.push_back(null_value);
push_child_offsets(offset, child_offsets);
return offset;
}
template<typename KeyT, typename ValueT, typename TraitsT>
void packed_trie_map<KeyT, ValueT, TraitsT>::check_value_size_or_throw() const
{
if (m_value_store.size() >= max_value_pos)
{
std::ostringstream os;
os << "maximum allowed number of values (" << (max_value_pos + 1) << ") exceeded";
throw size_error(os.str());
}
}
template<typename KeyT, typename ValueT, typename TraitsT>
auto packed_trie_map<KeyT, ValueT, TraitsT>::push_value_to_store(trie::detail::copy_to_pack, const value_type& value)
-> size_type
{
check_value_size_or_throw();
auto pos = m_value_store.size();
m_value_store.push_back(value); // copy the value object
return pos;
}
template<typename KeyT, typename ValueT, typename TraitsT>
auto packed_trie_map<KeyT, ValueT, TraitsT>::push_value_to_store(trie::detail::move_to_pack, value_type& value)
-> size_type
{
check_value_size_or_throw();
auto pos = m_value_store.size();
m_value_store.emplace_back(std::move(value)); // move the value object
return pos;
}
template<typename KeyT, typename ValueT, typename TraitsT>
void packed_trie_map<KeyT, ValueT, TraitsT>::push_child_offsets(
size_type offset, const child_offsets_type& child_offsets)
{
m_packed.push_back(pack_value_type(child_offsets.size() * 2));
std::for_each(child_offsets.begin(), child_offsets.end(), [&](const std::tuple<size_t, key_unit_type>& v) {
key_unit_type key = std::get<1>(v);
size_t child_offset = std::get<0>(v);
m_packed.push_back(key);
m_packed.push_back(offset - child_offset);
});
}
template<typename KeyT, typename ValueT, typename TraitsT>
void packed_trie_map<KeyT, ValueT, TraitsT>::init_pack()
{
packed_type init(size_t(1), pack_value_type(0));
m_packed.swap(init);
assert(m_packed.size() == 1);
}
template<typename KeyT, typename ValueT, typename TraitsT>
void packed_trie_map<KeyT, ValueT, TraitsT>::compact(const trie_node& root)
{
init_pack();
size_t root_offset = compact_node(root);
m_packed[0] = root_offset;
}
template<typename KeyT, typename ValueT, typename TraitsT>
template<typename ModeT, typename NodeT>
void packed_trie_map<KeyT, ValueT, TraitsT>::compact(ModeT, NodeT& root)
{
init_pack();
size_t root_offset = compact_node(ModeT{}, root);
m_packed[0] = root_offset;
}
template<typename KeyT, typename ValueT, typename TraitsT>
packed_trie_map<KeyT, ValueT, TraitsT>::const_node_type::const_node_type(
const packed_type* packed, const value_store_type* value_store, const pack_value_type* pos)
: m_packed(packed), m_value_store(value_store), m_pos(pos)
{}
template<typename KeyT, typename ValueT, typename TraitsT>
auto packed_trie_map<KeyT, ValueT, TraitsT>::const_node_type::operator=(const const_node_type& other)
-> const_node_type&
{
m_packed = other.m_packed;
m_value_store = other.m_value_store;
m_pos = other.m_pos;
return *this;
}
template<typename KeyT, typename ValueT, typename TraitsT>
bool packed_trie_map<KeyT, ValueT, TraitsT>::const_node_type::valid() const
{
return m_pos != nullptr;
}
template<typename KeyT, typename ValueT, typename TraitsT>
bool packed_trie_map<KeyT, ValueT, TraitsT>::const_node_type::has_child() const
{
if (!m_pos)
return false;
#ifdef MDDS_TRIE_MAP_DEBUG
trie::detail::verify_packed_position(*m_packed, m_pos);
#endif
size_type index_size = *(m_pos + 1);
return index_size > 0u;
}
template<typename KeyT, typename ValueT, typename TraitsT>
bool packed_trie_map<KeyT, ValueT, TraitsT>::const_node_type::has_value() const
{
if (!m_pos)
return false;
#ifdef MDDS_TRIE_MAP_DEBUG
trie::detail::verify_packed_position(*m_packed, m_pos);
#endif
return *m_pos != null_value;
}
template<typename KeyT, typename ValueT, typename TraitsT>
auto packed_trie_map<KeyT, ValueT, TraitsT>::const_node_type::value() const -> const value_type&
{
#ifdef MDDS_TRIE_MAP_DEBUG
trie::detail::verify_packed_position(*m_packed, m_pos);
#endif
assert(m_value_store);
assert(*m_pos < m_value_store->size());
return (*m_value_store)[*m_pos];
}
template<typename KeyT, typename ValueT, typename TraitsT>
auto packed_trie_map<KeyT, ValueT, TraitsT>::const_node_type::child(key_unit_type c) const -> const_node_type
{
if (!m_pos)
return const_node_type();
#ifdef MDDS_TRIE_MAP_DEBUG
trie::detail::verify_packed_position(*m_packed, m_pos);
#endif
size_type index_size = *(m_pos + 1);
if (!index_size)
// no more child nodes
return const_node_type();
size_type n = index_size / 2;
const pack_value_type* p = m_pos + 2;
if (const auto* child_pos = trie::detail::find_child_pos(p, n, c); child_pos)
{
// Match found!
size_type offset = *(child_pos + 1);
const auto* p_child = m_pos - offset;
return const_node_type(m_packed, m_value_store, p_child);
}
return const_node_type();
}
template<typename KeyT, typename ValueT, typename TraitsT>
packed_trie_map<KeyT, ValueT, TraitsT>::packed_trie_map(
trie::detail::move_to_pack, trie_map<KeyT, ValueT, TraitsT>& from)
{
compact(trie::detail::move_to_pack{}, from.m_root);
}
template<typename KeyT, typename ValueT, typename TraitsT>
packed_trie_map<KeyT, ValueT, TraitsT>::packed_trie_map() : m_packed(3, 0u)
{
static_assert(!std::is_signed_v<pack_value_type>);
static_assert(std::is_integral_v<pack_value_type>);
// root offset (0), root value (1), and root child count (2).
m_packed[0] = 1;
}
template<typename KeyT, typename ValueT, typename TraitsT>
packed_trie_map<KeyT, ValueT, TraitsT>::packed_trie_map(const entry* entries, size_type entry_size)
{
const entry* p = entries;
const entry* p_end = p + entry_size;
#if defined(MDDS_TRIE_MAP_DEBUG)
// Make sure the entries really are sorted.
auto func_compare = [](const entry& left, const entry& right) -> bool {
size_type n_key = std::min(left.keylen, right.keylen);
int ret = std::memcmp(left.key, right.key, n_key);
if (ret == 0)
return left.keylen < right.keylen;
return ret < 0;
};
if (!std::is_sorted(p, p_end, func_compare))
throw integrity_error("the list of entries is not sorted.");
#endif
// Populate the normal tree first.
trie_node root(0);
node_pool_type node_pool;
traverse_range(root, node_pool, p, p_end, 0);
// Compact the trie into a packed array.
compact(root);
trie::detail::dump_packed_construction_state<packed_trie_map, packed_type, trie_node>{}(std::cout, m_packed, root);
}
template<typename KeyT, typename ValueT, typename TraitsT>
packed_trie_map<KeyT, ValueT, TraitsT>::packed_trie_map(const trie_map<KeyT, ValueT, TraitsT>& other)
{
compact(trie::detail::copy_to_pack{}, other.m_root);
}
template<typename KeyT, typename ValueT, typename TraitsT>
packed_trie_map<KeyT, ValueT, TraitsT>::packed_trie_map(const packed_trie_map& other) : m_packed(other.m_packed)
{
struct _handler
{
packed_trie_map& m_parent;
const packed_trie_map& m_other;
void node(const pack_value_type* node_pos, const key_unit_type*, size_type, size_type)
{
pack_value_type other_value_pos = *node_pos;
if (other_value_pos != null_value)
{
assert(other_value_pos < m_other.m_value_store.size());
pack_value_type pos = m_parent.m_value_store.size();
m_parent.m_value_store.push_back(m_other.m_value_store[other_value_pos]); // copy the value object.
const auto* head = m_parent.m_packed.data();
size_t offset = std::distance(head, node_pos);
m_parent.m_packed[offset] = pos;
}
}
void move_up(const pack_value_type*, const pack_value_type*, const pack_value_type*)
{}
void move_down()
{}
void next_child()
{}
void end()
{}
_handler(packed_trie_map& parent, const packed_trie_map& other) : m_parent(parent), m_other(other)
{}
} handler(*this, other);
traverse_tree(handler);
}
template<typename KeyT, typename ValueT, typename TraitsT>
packed_trie_map<KeyT, ValueT, TraitsT>::packed_trie_map(packed_trie_map&& other)
: m_value_store(std::move(other.m_value_store)), m_packed(std::move(other.m_packed))
{
// Even the empty structure needs to have the root offset and the empty root record.
other.m_packed.resize(3, 0u); // root offset (0), root value (1), and root child count (2).
other.m_packed[0] = 1;
}
template<typename KeyT, typename ValueT, typename TraitsT>
packed_trie_map<KeyT, ValueT, TraitsT>& packed_trie_map<KeyT, ValueT, TraitsT>::operator=(packed_trie_map other)
{
packed_trie_map tmp(std::move(other));
tmp.swap(*this);
return *this;
}
template<typename KeyT, typename ValueT, typename TraitsT>
bool packed_trie_map<KeyT, ValueT, TraitsT>::operator==(const packed_trie_map& other) const
{
if (m_value_store.size() != other.m_value_store.size())
return false;
// Since the two containers are of the same size, the iterator ranges should
// be the same as well.
auto left = cbegin(), right = other.cbegin();
for (; left != cend(); ++left, ++right)
{
assert(right != other.cend());
if (*left != *right)
return false;
}
return true;
}
template<typename KeyT, typename ValueT, typename TraitsT>
bool packed_trie_map<KeyT, ValueT, TraitsT>::operator!=(const packed_trie_map& other) const
{
return !operator==(other);
}
template<typename KeyT, typename ValueT, typename TraitsT>
typename packed_trie_map<KeyT, ValueT, TraitsT>::const_iterator packed_trie_map<KeyT, ValueT, TraitsT>::begin() const
{
return cbegin();
}
template<typename KeyT, typename ValueT, typename TraitsT>
typename packed_trie_map<KeyT, ValueT, TraitsT>::const_iterator packed_trie_map<KeyT, ValueT, TraitsT>::end() const
{
return cend();
}
template<typename KeyT, typename ValueT, typename TraitsT>
typename packed_trie_map<KeyT, ValueT, TraitsT>::const_iterator packed_trie_map<KeyT, ValueT, TraitsT>::cbegin() const
{
node_stack_type node_stack = get_root_stack();
const stack_item* si = &node_stack.back();
if (si->child_pos == si->child_end)
// empty container.
return const_iterator(&m_value_store, std::move(node_stack), key_type());
const auto* node_pos = si->node_pos;
const auto* child_pos = si->child_pos;
const auto* child_end = si->child_end;
const auto* p = child_pos;
// Follow the root node's left-most child.
key_type buf;
key_unit_type c = *p;
buf.push_back(c);
++p;
size_type offset = *p;
node_pos -= offset; // jump to the child node.
p = node_pos;
++p;
size_type index_size = *p;
++p;
child_pos = p;
child_end = child_pos + index_size;
// Push this child node onto the stack.
node_stack.emplace_back(&m_value_store, node_pos, child_pos, child_end);
pack_value_type pos = *node_pos;
while (pos == null_value)
{
// Keep following the left child node until we reach a node with value.
const_iterator::push_child_node_to_stack(&m_value_store, node_stack, buf, node_stack.back().child_pos);
pos = *node_stack.back().node_pos;
}
return const_iterator(&m_value_store, std::move(node_stack), std::move(buf), pos);
}
template<typename KeyT, typename ValueT, typename TraitsT>
typename packed_trie_map<KeyT, ValueT, TraitsT>::const_iterator packed_trie_map<KeyT, ValueT, TraitsT>::cend() const
{
node_stack_type node_stack = get_root_stack();
node_stack.back().child_pos = node_stack.back().child_end;
return const_iterator(&m_value_store, std::move(node_stack), key_type());
}
template<typename KeyT, typename ValueT, typename TraitsT>
typename packed_trie_map<KeyT, ValueT, TraitsT>::node_stack_type packed_trie_map<
KeyT, ValueT, TraitsT>::get_root_stack() const
{
assert(!m_packed.empty());
size_type root_offset = m_packed[0];
assert(root_offset < m_packed.size());
const auto* p = m_packed.data() + root_offset;
const auto* node_pos = p;
++p;
size_t index_size = *p;
++p;
const auto* child_pos = p;
const auto* child_end = child_pos + index_size;
node_stack_type node_stack;
node_stack.emplace_back(&m_value_store, node_pos, child_pos, child_end);
return node_stack;
}
template<typename KeyT, typename ValueT, typename TraitsT>
typename packed_trie_map<KeyT, ValueT, TraitsT>::const_iterator packed_trie_map<KeyT, ValueT, TraitsT>::find(
const key_type& key) const
{
return find(key.data(), key.size());
}
template<typename KeyT, typename ValueT, typename TraitsT>
typename packed_trie_map<KeyT, ValueT, TraitsT>::const_iterator packed_trie_map<KeyT, ValueT, TraitsT>::find(
const key_unit_type* input, size_type len) const
{
if (m_value_store.empty())
return end();
const key_unit_type* key_end = input + len;
size_type root_offset = m_packed[0];
assert(root_offset < m_packed.size());
const auto* root = m_packed.data() + root_offset;
node_stack_type node_stack;
const auto* vs = &m_value_store;
trie::detail::find_prefix_node<pack_value_type, key_unit_type, size_type>(
root, input, key_end,
[&node_stack,
vs](const pack_value_type* node_pos, const pack_value_type* child_pos, const pack_value_type* child_end) {
node_stack.emplace_back(vs, node_pos, child_pos, child_end);
});
if (node_stack.empty() || !node_stack.back().node_pos)
return end();
const stack_item& si = node_stack.back();
pack_value_type v = *si.node_pos;
if (v == null_value)
return end();
// Build the key value from the stack.
key_type buf;
auto end = node_stack.end();
--end; // Skip the node with value which doesn't store a key element.
std::for_each(node_stack.begin(), end, [&](const stack_item& this_si) { buf.push_back(*this_si.child_pos); });
return const_iterator(&m_value_store, std::move(node_stack), std::move(buf), v);
}
template<typename KeyT, typename ValueT, typename TraitsT>
typename packed_trie_map<KeyT, ValueT, TraitsT>::search_results packed_trie_map<KeyT, ValueT, TraitsT>::prefix_search(
const key_type& key) const
{
return prefix_search(key.data(), key.size());
}
template<typename KeyT, typename ValueT, typename TraitsT>
typename packed_trie_map<KeyT, ValueT, TraitsT>::search_results packed_trie_map<KeyT, ValueT, TraitsT>::prefix_search(
const key_unit_type* prefix, size_type len) const
{
if (m_value_store.empty())
return search_results(&m_value_store, nullptr, key_type());
const key_unit_type* prefix_end = prefix + len;
size_type root_offset = m_packed[0];
assert(root_offset < m_packed.size());
const auto* root = m_packed.data() + root_offset;
const auto* node = trie::detail::find_prefix_node<pack_value_type, key_unit_type, size_type>(
root, prefix, prefix_end,
[](const pack_value_type*, const pack_value_type*, const pack_value_type*) noexcept {});
return search_results(&m_value_store, node, key_type(prefix, len));
}
template<typename KeyT, typename ValueT, typename TraitsT>
typename packed_trie_map<KeyT, ValueT, TraitsT>::size_type packed_trie_map<KeyT, ValueT, TraitsT>::size() const noexcept
{
return m_value_store.size();
}
template<typename KeyT, typename ValueT, typename TraitsT>
bool packed_trie_map<KeyT, ValueT, TraitsT>::empty() const noexcept
{
return m_value_store.empty();
}
template<typename KeyT, typename ValueT, typename TraitsT>
void packed_trie_map<KeyT, ValueT, TraitsT>::swap(packed_trie_map& other)
{
m_value_store.swap(other.m_value_store);
m_packed.swap(other.m_packed);
}
template<typename KeyT, typename ValueT, typename TraitsT>
auto packed_trie_map<KeyT, ValueT, TraitsT>::root_node() const -> const_node_type
{
assert(m_packed.size() >= 3u);
std::size_t root_offset = m_packed[0];
return const_node_type(&m_packed, &m_value_store, m_packed.data() + root_offset);
}
template<typename KeyT, typename ValueT, typename TraitsT>
template<typename FuncT>
void packed_trie_map<KeyT, ValueT, TraitsT>::save_state(std::ostream& os) const
{
trie::detail::bin_value bv;
bv.ui16 = 0x0000; // write 2-byte flags
bv.ui16 |= (0x0001 & FuncT::variable_size);
os.write(bv.buffer, 2);
// Write the number of values (4-bytes).
bv.ui32 = m_value_store.size();
os.write(bv.buffer, 4);
// Dump the stored values first.
using value_size_type = std::bool_constant<FuncT::variable_size>;
trie::detail::write_values_to_ostream<FuncT, value_type, value_size_type> func;
func(os, m_value_store);
// Write 0xFF to signify the end of the value section.
bv.ui8 = 0xFF;
os.write(bv.buffer, 1);
// Write the size of pack_value_type.
bv.ui8 = sizeof(pack_value_type);
os.write(bv.buffer, 1);
// Write the size of the packed blob.
bv.ui64 = m_packed.size();
os.write(bv.buffer, 8);
struct _handler
{
const std::size_t m_elem_size = sizeof(pack_value_type);
std::ostream& m_os;
inline void write(pack_value_type v) const
{
const char* p = reinterpret_cast<const char*>(&v);
m_os.write(p, m_elem_size);
}
/** first element in the buffer. */
void root_offset(size_t /*i*/, pack_value_type v) const
{
write(v);
}
/** first element in each node section. */
void node_value(size_t /*i*/, pack_value_type v) const
{
write(v);
}
/**
* second element in each node section that stores the size of
* the child data sub-section.
*/
void node_index_size(size_t /*i*/, pack_value_type v) const
{
write(v);
}
/** element that stores the key value for child node. */
void node_child_key(size_t /*i*/, pack_value_type v) const
{
write(v);
}
/** element that stores the relative offset of the child node. */
void node_child_offset(size_t /*i*/, pack_value_type v) const
{
write(v);
}
_handler(std::ostream& os) : m_os(os)
{}
} handler(os);
trie::detail::traverse_packed_buffer<packed_type, _handler>{}(m_packed, handler);
// Write 0xFF to signify the end of the packed blob.
bv.ui8 = 0xFF;
os.write(bv.buffer, 1);
}
template<typename KeyT, typename ValueT, typename TraitsT>
template<typename FuncT>
void packed_trie_map<KeyT, ValueT, TraitsT>::load_state(std::istream& is)
{
trie::detail::bin_value bv;
is.read(bv.buffer, 2);
uint16_t flags = bv.ui16;
bool variable_size = (flags & 0x0001) != 0;
if (variable_size != FuncT::variable_size)
{
std::ostringstream os;
os << "This stream is meant for a value type of " << trie::detail::value_type_size_name(variable_size)
<< ", but the actual value type is of " << trie::detail::value_type_size_name(FuncT::variable_size) << ".";
throw std::invalid_argument(os.str());
}
// read the number of values
is.read(bv.buffer, 4);
uint32_t value_count = bv.ui32;
using value_size_type = std::bool_constant<FuncT::variable_size>;
trie::detail::read_values_from_istream<FuncT, value_type, value_size_type> func;
m_value_store = func(is, value_count);
// There should be a check byte of 0xFF.
is.read(bv.buffer, 1);
if (bv.ui8 != 0xFF)
throw std::invalid_argument("failed to find the check byte at the end of the value section.");
// Size of pack_value_type
is.read(bv.buffer, 1);
size_type pvt_size = bv.ui8;
if (pvt_size != sizeof(pack_value_type))
throw std::invalid_argument("size of pack_value_type is different.");
// Size of the packed blob.
is.read(bv.buffer, 8);
size_t n = bv.ui64;
packed_type packed;
packed.reserve(n);
for (size_t i = 0; i < n; ++i)
{
is.read(bv.buffer, pvt_size);
const auto* p = reinterpret_cast<const pack_value_type*>(bv.buffer);
packed.push_back(*p);
}
// the last byte must be 0xFF.
is.read(bv.buffer, 1);
if (bv.ui8 != 0xFF)
throw std::invalid_argument("failed to find the check byte at the end of the packed blob section.");
m_packed.swap(packed);
struct _handler
{
packed_trie_map& m_parent;
void root_offset(size_t /*i*/, pack_value_type /*v*/) const
{}
void node_value(size_t i, pack_value_type v) const
{
m_parent.m_packed[i] = v;
}
void node_index_size(size_t /*i*/, pack_value_type /*v*/) const
{}
void node_child_key(size_t /*i*/, pack_value_type /*v*/) const
{}
void node_child_offset(size_t /*i*/, pack_value_type /*v*/) const
{}
_handler(packed_trie_map& parent) : m_parent(parent)
{}
} handler(*this);
trie::detail::traverse_packed_buffer<packed_type, _handler>{}(m_packed, handler);
}
template<typename KeyT, typename ValueT, typename TraitsT>
std::string packed_trie_map<KeyT, ValueT, TraitsT>::dump_structure(trie::dump_structure_type type) const
{
std::ostringstream os;
switch (type)
{
case trie::dump_structure_type::packed_buffer:
{
trie::detail::dump_packed_buffer<packed_trie_map, packed_type>{}(os, m_packed);
break;
}
case trie::dump_structure_type::trie_traversal:
{
dump_trie_traversal(os);
break;
}
}
return os.str();
}
template<typename KeyT, typename ValueT, typename TraitsT>
void packed_trie_map<KeyT, ValueT, TraitsT>::dump_trie_traversal(std::ostream& os) const
{
os << "---\n"
<< "# trie traversal (depth first)\n"
<< "value entries: " << m_value_store.size() << "\n"
<< "packed buffer size: " << m_packed.size() << "\n"
<< "traversal:\n";
const auto* head = m_packed.data();
struct _handler
{
const value_store_type& m_value_store;
std::ostream& m_os;
const pack_value_type* m_head = nullptr;
_handler(const value_store_type& value_store, std::ostream& os, const pack_value_type* head)
: m_value_store(value_store), m_os(os), m_head(head)
{}
void node(const pack_value_type* node_pos, const key_unit_type* c, size_type depth, size_type index_size)
{
auto value_pos = *node_pos;
size_type offset = std::distance(m_head, node_pos);
m_os << " - node:\n"
<< " offset: " << offset << " (abs)\n"
<< " key: ";
if (c)
m_os << *c;
else
m_os << "<null>";
m_os << "\n"
<< " depth: " << depth << "\n"
<< " child count: " << (index_size / 2) << "\n"
<< " value position: ";
if (value_pos == null_value)
m_os << "(null)";
else
m_os << value_pos << " (value=" << m_value_store[value_pos] << ")";
m_os << std::endl;
}
void move_up(
const pack_value_type* node_pos, const pack_value_type* child_pos, const pack_value_type* child_end)
{
size_type offset = std::distance(m_head, node_pos);
size_type child_size = std::distance(child_pos, child_end) / 2;
m_os << " - move up:\n"
<< " offset: " << offset << " (abs)\n"
<< " remaining child count: " << child_size << std::endl;
}
void move_down()
{
m_os << " - move down" << std::endl;
}
void next_child()
{
m_os << " - next child" << std::endl;
}
void end()
{
m_os << " - end of traversal" << std::endl;
}
} handler(m_value_store, os, head);
traverse_tree(handler);
}
template<typename KeyT, typename ValueT, typename TraitsT>
template<typename _Handler>
void packed_trie_map<KeyT, ValueT, TraitsT>::traverse_tree(_Handler hdl) const
{
node_stack_type node_stack = get_root_stack();
stack_item* si = &node_stack.back();
size_type index_size = std::distance(si->child_pos, si->child_end);
size_type depth = 0;
hdl.node(si->node_pos, nullptr, depth, index_size);
if (si->child_pos == si->child_end)
// empty container
return;
const auto* node_pos = si->node_pos;
const auto* child_pos = si->child_pos;
const auto* child_end = si->child_end;
const auto* p = child_pos;
for (bool in_tree = true; in_tree;)
{
// Descend until the leaf node is reached by following the left-most child nodes.
while (true)
{
key_unit_type key = *p;
depth = node_stack.size();
++p;
size_type offset = *p;
// jump down to the child node.
hdl.move_down();
node_pos -= offset;
p = node_pos;
++p;
index_size = *p; // size of the buffer that stores child nodes' keys and offsets.
hdl.node(node_pos, &key, depth, index_size);
++p;
child_pos = p;
child_end = child_pos + index_size;
// Push this child node onto the stack.
node_stack.emplace_back(&m_value_store, node_pos, child_pos, child_end);
if (!index_size)
// no child nodes i.e. leaf node. Bail out of the loop.
break;
}
// Ascend up the tree until a node with an unvisited child node is
// found, then move sideways.
while (true)
{
// move up.
node_stack.pop_back();
si = &node_stack.back();
std::advance(si->child_pos, 2); // move to the next child node slot.
hdl.move_up(si->node_pos, si->child_pos, si->child_end);
if (si->child_pos != si->child_end)
{
// This is an unvisited child node. Bail out of the loop.
hdl.next_child();
node_pos = si->node_pos;
p = si->child_pos;
break;
}
if (node_stack.size() == 1)
{
// End of the tree has reached.
in_tree = false;
break;
}
}
}
hdl.end();
}
} // namespace mdds
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|