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_MICHAEL_LIST_NOGC_H
32 #define CDSLIB_CONTAINER_MICHAEL_LIST_NOGC_H
35 #include <cds/container/details/michael_list_base.h>
36 #include <cds/intrusive/michael_list_nogc.h>
37 #include <cds/container/details/make_michael_list.h>
39 namespace cds { namespace container {
44 template <typename T, class Traits>
45 struct make_michael_list_nogc: public make_michael_list<gc::nogc, T, Traits>
47 typedef make_michael_list<cds::gc::nogc, T, Traits> base_maker;
48 typedef typename base_maker::node_type node_type;
50 struct intrusive_traits: public base_maker::intrusive_traits
52 typedef typename base_maker::node_deallocator disposer;
55 typedef intrusive::MichaelList<cds::gc::nogc, node_type, intrusive_traits> type;
58 } // namespace details
61 /// Michael's lock-free ordered single-linked list (template specialization for \p gc::nogc)
62 /** @ingroup cds_nonintrusive_list
63 \anchor cds_nonintrusive_MichaelList_nogc
65 This specialization is intended for so-called append-only usage when no item
66 reclamation may be performed. The class does not support deleting of list item.
67 Usually, ordered single-linked list is used as a building block for the hash table implementation.
68 The complexity of searching is <tt>O(N)</tt>.
70 See \ref cds_nonintrusive_MichaelList_gc "MichaelList" for description of template parameters.
73 #ifdef CDS_DOXYGEN_INVOKED
74 class Traits = michael_list::traits
79 class MichaelList<gc::nogc, T, Traits>:
80 #ifdef CDS_DOXYGEN_INVOKED
81 protected intrusive::MichaelList< gc::nogc, T, Traits >
83 protected details::make_michael_list_nogc< T, Traits >::type
87 typedef details::make_michael_list_nogc< T, Traits > maker;
88 typedef typename maker::type base_class;
92 typedef cds::gc::nogc gc; ///< Garbage collector used
93 typedef T value_type; ///< Type of value stored in the list
94 typedef Traits traits; ///< List traits
96 typedef typename base_class::back_off back_off; ///< Back-off strategy used
97 typedef typename maker::allocator_type allocator_type; ///< Allocator type used for allocate/deallocate the nodes
98 typedef typename base_class::item_counter item_counter; ///< Item counting policy used
99 typedef typename maker::key_comparator key_comparator; ///< key comparison functor
100 typedef typename base_class::memory_model memory_model; ///< Memory ordering. See cds::opt::memory_model option
101 typedef typename base_class::stat stat; ///< Internal statistics
105 typedef typename base_class::value_type node_type;
106 typedef typename maker::cxx_allocator cxx_allocator;
107 typedef typename maker::node_deallocator node_deallocator;
108 typedef typename maker::intrusive_traits::compare intrusive_key_comparator;
110 typedef typename base_class::atomic_node_ptr head_type;
115 static node_type * alloc_node()
117 return cxx_allocator().New();
120 static node_type * alloc_node( value_type const& v )
122 return cxx_allocator().New( v );
125 template <typename... Args>
126 static node_type * alloc_node( Args&&... args )
128 return cxx_allocator().MoveNew( std::forward<Args>(args)... );
131 static void free_node( node_type * pNode )
133 cxx_allocator().Delete( pNode );
136 struct node_disposer {
137 void operator()( node_type * pNode )
142 typedef std::unique_ptr< node_type, node_disposer > scoped_node_ptr;
146 return base_class::m_pHead;
149 head_type const& head() const
151 return base_class::m_pHead;
157 template <bool IsConst>
158 class iterator_type: protected base_class::template iterator_type<IsConst>
160 typedef typename base_class::template iterator_type<IsConst> iterator_base;
162 iterator_type( head_type const& pNode )
163 : iterator_base( pNode )
166 explicit iterator_type( const iterator_base& it )
167 : iterator_base( it )
170 friend class MichaelList;
173 explicit iterator_type( node_type& pNode )
174 : iterator_base( &pNode )
178 typedef typename cds::details::make_const_type<value_type, IsConst>::pointer value_ptr;
179 typedef typename cds::details::make_const_type<value_type, IsConst>::reference value_ref;
184 iterator_type( const iterator_type& src )
185 : iterator_base( src )
188 value_ptr operator ->() const
190 typename iterator_base::value_ptr p = iterator_base::operator ->();
191 return p ? &(p->m_Value) : nullptr;
194 value_ref operator *() const
196 return (iterator_base::operator *()).m_Value;
200 iterator_type& operator ++()
202 iterator_base::operator ++();
207 iterator_type operator ++(int)
209 return iterator_base::operator ++(0);
213 bool operator ==(iterator_type<C> const& i ) const
215 return iterator_base::operator ==(i);
218 bool operator !=(iterator_type<C> const& i ) const
220 return iterator_base::operator !=(i);
226 ///@name Forward iterators
228 /// Returns a forward iterator addressing the first element in a list
230 For empty list \code begin() == end() \endcode
232 typedef iterator_type<false> iterator;
234 /// Const forward iterator
236 For iterator's features and requirements see \ref iterator
238 typedef iterator_type<true> const_iterator;
240 /// Returns a forward iterator addressing the first element in a list
242 For empty list \code begin() == end() \endcode
246 return iterator( head() );
249 /// Returns an iterator that addresses the location succeeding the last element in a list
251 Do not use the value returned by <tt>end</tt> function to access any item.
252 Internally, <tt>end</tt> returning value equals to \p nullptr.
254 The returned value can be used only to control reaching the end of the list.
255 For empty list \code begin() == end() \endcode
262 /// Returns a forward const iterator addressing the first element in a list
263 const_iterator begin() const
265 return const_iterator( head() );
268 /// Returns a forward const iterator addressing the first element in a list
269 const_iterator cbegin() const
271 return const_iterator( head() );
274 /// Returns an const iterator that addresses the location succeeding the last element in a list
275 const_iterator end() const
277 return const_iterator();
280 /// Returns an const iterator that addresses the location succeeding the last element in a list
281 const_iterator cend() const
283 return const_iterator();
289 iterator node_to_iterator( node_type * pNode )
292 return iterator( *pNode );
298 /// Default constructor
300 Initialize empty list
306 template <typename Stat, typename = std::enable_if<std::is_same<stat, michael_list::wrapped_stat<Stat>>::value >>
307 explicit MichaelList( Stat& st )
323 The function inserts \p val in the list if the list does not contain
324 an item with key equal to \p val.
326 Return an iterator pointing to inserted item if success \ref end() otherwise
328 template <typename Q>
329 iterator insert( const Q& val )
331 return node_to_iterator( insert_at( head(), val ) );
336 If \p key is not in the list and \p bAllowInsert is \p true,
337 the function inserts a new item.
338 Otherwise, the function returns an iterator pointing to the item found.
340 Returns <tt> std::pair<iterator, bool> </tt> where \p first is an iterator pointing to
341 item found or inserted, \p second is true if new item has been added or \p false if the item
342 already is in the list.
344 template <typename Q>
345 std::pair<iterator, bool> update( const Q& key, bool bAllowInsert = true )
347 std::pair< node_type *, bool > ret = update_at( head(), key, bAllowInsert );
348 return std::make_pair( node_to_iterator( ret.first ), ret.second );
351 template <typename Q>
352 CDS_DEPRECATED("ensure() is deprecated, use update()")
353 std::pair<iterator, bool> ensure( const Q& val )
355 return update( val, true );
359 /// Inserts data of type \ref value_type constructed with <tt>std::forward<Args>(args)...</tt>
361 Return an iterator pointing to inserted item if success \ref end() otherwise
363 template <typename... Args>
364 iterator emplace( Args&&... args )
366 return node_to_iterator( emplace_at( head(), std::forward<Args>(args)... ));
369 /// Checks whether the list contains \p key
371 The function searches the item with key equal to \p key
372 and returns an iterator pointed to item found if the key is found,
373 and \ref end() otherwise
375 template <typename Q>
376 iterator contains( Q const& key )
378 return node_to_iterator( find_at( head(), key, intrusive_key_comparator() ));
381 template <typename Q>
382 CDS_DEPRECATED("deprecated, use contains()")
383 iterator find( Q const& key )
385 return contains( key );
389 /// Checks whether the map contains \p key using \p pred predicate for searching
391 The function is an analog of <tt>contains( key )</tt> but \p pred is used for key comparing.
392 \p Less functor has the interface like \p std::less.
393 \p Less must imply the same element order as the comparator used for building the list.
395 template <typename Q, typename Less>
396 iterator contains( Q const& key, Less pred )
399 return node_to_iterator( find_at( head(), key, typename maker::template less_wrapper<Less>::type() ) );
402 template <typename Q, typename Less>
403 CDS_DEPRECATED("deprecated, use contains()")
404 iterator find_with( Q const& key, Less pred )
406 return contains( key, pred );
410 /// Check if the list is empty
413 return base_class::empty();
416 /// Returns list's item count
418 The value returned depends on item counter provided by \p Traits. For \p atomicity::empty_item_counter,
419 this function always returns 0.
421 @note Even if you use real item counter and it returns 0, this fact does not mean that the list
422 is empty. To check list emptyness use \p empty() method.
426 return base_class::size();
429 /// Returns const reference to internal statistics
430 stat const& statistics() const
432 return base_class::statistics();
443 static value_type& node_to_value( node_type& n )
448 iterator insert_node( node_type * pNode )
450 return node_to_iterator( insert_node_at( head(), pNode ));
453 node_type * insert_node_at( head_type& refHead, node_type * pNode )
455 assert( pNode != nullptr );
456 scoped_node_ptr p(pNode);
457 if ( base_class::insert_at( refHead, *pNode ))
463 template <typename Q>
464 node_type * insert_at( head_type& refHead, const Q& val )
466 return insert_node_at( refHead, alloc_node( val ));
469 template <typename Q>
470 std::pair< node_type *, bool > update_at( head_type& refHead, const Q& val, bool bAllowInsert )
472 scoped_node_ptr pNode( alloc_node( val ));
473 node_type * pItemFound = nullptr;
475 std::pair<bool, bool> ret = base_class::update_at( refHead, *pNode,
476 [&pItemFound](bool, node_type& item, node_type&) { pItemFound = &item; },
481 return std::make_pair( pItemFound, ret.second );
484 template <typename... Args>
485 node_type * emplace_at( head_type& refHead, Args&&... args )
487 return insert_node_at( refHead, alloc_node( std::forward<Args>(args)...));
490 template <typename Q, typename Compare>
491 node_type * find_at( head_type& refHead, Q const& key, Compare cmp )
493 return base_class::find_at( refHead, key, cmp );
498 }} // namespace cds::container
500 #endif // #ifndef CDSLIB_CONTAINER_MICHAEL_LIST_NOGC_H