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
|
/**
* MIT License
*
* Copyright (c) 2017 Tessil
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef TSL_ARRAY_HASH_H
#define TSL_ARRAY_HASH_H
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <iterator>
#include <limits>
#include <memory>
#include <stdexcept>
#include <type_traits>
#include <utility>
#include <vector>
#include "array_growth_policy.h"
/*
* __has_include is a bit useless (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79433),
* check also __cplusplus version.
*/
#ifdef __has_include
# if __has_include(<string_view>) && __cplusplus >= 201703L
# define TSL_AH_HAS_STRING_VIEW
# endif
#endif
#ifdef TSL_AH_HAS_STRING_VIEW
# include <string_view>
#endif
#ifdef TSL_DEBUG
# define tsl_ah_assert(expr) assert(expr)
#else
# define tsl_ah_assert(expr) (static_cast<void>(0))
#endif
/**
* Implementation of the array hash structure described in the
* "Cache-conscious collision resolution in string hash tables." (Askitis Nikolas and Justin Zobel, 2005) paper.
*/
namespace tsl {
namespace ah {
template<class CharT>
struct str_hash {
#ifdef TSL_AH_HAS_STRING_VIEW
std::size_t operator()(const CharT* key, std::size_t key_size) const {
return std::hash<std::basic_string_view<CharT>>()(std::basic_string_view<CharT>(key, key_size));
}
#else
/**
* FNV-1a hash
*/
std::size_t operator()(const CharT* key, std::size_t key_size) const {
static const std::size_t init = std::size_t((sizeof(std::size_t) == 8)?0xcbf29ce484222325:0x811c9dc5);
static const std::size_t multiplier = std::size_t((sizeof(std::size_t) == 8)?0x100000001b3:0x1000193);
std::size_t hash = init;
for (std::size_t i = 0; i < key_size; ++i) {
hash ^= key[i];
hash *= multiplier;
}
return hash;
}
#endif
};
template<class CharT>
struct str_equal {
bool operator()(const CharT* key_lhs, std::size_t key_size_lhs,
const CharT* key_rhs, std::size_t key_size_rhs) const
{
if(key_size_lhs != key_size_rhs) {
return false;
}
else {
return std::memcmp(key_lhs, key_rhs, key_size_lhs * sizeof(CharT)) == 0;
}
}
};
}
namespace detail_array_hash {
template<typename T, typename = void>
struct is_iterator: std::false_type {
};
template<typename T>
struct is_iterator<T, typename std::enable_if<!std::is_same<typename std::iterator_traits<T>::iterator_category, void>::value>::type>: std::true_type {
};
static constexpr bool is_power_of_two(std::size_t value) {
return value != 0 && (value & (value - 1)) == 0;
}
template<typename T, typename U>
static T numeric_cast(U value, const char* error_message = "numeric_cast() failed.") {
T ret = static_cast<T>(value);
if(static_cast<U>(ret) != value) {
throw std::runtime_error(error_message);
}
const bool is_same_signedness = (std::is_unsigned<T>::value && std::is_unsigned<U>::value) ||
(std::is_signed<T>::value && std::is_signed<U>::value);
if(!is_same_signedness && (ret < T{}) != (value < U{})) {
throw std::runtime_error(error_message);
}
return ret;
}
/**
* Fixed size type used to represent size_type values on serialization. Need to be big enough
* to represent a std::size_t on 32 and 64 bits platforms, and must be the same size on both platforms.
*/
using slz_size_type = std::uint64_t;
template<class T, class Deserializer>
static T deserialize_value(Deserializer& deserializer) {
// MSVC < 2017 is not conformant, circumvent the problem by removing the template keyword
#if defined (_MSC_VER) && _MSC_VER < 1910
return deserializer.Deserializer::operator()<T>();
#else
return deserializer.Deserializer::template operator()<T>();
#endif
}
/**
* For each string in the bucket, store the size of the string, the chars of the string
* and T, if it's not void. T should be either void or an unsigned type.
*
* End the buffer with END_OF_BUCKET flag. END_OF_BUCKET has the same type as the string size variable.
*
* m_buffer (CharT*):
* | size of str1 (KeySizeT) | str1 (const CharT*) | value (T if T != void) | ... |
* | size of strN (KeySizeT) | strN (const CharT*) | value (T if T != void) | END_OF_BUCKET (KeySizeT) |
*
* m_buffer is null if there is no string in the bucket.
*
* KeySizeT and T are extended to be a multiple of CharT when stored in the buffer.
*
* Use std::malloc and std::free instead of new and delete so we can have access to std::realloc.
*/
template<class CharT,
class T,
class KeyEqual,
class KeySizeT,
bool StoreNullTerminator>
class array_bucket {
template<typename U>
using has_mapped_type = typename std::integral_constant<bool, !std::is_same<U, void>::value>;
static_assert(!has_mapped_type<T>::value || std::is_unsigned<T>::value,
"T should be either void or an unsigned type.");
static_assert(std::is_unsigned<KeySizeT>::value, "KeySizeT should be an unsigned type.");
public:
template<bool IsConst>
class array_bucket_iterator;
using char_type = CharT;
using key_size_type = KeySizeT;
using mapped_type = T;
using size_type = std::size_t;
using key_equal = KeyEqual;
using iterator = array_bucket_iterator<false>;
using const_iterator = array_bucket_iterator<true>;
static_assert(sizeof(KeySizeT) <= sizeof(size_type), "sizeof(KeySizeT) should be <= sizeof(std::size_t;)");
static_assert(std::is_unsigned<size_type>::value, "");
private:
/**
* Return how much space in bytes the type U will take when stored in the buffer.
* As the buffer is of type CharT, U may take more space than sizeof(U).
*
* Example: sizeof(CharT) = 4, sizeof(U) = 2 => U will take 4 bytes in the buffer instead of 2.
*/
template<typename U>
static constexpr size_type sizeof_in_buff() noexcept {
static_assert(is_power_of_two(sizeof(U)), "sizeof(U) should be a power of two.");
static_assert(is_power_of_two(sizeof(CharT)), "sizeof(CharT) should be a power of two.");
return std::max(sizeof(U), sizeof(CharT));
}
/**
* Same as sizeof_in_buff<U>, but instead of returning the size in bytes return it in term of sizeof(CharT).
*/
template<typename U>
static constexpr size_type size_as_char_t() noexcept {
return sizeof_in_buff<U>() / sizeof(CharT);
}
static key_size_type read_key_size(const CharT* buffer) noexcept {
key_size_type key_size;
std::memcpy(&key_size, buffer, sizeof(key_size));
return key_size;
}
static mapped_type read_value(const CharT* buffer) noexcept {
mapped_type value;
std::memcpy(&value, buffer, sizeof(value));
return value;
}
static bool is_end_of_bucket(const CharT* buffer) noexcept {
return read_key_size(buffer) == END_OF_BUCKET;
}
public:
/**
* Return the size required for an entry with a key of size 'key_size'.
*/
template<class U = T, typename std::enable_if<!has_mapped_type<U>::value>::type* = nullptr>
static size_type entry_required_bytes(size_type key_size) noexcept {
return sizeof_in_buff<key_size_type>() + (key_size + KEY_EXTRA_SIZE) * sizeof(CharT);
}
template<class U = T, typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr>
static size_type entry_required_bytes(size_type key_size) noexcept {
return sizeof_in_buff<key_size_type>() + (key_size + KEY_EXTRA_SIZE) * sizeof(CharT) +
sizeof_in_buff<mapped_type>();
}
private:
/**
* Return the size of the current entry in buffer.
*/
static size_type entry_size_bytes(const CharT* buffer) noexcept {
return entry_required_bytes(read_key_size(buffer));
}
public:
template<bool IsConst>
class array_bucket_iterator {
friend class array_bucket;
using buffer_type = typename std::conditional<IsConst, const CharT, CharT>::type;
explicit array_bucket_iterator(buffer_type* position) noexcept: m_position(position) {
}
public:
using iterator_category = std::forward_iterator_tag;
using value_type = void;
using difference_type = std::ptrdiff_t;
using reference = void;
using pointer = void;
public:
array_bucket_iterator() noexcept: m_position(nullptr) {
}
const CharT* key() const {
return m_position + size_as_char_t<key_size_type>();
}
size_type key_size() const {
return read_key_size(m_position);
}
template<class U = T, typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr>
U value() const {
return read_value(m_position + size_as_char_t<key_size_type>() + key_size() + KEY_EXTRA_SIZE);
}
template<class U = T, typename std::enable_if<has_mapped_type<U>::value && !IsConst && std::is_same<U, T>::value>::type* = nullptr>
void set_value(U value) noexcept {
std::memcpy(m_position + size_as_char_t<key_size_type>() + key_size() + KEY_EXTRA_SIZE,
&value, sizeof(value));
}
array_bucket_iterator& operator++() {
m_position += entry_size_bytes(m_position)/sizeof(CharT);
if(is_end_of_bucket(m_position)) {
m_position = nullptr;
}
return *this;
}
array_bucket_iterator operator++(int) {
array_bucket_iterator tmp(*this);
++*this;
return tmp;
}
friend bool operator==(const array_bucket_iterator& lhs, const array_bucket_iterator& rhs) {
return lhs.m_position == rhs.m_position;
}
friend bool operator!=(const array_bucket_iterator& lhs, const array_bucket_iterator& rhs) {
return !(lhs == rhs);
}
private:
buffer_type* m_position;
};
static iterator end_it() noexcept {
return iterator(nullptr);
}
static const_iterator cend_it() noexcept {
return const_iterator(nullptr);
}
public:
array_bucket(): m_buffer(nullptr) {
}
/**
* Reserve 'size' in the buffer of the bucket. The created bucket is empty.
*/
array_bucket(std::size_t size): m_buffer(nullptr) {
if(size == 0) {
return;
}
m_buffer = static_cast<CharT*>(std::malloc(size*sizeof(CharT) + sizeof_in_buff<decltype(END_OF_BUCKET)>()));
if(m_buffer == nullptr) {
throw std::bad_alloc();
}
const auto end_of_bucket = END_OF_BUCKET;
std::memcpy(m_buffer, &end_of_bucket, sizeof(end_of_bucket));
}
~array_bucket() {
clear();
}
array_bucket(const array_bucket& other) {
if(other.m_buffer == nullptr) {
m_buffer = nullptr;
return;
}
const size_type other_buffer_size = other.size();
m_buffer = static_cast<CharT*>(std::malloc(other_buffer_size*sizeof(CharT) + sizeof_in_buff<decltype(END_OF_BUCKET)>()));
if(m_buffer == nullptr) {
throw std::bad_alloc();
}
std::memcpy(m_buffer, other.m_buffer, other_buffer_size*sizeof(CharT));
const auto end_of_bucket = END_OF_BUCKET;
std::memcpy(m_buffer + other_buffer_size, &end_of_bucket, sizeof(end_of_bucket));
}
array_bucket(array_bucket&& other) noexcept: m_buffer(other.m_buffer) {
other.m_buffer = nullptr;
}
array_bucket& operator=(array_bucket other) noexcept {
other.swap(*this);
return *this;
}
void swap(array_bucket& other) noexcept {
std::swap(m_buffer, other.m_buffer);
}
iterator begin() noexcept { return iterator(m_buffer); }
iterator end() noexcept { return iterator(nullptr); }
const_iterator begin() const noexcept { return cbegin(); }
const_iterator end() const noexcept { return cend(); }
const_iterator cbegin() const noexcept { return const_iterator(m_buffer); }
const_iterator cend() const noexcept { return const_iterator(nullptr); }
/**
* Return an iterator pointing to the key entry if presents or, if not there, to the position
* past the last element of the bucket. Return end() if the bucket has not be initialized yet.
*
* The boolean of the pair is set to true if the key is there, false otherwise.
*/
std::pair<const_iterator, bool> find_or_end_of_bucket(const CharT* key, size_type key_size) const noexcept {
if(m_buffer == nullptr) {
return std::make_pair(cend(), false);
}
const CharT* buffer_ptr_in_out = m_buffer;
const bool found = find_or_end_of_bucket_impl(key, key_size, buffer_ptr_in_out);
return std::make_pair(const_iterator(buffer_ptr_in_out), found);
}
/**
* Append the element 'key' with its potential value at the end of the bucket.
* 'end_of_bucket' should point past the end of the last element in the bucket, end() if the bucket
* was not intiailized yet. You usually get this value from find_or_end_of_bucket.
*
* Return the position where the element was actually inserted.
*/
template<class... ValueArgs>
const_iterator append(const_iterator end_of_bucket, const CharT* key, size_type key_size,
ValueArgs&&... value)
{
const key_size_type key_sz = as_key_size_type(key_size);
if(end_of_bucket == cend()) {
tsl_ah_assert(m_buffer == nullptr);
const size_type buffer_size = entry_required_bytes(key_sz) + sizeof_in_buff<decltype(END_OF_BUCKET)>();
m_buffer = static_cast<CharT*>(std::malloc(buffer_size));
if(m_buffer == nullptr) {
throw std::bad_alloc();
}
append_impl(key, key_sz, m_buffer, std::forward<ValueArgs>(value)...);
return const_iterator(m_buffer);
}
else {
tsl_ah_assert(is_end_of_bucket(end_of_bucket.m_position));
const size_type current_size = ((end_of_bucket.m_position + size_as_char_t<decltype(END_OF_BUCKET)>()) -
m_buffer) * sizeof(CharT);
const size_type new_size = current_size + entry_required_bytes(key_sz);
CharT* new_buffer = static_cast<CharT*>(std::realloc(m_buffer, new_size));
if(new_buffer == nullptr) {
throw std::bad_alloc();
}
m_buffer = new_buffer;
CharT* buffer_append_pos = m_buffer + current_size / sizeof(CharT) -
size_as_char_t<decltype(END_OF_BUCKET)>();
append_impl(key, key_sz, buffer_append_pos, std::forward<ValueArgs>(value)...);
return const_iterator(buffer_append_pos);
}
}
const_iterator erase(const_iterator position) noexcept {
tsl_ah_assert(position.m_position != nullptr && !is_end_of_bucket(position.m_position));
// get mutable pointers
CharT* start_entry = m_buffer + (position.m_position - m_buffer);
CharT* start_next_entry = start_entry + entry_size_bytes(start_entry) / sizeof(CharT);
CharT* end_buffer_ptr = start_next_entry;
while(!is_end_of_bucket(end_buffer_ptr)) {
end_buffer_ptr += entry_size_bytes(end_buffer_ptr) / sizeof(CharT);
}
end_buffer_ptr += size_as_char_t<decltype(END_OF_BUCKET)>();
const size_type size_to_move = (end_buffer_ptr - start_next_entry) * sizeof(CharT);
std::memmove(start_entry, start_next_entry, size_to_move);
if(is_end_of_bucket(m_buffer)) {
clear();
return cend();
}
else if(is_end_of_bucket(start_entry)) {
return cend();
}
else {
return const_iterator(start_entry);
}
}
/**
* Return true if an element has been erased
*/
bool erase(const CharT* key, size_type key_size) noexcept {
if(m_buffer == nullptr) {
return false;
}
const CharT* entry_buffer_ptr_in_out = m_buffer;
bool found = find_or_end_of_bucket_impl(key, key_size, entry_buffer_ptr_in_out);
if(found) {
erase(const_iterator(entry_buffer_ptr_in_out));
return true;
}
else {
return false;
}
}
/**
* Bucket should be big enough and there is no check to see if the key already exists.
* No check on key_size.
*/
template<class... ValueArgs>
void append_in_reserved_bucket_no_check(const CharT* key, size_type key_size, ValueArgs&&... value) noexcept {
CharT* buffer_ptr = m_buffer;
while(!is_end_of_bucket(buffer_ptr)) {
buffer_ptr += entry_size_bytes(buffer_ptr)/sizeof(CharT);
}
append_impl(key, key_size_type(key_size), buffer_ptr, std::forward<ValueArgs>(value)...);
}
bool empty() const noexcept {
return m_buffer == nullptr || is_end_of_bucket(m_buffer);
}
void clear() noexcept {
std::free(m_buffer);
m_buffer = nullptr;
}
iterator mutable_iterator(const_iterator pos) noexcept {
return iterator(m_buffer + (pos.m_position - m_buffer));
}
template<class Serializer>
void serialize(Serializer& serializer) const {
const slz_size_type bucket_size = size();
tsl_ah_assert(m_buffer != nullptr || bucket_size == 0);
serializer(bucket_size);
serializer(m_buffer, bucket_size);
}
template<class Deserializer>
static array_bucket deserialize(Deserializer& deserializer) {
array_bucket bucket;
const slz_size_type bucket_size_ds = deserialize_value<slz_size_type>(deserializer);
if(bucket_size_ds == 0) {
return bucket;
}
const std::size_t bucket_size = numeric_cast<std::size_t>(bucket_size_ds, "Deserialized bucket_size is too big.");
bucket.m_buffer = static_cast<CharT*>(std::malloc(bucket_size*sizeof(CharT) + sizeof_in_buff<decltype(END_OF_BUCKET)>()));
if(bucket.m_buffer == nullptr) {
throw std::bad_alloc();
}
deserializer(bucket.m_buffer, bucket_size);
const auto end_of_bucket = END_OF_BUCKET;
std::memcpy(bucket.m_buffer + bucket_size, &end_of_bucket, sizeof(end_of_bucket));
tsl_ah_assert(bucket.size() == bucket_size);
return bucket;
}
private:
key_size_type as_key_size_type(size_type key_size) const {
if(key_size > MAX_KEY_SIZE) {
throw std::length_error("Key is too long.");
}
return key_size_type(key_size);
}
/*
* Return true if found, false otherwise.
* If true, buffer_ptr_in_out points to the start of the entry matching 'key'.
* If false, buffer_ptr_in_out points to where the 'key' should be inserted.
*
* Start search from buffer_ptr_in_out.
*/
bool find_or_end_of_bucket_impl(const CharT* key, size_type key_size,
const CharT* & buffer_ptr_in_out) const noexcept
{
while(!is_end_of_bucket(buffer_ptr_in_out)) {
const key_size_type buffer_key_size = read_key_size(buffer_ptr_in_out);
const CharT* buffer_str = buffer_ptr_in_out + size_as_char_t<key_size_type>();
if(KeyEqual()(buffer_str, buffer_key_size, key, key_size)) {
return true;
}
buffer_ptr_in_out += entry_size_bytes(buffer_ptr_in_out)/sizeof(CharT);
}
return false;
}
template<typename U = T, typename std::enable_if<!has_mapped_type<U>::value>::type* = nullptr>
void append_impl(const CharT* key, key_size_type key_size, CharT* buffer_append_pos) noexcept {
std::memcpy(buffer_append_pos, &key_size, sizeof(key_size));
buffer_append_pos += size_as_char_t<key_size_type>();
std::memcpy(buffer_append_pos, key, key_size * sizeof(CharT));
buffer_append_pos += key_size;
const CharT zero = 0;
std::memcpy(buffer_append_pos, &zero, KEY_EXTRA_SIZE * sizeof(CharT));
buffer_append_pos += KEY_EXTRA_SIZE;
const auto end_of_bucket = END_OF_BUCKET;
std::memcpy(buffer_append_pos, &end_of_bucket, sizeof(end_of_bucket));
}
template<typename U = T, typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr>
void append_impl(const CharT* key, key_size_type key_size, CharT* buffer_append_pos,
typename array_bucket<CharT, U, KeyEqual, KeySizeT, StoreNullTerminator>::mapped_type value) noexcept
{
std::memcpy(buffer_append_pos, &key_size, sizeof(key_size));
buffer_append_pos += size_as_char_t<key_size_type>();
std::memcpy(buffer_append_pos, key, key_size * sizeof(CharT));
buffer_append_pos += key_size;
const CharT zero = 0;
std::memcpy(buffer_append_pos, &zero, KEY_EXTRA_SIZE * sizeof(CharT));
buffer_append_pos += KEY_EXTRA_SIZE;
std::memcpy(buffer_append_pos, &value, sizeof(value));
buffer_append_pos += size_as_char_t<mapped_type>();
const auto end_of_bucket = END_OF_BUCKET;
std::memcpy(buffer_append_pos, &end_of_bucket, sizeof(end_of_bucket));
}
/**
* Return the number of CharT in m_buffer. As the size of the buffer is not stored to gain some space,
* the method need to find the EOF marker and is thus in O(n).
*/
size_type size() const noexcept {
if(m_buffer == nullptr) {
return 0;
}
CharT* buffer_ptr = m_buffer;
while(!is_end_of_bucket(buffer_ptr)) {
buffer_ptr += entry_size_bytes(buffer_ptr)/sizeof(CharT);
}
return buffer_ptr - m_buffer;
}
private:
static const key_size_type END_OF_BUCKET = std::numeric_limits<key_size_type>::max();
static const key_size_type KEY_EXTRA_SIZE = StoreNullTerminator?1:0;
CharT* m_buffer;
public:
static const key_size_type MAX_KEY_SIZE =
// -1 for END_OF_BUCKET
key_size_type(std::numeric_limits<key_size_type>::max() - KEY_EXTRA_SIZE - 1);
};
template<class T>
class value_container {
public:
void clear() noexcept {
m_values.clear();
}
void reserve(std::size_t new_cap) {
m_values.reserve(new_cap);
}
void shrink_to_fit() {
m_values.shrink_to_fit();
}
friend void swap(value_container& lhs, value_container& rhs) {
lhs.m_values.swap(rhs.m_values);
}
protected:
static constexpr float VECTOR_GROWTH_RATE = 1.5f;
// TODO use a sparse array? or a std::dequeu
std::vector<T> m_values;
};
template<>
class value_container<void> {
public:
void clear() noexcept {
}
void shrink_to_fit() {
}
void reserve(std::size_t /*new_cap*/) {
}
};
/**
* If there is no value in the array_hash (in the case of a set for example), T should be void.
*
* The size of a key string is limited to std::numeric_limits<KeySizeT>::max() - 1.
*
* The number of elements in the map is limited to std::numeric_limits<IndexSizeT>::max().
*/
template<class CharT,
class T,
class Hash,
class KeyEqual,
bool StoreNullTerminator,
class KeySizeT,
class IndexSizeT,
class GrowthPolicy>
class array_hash: private value_container<T>, private Hash, private GrowthPolicy {
private:
template<typename U>
using has_mapped_type = typename std::integral_constant<bool, !std::is_same<U, void>::value>;
/**
* If there is a mapped type in array_hash, we store the values in m_values of value_container class
* and we store an index to m_values in the bucket. The index is of type IndexSizeT.
*/
using array_bucket = tsl::detail_array_hash::array_bucket<CharT,
typename std::conditional<has_mapped_type<T>::value,
IndexSizeT,
void>::type,
KeyEqual, KeySizeT, StoreNullTerminator>;
public:
template<bool IsConst>
class array_hash_iterator;
using char_type = CharT;
using key_size_type = KeySizeT;
using index_size_type = IndexSizeT;
using size_type = std::size_t;
using hasher = Hash;
using key_equal = KeyEqual;
using iterator = array_hash_iterator<false>;
using const_iterator = array_hash_iterator<true>;
/*
* Iterator classes
*/
public:
template<bool IsConst>
class array_hash_iterator {
friend class array_hash;
private:
using iterator_array_bucket = typename array_bucket::const_iterator;
using iterator_buckets = typename std::conditional<IsConst,
typename std::vector<array_bucket>::const_iterator,
typename std::vector<array_bucket>::iterator>::type;
using array_hash_ptr = typename std::conditional<IsConst,
const array_hash*,
array_hash*>::type;
public:
using iterator_category = std::forward_iterator_tag;
using value_type = typename std::conditional<has_mapped_type<T>::value, T, void>::type;
using difference_type = std::ptrdiff_t;
using reference = typename std::conditional<has_mapped_type<T>::value,
typename std::conditional<
IsConst,
typename std::add_lvalue_reference<const T>::type,
typename std::add_lvalue_reference<T>::type>::type,
void>::type;
using pointer = typename std::conditional<has_mapped_type<T>::value,
typename std::conditional<IsConst, const T*, T*>::type,
void>::type;
private:
array_hash_iterator(iterator_buckets buckets_iterator, iterator_array_bucket array_bucket_iterator,
array_hash_ptr array_hash_p) noexcept:
m_buckets_iterator(buckets_iterator),
m_array_bucket_iterator(array_bucket_iterator),
m_array_hash(array_hash_p)
{
tsl_ah_assert(m_array_hash != nullptr);
}
public:
array_hash_iterator() noexcept: m_array_hash(nullptr) {
}
template<bool TIsConst = IsConst, typename std::enable_if<TIsConst>::type* = nullptr>
array_hash_iterator(const array_hash_iterator<!TIsConst>& other) noexcept :
m_buckets_iterator(other.m_buckets_iterator),
m_array_bucket_iterator(other.m_array_bucket_iterator),
m_array_hash(other.m_array_hash)
{
}
array_hash_iterator(const array_hash_iterator& other) = default;
array_hash_iterator(array_hash_iterator&& other) = default;
array_hash_iterator& operator=(const array_hash_iterator& other) = default;
array_hash_iterator& operator=(array_hash_iterator&& other) = default;
const CharT* key() const {
return m_array_bucket_iterator.key();
}
size_type key_size() const {
return m_array_bucket_iterator.key_size();
}
#ifdef TSL_AH_HAS_STRING_VIEW
std::basic_string_view<CharT> key_sv() const {
return std::basic_string_view<CharT>(key(), key_size());
}
#endif
template<class U = T, typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr>
reference value() const {
return this->m_array_hash->m_values[value_position()];
}
template<class U = T, typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr>
reference operator*() const {
return value();
}
template<class U = T, typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr>
pointer operator->() const {
return std::addressof(value());
}
array_hash_iterator& operator++() {
tsl_ah_assert(m_buckets_iterator != m_array_hash->m_buckets_data.end());
tsl_ah_assert(m_array_bucket_iterator != m_buckets_iterator->cend());
++m_array_bucket_iterator;
if(m_array_bucket_iterator == m_buckets_iterator->cend()) {
do {
++m_buckets_iterator;
} while(m_buckets_iterator != m_array_hash->m_buckets_data.end() &&
m_buckets_iterator->empty());
if(m_buckets_iterator != m_array_hash->m_buckets_data.end()) {
m_array_bucket_iterator = m_buckets_iterator->cbegin();
}
}
return *this;
}
array_hash_iterator operator++(int) {
array_hash_iterator tmp(*this);
++*this;
return tmp;
}
friend bool operator==(const array_hash_iterator& lhs, const array_hash_iterator& rhs) {
return lhs.m_buckets_iterator == rhs.m_buckets_iterator &&
lhs.m_array_bucket_iterator == rhs.m_array_bucket_iterator &&
lhs.m_array_hash == rhs.m_array_hash;
}
friend bool operator!=(const array_hash_iterator& lhs, const array_hash_iterator& rhs) {
return !(lhs == rhs);
}
private:
template<class U = T, typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr>
IndexSizeT value_position() const {
return this->m_array_bucket_iterator.value();
}
private:
iterator_buckets m_buckets_iterator;
iterator_array_bucket m_array_bucket_iterator;
array_hash_ptr m_array_hash;
};
public:
array_hash(size_type bucket_count,
const Hash& hash,
float max_load_factor): value_container<T>(),
Hash(hash),
GrowthPolicy(bucket_count),
m_buckets_data(bucket_count > max_bucket_count()?
throw std::length_error("The map exceeds its maxmimum bucket count."):
bucket_count),
m_buckets(m_buckets_data.empty()?static_empty_bucket_ptr():m_buckets_data.data()),
m_nb_elements(0)
{
this->max_load_factor(max_load_factor);
}
array_hash(const array_hash& other): value_container<T>(other),
Hash(other),
GrowthPolicy(other),
m_buckets_data(other.m_buckets_data),
m_buckets(m_buckets_data.empty()?static_empty_bucket_ptr():m_buckets_data.data()),
m_nb_elements(other.m_nb_elements),
m_max_load_factor(other.m_max_load_factor),
m_load_threshold(other.m_load_threshold)
{
}
array_hash(array_hash&& other) noexcept(std::is_nothrow_move_constructible<value_container<T>>::value &&
std::is_nothrow_move_constructible<Hash>::value &&
std::is_nothrow_move_constructible<GrowthPolicy>::value &&
std::is_nothrow_move_constructible<std::vector<array_bucket>>::value)
: value_container<T>(std::move(other)),
Hash(std::move(other)),
GrowthPolicy(std::move(other)),
m_buckets_data(std::move(other.m_buckets_data)),
m_buckets(m_buckets_data.empty()?static_empty_bucket_ptr():m_buckets_data.data()),
m_nb_elements(other.m_nb_elements),
m_max_load_factor(other.m_max_load_factor),
m_load_threshold(other.m_load_threshold)
{
other.value_container<T>::clear();
other.GrowthPolicy::clear();
other.m_buckets_data.clear();
other.m_buckets = static_empty_bucket_ptr();
other.m_nb_elements = 0;
other.m_load_threshold = 0;
}
array_hash& operator=(const array_hash& other) {
if(&other != this) {
value_container<T>::operator=(other);
Hash::operator=(other);
GrowthPolicy::operator=(other);
m_buckets_data = other.m_buckets_data;
m_buckets = m_buckets_data.empty()?static_empty_bucket_ptr():
m_buckets_data.data();
m_nb_elements = other.m_nb_elements;
m_max_load_factor = other.m_max_load_factor;
m_load_threshold = other.m_load_threshold;
}
return *this;
}
array_hash& operator=(array_hash&& other) {
other.swap(*this);
other.clear();
return *this;
}
/*
* Iterators
*/
iterator begin() noexcept {
auto begin = m_buckets_data.begin();
while(begin != m_buckets_data.end() && begin->empty()) {
++begin;
}
return (begin != m_buckets_data.end())?iterator(begin, begin->cbegin(), this):end();
}
const_iterator begin() const noexcept {
return cbegin();
}
const_iterator cbegin() const noexcept {
auto begin = m_buckets_data.cbegin();
while(begin != m_buckets_data.cend() && begin->empty()) {
++begin;
}
return (begin != m_buckets_data.cend())?const_iterator(begin, begin->cbegin(), this):cend();
}
iterator end() noexcept {
return iterator(m_buckets_data.end(), array_bucket::cend_it(), this);
}
const_iterator end() const noexcept {
return cend();
}
const_iterator cend() const noexcept {
return const_iterator(m_buckets_data.end(), array_bucket::cend_it(), this);
}
/*
* Capacity
*/
bool empty() const noexcept {
return m_nb_elements == 0;
}
size_type size() const noexcept {
return m_nb_elements;
}
size_type max_size() const noexcept {
return std::numeric_limits<IndexSizeT>::max();
}
size_type max_key_size() const noexcept {
return MAX_KEY_SIZE;
}
void shrink_to_fit() {
clear_old_erased_values();
value_container<T>::shrink_to_fit();
rehash_impl(size_type(std::ceil(float(size())/max_load_factor())));
}
/*
* Modifiers
*/
void clear() noexcept {
value_container<T>::clear();
for(auto& bucket: m_buckets_data) {
bucket.clear();
}
m_nb_elements = 0;
}
template<class... ValueArgs>
std::pair<iterator, bool> emplace(const CharT* key, size_type key_size, ValueArgs&&... value_args) {
const std::size_t hash = hash_key(key, key_size);
std::size_t ibucket = bucket_for_hash(hash);
auto it_find = m_buckets[ibucket].find_or_end_of_bucket(key, key_size);
if(it_find.second) {
return std::make_pair(iterator(m_buckets_data.begin() + ibucket, it_find.first, this), false);
}
if(grow_on_high_load()) {
ibucket = bucket_for_hash(hash);
it_find = m_buckets[ibucket].find_or_end_of_bucket(key, key_size);
}
return emplace_impl(ibucket, it_find.first, key, key_size, std::forward<ValueArgs>(value_args)...);
}
template<class M>
std::pair<iterator, bool> insert_or_assign(const CharT* key, size_type key_size, M&& obj) {
auto it = emplace(key, key_size, std::forward<M>(obj));
if(!it.second) {
it.first.value() = std::forward<M>(obj);
}
return it;
}
iterator erase(const_iterator pos) {
if(shoud_clear_old_erased_values()) {
clear_old_erased_values();
}
return erase_from_bucket(mutable_iterator(pos));
}
iterator erase(const_iterator first, const_iterator last) {
if(first == last) {
return mutable_iterator(first);
}
/**
* When erasing an element from a bucket with erase_from_bucket, it invalidates all the iterators
* in the array bucket of the element (m_array_bucket_iterator) but not the iterators of the buckets
* itself (m_buckets_iterator).
*
* So first erase all the values between first and last which are not part of the bucket of last,
* and then erase carefully the values in last's bucket.
*/
auto to_delete = mutable_iterator(first);
while(to_delete.m_buckets_iterator != last.m_buckets_iterator) {
to_delete = erase_from_bucket(to_delete);
}
std::size_t nb_elements_until_last = std::distance(to_delete.m_array_bucket_iterator,
last.m_array_bucket_iterator);
while(nb_elements_until_last > 0) {
to_delete = erase_from_bucket(to_delete);
nb_elements_until_last--;
}
if(shoud_clear_old_erased_values()) {
clear_old_erased_values();
}
return to_delete;
}
size_type erase(const CharT* key, size_type key_size) {
return erase(key, key_size, hash_key(key, key_size));
}
size_type erase(const CharT* key, size_type key_size, std::size_t hash) {
if(shoud_clear_old_erased_values()) {
clear_old_erased_values();
}
const std::size_t ibucket = bucket_for_hash(hash);
if(m_buckets[ibucket].erase(key, key_size)) {
m_nb_elements--;
return 1;
}
else {
return 0;
}
}
void swap(array_hash& other) {
using std::swap;
swap(static_cast<value_container<T>&>(*this), static_cast<value_container<T>&>(other));
swap(static_cast<Hash&>(*this), static_cast<Hash&>(other));
swap(static_cast<GrowthPolicy&>(*this), static_cast<GrowthPolicy&>(other));
swap(m_buckets_data, other.m_buckets_data);
swap(m_buckets, other.m_buckets);
swap(m_nb_elements, other.m_nb_elements);
swap(m_max_load_factor, other.m_max_load_factor);
swap(m_load_threshold, other.m_load_threshold);
}
/*
* Lookup
*/
template<class U = T, typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr>
U& at(const CharT* key, size_type key_size) {
return at(key, key_size, hash_key(key, key_size));
}
template<class U = T, typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr>
const U& at(const CharT* key, size_type key_size) const {
return at(key, key_size, hash_key(key, key_size));
}
template<class U = T, typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr>
U& at(const CharT* key, size_type key_size, std::size_t hash) {
return const_cast<U&>(static_cast<const array_hash*>(this)->at(key, key_size, hash));
}
template<class U = T, typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr>
const U& at(const CharT* key, size_type key_size, std::size_t hash) const {
const std::size_t ibucket = bucket_for_hash(hash);
auto it_find = m_buckets[ibucket].find_or_end_of_bucket(key, key_size);
if(it_find.second) {
return this->m_values[it_find.first.value()];
}
else {
throw std::out_of_range("Couldn't find key.");
}
}
template<class U = T, typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr>
U& access_operator(const CharT* key, size_type key_size) {
const std::size_t hash = hash_key(key, key_size);
std::size_t ibucket = bucket_for_hash(hash);
auto it_find = m_buckets[ibucket].find_or_end_of_bucket(key, key_size);
if(it_find.second) {
return this->m_values[it_find.first.value()];
}
else {
if(grow_on_high_load()) {
ibucket = bucket_for_hash(hash);
it_find = m_buckets[ibucket].find_or_end_of_bucket(key, key_size);
}
return emplace_impl(ibucket, it_find.first, key, key_size, U{}).first.value();
}
}
size_type count(const CharT* key, size_type key_size) const {
return count(key, key_size, hash_key(key, key_size));
}
size_type count(const CharT* key, size_type key_size, std::size_t hash) const {
const std::size_t ibucket = bucket_for_hash(hash);
auto it_find = m_buckets[ibucket].find_or_end_of_bucket(key, key_size);
if(it_find.second) {
return 1;
}
else {
return 0;
}
}
iterator find(const CharT* key, size_type key_size) {
return find(key, key_size, hash_key(key, key_size));
}
const_iterator find(const CharT* key, size_type key_size) const {
return find(key, key_size, hash_key(key, key_size));
}
iterator find(const CharT* key, size_type key_size, std::size_t hash) {
const std::size_t ibucket = bucket_for_hash(hash);
auto it_find = m_buckets[ibucket].find_or_end_of_bucket(key, key_size);
if(it_find.second) {
return iterator(m_buckets_data.begin() + ibucket, it_find.first, this);
}
else {
return end();
}
}
const_iterator find(const CharT* key, size_type key_size, std::size_t hash) const {
const std::size_t ibucket = bucket_for_hash(hash);
auto it_find = m_buckets[ibucket].find_or_end_of_bucket(key, key_size);
if(it_find.second) {
return const_iterator(m_buckets_data.cbegin() + ibucket, it_find.first, this);
}
else {
return cend();
}
}
std::pair<iterator, iterator> equal_range(const CharT* key, size_type key_size) {
return equal_range(key, key_size, hash_key(key, key_size));
}
std::pair<const_iterator, const_iterator> equal_range(const CharT* key, size_type key_size) const {
return equal_range(key, key_size, hash_key(key, key_size));
}
std::pair<iterator, iterator> equal_range(const CharT* key, size_type key_size, std::size_t hash) {
iterator it = find(key, key_size, hash);
return std::make_pair(it, (it == end())?it:std::next(it));
}
std::pair<const_iterator, const_iterator> equal_range(const CharT* key, size_type key_size,
std::size_t hash) const
{
const_iterator it = find(key, key_size, hash);
return std::make_pair(it, (it == cend())?it:std::next(it));
}
/*
* Bucket interface
*/
size_type bucket_count() const {
return m_buckets_data.size();
}
size_type max_bucket_count() const {
return std::min(GrowthPolicy::max_bucket_count(), m_buckets_data.max_size());
}
/*
* Hash policy
*/
float load_factor() const {
if(bucket_count() == 0) {
return 0;
}
return float(m_nb_elements) / float(bucket_count());
}
float max_load_factor() const {
return m_max_load_factor;
}
void max_load_factor(float ml) {
m_max_load_factor = std::max(0.1f, ml);
m_load_threshold = size_type(float(bucket_count())*m_max_load_factor);
}
void rehash(size_type count) {
count = std::max(count, size_type(std::ceil(float(size())/max_load_factor())));
rehash_impl(count);
}
void reserve(size_type count) {
rehash(size_type(std::ceil(float(count)/max_load_factor())));
}
/*
* Observers
*/
hasher hash_function() const {
return static_cast<const hasher&>(*this);
}
// TODO add support for statefull KeyEqual
key_equal key_eq() const {
return KeyEqual();
}
/*
* Other
*/
iterator mutable_iterator(const_iterator it) noexcept {
auto it_bucket = m_buckets_data.begin() + std::distance(m_buckets_data.cbegin(), it.m_buckets_iterator);
return iterator(it_bucket, it.m_array_bucket_iterator, this);
}
template<class Serializer>
void serialize(Serializer& serializer) const {
serialize_impl(serializer);
}
template<class Deserializer>
void deserialize(Deserializer& deserializer, bool hash_compatible) {
deserialize_impl(deserializer, hash_compatible);
}
private:
std::size_t hash_key(const CharT* key, size_type key_size) const {
return Hash::operator()(key, key_size);
}
std::size_t bucket_for_hash(std::size_t hash) const {
return GrowthPolicy::bucket_for_hash(hash);
}
/**
* If there is a mapped_type, the mapped value in m_values is not erased now.
* It will be erased when the ratio between the size of the map and
* the size of the map + the number of deleted values still stored is low enough (see clear_old_erased_values).
*/
iterator erase_from_bucket(iterator pos) noexcept {
auto array_bucket_next_it = pos.m_buckets_iterator->erase(pos.m_array_bucket_iterator);
m_nb_elements--;
if(array_bucket_next_it != pos.m_buckets_iterator->cend()) {
return iterator(pos.m_buckets_iterator, array_bucket_next_it, this);
}
else {
do {
++pos.m_buckets_iterator;
} while(pos.m_buckets_iterator != m_buckets_data.end() && pos.m_buckets_iterator->empty());
if(pos.m_buckets_iterator != m_buckets_data.end()) {
return iterator(pos.m_buckets_iterator, pos.m_buckets_iterator->cbegin(), this);
}
else {
return end();
}
}
}
template<class U = T, typename std::enable_if<!has_mapped_type<U>::value>::type* = nullptr>
bool shoud_clear_old_erased_values(float /*threshold*/ = DEFAULT_CLEAR_OLD_ERASED_VALUE_THRESHOLD) const {
return false;
}
template<class U = T, typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr>
bool shoud_clear_old_erased_values(float threshold = DEFAULT_CLEAR_OLD_ERASED_VALUE_THRESHOLD) const {
if(this->m_values.size() == 0) {
return false;
}
return float(m_nb_elements)/float(this->m_values.size()) < threshold;
}
template<class U = T, typename std::enable_if<!has_mapped_type<U>::value>::type* = nullptr>
void clear_old_erased_values() {
}
template<class U = T, typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr>
void clear_old_erased_values() {
static_assert(std::is_nothrow_move_constructible<U>::value ||
std::is_copy_constructible<U>::value,
"mapped_value must be either copy constructible or nothrow move constructible.");
if(m_nb_elements == this->m_values.size()) {
return;
}
std::vector<T> new_values;
new_values.reserve(size());
for(auto it = begin(); it != end(); ++it) {
new_values.push_back(std::move_if_noexcept(it.value()));
}
IndexSizeT ivalue = 0;
for(auto it = begin(); it != end(); ++it) {
auto it_array_bucket = it.m_buckets_iterator->mutable_iterator(it.m_array_bucket_iterator);
it_array_bucket.set_value(ivalue);
ivalue++;
}
new_values.swap(this->m_values);
tsl_ah_assert(m_nb_elements == this->m_values.size());
}
/**
* Return true if a rehash occured.
*/
bool grow_on_high_load() {
if(size() >= m_load_threshold) {
rehash_impl(GrowthPolicy::next_bucket_count());
return true;
}
return false;
}
template<class... ValueArgs, class U = T, typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr>
std::pair<iterator, bool> emplace_impl(std::size_t ibucket, typename array_bucket::const_iterator end_of_bucket,
const CharT* key, size_type key_size, ValueArgs&&... value_args)
{
if(this->m_values.size() >= max_size()) {
// Try to clear old erased values lingering in m_values. Throw if it doesn't change anything.
clear_old_erased_values();
if(this->m_values.size() >= max_size()) {
throw std::length_error("Can't insert value, too much values in the map.");
}
}
if(this->m_values.size() == this->m_values.capacity()) {
this->m_values.reserve(std::size_t(float(this->m_values.size()) * value_container<T>::VECTOR_GROWTH_RATE));
}
this->m_values.emplace_back(std::forward<ValueArgs>(value_args)...);
try {
auto it = m_buckets[ibucket].append(end_of_bucket, key, key_size, IndexSizeT(this->m_values.size() - 1));
m_nb_elements++;
return std::make_pair(iterator(m_buckets_data.begin() + ibucket, it, this), true);
} catch(...) {
// Rollback
this->m_values.pop_back();
throw;
}
}
template<class U = T, typename std::enable_if<!has_mapped_type<U>::value>::type* = nullptr>
std::pair<iterator, bool> emplace_impl(std::size_t ibucket, typename array_bucket::const_iterator end_of_bucket,
const CharT* key, size_type key_size)
{
if(m_nb_elements >= max_size()) {
throw std::length_error("Can't insert value, too much values in the map.");
}
auto it = m_buckets[ibucket].append(end_of_bucket, key, key_size);
m_nb_elements++;
return std::make_pair(iterator(m_buckets_data.begin() + ibucket, it, this), true);
}
void rehash_impl(size_type bucket_count) {
GrowthPolicy new_growth_policy(bucket_count);
if(bucket_count == this->bucket_count()) {
return;
}
if(shoud_clear_old_erased_values(REHASH_CLEAR_OLD_ERASED_VALUE_THRESHOLD)) {
clear_old_erased_values();
}
std::vector<std::size_t> required_size_for_bucket(bucket_count, 0);
std::vector<std::size_t> bucket_for_ivalue(size(), 0);
std::size_t ivalue = 0;
for(auto it = begin(); it != end(); ++it) {
const std::size_t hash = hash_key(it.key(), it.key_size());
const std::size_t ibucket = new_growth_policy.bucket_for_hash(hash);
bucket_for_ivalue[ivalue] = ibucket;
required_size_for_bucket[ibucket] += array_bucket::entry_required_bytes(it.key_size());
ivalue++;
}
std::vector<array_bucket> new_buckets;
new_buckets.reserve(bucket_count);
for(std::size_t ibucket = 0; ibucket < bucket_count; ibucket++) {
new_buckets.emplace_back(required_size_for_bucket[ibucket]);
}
ivalue = 0;
for(auto it = begin(); it != end(); ++it) {
const std::size_t ibucket = bucket_for_ivalue[ivalue];
append_iterator_in_reserved_bucket_no_check(new_buckets[ibucket], it);
ivalue++;
}
using std::swap;
swap(static_cast<GrowthPolicy&>(*this), new_growth_policy);
m_buckets_data.swap(new_buckets);
m_buckets = !m_buckets_data.empty()?m_buckets_data.data():
static_empty_bucket_ptr();
// Call max_load_factor to change m_load_threshold
max_load_factor(m_max_load_factor);
}
template<class U = T, typename std::enable_if<!has_mapped_type<U>::value>::type* = nullptr>
void append_iterator_in_reserved_bucket_no_check(array_bucket& bucket, iterator it) {
bucket.append_in_reserved_bucket_no_check(it.key(), it.key_size());
}
template<class U = T, typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr>
void append_iterator_in_reserved_bucket_no_check(array_bucket& bucket, iterator it) {
bucket.append_in_reserved_bucket_no_check(it.key(), it.key_size(), it.value_position());
}
/**
* On serialization the values of each bucket (if has_mapped_type is true) are serialized
* next to the bucket. The potential old erased values in value_container are thus not serialized.
*
* On deserialization, when hash_compatible is true, we reaffect the value index (IndexSizeT) of each
* bucket with set_value as the position of each value is no more the same in value_container compared
* to when they were serialized.
*
* It's done this way as we can't call clear_old_erased_values() because we want the serialize
* method to remain const and we don't want to serialize/deserialize old erased values. As we may
* not serialize all the values in value_container, the values we keep can change of index.
* We thus have to modify the value indexes in the buckets.
*/
template<class Serializer>
void serialize_impl(Serializer& serializer) const {
const slz_size_type version = SERIALIZATION_PROTOCOL_VERSION;
serializer(version);
const slz_size_type bucket_count = m_buckets_data.size();
serializer(bucket_count);
const slz_size_type nb_elements = m_nb_elements;
serializer(nb_elements);
const float max_load_factor = m_max_load_factor;
serializer(max_load_factor);
for(const array_bucket& bucket: m_buckets_data) {
bucket.serialize(serializer);
serialize_bucket_values(serializer, bucket);
}
}
template<class Serializer, class U = T,
typename std::enable_if<!has_mapped_type<U>::value>::type* = nullptr>
void serialize_bucket_values(Serializer& /*serializer*/, const array_bucket& /*bucket*/) const {
}
template<class Serializer, class U = T,
typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr>
void serialize_bucket_values(Serializer& serializer, const array_bucket& bucket) const {
for(auto it = bucket.begin(); it != bucket.end(); ++it) {
serializer(this->m_values[it.value()]);
}
}
template<class Deserializer>
void deserialize_impl(Deserializer& deserializer, bool hash_compatible) {
tsl_ah_assert(m_buckets_data.empty()); // Current hash table must be empty
const slz_size_type version = deserialize_value<slz_size_type>(deserializer);
// For now we only have one version of the serialization protocol.
// If it doesn't match there is a problem with the file.
if(version != SERIALIZATION_PROTOCOL_VERSION) {
throw std::runtime_error("Can't deserialize the array_map/set. The protocol version header is invalid.");
}
const slz_size_type bucket_count_ds = deserialize_value<slz_size_type>(deserializer);
const slz_size_type nb_elements = deserialize_value<slz_size_type>(deserializer);
const float max_load_factor = deserialize_value<float>(deserializer);
m_nb_elements = numeric_cast<IndexSizeT>(nb_elements, "Deserialized nb_elements is too big.");
size_type bucket_count = numeric_cast<size_type>(bucket_count_ds, "Deserialized bucket_count is too big.");
GrowthPolicy::operator=(GrowthPolicy(bucket_count));
this->max_load_factor(max_load_factor);
value_container<T>::reserve(m_nb_elements);
if(hash_compatible) {
if(bucket_count != bucket_count_ds) {
throw std::runtime_error("The GrowthPolicy is not the same even though hash_compatible is true.");
}
m_buckets_data.reserve(bucket_count);
for(size_type i = 0; i < bucket_count; i++) {
m_buckets_data.push_back(array_bucket::deserialize(deserializer));
deserialize_bucket_values(deserializer, m_buckets_data.back());
}
}
else {
m_buckets_data.resize(bucket_count);
for(size_type i = 0; i < bucket_count; i++) {
// TODO use buffer to avoid reallocation on each deserialization.
array_bucket bucket = array_bucket::deserialize(deserializer);
deserialize_bucket_values(deserializer, bucket);
for(auto it_val = bucket.cbegin(); it_val != bucket.cend(); ++it_val) {
const std::size_t ibucket = bucket_for_hash(hash_key(it_val.key(), it_val.key_size()));
auto it_find = m_buckets_data[ibucket].find_or_end_of_bucket(it_val.key(), it_val.key_size());
if(it_find.second) {
throw std::runtime_error("Error on deserialization, the same key is presents multiple times.");
}
append_array_bucket_iterator_in_bucket(m_buckets_data[ibucket], it_find.first, it_val);
}
}
}
m_buckets = m_buckets_data.data();
if(load_factor() > this->max_load_factor()) {
throw std::runtime_error("Invalid max_load_factor. Check that the serializer and deserializer supports "
"floats correctly as they can be converted implicitely to ints.");
}
}
template<class Deserializer, class U = T,
typename std::enable_if<!has_mapped_type<U>::value>::type* = nullptr>
void deserialize_bucket_values(Deserializer& /*deserializer*/, array_bucket& /*bucket*/) {
}
template<class Deserializer, class U = T,
typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr>
void deserialize_bucket_values(Deserializer& deserializer, array_bucket& bucket) {
for(auto it = bucket.begin(); it != bucket.end(); ++it) {
this->m_values.emplace_back(deserialize_value<U>(deserializer));
tsl_ah_assert(this->m_values.size() - 1 <= std::numeric_limits<IndexSizeT>::max());
it.set_value(static_cast<IndexSizeT>(this->m_values.size() - 1));
}
}
template<class U = T, typename std::enable_if<!has_mapped_type<U>::value>::type* = nullptr>
void append_array_bucket_iterator_in_bucket(array_bucket& bucket,
typename array_bucket::const_iterator end_of_bucket,
typename array_bucket::const_iterator it_val)
{
bucket.append(end_of_bucket, it_val.key(), it_val.key_size());
}
template<class U = T, typename std::enable_if<has_mapped_type<U>::value>::type* = nullptr>
void append_array_bucket_iterator_in_bucket(array_bucket& bucket,
typename array_bucket::const_iterator end_of_bucket,
typename array_bucket::const_iterator it_val)
{
bucket.append(end_of_bucket, it_val.key(), it_val.key_size(), it_val.value());
}
public:
static const size_type DEFAULT_INIT_BUCKET_COUNT = 0;
static constexpr float DEFAULT_MAX_LOAD_FACTOR = 2.0f;
static const size_type MAX_KEY_SIZE = array_bucket::MAX_KEY_SIZE;
private:
/**
* Protocol version currenlty used for serialization.
*/
static const slz_size_type SERIALIZATION_PROTOCOL_VERSION = 1;
static constexpr float DEFAULT_CLEAR_OLD_ERASED_VALUE_THRESHOLD = 0.6f;
static constexpr float REHASH_CLEAR_OLD_ERASED_VALUE_THRESHOLD = 0.9f;
/**
* Return an always valid pointer to a static empty array_bucket.
*/
array_bucket* static_empty_bucket_ptr() {
static array_bucket empty_bucket;
return &empty_bucket;
}
private:
std::vector<array_bucket> m_buckets_data;
/**
* Points to m_buckets_data.data() if !m_buckets_data.empty() otherwise points to static_empty_bucket_ptr.
* This variable is useful to avoid the cost of checking if m_buckets_data is empty when trying
* to find an element.
*
* TODO Remove m_buckets_data and only use a pointer+size instead of a pointer+vector to save some space in the array_hash object.
*/
array_bucket* m_buckets;
IndexSizeT m_nb_elements;
float m_max_load_factor;
size_type m_load_threshold;
};
} // end namespace detail_array_hash
} //end namespace tsl
#endif
|