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
|
/*
Copyright (c) 2005-2025 Intel Corporation
Copyright (c) 2025 UXL Foundation Contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef __TBB_detail__concurrent_unordered_base_H
#define __TBB_detail__concurrent_unordered_base_H
#if !defined(__TBB_concurrent_unordered_map_H) && !defined(__TBB_concurrent_unordered_set_H)
#error Do not #include this internal file directly; use public TBB headers instead.
#endif
#include "_range_common.h"
#include "_containers_helpers.h"
#include "_segment_table.h"
#include "_hash_compare.h"
#include "_allocator_traits.h"
#include "_node_handle.h"
#include "_assert.h"
#include "_utils.h"
#include "_exception.h"
#include <iterator>
#include <utility>
#include <functional>
#include <initializer_list>
#include <atomic>
#include <type_traits>
#include <memory>
#include <algorithm>
#if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
#pragma warning(push)
#pragma warning(disable: 4127) // warning C4127: conditional expression is constant
#endif
namespace tbb {
namespace detail {
namespace d2 {
template <typename Traits>
class concurrent_unordered_base;
template<typename Container, typename Value>
class solist_iterator {
private:
using node_ptr = typename Container::value_node_ptr;
template <typename T, typename Allocator>
friend class split_ordered_list;
template<typename M, typename V>
friend class solist_iterator;
template <typename Traits>
friend class concurrent_unordered_base;
template<typename M, typename T, typename U>
friend bool operator==( const solist_iterator<M,T>& i, const solist_iterator<M,U>& j );
template<typename M, typename T, typename U>
friend bool operator!=( const solist_iterator<M,T>& i, const solist_iterator<M,U>& j );
public:
using value_type = Value;
using difference_type = typename Container::difference_type;
using pointer = value_type*;
using reference = value_type&;
using iterator_category = std::forward_iterator_tag;
solist_iterator() : my_node_ptr(nullptr) {}
solist_iterator( const solist_iterator<Container, typename Container::value_type>& other )
: my_node_ptr(other.my_node_ptr) {}
solist_iterator& operator=( const solist_iterator<Container, typename Container::value_type>& other ) {
my_node_ptr = other.my_node_ptr;
return *this;
}
reference operator*() const {
return my_node_ptr->value();
}
pointer operator->() const {
return my_node_ptr->storage();
}
solist_iterator& operator++() {
auto next_node = my_node_ptr->next();
while(next_node && next_node->is_dummy()) {
next_node = next_node->next();
}
my_node_ptr = static_cast<node_ptr>(next_node);
return *this;
}
solist_iterator operator++(int) {
solist_iterator tmp = *this;
++*this;
return tmp;
}
private:
solist_iterator( node_ptr pnode ) : my_node_ptr(pnode) {}
node_ptr get_node_ptr() const { return my_node_ptr; }
node_ptr my_node_ptr;
};
template<typename Solist, typename T, typename U>
bool operator==( const solist_iterator<Solist, T>& i, const solist_iterator<Solist, U>& j ) {
return i.my_node_ptr == j.my_node_ptr;
}
template<typename Solist, typename T, typename U>
bool operator!=( const solist_iterator<Solist, T>& i, const solist_iterator<Solist, U>& j ) {
return i.my_node_ptr != j.my_node_ptr;
}
template <typename SokeyType>
class list_node {
public:
using node_ptr = list_node*;
using sokey_type = SokeyType;
list_node(sokey_type key) : my_next(nullptr), my_order_key(key) {}
void init( sokey_type key ) {
my_order_key = key;
}
sokey_type order_key() const {
return my_order_key;
}
bool is_dummy() {
// The last bit of order key is unset for dummy nodes
return (my_order_key & 0x1) == 0;
}
node_ptr next() const {
return my_next.load(std::memory_order_acquire);
}
void set_next( node_ptr next_node ) {
my_next.store(next_node, std::memory_order_release);
}
bool try_set_next( node_ptr expected_next, node_ptr new_next ) {
return my_next.compare_exchange_strong(expected_next, new_next);
}
private:
std::atomic<node_ptr> my_next;
sokey_type my_order_key;
}; // class list_node
template <typename ValueType, typename SokeyType>
class value_node : public list_node<SokeyType>
{
public:
using base_type = list_node<SokeyType>;
using sokey_type = typename base_type::sokey_type;
using value_type = ValueType;
value_node( sokey_type ord_key ) : base_type(ord_key) {}
~value_node() {}
value_type* storage() {
return &my_value;
}
value_type& value() {
return *storage();
}
private:
union {
value_type my_value;
};
}; // class value_node
template <typename Traits>
class concurrent_unordered_base {
using self_type = concurrent_unordered_base<Traits>;
using traits_type = Traits;
using hash_compare_type = typename traits_type::hash_compare_type;
class unordered_segment_table;
public:
using value_type = typename traits_type::value_type;
using key_type = typename traits_type::key_type;
using allocator_type = typename traits_type::allocator_type;
private:
using allocator_traits_type = tbb::detail::allocator_traits<allocator_type>;
// TODO: check assert conditions for different C++ standards
static_assert(std::is_same<typename allocator_traits_type::value_type, value_type>::value,
"value_type of the container must be the same as its allocator");
using sokey_type = std::size_t;
public:
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using iterator = solist_iterator<self_type, value_type>;
using const_iterator = solist_iterator<self_type, const value_type>;
using local_iterator = iterator;
using const_local_iterator = const_iterator;
using reference = value_type&;
using const_reference = const value_type&;
using pointer = typename allocator_traits_type::pointer;
using const_pointer = typename allocator_traits_type::const_pointer;
using hasher = typename hash_compare_type::hasher;
using key_equal = typename hash_compare_type::key_equal;
private:
using list_node_type = list_node<sokey_type>;
using value_node_type = value_node<value_type, sokey_type>;
using node_ptr = list_node_type*;
using value_node_ptr = value_node_type*;
using value_node_allocator_type = typename allocator_traits_type::template rebind_alloc<value_node_type>;
using node_allocator_type = typename allocator_traits_type::template rebind_alloc<list_node_type>;
using node_allocator_traits = tbb::detail::allocator_traits<node_allocator_type>;
using value_node_allocator_traits = tbb::detail::allocator_traits<value_node_allocator_type>;
static constexpr size_type round_up_to_power_of_two( size_type bucket_count ) {
return size_type(1) << size_type(tbb::detail::log2(uintptr_t(bucket_count == 0 ? 1 : bucket_count) * 2 - 1));
}
template <typename T>
using is_transparent = dependent_bool<has_transparent_key_equal<key_type, hasher, key_equal>, T>;
public:
using node_type = d1::node_handle<key_type, value_type, value_node_type, allocator_type>;
explicit concurrent_unordered_base( size_type bucket_count, const hasher& hash = hasher(),
const key_equal& equal = key_equal(), const allocator_type& alloc = allocator_type() )
: my_size(0),
my_bucket_count(round_up_to_power_of_two(bucket_count)),
my_max_load_factor(float(initial_max_load_factor)),
my_hash_compare(hash, equal),
my_head(sokey_type(0)),
my_segments(alloc) {}
concurrent_unordered_base() : concurrent_unordered_base(initial_bucket_count) {}
concurrent_unordered_base( size_type bucket_count, const allocator_type& alloc )
: concurrent_unordered_base(bucket_count, hasher(), key_equal(), alloc) {}
concurrent_unordered_base( size_type bucket_count, const hasher& hash, const allocator_type& alloc )
: concurrent_unordered_base(bucket_count, hash, key_equal(), alloc) {}
explicit concurrent_unordered_base( const allocator_type& alloc )
: concurrent_unordered_base(initial_bucket_count, hasher(), key_equal(), alloc) {}
template <typename InputIterator>
concurrent_unordered_base( InputIterator first, InputIterator last,
size_type bucket_count = initial_bucket_count, const hasher& hash = hasher(),
const key_equal& equal = key_equal(), const allocator_type& alloc = allocator_type() )
: concurrent_unordered_base(bucket_count, hash, equal, alloc)
{
insert(first, last);
}
template <typename InputIterator>
concurrent_unordered_base( InputIterator first, InputIterator last,
size_type bucket_count, const allocator_type& alloc )
: concurrent_unordered_base(first, last, bucket_count, hasher(), key_equal(), alloc) {}
template <typename InputIterator>
concurrent_unordered_base( InputIterator first, InputIterator last,
size_type bucket_count, const hasher& hash, const allocator_type& alloc )
: concurrent_unordered_base(first, last, bucket_count, hash, key_equal(), alloc) {}
concurrent_unordered_base( const concurrent_unordered_base& other )
: my_size(other.my_size.load(std::memory_order_relaxed)),
my_bucket_count(other.my_bucket_count.load(std::memory_order_relaxed)),
my_max_load_factor(other.my_max_load_factor),
my_hash_compare(other.my_hash_compare),
my_head(other.my_head.order_key()),
my_segments(other.my_segments)
{
try_call( [&] {
internal_copy(other);
} ).on_exception( [&] {
clear();
});
}
concurrent_unordered_base( const concurrent_unordered_base& other, const allocator_type& alloc )
: my_size(other.my_size.load(std::memory_order_relaxed)),
my_bucket_count(other.my_bucket_count.load(std::memory_order_relaxed)),
my_max_load_factor(other.my_max_load_factor),
my_hash_compare(other.my_hash_compare),
my_head(other.my_head.order_key()),
my_segments(other.my_segments, alloc)
{
try_call( [&] {
internal_copy(other);
} ).on_exception( [&] {
clear();
});
}
concurrent_unordered_base( concurrent_unordered_base&& other )
: my_size(other.my_size.load(std::memory_order_relaxed)),
my_bucket_count(other.my_bucket_count.load(std::memory_order_relaxed)),
my_max_load_factor(std::move(other.my_max_load_factor)),
my_hash_compare(std::move(other.my_hash_compare)),
my_head(other.my_head.order_key()),
my_segments(std::move(other.my_segments))
{
move_content(std::move(other));
}
concurrent_unordered_base( concurrent_unordered_base&& other, const allocator_type& alloc )
: my_size(other.my_size.load(std::memory_order_relaxed)),
my_bucket_count(other.my_bucket_count.load(std::memory_order_relaxed)),
my_max_load_factor(std::move(other.my_max_load_factor)),
my_hash_compare(std::move(other.my_hash_compare)),
my_head(other.my_head.order_key()),
my_segments(std::move(other.my_segments), alloc)
{
using is_always_equal = typename allocator_traits_type::is_always_equal;
internal_move_construct_with_allocator(std::move(other), alloc, is_always_equal());
}
concurrent_unordered_base( std::initializer_list<value_type> init,
size_type bucket_count = initial_bucket_count,
const hasher& hash = hasher(), const key_equal& equal = key_equal(),
const allocator_type& alloc = allocator_type() )
: concurrent_unordered_base(init.begin(), init.end(), bucket_count, hash, equal, alloc) {}
concurrent_unordered_base( std::initializer_list<value_type> init,
size_type bucket_count, const allocator_type& alloc )
: concurrent_unordered_base(init, bucket_count, hasher(), key_equal(), alloc) {}
concurrent_unordered_base( std::initializer_list<value_type> init,
size_type bucket_count, const hasher& hash, const allocator_type& alloc )
: concurrent_unordered_base(init, bucket_count, hash, key_equal(), alloc) {}
~concurrent_unordered_base() {
internal_clear();
}
concurrent_unordered_base& operator=( const concurrent_unordered_base& other ) {
if (this != &other) {
clear();
my_size.store(other.my_size.load(std::memory_order_relaxed), std::memory_order_relaxed);
my_bucket_count.store(other.my_bucket_count.load(std::memory_order_relaxed), std::memory_order_relaxed);
my_max_load_factor = other.my_max_load_factor;
my_hash_compare = other.my_hash_compare;
my_segments = other.my_segments;
internal_copy(other); // TODO: guards for exceptions?
}
return *this;
}
concurrent_unordered_base& operator=( concurrent_unordered_base&& other ) noexcept(unordered_segment_table::is_noexcept_assignment) {
if (this != &other) {
clear();
my_size.store(other.my_size.load(std::memory_order_relaxed), std::memory_order_relaxed);
my_bucket_count.store(other.my_bucket_count.load(std::memory_order_relaxed), std::memory_order_relaxed);
my_max_load_factor = std::move(other.my_max_load_factor);
my_hash_compare = std::move(other.my_hash_compare);
my_segments = std::move(other.my_segments);
using pocma_type = typename allocator_traits_type::propagate_on_container_move_assignment;
using is_always_equal = typename allocator_traits_type::is_always_equal;
internal_move_assign(std::move(other), tbb::detail::disjunction<pocma_type, is_always_equal>());
}
return *this;
}
concurrent_unordered_base& operator=( std::initializer_list<value_type> init ) {
clear();
insert(init);
return *this;
}
void swap( concurrent_unordered_base& other ) noexcept(unordered_segment_table::is_noexcept_swap) {
if (this != &other) {
using pocs_type = typename allocator_traits_type::propagate_on_container_swap;
using is_always_equal = typename allocator_traits_type::is_always_equal;
internal_swap(other, tbb::detail::disjunction<pocs_type, is_always_equal>());
}
}
allocator_type get_allocator() const noexcept { return my_segments.get_allocator(); }
iterator begin() noexcept { return iterator(first_value_node(&my_head)); }
const_iterator begin() const noexcept { return const_iterator(first_value_node(const_cast<node_ptr>(&my_head))); }
const_iterator cbegin() const noexcept { return const_iterator(first_value_node(const_cast<node_ptr>(&my_head))); }
iterator end() noexcept { return iterator(nullptr); }
const_iterator end() const noexcept { return const_iterator(nullptr); }
const_iterator cend() const noexcept { return const_iterator(nullptr); }
__TBB_nodiscard bool empty() const noexcept { return size() == 0; }
size_type size() const noexcept { return my_size.load(std::memory_order_relaxed); }
size_type max_size() const noexcept { return allocator_traits_type::max_size(get_allocator()); }
void clear() noexcept {
internal_clear();
}
std::pair<iterator, bool> insert( const value_type& value ) {
return internal_insert_value(value);
}
std::pair<iterator, bool> insert( value_type&& value ) {
return internal_insert_value(std::move(value));
}
iterator insert( const_iterator, const value_type& value ) {
// Ignore hint
return insert(value).first;
}
iterator insert( const_iterator, value_type&& value ) {
// Ignore hint
return insert(std::move(value)).first;
}
template <typename InputIterator>
void insert( InputIterator first, InputIterator last ) {
for (; first != last; ++first) {
insert(*first);
}
}
void insert( std::initializer_list<value_type> init ) {
insert(init.begin(), init.end());
}
std::pair<iterator, bool> insert( node_type&& nh ) {
if (!nh.empty()) {
value_node_ptr insert_node = d1::node_handle_accessor::get_node_ptr(nh);
auto init_node = [&insert_node]( sokey_type order_key )->value_node_ptr {
insert_node->init(order_key);
return insert_node;
};
auto insert_result = internal_insert(insert_node->value(), init_node);
if (insert_result.inserted) {
// If the insertion succeeded - set node handle to the empty state
__TBB_ASSERT(insert_result.remaining_node == nullptr,
"internal_insert_node should not return the remaining node if the insertion succeeded");
d1::node_handle_accessor::deactivate(nh);
}
return { iterator(insert_result.node_with_equal_key), insert_result.inserted };
}
return {end(), false};
}
iterator insert( const_iterator, node_type&& nh ) {
// Ignore hint
return insert(std::move(nh)).first;
}
template <typename... Args>
std::pair<iterator, bool> emplace( Args&&... args ) {
// Create a node with temporary order_key 0, which will be reinitialize
// in internal_insert after the hash calculation
value_node_ptr insert_node = create_node(0, std::forward<Args>(args)...);
auto init_node = [&insert_node]( sokey_type order_key )->value_node_ptr {
insert_node->init(order_key);
return insert_node;
};
auto insert_result = internal_insert(insert_node->value(), init_node);
if (!insert_result.inserted) {
// If the insertion failed - destroy the node which was created
insert_node->init(split_order_key_regular(1));
destroy_node(insert_node);
}
return { iterator(insert_result.node_with_equal_key), insert_result.inserted };
}
template <typename... Args>
iterator emplace_hint( const_iterator, Args&&... args ) {
// Ignore hint
return emplace(std::forward<Args>(args)...).first;
}
iterator unsafe_erase( const_iterator pos ) {
return iterator(first_value_node(internal_erase(pos.get_node_ptr())));
}
iterator unsafe_erase( iterator pos ) {
return iterator(first_value_node(internal_erase(pos.get_node_ptr())));
}
iterator unsafe_erase( const_iterator first, const_iterator last ) {
while(first != last) {
first = unsafe_erase(first);
}
return iterator(first.get_node_ptr());
}
size_type unsafe_erase( const key_type& key ) {
return internal_erase_by_key(key);
}
template <typename K>
typename std::enable_if<is_transparent<K>::value
&& !std::is_convertible<K, const_iterator>::value
&& !std::is_convertible<K, iterator>::value,
size_type>::type unsafe_erase( const K& key )
{
return internal_erase_by_key(key);
}
node_type unsafe_extract( const_iterator pos ) {
internal_extract(pos.get_node_ptr());
return d1::node_handle_accessor::construct<node_type>(pos.get_node_ptr());
}
node_type unsafe_extract( iterator pos ) {
internal_extract(pos.get_node_ptr());
return d1::node_handle_accessor::construct<node_type>(pos.get_node_ptr());
}
node_type unsafe_extract( const key_type& key ) {
iterator item = find(key);
return item == end() ? node_type() : unsafe_extract(item);
}
template <typename K>
typename std::enable_if<is_transparent<K>::value
&& !std::is_convertible<K, const_iterator>::value
&& !std::is_convertible<K, iterator>::value,
node_type>::type unsafe_extract( const K& key )
{
iterator item = find(key);
return item == end() ? node_type() : unsafe_extract(item);
}
// Lookup functions
iterator find( const key_type& key ) {
value_node_ptr result = internal_find(key);
return result == nullptr ? end() : iterator(result);
}
const_iterator find( const key_type& key ) const {
value_node_ptr result = const_cast<self_type*>(this)->internal_find(key);
return result == nullptr ? end() : const_iterator(result);
}
template <typename K>
typename std::enable_if<is_transparent<K>::value, iterator>::type find( const K& key ) {
value_node_ptr result = internal_find(key);
return result == nullptr ? end() : iterator(result);
}
template <typename K>
typename std::enable_if<is_transparent<K>::value, const_iterator>::type find( const K& key ) const {
value_node_ptr result = const_cast<self_type*>(this)->internal_find(key);
return result == nullptr ? end() : const_iterator(result);
}
std::pair<iterator, iterator> equal_range( const key_type& key ) {
auto result = internal_equal_range(key);
return std::make_pair(iterator(result.first), iterator(result.second));
}
std::pair<const_iterator, const_iterator> equal_range( const key_type& key ) const {
auto result = const_cast<self_type*>(this)->internal_equal_range(key);
return std::make_pair(const_iterator(result.first), const_iterator(result.second));
}
template <typename K>
typename std::enable_if<is_transparent<K>::value, std::pair<iterator, iterator>>::type equal_range( const K& key ) {
auto result = internal_equal_range(key);
return std::make_pair(iterator(result.first), iterator(result.second));
}
template <typename K>
typename std::enable_if<is_transparent<K>::value, std::pair<const_iterator, const_iterator>>::type equal_range( const K& key ) const {
auto result = const_cast<self_type*>(this)->internal_equal_range(key);
return std::make_pair(iterator(result.first), iterator(result.second));
}
size_type count( const key_type& key ) const {
return internal_count(key);
}
template <typename K>
typename std::enable_if<is_transparent<K>::value, size_type>::type count( const K& key ) const {
return internal_count(key);
}
bool contains( const key_type& key ) const {
return find(key) != end();
}
template <typename K>
typename std::enable_if<is_transparent<K>::value, bool>::type contains( const K& key ) const {
return find(key) != end();
}
// Bucket interface
local_iterator unsafe_begin( size_type n ) {
return local_iterator(first_value_node(get_bucket(n)));
}
const_local_iterator unsafe_begin( size_type n ) const {
auto bucket_begin = first_value_node(const_cast<self_type*>(this)->get_bucket(n));
return const_local_iterator(bucket_begin);
}
const_local_iterator unsafe_cbegin( size_type n ) const {
auto bucket_begin = first_value_node(const_cast<self_type*>(this)->get_bucket(n));
return const_local_iterator(bucket_begin);
}
local_iterator unsafe_end( size_type n ) {
size_type bucket_count = my_bucket_count.load(std::memory_order_relaxed);
return n != bucket_count - 1 ? unsafe_begin(get_next_bucket_index(n)) : local_iterator(nullptr);
}
const_local_iterator unsafe_end( size_type n ) const {
size_type bucket_count = my_bucket_count.load(std::memory_order_relaxed);
return n != bucket_count - 1 ? unsafe_begin(get_next_bucket_index(n)) : const_local_iterator(nullptr);
}
const_local_iterator unsafe_cend( size_type n ) const {
size_type bucket_count = my_bucket_count.load(std::memory_order_relaxed);
return n != bucket_count - 1 ? unsafe_begin(get_next_bucket_index(n)) : const_local_iterator(nullptr);
}
size_type unsafe_bucket_count() const { return my_bucket_count.load(std::memory_order_relaxed); }
size_type unsafe_max_bucket_count() const {
return max_size();
}
size_type unsafe_bucket_size( size_type n ) const {
return size_type(std::distance(unsafe_begin(n), unsafe_end(n)));
}
size_type unsafe_bucket( const key_type& key ) const {
return my_hash_compare(key) % my_bucket_count.load(std::memory_order_relaxed);
}
// Hash policy
float load_factor() const {
return float(size() / float(my_bucket_count.load(std::memory_order_acquire)));
}
float max_load_factor() const { return my_max_load_factor; }
void max_load_factor( float mlf ) {
if (mlf != mlf || mlf < 0) {
tbb::detail::throw_exception(exception_id::invalid_load_factor);
}
my_max_load_factor = mlf;
} // TODO: unsafe?
void rehash( size_type bucket_count ) {
size_type current_bucket_count = my_bucket_count.load(std::memory_order_acquire);
if (current_bucket_count < bucket_count) {
// TODO: do we need do-while here?
my_bucket_count.compare_exchange_strong(current_bucket_count, round_up_to_power_of_two(bucket_count));
}
}
void reserve( size_type elements_count ) {
size_type current_bucket_count = my_bucket_count.load(std::memory_order_acquire);
size_type necessary_bucket_count = current_bucket_count;
// max_load_factor() is currently unsafe, so we can assume that my_max_load_factor
// would not be changed during the calculation
// TODO: Log2 seems useful here
while (necessary_bucket_count * max_load_factor() < elements_count) {
necessary_bucket_count <<= 1;
}
while (!my_bucket_count.compare_exchange_strong(current_bucket_count, necessary_bucket_count)) {
if (current_bucket_count >= necessary_bucket_count)
break;
}
}
// Observers
hasher hash_function() const { return my_hash_compare.hash_function(); }
key_equal key_eq() const { return my_hash_compare.key_eq(); }
class const_range_type {
private:
const concurrent_unordered_base& my_instance;
node_ptr my_begin_node; // may be node* const
node_ptr my_end_node;
mutable node_ptr my_midpoint_node;
public:
using size_type = typename concurrent_unordered_base::size_type;
using value_type = typename concurrent_unordered_base::value_type;
using reference = typename concurrent_unordered_base::reference;
using difference_type = typename concurrent_unordered_base::difference_type;
using iterator = typename concurrent_unordered_base::const_iterator;
bool empty() const { return my_begin_node == my_end_node; }
bool is_divisible() const {
return my_midpoint_node != my_end_node;
}
size_type grainsize() const { return 1; }
const_range_type( const_range_type& range, split )
: my_instance(range.my_instance),
my_begin_node(range.my_midpoint_node),
my_end_node(range.my_end_node)
{
range.my_end_node = my_begin_node;
__TBB_ASSERT(!empty(), "Splitting despite the range is not divisible");
__TBB_ASSERT(!range.empty(), "Splitting despite the range is not divisible");
set_midpoint();
range.set_midpoint();
}
iterator begin() const { return iterator(my_instance.first_value_node(my_begin_node)); }
iterator end() const { return iterator(my_instance.first_value_node(my_end_node)); }
const_range_type( const concurrent_unordered_base& table )
: my_instance(table), my_begin_node(my_instance.first_value_node(const_cast<node_ptr>(&table.my_head))), my_end_node(nullptr)
{
set_midpoint();
}
private:
void set_midpoint() const {
if (empty()) {
my_midpoint_node = my_end_node;
} else {
sokey_type invalid_key = ~sokey_type(0);
sokey_type begin_key = my_begin_node != nullptr ? my_begin_node->order_key() : invalid_key;
sokey_type end_key = my_end_node != nullptr ? my_end_node->order_key() : invalid_key;
size_type mid_bucket = reverse_bits(begin_key + (end_key - begin_key) / 2) %
my_instance.my_bucket_count.load(std::memory_order_relaxed);
while( my_instance.my_segments[mid_bucket].load(std::memory_order_relaxed) == nullptr) {
mid_bucket = my_instance.get_parent(mid_bucket);
}
if (reverse_bits(mid_bucket) > begin_key) {
// Found a dummy node between begin and end
my_midpoint_node = my_instance.first_value_node(
my_instance.my_segments[mid_bucket].load(std::memory_order_relaxed));
} else {
// Didn't find a dummy node between begin and end
my_midpoint_node = my_end_node;
}
}
}
}; // class const_range_type
class range_type : public const_range_type {
public:
using iterator = typename concurrent_unordered_base::iterator;
using const_range_type::const_range_type;
iterator begin() const { return iterator(const_range_type::begin().get_node_ptr()); }
iterator end() const { return iterator(const_range_type::end().get_node_ptr()); }
}; // class range_type
// Parallel iteration
range_type range() {
return range_type(*this);
}
const_range_type range() const {
return const_range_type(*this);
}
protected:
static constexpr bool allow_multimapping = traits_type::allow_multimapping;
private:
static constexpr size_type initial_bucket_count = 8;
static constexpr float initial_max_load_factor = 4; // TODO: consider 1?
static constexpr size_type pointers_per_embedded_table = sizeof(size_type) * 8 - 1;
class unordered_segment_table
: public d1::segment_table<std::atomic<node_ptr>, allocator_type, unordered_segment_table, pointers_per_embedded_table>
{
using self_type = unordered_segment_table;
using atomic_node_ptr = std::atomic<node_ptr>;
using base_type = d1::segment_table<std::atomic<node_ptr>, allocator_type, unordered_segment_table, pointers_per_embedded_table>;
using segment_type = typename base_type::segment_type;
using base_allocator_type = typename base_type::allocator_type;
using segment_allocator_type = typename allocator_traits_type::template rebind_alloc<atomic_node_ptr>;
using segment_allocator_traits = tbb::detail::allocator_traits<segment_allocator_type>;
public:
// Segment table for unordered containers should not be extended in the wait- free implementation
static constexpr bool allow_table_extending = false;
static constexpr bool is_noexcept_assignment = std::is_nothrow_move_assignable<hasher>::value &&
std::is_nothrow_move_assignable<key_equal>::value &&
segment_allocator_traits::is_always_equal::value;
static constexpr bool is_noexcept_swap = tbb::detail::is_nothrow_swappable<hasher>::value &&
tbb::detail::is_nothrow_swappable<key_equal>::value &&
segment_allocator_traits::is_always_equal::value;
// TODO: using base_type::base_type is not compiling on Windows and Intel Compiler - investigate
unordered_segment_table( const base_allocator_type& alloc = base_allocator_type() )
: base_type(alloc) {}
unordered_segment_table( const unordered_segment_table& ) = default;
unordered_segment_table( const unordered_segment_table& other, const base_allocator_type& alloc )
: base_type(other, alloc) {}
unordered_segment_table( unordered_segment_table&& ) = default;
unordered_segment_table( unordered_segment_table&& other, const base_allocator_type& alloc )
: base_type(std::move(other), alloc) {}
unordered_segment_table& operator=( const unordered_segment_table& ) = default;
unordered_segment_table& operator=( unordered_segment_table&& ) = default;
segment_type create_segment( typename base_type::segment_table_type, typename base_type::segment_index_type segment_index, size_type ) {
segment_allocator_type alloc(this->get_allocator());
size_type seg_size = this->segment_size(segment_index);
segment_type new_segment = segment_allocator_traits::allocate(alloc, seg_size);
for (size_type i = 0; i != seg_size; ++i) {
segment_allocator_traits::construct(alloc, new_segment + i, nullptr);
}
return new_segment;
}
segment_type nullify_segment( typename base_type::segment_table_type table, size_type segment_index ) {
segment_type target_segment = table[segment_index].load(std::memory_order_relaxed);
table[segment_index].store(nullptr, std::memory_order_relaxed);
return target_segment;
}
// deallocate_segment is required by the segment_table base class, but
// in unordered, it is also necessary to call the destructor during deallocation
void deallocate_segment( segment_type address, size_type index ) {
destroy_segment(address, index);
}
void destroy_segment( segment_type address, size_type index ) {
segment_allocator_type alloc(this->get_allocator());
for (size_type i = 0; i != this->segment_size(index); ++i) {
segment_allocator_traits::destroy(alloc, address + i);
}
segment_allocator_traits::deallocate(alloc, address, this->segment_size(index));
}
void copy_segment( size_type index, segment_type, segment_type to ) {
if (index == 0) {
// The first element in the first segment is embedded into the table (my_head)
// so the first pointer should not be stored here
// It would be stored during move ctor/assignment operation
to[1].store(nullptr, std::memory_order_relaxed);
} else {
for (size_type i = 0; i != this->segment_size(index); ++i) {
to[i].store(nullptr, std::memory_order_relaxed);
}
}
}
void move_segment( size_type index, segment_type from, segment_type to ) {
if (index == 0) {
// The first element in the first segment is embedded into the table (my_head)
// so the first pointer should not be stored here
// It would be stored during move ctor/assignment operation
to[1].store(from[1].load(std::memory_order_relaxed), std::memory_order_relaxed);
} else {
for (size_type i = 0; i != this->segment_size(index); ++i) {
to[i].store(from[i].load(std::memory_order_relaxed), std::memory_order_relaxed);
from[i].store(nullptr, std::memory_order_relaxed);
}
}
}
// allocate_long_table is required by the segment_table base class, but unused for unordered containers
typename base_type::segment_table_type allocate_long_table( const typename base_type::atomic_segment*, size_type ) {
__TBB_ASSERT(false, "This method should never been called");
// TableType is a pointer
return nullptr;
}
// destroy_elements is required by the segment_table base class, but unused for unordered containers
// this function call but do nothing
void destroy_elements() {}
}; // struct unordered_segment_table
void internal_clear() {
// TODO: consider usefulness of two versions of clear() - with dummy nodes deallocation and without it
node_ptr next = my_head.next();
node_ptr curr = next;
my_head.set_next(nullptr);
while (curr != nullptr) {
next = curr->next();
destroy_node(curr);
curr = next;
}
my_size.store(0, std::memory_order_relaxed);
my_segments.clear();
}
void destroy_node( node_ptr node ) {
if (node->is_dummy()) {
node_allocator_type dummy_node_allocator(my_segments.get_allocator());
// Destroy the node
node_allocator_traits::destroy(dummy_node_allocator, node);
// Deallocate the memory
node_allocator_traits::deallocate(dummy_node_allocator, node, 1);
} else {
// GCC 11.1 issues a warning here that incorrect destructor might be called for dummy_nodes
#if (__TBB_GCC_VERSION >= 110100 && __TBB_GCC_VERSION < 160000 ) && !__clang__ && !__INTEL_COMPILER
volatile
#endif
value_node_ptr val_node = static_cast<value_node_ptr>(node);
value_node_allocator_type value_node_allocator(my_segments.get_allocator());
// Destroy the value
value_node_allocator_traits::destroy(value_node_allocator, val_node->storage());
// Destroy the node
value_node_allocator_traits::destroy(value_node_allocator, val_node);
// Deallocate the memory
value_node_allocator_traits::deallocate(value_node_allocator, val_node, 1);
}
}
struct internal_insert_return_type {
// If the insertion failed - the remaining_node points to the node, which was failed to insert
// This node can be allocated in process of insertion
value_node_ptr remaining_node;
// If the insertion failed - node_with_equal_key points to the node in the list with the
// key, equivalent to the inserted, otherwise it points to the node, which was inserted.
value_node_ptr node_with_equal_key;
// Insertion status
// NOTE: if it is true - remaining_node should be nullptr
bool inserted;
}; // struct internal_insert_return_type
// Inserts the value into the split ordered list
template <typename ValueType>
std::pair<iterator, bool> internal_insert_value( ValueType&& value ) {
auto create_value_node = [&value, this]( sokey_type order_key )->value_node_ptr {
return create_node(order_key, std::forward<ValueType>(value));
};
auto insert_result = internal_insert(value, create_value_node);
if (insert_result.remaining_node != nullptr) {
// If the insertion fails - destroy the node which was failed to insert if it exist
__TBB_ASSERT(!insert_result.inserted,
"remaining_node should be nullptr if the node was successfully inserted");
destroy_node(insert_result.remaining_node);
}
return { iterator(insert_result.node_with_equal_key), insert_result.inserted };
}
// Inserts the node into the split ordered list
// Creates a node using the specified callback after the place for insertion was found
// Returns internal_insert_return_type object, where:
// - If the insertion succeeded:
// - remaining_node is nullptr
// - node_with_equal_key point to the inserted node
// - inserted is true
// - If the insertion failed:
// - remaining_node points to the node, that was failed to insert if it was created.
// nullptr if the node was not created, because the requested key was already
// presented in the list
// - node_with_equal_key point to the element in the list with the key, equivalent to
// to the requested key
// - inserted is false
template <typename ValueType, typename CreateInsertNode>
internal_insert_return_type internal_insert( ValueType&& value, CreateInsertNode create_insert_node ) {
static_assert(std::is_same<typename std::decay<ValueType>::type, value_type>::value,
"Incorrect type in internal_insert");
const key_type& key = traits_type::get_key(value);
sokey_type hash_key = sokey_type(my_hash_compare(key));
sokey_type order_key = split_order_key_regular(hash_key);
node_ptr prev = prepare_bucket(hash_key);
__TBB_ASSERT(prev != nullptr, "Invalid head node");
auto search_result = search_after(prev, order_key, key);
if (search_result.second) {
return internal_insert_return_type{ nullptr, search_result.first, false };
}
value_node_ptr new_node = create_insert_node(order_key);
node_ptr curr = search_result.first;
while (!try_insert(prev, new_node, curr)) {
search_result = search_after(prev, order_key, key);
if (search_result.second) {
return internal_insert_return_type{ new_node, search_result.first, false };
}
curr = search_result.first;
}
auto sz = my_size.fetch_add(1);
adjust_table_size(sz + 1, my_bucket_count.load(std::memory_order_acquire));
return internal_insert_return_type{ nullptr, static_cast<value_node_ptr>(new_node), true };
}
// Searches the node with the key, equivalent to key with requested order key after the node prev
// Returns the existing node and true if the node is already in the list
// Returns the first node with the order key, greater than requested and false if the node is not presented in the list
std::pair<value_node_ptr, bool> search_after( node_ptr& prev, sokey_type order_key, const key_type& key ) {
// NOTE: static_cast<value_node_ptr>(curr) should be done only after we would ensure
// that the node is not a dummy node
node_ptr curr = prev->next();
while (curr != nullptr && (curr->order_key() < order_key ||
(curr->order_key() == order_key && !my_hash_compare(traits_type::get_key(static_cast<value_node_ptr>(curr)->value()), key))))
{
prev = curr;
curr = curr->next();
}
if (curr != nullptr && curr->order_key() == order_key && !allow_multimapping) {
return { static_cast<value_node_ptr>(curr), true };
}
return { static_cast<value_node_ptr>(curr), false };
}
void adjust_table_size( size_type total_elements, size_type current_size ) {
// Grow the table by a factor of 2 if possible and needed
if ( (float(total_elements) / float(current_size)) > my_max_load_factor ) {
// Double the size of the hash only if size hash not changed in between loads
my_bucket_count.compare_exchange_strong(current_size, 2u * current_size);
}
}
node_ptr insert_dummy_node( node_ptr parent_dummy_node, sokey_type order_key ) {
node_ptr prev_node = parent_dummy_node;
node_ptr dummy_node = create_dummy_node(order_key);
node_ptr next_node;
do {
next_node = prev_node->next();
// Move forward through the list while the order key is less than requested
while (next_node != nullptr && next_node->order_key() < order_key) {
prev_node = next_node;
next_node = next_node->next();
}
if (next_node != nullptr && next_node->order_key() == order_key) {
// Another dummy node with the same order key was inserted by another thread
// Destroy the node and exit
destroy_node(dummy_node);
return next_node;
}
} while (!try_insert(prev_node, dummy_node, next_node));
return dummy_node;
}
// Try to insert a node between prev_node and expected next
// If the next is not equal to expected next - return false
static bool try_insert( node_ptr prev_node, node_ptr new_node, node_ptr current_next_node ) {
new_node->set_next(current_next_node);
return prev_node->try_set_next(current_next_node, new_node);
}
// Returns the bucket, associated with the hash_key
node_ptr prepare_bucket( sokey_type hash_key ) {
size_type bucket = hash_key % my_bucket_count.load(std::memory_order_acquire);
return get_bucket(bucket);
}
// Initialize the corresponding bucket if it is not initialized
node_ptr get_bucket( size_type bucket_index ) {
if (my_segments[bucket_index].load(std::memory_order_acquire) == nullptr) {
init_bucket(bucket_index);
}
return my_segments[bucket_index].load(std::memory_order_acquire);
}
void init_bucket( size_type bucket ) {
if (bucket == 0) {
// Atomicaly store the first bucket into my_head
node_ptr disabled = nullptr;
my_segments[0].compare_exchange_strong(disabled, &my_head);
return;
}
size_type parent_bucket = get_parent(bucket);
while (my_segments[parent_bucket].load(std::memory_order_acquire) == nullptr) {
// Initialize all of the parent buckets
init_bucket(parent_bucket);
}
__TBB_ASSERT(my_segments[parent_bucket].load(std::memory_order_acquire) != nullptr, "Parent bucket should be initialized");
node_ptr parent = my_segments[parent_bucket].load(std::memory_order_acquire);
// Insert dummy node into the list
node_ptr dummy_node = insert_dummy_node(parent, split_order_key_dummy(bucket));
// TODO: consider returning pair<node_ptr, bool> to avoid store operation if the bucket was stored by an other thread
// or move store to insert_dummy_node
// Add dummy_node into the segment table
my_segments[bucket].store(dummy_node, std::memory_order_release);
}
node_ptr create_dummy_node( sokey_type order_key ) {
node_allocator_type dummy_node_allocator(my_segments.get_allocator());
node_ptr dummy_node = node_allocator_traits::allocate(dummy_node_allocator, 1);
node_allocator_traits::construct(dummy_node_allocator, dummy_node, order_key);
return dummy_node;
}
template <typename... Args>
value_node_ptr create_node( sokey_type order_key, Args&&... args ) {
value_node_allocator_type value_node_allocator(my_segments.get_allocator());
// Allocate memory for the value_node
value_node_ptr new_node = value_node_allocator_traits::allocate(value_node_allocator, 1);
// Construct the node
value_node_allocator_traits::construct(value_node_allocator, new_node, order_key);
// try_call API is not convenient here due to broken
// variadic capture on GCC 4.8.5
auto value_guard = make_raii_guard([&] {
value_node_allocator_traits::destroy(value_node_allocator, new_node);
value_node_allocator_traits::deallocate(value_node_allocator, new_node, 1);
});
// Construct the value in the node
value_node_allocator_traits::construct(value_node_allocator, new_node->storage(), std::forward<Args>(args)...);
value_guard.dismiss();
return new_node;
}
value_node_ptr first_value_node( node_ptr first_node ) const {
while (first_node != nullptr && first_node->is_dummy()) {
first_node = first_node->next();
}
return static_cast<value_node_ptr>(first_node);
}
// Unsafe method, which removes the node from the list and returns the next node
node_ptr internal_erase( value_node_ptr node_to_erase ) {
__TBB_ASSERT(node_to_erase != nullptr, "Invalid iterator for erase");
node_ptr next_node = node_to_erase->next();
internal_extract(node_to_erase);
destroy_node(node_to_erase);
return next_node;
}
template <typename K>
size_type internal_erase_by_key( const K& key ) {
// TODO: consider reimplementation without equal_range - it is not effective to perform lookup over a bucket
// for each unsafe_erase call
auto eq_range = equal_range(key);
size_type erased_count = 0;
for (auto it = eq_range.first; it != eq_range.second;) {
it = unsafe_erase(it);
++erased_count;
}
return erased_count;
}
// Unsafe method, which extracts the node from the list
void internal_extract( value_node_ptr node_to_extract ) {
const key_type& key = traits_type::get_key(node_to_extract->value());
sokey_type hash_key = sokey_type(my_hash_compare(key));
node_ptr prev_node = prepare_bucket(hash_key);
for (node_ptr node = prev_node->next(); node != nullptr; prev_node = node, node = node->next()) {
if (node == node_to_extract) {
unlink_node(prev_node, node, node_to_extract->next());
my_size.store(my_size.load(std::memory_order_relaxed) - 1, std::memory_order_relaxed);
return;
}
__TBB_ASSERT(node->order_key() <= node_to_extract->order_key(),
"node, which is going to be extracted should be presented in the list");
}
}
protected:
template <typename SourceType>
void internal_merge( SourceType&& source ) {
static_assert(std::is_same<node_type, typename std::decay<SourceType>::type::node_type>::value,
"Incompatible containers cannot be merged");
for (node_ptr source_prev = &source.my_head; source_prev->next() != nullptr;) {
if (!source_prev->next()->is_dummy()) {
value_node_ptr curr = static_cast<value_node_ptr>(source_prev->next());
// If the multimapping is allowed, or the key is not presented
// in the *this container - extract the node from the list
if (allow_multimapping || !contains(traits_type::get_key(curr->value()))) {
node_ptr next_node = curr->next();
source.unlink_node(source_prev, curr, next_node);
// Remember the old order key
sokey_type old_order_key = curr->order_key();
// Node handle with curr cannot be used directly in insert call, because
// the destructor of node_type will destroy curr
node_type curr_node = d1::node_handle_accessor::construct<node_type>(curr);
// If the insertion fails - return ownership of the node to the source
if (!insert(std::move(curr_node)).second) {
__TBB_ASSERT(!allow_multimapping, "Insertion should succeed for multicontainer");
__TBB_ASSERT(source_prev->next() == next_node,
"Concurrent operations with the source container in merge are prohibited");
// Initialize the node with the old order key, because the order key
// can change during the insertion
curr->init(old_order_key);
__TBB_ASSERT(old_order_key >= source_prev->order_key() &&
(next_node == nullptr || old_order_key <= next_node->order_key()),
"Wrong nodes order in the source container");
// Merge is unsafe for source container, so the insertion back can be done without compare_exchange
curr->set_next(next_node);
source_prev->set_next(curr);
source_prev = curr;
d1::node_handle_accessor::deactivate(curr_node);
} else {
source.my_size.fetch_sub(1, std::memory_order_relaxed);
}
} else {
source_prev = curr;
}
} else {
source_prev = source_prev->next();
}
}
}
private:
// Unsafe method, which unlinks the node between prev and next
void unlink_node( node_ptr prev_node, node_ptr node_to_unlink, node_ptr next_node ) {
__TBB_ASSERT(prev_node->next() == node_to_unlink &&
node_to_unlink->next() == next_node,
"erasing and extracting nodes from the containers are unsafe in concurrent mode");
prev_node->set_next(next_node);
node_to_unlink->set_next(nullptr);
}
template <typename K>
value_node_ptr internal_find( const K& key ) {
sokey_type hash_key = sokey_type(my_hash_compare(key));
sokey_type order_key = split_order_key_regular(hash_key);
node_ptr curr = prepare_bucket(hash_key);
while (curr != nullptr) {
if (curr->order_key() > order_key) {
// If the order key is greater than the requested order key,
// the element is not in the hash table
return nullptr;
} else if (curr->order_key() == order_key &&
my_hash_compare(traits_type::get_key(static_cast<value_node_ptr>(curr)->value()), key)) {
// The fact that order keys match does not mean that the element is found.
// Key function comparison has to be performed to check whether this is the
// right element. If not, keep searching while order key is the same.
return static_cast<value_node_ptr>(curr);
}
curr = curr->next();
}
return nullptr;
}
template <typename K>
std::pair<value_node_ptr, value_node_ptr> internal_equal_range( const K& key ) {
sokey_type hash_key = sokey_type(my_hash_compare(key));
sokey_type order_key = split_order_key_regular(hash_key);
node_ptr curr = prepare_bucket(hash_key);
while (curr != nullptr) {
if (curr->order_key() > order_key) {
// If the order key is greater than the requested order key,
// the element is not in the hash table
return std::make_pair(nullptr, nullptr);
} else if (curr->order_key() == order_key &&
my_hash_compare(traits_type::get_key(static_cast<value_node_ptr>(curr)->value()), key)) {
value_node_ptr first = static_cast<value_node_ptr>(curr);
node_ptr last = first;
do {
last = last->next();
} while (allow_multimapping && last != nullptr && !last->is_dummy() &&
my_hash_compare(traits_type::get_key(static_cast<value_node_ptr>(last)->value()), key));
return std::make_pair(first, first_value_node(last));
}
curr = curr->next();
}
return {nullptr, nullptr};
}
template <typename K>
size_type internal_count( const K& key ) const {
if (allow_multimapping) {
// TODO: consider reimplementing the internal_equal_range with elements counting to avoid std::distance
auto eq_range = equal_range(key);
return std::distance(eq_range.first, eq_range.second);
} else {
return contains(key) ? 1 : 0;
}
}
void internal_copy( const concurrent_unordered_base& other ) {
node_ptr last_node = &my_head;
my_segments[0].store(&my_head, std::memory_order_relaxed);
for (node_ptr node = other.my_head.next(); node != nullptr; node = node->next()) {
node_ptr new_node;
if (!node->is_dummy()) {
// The node in the right table contains a value
new_node = create_node(node->order_key(), static_cast<value_node_ptr>(node)->value());
} else {
// The node in the right table is a dummy node
new_node = create_dummy_node(node->order_key());
my_segments[reverse_bits(node->order_key())].store(new_node, std::memory_order_relaxed);
}
last_node->set_next(new_node);
last_node = new_node;
}
}
void internal_move( concurrent_unordered_base&& other ) {
node_ptr last_node = &my_head;
my_segments[0].store(&my_head, std::memory_order_relaxed);
for (node_ptr node = other.my_head.next(); node != nullptr; node = node->next()) {
node_ptr new_node;
if (!node->is_dummy()) {
// The node in the right table contains a value
new_node = create_node(node->order_key(), std::move(static_cast<value_node_ptr>(node)->value()));
} else {
// TODO: do we need to destroy a dummy node in the right container?
// The node in the right table is a dummy_node
new_node = create_dummy_node(node->order_key());
my_segments[reverse_bits(node->order_key())].store(new_node, std::memory_order_relaxed);
}
last_node->set_next(new_node);
last_node = new_node;
}
}
void move_content( concurrent_unordered_base&& other ) {
// NOTE: allocators should be equal
my_head.set_next(other.my_head.next());
other.my_head.set_next(nullptr);
my_segments[0].store(&my_head, std::memory_order_relaxed);
other.my_bucket_count.store(initial_bucket_count, std::memory_order_relaxed);
other.my_max_load_factor = initial_max_load_factor;
other.my_size.store(0, std::memory_order_relaxed);
}
void internal_move_construct_with_allocator( concurrent_unordered_base&& other, const allocator_type&,
/*is_always_equal = */std::true_type ) {
// Allocators are always equal - no need to compare for equality
move_content(std::move(other));
}
void internal_move_construct_with_allocator( concurrent_unordered_base&& other, const allocator_type& alloc,
/*is_always_equal = */std::false_type ) {
// Allocators are not always equal
if (alloc == other.my_segments.get_allocator()) {
move_content(std::move(other));
} else {
try_call( [&] {
internal_move(std::move(other));
} ).on_exception( [&] {
clear();
});
}
}
// Move assigns the hash table to other is any instances of allocator_type are always equal
// or propagate_on_container_move_assignment is true
void internal_move_assign( concurrent_unordered_base&& other, /*is_always_equal || POCMA = */std::true_type ) {
move_content(std::move(other));
}
// Move assigns the hash table to other is any instances of allocator_type are not always equal
// and propagate_on_container_move_assignment is false
void internal_move_assign( concurrent_unordered_base&& other, /*is_always_equal || POCMA = */std::false_type ) {
if (my_segments.get_allocator() == other.my_segments.get_allocator()) {
move_content(std::move(other));
} else {
// TODO: guards for exceptions
internal_move(std::move(other));
}
}
void internal_swap( concurrent_unordered_base& other, /*is_always_equal || POCS = */std::true_type ) {
internal_swap_fields(other);
}
void internal_swap( concurrent_unordered_base& other, /*is_always_equal || POCS = */std::false_type ) {
__TBB_ASSERT(my_segments.get_allocator() == other.my_segments.get_allocator(),
"Swapping with unequal allocators is not allowed");
internal_swap_fields(other);
}
void internal_swap_fields( concurrent_unordered_base& other ) {
node_ptr first_node = my_head.next();
my_head.set_next(other.my_head.next());
other.my_head.set_next(first_node);
size_type current_size = my_size.load(std::memory_order_relaxed);
my_size.store(other.my_size.load(std::memory_order_relaxed), std::memory_order_relaxed);
other.my_size.store(current_size, std::memory_order_relaxed);
size_type bucket_count = my_bucket_count.load(std::memory_order_relaxed);
my_bucket_count.store(other.my_bucket_count.load(std::memory_order_relaxed), std::memory_order_relaxed);
other.my_bucket_count.store(bucket_count, std::memory_order_relaxed);
using std::swap;
swap(my_max_load_factor, other.my_max_load_factor);
swap(my_hash_compare, other.my_hash_compare);
my_segments.swap(other.my_segments);
// swap() method from segment table swaps all of the segments including the first segment
// We should restore it to my_head. Without it the first segment of the container will point
// to other.my_head.
my_segments[0].store(&my_head, std::memory_order_relaxed);
other.my_segments[0].store(&other.my_head, std::memory_order_relaxed);
}
// A regular order key has its original hash value reversed and the last bit set
static constexpr sokey_type split_order_key_regular( sokey_type hash ) {
return reverse_bits(hash) | 0x1;
}
// A dummy order key has its original hash value reversed and the last bit unset
static constexpr sokey_type split_order_key_dummy( sokey_type hash ) {
return reverse_bits(hash) & ~sokey_type(0x1);
}
size_type get_parent( size_type bucket ) const {
// Unset bucket's most significant turned-on bit
__TBB_ASSERT(bucket != 0, "Unable to get_parent of the bucket 0");
size_type msb = tbb::detail::log2(bucket);
return bucket & ~(size_type(1) << msb);
}
size_type get_next_bucket_index( size_type bucket ) const {
size_type bits = tbb::detail::log2(my_bucket_count.load(std::memory_order_relaxed));
size_type reversed_next = reverse_n_bits(bucket, bits) + 1;
return reverse_n_bits(reversed_next, bits);
}
std::atomic<size_type> my_size;
std::atomic<size_type> my_bucket_count;
float my_max_load_factor;
hash_compare_type my_hash_compare;
list_node_type my_head; // Head node for split ordered list
unordered_segment_table my_segments; // Segment table of pointers to nodes
template <typename Container, typename Value>
friend class solist_iterator;
template <typename OtherTraits>
friend class concurrent_unordered_base;
}; // class concurrent_unordered_base
template <typename Traits>
bool operator==( const concurrent_unordered_base<Traits>& lhs,
const concurrent_unordered_base<Traits>& rhs ) {
if (&lhs == &rhs) { return true; }
if (lhs.size() != rhs.size()) { return false; }
#if _MSC_VER
// Passing "unchecked" iterators to std::permutation with 3 parameters
// causes compiler warnings.
// The workaround is to use overload with 4 parameters, which is
// available since C++14 - minimally supported version on MSVC
return std::is_permutation(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
#else
return std::is_permutation(lhs.begin(), lhs.end(), rhs.begin());
#endif
}
#if !__TBB_CPP20_COMPARISONS_PRESENT
template <typename Traits>
bool operator!=( const concurrent_unordered_base<Traits>& lhs,
const concurrent_unordered_base<Traits>& rhs ) {
return !(lhs == rhs);
}
#endif
#if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
#pragma warning(pop) // warning 4127 is back
#endif
} // namespace d2
} // namespace detail
} // namespace tbb
#endif // __TBB_detail__concurrent_unordered_base_H
|