3 #ifndef CDSLIB_CONTAINER_LAZY_KVLIST_NOGC_H
4 #define CDSLIB_CONTAINER_LAZY_KVLIST_NOGC_H
7 #include <cds/container/details/lazy_list_base.h>
8 #include <cds/intrusive/lazy_list_nogc.h>
9 #include <cds/container/details/make_lazy_kvlist.h>
11 namespace cds { namespace container {
13 /// Lazy ordered list (key-value pair, template specialization for gc::nogc)
14 /** @ingroup cds_nonintrusive_list
15 @anchor cds_nonintrusive_LazyKVList_nogc
17 This specialization is append-only list when no item
18 reclamation may be performed. The class does not support deleting of list's item.
20 See @ref cds_nonintrusive_LazyList_gc "cds::container::LazyList<cds::gc::nogc, T, Traits>"
25 #ifdef CDS_DOXYGEN_INVOKED
26 typename Traits = lazy_list::traits
31 class LazyKVList<gc::nogc, Key, Value, Traits>:
32 #ifdef CDS_DOXYGEN_INVOKED
33 protected intrusive::LazyList< gc::nogc, implementation_defined, Traits >
35 protected details::make_lazy_kvlist< cds::gc::nogc, Key, Value, Traits >::type
39 typedef details::make_lazy_kvlist< cds::gc::nogc, Key, Value, Traits > maker;
40 typedef typename maker::type base_class;
44 typedef Traits traits; ///< List traits
45 typedef cds::gc::nogc gc; ///< Garbage collector
46 #ifdef CDS_DOXYGEN_INVOKED
47 typedef Key key_type ; ///< Key type
48 typedef Value mapped_type ; ///< Type of value stored in the list
49 typedef std::pair<key_type const, mapped_type> value_type ; ///< key/value pair stored in the list
51 typedef typename maker::key_type key_type;
52 typedef typename maker::mapped_type mapped_type;
53 typedef typename maker::value_type value_type;
55 typedef typename base_class::back_off back_off; ///< Back-off strategy used
56 typedef typename maker::allocator_type allocator_type; ///< Allocator type used for allocate/deallocate the nodes
57 typedef typename base_class::item_counter item_counter; ///< Item counting policy used
58 typedef typename maker::key_comparator key_comparator; ///< key comparison functor
59 typedef typename base_class::memory_model memory_model; ///< Memory ordering. See cds::opt::memory_model option
60 static CDS_CONSTEXPR bool const c_bSort = base_class::c_bSort; ///< List type: ordered (\p true) or unordered (\p false)
64 typedef typename base_class::value_type node_type;
65 typedef typename maker::cxx_allocator cxx_allocator;
66 typedef typename maker::node_deallocator node_deallocator;
67 typedef typename base_class::key_comparator intrusive_key_comparator;
68 typedef typename base_class::node_type head_type;
74 static node_type * alloc_node(const K& key)
76 return cxx_allocator().New( key );
79 template <typename K, typename V>
80 static node_type * alloc_node( const K& key, const V& val )
82 return cxx_allocator().New( key, val );
85 template <typename... Args>
86 static node_type * alloc_node( Args&&... args )
88 return cxx_allocator().MoveNew( std::forward<Args>(args)... );
91 static void free_node( node_type * pNode )
93 cxx_allocator().Delete( pNode );
96 struct node_disposer {
97 void operator()( node_type * pNode )
102 typedef std::unique_ptr< node_type, node_disposer > scoped_node_ptr;
106 return base_class::m_Head;
109 head_type const& head() const
111 return base_class::m_Head;
116 return base_class::m_Tail;
119 head_type const& tail() const
121 return base_class::m_Tail;
127 template <bool IsConst>
128 class iterator_type: protected base_class::template iterator_type<IsConst>
130 typedef typename base_class::template iterator_type<IsConst> iterator_base;
132 iterator_type( head_type const& refNode )
133 : iterator_base( const_cast<head_type *>( &refNode ))
136 explicit iterator_type( const iterator_base& it )
137 : iterator_base( it )
140 friend class LazyKVList;
143 explicit iterator_type( node_type& pNode )
144 : iterator_base( &pNode )
148 typedef typename cds::details::make_const_type<mapped_type, IsConst>::reference value_ref;
149 typedef typename cds::details::make_const_type<mapped_type, IsConst>::pointer value_ptr;
151 typedef typename cds::details::make_const_type<value_type, IsConst>::reference pair_ref;
152 typedef typename cds::details::make_const_type<value_type, IsConst>::pointer pair_ptr;
158 iterator_type( const iterator_type& src )
159 : iterator_base( src )
162 key_type const& key() const
164 typename iterator_base::value_ptr p = iterator_base::operator ->();
165 assert( p != nullptr );
166 return p->m_Data.first;
169 value_ref val() const
171 typename iterator_base::value_ptr p = iterator_base::operator ->();
172 assert( p != nullptr );
173 return p->m_Data.second;
176 pair_ptr operator ->() const
178 typename iterator_base::value_ptr p = iterator_base::operator ->();
179 return p ? &(p->m_Data) : nullptr;
182 pair_ref operator *() const
184 typename iterator_base::value_ref p = iterator_base::operator *();
189 iterator_type& operator ++()
191 iterator_base::operator ++();
196 iterator_type operator ++(int)
198 return iterator_base::operator ++(0);
202 bool operator ==(iterator_type<C> const& i ) const
204 return iterator_base::operator ==(i);
207 bool operator !=(iterator_type<C> const& i ) const
209 return iterator_base::operator !=(i);
217 The forward iterator for lazy list based on gc::nogc has pre- and post-increment operators.
219 The iterator interface to access item data:
220 - <tt> operator -> </tt> - returns a pointer to \ref value_type for iterator
221 - <tt> operator *</tt> - returns a reference (a const reference for \p const_iterator) to \ref value_type for iterator
222 - <tt> const key_type& key() </tt> - returns a key reference for iterator
223 - <tt> mapped_type& val() </tt> - retuns a value reference for iterator (const reference for \p const_iterator)
225 For both functions the iterator should not be equal to <tt> end() </tt>
227 typedef iterator_type<false> iterator;
229 /// Const forward iterator
231 For iterator's features and requirements see \ref iterator
233 typedef iterator_type<true> const_iterator;
235 /// Returns a forward iterator addressing the first element in a list
237 For empty list \code begin() == end() \endcode
241 iterator it( head() );
242 ++it ; // skip dummy head
246 /// Returns an iterator that addresses the location succeeding the last element in a list
248 Do not use the value returned by <tt>end</tt> function to access any item.
249 Internally, <tt>end</tt> returning value equals to nullptr.
251 The returned value can be used only to control reaching the end of the list.
252 For empty list \code begin() == end() \endcode
256 return iterator( tail());
259 /// Returns a forward const iterator addressing the first element in a list
261 const_iterator begin() const
263 const_iterator it( head() );
264 ++it ; // skip dummy head
267 const_iterator cbegin() const
269 const_iterator it( head() );
270 ++it ; // skip dummy head
275 /// Returns an const iterator that addresses the location succeeding the last element in a list
277 const_iterator end() const
279 return const_iterator( tail());
281 const_iterator cend() const
283 return const_iterator( tail());
289 iterator node_to_iterator( node_type * pNode )
292 return iterator( *pNode );
298 /// Default constructor
302 /// Desctructor clears the list
308 /// Inserts new node with key and default value
310 The function creates a node with \p key and default value, and then inserts the node created into the list.
313 - The \ref key_type should be constructible from value of type \p K.
314 In trivial case, \p K is equal to \ref key_type.
315 - The \ref mapped_type should be default-constructible.
317 Returns an iterator pointed to inserted value, or \p end() if inserting is failed
319 template <typename K>
320 iterator insert( const K& key )
322 return node_to_iterator( insert_at( head(), key ));
325 /// Inserts new node with a key and a value
327 The function creates a node with \p key and value \p val, and then inserts the node created into the list.
330 - The \ref key_type should be constructible from \p key of type \p K.
331 - The \ref mapped_type should be constructible from \p val of type \p V.
333 Returns an iterator pointed to inserted value, or \p end() if inserting is failed
335 template <typename K, typename V>
336 iterator insert( const K& key, const V& val )
338 // We cannot use insert with functor here
339 // because we cannot lock inserted node for updating
340 // Therefore, we use separate function
341 return node_to_iterator( insert_at( head(), key, val ));
344 /// Inserts new node and initialize it by a functor
346 This function inserts new node with key \p key and if inserting is successful then it calls
347 \p func functor with signature
348 \code void func( value_type& item ) ; endcode
352 void operator()( value_type& item );
356 The argument \p item of user-defined functor \p func is the reference
357 to the list's item inserted. <tt>item.second</tt> is a reference to item's value that may be changed.
358 The user-defined functor is called only if the inserting is successful.
360 The key_type should be constructible from value of type \p K.
362 The function allows to split creating of new item into two part:
363 - create item from \p key;
364 - insert new item into the list;
365 - if inserting is successful, initialize the value of item by calling \p f functor
367 This can be useful if complete initialization of object of \p mapped_type is heavyweight and
368 it is preferable that the initialization should be completed only if inserting is successful.
370 Returns an iterator pointed to inserted value, or \p end() if inserting is failed
372 template <typename K, typename Func>
373 iterator insert_with( const K& key, Func func )
375 return node_to_iterator( insert_with_at( head(), key, func ));
378 /// Ensures that the key \p key exists in the list
380 The operation inserts new item if the key \p key is not found in the list.
381 Otherwise, the function returns an iterator that points to item found.
383 Returns <tt> std::pair<iterator, bool> </tt> where \p first is an iterator pointing to
384 item found or inserted, \p second is true if new item has been added or \p false if the item
385 already is in the list.
387 template <typename K>
388 std::pair<iterator, bool> ensure( const K& key )
390 std::pair< node_type *, bool > ret = ensure_at( head(), key );
391 return std::make_pair( node_to_iterator( ret.first ), ret.second );
394 /// Inserts data of type \ref mapped_type constructed with <tt>std::forward<Args>(args)...</tt>
396 Returns an iterator pointed to inserted value, or \p end() if inserting is failed
398 template <typename... Args>
399 iterator emplace( Args&&... args )
401 return node_to_iterator( emplace_at( head(), std::forward<Args>(args)... ));
404 /// Find the key \p key
405 /** \anchor cds_nonintrusive_LazyKVList_nogc_find
406 The function searches the item with key equal to \p key
407 and returns an iterator pointed to item found if the key is found,
408 and \ref end() otherwise
410 template <typename Q>
411 iterator find( Q const& key )
413 return node_to_iterator( find_at( head(), key, intrusive_key_comparator() ) );
416 /// Finds the key \p val using \p pred predicate for searching (for ordered list only)
418 The function is an analog of \ref cds_nonintrusive_LazyKVList_nogc_find "find(Q const&)"
419 but \p pred is used for key comparing.
420 \p Less functor has the interface like \p std::less.
421 \p pred must imply the same element order as the comparator used for building the list.
423 template <typename Q, typename Less, bool Sort = c_bSort>
424 typename std::enable_if<Sort, iterator>::type find_with( Q const& key, Less pred )
427 return node_to_iterator( find_at( head(), key, typename maker::template less_wrapper<Less>::type() ) );
430 /// Finds the key \p val using \p equal predicate for searching (for unordered list only)
432 The function is an analog of \ref cds_nonintrusive_LazyKVList_nogc_find "find(Q const&)"
433 but \p equal is used for key comparing.
434 \p Equal functor has the interface like \p std::equal_to.
436 template <typename Q, typename Equal, bool Sort = c_bSort>
437 typename std::enable_if<!Sort, iterator>::type find_with( Q const& key, Equal equal )
440 return node_to_iterator( find_at( head(), key, typename maker::template equal_to_wrapper<Equal>::type() ) );
443 /// Check if the list is empty
446 return base_class::empty();
449 /// Returns list's item count
451 The value returned depends on opt::item_counter option. For atomicity::empty_item_counter,
452 this function always returns 0.
454 @note Even if you use real item counter and it returns 0, this fact is not mean that the list
455 is empty. To check list emptyness use \ref empty() method.
459 return base_class::size();
464 Post-condition: the list is empty
473 node_type * insert_node_at( head_type& refHead, node_type * pNode )
475 assert( pNode != nullptr );
476 scoped_node_ptr p( pNode );
477 if ( base_class::insert_at( &refHead, *p ))
483 template <typename K>
484 node_type * insert_at( head_type& refHead, const K& key )
486 return insert_node_at( refHead, alloc_node( key ));
489 template <typename K, typename V>
490 node_type * insert_at( head_type& refHead, const K& key, const V& val )
492 return insert_node_at( refHead, alloc_node( key, val ));
495 template <typename K, typename Func>
496 node_type * insert_with_at( head_type& refHead, const K& key, Func f )
498 scoped_node_ptr pNode( alloc_node( key ));
500 if ( base_class::insert_at( &refHead, *pNode )) {
502 return pNode.release();
509 template <typename K>
510 std::pair< node_type *, bool > ensure_at( head_type& refHead, const K& key )
512 scoped_node_ptr pNode( alloc_node( key ));
513 node_type * pItemFound = nullptr;
515 std::pair<bool, bool> ret = base_class::ensure_at( &refHead, *pNode, [&pItemFound](bool, node_type& item, node_type&){ pItemFound = &item; } );
516 if ( ret.first && ret.second )
519 assert( pItemFound != nullptr );
520 return std::make_pair( pItemFound, ret.second );
523 template <typename... Args>
524 node_type * emplace_at( head_type& refHead, Args&&... args )
526 return insert_node_at( refHead, alloc_node( std::forward<Args>(args)... ));
529 template <typename K, typename Compare>
530 node_type * find_at( head_type& refHead, const K& key, Compare cmp )
532 return base_class::find_at( &refHead, key, cmp );
536 template <typename K, typenam Compare, typename Func>
537 bool find_at( head_type& refHead, K& key, Compare cmp, Func f )
539 return base_class::find_at( &refHead, key, cmp, [&f]( node_type& node, K const& ){ f( node.m_Data ); });
545 }} // namespace cds::container
547 #endif // #ifndef CDSLIB_CONTAINER_LAZY_KVLIST_NOGC_H