3 #ifndef __CDS_CONTAINER_LAZY_LIST_NOGC_H
4 #define __CDS_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 @copydetails cds_nonintrusive_LazyList_gc
24 #ifdef CDS_DOXYGEN_INVOKED
25 typename Traits = lazy_list::traits
30 class LazyList<cds::gc::nogc, T, Traits>:
31 #ifdef CDS_DOXYGEN_INVOKED
32 protected intrusive::LazyList< gc::nogc, T, Traits >
34 protected details::make_lazy_list< cds::gc::nogc, T, Traits >::type
38 typedef details::make_lazy_list< cds::gc::nogc, T, Traits > maker;
39 typedef typename maker::type base_class;
43 typedef cds::gc::nogc gc; ///< Garbage collector
44 typedef T value_type; ///< Type of value stored in the list
45 typedef Traits traits; ///< List traits
47 typedef typename base_class::back_off back_off; ///< Back-off strategy used
48 typedef typename maker::allocator_type allocator_type; ///< Allocator type used for allocate/deallocate the nodes
49 typedef typename base_class::item_counter item_counter; ///< Item counting policy used
50 typedef typename maker::key_comparator key_comparator; ///< key comparison functor
51 typedef typename base_class::memory_model memory_model; ///< Memory ordering. See cds::opt::memory_model option
55 typedef typename base_class::value_type node_type;
56 typedef typename maker::cxx_allocator cxx_allocator;
57 typedef typename maker::node_deallocator node_deallocator;
58 typedef typename maker::intrusive_traits::compare intrusive_key_comparator;
60 typedef typename base_class::node_type head_type;
65 static node_type * alloc_node()
67 return cxx_allocator().New();
70 static node_type * alloc_node( value_type const& v )
72 return cxx_allocator().New( v );
75 template <typename... Args>
76 static node_type * alloc_node( Args&&... args )
78 return cxx_allocator().MoveNew( std::forward<Args>(args)... );
81 static void free_node( node_type * pNode )
83 cxx_allocator().Delete( pNode );
86 struct node_disposer {
87 void operator()( node_type * pNode )
92 typedef std::unique_ptr< node_type, node_disposer > scoped_node_ptr;
96 return base_class::m_Head;
99 head_type const& head() const
101 return base_class::m_Head;
106 return base_class::m_Tail;
109 head_type const& tail() const
111 return base_class::m_Tail;
117 template <bool IsConst>
118 class iterator_type: protected base_class::template iterator_type<IsConst>
120 typedef typename base_class::template iterator_type<IsConst> iterator_base;
122 iterator_type( head_type const& pNode )
123 : iterator_base( const_cast<head_type *>(&pNode) )
126 explicit iterator_type( const iterator_base& it )
127 : iterator_base( it )
130 friend class LazyList;
133 explicit iterator_type( node_type& pNode )
134 : iterator_base( &pNode )
138 typedef typename cds::details::make_const_type<value_type, IsConst>::pointer value_ptr;
139 typedef typename cds::details::make_const_type<value_type, IsConst>::reference value_ref;
144 iterator_type( const iterator_type& src )
145 : iterator_base( src )
148 value_ptr operator ->() const
150 typename iterator_base::value_ptr p = iterator_base::operator ->();
151 return p ? &(p->m_Value) : nullptr;
154 value_ref operator *() const
156 return (iterator_base::operator *()).m_Value;
160 iterator_type& operator ++()
162 iterator_base::operator ++();
167 iterator_type operator ++(int)
169 return iterator_base::operator ++(0);
173 bool operator ==(iterator_type<C> const& i ) const
175 return iterator_base::operator ==(i);
178 bool operator !=(iterator_type<C> const& i ) const
180 return iterator_base::operator !=(i);
186 /// Returns a forward iterator addressing the first element in a list
188 For empty list \code begin() == end() \endcode
190 typedef iterator_type<false> iterator;
192 /// Const forward iterator
194 For iterator's features and requirements see \ref iterator
196 typedef iterator_type<true> const_iterator;
198 /// Returns a forward iterator addressing the first element in a list
200 For empty list \code begin() == end() \endcode
204 iterator it( head() );
205 ++it ; // skip dummy head node
209 /// Returns an iterator that addresses the location succeeding the last element in a list
211 Do not use the value returned by <tt>end</tt> function to access any item.
213 The returned value can be used only to control reaching the end of the list.
214 For empty list \code begin() == end() \endcode
218 return iterator( tail());
221 /// Returns a forward const iterator addressing the first element in a list
223 const_iterator begin() const
225 const_iterator it( head() );
226 ++it ; // skip dummy head node
229 const_iterator cbegin() const
231 const_iterator it( head() );
232 ++it ; // skip dummy head node
237 /// Returns an const iterator that addresses the location succeeding the last element in a list
239 const_iterator end() const
241 return const_iterator( tail());
243 const_iterator cend() const
245 return const_iterator( tail());
251 iterator node_to_iterator( node_type * pNode )
254 return iterator( *pNode );
260 /// Default constructor
264 /// Desctructor clears the list
272 The function inserts \p val in the list if the list does not contain
273 an item with key equal to \p val.
275 Return an iterator pointing to inserted item if success \ref end() otherwise
277 template <typename Q>
278 iterator insert( Q const& val )
280 return node_to_iterator( insert_at( head(), val ) );
283 /// Inserts data of type \p value_type created from \p args
285 Return an iterator pointing to inserted item if success \ref end() otherwise
287 template <typename... Args>
288 iterator emplace( Args&&... args )
290 return node_to_iterator( emplace_at( head(), std::forward<Args>(args)... ));
293 /// Ensures that the item \p val exists in the list
295 The operation inserts new item if the key \p val is not found in the list.
296 Otherwise, the function returns an iterator that points to item found.
298 Returns <tt> std::pair<iterator, bool> </tt> where \p first is an iterator pointing to
299 item found or inserted, \p second is \p true if new item has been added or \p false if the item
300 already is in the list.
302 template <typename Q>
303 std::pair<iterator, bool> ensure( Q const& val )
305 std::pair< node_type *, bool > ret = ensure_at( head(), val );
306 return std::make_pair( node_to_iterator( ret.first ), ret.second );
309 /// Find the key \p val
310 /** \anchor cds_nonintrusive_LazyList_nogc_find
311 The function searches the item with key equal to \p val
312 and returns an iterator pointed to item found if the key is found,
313 and \ref end() otherwise
315 template <typename Q>
316 iterator find( Q const& key )
318 return node_to_iterator( find_at( head(), key, intrusive_key_comparator() ));
321 /// Finds the key \p val using \p pred predicate for searching
323 The function is an analog of \ref cds_nonintrusive_LazyList_nogc_find "find(Q const&)"
324 but \p pred is used for key comparing.
325 \p Less functor has the interface like \p std::less.
326 \p pred must imply the same element order as the comparator used for building the list.
328 template <typename Q, typename Less>
329 iterator find_with( Q const& key, Less pred )
332 return node_to_iterator( find_at( head(), key, typename maker::template less_wrapper<Less>::type() ));
335 /// Check if the list is empty
338 return base_class::empty();
341 /// Returns list's item count
343 The value returned depends on \p Traits::item_counter type. For \p atomicity::empty_item_counter,
344 this function always returns 0.
346 @note Even if you use real item counter and it returns 0, this fact is not mean that the list
347 is empty. To check list emptyness use \ref empty() method.
351 return base_class::size();
362 node_type * insert_node_at( head_type& refHead, node_type * pNode )
364 assert( pNode != nullptr );
365 scoped_node_ptr p( pNode );
366 if ( base_class::insert_at( &refHead, *p ))
372 template <typename Q>
373 node_type * insert_at( head_type& refHead, Q const& val )
375 return insert_node_at( refHead, alloc_node( val ));
378 template <typename... Args>
379 node_type * emplace_at( head_type& refHead, Args&&... args )
381 return insert_node_at( refHead, alloc_node( std::forward<Args>(args)... ));
384 template <typename Q>
385 std::pair< node_type *, bool > ensure_at( head_type& refHead, Q const& val )
387 scoped_node_ptr pNode( alloc_node( val ));
388 node_type * pItemFound = nullptr;
390 std::pair<bool, bool> ret = base_class::ensure_at( &refHead, *pNode,
391 [&pItemFound](bool, node_type& item, node_type&){ pItemFound = &item; });
392 assert( pItemFound != nullptr );
394 if ( ret.first && ret.second )
397 return std::make_pair( pItemFound, ret.second );
400 template <typename Q, typename Compare>
401 node_type * find_at( head_type& refHead, Q const& key, Compare cmp )
403 return base_class::find_at( &refHead, key, cmp );
408 }} // namespace cds::container
410 #endif // #ifndef __CDS_CONTAINER_LAZY_LIST_NOGC_H