Removed redundant functions
[libcds.git] / cds / intrusive / impl / iterable_list.h
1 /*
2     This file is a part of libcds - Concurrent Data Structures library
3
4     (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016
5
6     Source code repo: http://github.com/khizmax/libcds/
7     Download: http://sourceforge.net/projects/libcds/files/
8     
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions are met:
11
12     * Redistributions of source code must retain the above copyright notice, this
13       list of conditions and the following disclaimer.
14
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.
18
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.
29 */
30
31 #ifndef CDSLIB_INTRUSIVE_IMPL_ITERABLE_LIST_H
32 #define CDSLIB_INTRUSIVE_IMPL_ITERABLE_LIST_H
33
34 #include <cds/intrusive/details/iterable_list_base.h>
35 #include <cds/details/make_const_type.h>
36
37 namespace cds { namespace intrusive {
38
39     /// Iterable lock-free ordered single-linked list
40     /** @ingroup cds_intrusive_list
41         \anchor cds_intrusive_IterableList_hp
42
43         This lock-free list implementation supports thread-safe iterators.
44         Unlike \p cds::intrusive::MichaelList the iterable list does not require
45         any hook in \p T to be stored in the list.
46
47         Usually, ordered single-linked list is used as a building block for the hash table implementation.
48         Iterable list is suitable for almost append-only hash table because the list doesn't delete
49         its internal node when erasing a key but it is marked them as empty to be reused in the future.
50         However, plenty of empty nodes degrades performance.
51         Separation of internal nodes and user data implies the need for an allocator for internal node
52         so the iterable list is not fully intrusive. Nevertheless, if you need thread-safe iterator,
53         the iterable list is good choice.
54
55         The complexity of searching is <tt>O(N)</tt>.
56
57         Template arguments:
58         - \p GC - Garbage collector used.
59         - \p T - type to be stored in the list.
60         - \p Traits - type traits, default is \p iterable_list::traits. It is possible to declare option-based
61              list with \p cds::intrusive::iterable_list::make_traits metafunction:
62             For example, the following traits-based declaration of \p gc::HP iterable list
63             \code
64             #include <cds/intrusive/iterable_list_hp.h>
65             // Declare item stored in your list
66             struct foo
67             {
68                 int nKey;
69                 // .... other data
70             };
71
72             // Declare comparator for the item
73             struct my_compare {
74                 int operator()( foo const& i1, foo const& i2 ) const
75                 {
76                     return i1.nKey - i2.nKey;
77                 }
78             };
79
80             // Declare traits
81             struct my_traits: public cds::intrusive::iterable_list::traits
82             {
83                 typedef my_compare compare;
84             };
85
86             // Declare list
87             typedef cds::intrusive::IterableList< cds::gc::HP, foo, my_traits > list_type;
88             \endcode
89             is equivalent for the following option-based list
90             \code
91             #include <cds/intrusive/iterable_list_hp.h>
92
93             // foo struct and my_compare are the same
94
95             // Declare option-based list
96             typedef cds::intrusive::IterableList< cds::gc::HP, foo,
97                 typename cds::intrusive::iterable_list::make_traits<
98                     cds::intrusive::opt::compare< my_compare >     // item comparator option
99                 >::type
100             > option_list_type;
101             \endcode
102
103         \par Usage
104         There are different specializations of this template for each garbage collecting schema.
105         You should select GC you want and include appropriate .h-file:
106         - for \p gc::HP: <tt> <cds/intrusive/iterable_list_hp.h> </tt>
107         - for \p gc::DHP: <tt> <cds/intrusive/iterable_list_dhp.h> </tt>
108         - for \ref cds_urcu_gc "RCU type" - see \ref cds_intrusive_IterableList_rcu "RCU-based IterableList"
109     */
110     template <
111         class GC
112         ,typename T
113 #ifdef CDS_DOXYGEN_INVOKED
114         ,class Traits = iterable_list::traits
115 #else
116         ,class Traits
117 #endif
118     >
119     class IterableList
120     {
121     public:
122         typedef T       value_type; ///< type of value stored in the list
123         typedef Traits  traits;     ///< Traits template parameter
124
125         typedef iterable_list::node< value_type > node_type; ///< node type
126
127 #   ifdef CDS_DOXYGEN_INVOKED
128         typedef implementation_defined key_comparator  ;    ///< key comparison functor based on opt::compare and opt::less option setter.
129 #   else
130         typedef typename opt::details::make_comparator< value_type, traits >::type key_comparator;
131 #   endif
132
133         typedef typename traits::disposer  disposer; ///< disposer for \p value_type
134
135         typedef GC  gc;   ///< Garbage collector
136         typedef typename traits::back_off       back_off;       ///< back-off strategy
137         typedef typename traits::item_counter   item_counter;   ///< Item counting policy used
138         typedef typename traits::memory_model   memory_model;   ///< Memory ordering. See \p cds::opt::memory_model option
139         typedef typename traits::node_allocator node_allocator; ///< Node allocator
140         typedef typename traits::stat           stat;           ///< Internal statistics
141
142         typedef typename gc::template guarded_ptr< value_type > guarded_ptr; ///< Guarded pointer
143
144         static CDS_CONSTEXPR const size_t c_nHazardPtrCount = 2; ///< Count of hazard pointer required for the algorithm
145
146         //@cond
147         // Rebind traits (split-list support)
148         template <typename... Options>
149         struct rebind_traits {
150             typedef IterableList<
151                 gc
152                 , value_type
153                 , typename cds::opt::make_options< traits, Options...>::type
154             > type;
155         };
156
157         // Stat selector
158         template <typename Stat>
159         using select_stat_wrapper = iterable_list::select_stat_wrapper< Stat >;
160         //@endcond
161
162     protected:
163         typedef atomics::atomic< node_type* > atomic_node_ptr;  ///< Atomic node pointer
164         typedef atomic_node_ptr               auxiliary_head;   ///< Auxiliary head type (for split-list support)
165
166         atomic_node_ptr m_pHead;        ///< Head pointer
167         item_counter    m_ItemCounter;  ///< Item counter
168         mutable stat    m_Stat;         ///< Internal statistics
169
170         //@cond
171         typedef cds::details::Allocator< node_type, node_allocator > cxx_node_allocator;
172
173         /// Position pointer for item search
174         struct position {
175             atomic_node_ptr * pHead; ///< Previous node (pointer to pPrev->next or to m_pHead)
176             node_type *       pPrev;  ///< Previous node
177             node_type *       pCur;   ///< Current node
178
179             value_type *      pFound; ///< Value of \p pCur->data, valid only if data found
180             typename gc::Guard guard; ///< guard for \p pFound
181         };
182         //@endcond
183
184     protected:
185         //@cond
186         template <bool IsConst>
187         class iterator_type
188         {
189             friend class IterableList;
190
191         protected:
192             node_type*  m_pNode;
193             value_type* m_pVal;
194             typename gc::Guard  m_Guard; // for m_pVal
195
196             void next()
197             {
198                 while ( m_pNode ) {
199                     m_pNode = m_pNode->next.load( memory_model::memory_order_relaxed );
200                     if ( !m_pNode )
201                         break;
202                     m_pVal = m_Guard.protect( m_pNode->data );
203                     if ( m_pVal )
204                         break;
205                 }
206             }
207
208             explicit iterator_type( atomic_node_ptr const& pNode )
209                 : m_pNode( pNode.load( memory_model::memory_order_relaxed ))
210                 , m_pVal( nullptr )
211             {
212                 if ( m_pNode ) {
213                     m_pVal = m_Guard.protect( m_pNode->data );
214                     if ( !m_pVal )
215                         next();
216                 }
217             }
218
219             iterator_type( node_type* pNode, value_type* pVal )
220                 : m_pNode( pNode )
221                 , m_pVal( pVal )
222             {
223                 if ( m_pNode ) {
224                     assert( pVal != nullptr );
225                     m_Guard.assign( pVal );
226                 }
227             }
228
229         public:
230             typedef typename cds::details::make_const_type<value_type, IsConst>::pointer   value_ptr;
231             typedef typename cds::details::make_const_type<value_type, IsConst>::reference value_ref;
232
233             iterator_type()
234                 : m_pNode( nullptr )
235                 , m_pVal( nullptr )
236             {}
237
238             iterator_type( iterator_type const& src )
239                 : m_pNode( src.m_pNode )
240                 , m_pVal( src.m_pVal )
241             {
242                 m_Guard.assign( m_pVal );
243             }
244
245             value_ptr operator ->() const
246             {
247                 return m_pVal;
248             }
249
250             value_ref operator *() const
251             {
252                 assert( m_pVal != nullptr );
253                 return *m_pVal;
254             }
255
256             /// Pre-increment
257             iterator_type& operator ++()
258             {
259                 next();
260                 return *this;
261             }
262
263             iterator_type& operator = (iterator_type const& src)
264             {
265                 m_pNode = src.m_pNode;
266                 m_pVal = src.m_pVal;
267                 m_Guard.assign( m_pVal );
268                 return *this;
269             }
270
271             template <bool C>
272             bool operator ==(iterator_type<C> const& i ) const
273             {
274                 return m_pNode == i.m_pNode;
275             }
276             template <bool C>
277             bool operator !=(iterator_type<C> const& i ) const
278             {
279                 return m_pNode != i.m_pNode;
280             }
281         };
282         //@endcond
283
284     public:
285     ///@name Thread-safe forward iterators
286     //@{
287         /// Forward iterator
288         /**
289             The forward iterator for iterable list has some features:
290             - it has no post-increment operator
291             - to protect the value, the iterator contains a GC-specific guard.
292               For some GC (like as \p gc::HP), a guard is a limited resource per thread, so an exception (or assertion) "no free guard"
293               may be thrown if the limit of guard count per thread is exceeded.
294             - The iterator cannot be moved across thread boundary since it contains thread-private GC's guard.
295             - Iterator is thread-safe: even if the element the iterator points to is removed, the iterator stays valid because
296               it contains the guard keeping the value from to be recycled.
297
298             The iterator interface:
299             \code
300             class iterator {
301             public:
302                 // Default constructor
303                 iterator();
304
305                 // Copy construtor
306                 iterator( iterator const& src );
307
308                 // Dereference operator
309                 value_type * operator ->() const;
310
311                 // Dereference operator
312                 value_type& operator *() const;
313
314                 // Preincrement operator
315                 iterator& operator ++();
316
317                 // Assignment operator
318                 iterator& operator = (iterator const& src);
319
320                 // Equality operators
321                 bool operator ==(iterator const& i ) const;
322                 bool operator !=(iterator const& i ) const;
323             };
324             \endcode
325
326             @note For two iterators pointed to the same element the value can be different;
327             this code
328             \code
329                 if ( it1 == it2 )
330                     assert( &(*it1) == &(*it2) );
331             \endcode
332             can throw assertion. The point is that the iterator stores the value of element which can be modified later by other thread.
333             The guard inside the iterator prevents recycling that value so the iterator's value remains valid even after such changing.
334             Other iterator can observe modified value of the element.
335         */
336         typedef iterator_type<false>    iterator;
337         /// Const forward iterator
338         /**
339             For iterator's features and requirements see \ref iterator
340         */
341         typedef iterator_type<true>     const_iterator;
342
343         /// Returns a forward iterator addressing the first element in a list
344         /**
345             For empty list \code begin() == end() \endcode
346         */
347         iterator begin()
348         {
349             return iterator( m_pHead );
350         }
351
352         /// Returns an iterator that addresses the location succeeding the last element in a list
353         /**
354             Do not use the value returned by <tt>end</tt> function to access any item.
355             Internally, <tt>end</tt> returning value equals to \p nullptr.
356
357             The returned value can be used only to control reaching the end of the list.
358             For empty list <tt>begin() == end()</tt>
359         */
360         iterator end()
361         {
362             return iterator();
363         }
364
365         /// Returns a forward const iterator addressing the first element in a list
366         const_iterator cbegin() const
367         {
368             return const_iterator( m_pHead );
369         }
370
371         /// Returns a forward const iterator addressing the first element in a list
372         const_iterator begin() const
373         {
374             return const_iterator( m_pHead );
375         }
376
377         /// Returns an const iterator that addresses the location succeeding the last element in a list
378         const_iterator end() const
379         {
380             return const_iterator();
381         }
382
383         /// Returns an const iterator that addresses the location succeeding the last element in a list
384         const_iterator cend() const
385         {
386             return const_iterator();
387         }
388     //@}
389
390     public:
391         /// Default constructor initializes empty list
392         IterableList()
393             : m_pHead( nullptr )
394         {}
395
396         //@cond
397         template <typename Stat, typename = std::enable_if<std::is_same<stat, iterable_list::wrapped_stat<Stat>>::value >>
398         explicit IterableList( Stat& st )
399             : m_pHead( nullptr )
400             , m_Stat( st )
401         {}
402         //@endcond
403
404         /// Destroys the list object
405         ~IterableList()
406         {
407             destroy();
408         }
409
410         /// Inserts new node
411         /**
412             The function inserts \p val into the list if the list does not contain
413             an item with key equal to \p val.
414
415             Returns \p true if \p val has been linked to the list, \p false otherwise.
416         */
417         bool insert( value_type& val )
418         {
419             return insert_at( m_pHead, val );
420         }
421
422         /// Inserts new node
423         /**
424             This function is intended for derived non-intrusive containers.
425
426             The function allows to split new item creating into two part:
427             - create item with key only
428             - insert new item into the list
429             - if inserting is success, calls  \p f functor to initialize value-field of \p val.
430
431             The functor signature is:
432             \code
433                 void func( value_type& val );
434             \endcode
435             where \p val is the item inserted. User-defined functor \p f should guarantee that during changing
436             \p val no any other changes could be made on this list's item by concurrent threads.
437             The user-defined functor is called only if the inserting is success.
438
439             @warning See \ref cds_intrusive_item_creating "insert item troubleshooting"
440         */
441         template <typename Func>
442         bool insert( value_type& val, Func f )
443         {
444             return insert_at( m_pHead, val, f );
445         }
446
447         /// Updates the node
448         /**
449             The operation performs inserting or changing data with lock-free manner.
450
451             If the item \p val is not found in the list, then \p val is inserted
452             iff \p bInsert is \p true.
453             Otherwise, the current element is changed to \p val, the element will be retired later
454             by call \p Traits::disposer.
455             The functor \p func is called after inserting or replacing, it signature is:
456             \code
457                 void func( value_type& val, value_type * old );
458             \endcode
459             where
460             - \p val - argument \p val passed into the \p %update() function
461             - \p old - old value that will be retired. If new item has been inserted then \p old is \p nullptr.
462
463             Returns std::pair<bool, bool> where \p first is \p true if operation is successful,
464             \p second is \p true if \p val has been added or \p false if the item with that key
465             already in the list.
466         */
467         template <typename Func>
468         std::pair<bool, bool> update( value_type& val, Func func, bool bInsert = true )
469         {
470             return update_at( m_pHead, val, func, bInsert );
471         }
472
473         /// Insert or update
474         /**
475             The operation performs inserting or updating data with lock-free manner.
476
477             If the item \p val is not found in the list, then \p val is inserted
478             iff \p bInsert is \p true.
479             Otherwise, the current element is changed to \p val, the old element will be retired later
480             by call \p Traits::disposer.
481
482             Returns std::pair<bool, bool> where \p first is \p true if operation is successful,
483             \p second is \p true if \p val has been added or \p false if the item with that key
484             already in the list.
485         */
486         std::pair<bool, bool> upsert( value_type& val, bool bInsert = true )
487         {
488             return update_at( m_pHead, val, []( value_type&, value_type* ) {}, bInsert );
489         }
490
491         /// Unlinks the item \p val from the list
492         /**
493             The function searches the item \p val in the list and unlinks it from the list
494             if it is found and it is equal to \p val.
495
496             Difference between \p erase() and \p %unlink(): \p %erase() finds <i>a key</i>
497             and deletes the item found. \p %unlink() finds an item by key and deletes it
498             only if \p val is an item of the list, i.e. the pointer to item found
499             is equal to <tt> &val </tt>.
500
501             \p disposer specified in \p Traits is called for deleted item.
502
503             The function returns \p true if success and \p false otherwise.
504         */
505         bool unlink( value_type& val )
506         {
507             return unlink_at( m_pHead, val );
508         }
509
510         /// Deletes the item from the list
511         /** \anchor cds_intrusive_IterableList_hp_erase_val
512             The function searches an item with key equal to \p key in the list,
513             unlinks it from the list, and returns \p true.
514             If \p key is not found the function return \p false.
515
516             \p disposer specified in \p Traits is called for deleted item.
517         */
518         template <typename Q>
519         bool erase( Q const& key )
520         {
521             return erase_at( m_pHead, key, key_comparator());
522         }
523
524         /// Deletes the item from the list using \p pred predicate for searching
525         /**
526             The function is an analog of \ref cds_intrusive_IterableList_hp_erase_val "erase(Q const&)"
527             but \p pred is used for key comparing.
528             \p Less functor has the interface like \p std::less.
529             \p pred must imply the same element order as the comparator used for building the list.
530
531             \p disposer specified in \p Traits is called for deleted item.
532         */
533         template <typename Q, typename Less>
534         bool erase_with( Q const& key, Less pred )
535         {
536             CDS_UNUSED( pred );
537             return erase_at( m_pHead, key, cds::opt::details::make_comparator_from_less<Less>());
538         }
539
540         /// Deletes the item from the list
541         /** \anchor cds_intrusive_IterableList_hp_erase_func
542             The function searches an item with key equal to \p key in the list,
543             call \p func functor with item found, unlinks it from the list, and returns \p true.
544             The \p Func interface is
545             \code
546             struct functor {
547                 void operator()( value_type const& item );
548             };
549             \endcode
550             If \p key is not found the function return \p false, \p func is not called.
551
552             \p disposer specified in \p Traits is called for deleted item.
553         */
554         template <typename Q, typename Func>
555         bool erase( Q const& key, Func func )
556         {
557             return erase_at( m_pHead, key, key_comparator(), func );
558         }
559
560         /// Deletes the item from the list using \p pred predicate for searching
561         /**
562             The function is an analog of \ref cds_intrusive_IterableList_hp_erase_func "erase(Q const&, Func)"
563             but \p pred is used for key comparing.
564             \p Less functor has the interface like \p std::less.
565             \p pred must imply the same element order as the comparator used for building the list.
566
567             \p disposer specified in \p Traits is called for deleted item.
568         */
569         template <typename Q, typename Less, typename Func>
570         bool erase_with( Q const& key, Less pred, Func f )
571         {
572             CDS_UNUSED( pred );
573             return erase_at( m_pHead, key, cds::opt::details::make_comparator_from_less<Less>(), f );
574         }
575
576         /// Extracts the item from the list with specified \p key
577         /** \anchor cds_intrusive_IterableList_hp_extract
578             The function searches an item with key equal to \p key,
579             unlinks it from the list, and returns it as \p guarded_ptr.
580             If \p key is not found returns an empty guarded pointer.
581
582             Note the compare functor should accept a parameter of type \p Q that can be not the same as \p value_type.
583
584             The \ref disposer specified in \p Traits class template parameter is called automatically
585             by garbage collector \p GC when returned \ref guarded_ptr object will be destroyed or released.
586             @note Each \p guarded_ptr object uses the GC's guard that can be limited resource.
587
588             Usage:
589             \code
590             typedef cds::intrusive::IterableList< cds::gc::HP, foo, my_traits >  ord_list;
591             ord_list theList;
592             // ...
593             {
594                 ord_list::guarded_ptr gp(theList.extract( 5 ));
595                 if ( gp ) {
596                     // Deal with gp
597                     // ...
598                 }
599                 // Destructor of gp releases internal HP guard
600             }
601             \endcode
602         */
603         template <typename Q>
604         guarded_ptr extract( Q const& key )
605         {
606             guarded_ptr gp;
607             extract_at( m_pHead, gp.guard(), key, key_comparator());
608             return gp;
609         }
610
611         /// Extracts the item using compare functor \p pred
612         /**
613             The function is an analog of \ref cds_intrusive_IterableList_hp_extract "extract(Q const&)"
614             but \p pred predicate is used for key comparing.
615
616             \p Less functor has the semantics like \p std::less but should take arguments of type \ref value_type and \p Q
617             in any order.
618             \p pred must imply the same element order as the comparator used for building the list.
619         */
620         template <typename Q, typename Less>
621         guarded_ptr extract_with( Q const& key, Less pred )
622         {
623             CDS_UNUSED( pred );
624             guarded_ptr gp;
625             extract_at( m_pHead, gp.guard(), key, cds::opt::details::make_comparator_from_less<Less>());
626             return gp;
627         }
628
629         /// Finds \p key in the list
630         /** \anchor cds_intrusive_IterableList_hp_find_func
631             The function searches the item with key equal to \p key and calls the functor \p f for item found.
632             The interface of \p Func functor is:
633             \code
634             struct functor {
635                 void operator()( value_type& item, Q& key );
636             };
637             \endcode
638             where \p item is the item found, \p key is the \p %find() function argument.
639
640             The functor may change non-key fields of \p item. Note that the function is only guarantee
641             that \p item cannot be disposed during functor is executing.
642             The function does not serialize simultaneous access to the \p item. If such access is
643             possible you must provide your own synchronization schema to keep out unsafe item modifications.
644
645             The function returns \p true if \p val is found, \p false otherwise.
646         */
647         template <typename Q, typename Func>
648         bool find( Q& key, Func f ) const
649         {
650             return find_at( m_pHead, key, key_comparator(), f );
651         }
652         //@cond
653         template <typename Q, typename Func>
654         bool find( Q const& key, Func f ) const
655         {
656             return find_at( m_pHead, key, key_comparator(), f );
657         }
658         //@endcond
659
660         /// Finds \p key in the list and returns iterator pointed to the item found
661         /**
662             If \p key is not found the function returns \p end().
663         */
664         template <typename Q>
665         iterator find( Q const& key ) const
666         {
667             return find_iterator_at( m_pHead, key, key_comparator());
668         }
669
670         /// Finds the \p key using \p pred predicate for searching
671         /**
672             The function is an analog of \ref cds_intrusive_IterableList_hp_find_func "find(Q&, Func)"
673             but \p pred is used for key comparing.
674             \p Less functor has the interface like \p std::less.
675             \p pred must imply the same element order as the comparator used for building the list.
676         */
677         template <typename Q, typename Less, typename Func>
678         bool find_with( Q& key, Less pred, Func f ) const
679         {
680             CDS_UNUSED( pred );
681             return find_at( m_pHead, key, cds::opt::details::make_comparator_from_less<Less>(), f );
682         }
683         //@cond
684         template <typename Q, typename Less, typename Func>
685         bool find_with( Q const& key, Less pred, Func f ) const
686         {
687             CDS_UNUSED( pred );
688             return find_at( m_pHead, key, cds::opt::details::make_comparator_from_less<Less>(), f );
689         }
690         //@endcond
691
692         /// Finds \p key in the list using \p pred predicate for searching and returns iterator pointed to the item found
693         /**
694             The function is an analog of \p find(Q&) but \p pred is used for key comparing.
695             \p Less functor has the interface like \p std::less.
696             \p pred must imply the same element order as the comparator used for building the list.
697
698             If \p key is not found the function returns \p end().
699         */
700         template <typename Q, typename Less>
701         iterator find_with( Q const& key, Less pred ) const
702         {
703             CDS_UNUSED( pred );
704             return find_iterator_at( m_pHead, key, cds::opt::details::make_comparator_from_less<Less>());
705         }
706
707         /// Checks whether the list contains \p key
708         /**
709             The function searches the item with key equal to \p key
710             and returns \p true if it is found, and \p false otherwise.
711         */
712         template <typename Q>
713         bool contains( Q const& key ) const
714         {
715             return find_at( m_pHead, key, key_comparator());
716         }
717
718         /// Checks whether the list contains \p key using \p pred predicate for searching
719         /**
720             The function is an analog of <tt>contains( key )</tt> but \p pred is used for key comparing.
721             \p Less functor has the interface like \p std::less.
722             \p Less must imply the same element order as the comparator used for building the list.
723         */
724         template <typename Q, typename Less>
725         bool contains( Q const& key, Less pred ) const
726         {
727             CDS_UNUSED( pred );
728             return find_at( m_pHead, key, cds::opt::details::make_comparator_from_less<Less>());
729         }
730
731         /// Finds the \p key and return the item found
732         /** \anchor cds_intrusive_IterableList_hp_get
733             The function searches the item with key equal to \p key
734             and returns it as \p guarded_ptr.
735             If \p key is not found the function returns an empty guarded pointer.
736
737             The \ref disposer specified in \p Traits class template parameter is called
738             by garbage collector \p GC automatically when returned \ref guarded_ptr object
739             will be destroyed or released.
740             @note Each \p guarded_ptr object uses one GC's guard which can be limited resource.
741
742             Usage:
743             \code
744             typedef cds::intrusive::IterableList< cds::gc::HP, foo, my_traits >  ord_list;
745             ord_list theList;
746             // ...
747             {
748                 ord_list::guarded_ptr gp(theList.get( 5 ));
749                 if ( gp ) {
750                     // Deal with gp
751                     //...
752                 }
753                 // Destructor of guarded_ptr releases internal HP guard
754             }
755             \endcode
756
757             Note the compare functor specified for \p Traits template parameter
758             should accept a parameter of type \p Q that can be not the same as \p value_type.
759         */
760         template <typename Q>
761         guarded_ptr get( Q const& key ) const
762         {
763             guarded_ptr gp;
764             get_at( m_pHead, gp.guard(), key, key_comparator());
765             return gp;
766         }
767
768         /// Finds the \p key and return the item found
769         /**
770             The function is an analog of \ref cds_intrusive_IterableList_hp_get "get( Q const&)"
771             but \p pred is used for comparing the keys.
772
773             \p Less functor has the semantics like \p std::less but should take arguments of type \ref value_type and \p Q
774             in any order.
775             \p pred must imply the same element order as the comparator used for building the list.
776         */
777         template <typename Q, typename Less>
778         guarded_ptr get_with( Q const& key, Less pred ) const
779         {
780             CDS_UNUSED( pred );
781             guarded_ptr gp;
782             get_at( m_pHead, gp.guard(), key, cds::opt::details::make_comparator_from_less<Less>());
783             return gp;
784         }
785
786         /// Clears the list (thread safe, not atomic)
787         void clear()
788         {
789             position pos;
790             for ( pos.pCur = m_pHead.load( memory_model::memory_order_relaxed ); pos.pCur; pos.pCur = pos.pCur->next.load( memory_model::memory_order_relaxed )) {
791                 while ( true ) {
792                     pos.pFound = pos.guard.protect( pos.pCur->data );
793                     if ( !pos.pFound )
794                         break;
795                     if ( cds_likely( unlink_node( pos ))) {
796                         --m_ItemCounter;
797                         break;
798                     }
799                 }
800             }
801         }
802
803         /// Checks if the list is empty
804         /**
805             Emptiness is checked by item counting: if item count is zero then the set is empty.
806             Thus, if you need to use \p %empty() you should provide appropriate (non-empty) \p iterable_list::traits::item_counter
807             feature.
808         */
809         bool empty() const
810         {
811             return size() == 0;
812         }
813
814         /// Returns list's item count
815         /**
816             The value returned depends on item counter provided by \p iterable_list::traits::item_counter. For \p atomicity::empty_item_counter,
817             this function always returns 0.
818         */
819         size_t size() const
820         {
821             return m_ItemCounter.value();
822         }
823
824         /// Returns const reference to internal statistics
825         stat const& statistics() const
826         {
827             return m_Stat;
828         }
829
830     protected:
831         //@cond
832 #if 0
833         // split-list support
834         bool insert_aux_node( node_type * pNode )
835         {
836             return insert_aux_node( m_pHead, pNode );
837         }
838
839         // split-list support
840         bool insert_aux_node( atomic_node_ptr& refHead, node_type * pNode )
841         {
842             assert( pNode != nullptr );
843
844             // Hack: convert node_type to value_type.
845             // In principle, auxiliary node can be non-reducible to value_type
846             // We assume that comparator can correctly distinguish aux and regular node.
847             return insert_at( refHead, *node_traits::to_value_ptr( pNode ) );
848         }
849 #endif
850
851         bool insert_at( atomic_node_ptr& refHead, value_type& val )
852         {
853             position pos;
854
855             while ( true ) {
856                 if ( search( refHead, val, pos, key_comparator() )) {
857                     m_Stat.onInsertFailed();
858                     return false;
859                 }
860
861                 if ( link_node( &val, pos ) ) {
862                     ++m_ItemCounter;
863                     m_Stat.onInsertSuccess();
864                     return true;
865                 }
866
867                 m_Stat.onInsertRetry();
868             }
869         }
870
871         template <typename Func>
872         bool insert_at( atomic_node_ptr& refHead, value_type& val, Func f )
873         {
874             position pos;
875
876             typename gc::Guard guard;
877             guard.assign( &val );
878
879             while ( true ) {
880                 if ( search( refHead, val, pos, key_comparator() ) ) {
881                     m_Stat.onInsertFailed();
882                     return false;
883                 }
884
885                 if ( link_node( &val, pos ) ) {
886                     f( val );
887                     ++m_ItemCounter;
888                     m_Stat.onInsertSuccess();
889                     return true;
890                 }
891
892                 m_Stat.onInsertRetry();
893             }
894         }
895
896         template <typename Func>
897         std::pair<bool, bool> update_at( atomic_node_ptr& refHead, value_type& val, Func func, bool bInsert )
898         {
899             position pos;
900
901             typename gc::Guard guard;
902             guard.assign( &val );
903
904             while ( true ) {
905                 if ( search( refHead, val, pos, key_comparator() ) ) {
906                     // try to replace pCur->data with val
907                     assert( pos.pFound != nullptr );
908                     assert( key_comparator()(*pos.pFound, val) == 0 );
909
910                     if ( cds_likely( pos.pCur->data.compare_exchange_strong( pos.pFound, &val, memory_model::memory_order_release, atomics::memory_order_relaxed ))) {
911                         if ( pos.pFound != &val ) {
912                             retire_data( pos.pFound );
913                             func( val, pos.pFound );
914                         }
915                         m_Stat.onUpdateExisting();
916                         return std::make_pair( true, false );
917                     }
918                 }
919                 else {
920                     if ( !bInsert ) {
921                         m_Stat.onUpdateFailed();
922                         return std::make_pair( false, false );
923                     }
924
925                     if ( link_node( &val, pos )) {
926                         func( val, static_cast<value_type*>( nullptr ));
927                         ++m_ItemCounter;
928                         m_Stat.onUpdateNew();
929                         return std::make_pair( true, true );
930                     }
931                 }
932
933                 m_Stat.onUpdateRetry();
934             }
935         }
936
937         bool unlink_at( atomic_node_ptr& refHead, value_type& val )
938         {
939             position pos;
940
941             back_off bkoff;
942             while ( search( refHead, val, pos, key_comparator())) {
943                 if ( pos.pFound == &val ) {
944                     if ( unlink_node( pos )) {
945                         --m_ItemCounter;
946                         m_Stat.onEraseSuccess();
947                         return true;
948                     }
949                     else
950                         bkoff();
951                 }
952                 else
953                     break;
954
955                 m_Stat.onEraseRetry();
956             }
957
958             m_Stat.onEraseFailed();
959             return false;
960         }
961
962         template <typename Q, typename Compare, typename Func>
963         bool erase_at( atomic_node_ptr& refHead, const Q& val, Compare cmp, Func f, position& pos )
964         {
965             back_off bkoff;
966             while ( search( refHead, val, pos, cmp )) {
967                 if ( unlink_node( pos )) {
968                     f( *pos.pFound );
969                     --m_ItemCounter;
970                     m_Stat.onEraseSuccess();
971                     return true;
972                 }
973                 else
974                     bkoff();
975
976                 m_Stat.onEraseRetry();
977             }
978
979             m_Stat.onEraseFailed();
980             return false;
981         }
982
983         template <typename Q, typename Compare, typename Func>
984         bool erase_at( atomic_node_ptr& refHead, const Q& val, Compare cmp, Func f )
985         {
986             position pos;
987             return erase_at( refHead, val, cmp, f, pos );
988         }
989
990         template <typename Q, typename Compare>
991         bool erase_at( atomic_node_ptr& refHead, Q const& val, Compare cmp )
992         {
993             position pos;
994             return erase_at( refHead, val, cmp, [](value_type const&){}, pos );
995         }
996
997         template <typename Q, typename Compare>
998         bool extract_at( atomic_node_ptr& refHead, typename guarded_ptr::native_guard& dest, Q const& val, Compare cmp )
999         {
1000             position pos;
1001             back_off bkoff;
1002             while ( search( refHead, val, pos, cmp )) {
1003                 if ( unlink_node( pos )) {
1004                     dest.set( pos.pFound );
1005                     --m_ItemCounter;
1006                     m_Stat.onEraseSuccess();
1007                     return true;
1008                 }
1009                 else
1010                     bkoff();
1011
1012                 m_Stat.onEraseRetry();
1013             }
1014
1015             m_Stat.onEraseFailed();
1016             return false;
1017         }
1018
1019         template <typename Q, typename Compare>
1020         bool find_at( atomic_node_ptr const& refHead, Q const& val, Compare cmp ) const
1021         {
1022             position pos;
1023             if ( search( refHead, val, pos, cmp ) ) {
1024                 m_Stat.onFindSuccess();
1025                 return true;
1026             }
1027
1028             m_Stat.onFindFailed();
1029             return false;
1030         }
1031
1032         template <typename Q, typename Compare, typename Func>
1033         bool find_at( atomic_node_ptr const& refHead, Q& val, Compare cmp, Func f ) const
1034         {
1035             position pos;
1036             if ( search( refHead, val, pos, cmp )) {
1037                 assert( pos.pFound != nullptr );
1038                 f( *pos.pFound, val );
1039                 m_Stat.onFindSuccess();
1040                 return true;
1041             }
1042
1043             m_Stat.onFindFailed();
1044             return false;
1045         }
1046
1047         template <typename Q, typename Compare>
1048         iterator find_iterator_at( atomic_node_ptr const& refHead, Q const& val, Compare cmp ) const
1049         {
1050             position pos;
1051             if ( search( refHead, val, pos, cmp )) {
1052                 assert( pos.pCur != nullptr );
1053                 assert( pos.pFound != nullptr );
1054                 m_Stat.onFindSuccess();
1055                 return iterator( pos.pCur, pos.pFound );
1056             }
1057
1058             m_Stat.onFindFailed();
1059             return iterator{};
1060         }
1061
1062         template <typename Q, typename Compare>
1063         bool get_at( atomic_node_ptr const& refHead, typename guarded_ptr::native_guard& guard, Q const& val, Compare cmp ) const
1064         {
1065             position pos;
1066             if ( search( refHead, val, pos, cmp )) {
1067                 guard.set( pos.pFound );
1068                 m_Stat.onFindSuccess();
1069                 return true;
1070             }
1071
1072             m_Stat.onFindFailed();
1073             return false;
1074         }
1075         //@endcond
1076
1077     protected:
1078
1079         //@cond
1080         template <typename Q, typename Compare >
1081         bool search( atomic_node_ptr const& refHead, const Q& val, position& pos, Compare cmp ) const
1082         {
1083             atomic_node_ptr* pHead = const_cast<atomic_node_ptr*>( &refHead );
1084             node_type * pPrev = nullptr;
1085
1086             while ( true ) {
1087                 node_type * pCur = pHead->load( memory_model::memory_order_relaxed );
1088
1089                 if ( pCur == nullptr ) {
1090                     // end-of-list
1091                     pos.pHead = pHead;
1092                     pos.pPrev = pPrev;
1093                     pos.pCur = nullptr;
1094                     pos.pFound = nullptr;
1095                     return false;
1096                 }
1097
1098                 value_type * pVal = pos.guard.protect( pCur->data );
1099
1100                 if ( pVal ) {
1101                     int nCmp = cmp( *pVal, val );
1102                     if ( nCmp >= 0 ) {
1103                         pos.pHead = pHead;
1104                         pos.pPrev = pPrev;
1105                         pos.pCur = pCur;
1106                         pos.pFound = pVal;
1107                         return nCmp == 0;
1108                     }
1109                 }
1110
1111                 pPrev = pCur;
1112                 pHead = &( pCur->next );
1113             }
1114         }
1115         //@endcond
1116
1117     private:
1118         //@cond
1119         node_type * alloc_node( value_type * pVal )
1120         {
1121             m_Stat.onNodeCreated();
1122             return cxx_node_allocator().New( pVal );
1123         }
1124
1125         void delete_node( node_type * pNode )
1126         {
1127             m_Stat.onNodeRemoved();
1128             cxx_node_allocator().Delete( pNode );
1129         }
1130
1131         static void retire_data( value_type * pVal )
1132         {
1133             assert( pVal != nullptr );
1134             gc::template retire<disposer>( pVal );
1135         }
1136
1137         void destroy()
1138         {
1139             node_type * pNode = m_pHead.load( memory_model::memory_order_relaxed );
1140             while ( pNode ) {
1141                 value_type * pVal = pNode->data.load( memory_model::memory_order_relaxed );
1142                 if ( pVal )
1143                     retire_data( pVal );
1144                 node_type * pNext = pNode->next.load( memory_model::memory_order_relaxed );
1145                 delete_node( pNode );
1146                 pNode = pNext;
1147             }
1148         }
1149
1150         bool link_node( value_type * pVal, position& pos )
1151         {
1152             if ( pos.pPrev ) {
1153                 if ( pos.pPrev->data.load( memory_model::memory_order_relaxed ) == nullptr ) {
1154                     // reuse pPrev
1155                     value_type * p = nullptr;
1156                     return pos.pPrev->data.compare_exchange_strong( p, pVal, memory_model::memory_order_release, atomics::memory_order_relaxed );
1157                 }
1158                 else {
1159                     // insert new node between pos.pPrev and pos.pCur
1160                     node_type * pNode = alloc_node( pVal );
1161                     pNode->next.store( pos.pCur, memory_model::memory_order_relaxed );
1162
1163                     if ( cds_likely( pos.pPrev->next.compare_exchange_strong( pos.pCur, pNode, memory_model::memory_order_release, atomics::memory_order_relaxed )))
1164                         return true;
1165
1166                     delete_node( pNode );
1167                 }
1168             }
1169             else {
1170                 node_type * pNode = alloc_node( pVal );
1171                 pNode->next.store( pos.pCur, memory_model::memory_order_relaxed );
1172                 if ( cds_likely( pos.pHead->compare_exchange_strong( pos.pCur, pNode, memory_model::memory_order_release, atomics::memory_order_relaxed ) ) )
1173                     return true;
1174
1175                 delete_node( pNode );
1176             }
1177             return false;
1178         }
1179
1180         static bool unlink_node( position& pos )
1181         {
1182             assert( pos.pCur != nullptr );
1183             assert( pos.pFound != nullptr );
1184
1185             if ( pos.pCur->data.compare_exchange_strong( pos.pFound, nullptr, memory_model::memory_order_acquire, atomics::memory_order_relaxed ) ) {
1186                 retire_data( pos.pFound );
1187                 return true;
1188             }
1189             return false;
1190         }
1191
1192         //@endcond
1193     };
1194 }} // namespace cds::intrusive
1195
1196 #endif // #ifndef CDSLIB_INTRUSIVE_IMPL_ITERABLE_LIST_H