intrusive::SplitListSet refactoring
[libcds.git] / cds / intrusive / details / split_list_base.h
1 //$$CDS-header$$
2
3 #ifndef __CDS_INTRUSIVE_DETAILS_SPLIT_LIST_BASE_H
4 #define __CDS_INTRUSIVE_DETAILS_SPLIT_LIST_BASE_H
5
6 #include <cds/intrusive/details/base.h>
7 #include <cds/cxx11_atomic.h>
8 #include <cds/details/allocator.h>
9 #include <cds/algo/int_algo.h>
10 #include <cds/algo/bitop.h>
11
12 namespace cds { namespace intrusive {
13
14     /// Split-ordered list related definitions
15     /** @ingroup cds_intrusive_helper
16     */
17     namespace split_list {
18         /// Split-ordered list node
19         /**
20             Template parameter:
21             - OrderedListNode - node type for underlying ordered list
22         */
23         template <typename OrderedListNode>
24         struct node: public OrderedListNode
25         {
26             //@cond
27             typedef OrderedListNode base_class;
28             //@endcond
29
30             size_t  m_nHash ;   ///< Hash value for node
31
32             /// Default constructor
33             node()
34                 : m_nHash(0)
35             {
36                 assert( is_dummy() );
37             }
38
39             /// Initializes dummy node with \p nHash value
40             node( size_t nHash )
41                 : m_nHash( nHash )
42             {
43                 assert( is_dummy() );
44             }
45
46             /// Checks if the node is dummy node
47             bool is_dummy() const
48             {
49                 return (m_nHash & 1) == 0;
50             }
51         };
52
53         /// SplitListSet internal statistics. May be used for debugging or profiling
54         /**
55             Template argument \p Counter defines type of counter.
56             Default is \p cds::atomicity::event_counter, that is weak, i.e. it is not guaranteed
57             strict event counting.
58             You may use stronger type of counter like as \p cds::atomicity::item_counter,
59             or even integral type, for example, \p int.
60         */
61         template <typename Counter = cds::atomicity::event_counter >
62         struct stat
63         {
64             typedef Counter     counter_type;   ///< Counter type
65
66             counter_type    m_nInsertSuccess;        ///< Count of success inserting
67             counter_type    m_nInsertFailed;         ///< Count of failed inserting
68             counter_type    m_nEnsureNew;            ///< Count of new item created by \p ensure() member function
69             counter_type    m_nEnsureExist;          ///< Count of \p ensure() call for existing item
70             counter_type    m_nEraseSuccess;         ///< Count of success erasing of items
71             counter_type    m_nEraseFailed;          ///< Count of attempts to erase unknown item
72             counter_type    m_nExtractSuccess;       ///< Count of success extracting of items
73             counter_type    m_nExtractFailed;        ///< Count of attempts to extract unknown item
74             counter_type    m_nFindSuccess;          ///< Count of success finding
75             counter_type    m_nFindFailed;           ///< Count of failed finding
76             counter_type    m_nHeadNodeAllocated;    ///< Count of allocated head node
77             counter_type    m_nHeadNodeFreed;        ///< Count of freed head node
78             counter_type    m_nBucketCount;          ///< Current bucket count
79             counter_type    m_nInitBucketRecursive;  ///< Count of recursive bucket initialization
80             counter_type    m_nInitBucketContention; ///< Count of bucket init contention encountered
81             counter_type    m_nBusyWaitBucketInit;   ///< Count of busy wait cycle while a bucket is initialized
82
83             //@cond
84             void onInsertSuccess()       { ++m_nInsertSuccess; }
85             void onInsertFailed()        { ++m_nInsertFailed; }
86             void onEnsureNew()           { ++m_nEnsureNew; }
87             void onEnsureExist()         { ++m_nEnsureExist; }
88             void onEraseSuccess()        { ++m_nEraseSuccess; }
89             void onEraseFailed()         { ++m_nEraseFailed; }
90             void onExtractSuccess()      { ++m_nExtractSuccess; }
91             void onExtractFailed()       { ++m_nExtractFailed; }
92             void onFindSuccess()         { ++m_nFindSuccess; }
93             void onFindFailed()          { ++m_nFindFailed; }
94             bool onFind(bool bSuccess) 
95             { 
96                 if ( bSuccess )
97                     onFindSuccess();
98                 else
99                     onFindFailed();
100                 return bSuccess;
101             }
102             void onHeadNodeAllocated()   { ++m_nHeadNodeAllocated; }
103             void onHeadNodeFreed()       { ++m_nHeadNodeFreed; }
104             void onNewBucket()           { ++m_nBucketCount; }
105             void onRecursiveInitBucket() { ++m_nInitBucketRecursive; }
106             void onBucketInitContenton() { ++m_nInitBucketContention; }
107             void onBusyWaitBucketInit()  { ++m_nBusyWaitBucketInit; }
108             //@endcond
109         };
110
111         /// Dummy queue statistics - no counting is performed, no overhead. Support interface like \p split_list::stat
112         struct empty_stat {
113             //@cond
114             void onInsertSuccess()       const {}
115             void onInsertFailed()        const {}
116             void onEnsureNew()           const {}
117             void onEnsureExist()         const {}
118             void onEraseSuccess()        const {}
119             void onEraseFailed()         const {}
120             void onExtractSuccess()      const {}
121             void onExtractFailed()       const {}
122             void onFindSuccess()         const {}
123             void onFindFailed()          const {}
124             bool onFind( bool bSuccess ) const { return bSuccess; }
125             void onHeadNodeAllocated()   const {}
126             void onHeadNodeFreed()       const {}
127             void onNewBucket()           const {}
128             void onRecursiveInitBucket() const {}
129             void onBucketInitContenton() const {}
130             void onBusyWaitBucketInit()  const {}
131             //@endcond
132         };
133
134         /// SplitListSet traits
135         struct traits
136         {
137             /// Hash function
138             /**
139                 Hash function converts the key fields of struct \p T stored in the split list
140                 into hash value of type \p size_t that is an index in hash table.
141
142                 Hash typedef is mandatory and has no predefined one.
143             */
144             typedef opt::none       hash;
145
146             /// Item counter
147             /**
148                 The item counting is an important part of \p SplitListSet algorithm:
149                 the <tt>empty()</tt> member function depends on correct item counting.
150                 Therefore, \p cds::atomicity::empty_item_counter is not allowed as a type of the option.
151
152                 Default is \p cds::atomicity::item_counter.
153             */
154             typedef cds::atomicity::item_counter item_counter;
155
156             /// Bucket table allocator
157             /**
158                 Allocator for bucket table. Default is \ref CDS_DEFAULT_ALLOCATOR
159             */
160             typedef CDS_DEFAULT_ALLOCATOR   allocator;
161
162             /// Internal statistics (by default, disabled)
163             /**
164                 Possible statistics types are: \p split_list::stat (enable internal statistics), 
165                 \p split_list::empty_stat (the default, internal statistics disabled),
166                 user-provided class that supports \p %split_list::stat interface.
167             */
168             typedef split_list::empty_stat  stat;
169
170
171             /// C++ memory ordering model
172             /** 
173                 Can be \p opt::v::relaxed_ordering (relaxed memory model, the default)
174                 or \p opt::v::sequential_consistent (sequentially consisnent memory model).
175             */
176             typedef opt::v::relaxed_ordering memory_model;
177
178             /// What type of bucket table is used
179             /**
180                 \p true - use \p split_list::expandable_bucket_table that can be expanded
181                     if the load factor of the set is exhausted.
182                 \p false - use \p split_list::static_bucket_table that cannot be expanded
183                     and is allocated in \p SplitListSet constructor.
184
185                 Default is \p true.
186             */
187             static const bool dynamic_bucket_table = true;
188
189             /// Back-off strategy
190             typedef cds::backoff::Default back_off;
191         };
192
193         /// [value-option] Split-list dynamic bucket table option
194         /**
195             The option is used to select bucket table implementation.
196             Possible values of \p Value are:
197             - \p true - select \p expandable_bucket_table
198             - \p false - select \p static_bucket_table
199         */
200         template <bool Value>
201         struct dynamic_bucket_table
202         {
203             //@cond
204             template <typename Base> struct pack: public Base
205             {
206                 enum { dynamic_bucket_table = Value };
207             };
208             //@endcond
209         };
210
211         /// Metafunction converting option list to \p split_list::traits
212         /**
213             Available \p Options:
214             - \p opt::hash - mandatory option, specifies hash functor.
215             - \p opt::item_counter - optional, specifies item counting policy. See \p traits::item_counter
216                 for default type.
217             - \p opt::memory_model - C++ memory model for atomic operations.
218                 Can be \p opt::v::relaxed_ordering (relaxed memory model, the default) 
219                 or \p opt::v::sequential_consistent (sequentially consisnent memory model).
220             - \p opt::allocator - optional, bucket table allocator. Default is \ref CDS_DEFAULT_ALLOCATOR.
221             - \p split_list::dynamic_bucket_table - use dynamic or static bucket table implementation.
222                 Dynamic bucket table expands its size up to maximum bucket count when necessary
223             - \p opt::back_off - back-off strategy used for spinning, defult is \p cds::backoff::Default.
224         */
225         template <typename... Options>
226         struct make_traits {
227             typedef typename cds::opt::make_options< traits, Options...>::type type  ;   ///< Result of metafunction
228         };
229
230         /// Static bucket table
231         /**
232             Non-resizeable bucket table for \p SplitListSet class.
233             The capacity of table (max bucket count) is defined in the constructor call.
234
235             Template parameter:
236             - \p GC - garbage collector
237             - \p Node - node type, must be a type based on \p split_list::node
238             - \p Options... - options
239
240             \p Options are:
241             - \p opt::allocator - allocator used to allocate bucket table. Default is \ref CDS_DEFAULT_ALLOCATOR
242             - \p opt::memory_model - memory model used. Possible types are \p opt::v::sequential_consistent, \p opt::v::relaxed_ordering
243         */
244         template <typename GC, typename Node, typename... Options>
245         class static_bucket_table
246         {
247             //@cond
248             struct default_options
249             {
250                 typedef CDS_DEFAULT_ALLOCATOR       allocator;
251                 typedef opt::v::relaxed_ordering    memory_model;
252             };
253             typedef typename opt::make_options< default_options, Options... >::type options;
254             //@endcond
255
256         public:
257             typedef GC      gc;         ///< Garbage collector
258             typedef Node    node_type;  ///< Bucket node type
259             typedef atomics::atomic<node_type *> table_entry;  ///< Table entry type
260
261             /// Bucket table allocator
262             typedef cds::details::Allocator< table_entry, typename options::allocator >  bucket_table_allocator;
263
264             /// Memory model for atomic operations
265             typedef typename options::memory_model  memory_model;
266
267         protected:
268             const size_t   m_nLoadFactor; ///< load factor (average count of items per bucket)
269             const size_t   m_nCapacity;   ///< Bucket table capacity
270             table_entry *  m_Table;       ///< Bucket table
271
272         protected:
273             //@cond
274             void allocate_table()
275             {
276                 m_Table = bucket_table_allocator().NewArray( m_nCapacity, nullptr );
277             }
278
279             void destroy_table()
280             {
281                 bucket_table_allocator().Delete( m_Table, m_nCapacity );
282             }
283             //@endcond
284
285         public:
286             /// Constructs bucket table for 512K buckets. Load factor is 1.
287             static_bucket_table()
288                 : m_nLoadFactor(1)
289                 , m_nCapacity( 512 * 1024 )
290             {
291                 allocate_table();
292             }
293
294             /// Creates the table with specified size rounded up to nearest power-of-two
295             static_bucket_table(
296                 size_t nItemCount,        ///< Max expected item count in split-ordered list
297                 size_t nLoadFactor        ///< Load factor
298                 )
299                 : m_nLoadFactor( nLoadFactor > 0 ? nLoadFactor : (size_t) 1 ),
300                 m_nCapacity( cds::beans::ceil2( nItemCount / m_nLoadFactor ) )
301             {
302                 // m_nCapacity must be power of 2
303                 assert( cds::beans::is_power2( m_nCapacity ) );
304                 allocate_table();
305             }
306
307             /// Destroys bucket table
308             ~static_bucket_table()
309             {
310                 destroy_table();
311             }
312
313             /// Returns head node of bucket \p nBucket
314             node_type * bucket( size_t nBucket ) const
315             {
316                 assert( nBucket < capacity() );
317                 return m_Table[ nBucket ].load(memory_model::memory_order_acquire);
318             }
319
320             /// Set \p pNode as a head of bucket \p nBucket
321             void bucket( size_t nBucket, node_type * pNode )
322             {
323                 assert( nBucket < capacity() );
324                 assert( bucket( nBucket ) == nullptr );
325
326                 m_Table[ nBucket ].store( pNode, memory_model::memory_order_release );
327             }
328
329             /// Returns the capacity of the bucket table
330             size_t capacity() const
331             {
332                 return m_nCapacity;
333             }
334
335             /// Returns the load factor, i.e. average count of items per bucket
336             size_t load_factor() const
337             {
338                 return m_nLoadFactor;
339             }
340         };
341
342         /// Expandable bucket table
343         /**
344             This bucket table can dynamically grow its capacity when necessary
345             up to maximum bucket count.
346
347             Template parameter:
348             - \p GC - garbage collector
349             - \p Node - node type, must be derived from \p split_list::node
350             - \p Options... - options
351
352             \p Options are:
353             - \p opt::allocator - allocator used to allocate bucket table. Default is \ref CDS_DEFAULT_ALLOCATOR
354             - \p opt::memory_model - memory model used. Possible types are \p opt::v::sequential_consistent, \p opt::v::relaxed_ordering
355         */
356         template <typename GC, typename Node, typename... Options>
357         class expandable_bucket_table
358         {
359             //@cond
360             struct default_options
361             {
362                 typedef CDS_DEFAULT_ALLOCATOR       allocator;
363                 typedef opt::v::relaxed_ordering    memory_model;
364             };
365             typedef typename opt::make_options< default_options, Options... >::type options;
366             //@endcond
367         public:
368             typedef GC      gc;        ///< Garbage collector
369             typedef Node    node_type; ///< Bucket node type
370             typedef atomics::atomic<node_type *> table_entry; ///< Table entry type
371
372             /// Memory model for atomic operations
373             typedef typename options::memory_model memory_model;
374
375         protected:
376             typedef atomics::atomic<table_entry *>   segment_type; ///< Bucket table segment type
377
378         public:
379             /// Bucket table allocator
380             typedef cds::details::Allocator< segment_type, typename options::allocator >  bucket_table_allocator;
381
382             /// Bucket table segment allocator
383             typedef cds::details::Allocator< table_entry, typename options::allocator >  segment_allocator;
384
385         protected:
386             /// Bucket table metrics
387             struct metrics {
388                 size_t    nSegmentCount;    ///< max count of segments in bucket table
389                 size_t    nSegmentSize;     ///< the segment's capacity. The capacity must be power of two.
390                 size_t    nSegmentSizeLog2; ///< <tt> log2( m_nSegmentSize )</tt>
391                 size_t    nLoadFactor;      ///< load factor
392                 size_t    nCapacity;        ///< max capacity of bucket table
393
394                 //@cond
395                 metrics()
396                     : nSegmentCount(1024)
397                     , nSegmentSize(512)
398                     , nSegmentSizeLog2( cds::beans::log2( nSegmentSize ) )
399                     , nLoadFactor(1)
400                     , nCapacity( nSegmentCount * nSegmentSize )
401                 {}
402                 //@endcond
403             };
404             const metrics   m_metrics; ///< Dynamic bucket table metrics
405
406         protected:
407             segment_type * m_Segments; ///< bucket table - array of segments
408
409         protected:
410             //@cond
411             metrics calc_metrics( size_t nItemCount, size_t nLoadFactor )
412             {
413                 metrics m;
414
415                 // Calculate m_nSegmentSize and m_nSegmentCount  by nItemCount
416                 m.nLoadFactor = nLoadFactor > 0 ? nLoadFactor : 1;
417
418                 size_t nBucketCount = (size_t)( ((float) nItemCount) / m.nLoadFactor );
419                 if ( nBucketCount <= 2 ) {
420                     m.nSegmentCount = 1;
421                     m.nSegmentSize = 2;
422                 }
423                 else if ( nBucketCount <= 1024 ) {
424                     m.nSegmentCount = 1;
425                     m.nSegmentSize = ((size_t) 1) << beans::log2ceil( nBucketCount );
426                 }
427                 else {
428                     nBucketCount = beans::log2ceil( nBucketCount );
429                     m.nSegmentCount =
430                         m.nSegmentSize = ((size_t) 1) << ( nBucketCount / 2 );
431                     if ( nBucketCount & 1 )
432                         m.nSegmentSize *= 2;
433                     if ( m.nSegmentCount * m.nSegmentSize * m.nLoadFactor < nItemCount )
434                         m.nSegmentSize *= 2;
435                 }
436                 m.nCapacity = m.nSegmentCount * m.nSegmentSize;
437                 m.nSegmentSizeLog2 = cds::beans::log2( m.nSegmentSize );
438                 assert( m.nSegmentSizeLog2 != 0 )   ;   //
439                 return m;
440             }
441
442             segment_type * allocate_table()
443             {
444                 return bucket_table_allocator().NewArray( m_metrics.nSegmentCount, nullptr );
445             }
446
447             void destroy_table( segment_type * pTable )
448             {
449                 bucket_table_allocator().Delete( pTable, m_metrics.nSegmentCount );
450             }
451
452             table_entry * allocate_segment()
453             {
454                 return segment_allocator().NewArray( m_metrics.nSegmentSize, nullptr );
455             }
456
457             void destroy_segment( table_entry * pSegment )
458             {
459                 segment_allocator().Delete( pSegment, m_metrics.nSegmentSize );
460             }
461
462             void init_segments()
463             {
464                 // m_nSegmentSize must be 2**N
465                 assert( cds::beans::is_power2( m_metrics.nSegmentSize ));
466                 assert( ( ((size_t) 1) << m_metrics.nSegmentSizeLog2) == m_metrics.nSegmentSize );
467
468                 // m_nSegmentCount must be 2**K
469                 assert( cds::beans::is_power2( m_metrics.nSegmentCount ));
470
471                 m_Segments = allocate_table();
472             }
473
474             //@endcond
475
476         public:
477             /// Constructs bucket table for 512K buckets. Load factor is 1.
478             expandable_bucket_table()
479                 : m_metrics( calc_metrics( 512 * 1024, 1 ))
480             {
481                 init_segments();
482             }
483
484             /// Creates the table with specified capacity rounded up to nearest power-of-two
485             expandable_bucket_table(
486                 size_t nItemCount,        ///< Max expected item count in split-ordered list
487                 size_t nLoadFactor        ///< Load factor
488                 )
489                 : m_metrics( calc_metrics( nItemCount, nLoadFactor ))
490             {
491                 init_segments();
492             }
493
494             /// Destroys bucket table
495             ~expandable_bucket_table()
496             {
497                 segment_type * pSegments = m_Segments;
498                 for ( size_t i = 0; i < m_metrics.nSegmentCount; ++i ) {
499                     table_entry * pEntry = pSegments[i].load(memory_model::memory_order_relaxed);
500                     if ( pEntry != nullptr )
501                         destroy_segment( pEntry );
502                 }
503                 destroy_table( pSegments );
504             }
505
506             /// Returns head node of the bucket \p nBucket
507             node_type * bucket( size_t nBucket ) const
508             {
509                 size_t nSegment = nBucket >> m_metrics.nSegmentSizeLog2;
510                 assert( nSegment < m_metrics.nSegmentCount );
511
512                 table_entry * pSegment = m_Segments[ nSegment ].load(memory_model::memory_order_acquire);
513                 if ( pSegment == nullptr )
514                     return nullptr;    // uninitialized bucket
515                 return pSegment[ nBucket & (m_metrics.nSegmentSize - 1) ].load(memory_model::memory_order_acquire);
516             }
517
518             /// Set \p pNode as a head of bucket \p nBucket
519             void bucket( size_t nBucket, node_type * pNode )
520             {
521                 size_t nSegment = nBucket >> m_metrics.nSegmentSizeLog2;
522                 assert( nSegment < m_metrics.nSegmentCount );
523
524                 segment_type& segment = m_Segments[nSegment];
525                 if ( segment.load( memory_model::memory_order_relaxed ) == nullptr ) {
526                     table_entry * pNewSegment = allocate_segment();
527                     table_entry * pNull = nullptr;
528                     if ( !segment.compare_exchange_strong( pNull, pNewSegment, memory_model::memory_order_release, atomics::memory_order_relaxed )) {
529                         destroy_segment( pNewSegment );
530                     }
531                 }
532                 segment.load(memory_model::memory_order_acquire)[ nBucket & (m_metrics.nSegmentSize - 1) ].store( pNode, memory_model::memory_order_release );
533             }
534
535             /// Returns the capacity of the bucket table
536             size_t capacity() const
537             {
538                 return m_metrics.nCapacity;
539             }
540
541             /// Returns the load factor, i.e. average count of items per bucket
542             size_t load_factor() const
543             {
544                 return m_metrics.nLoadFactor;
545             }
546         };
547
548         /// Split-list node traits
549         /**
550             This traits is intended for converting between underlying ordered list node type
551             and split-list node type
552
553             Template parameter:
554             - \p BaseNodeTraits - node traits of base ordered list type
555         */
556         template <class BaseNodeTraits>
557         struct node_traits: private BaseNodeTraits
558         {
559             typedef BaseNodeTraits base_class;     ///< Base ordered list node type
560             typedef typename base_class::value_type value_type;     ///< Value type
561             typedef typename base_class::node_type  base_node_type; ///< Ordered list node type
562             typedef node<base_node_type>            node_type;      ///< Spit-list node type
563
564             /// Convert value reference to node pointer
565             static node_type * to_node_ptr( value_type& v )
566             {
567                 return static_cast<node_type *>( base_class::to_node_ptr( v ) );
568             }
569
570             /// Convert value pointer to node pointer
571             static node_type * to_node_ptr( value_type * v )
572             {
573                 return static_cast<node_type *>( base_class::to_node_ptr( v ) );
574             }
575
576             /// Convert value reference to node pointer (const version)
577             static node_type const * to_node_ptr( value_type const& v )
578             {
579                 return static_cast<node_type const*>( base_class::to_node_ptr( v ) );
580             }
581
582             /// Convert value pointer to node pointer (const version)
583             static node_type const * to_node_ptr( value_type const * v )
584             {
585                 return static_cast<node_type const *>( base_class::to_node_ptr( v ) );
586             }
587
588             /// Convert node refernce to value pointer
589             static value_type * to_value_ptr( node_type&  n )
590             {
591                 return base_class::to_value_ptr( static_cast<base_node_type &>( n ) );
592             }
593
594             /// Convert node pointer to value pointer
595             static value_type * to_value_ptr( node_type *  n )
596             {
597                 return base_class::to_value_ptr( static_cast<base_node_type *>( n ) );
598             }
599
600             /// Convert node reference to value pointer (const version)
601             static const value_type * to_value_ptr( node_type const & n )
602             {
603                 return base_class::to_value_ptr( static_cast<base_node_type const &>( n ) );
604             }
605
606             /// Convert node pointer to value pointer (const version)
607             static const value_type * to_value_ptr( node_type const * n )
608             {
609                 return base_class::to_value_ptr( static_cast<base_node_type const *>( n ) );
610             }
611         };
612
613         //@cond
614         namespace details {
615             template <bool Value, typename GC, typename Node, typename... Options>
616             struct bucket_table_selector;
617
618             template <typename GC, typename Node, typename... Options>
619             struct bucket_table_selector< true, GC, Node, Options...>
620             {
621                 typedef expandable_bucket_table<GC, Node, Options...>    type;
622             };
623
624             template <typename GC, typename Node, typename... Options>
625             struct bucket_table_selector< false, GC, Node, Options...>
626             {
627                 typedef static_bucket_table<GC, Node, Options...>    type;
628             };
629
630             template <typename GC, class Alloc >
631             struct dummy_node_disposer {
632                 template <typename Node>
633                 void operator()( Node * p )
634                 {
635                     typedef cds::details::Allocator< Node, Alloc >  node_deallocator;
636                     node_deallocator().Delete( p );
637                 }
638             };
639
640             template <typename Q>
641             struct search_value_type
642             {
643                 Q&      val;
644                 size_t  nHash;
645
646                 search_value_type( Q& v, size_t h )
647                     : val( v )
648                     , nHash( h )
649                 {}
650             };
651
652             template <class OrderedList, class Options>
653             class rebind_list_options
654             {
655                 typedef OrderedList native_ordered_list;
656                 typedef Options     options;
657
658                 typedef typename native_ordered_list::gc                gc;
659                 typedef typename native_ordered_list::key_comparator    native_key_comparator;
660                 typedef typename native_ordered_list::node_type         node_type;
661                 typedef typename native_ordered_list::value_type        value_type;
662                 typedef typename native_ordered_list::node_traits       node_traits;
663                 typedef typename native_ordered_list::disposer          native_disposer;
664
665                 typedef split_list::node<node_type>                     splitlist_node_type;
666
667                 struct key_compare {
668                     int operator()( value_type const& v1, value_type const& v2 ) const
669                     {
670                         splitlist_node_type const * n1 = static_cast<splitlist_node_type const *>( node_traits::to_node_ptr( v1 ));
671                         splitlist_node_type const * n2 = static_cast<splitlist_node_type const *>( node_traits::to_node_ptr( v2 ));
672                         if ( n1->m_nHash != n2->m_nHash )
673                             return n1->m_nHash < n2->m_nHash ? -1 : 1;
674
675                         if ( n1->is_dummy() ) {
676                             assert( n2->is_dummy() );
677                             return 0;
678                         }
679
680                         assert( !n1->is_dummy() && !n2->is_dummy() );
681
682                         return native_key_comparator()( v1, v2 );
683                     }
684
685                     template <typename Q>
686                     int operator()( value_type const& v, search_value_type<Q> const& q ) const
687                     {
688                         splitlist_node_type const * n = static_cast<splitlist_node_type const *>( node_traits::to_node_ptr( v ));
689                         if ( n->m_nHash != q.nHash )
690                             return n->m_nHash < q.nHash ? -1 : 1;
691
692                         assert( !n->is_dummy() );
693                         return native_key_comparator()( v, q.val );
694                     }
695
696                     template <typename Q>
697                     int operator()( search_value_type<Q> const& q, value_type const& v ) const
698                     {
699                         return -operator()( v, q );
700                     }
701                 };
702
703                 struct wrapped_disposer
704                 {
705                     void operator()( value_type * v )
706                     {
707                         splitlist_node_type * p = static_cast<splitlist_node_type *>( node_traits::to_node_ptr( v ));
708                         if ( p->is_dummy() )
709                             dummy_node_disposer<gc, typename options::allocator>()( p );
710                         else
711                             native_disposer()( v );
712                     }
713                 };
714
715             public:
716                 template <typename Less>
717                 struct make_compare_from_less: public cds::opt::details::make_comparator_from_less<Less>
718                 {
719                     typedef cds::opt::details::make_comparator_from_less<Less>  base_class;
720
721                     template <typename Q>
722                     int operator()( value_type const& v, search_value_type<Q> const& q ) const
723                     {
724                         splitlist_node_type const * n = static_cast<splitlist_node_type const *>( node_traits::to_node_ptr( v ));
725                         if ( n->m_nHash != q.nHash )
726                             return n->m_nHash < q.nHash ? -1 : 1;
727
728                         assert( !n->is_dummy() );
729                         return base_class()( v, q.val );
730                     }
731
732                     template <typename Q>
733                     int operator()( search_value_type<Q> const& q, value_type const& v ) const
734                     {
735                         splitlist_node_type const * n = static_cast<splitlist_node_type const *>( node_traits::to_node_ptr( v ));
736                         if ( n->m_nHash != q.nHash )
737                             return q.nHash < n->m_nHash ? -1 : 1;
738
739                         assert( !n->is_dummy() );
740                         return base_class()( q.val, v );
741                     }
742
743                     template <typename Q1, typename Q2>
744                     int operator()( Q1 const& v1, Q2 const& v2 ) const
745                     {
746                         return base_class()( v1, v2 );
747                     }
748                 };
749
750                 typedef typename native_ordered_list::template rebind_traits<
751                     opt::compare< key_compare >
752                     ,opt::disposer< wrapped_disposer >
753                     ,opt::boundary_node_type< splitlist_node_type >
754                 >::type    result;
755             };
756
757             template <typename OrderedList, bool IsConst>
758             struct select_list_iterator;
759
760             template <typename OrderedList>
761             struct select_list_iterator<OrderedList, false>
762             {
763                 typedef typename OrderedList::iterator  type;
764             };
765
766             template <typename OrderedList>
767             struct select_list_iterator<OrderedList, true>
768             {
769                 typedef typename OrderedList::const_iterator  type;
770             };
771
772             template <typename NodeTraits, typename OrderedList, bool IsConst>
773             class iterator_type
774             {
775                 typedef OrderedList     ordered_list_type;
776             protected:
777                 typedef typename select_list_iterator<ordered_list_type, IsConst>::type    list_iterator;
778                 typedef NodeTraits      node_traits;
779
780             private:
781                 list_iterator   m_itCur;
782                 list_iterator   m_itEnd;
783
784             public:
785                 typedef typename list_iterator::value_ptr   value_ptr;
786                 typedef typename list_iterator::value_ref   value_ref;
787
788             public:
789                 iterator_type()
790                 {}
791
792                 iterator_type( iterator_type const& src )
793                     : m_itCur( src.m_itCur )
794                     , m_itEnd( src.m_itEnd )
795                 {}
796
797                 // This ctor should be protected...
798                 iterator_type( list_iterator itCur, list_iterator itEnd )
799                     : m_itCur( itCur )
800                     , m_itEnd( itEnd )
801                 {
802                     // skip dummy nodes
803                     while ( m_itCur != m_itEnd && node_traits::to_node_ptr( *m_itCur )->is_dummy() )
804                         ++m_itCur;
805                 }
806
807
808                 value_ptr operator ->() const
809                 {
810                     return m_itCur.operator->();
811                 }
812
813                 value_ref operator *() const
814                 {
815                     return m_itCur.operator*();
816                 }
817
818                 /// Pre-increment
819                 iterator_type& operator ++()
820                 {
821                     if ( m_itCur != m_itEnd ) {
822                         do {
823                             ++m_itCur;
824                         } while ( m_itCur != m_itEnd && node_traits::to_node_ptr( *m_itCur )->is_dummy() );
825                     }
826                     return *this;
827                 }
828
829                 iterator_type& operator = (iterator_type const& src)
830                 {
831                     m_itCur = src.m_itCur;
832                     m_itEnd = src.m_itEnd;
833                     return *this;
834                 }
835
836                 template <bool C>
837                 bool operator ==(iterator_type<node_traits, ordered_list_type, C> const& i ) const
838                 {
839                     return m_itCur == i.m_itCur;
840                 }
841                 template <bool C>
842                 bool operator !=(iterator_type<node_traits, ordered_list_type, C> const& i ) const
843                 {
844                     return m_itCur != i.m_itCur;
845                 }
846             };
847         }   // namespace details
848         //@endcond
849
850         //@cond
851         // Helper functions
852
853         /// Reverses bit order in \p nHash
854         static inline size_t reverse_bits( size_t nHash )
855         {
856             return bitop::RBO( nHash );
857         }
858
859         static inline size_t regular_hash( size_t nHash )
860         {
861             return reverse_bits( nHash ) | size_t(1);
862         }
863
864         static inline size_t dummy_hash( size_t nHash )
865         {
866             return reverse_bits( nHash ) & ~size_t(1);
867         }
868         //@endcond
869
870     }   // namespace split_list
871
872     //@cond
873     // Forward declaration
874     template <class GC, class OrderedList, class Traits = split_list::traits>
875     class SplitListSet;
876     //@endcond
877
878 }}  // namespace cds::intrusive
879
880 #endif // #ifndef __CDS_INTRUSIVE_DETAILS_SPLIT_LIST_BASE_H