3 #ifndef CDSLIB_CONTAINER_SPLIT_LIST_MAP_H
4 #define CDSLIB_CONTAINER_SPLIT_LIST_MAP_H
6 #include <cds/container/split_list_set.h>
7 #include <cds/details/binary_functor_wrapper.h>
9 namespace cds { namespace container {
11 /// Split-ordered list map
12 /** @ingroup cds_nonintrusive_map
13 \anchor cds_nonintrusive_SplitListMap_hp
15 Hash table implementation based on split-ordered list algorithm discovered by Ori Shalev and Nir Shavit, see
16 - [2003] Ori Shalev, Nir Shavit "Split-Ordered Lists - Lock-free Resizable Hash Tables"
17 - [2008] Nir Shavit "The Art of Multiprocessor Programming"
19 See intrusive::SplitListSet for a brief description of the split-list algorithm.
22 - \p GC - Garbage collector used like \p cds::gc::HP or \p cds::gc::DHP
23 - \p Key - key type of an item stored in the map. It should be copy-constructible
24 - \p Value - value type stored in the map
25 - \p Traits - map traits, default is \p split_list::traits. Instead of declaring \p %split_list::traits -based
26 struct you may apply option-based notation with \p split_list::make_traits metafunction.
28 There are the specializations:
29 - for \ref cds_urcu_desc "RCU" - declared in <tt>cd/container/split_list_map_rcu.h</tt>,
30 see \ref cds_nonintrusive_SplitListMap_rcu "SplitListMap<RCU>".
31 - for \ref cds::gc::nogc declared in <tt>cds/container/split_list_map_nogc.h</tt>,
32 see \ref cds_nonintrusive_SplitListMap_nogc "SplitListMap<gc::nogc>".
36 You should decide what garbage collector you want, and what ordered list you want to use. Split-ordered list
37 is original data structure based on an ordered list. Suppose, you want construct split-list map based on \p gc::HP GC
38 and \p MichaelList as ordered list implementation. Your map should map \p int key to \p std::string value.
39 So, you beginning your program with following include:
41 #include <cds/container/michael_list_hp.h>
42 #include <cds/container/split_list_map.h>
44 namespace cc = cds::container;
46 The inclusion order is important: first, include file for ordered-list implementation (for this example, <tt>cds/container/michael_list_hp.h</tt>),
47 then the header for split-list map <tt>cds/container/split_list_map.h</tt>.
49 Now, you should declare traits for split-list map. The main parts of traits are a hash functor and a comparing functor for the ordered list.
50 We use <tt>std::hash<int></tt> as hash functor and <tt>std::less<int></tt> predicate as comparing functor.
52 The second attention: instead of using \p %MichaelList in \p %SplitListMap traits we use a tag \p cds::contaner::michael_list_tag for the Michael's list.
53 The split-list requires significant support from underlying ordered list class and it is not good idea to dive you
54 into deep implementation details of split-list and ordered list interrelations. The tag paradigm simplifies split-list interface.
57 // SplitListMap traits
58 struct foo_set_traits: public cc::split_list::traits
60 typedef cc::michael_list_tag ordered_list ; // what type of ordered list we want to use
61 typedef std::hash<int> hash ; // hash functor for the key stored in split-list map
63 // Type traits for our MichaelList class
64 struct ordered_list_traits: public cc::michael_list::traits
66 typedef std::less<int> less ; // use our std::less predicate as comparator to order list nodes
71 Now you are ready to declare our map class based on \p %SplitListMap:
73 typedef cc::SplitListMap< cds::gc::DHP, int, std::string, foo_set_traits > int_string_map;
76 You may use the modern option-based declaration instead of classic type-traits-based one:
78 typedef cc::SplitListMap<
79 cs::gc::DHP // GC used
81 ,std::string // value type
82 ,cc::split_list::make_traits< // metafunction to build split-list traits
83 cc::split_list::ordered_list<cc::michael_list_tag> // tag for underlying ordered list implementation
84 ,cc::opt::hash< std::hash<int> > // hash functor
85 ,cc::split_list::ordered_list_traits< // ordered list traits desired
86 cc::michael_list::make_traits< // metafunction to build lazy list traits
87 cc::opt::less< std::less<int> > // less-based compare functor
93 In case of option-based declaration with \p split_list::make_traits metafunction the struct \p foo_set_traits is not required.
95 Now, the map of type \p int_string_map is ready to use in your program.
97 Note that in this example we show only mandatory \p traits parts, optional ones is the default and they are inherited
98 from \p container::split_list::traits. There are many other options for deep tuning of the split-list and
99 ordered-list containers.
105 #ifdef CDS_DOXYGEN_INVOKED
106 class Traits = split_list::traits
112 protected container::SplitListSet<
114 std::pair<Key const, Value>,
115 split_list::details::wrap_map_traits<Key, Value, Traits>
119 typedef container::SplitListSet<
121 std::pair<Key const, Value>,
122 split_list::details::wrap_map_traits<Key, Value, Traits>
127 typedef GC gc; ///< Garbage collector
128 typedef Key key_type; ///< key type
129 typedef Value mapped_type; ///< type of value to be stored in the map
130 typedef Traits options; ///< Map traits
132 typedef std::pair<key_type const, mapped_type> value_type ; ///< key-value pair type
133 typedef typename base_class::ordered_list ordered_list; ///< Underlying ordered list class
134 typedef typename base_class::key_comparator key_comparator; ///< key compare functor
136 typedef typename base_class::hash hash; ///< Hash functor for \ref key_type
137 typedef typename base_class::item_counter item_counter; ///< Item counter type
138 typedef typename base_class::stat stat; ///< Internal statistics
141 typedef cds::container::split_list::implementation_tag implementation_tag;
146 typedef typename base_class::maker::traits::key_accessor key_accessor;
147 typedef typename base_class::node_type node_type;
152 typedef typename gc::template guarded_ptr< node_type, value_type, details::guarded_ptr_cast_set<node_type, value_type> > guarded_ptr;
155 /// Forward iterator (see \p SplitListSet::iterator)
157 Remember, the iterator <tt>operator -> </tt> and <tt>operator *</tt> returns \ref value_type pointer and reference.
158 To access item key and value use <tt>it->first</tt> and <tt>it->second</tt> respectively.
160 typedef typename base_class::iterator iterator;
162 /// Const forward iterator (see SplitListSet::const_iterator)
163 typedef typename base_class::const_iterator const_iterator;
165 /// Returns a forward iterator addressing the first element in a map
167 For empty map \code begin() == end() \endcode
171 return base_class::begin();
174 /// Returns an iterator that addresses the location succeeding the last element in a map
176 Do not use the value returned by <tt>end</tt> function to access any item.
177 The returned value can be used only to control reaching the end of the map.
178 For empty map \code begin() == end() \endcode
182 return base_class::end();
185 /// Returns a forward const iterator addressing the first element in a map
187 const_iterator begin() const
189 return base_class::begin();
191 const_iterator cbegin() const
193 return base_class::cbegin();
197 /// Returns an const iterator that addresses the location succeeding the last element in a map
199 const_iterator end() const
201 return base_class::end();
203 const_iterator cend() const
205 return base_class::cend();
210 /// Initializes split-ordered map of default capacity
212 The default capacity is defined in bucket table constructor.
213 See \p intrusive::split_list::expandable_bucket_table, \p intrusive::split_list::static_bucket_table
214 which selects by \p intrusive::split_list::traits::dynamic_bucket_table.
220 /// Initializes split-ordered map
222 size_t nItemCount ///< estimated average item count
223 , size_t nLoadFactor = 1 ///< load factor - average item count per bucket. Small integer up to 10, default is 1.
225 : base_class( nItemCount, nLoadFactor )
229 /// Inserts new node with key and default value
231 The function creates a node with \p key and default value, and then inserts the node created into the map.
234 - The \ref key_type should be constructible from value of type \p K.
235 In trivial case, \p K is equal to \ref key_type.
236 - The \ref mapped_type should be default-constructible.
238 Returns \p true if inserting successful, \p false otherwise.
240 template <typename K>
241 bool insert( K const& key )
243 //TODO: pass arguments by reference (make_pair makes copy)
244 return base_class::insert( std::make_pair( key, mapped_type() ) );
249 The function creates a node with copy of \p val value
250 and then inserts the node created into the map.
253 - The \ref key_type should be constructible from \p key of type \p K.
254 - The \ref mapped_type should be constructible from \p val of type \p V.
256 Returns \p true if \p val is inserted into the map, \p false otherwise.
258 template <typename K, typename V>
259 bool insert( K const& key, V const& val )
261 //TODO: pass arguments by reference (make_pair makes copy)
262 return base_class::insert( std::make_pair(key, val) );
265 /// Inserts new node and initialize it by a functor
267 This function inserts new node with key \p key and if inserting is successful then it calls
268 \p func functor with signature
271 void operator()( value_type& item );
275 The argument \p item of user-defined functor \p func is the reference
276 to the map's item inserted:
277 - <tt>item.first</tt> is a const reference to item's key that cannot be changed.
278 - <tt>item.second</tt> is a reference to item's value that may be changed.
280 It should be keep in mind that concurrent modifications of \p <tt>item.second</tt> may be possible.
282 The key_type should be constructible from value of type \p K.
284 The function allows to split creating of new item into two part:
285 - create item from \p key;
286 - insert new item into the map;
287 - if inserting is successful, initialize the value of item by calling \p func functor
289 This can be useful if complete initialization of object of \p mapped_type is heavyweight and
290 it is preferable that the initialization should be completed only if inserting is successful.
292 @warning For \ref cds_nonintrusive_MichaelKVList_gc "MichaelKVList" as the bucket see \ref cds_intrusive_item_creating "insert item troubleshooting".
293 \ref cds_nonintrusive_LazyKVList_gc "LazyKVList" provides exclusive access to inserted item and does not require any node-level
296 template <typename K, typename Func>
297 bool insert_with( K const& key, Func func )
299 //TODO: pass arguments by reference (make_pair makes copy)
300 return base_class::insert( std::make_pair( key, mapped_type() ), func );
303 /// For key \p key inserts data of type \p mapped_type created from \p args
305 \p key_type should be constructible from type \p K
307 Returns \p true if inserting successful, \p false otherwise.
309 template <typename K, typename... Args>
310 bool emplace( K&& key, Args&&... args )
312 return base_class::emplace( std::forward<K>(key), std::move(mapped_type(std::forward<Args>(args)...)));
315 /// Ensures that the \p key exists in the map
317 The operation performs inserting or changing data with lock-free manner.
319 If the \p key not found in the map, then the new item created from \p key
320 is inserted into the map (note that in this case the \ref key_type should be
321 constructible from type \p K).
322 Otherwise, the functor \p func is called with item found.
323 The functor \p Func may be a function with signature:
325 void func( bool bNew, value_type& item );
330 void operator()( bool bNew, value_type& item );
335 - \p bNew - \p true if the item has been inserted, \p false otherwise
336 - \p item - item of the list
338 Returns <tt> std::pair<bool, bool> </tt> where \p first is true if operation is successfull,
339 \p second is true if new item has been added or \p false if the item with \p key
340 already is in the list.
342 @warning For \ref cds_nonintrusive_MichaelKVList_gc "MichaelKVList" as the ordered list see \ref cds_intrusive_item_creating "insert item troubleshooting".
343 \ref cds_nonintrusive_LazyKVList_gc "LazyKVList" provides exclusive access to inserted item and does not require any node-level
346 template <typename K, typename Func>
347 std::pair<bool, bool> ensure( K const& key, Func func )
349 //TODO: pass arguments by reference (make_pair makes copy)
350 return base_class::ensure( std::make_pair( key, mapped_type() ),
351 [&func](bool bNew, value_type& item, value_type const& /*val*/) {
356 /// Deletes \p key from the map
357 /** \anchor cds_nonintrusive_SplitListMap_erase_val
359 Return \p true if \p key is found and deleted, \p false otherwise
361 template <typename K>
362 bool erase( K const& key )
364 return base_class::erase( key );
367 /// Deletes the item from the map using \p pred predicate for searching
369 The function is an analog of \ref cds_nonintrusive_SplitListMap_erase_val "erase(K const&)"
370 but \p pred is used for key comparing.
371 \p Less functor has the interface like \p std::less.
372 \p Less must imply the same element order as the comparator used for building the map.
374 template <typename K, typename Less>
375 bool erase_with( K const& key, Less pred )
378 return base_class::erase_with( key, cds::details::predicate_wrapper<value_type, Less, key_accessor>() );
381 /// Deletes \p key from the map
382 /** \anchor cds_nonintrusive_SplitListMap_erase_func
384 The function searches an item with key \p key, calls \p f functor
385 and deletes the item. If \p key is not found, the functor is not called.
387 The functor \p Func interface is:
390 void operator()(value_type& item) { ... }
394 Return \p true if key is found and deleted, \p false otherwise
396 template <typename K, typename Func>
397 bool erase( K const& key, Func f )
399 return base_class::erase( key, f );
402 /// Deletes the item from the map using \p pred predicate for searching
404 The function is an analog of \ref cds_nonintrusive_SplitListMap_erase_func "erase(K const&, Func)"
405 but \p pred is used for key comparing.
406 \p Less functor has the interface like \p std::less.
407 \p Less must imply the same element order as the comparator used for building the map.
409 template <typename K, typename Less, typename Func>
410 bool erase_with( K const& key, Less pred, Func f )
413 return base_class::erase_with( key, cds::details::predicate_wrapper<value_type, Less, key_accessor>(), f );
416 /// Extracts the item with specified \p key
417 /** \anchor cds_nonintrusive_SplitListMap_hp_extract
418 The function searches an item with key equal to \p key,
419 unlinks it from the map, and returns it as \p guarded_ptr.
420 If \p key is not found the function returns an empty guarded pointer.
422 Note the compare functor should accept a parameter of type \p K that may be not the same as \p value_type.
424 The extracted item is freed automatically when returned \p guarded_ptr object will be destroyed or released.
425 @note Each \p guarded_ptr object uses the GC's guard that can be limited resource.
429 typedef cds::container::SplitListMap< your_template_args > splitlist_map;
430 splitlist_map theMap;
433 splitlist_map::guarded_ptr gp(theMap.extract( 5 ));
438 // Destructor of gp releases internal HP guard
442 template <typename K>
443 guarded_ptr extract( K const& key )
446 base_class::extract_( gp.guard(), key );
450 /// Extracts the item using compare functor \p pred
452 The function is an analog of \ref cds_nonintrusive_SplitListMap_hp_extract "extract(K const&)"
453 but \p pred predicate is used for key comparing.
455 \p Less functor has the semantics like \p std::less but should take arguments of type \ref value_type and \p K
457 \p pred must imply the same element order as the comparator used for building the map.
459 template <typename K, typename Less>
460 guarded_ptr extract_with( K const& key, Less pred )
464 base_class::extract_with_( gp.guard(), key, cds::details::predicate_wrapper<value_type, Less, key_accessor>() );
468 /// Finds the key \p key
469 /** \anchor cds_nonintrusive_SplitListMap_find_cfunc
471 The function searches the item with key equal to \p key and calls the functor \p f for item found.
472 The interface of \p Func functor is:
475 void operator()( value_type& item );
478 where \p item is the item found.
480 The functor may change \p item.second. Note that the functor is only guarantee
481 that \p item cannot be disposed during functor is executing.
482 The functor does not serialize simultaneous access to the map's \p item. If such access is
483 possible you must provide your own synchronization schema on item level to exclude unsafe item modifications.
485 The function returns \p true if \p key is found, \p false otherwise.
487 template <typename K, typename Func>
488 bool find( K const& key, Func f )
490 return base_class::find( key, [&f](value_type& pair, K const&){ f( pair ); } );
493 /// Finds the key \p val using \p pred predicate for searching
495 The function is an analog of \ref cds_nonintrusive_SplitListMap_find_cfunc "find(K const&, Func)"
496 but \p pred is used for key comparing.
497 \p Less functor has the interface like \p std::less.
498 \p Less must imply the same element order as the comparator used for building the map.
500 template <typename K, typename Less, typename Func>
501 bool find_with( K const& key, Less pred, Func f )
504 return base_class::find_with( key,
505 cds::details::predicate_wrapper<value_type, Less, key_accessor>(),
506 [&f](value_type& pair, K const&){ f( pair ); } );
509 /// Finds the key \p key
510 /** \anchor cds_nonintrusive_SplitListMap_find_val
512 The function searches the item with key equal to \p key
513 and returns \p true if it is found, and \p false otherwise.
515 template <typename K>
516 bool find( K const& key )
518 return base_class::find( key );
521 /// Finds the key \p val using \p pred predicate for searching
523 The function is an analog of \ref cds_nonintrusive_SplitListMap_find_val "find(K const&)"
524 but \p pred is used for key comparing.
525 \p Less functor has the interface like \p std::less.
526 \p Less must imply the same element order as the comparator used for building the map.
528 template <typename K, typename Less>
529 bool find_with( K const& key, Less pred )
532 return base_class::find( key, cds::details::predicate_wrapper<value_type, Less, key_accessor>() );
535 /// Finds \p key and return the item found
536 /** \anchor cds_nonintrusive_SplitListMap_hp_get
537 The function searches the item with key equal to \p key
538 and returns the item found as a guarded pointer.
539 If \p key is not found the function returns an empty guarded pointer.
541 @note Each \p guarded_ptr object uses one GC's guard which can be limited resource.
545 typedef cds::container::SplitListMap< your_template_params > splitlist_map;
546 splitlist_map theMap;
549 splitlist_map::guarded_ptr gp(theMap.get( 5 ));
554 // Destructor of guarded_ptr releases internal HP guard
558 Note the compare functor specified for split-list map
559 should accept a parameter of type \p K that can be not the same as \p value_type.
561 template <typename K>
562 guarded_ptr get( K const& key )
565 base_class::get_( gp.guard(), key );
569 /// Finds \p key and return the item found
571 The function is an analog of \ref cds_nonintrusive_SplitListMap_hp_get "get( K const&)"
572 but \p pred is used for comparing the keys.
574 \p Less functor has the semantics like \p std::less but should take arguments of type \ref value_type and \p K
576 \p pred must imply the same element order as the comparator used for building the map.
578 template <typename K, typename Less>
579 guarded_ptr get_with( K const& key, Less pred )
583 base_class::get_with_( gp.guard(), key, cds::details::predicate_wrapper<value_type, Less, key_accessor>() );
587 /// Clears the map (not atomic)
593 /// Checks if the map is empty
595 Emptiness is checked by item counting: if item count is zero then the map is empty.
596 Thus, the correct item counting is an important part of the map implementation.
600 return base_class::empty();
603 /// Returns item count in the map
606 return base_class::size();
609 /// Returns internal statistics
610 stat const& statistics() const
612 return base_class::statistics();
617 }} // namespace cds::container
619 #endif // #ifndef CDSLIB_CONTAINER_SPLIT_LIST_MAP_H