-
Notifications
You must be signed in to change notification settings - Fork 359
/
skip_list_nogc.h
969 lines (816 loc) · 34.7 KB
/
skip_list_nogc.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
// Copyright (c) 2006-2018 Maxim Khizhinsky
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef CDSLIB_INTRUSIVE_SKIP_LIST_NOGC_H
#define CDSLIB_INTRUSIVE_SKIP_LIST_NOGC_H
#include <type_traits>
#include <memory>
#include <cds/gc/nogc.h>
#include <cds/intrusive/details/skip_list_base.h>
#include <cds/opt/compare.h>
#include <cds/details/binary_functor_wrapper.h>
namespace cds { namespace intrusive {
//@cond
namespace skip_list {
template <typename Tag>
class node< cds::gc::nogc, Tag >
{
public:
typedef cds::gc::nogc gc; ///< Garbage collector
typedef Tag tag; ///< tag
typedef atomics::atomic<node * > atomic_ptr;
typedef atomic_ptr tower_item_type;
protected:
atomic_ptr m_pNext; ///< Next item in bottom-list (list at level 0)
unsigned int m_nHeight; ///< Node height (size of m_arrNext array). For node at level 0 the height is 1.
atomic_ptr * m_arrNext; ///< Array of next items for levels 1 .. m_nHeight - 1. For node at level 0 \p m_arrNext is \p nullptr
public:
/// Constructs a node of height 1 (a bottom-list node)
node()
: m_pNext( nullptr )
, m_nHeight(1)
, m_arrNext( nullptr )
{}
/// Constructs a node of height \p nHeight
void make_tower( unsigned int nHeight, atomic_ptr * nextTower )
{
assert( nHeight > 0 );
assert( (nHeight == 1 && nextTower == nullptr) // bottom-list node
|| (nHeight > 1 && nextTower != nullptr) // node at level of more than 0
);
m_arrNext = nextTower;
m_nHeight = nHeight;
}
atomic_ptr * release_tower()
{
atomic_ptr * pTower = m_arrNext;
m_arrNext = nullptr;
m_nHeight = 1;
return pTower;
}
atomic_ptr * get_tower() const
{
return m_arrNext;
}
/// Access to element of next pointer array
atomic_ptr& next( unsigned int nLevel )
{
assert( nLevel < height());
assert( nLevel == 0 || (nLevel > 0 && m_arrNext != nullptr));
return nLevel ? m_arrNext[ nLevel - 1] : m_pNext;
}
/// Access to element of next pointer array (const version)
atomic_ptr const& next( unsigned int nLevel ) const
{
assert( nLevel < height());
assert( nLevel == 0 || nLevel > 0 && m_arrNext != nullptr );
return nLevel ? m_arrNext[ nLevel - 1] : m_pNext;
}
/// Access to element of next pointer array (same as \ref next function)
atomic_ptr& operator[]( unsigned int nLevel )
{
return next( nLevel );
}
/// Access to element of next pointer array (same as \ref next function)
atomic_ptr const& operator[]( unsigned int nLevel ) const
{
return next( nLevel );
}
/// Height of the node
unsigned int height() const
{
return m_nHeight;
}
/// Clears internal links
void clear()
{
assert( m_arrNext == nullptr );
m_pNext.store( nullptr, atomics::memory_order_release );
}
bool is_cleared() const
{
return m_pNext.load( atomics::memory_order_relaxed ) == nullptr
&& m_arrNext == nullptr
&& m_nHeight <= 1
;
}
};
} // namespace skip_list
namespace skip_list { namespace details {
template <typename NodeTraits, typename BackOff, bool IsConst>
class iterator< cds::gc::nogc, NodeTraits, BackOff, IsConst>
{
public:
typedef cds::gc::nogc gc;
typedef NodeTraits node_traits;
typedef BackOff back_off;
typedef typename node_traits::node_type node_type;
typedef typename node_traits::value_type value_type;
static constexpr bool const c_isConst = IsConst;
typedef typename std::conditional< c_isConst, value_type const &, value_type &>::type value_ref;
friend class iterator< gc, node_traits, back_off, !c_isConst >;
protected:
typedef typename node_type::atomic_ptr atomic_ptr;
node_type * m_pNode;
public: // for internal use only!!!
iterator( node_type& refHead )
: m_pNode( refHead[0].load( atomics::memory_order_relaxed ))
{}
static iterator from_node( node_type * pNode )
{
iterator it;
it.m_pNode = pNode;
return it;
}
public:
iterator()
: m_pNode( nullptr )
{}
iterator( iterator const& s)
: m_pNode( s.m_pNode )
{}
value_type * operator ->() const
{
assert( m_pNode != nullptr );
assert( node_traits::to_value_ptr( m_pNode ) != nullptr );
return node_traits::to_value_ptr( m_pNode );
}
value_ref operator *() const
{
assert( m_pNode != nullptr );
assert( node_traits::to_value_ptr( m_pNode ) != nullptr );
return *node_traits::to_value_ptr( m_pNode );
}
/// Pre-increment
iterator& operator ++()
{
if ( m_pNode )
m_pNode = m_pNode->next(0).load( atomics::memory_order_relaxed );
return *this;
}
iterator& operator =(const iterator& src)
{
m_pNode = src.m_pNode;
return *this;
}
template <typename Bkoff, bool C>
bool operator ==(iterator<gc, node_traits, Bkoff, C> const& i ) const
{
return m_pNode == i.m_pNode;
}
template <typename Bkoff, bool C>
bool operator !=(iterator<gc, node_traits, Bkoff, C> const& i ) const
{
return !( *this == i );
}
};
}} // namespace skip_list::details
//@endcond
/// Lock-free skip-list set (template specialization for gc::nogc)
/** @ingroup cds_intrusive_map
@anchor cds_intrusive_SkipListSet_nogc
This specialization is so-called append-only when no item
reclamation may be performed. The class does not support deleting of list item.
See \ref cds_intrusive_SkipListSet_hp "SkipListSet" for description of skip-list.
<b>Template arguments</b> :
- \p T - type to be stored in the set. The type must be based on \p skip_list::node (for \p skip_list::base_hook)
or it must have a member of type \p skip_list::node (for \p skip_list::member_hook).
- \p Traits - type traits, default is \p skip_list::traits.
It is possible to declare option-based list with \p cds::intrusive::skip_list::make_traits metafunction
istead of \p Traits template argument.
<b>Iterators</b>
The class supports a forward iterator (\ref iterator and \ref const_iterator).
The iteration is ordered.
The iterator class supports the following minimalistic interface:
\code
struct iterator {
// Default ctor
iterator();
// Copy ctor
iterator( iterator const& s);
value_type * operator ->() const;
value_type& operator *() const;
// Pre-increment
iterator& operator ++();
// Copy assignment
iterator& operator = (const iterator& src);
bool operator ==(iterator const& i ) const;
bool operator !=(iterator const& i ) const;
};
\endcode
Note, the iterator object returned by \ref end, \p cend member functions points to \p nullptr and should not be dereferenced.
<b>How to use</b>
You should incorporate \p skip_list::node into your struct \p T and provide
appropriate \p skip_list::traits::hook in your \p Traits template parameters. Usually, for \p Traits you
define a struct based on \p skip_list::traits.
Example for base hook:
\code
#include <cds/intrusive/skip_list_nogc.h>
// Data stored in skip list
struct my_data: public cds::intrusive::skip_list::node< cds::gc::nogc >
{
// key field
std::string strKey;
// other data
// ...
};
// my_data compare functor
struct my_data_cmp {
int operator()( const my_data& d1, const my_data& d2 )
{
return d1.strKey.compare( d2.strKey );
}
int operator()( const my_data& d, const std::string& s )
{
return d.strKey.compare(s);
}
int operator()( const std::string& s, const my_data& d )
{
return s.compare( d.strKey );
}
};
// Declare traits
struct my_traits: public cds::intrusive::skip_list::traits
{
typedef cds::intrusive::skip_list::base_hook< cds::opt::gc< cds::gc::nogc > > hook;
typedef my_data_cmp compare;
};
// Declare skip-list set type
typedef cds::intrusive::SkipListSet< cds::gc::nogc, my_data, my_traits > traits_based_set;
\endcode
Equivalent option-based code:
\code
// GC-related specialization
#include <cds/intrusive/skip_list_nogc.h>
struct my_data {
// see above
};
struct compare {
// see above
};
// Declare option-based skip-list set
typedef cds::intrusive::SkipListSet< cds::gc::nogc
,my_data
, typename cds::intrusive::skip_list::make_traits<
cds::intrusive::opt::hook< cds::intrusive::skip_list::base_hook< cds::opt::gc< cds::gc::nogc > > >
,cds::intrusive::opt::compare< my_data_cmp >
>::type
> option_based_set;
\endcode
*/
template <
typename T
#ifdef CDS_DOXYGEN_INVOKED
,typename Traits = skip_list::traits
#else
,typename Traits
#endif
>
class SkipListSet< cds::gc::nogc, T, Traits >
{
public:
typedef cds::gc::nogc gc; ///< No garbage collector is used
typedef T value_type; ///< type of value stored in the skip-list
typedef Traits traits; ///< Traits template parameter
typedef typename traits::hook hook; ///< hook type
typedef typename hook::node_type node_type; ///< node type
# ifdef CDS_DOXYGEN_INVOKED
typedef implementation_defined key_comparator; ///< key comparison functor based on \p Traits::compare and \p Traits::less
# else
typedef typename opt::details::make_comparator< value_type, traits >::type key_comparator;
# endif
typedef typename get_node_traits< value_type, node_type, hook>::type node_traits; ///< node traits
typedef typename traits::item_counter item_counter; ///< Item counting policy
typedef typename traits::memory_model memory_model; ///< Memory ordering. See \p cds::opt::memory_model option
typedef typename traits::random_level_generator random_level_generator ; ///< random level generator
typedef typename traits::allocator allocator_type; ///< allocator for maintaining array of next pointers of the node
typedef typename traits::back_off back_off; ///< Back-off strategy
typedef typename traits::stat stat; ///< internal statistics type
typedef typename traits::disposer disposer; ///< disposer
/// Max node height. The actual node height should be in range <tt>[0 .. c_nMaxHeight)</tt>
/**
The max height is specified by \ref skip_list::random_level_generator "random level generator" constant \p m_nUpperBound
but it should be no more than 32 (\p skip_list::c_nHeightLimit).
*/
static unsigned int const c_nMaxHeight = std::conditional<
(random_level_generator::c_nUpperBound <= skip_list::c_nHeightLimit),
std::integral_constant< unsigned int, random_level_generator::c_nUpperBound >,
std::integral_constant< unsigned int, skip_list::c_nHeightLimit >
>::type::value;
//@cond
static unsigned int const c_nMinHeight = 3;
//@endcond
protected:
typedef typename node_type::atomic_ptr atomic_node_ptr; ///< Atomic node pointer
protected:
//@cond
typedef skip_list::details::intrusive_node_builder< node_type, atomic_node_ptr, allocator_type > intrusive_node_builder;
typedef typename std::conditional<
std::is_same< typename traits::internal_node_builder, cds::opt::none >::value
,intrusive_node_builder
,typename traits::internal_node_builder
>::type node_builder;
typedef std::unique_ptr< node_type, typename node_builder::node_disposer > scoped_node_ptr;
struct position {
node_type * pPrev[ c_nMaxHeight ];
node_type * pSucc[ c_nMaxHeight ];
node_type * pCur;
};
class head_node: public node_type
{
typename node_type::atomic_ptr m_Tower[c_nMaxHeight];
public:
head_node( unsigned int nHeight )
{
for ( size_t i = 0; i < sizeof(m_Tower) / sizeof(m_Tower[0]); ++i )
m_Tower[i].store( nullptr, atomics::memory_order_relaxed );
node_type::make_tower( nHeight, m_Tower );
}
node_type * head() const
{
return const_cast<node_type *>( static_cast<node_type const *>(this));
}
void clear()
{
for (unsigned int i = 0; i < sizeof(m_Tower) / sizeof(m_Tower[0]); ++i )
m_Tower[i].store( nullptr, atomics::memory_order_relaxed );
node_type::m_pNext.store( nullptr, atomics::memory_order_relaxed );
}
};
//@endcond
protected:
head_node m_Head; ///< head tower (max height)
random_level_generator m_RandomLevelGen; ///< random level generator instance
atomics::atomic<unsigned int> m_nHeight; ///< estimated high level
item_counter m_ItemCounter; ///< item counter
mutable stat m_Stat; ///< internal statistics
protected:
//@cond
unsigned int random_level()
{
// Random generator produces a number from range [0..31]
// We need a number from range [1..32]
return m_RandomLevelGen() + 1;
}
template <typename Q>
node_type * build_node( Q v )
{
return node_builder::make_tower( v, m_RandomLevelGen );
}
static void dispose_node( node_type * pNode )
{
assert( pNode != nullptr );
typename node_builder::node_disposer()( pNode );
disposer()( node_traits::to_value_ptr( pNode ));
}
template <typename Q, typename Compare >
bool find_position( Q const& val, position& pos, Compare cmp, bool bStopIfFound, bool bStrictSearch ) const
{
node_type * pPred;
node_type * pSucc;
node_type * pCur = nullptr;
int nCmp = 1;
unsigned int nHeight = c_nMaxHeight;
retry:
if ( !bStrictSearch )
nHeight = m_nHeight.load( memory_model::memory_order_relaxed );
pPred = m_Head.head();
for ( int nLevel = (int) nHeight - 1; nLevel >= 0; --nLevel ) {
while ( true ) {
pCur = pPred->next( nLevel ).load( memory_model::memory_order_relaxed );
if ( !pCur ) {
// end of the list at level nLevel - goto next level
break;
}
pSucc = pCur->next( nLevel ).load( memory_model::memory_order_relaxed );
if ( pPred->next( nLevel ).load( memory_model::memory_order_acquire ) != pCur
|| pCur->next( nLevel ).load( memory_model::memory_order_acquire ) != pSucc )
{
goto retry;
}
nCmp = cmp( *node_traits::to_value_ptr( pCur ), val );
if ( nCmp < 0 )
pPred = pCur;
else if ( nCmp == 0 && bStopIfFound )
goto found;
else
break;
}
pos.pPrev[ nLevel ] = pPred;
pos.pSucc[ nLevel ] = pCur;
}
if ( nCmp != 0 )
return false;
found:
pos.pCur = pCur;
return pCur && nCmp == 0;
}
template <typename Func>
bool insert_at_position( value_type& val, node_type * pNode, position& pos, Func f )
{
unsigned int nHeight = pNode->height();
for ( unsigned int nLevel = 1; nLevel < nHeight; ++nLevel )
pNode->next( nLevel ).store( nullptr, memory_model::memory_order_relaxed );
{
node_type * p = pos.pSucc[0];
pNode->next( 0 ).store( pos.pSucc[ 0 ], memory_model::memory_order_release );
if ( !pos.pPrev[0]->next(0).compare_exchange_strong( p, pNode, memory_model::memory_order_release, memory_model::memory_order_relaxed )) {
return false;
}
f( val );
}
for ( unsigned int nLevel = 1; nLevel < nHeight; ++nLevel ) {
node_type * p = nullptr;
while ( true ) {
node_type * q = pos.pSucc[ nLevel ];
if ( pNode->next( nLevel ).compare_exchange_strong( p, q, memory_model::memory_order_release, memory_model::memory_order_relaxed )) {
p = q;
if ( pos.pPrev[nLevel]->next(nLevel).compare_exchange_strong( q, pNode, memory_model::memory_order_release, memory_model::memory_order_relaxed ))
break;
}
// Renew insert position
find_position( val, pos, key_comparator(), false, true );
}
}
return true;
}
template <typename Q, typename Compare, typename Func>
node_type * find_with_( Q& val, Compare cmp, Func f ) const
{
position pos;
if ( find_position( val, pos, cmp, true, false )) {
assert( cmp( *node_traits::to_value_ptr( pos.pCur ), val ) == 0 );
f( *node_traits::to_value_ptr( pos.pCur ), val );
m_Stat.onFindFastSuccess();
return pos.pCur;
}
else {
m_Stat.onFindFastFailed();
return nullptr;
}
}
void increase_height( unsigned int nHeight )
{
unsigned int nCur = m_nHeight.load( memory_model::memory_order_relaxed );
while ( nCur < nHeight && !m_nHeight.compare_exchange_weak( nCur, nHeight, memory_model::memory_order_acquire, atomics::memory_order_relaxed ));
}
//@endcond
public:
/// Default constructor
/**
The constructor checks whether the count of guards is enough
for skip-list and may raise an exception if not.
*/
SkipListSet()
: m_Head( c_nMaxHeight )
, m_nHeight( c_nMinHeight )
{
static_assert( (std::is_same< gc, typename node_type::gc >::value), "GC and node_type::gc must be the same type" );
// Barrier for head node
atomics::atomic_thread_fence( memory_model::memory_order_release );
}
/// Clears and destructs the skip-list
~SkipListSet()
{
clear();
}
public:
///@name Forward iterators
//@{
/// Forward iterator
/**
The forward iterator for a split-list has some features:
- it has no post-increment operator
- it depends on iterator of underlying \p OrderedList
*/
typedef skip_list::details::iterator< gc, node_traits, back_off, false > iterator;
/// Const iterator type
typedef skip_list::details::iterator< gc, node_traits, back_off, true > const_iterator;
/// Returns a forward iterator addressing the first element in a set
iterator begin()
{
return iterator( *m_Head.head());
}
/// Returns a forward const iterator addressing the first element in a set
const_iterator begin() const
{
return const_iterator( *m_Head.head());
}
/// Returns a forward const iterator addressing the first element in a set
const_iterator cbegin() const
{
return const_iterator( *m_Head.head());
}
/// Returns a forward iterator that addresses the location succeeding the last element in a set.
iterator end()
{
return iterator();
}
/// Returns a forward const iterator that addresses the location succeeding the last element in a set.
const_iterator end() const
{
return const_iterator();
}
/// Returns a forward const iterator that addresses the location succeeding the last element in a set.
const_iterator cend() const
{
return const_iterator();
}
//@}
protected:
//@cond
iterator nonconst_end() const
{
return iterator();
}
//@endcond
public:
/// Inserts new node
/**
The function inserts \p val in the set if it does not contain
an item with key equal to \p val.
Returns \p true if \p val is placed into the set, \p false otherwise.
*/
bool insert( value_type& val )
{
node_type * pNode = node_traits::to_node_ptr( val );
scoped_node_ptr scp( pNode );
unsigned int nHeight = pNode->height();
bool bTowerOk = nHeight > 1 && pNode->get_tower() != nullptr;
bool bTowerMade = false;
position pos;
while ( true )
{
bool bFound = find_position( val, pos, key_comparator(), true, true );
if ( bFound ) {
// scoped_node_ptr deletes the node tower if we create it
if ( !bTowerMade )
scp.release();
m_Stat.onInsertFailed();
return false;
}
if ( !bTowerOk ) {
build_node( pNode );
nHeight = pNode->height();
bTowerMade =
bTowerOk = true;
}
if ( !insert_at_position( val, pNode, pos, []( value_type& ) {} )) {
m_Stat.onInsertRetry();
continue;
}
increase_height( nHeight );
++m_ItemCounter;
m_Stat.onAddNode( nHeight );
m_Stat.onInsertSuccess();
scp.release();
return true;
}
}
/// Updates the node
/**
The operation performs inserting or changing data with lock-free manner.
If the item \p val is not found in the set, then \p val is inserted into the set
iff \p bInsert is \p true.
Otherwise, the functor \p func is called with item found.
The functor signature is:
\code
void func( bool bNew, value_type& item, value_type& val );
\endcode
with arguments:
- \p bNew - \p true if the item has been inserted, \p false otherwise
- \p item - item of the set
- \p val - argument \p val passed into the \p %update() function
If new item has been inserted (i.e. \p bNew is \p true) then \p item and \p val arguments
refer to the same thing.
The functor can change non-key fields of the \p item; however, \p func must guarantee
that during changing no any other modifications could be made on this item by concurrent threads.
Returns std::pair<bool, bool> where \p first is \p true if operation is successful,
\p second is \p true if new item has been added or \p false if the item with \p key
already is in the set.
@warning See \ref cds_intrusive_item_creating "insert item troubleshooting"
*/
template <typename Func>
std::pair<bool, bool> update( value_type& val, Func func, bool bInsert = true )
{
node_type * pNode = node_traits::to_node_ptr( val );
scoped_node_ptr scp( pNode );
unsigned int nHeight = pNode->height();
bool bTowerOk = nHeight > 1 && pNode->get_tower() != nullptr;
bool bTowerMade = false;
position pos;
while ( true )
{
bool bFound = find_position( val, pos, key_comparator(), true, true );
if ( bFound ) {
// scoped_node_ptr deletes the node tower if we create it before
if ( !bTowerMade )
scp.release();
func( false, *node_traits::to_value_ptr(pos.pCur), val );
m_Stat.onUpdateExist();
return std::make_pair( true, false );
}
if ( !bInsert ) {
scp.release();
return std::make_pair( false, false );
}
if ( !bTowerOk ) {
build_node( pNode );
nHeight = pNode->height();
bTowerMade =
bTowerOk = true;
}
if ( !insert_at_position( val, pNode, pos, [&func]( value_type& item ) { func( true, item, item ); })) {
m_Stat.onInsertRetry();
continue;
}
increase_height( nHeight );
++m_ItemCounter;
scp.release();
m_Stat.onAddNode( nHeight );
m_Stat.onUpdateNew();
return std::make_pair( true, true );
}
}
//@cond
template <typename Func>
CDS_DEPRECATED("ensure() is deprecated, use update()")
std::pair<bool, bool> ensure( value_type& val, Func func )
{
return update( val, func, true );
}
//@endcond
/// Finds \p key
/** \anchor cds_intrusive_SkipListSet_nogc_find_func
The function searches the item with key equal to \p key and calls the functor \p f for item found.
The interface of \p Func functor is:
\code
struct functor {
void operator()( value_type& item, Q& key );
};
\endcode
where \p item is the item found, \p key is the <tt>find</tt> function argument.
The functor can change non-key fields of \p item. Note that the functor is only guarantee
that \p item cannot be disposed during functor is executing.
The functor does not serialize simultaneous access to the set \p item. If such access is
possible you must provide your own synchronization schema on item level to exclude unsafe item modifications.
The \p key argument is non-const since it can be used as \p f functor destination i.e., the functor
can modify both arguments.
Note the hash functor specified for class \p Traits template parameter
should accept a parameter of type \p Q that can be not the same as \p value_type.
The function returns \p true if \p key is found, \p false otherwise.
*/
template <typename Q, typename Func>
bool find( Q& key, Func f ) const
{
return find_with_( key, key_comparator(), f ) != nullptr;
}
//@cond
template <typename Q, typename Func>
bool find( Q const& key, Func f ) const
{
return find_with_( key, key_comparator(), f ) != nullptr;
}
//@endcond
/// Finds the key \p key using \p pred predicate for comparing
/**
The function is an analog of \ref cds_intrusive_SkipListSet_nogc_find_func "find(Q&, Func)"
but \p pred predicate is used for key compare.
\p Less has the interface like \p std::less.
\p pred must imply the same element order as the comparator used for building the set.
*/
template <typename Q, typename Less, typename Func>
bool find_with( Q& key, Less pred, Func f ) const
{
CDS_UNUSED( pred );
return find_with_( key, cds::opt::details::make_comparator_from_less<Less>(), f ) != nullptr;
}
//@cond
template <typename Q, typename Less, typename Func>
bool find_with( Q const& key, Less pred, Func f ) const
{
CDS_UNUSED( pred );
return find_with_( key, cds::opt::details::make_comparator_from_less<Less>(), f ) != nullptr;
}
//@endcond
/// Checks whether the set contains \p key
/**
The function searches the item with key equal to \p key
and returns pointer to item found or \p nullptr.
*/
template <typename Q>
value_type * contains( Q const& key ) const
{
node_type * pNode = find_with_( key, key_comparator(), [](value_type& , Q const& ) {} );
if ( pNode )
return node_traits::to_value_ptr( pNode );
return nullptr;
}
//@cond
template <typename Q>
CDS_DEPRECATED("deprecated, use contains()")
value_type * find( Q const& key ) const
{
return contains( key );
}
//@endcond
/// Checks whether the set contains \p key using \p pred predicate for searching
/**
The function is similar to <tt>contains( key )</tt> but \p pred is used for key comparing.
\p Less functor has the interface like \p std::less.
\p Less must imply the same element order as the comparator used for building the set.
*/
template <typename Q, typename Less>
value_type * contains( Q const& key, Less pred ) const
{
CDS_UNUSED( pred );
node_type * pNode = find_with_( key, cds::opt::details::make_comparator_from_less<Less>(), [](value_type& , Q const& ) {} );
if ( pNode )
return node_traits::to_value_ptr( pNode );
return nullptr;
}
//@cond
template <typename Q, typename Less>
CDS_DEPRECATED("deprecated, use contains()")
value_type * find_with( Q const& key, Less pred ) const
{
return contains( key, pred );
}
//@endcond
/// Gets minimum key from the set
/**
If the set is empty the function returns \p nullptr
*/
value_type * get_min() const
{
return node_traits::to_value_ptr( m_Head.head()->next( 0 ));
}
/// Gets maximum key from the set
/**
The function returns \p nullptr if the set is empty
*/
value_type * get_max() const
{
node_type * pPred;
unsigned int nHeight = m_nHeight.load( memory_model::memory_order_relaxed );
pPred = m_Head.head();
for ( int nLevel = (int) nHeight - 1; nLevel >= 0; --nLevel ) {
while ( true ) {
node_type * pCur = pPred->next( nLevel ).load( memory_model::memory_order_relaxed );
if ( !pCur ) {
// end of the list at level nLevel - goto next level
break;
}
pPred = pCur;
}
}
return pPred && pPred != m_Head.head() ? node_traits::to_value_ptr( pPred ) : nullptr;
}
/// Clears the set (non-atomic)
/**
The function is not atomic.
Finding and/or inserting is prohibited while clearing.
Otherwise an unpredictable result may be encountered.
Thus, \p clear() may be used only for debugging purposes.
*/
void clear()
{
node_type * pNode = m_Head.head()->next(0).load( memory_model::memory_order_relaxed );
m_Head.clear();
m_ItemCounter.reset();
m_nHeight.store( c_nMinHeight, memory_model::memory_order_release );
while ( pNode ) {
node_type * pNext = pNode->next(0).load( memory_model::memory_order_relaxed );
dispose_node( pNode );
pNode = pNext;
}
}
/// Returns item count in the set
/**
The value returned depends on item counter type provided by \p Traits template parameter.
For \p atomicity::empty_item_counter the function always returns 0.
The function is not suitable for checking the set emptiness, use \p empty().
*/
size_t size() const
{
return m_ItemCounter;
}
/// Checks if the set is empty
bool empty() const
{
return m_Head.head()->next( 0 ).load( memory_model::memory_order_relaxed ) == nullptr;
}
/// Returns maximum height of skip-list. The max height is a constant for each object and does not exceed 32.
static constexpr unsigned int max_height() noexcept
{
return c_nMaxHeight;
}
/// Returns const reference to internal statistics
stat const& statistics() const
{
return m_Stat;
}
};
}} // namespace cds::intrusive
#endif // #ifndef CDSLIB_INTRUSIVE_SKIP_LIST_IMPL_H