3 #ifndef CDSLIB_CONTAINER_LAZY_LIST_NOGC_H
4 #define CDSLIB_CONTAINER_LAZY_LIST_NOGC_H
7 #include <cds/container/details/lazy_list_base.h>
8 #include <cds/intrusive/lazy_list_nogc.h>
9 #include <cds/container/details/make_lazy_list.h>
11 namespace cds { namespace container {
13 /// Lazy ordered single-linked list (template specialization for gc::nogc)
14 /** @ingroup cds_nonintrusive_list
15 \anchor cds_nonintrusive_LazyList_nogc
17 This specialization is so-called append-only when no item
18 reclamation may be performed. The class does not support deleting of list item.
20 The list can be ordered if \p Traits::sort is \p true that is default
21 or unordered otherwise. Unordered list can be maintained by \p equal_to
22 relationship (\p Traits::equal_to), but for the ordered list \p less
23 or \p compare relations should be specified in \p Traits.
25 See @ref cds_nonintrusive_LazyList_gc "cds::container::LazyList<cds::gc::nogc, T, Traits>"
29 #ifdef CDS_DOXYGEN_INVOKED
30 typename Traits = lazy_list::traits
35 class LazyList<cds::gc::nogc, T, Traits>:
36 #ifdef CDS_DOXYGEN_INVOKED
37 protected intrusive::LazyList< gc::nogc, T, Traits >
39 protected details::make_lazy_list< cds::gc::nogc, T, Traits >::type
43 typedef details::make_lazy_list< cds::gc::nogc, T, Traits > maker;
44 typedef typename maker::type base_class;
48 typedef cds::gc::nogc gc; ///< Garbage collector
49 typedef T value_type; ///< Type of value stored in the list
50 typedef Traits traits; ///< List traits
52 typedef typename base_class::back_off back_off; ///< Back-off strategy used
53 typedef typename maker::allocator_type allocator_type; ///< Allocator type used for allocate/deallocate the nodes
54 typedef typename base_class::item_counter item_counter; ///< Item counting policy used
55 typedef typename maker::key_comparator key_comparator; ///< key comparing functor
56 typedef typename base_class::memory_model memory_model; ///< Memory ordering. See cds::opt::memory_model option
57 static CDS_CONSTEXPR bool const c_bSort = base_class::c_bSort; ///< List type: ordered (\p true) or unordered (\p false)
61 typedef typename base_class::value_type node_type;
62 typedef typename maker::cxx_allocator cxx_allocator;
63 typedef typename maker::node_deallocator node_deallocator;
64 typedef typename base_class::key_comparator intrusive_key_comparator;
66 typedef typename base_class::node_type head_type;
71 static node_type * alloc_node()
73 return cxx_allocator().New();
76 static node_type * alloc_node( value_type const& v )
78 return cxx_allocator().New( v );
81 template <typename... Args>
82 static node_type * alloc_node( Args&&... args )
84 return cxx_allocator().MoveNew( std::forward<Args>(args)... );
87 static void free_node( node_type * pNode )
89 cxx_allocator().Delete( pNode );
92 struct node_disposer {
93 void operator()( node_type * pNode )
98 typedef std::unique_ptr< node_type, node_disposer > scoped_node_ptr;
102 return base_class::m_Head;
105 head_type const& head() const
107 return base_class::m_Head;
112 return base_class::m_Tail;
115 head_type const& tail() const
117 return base_class::m_Tail;
123 template <bool IsConst>
124 class iterator_type: protected base_class::template iterator_type<IsConst>
126 typedef typename base_class::template iterator_type<IsConst> iterator_base;
128 iterator_type( head_type const& pNode )
129 : iterator_base( const_cast<head_type *>(&pNode) )
132 explicit iterator_type( const iterator_base& it )
133 : iterator_base( it )
136 friend class LazyList;
139 explicit iterator_type( node_type& pNode )
140 : iterator_base( &pNode )
144 typedef typename cds::details::make_const_type<value_type, IsConst>::pointer value_ptr;
145 typedef typename cds::details::make_const_type<value_type, IsConst>::reference value_ref;
150 iterator_type( const iterator_type& src )
151 : iterator_base( src )
154 value_ptr operator ->() const
156 typename iterator_base::value_ptr p = iterator_base::operator ->();
157 return p ? &(p->m_Value) : nullptr;
160 value_ref operator *() const
162 return (iterator_base::operator *()).m_Value;
166 iterator_type& operator ++()
168 iterator_base::operator ++();
173 iterator_type operator ++(int)
175 return iterator_base::operator ++(0);
179 bool operator ==(iterator_type<C> const& i ) const
181 return iterator_base::operator ==(i);
184 bool operator !=(iterator_type<C> const& i ) const
186 return iterator_base::operator !=(i);
192 /// Returns a forward iterator addressing the first element in a list
194 For empty list \code begin() == end() \endcode
196 typedef iterator_type<false> iterator;
198 /// Const forward iterator
200 For iterator's features and requirements see \ref iterator
202 typedef iterator_type<true> const_iterator;
204 /// Returns a forward iterator addressing the first element in a list
206 For empty list \code begin() == end() \endcode
210 iterator it( head() );
211 ++it ; // skip dummy head node
215 /// Returns an iterator that addresses the location succeeding the last element in a list
217 Do not use the value returned by <tt>end</tt> function to access any item.
219 The returned value can be used only to control reaching the end of the list.
220 For empty list \code begin() == end() \endcode
224 return iterator( tail());
227 /// Returns a forward const iterator addressing the first element in a list
229 const_iterator begin() const
231 const_iterator it( head() );
232 ++it ; // skip dummy head node
235 const_iterator cbegin() const
237 const_iterator it( head() );
238 ++it ; // skip dummy head node
243 /// Returns an const iterator that addresses the location succeeding the last element in a list
245 const_iterator end() const
247 return const_iterator( tail());
249 const_iterator cend() const
251 return const_iterator( tail());
257 iterator node_to_iterator( node_type * pNode )
260 return iterator( *pNode );
266 /// Default constructor
270 /// Desctructor clears the list
278 The function inserts \p val in the list if the list does not contain
279 an item with key equal to \p val.
281 Return an iterator pointing to inserted item if success \ref end() otherwise
283 template <typename Q>
284 iterator insert( Q const& val )
286 return node_to_iterator( insert_at( head(), val ) );
289 /// Inserts data of type \p value_type created from \p args
291 Return an iterator pointing to inserted item if success \ref end() otherwise
293 template <typename... Args>
294 iterator emplace( Args&&... args )
296 return node_to_iterator( emplace_at( head(), std::forward<Args>(args)... ));
299 /// Ensures that the item \p val exists in the list
301 The operation inserts new item if the key \p val is not found in the list.
302 Otherwise, the function returns an iterator that points to item found.
304 Returns <tt> std::pair<iterator, bool> </tt> where \p first is an iterator pointing to
305 item found or inserted, \p second is \p true if new item has been added or \p false if the item
306 already is in the list.
308 template <typename Q>
309 std::pair<iterator, bool> ensure( Q const& val )
311 std::pair< node_type *, bool > ret = ensure_at( head(), val );
312 return std::make_pair( node_to_iterator( ret.first ), ret.second );
315 /// Find the key \p val
316 /** \anchor cds_nonintrusive_LazyList_nogc_find
317 The function searches the item with key equal to \p val
318 and returns an iterator pointed to item found if the key is found,
319 and \ref end() otherwise
321 template <typename Q>
322 iterator find( Q const& key )
324 return node_to_iterator( find_at( head(), key, intrusive_key_comparator() ));
327 /// Finds the key \p val using \p pred predicate for searching (only for ordered list)
329 The function is an analog of \ref cds_nonintrusive_LazyList_nogc_find "find(Q const&)"
330 but \p pred is used for key comparing.
331 \p Less functor has the interface like \p std::less.
332 \p pred must imply the same element order as the comparator used for building the list.
334 template <typename Q, typename Less, bool Sort = c_bSort>
335 typename std::enable_if<Sort, iterator>::type find_with( Q const& key, Less pred )
338 return node_to_iterator( find_at( head(), key, typename maker::template less_wrapper<Less>::type() ));
341 /// Finds the key \p val using \p equal predicate for searching (only for unordered list)
343 The function is an analog of \ref cds_nonintrusive_LazyList_nogc_find "find(Q const&)"
344 but \p pred is used for key comparing.
345 \p Equal functor has the interface like \p std::equal_to.
347 template <typename Q, typename Equal, bool Sort = c_bSort>
348 typename std::enable_if<!Sort, iterator>::type find_with( Q const& key, Equal equal )
351 return node_to_iterator( find_at( head(), key, typename maker::template equal_to_wrapper<Equal>::type() ));
354 /// Check if the list is empty
357 return base_class::empty();
360 /// Returns list's item count
362 The value returned depends on \p Traits::item_counter type. For \p atomicity::empty_item_counter,
363 this function always returns 0.
365 @note Even if you use real item counter and it returns 0, this fact is not mean that the list
366 is empty. To check list emptyness use \ref empty() method.
370 return base_class::size();
381 node_type * insert_node_at( head_type& refHead, node_type * pNode )
383 assert( pNode != nullptr );
384 scoped_node_ptr p( pNode );
385 if ( base_class::insert_at( &refHead, *p ))
391 template <typename Q>
392 node_type * insert_at( head_type& refHead, Q const& val )
394 return insert_node_at( refHead, alloc_node( val ));
397 template <typename... Args>
398 node_type * emplace_at( head_type& refHead, Args&&... args )
400 return insert_node_at( refHead, alloc_node( std::forward<Args>(args)... ));
403 template <typename Q>
404 std::pair< node_type *, bool > ensure_at( head_type& refHead, Q const& val )
406 scoped_node_ptr pNode( alloc_node( val ));
407 node_type * pItemFound = nullptr;
409 std::pair<bool, bool> ret = base_class::ensure_at( &refHead, *pNode,
410 [&pItemFound](bool, node_type& item, node_type&){ pItemFound = &item; });
411 assert( pItemFound != nullptr );
413 if ( ret.first && ret.second )
416 return std::make_pair( pItemFound, ret.second );
419 template <typename Q, typename Compare>
420 node_type * find_at( head_type& refHead, Q const& key, Compare cmp )
422 return base_class::find_at( &refHead, key, cmp );
427 }} // namespace cds::container
429 #endif // #ifndef CDSLIB_CONTAINER_LAZY_LIST_NOGC_H