3 #ifndef __CDS_CONTAINER_IMPL_LAZY_KVLIST_H
4 #define __CDS_CONTAINER_IMPL_LAZY_KVLIST_H
7 #include <cds/container/details/guarded_ptr_cast.h>
9 namespace cds { namespace container {
11 /// Lazy ordered list (key-value pair)
12 /** @ingroup cds_nonintrusive_list
13 \anchor cds_nonintrusive_LazyKVList_gc
15 This is key-value variation of non-intrusive LazyList.
16 Like standard container, this implementation split a value stored into two part -
17 constant key and alterable value.
19 Usually, ordered single-linked list is used as a building block for the hash table implementation.
20 The complexity of searching is <tt>O(N)</tt>.
23 - \p GC - garbage collector
24 - \p Key - key type of an item to be stored in the list. It should be copy-constructible
25 - \p Value - value type to be stored in the list
26 - \p Traits - type traits, default is \p lazy_list::traits
27 It is possible to declare option-based list with cds::container::lazy_list::make_traits metafunction istead of \p Traits template
28 argument. For example, the following traits-based declaration of \p gc::HP lazy list
30 #include <cds/container/lazy_kvlist_hp.h>
31 // Declare comparator for the item
33 int operator ()( int i1, int i2 )
40 struct my_traits: public cds::container::lazy_list::traits
42 typedef my_compare compare;
45 // Declare traits-based list
46 typedef cds::container::LazyKVList< cds::gc::HP, int, int, my_traits > traits_based_list;
48 is equal to the following option-based list
50 #include <cds/container/lazy_kvlist_hp.h>
52 // my_compare is the same
54 // Declare option-based list
55 typedef cds::container::LazyKVList< cds::gc::HP, int, int,
56 typename cds::container::lazy_list::make_traits<
57 cds::container::opt::compare< my_compare > // item comparator option
63 There are different specializations of this template for each garbage collecting schema used.
64 You should include appropriate .h-file depending on GC you are using:
65 - for \p gc::HP: <tt> <cds/container/lazy_kvlist_hp.h> </tt>
66 - for \p gc::DHP: <tt> <cds/container/lazy_kvlist_dhp.h> </tt>
67 - for \ref cds_urcu_desc "RCU": <tt> <cds/container/lazy_kvlist_rcu.h> </tt>
68 - for \p gc::nogc: <tt> <cds/container/lazy_kvlist_nogc.h> </tt>
74 #ifdef CDS_DOXYGEN_INVOKED
75 typename Traits = lazy_list::traits
81 #ifdef CDS_DOXYGEN_INVOKED
82 protected intrusive::LazyList< GC, implementation_defined, Traits >
84 protected details::make_lazy_kvlist< GC, Key, Value, Traits >::type
88 typedef details::make_lazy_kvlist< GC, Key, Value, Traits > maker;
89 typedef typename maker::type base_class;
93 typedef GC gc; ///< Garbage collector
94 #ifdef CDS_DOXYGEN_INVOKED
95 typedef Key key_type ; ///< Key type
96 typedef Value mapped_type ; ///< Type of value stored in the list
97 typedef std::pair<key_type const, mapped_type> value_type ; ///< key/value pair stored in the list
99 typedef typename maker::key_type key_type;
100 typedef typename maker::mapped_type mapped_type;
101 typedef typename maker::value_type value_type;
103 typedef typename base_class::back_off back_off; ///< Back-off strategy
104 typedef typename maker::allocator_type allocator_type; ///< Allocator type used for allocate/deallocate the nodes
105 typedef typename base_class::item_counter item_counter; ///< Item counter type
106 typedef typename maker::key_comparator key_comparator; ///< key comparing functor
107 typedef typename base_class::memory_model memory_model; ///< Memory ordering. See \p cds::opt::memory_model
111 typedef typename base_class::value_type node_type;
112 typedef typename maker::cxx_allocator cxx_allocator;
113 typedef typename maker::node_deallocator node_deallocator;
114 typedef typename maker::intrusive_traits::compare intrusive_key_comparator;
116 typedef typename base_class::node_type head_type;
121 typedef cds::gc::guarded_ptr< gc, node_type, value_type, details::guarded_ptr_cast_map<node_type, value_type> > guarded_ptr;
125 template <typename K>
126 static node_type * alloc_node(const K& key)
128 return cxx_allocator().New( key );
131 template <typename K, typename V>
132 static node_type * alloc_node( const K& key, const V& val )
134 return cxx_allocator().New( key, val );
137 template <typename... Args>
138 static node_type * alloc_node( Args&&... args )
140 return cxx_allocator().MoveNew( std::forward<Args>(args)... );
143 static void free_node( node_type * pNode )
145 cxx_allocator().Delete( pNode );
148 struct node_disposer {
149 void operator()( node_type * pNode )
154 typedef std::unique_ptr< node_type, node_disposer > scoped_node_ptr;
158 return base_class::m_Head;
161 head_type const& head() const
163 return base_class::m_Head;
168 return base_class::m_Tail;
171 head_type const& tail() const
173 return base_class::m_Tail;
180 template <bool IsConst>
181 class iterator_type: protected base_class::template iterator_type<IsConst>
183 typedef typename base_class::template iterator_type<IsConst> iterator_base;
185 iterator_type( head_type const& pNode )
186 : iterator_base( const_cast<head_type *>(&pNode) )
188 iterator_type( head_type const * pNode )
189 : iterator_base( const_cast<head_type *>(pNode) )
192 friend class LazyKVList;
195 typedef typename cds::details::make_const_type<mapped_type, IsConst>::reference value_ref;
196 typedef typename cds::details::make_const_type<mapped_type, IsConst>::pointer value_ptr;
198 typedef typename cds::details::make_const_type<value_type, IsConst>::reference pair_ref;
199 typedef typename cds::details::make_const_type<value_type, IsConst>::pointer pair_ptr;
204 iterator_type( iterator_type const& src )
205 : iterator_base( src )
208 key_type const& key() const
210 typename iterator_base::value_ptr p = iterator_base::operator ->();
211 assert( p != nullptr );
212 return p->m_Data.first;
215 value_ref val() const
217 typename iterator_base::value_ptr p = iterator_base::operator ->();
218 assert( p != nullptr );
219 return p->m_Data.second;
222 pair_ptr operator ->() const
224 typename iterator_base::value_ptr p = iterator_base::operator ->();
225 return p ? &(p->m_Data) : nullptr;
228 pair_ref operator *() const
230 typename iterator_base::value_ref p = iterator_base::operator *();
235 iterator_type& operator ++()
237 iterator_base::operator ++();
242 bool operator ==(iterator_type<C> const& i ) const
244 return iterator_base::operator ==(i);
247 bool operator !=(iterator_type<C> const& i ) const
249 return iterator_base::operator !=(i);
257 The forward iterator for lazy list has some features:
258 - it has no post-increment operator
259 - to protect the value, the iterator contains a GC-specific guard + another guard is required locally for increment operator.
260 For some GC (\p gc::HP), a guard is limited resource per thread, so an exception (or assertion) "no free guard"
261 may be thrown if a limit of guard count per thread is exceeded.
262 - The iterator cannot be moved across thread boundary since it contains GC's guard that is thread-private GC data.
263 - Iterator ensures thread-safety even if you delete the item that iterator points to. However, in case of concurrent
264 deleting operations it is no guarantee that you iterate all item in the list.
266 Therefore, the use of iterators in concurrent environment is not good idea. Use the iterator on the concurrent container
267 for debug purpose only.
269 The iterator interface to access item data:
270 - <tt> operator -> </tt> - returns a pointer to \ref value_type for iterator
271 - <tt> operator *</tt> - returns a reference (a const reference for \p const_iterator) to \ref value_type for iterator
272 - <tt> const key_type& key() </tt> - returns a key reference for iterator
273 - <tt> mapped_type& val() </tt> - retuns a value reference for iterator (const reference for \p const_iterator)
275 For both functions the iterator should not be equal to <tt> end() </tt>
277 typedef iterator_type<false> iterator;
279 /// Const forward iterator
281 For iterator's features and requirements see \ref iterator
283 typedef iterator_type<true> const_iterator;
285 /// Returns a forward iterator addressing the first element in a list
287 For empty list \code begin() == end() \endcode
291 iterator it( head() );
292 ++it ; // skip dummy head
296 /// Returns an iterator that addresses the location succeeding the last element in a list
298 Do not use the value returned by <tt>end</tt> function to access any item.
299 Internally, <tt>end</tt> returning value equals to \p nullptr.
301 The returned value can be used only to control reaching the end of the list.
302 For empty list \code begin() == end() \endcode
306 return iterator( tail() );
309 /// Returns a forward const iterator addressing the first element in a list
311 const_iterator begin() const
313 const_iterator it( head() );
314 ++it; // skip dummy head
317 const_iterator cbegin()
319 const_iterator it( head() );
320 ++it; // skip dummy head
325 /// Returns an const iterator that addresses the location succeeding the last element in a list
327 const_iterator end() const
329 return const_iterator( tail());
331 const_iterator cend()
333 return const_iterator( tail());
338 /// Default constructor
342 /// Destructor clears the list
348 /// Inserts new node with key and default value
350 The function creates a node with \p key and default value, and then inserts the node created into the list.
353 - The \ref key_type should be constructible from value of type \p K.
354 In trivial case, \p K is equal to \ref key_type.
355 - The \ref mapped_type should be default-constructible.
357 Returns \p true if inserting successful, \p false otherwise.
359 template <typename K>
360 bool insert( const K& key )
362 return insert_at( head(), key );
365 /// Inserts new node with a key and a value
367 The function creates a node with \p key and value \p val, and then inserts the node created into the list.
370 - The \ref key_type should be constructible from \p key of type \p K.
371 - The \ref mapped_type should be constructible from \p val of type \p V.
373 Returns \p true if inserting successful, \p false otherwise.
375 template <typename K, typename V>
376 bool insert( const K& key, const V& val )
378 // We cannot use insert with functor here
379 // because we cannot lock inserted node for updating
380 // Therefore, we use separate function
381 return insert_at( head(), key, val );
384 /// Inserts new node and initializes it by a functor
386 This function inserts new node with key \p key and if inserting is successful then it calls
387 \p func functor with signature
390 void operator()( value_type& item );
394 The argument \p item of user-defined functor \p func is the reference
395 to the list's item inserted. <tt>item.second</tt> is a reference to item's value that may be changed.
396 The user-defined functor is called only if inserting is successful.
398 The \p key_type should be constructible from value of type \p K.
400 The function allows to split creating of new item into two part:
401 - create item from \p key;
402 - insert new item into the list;
403 - if inserting is successful, initialize the value of item by calling \p func functor
405 This can be useful if complete initialization of object of \p mapped_type is heavyweight and
406 it is preferable that the initialization should be completed only if inserting is successful.
408 template <typename K, typename Func>
409 bool insert_key( const K& key, Func func )
411 return insert_key_at( head(), key, func );
414 /// Inserts data of type \ref mapped_type constructed with <tt>std::forward<Args>(args)...</tt>
416 Returns \p true if inserting successful, \p false otherwise.
418 template <typename... Args>
419 bool emplace( Args&&... args )
421 return emplace_at( head(), std::forward<Args>(args)... );
424 /// Ensures that the \p key exists in the list
426 The operation performs inserting or changing data with lock-free manner.
428 If the \p key not found in the list, then the new item created from \p key
429 is inserted into the list (note that in this case the \p key_type should be
430 constructible from type \p K).
431 Otherwise, the functor \p func is called with item found.
432 The functor signature is:
435 void operator()( bool bNew, value_type& item );
440 - \p bNew - \p true if the item has been inserted, \p false otherwise
441 - \p item - item of the list
443 The functor may change any fields of the \p item.second that is \p mapped_type.
445 Returns <tt> std::pair<bool, bool> </tt> where \p first is true if operation is successfull,
446 \p second is true if new item has been added or \p false if the item with \p key
447 already is in the list.
449 template <typename K, typename Func>
450 std::pair<bool, bool> ensure( const K& key, Func f )
452 return ensure_at( head(), key, f );
455 /// Deletes \p key from the list
456 /** \anchor cds_nonintrusive_LazyKVList_hp_erase_val
458 Returns \p true if \p key is found and has been deleted, \p false otherwise
460 template <typename K>
461 bool erase( K const& key )
463 return erase_at( head(), key, intrusive_key_comparator() );
466 /// Deletes the item from the list using \p pred predicate for searching
468 The function is an analog of \ref cds_nonintrusive_LazyKVList_hp_erase_val "erase(K const&)"
469 but \p pred is used for key comparing.
470 \p Less functor has the interface like \p std::less.
471 \p pred must imply the same element order as the comparator used for building the list.
473 template <typename K, typename Less>
474 bool erase_with( K const& key, Less pred )
476 return erase_at( head(), key, typename maker::template less_wrapper<Less>::type() );
479 /// Deletes \p key from the list
480 /** \anchor cds_nonintrusive_LazyKVList_hp_erase_func
481 The function searches an item with key \p key, calls \p f functor with item found
482 and deletes it. If \p key is not found, the functor is not called.
484 The functor \p Func interface:
487 void operator()(value_type& val) { ... }
490 The functor may be passed by reference with <tt>boost:ref</tt>
492 Returns \p true if key is found and deleted, \p false otherwise
494 template <typename K, typename Func>
495 bool erase( K const& key, Func f )
497 return erase_at( head(), key, intrusive_key_comparator(), f );
500 /// Deletes the item from the list using \p pred predicate for searching
502 The function is an analog of \ref cds_nonintrusive_LazyKVList_hp_erase_func "erase(K const&, Func)"
503 but \p pred is used for key comparing.
504 \p Less functor has the interface like \p std::less.
505 \p pred must imply the same element order as the comparator used for building the list.
507 template <typename K, typename Less, typename Func>
508 bool erase_with( K const& key, Less pred, Func f )
510 return erase_at( head(), key, typename maker::template less_wrapper<Less>::type(), f );
513 /// Extracts the item from the list with specified \p key
514 /** \anchor cds_nonintrusive_LazyKVList_hp_extract
515 The function searches an item with key equal to \p key,
516 unlinks it from the list, and returns it in \p dest parameter.
517 If the item with key equal to \p key is not found the function returns \p false.
519 Note the compare functor should accept a parameter of type \p K that can be not the same as \p key_type.
521 @note Each \p guarded_ptr object uses the GC's guard that can be limited resource.
525 typedef cds::container::LazyKVList< cds::gc::HP, int, foo, my_traits > ord_list;
529 ord_list::guarded_ptr gp;
530 theList.extract( gp, 5 );
534 // Destructor of gp releases internal HP guard and frees the item
538 template <typename K>
539 bool extract( guarded_ptr& dest, K const& key )
541 return extract_at( head(), dest.guard(), key, intrusive_key_comparator() );
544 /// Extracts the item from the list with comparing functor \p pred
546 The function is an analog of \ref cds_nonintrusive_LazyKVList_hp_extract "extract(guarded_ptr&, K const&)"
547 but \p pred predicate is used for key comparing.
549 \p Less functor has the semantics like \p std::less but should take arguments of type \ref key_type and \p K
551 \p pred must imply the same element order as the comparator used for building the list.
553 template <typename K, typename Less>
554 bool extract_with( guarded_ptr& dest, K const& key, Less pred )
556 return extract_at( head(), dest.guard(), key, typename maker::template less_wrapper<Less>::type() );
559 /// Finds the key \p key
560 /** \anchor cds_nonintrusive_LazyKVList_hp_find_val
561 The function searches the item with key equal to \p key
562 and returns \p true if it is found, and \p false otherwise
564 template <typename Q>
565 bool find( Q const& key )
567 return find_at( head(), key, intrusive_key_comparator() );
570 /// Finds the key \p val using \p pred predicate for searching
572 The function is an analog of \ref cds_nonintrusive_LazyKVList_hp_find_val "find(Q const&)"
573 but \p pred is used for key comparing.
574 \p Less functor has the interface like \p std::less.
575 \p pred must imply the same element order as the comparator used for building the list.
577 template <typename Q, typename Less>
578 bool find_with( Q const& key, Less pred )
580 return find_at( head(), key, typename maker::template less_wrapper<Less>::type() );
583 /// Finds the key \p key and performs an action with it
584 /** \anchor cds_nonintrusive_LazyKVList_hp_find_func
585 The function searches an item with key equal to \p key and calls the functor \p f for the item found.
586 The interface of \p Func functor is:
589 void operator()( value_type& item );
592 where \p item is the item found.
594 You may pass \p f argument by reference using \p std::ref.
596 The functor may change <tt>item.second</tt> that is reference to value of node.
597 Note that the function is only guarantee that \p item cannot be deleted during functor is executing.
598 The function does not serialize simultaneous access to the list \p item. If such access is
599 possible you must provide your own synchronization schema to exclude unsafe item modifications.
601 The function returns \p true if \p key is found, \p false otherwise.
603 template <typename Q, typename Func>
604 bool find( Q const& key, Func f )
606 return find_at( head(), key, intrusive_key_comparator(), f );
609 /// Finds the key \p val using \p pred predicate for searching
611 The function is an analog of \ref cds_nonintrusive_LazyKVList_hp_find_func "find(Q&, Func)"
612 but \p pred is used for key comparing.
613 \p Less functor has the interface like \p std::less.
614 \p pred must imply the same element order as the comparator used for building the list.
616 template <typename Q, typename Less, typename Func>
617 bool find_with( Q const& key, Less pred, Func f )
619 return find_at( head(), key, typename maker::template less_wrapper<Less>::type(), f );
622 /// Finds \p key and return the item found
623 /** \anchor cds_nonintrusive_LazyKVList_hp_get
624 The function searches the item with key equal to \p key
625 and assigns the item found to guarded pointer \p ptr.
626 The function returns \p true if \p key is found, and \p false otherwise.
627 If \p key is not found the \p ptr parameter is not changed.
629 @note Each \p guarded_ptr object uses one GC's guard which can be limited resource.
633 typedef cds::container::LazyKVList< cds::gc::HP, int, foo, my_traits > ord_list;
637 ord_list::guarded_ptr gp;
638 if ( theList.get( gp, 5 )) {
642 // Destructor of guarded_ptr releases internal HP guard and frees the item
646 Note the compare functor specified for class \p Traits template parameter
647 should accept a parameter of type \p K that can be not the same as \p key_type.
649 template <typename K>
650 bool get( guarded_ptr& ptr, K const& key )
652 return get_at( head(), ptr.guard(), key, intrusive_key_comparator() );
655 /// Finds the key \p val and return the item found
657 The function is an analog of \ref cds_nonintrusive_LazyKVList_hp_get "get(guarded_ptr& ptr, K const&)"
658 but \p pred is used for comparing the keys.
660 \p Less functor has the semantics like \p std::less but should take arguments of type \ref key_type and \p K
662 \p pred must imply the same element order as the comparator used for building the list.
664 template <typename K, typename Less>
665 bool get_with( guarded_ptr& ptr, K const& key, Less pred )
667 return get_at( head(), ptr.guard(), key, typename maker::template less_wrapper<Less>::type() );
670 /// Checks if the list is empty
673 return base_class::empty();
676 /// Returns list's item count
678 The value returned depends on opt::item_counter option. For atomicity::empty_item_counter,
679 this function always returns 0.
681 @note Even if you use real item counter and it returns 0, this fact is not mean that the list
682 is empty. To check list emptyness use \ref empty() method.
686 return base_class::size();
697 bool insert_node_at( head_type& refHead, node_type * pNode )
699 assert( pNode != nullptr );
700 scoped_node_ptr p( pNode );
702 if ( base_class::insert_at( &refHead, *p )) {
710 template <typename K>
711 bool insert_at( head_type& refHead, const K& key )
713 return insert_node_at( refHead, alloc_node( key ));
716 template <typename K, typename V>
717 bool insert_at( head_type& refHead, const K& key, const V& val )
719 return insert_node_at( refHead, alloc_node( key, val ));
722 template <typename K, typename Func>
723 bool insert_key_at( head_type& refHead, const K& key, Func f )
725 scoped_node_ptr pNode( alloc_node( key ));
727 if ( base_class::insert_at( &refHead, *pNode, [&f](node_type& node){ f( node.m_Data ); } )) {
734 template <typename... Args>
735 bool emplace_at( head_type& refHead, Args&&... args )
737 return insert_node_at( refHead, alloc_node( std::forward<Args>(args)... ));
740 template <typename K, typename Compare>
741 bool erase_at( head_type& refHead, K const& key, Compare cmp )
743 return base_class::erase_at( &refHead, key, cmp );
746 template <typename K, typename Compare, typename Func>
747 bool erase_at( head_type& refHead, K const& key, Compare cmp, Func f )
749 return base_class::erase_at( &refHead, key, cmp, [&f](node_type const & node){f( const_cast<value_type&>(node.m_Data)); });
752 template <typename K, typename Compare>
753 bool extract_at( head_type& refHead, typename gc::Guard& dest, K const& key, Compare cmp )
755 return base_class::extract_at( &refHead, dest, key, cmp );
758 template <typename K, typename Func>
759 std::pair<bool, bool> ensure_at( head_type& refHead, const K& key, Func f )
761 scoped_node_ptr pNode( alloc_node( key ));
763 std::pair<bool, bool> ret = base_class::ensure_at( &refHead, *pNode,
764 [&f]( bool bNew, node_type& node, node_type& ){ f( bNew, node.m_Data ); });
765 if ( ret.first && ret.second )
771 template <typename K, typename Compare>
772 bool find_at( head_type& refHead, K const& key, Compare cmp )
774 return base_class::find_at( &refHead, key, cmp );
777 template <typename K, typename Compare, typename Func>
778 bool find_at( head_type& refHead, K& key, Compare cmp, Func f )
780 return base_class::find_at( &refHead, key, cmp, [&f]( node_type& node, K& ){ f( node.m_Data ); });
783 template <typename K, typename Compare>
784 bool get_at( head_type& refHead, typename gc::Guard& guard, K const& key, Compare cmp )
786 return base_class::get_at( &refHead, guard, key, cmp );
792 }} // namespace cds::container
794 #endif // #ifndef __CDS_CONTAINER_IMPL_LAZY_KVLIST_H