2 This file is a part of libcds - Concurrent Data Structures library
4 (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
6 Source code repo: http://github.com/khizmax/libcds/
7 Download: http://sourceforge.net/projects/libcds/files/
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
12 * Redistributions of source code must retain the above copyright notice, this
13 list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 this list of conditions and the following disclaimer in the documentation
17 and/or other materials provided with the distribution.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef CDSLIB_CONTAINER_LAZY_LIST_NOGC_H
32 #define CDSLIB_CONTAINER_LAZY_LIST_NOGC_H
35 #include <cds/container/details/lazy_list_base.h>
36 #include <cds/intrusive/lazy_list_nogc.h>
37 #include <cds/container/details/make_lazy_list.h>
39 namespace cds { namespace container {
41 /// Lazy ordered single-linked list (template specialization for gc::nogc)
42 /** @ingroup cds_nonintrusive_list
43 \anchor cds_nonintrusive_LazyList_nogc
45 This specialization is so-called append-only when no item
46 reclamation may be performed. The class does not support deleting of list item.
48 The list can be ordered if \p Traits::sort is \p true that is default
49 or unordered otherwise. Unordered list can be maintained by \p equal_to
50 relationship (\p Traits::equal_to), but for the ordered list \p less
51 or \p compare relations should be specified in \p Traits.
53 See @ref cds_nonintrusive_LazyList_gc "cds::container::LazyList<cds::gc::nogc, T, Traits>"
57 #ifdef CDS_DOXYGEN_INVOKED
58 typename Traits = lazy_list::traits
63 class LazyList<cds::gc::nogc, T, Traits>:
64 #ifdef CDS_DOXYGEN_INVOKED
65 protected intrusive::LazyList< gc::nogc, T, Traits >
67 protected details::make_lazy_list< cds::gc::nogc, T, Traits >::type
71 typedef details::make_lazy_list< cds::gc::nogc, T, Traits > maker;
72 typedef typename maker::type base_class;
76 typedef cds::gc::nogc gc; ///< Garbage collector
77 typedef T value_type; ///< Type of value stored in the list
78 typedef Traits traits; ///< List traits
80 typedef typename base_class::back_off back_off; ///< Back-off strategy used
81 typedef typename maker::allocator_type allocator_type; ///< Allocator type used for allocate/deallocate the nodes
82 typedef typename base_class::item_counter item_counter; ///< Item counting policy used
83 typedef typename maker::key_comparator key_comparator; ///< key comparing functor
84 typedef typename base_class::memory_model memory_model; ///< Memory ordering. See cds::opt::memory_model option
85 static CDS_CONSTEXPR bool const c_bSort = base_class::c_bSort; ///< List type: ordered (\p true) or unordered (\p false)
89 typedef typename base_class::value_type node_type;
90 typedef typename maker::cxx_allocator cxx_allocator;
91 typedef typename maker::node_deallocator node_deallocator;
92 typedef typename base_class::key_comparator intrusive_key_comparator;
94 typedef typename base_class::node_type head_type;
99 static node_type * alloc_node()
101 return cxx_allocator().New();
104 static node_type * alloc_node( value_type const& v )
106 return cxx_allocator().New( v );
109 template <typename... Args>
110 static node_type * alloc_node( Args&&... args )
112 return cxx_allocator().MoveNew( std::forward<Args>(args)... );
115 static void free_node( node_type * pNode )
117 cxx_allocator().Delete( pNode );
120 struct node_disposer {
121 void operator()( node_type * pNode )
126 typedef std::unique_ptr< node_type, node_disposer > scoped_node_ptr;
130 return base_class::m_Head;
133 head_type const& head() const
135 return base_class::m_Head;
140 return base_class::m_Tail;
143 head_type const& tail() const
145 return base_class::m_Tail;
151 template <bool IsConst>
152 class iterator_type: protected base_class::template iterator_type<IsConst>
154 typedef typename base_class::template iterator_type<IsConst> iterator_base;
156 iterator_type( head_type const& pNode )
157 : iterator_base( const_cast<head_type *>(&pNode) )
160 explicit iterator_type( const iterator_base& it )
161 : iterator_base( it )
164 friend class LazyList;
167 explicit iterator_type( node_type& pNode )
168 : iterator_base( &pNode )
172 typedef typename cds::details::make_const_type<value_type, IsConst>::pointer value_ptr;
173 typedef typename cds::details::make_const_type<value_type, IsConst>::reference value_ref;
178 iterator_type( const iterator_type& src )
179 : iterator_base( src )
182 value_ptr operator ->() const
184 typename iterator_base::value_ptr p = iterator_base::operator ->();
185 return p ? &(p->m_Value) : nullptr;
188 value_ref operator *() const
190 return (iterator_base::operator *()).m_Value;
194 iterator_type& operator ++()
196 iterator_base::operator ++();
201 iterator_type operator ++(int)
203 return iterator_base::operator ++(0);
207 bool operator ==(iterator_type<C> const& i ) const
209 return iterator_base::operator ==(i);
212 bool operator !=(iterator_type<C> const& i ) const
214 return iterator_base::operator !=(i);
220 ///@name Forward iterators
222 /// Returns a forward iterator addressing the first element in a list
224 For empty list \code begin() == end() \endcode
226 typedef iterator_type<false> iterator;
228 /// Const forward iterator
230 For iterator's features and requirements see \ref iterator
232 typedef iterator_type<true> const_iterator;
234 /// Returns a forward iterator addressing the first element in a list
236 For empty list \code begin() == end() \endcode
240 iterator it( head() );
241 ++it ; // skip dummy head node
245 /// Returns an iterator that addresses the location succeeding the last element in a list
247 Do not use the value returned by <tt>end</tt> function to access any item.
249 The returned value can be used only to control reaching the end of the list.
250 For empty list \code begin() == end() \endcode
254 return iterator( tail());
257 /// Returns a forward const iterator addressing the first element in a list
258 const_iterator begin() const
260 const_iterator it( head() );
261 ++it ; // skip dummy head node
265 /// Returns a forward const iterator addressing the first element in a list
266 const_iterator cbegin() const
268 const_iterator it( head() );
269 ++it ; // skip dummy head node
273 /// Returns an const iterator that addresses the location succeeding the last element in a list
274 const_iterator end() const
276 return const_iterator( tail());
279 /// Returns an const iterator that addresses the location succeeding the last element in a list
280 const_iterator cend() const
282 return const_iterator( tail());
288 iterator node_to_iterator( node_type * pNode )
291 return iterator( *pNode );
297 /// Default constructor
301 /// Desctructor clears the list
309 The function inserts \p val in the list if the list does not contain
310 an item with key equal to \p val.
312 Return an iterator pointing to inserted item if success \ref end() otherwise
314 template <typename Q>
315 iterator insert( Q const& val )
317 return node_to_iterator( insert_at( head(), val ) );
320 /// Inserts data of type \p value_type created from \p args
322 Return an iterator pointing to inserted item if success \ref end() otherwise
324 template <typename... Args>
325 iterator emplace( Args&&... args )
327 return node_to_iterator( emplace_at( head(), std::forward<Args>(args)... ));
332 If \p key is not in the list and \p bAllowInsert is \p true,
334 the function inserts a new item.
335 Otherwise, the function returns an iterator pointing to the item found.
337 Returns <tt> std::pair<iterator, bool> </tt> where \p first is an iterator pointing to
338 item found or inserted, \p second is true if new item has been added or \p false if the item
339 already is in the list.
341 template <typename Q>
342 std::pair<iterator, bool> update( Q const& val, bool bAllowInsert = true )
344 std::pair< node_type *, bool > ret = update_at( head(), val, bAllowInsert );
345 return std::make_pair( node_to_iterator( ret.first ), ret.second );
348 template <typename Q>
349 CDS_DEPRECATED("ensure() is deprecated, use update()")
350 std::pair<iterator, bool> ensure( Q const& val )
352 return update( val, true );
356 /// Checks whether the list contains \p key
358 The function searches the item with key equal to \p key
359 and returns an iterator pointed to item found if the key is found,
360 and \ref end() otherwise
362 template <typename Q>
363 iterator contains( Q const& key )
365 return node_to_iterator( find_at( head(), key, intrusive_key_comparator() ));
368 template <typename Q>
369 CDS_DEPRECATED("deprecated, use contains()")
370 iterator find( Q const& key )
372 return contains( key );
376 /// Checks whether the map contains \p key using \p pred predicate for searching (ordered list version)
378 The function is an analog of <tt>contains( key )</tt> but \p pred is used for key comparing.
379 \p Less functor has the interface like \p std::less.
380 \p Less must imply the same element order as the comparator used for building the list.
382 template <typename Q, typename Less, bool Sort = c_bSort>
383 typename std::enable_if<Sort, iterator>::type contains( Q const& key, Less pred )
386 return node_to_iterator( find_at( head(), key, typename maker::template less_wrapper<Less>::type() ));
389 template <typename Q, typename Less, bool Sort = c_bSort>
390 CDS_DEPRECATED("deprecated, use contains()")
391 typename std::enable_if<Sort, iterator>::type find_with( Q const& key, Less pred )
393 return contains( key, pred );
397 /// Finds the key \p val using \p equal predicate for searching (unordered list version)
399 The function is an analog of <tt>contains( key )</tt> but \p equal is used for key comparing.
400 \p Equal functor has the interface like \p std::equal_to.
402 template <typename Q, typename Equal, bool Sort = c_bSort>
403 typename std::enable_if<!Sort, iterator>::type contains( Q const& key, Equal equal )
406 return node_to_iterator( find_at( head(), key, typename maker::template equal_to_wrapper<Equal>::type() ));
409 template <typename Q, typename Equal, bool Sort = c_bSort>
410 CDS_DEPRECATED("deprecated, use contains()")
411 typename std::enable_if<!Sort, iterator>::type find_with( Q const& key, Equal equal )
413 return contains( key, equal );
417 /// Check if the list is empty
420 return base_class::empty();
423 /// Returns list's item count
425 The value returned depends on \p Traits::item_counter type. For \p atomicity::empty_item_counter,
426 this function always returns 0.
428 @note Even if you use real item counter and it returns 0, this fact is not mean that the list
429 is empty. To check list emptyness use \ref empty() method.
433 return base_class::size();
444 node_type * insert_node_at( head_type& refHead, node_type * pNode )
446 assert( pNode != nullptr );
447 scoped_node_ptr p( pNode );
448 if ( base_class::insert_at( &refHead, *p ))
454 template <typename Q>
455 node_type * insert_at( head_type& refHead, Q const& val )
457 return insert_node_at( refHead, alloc_node( val ));
460 template <typename... Args>
461 node_type * emplace_at( head_type& refHead, Args&&... args )
463 return insert_node_at( refHead, alloc_node( std::forward<Args>(args)... ));
466 template <typename Q>
467 std::pair< node_type *, bool > update_at( head_type& refHead, Q const& val, bool bAllowInsert )
469 scoped_node_ptr pNode( alloc_node( val ));
470 node_type * pItemFound = nullptr;
472 std::pair<bool, bool> ret = base_class::update_at( &refHead, *pNode,
473 [&pItemFound](bool, node_type& item, node_type&){ pItemFound = &item; },
479 return std::make_pair( pItemFound, ret.second );
482 template <typename Q, typename Compare>
483 node_type * find_at( head_type& refHead, Q const& key, Compare cmp )
485 return base_class::find_at( &refHead, key, cmp );
490 }} // namespace cds::container
492 #endif // #ifndef CDSLIB_CONTAINER_LAZY_LIST_NOGC_H