3 #ifndef CDSLIB_CONTAINER_DETAILS_SKIP_LIST_BASE_H
4 #define CDSLIB_CONTAINER_DETAILS_SKIP_LIST_BASE_H
6 #include <cds/intrusive/details/skip_list_base.h>
7 #include <cds/container/details/base.h>
9 namespace cds { namespace container {
11 /// SkipListSet related definitions
12 /** @ingroup cds_nonintrusive_helper
16 /// Option specifying random level generator
17 template <typename Type>
18 using random_level_generator = cds::intrusive::skip_list::random_level_generator<Type>;
20 /// Xor-shift random level generator
21 typedef cds::intrusive::skip_list::xorshift xorshift;
23 /// Turbo-pascal random level generator
24 typedef cds::intrusive::skip_list::turbo_pascal turbo_pascal;
26 /// Skip list internal statistics
27 template <typename EventCounter = cds::atomicity::event_counter>
28 using stat = cds::intrusive::skip_list::stat < EventCounter >;
30 /// Skip list empty internal statistics
31 typedef cds::intrusive::skip_list::empty_stat empty_stat;
33 /// SkipListSet traits
36 /// Key comparison functor
38 No default functor is provided. If the option is not specified, the \p less is used.
40 typedef opt::none compare;
42 /// specifies binary predicate used for key compare.
44 Default is \p std::less<T>.
46 typedef opt::none less;
50 The type for item counting feature,
51 by defaulr disabled (\p atomicity::empty_item_counter)
53 typedef atomicity::empty_item_counter item_counter;
55 /// C++ memory ordering model
57 List of available memory ordering see \p opt::memory_model
59 typedef opt::v::relaxed_ordering memory_model;
61 /// Random level generator
63 The random level generator is an important part of skip-list algorithm.
64 The node height in the skip-list have a probabilistic distribution
65 where half of the nodes that have level \p i also have level <tt>i+1</tt>
66 (i = 0..30). The height of a node is in range [0..31].
68 See \p skip_list::random_level_generator option setter.
70 typedef turbo_pascal random_level_generator;
72 /// Allocator for skip-list nodes, \p std::allocator interface
73 typedef CDS_DEFAULT_ALLOCATOR allocator;
75 /// back-off strategy, default is \p cds::backoff::Default
76 typedef cds::backoff::Default back_off;
78 /// Internal statistics, by default disabled. To enable, use \p split_list::stat
79 typedef empty_stat stat;
81 /// RCU deadlock checking policy (for \ref cds_nonintrusive_SkipListSet_rcu "RCU-based SkipListSet")
83 List of available options see opt::rcu_check_deadlock
85 typedef opt::v::rcu_throw_deadlock rcu_check_deadlock;
88 // For internal use only
89 typedef opt::none key_accessor;
93 /// Metafunction converting option list to SkipListSet traits
96 - \p opt::compare - key comparison functor. No default functor is provided.
97 If the option is not specified, the \p opt::less is used.
98 - \p opt::less - specifies binary predicate used for key comparison. Default is \p std::less<T>.
99 - \p opt::item_counter - the type of item counting feature. Default is \p atomicity::empty_item_counter that is no item counting.
100 - \p opt::memory_model - C++ memory ordering model. Can be \p opt::v::relaxed_ordering (relaxed memory model, the default)
101 or \p opt::v::sequential_consistent (sequentially consisnent memory model).
102 - \p skip_list::random_level_generator - random level generator. Can be \p skip_list::xorshift, \p skip_list::turbo_pascal or
104 Default is \p %skip_list::turbo_pascal.
105 - \p opt::allocator - allocator for skip-list node. Default is \ref CDS_DEFAULT_ALLOCATOR.
106 - \p opt::back_off - back-off strategy used. If the option is not specified, the \p cds::backoff::Default is used.
107 - \p opt::stat - internal statistics. Available types: \p skip_list::stat, \p skip_list::empty_stat (the default)
108 - \p opt::rcu_check_deadlock - a deadlock checking policy for RCU-based skip-list.
109 Default is \p opt::v::rcu_throw_deadlock
112 template <typename... Options>
114 # ifdef CDS_DOXYGEN_INVOKED
115 typedef implementation_defined type ; ///< Metafunction result
117 typedef typename cds::opt::make_options<
118 typename cds::opt::find_type_traits< traits, Options... >::type
127 template <typename Node, typename Traits>
131 typedef Node node_type;
132 typedef Traits traits;
134 typedef typename node_type::tower_item_type node_tower_item;
135 typedef typename traits::allocator::template rebind<unsigned char>::other tower_allocator_type;
136 typedef typename traits::allocator::template rebind<node_type>::other node_allocator_type;
138 static size_t const c_nTowerItemSize = sizeof(node_tower_item);
139 static size_t const c_nNodePadding = sizeof(node_type) % c_nTowerItemSize;
140 static size_t const c_nNodeSize = sizeof(node_type) + (c_nNodePadding ? (c_nTowerItemSize - c_nNodePadding) : 0);
142 static CDS_CONSTEXPR size_t node_size( unsigned int nHeight ) CDS_NOEXCEPT
144 return c_nNodeSize + (nHeight - 1) * c_nTowerItemSize;
146 static unsigned char * alloc_space( unsigned int nHeight )
149 unsigned char * pMem = tower_allocator_type().allocate( node_size(nHeight) );
151 // check proper alignments
152 assert( (((uintptr_t) pMem) & (alignof(node_type) - 1)) == 0 );
153 assert( (((uintptr_t) (pMem + c_nNodeSize)) & (alignof(node_tower_item) - 1)) == 0 );
157 return reinterpret_cast<unsigned char *>( node_allocator_type().allocate(1));
160 static void free_space( unsigned char * p, unsigned int nHeight )
162 assert( p != nullptr );
164 node_allocator_type().deallocate( reinterpret_cast<node_type *>(p), 1 );
166 tower_allocator_type().deallocate( p, node_size(nHeight));
170 template <typename Q>
171 node_type * New( unsigned int nHeight, Q const& v )
173 unsigned char * pMem = alloc_space( nHeight );
175 node_type( nHeight, nHeight > 1 ? reinterpret_cast<node_tower_item *>(pMem + c_nNodeSize) : nullptr, v );
178 template <typename... Args>
179 node_type * New( unsigned int nHeight, Args&&... args )
181 unsigned char * pMem = alloc_space( nHeight );
183 node_type( nHeight, nHeight > 1 ? reinterpret_cast<node_tower_item *>(pMem + c_nNodeSize) : nullptr,
184 std::forward<Args>(args)... );
187 void Delete( node_type * p )
189 assert( p != nullptr );
191 unsigned int nHeight = p->height();
192 node_allocator_type().destroy( p );
193 free_space( reinterpret_cast<unsigned char *>(p), nHeight );
197 template <typename IntrusiveNode>
198 struct dummy_node_builder {
199 typedef IntrusiveNode intrusive_node_type;
201 template <typename RandomGen>
202 static intrusive_node_type * make_tower( intrusive_node_type * pNode, RandomGen& /*gen*/ ) { return pNode ; }
203 static intrusive_node_type * make_tower( intrusive_node_type * pNode, unsigned int /*nHeight*/ ) { return pNode ; }
204 static void dispose_tower( intrusive_node_type * pNode )
206 pNode->release_tower();
209 struct node_disposer {
210 void operator()( intrusive_node_type * /*pNode*/ ) const {}
214 template <typename ForwardIterator>
217 typedef ForwardIterator intrusive_iterator;
218 typedef typename intrusive_iterator::value_type node_type;
219 typedef typename node_type::stored_value_type value_type;
220 static bool const c_isConst = intrusive_iterator::c_isConst;
222 typedef typename std::conditional< c_isConst, value_type const&, value_type&>::type value_ref;
223 template <typename FwdIt> friend class iterator;
225 intrusive_iterator m_It;
227 public: // for internal use only!!!
228 iterator( intrusive_iterator const& it )
237 iterator( iterator const& s)
241 value_type * operator ->() const
243 return &( m_It.operator->()->m_Value );
246 value_ref operator *() const
248 return m_It.operator*().m_Value;
252 iterator& operator ++()
258 iterator& operator = (iterator const& src)
264 template <typename FwIt>
265 bool operator ==(iterator<FwIt> const& i ) const
267 return m_It == i.m_It;
269 template <typename FwIt>
270 bool operator !=(iterator<FwIt> const& i ) const
272 return !( *this == i );
276 } // namespace details
279 } // namespace skip_list
281 // Forward declaration
282 template <class GC, typename T, typename Traits = skip_list::traits >
285 // Forward declaration
286 template <class GC, typename K, typename T, typename Traits = skip_list::traits >
289 }} // namespace cds::container
291 #endif // #ifndef CDSLIB_CONTAINER_DETAILS_SKIP_LIST_BASE_H