3 #ifndef __CDS_CONTAINER_LAZY_LIST_NOGC_H
4 #define __CDS_CONTAINER_LAZY_LIST_NOGC_H
7 #include <cds/container/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 {
16 template <typename T, class Traits>
17 struct make_lazy_list_nogc: public make_lazy_list<gc::nogc, T, Traits>
19 typedef make_lazy_list<cds::gc::nogc, T, Traits> base_maker;
20 typedef typename base_maker::node_type node_type;
22 struct type_traits: public base_maker::type_traits
24 typedef typename base_maker::node_deallocator disposer;
27 typedef intrusive::LazyList<cds::gc::nogc, node_type, type_traits> type;
30 } // namespace details
33 /// Lazy ordered single-linked list (template specialization for gc::nogc)
34 /** @ingroup cds_nonintrusive_list
35 \anchor cds_nonintrusive_LazyList_nogc
37 This specialization is intended for so-called persistent usage when no item
38 reclamation may be performed. The class does not support deleting of list item.
40 Usually, ordered single-linked list is used as a building block for the hash table implementation.
41 The complexity of searching is <tt>O(N)</tt>.
43 See \ref cds_nonintrusive_LazyList_gc "LazyList" for description of template parameters.
45 The interface of the specialization is a little different.
47 template <typename T, typename Traits>
48 class LazyList<gc::nogc, T, Traits>:
49 #ifdef CDS_DOXYGEN_INVOKED
50 protected intrusive::LazyList< gc::nogc, T, Traits >
52 protected details::make_lazy_list_nogc< T, Traits >::type
56 typedef details::make_lazy_list_nogc< T, Traits > options;
57 typedef typename options::type base_class;
61 typedef T value_type ; ///< Type of value stored in the list
62 typedef typename base_class::gc gc ; ///< Garbage collector used
63 typedef typename base_class::back_off back_off ; ///< Back-off strategy used
64 typedef typename options::allocator_type allocator_type ; ///< Allocator type used for allocate/deallocate the nodes
65 typedef typename base_class::item_counter item_counter ; ///< Item counting policy used
66 typedef typename options::key_comparator key_comparator ; ///< key comparison functor
67 typedef typename base_class::memory_model memory_model ; ///< Memory ordering. See cds::opt::memory_model option
71 typedef typename base_class::value_type node_type;
72 typedef typename options::cxx_allocator cxx_allocator;
73 typedef typename options::node_deallocator node_deallocator;
74 typedef typename options::type_traits::compare intrusive_key_comparator;
76 typedef typename base_class::node_type head_type;
81 # ifndef CDS_CXX11_LAMBDA_SUPPORT
84 node_type * m_pItemFound;
87 : m_pItemFound( nullptr )
90 void operator ()(bool, node_type& item, node_type& )
100 static node_type * alloc_node()
102 return cxx_allocator().New();
105 static node_type * alloc_node( value_type const& v )
107 return cxx_allocator().New( v );
110 # ifdef CDS_EMPLACE_SUPPORT
111 template <typename... Args>
112 static node_type * alloc_node( Args&&... args )
114 return cxx_allocator().MoveNew( std::forward<Args>(args)... );
118 static void free_node( node_type * pNode )
120 cxx_allocator().Delete( pNode );
123 struct node_disposer {
124 void operator()( node_type * pNode )
129 typedef std::unique_ptr< node_type, node_disposer > scoped_node_ptr;
133 return base_class::m_Head;
136 head_type const& head() const
138 return base_class::m_Head;
143 return base_class::m_Tail;
146 head_type const& tail() const
148 return base_class::m_Tail;
154 template <bool IsConst>
155 class iterator_type: protected base_class::template iterator_type<IsConst>
157 typedef typename base_class::template iterator_type<IsConst> iterator_base;
159 iterator_type( head_type const& pNode )
160 : iterator_base( const_cast<head_type *>(&pNode) )
163 explicit iterator_type( const iterator_base& it )
164 : iterator_base( it )
167 friend class LazyList;
170 explicit iterator_type( node_type& pNode )
171 : iterator_base( &pNode )
175 typedef typename cds::details::make_const_type<value_type, IsConst>::pointer value_ptr;
176 typedef typename cds::details::make_const_type<value_type, IsConst>::reference value_ref;
181 iterator_type( const iterator_type& src )
182 : iterator_base( src )
185 value_ptr operator ->() const
187 typename iterator_base::value_ptr p = iterator_base::operator ->();
188 return p ? &(p->m_Value) : nullptr;
191 value_ref operator *() const
193 return (iterator_base::operator *()).m_Value;
197 iterator_type& operator ++()
199 iterator_base::operator ++();
204 iterator_type operator ++(int)
206 return iterator_base::operator ++(0);
210 bool operator ==(iterator_type<C> const& i ) const
212 return iterator_base::operator ==(i);
215 bool operator !=(iterator_type<C> const& i ) const
217 return iterator_base::operator !=(i);
223 /// Returns a forward iterator addressing the first element in a list
225 For empty list \code begin() == end() \endcode
227 typedef iterator_type<false> iterator;
229 /// Const forward iterator
231 For iterator's features and requirements see \ref iterator
233 typedef iterator_type<true> const_iterator;
235 /// Returns a forward iterator addressing the first element in a list
237 For empty list \code begin() == end() \endcode
241 iterator it( head() );
242 ++it ; // skip dummy head node
246 /// Returns an iterator that addresses the location succeeding the last element in a list
248 Do not use the value returned by <tt>end</tt> function to access any item.
250 The returned value can be used only to control reaching the end of the list.
251 For empty list \code begin() == end() \endcode
255 return iterator( tail());
258 /// Returns a forward const iterator addressing the first element in a list
260 const_iterator begin() const
262 const_iterator it( head() );
263 ++it ; // skip dummy head node
266 const_iterator cbegin()
268 const_iterator it( head() );
269 ++it ; // skip dummy head node
274 /// Returns an const iterator that addresses the location succeeding the last element in a list
276 const_iterator end() const
278 return const_iterator( tail());
280 const_iterator cend()
282 return const_iterator( tail());
288 iterator node_to_iterator( node_type * pNode )
291 return iterator( *pNode );
297 /// Default constructor
299 Initialize empty list
315 The function inserts \p val in the list if the list does not contain
316 an item with key equal to \p val.
318 Return an iterator pointing to inserted item if success \ref end() otherwise
320 template <typename Q>
321 iterator insert( Q const& val )
323 return node_to_iterator( insert_at( head(), val ) );
326 # ifdef CDS_EMPLACE_SUPPORT
327 /// Inserts data of type \ref value_type constructed with <tt>std::forward<Args>(args)...</tt>
329 Return an iterator pointing to inserted item if success \ref end() otherwise
331 This function is available only for compiler that supports
332 variadic template and move semantics
334 template <typename... Args>
335 iterator emplace( Args&&... args )
337 return node_to_iterator( emplace_at( head(), std::forward<Args>(args)... ));
341 /// Ensures that the item \p val exists in the list
343 The operation inserts new item if the key \p val is not found in the list.
344 Otherwise, the function returns an iterator that points to item found.
346 Returns <tt> std::pair<iterator, bool> </tt> where \p first is an iterator pointing to
347 item found or inserted, \p second is true if new item has been added or \p false if the item
348 already is in the list.
350 template <typename Q>
351 std::pair<iterator, bool> ensure( Q const& val )
353 std::pair< node_type *, bool > ret = ensure_at( head(), val );
354 return std::make_pair( node_to_iterator( ret.first ), ret.second );
357 /// Find the key \p val
358 /** \anchor cds_nonintrusive_LazyList_nogc_find
359 The function searches the item with key equal to \p val
360 and returns an iterator pointed to item found if the key is found,
361 and \ref end() otherwise
363 template <typename Q>
364 iterator find( Q const& key )
366 return node_to_iterator( find_at( head(), key, intrusive_key_comparator() ));
369 /// Finds the key \p val using \p pred predicate for searching
371 The function is an analog of \ref cds_nonintrusive_LazyList_nogc_find "find(Q const&)"
372 but \p pred is used for key comparing.
373 \p Less functor has the interface like \p std::less.
374 \p pred must imply the same element order as the comparator used for building the list.
376 template <typename Q, typename Less>
377 iterator find_with( Q const& key, Less pred )
379 return node_to_iterator( find_at( head(), key, typename options::template less_wrapper<Less>::type() ));
382 /// Check if the list is empty
385 return base_class::empty();
388 /// Returns list's item count
390 The value returned depends on opt::item_counter option. For atomicity::empty_item_counter,
391 this function always returns 0.
393 <b>Warning</b>: even if you use real item counter and it returns 0, this fact is not mean that the list
394 is empty. To check list emptyness use \ref empty() method.
398 return base_class::size();
403 Post-condition: the list is empty
412 node_type * insert_node_at( head_type& refHead, node_type * pNode )
414 assert( pNode != nullptr );
415 scoped_node_ptr p( pNode );
416 if ( base_class::insert_at( &refHead, *p ))
422 template <typename Q>
423 node_type * insert_at( head_type& refHead, Q const& val )
425 return insert_node_at( refHead, alloc_node( val ));
428 # ifdef CDS_EMPLACE_SUPPORT
429 template <typename... Args>
430 node_type * emplace_at( head_type& refHead, Args&&... args )
432 return insert_node_at( refHead, alloc_node( std::forward<Args>(args)... ));
435 template <typename Q>
436 std::pair< node_type *, bool > ensure_at( head_type& refHead, Q const& val )
438 scoped_node_ptr pNode( alloc_node( val ));
439 node_type * pItemFound = nullptr;
441 # ifdef CDS_CXX11_LAMBDA_SUPPORT
442 std::pair<bool, bool> ret = base_class::ensure_at( &refHead, *pNode,
443 [&pItemFound](bool, node_type& item, node_type&){ pItemFound = &item; });
446 std::pair<bool, bool> ret = base_class::ensure_at( &refHead, *pNode, boost::ref(func) );
447 pItemFound = func.m_pItemFound;
449 assert( pItemFound != nullptr );
451 if ( ret.first && ret.second )
454 return std::make_pair( pItemFound, ret.second );
457 template <typename Q, typename Compare>
458 node_type * find_at( head_type& refHead, Q const& key, Compare cmp )
460 return base_class::find_at( &refHead, key, cmp );
465 }} // namespace cds::container
467 #endif // #ifndef __CDS_CONTAINER_LAZY_LIST_NOGC_H