-
Notifications
You must be signed in to change notification settings - Fork 1k
/
_concurrent_unordered_base.h
1514 lines (1264 loc) · 63.6 KB
/
_concurrent_unordered_base.h
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
/*
Copyright (c) 2005-2023 Intel Corporation
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 d1 {
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 reinterpret_cast<value_type*>(&my_value);
}
value_type& value() {
return *storage();
}
private:
using aligned_storage_type = typename std::aligned_storage<sizeof(value_type)>::type;
aligned_storage_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 = 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 = 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");
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 node_handle_accessor::construct<node_type>(pos.get_node_ptr());
}
node_type unsafe_extract( iterator pos ) {
internal_extract(pos.get_node_ptr());
return 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 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 = 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 < 140000 ) && !__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 };
}