cds  2.3.2
cds::container::SkipListMap< GC, Key, T, Traits > Class Template Reference

Lock-free skip-list map. More...

#include <cds/container/impl/skip_list_map.h>

Inheritance diagram for cds::container::SkipListMap< GC, Key, T, Traits >:
cds::intrusive::SkipListSet< GC, std::pair< Key const, T >, Traits >

Public Types

typedef GC gc
 Garbage collector.
 
typedef Key key_type
 Key type.
 
typedef T mapped_type
 Mapped type.
 
typedef Traits traits
 Map traits.
 
typedef std::pair< Key const, T > value_type
 Key-value pair to be stored in the map.
 
typedef base_class::back_off back_off
 Back-off strategy.
 
typedef traits::allocator allocator_type
 Allocator type used for allocate/deallocate the skip-list nodes.
 
typedef base_class::item_counter item_counter
 Item counting policy used.
 
typedef maker::key_comparator key_comparator
 key comparison functor
 
typedef base_class::memory_model memory_model
 Memory ordering, see cds::opt::memory_model.
 
typedef traits::random_level_generator random_level_generator
 random level generator
 
typedef traits::stat stat
 internal statistics type
 
typedef gc::template guarded_ptr< node_type, value_type, details::guarded_ptr_cast_set< node_type, value_type > > guarded_ptr
 Guarded pointer.
 

Public Member Functions

 SkipListMap ()
 Default ctor.
 
 ~SkipListMap ()
 Destructor destroys the set object.
 
template<typename K >
bool insert (K const &key)
 Inserts new node with key and default value. More...
 
template<typename K , typename V >
bool insert (K const &key, V const &val)
 Inserts new node. More...
 
template<typename K , typename Func >
bool insert_with (K const &key, Func func)
 Inserts new node and initialize it by a functor. More...
 
template<typename K , typename... Args>
bool emplace (K &&key, Args &&... args)
 For key key inserts data of type value_type created in-place from std::forward<Args>(args)... More...
 
template<typename K , typename Func >
std::pair< bool, bool > update (K const &key, Func func, bool bInsert=true)
 Updates data by key. More...
 
template<typename K >
bool erase (K const &key)
 Delete key from the map. More...
 
template<typename K , typename Less >
bool erase_with (K const &key, Less pred)
 Deletes the item from the map using pred predicate for searching. More...
 
template<typename K , typename Func >
bool erase (K const &key, Func f)
 Delete key from the map. More...
 
template<typename K , typename Less , typename Func >
bool erase_with (K const &key, Less pred, Func f)
 Deletes the item from the map using pred predicate for searching. More...
 
template<typename K >
guarded_ptr extract (K const &key)
 Extracts the item from the map with specified key. More...
 
template<typename K , typename Less >
guarded_ptr extract_with (K const &key, Less pred)
 Extracts the item from the map with comparing functor pred. More...
 
guarded_ptr extract_min ()
 Extracts an item with minimal key from the map. More...
 
guarded_ptr extract_max ()
 Extracts an item with maximal key from the map. More...
 
template<typename K , typename Func >
bool find (K const &key, Func f)
 Find the key key. More...
 
template<typename K , typename Less , typename Func >
bool find_with (K const &key, Less pred, Func f)
 Finds the key val using pred predicate for searching. More...
 
template<typename K >
bool contains (K const &key)
 Checks whether the map contains key. More...
 
template<typename K , typename Less >
bool contains (K const &key, Less pred)
 Checks whether the set contains key using pred predicate for searching. More...
 
template<typename K >
guarded_ptr get (K const &key)
 Finds the key key and return the item found. More...
 
template<typename K , typename Less >
guarded_ptr get_with (K const &key, Less pred)
 Finds the key key and return the item found. More...
 
void clear ()
 Clears the map.
 
bool empty () const
 Checks if the map is empty.
 
size_t size () const
 Returns item count in the map.
 
stat const & statistics () const
 Returns const reference to internal statistics.
 

Static Public Attributes

static size_t const c_nHazardPtrCount = base_class::c_nHazardPtrCount
 Count of hazard pointer required for the skip-list.
 

Forward iterators (only for debugging purpose)

typedef skip_list::details::iterator< typename base_class::iterator > iterator
 Iterator type. More...
 
typedef skip_list::details::iterator< typename base_class::const_iterator > const_iterator
 Const forward iterator type.
 
iterator begin ()
 Returns a forward iterator addressing the first element in a map.
 
const_iterator begin () const
 Returns a forward const iterator addressing the first element in a map.
 
const_iterator cbegin () const
 Returns a forward const iterator addressing the first element in a map.
 
iterator end ()
 Returns a forward iterator that addresses the location succeeding the last element in a map.
 
const_iterator end () const
 Returns a forward const iterator that addresses the location succeeding the last element in a map.
 
const_iterator cend () const
 Returns a forward const iterator that addresses the location succeeding the last element in a map.
 

Additional Inherited Members

- Protected Types inherited from cds::intrusive::SkipListSet< GC, std::pair< Key const, T >, Traits >
typedef node_type::atomic_marked_ptr atomic_node_ptr
 Atomic marked node pointer.
 
typedef node_type::marked_ptr marked_node_ptr
 Node marked pointer.
 
typedef GC gc
 Garbage collector.
 
typedef std::pair< Key const, T > value_type
 type of value stored in the skip-list
 
typedef Traits traits
 Traits template parameter.
 
typedef traits::hook hook
 hook type
 
typedef hook::node_type node_type
 node type
 
typedef implementation_defined key_comparator
 key comparison functor based on opt::compare and opt::less option setter.
 
typedef traits::disposer disposer
 item disposer
 
typedef get_node_traits< value_type, node_type, hook >::type node_traits
 node traits
 
typedef traits::item_counter item_counter
 Item counting policy.
 
typedef traits::memory_model memory_model
 Memory ordering, see cds::opt::memory_model option.
 
typedef traits::random_level_generator random_level_generator
 random level generator
 
typedef traits::allocator allocator_type
 allocator for maintaining array of next pointers of the node
 
typedef traits::back_off back_off
 Back-off strategy.
 
typedef traits::stat stat
 internal statistics type
 
typedef gc::template guarded_ptr< value_typeguarded_ptr
 Guarded pointer.
 
typedef skip_list::details::iterator< gc, node_traits, back_off, false > iterator
 Iterator type. More...
 
typedef skip_list::details::iterator< gc, node_traits, back_off, true > const_iterator
 Const iterator type.
 
- Protected Member Functions inherited from cds::intrusive::SkipListSet< GC, std::pair< Key const, T >, Traits >
 SkipListSet ()
 Default constructor. More...
 
 ~SkipListSet ()
 Clears and destructs the skip-list.
 
bool insert (value_type &val)
 Inserts new node. More...
 
bool insert (value_type &val, Func f)
 Inserts new node. More...
 
std::pair< bool, bool > update (value_type &val, Func func, bool bInsert=true)
 Updates the node. More...
 
bool unlink (value_type &val)
 Unlinks the item val from the set. More...
 
guarded_ptr extract (Q const &key)
 Extracts the item from the set with specified key. More...
 
guarded_ptr extract_with (Q const &key, Less pred)
 Extracts the item from the set with comparing functor pred. More...
 
guarded_ptr extract_min ()
 Extracts an item with minimal key from the list. More...
 
guarded_ptr extract_max ()
 Extracts an item with maximal key from the list. More...
 
bool erase (Q const &key)
 Deletes the item from the set. More...
 
bool erase (Q const &key, Func f)
 Deletes the item from the set. More...
 
bool erase_with (Q const &key, Less pred)
 Deletes the item from the set with comparing functor pred. More...
 
bool erase_with (Q const &key, Less pred, Func f)
 Deletes the item from the set with comparing functor pred. More...
 
bool find (Q &key, Func f)
 Finds key. More...
 
bool find_with (Q &key, Less pred, Func f)
 Finds the key key with pred predicate for comparing. More...
 
bool contains (Q const &key)
 Checks whether the set contains key. More...
 
bool contains (Q const &key, Less pred)
 Checks whether the set contains key using pred predicate for searching. More...
 
guarded_ptr get (Q const &key)
 Finds key and return the item found. More...
 
guarded_ptr get_with (Q const &key, Less pred)
 Finds key and return the item found. More...
 
size_t size () const
 Returns item count in the set. More...
 
bool empty () const
 Checks if the set is empty.
 
void clear ()
 Clears the set (not atomic) More...
 
stat const & statistics () const
 Returns const reference to internal statistics.
 
iterator begin ()
 Returns a forward iterator addressing the first element in a set.
 
const_iterator begin () const
 Returns a forward const iterator addressing the first element in a set.
 
const_iterator cbegin () const
 Returns a forward const iterator addressing the first element in a set.
 
iterator end ()
 Returns a forward iterator that addresses the location succeeding the last element in a set.
 
const_iterator end () const
 Returns a forward const iterator that addresses the location succeeding the last element in a set.
 
const_iterator cend () const
 Returns a forward const iterator that addresses the location succeeding the last element in a set.
 
- Static Protected Member Functions inherited from cds::intrusive::SkipListSet< GC, std::pair< Key const, T >, Traits >
static constexpr unsigned int max_height () noexcept
 Returns maximum height of skip-list. The max height is a constant for each object and does not exceed 32.
 
- Static Protected Attributes inherited from cds::intrusive::SkipListSet< GC, std::pair< Key const, T >, Traits >
static unsigned int const c_nMaxHeight
 Max node height. The actual node height should be in range [0 .. c_nMaxHeight) More...
 
static size_t const c_nHazardPtrCount
 Count of hazard pointer required for the skip-list.
 

Detailed Description

template<typename GC, typename Key, typename T, typename Traits = skip_list::traits>
class cds::container::SkipListMap< GC, Key, T, Traits >

Lock-free skip-list map.

The implementation of well-known probabilistic data structure called skip-list invented by W.Pugh in his papers:

  • [1989] W.Pugh Skip Lists: A Probabilistic Alternative to Balanced Trees
  • [1990] W.Pugh A Skip List Cookbook

A skip-list is a probabilistic data structure that provides expected logarithmic time search without the need of rebalance. The skip-list is a collection of sorted linked list. Nodes are ordered by key. Each node is linked into a subset of the lists. Each list has a level, ranging from 0 to 32. The bottom-level list contains all the nodes, and each higher-level list is a sublist of the lower-level lists. Each node is created with a random top level (with a random height), and belongs to all lists up to that level. The probability that a node has the height 1 is 1/2. The probability that a node has the height N is 1/2 ** N (more precisely, the distribution depends on an random generator provided, but our generators have this property).

The lock-free variant of skip-list is implemented according to book

  • [2008] M.Herlihy, N.Shavit "The Art of Multiprocessor Programming", chapter 14.4 "A Lock-Free Concurrent Skiplist"

Template arguments:

  • GC - Garbage collector used.
  • K - type of a key to be stored in the list.
  • T - type of a value to be stored in the list.
  • Traits - map traits, default is skip_list::traits It is possible to declare option-based list with cds::container::skip_list::make_traits metafunction istead of Traits template argument.

Like STL map class, SkipListMap stores the key-value pair as std:pair< K const, T>.

Warning
The skip-list requires up to 67 hazard pointers that may be critical for some GCs for which the guard count is limited (like gc::HP). Those GCs should be explicitly initialized with hazard pointer enough:
cds::gc::HP myhp( 67 )
. Otherwise an run-time exception may be raised when you try to create skip-list object.
Note
There are several specializations of SkipListMap for each GC. You should include:
  • <cds/container/skip_list_map_hp.h> for gc::HP garbage collector
  • <cds/container/skip_list_map_dhp.h> for gc::DHP garbage collector
  • <cds/container/skip_list_map_rcu.h> for RCU type
  • <cds/container/skip_list_map_nogc.h> for non-deletable SkipListMap

Member Typedef Documentation

◆ iterator

template<typename GC , typename Key , typename T , typename Traits = skip_list::traits>
typedef skip_list::details::iterator< typename base_class::iterator > cds::container::SkipListMap< GC, Key, T, Traits >::iterator

Iterator type.

The forward iterator has some features:

  • it is ordered
  • it has no post-increment operator
  • to protect the value, the iterator contains a GC-specific guard + another guard is required locally for increment operator. For some GC (like as gc::HP), a guard is a limited resource per thread, so an exception (or assertion) "no free guard" may be thrown if the limit of guard count per thread is exceeded.
  • The iterator cannot be moved across thread boundary because it contains thread-private GC's guard.
  • Iterator ensures thread-safety even if you delete the item the iterator points to. However, in case of concurrent deleting operations there is no guarantee that you iterate all item in the list. Moreover, a crash is possible when you try to iterate the next element that has been deleted by concurrent thread.
Warning
Use this iterator on the concurrent container for debugging purpose only.
Note
end() and cend() are not dereferenceable.

The iterator interface:

class iterator {
public:
// Default constructor
// Copy construtor
iterator( iterator const& src );
// Dereference operator
value_type * operator ->() const;
// Dereference operator
value_type& operator *() const;
// Preincrement operator
iterator& operator ++();
// Assignment operator
iterator& operator = (iterator const& src);
// Equality operators
bool operator ==(iterator const& i ) const;
bool operator !=(iterator const& i ) const;
};

Member Function Documentation

◆ contains() [1/2]

template<typename GC , typename Key , typename T , typename Traits = skip_list::traits>
template<typename K >
bool cds::container::SkipListMap< GC, Key, T, Traits >::contains ( K const &  key)
inline

Checks whether the map contains key.

The function searches the item with key equal to key and returns true if it is found, and false otherwise.

◆ contains() [2/2]

template<typename GC , typename Key , typename T , typename Traits = skip_list::traits>
template<typename K , typename Less >
bool cds::container::SkipListMap< GC, Key, T, Traits >::contains ( K const &  key,
Less  pred 
)
inline

Checks whether the set contains key using pred predicate for searching.

The function is similar to contains( key ) but pred is used for key comparing. Less functor has the interface like std::less. Less must imply the same element order as the comparator used for building the set.

◆ emplace()

template<typename GC , typename Key , typename T , typename Traits = skip_list::traits>
template<typename K , typename... Args>
bool cds::container::SkipListMap< GC, Key, T, Traits >::emplace ( K &&  key,
Args &&...  args 
)
inline

For key key inserts data of type value_type created in-place from std::forward<Args>(args)...

Returns true if inserting successful, false otherwise.

◆ erase() [1/2]

template<typename GC , typename Key , typename T , typename Traits = skip_list::traits>
template<typename K >
bool cds::container::SkipListMap< GC, Key, T, Traits >::erase ( K const &  key)
inline

Delete key from the map.

Return true if key is found and deleted, false otherwise

◆ erase() [2/2]

template<typename GC , typename Key , typename T , typename Traits = skip_list::traits>
template<typename K , typename Func >
bool cds::container::SkipListMap< GC, Key, T, Traits >::erase ( K const &  key,
Func  f 
)
inline

Delete key from the map.

The function searches an item with key key, calls f functor and deletes the item. If key is not found, the functor is not called.

The functor Func interface:

struct extractor {
void operator()(value_type& item) { ... }
};

Return true if key is found and deleted, false otherwise

◆ erase_with() [1/2]

template<typename GC , typename Key , typename T , typename Traits = skip_list::traits>
template<typename K , typename Less >
bool cds::container::SkipListMap< GC, Key, T, Traits >::erase_with ( K const &  key,
Less  pred 
)
inline

Deletes the item from the map using pred predicate for searching.

The function is an analog of erase(K const&) but pred is used for key comparing. Less functor has the interface like std::less. Less must imply the same element order as the comparator used for building the map.

◆ erase_with() [2/2]

template<typename GC , typename Key , typename T , typename Traits = skip_list::traits>
template<typename K , typename Less , typename Func >
bool cds::container::SkipListMap< GC, Key, T, Traits >::erase_with ( K const &  key,
Less  pred,
Func  f 
)
inline

Deletes the item from the map using pred predicate for searching.

The function is an analog of erase(K const&, Func) but pred is used for key comparing. Less functor has the interface like std::less. Less must imply the same element order as the comparator used for building the map.

◆ extract()

template<typename GC , typename Key , typename T , typename Traits = skip_list::traits>
template<typename K >
guarded_ptr cds::container::SkipListMap< GC, Key, T, Traits >::extract ( K const &  key)
inline

Extracts the item from the map with specified key.

The function searches an item with key equal to key in the map, unlinks it from the map, and returns a guarded pointer to the item found. If key is not found the function returns an empty guarded pointer.

Note the compare functor should accept a parameter of type K that can be not the same as key_type.

The item extracted is freed automatically by garbage collector GC when returned guarded_ptr object will be destroyed or released.

Note
Each guarded_ptr object uses the GC's guard that can be limited resource.

Usage:

skip_list theList;
// ...
{
skip_list::guarded_ptr gp( theList.extract( 5 ));
if ( gp ) {
// Deal with gp
// ...
}
// Destructor of gp releases internal HP guard and frees the pointer
}

◆ extract_max()

template<typename GC , typename Key , typename T , typename Traits = skip_list::traits>
guarded_ptr cds::container::SkipListMap< GC, Key, T, Traits >::extract_max ( )
inline

Extracts an item with maximal key from the map.

The function searches an item with maximal key, unlinks it, and returns a guarded pointer to item found. If the skip-list is empty the function returns an empty guarded_ptr.

The item found is freed by garbage collector GC automatically when returned guarded_ptr object will be destroyed or released.

Note
Each guarded_ptr object uses the GC's guard that can be limited resource.

Usage:

skip_list theList;
// ...
{
skip_list::guarded_ptr gp( theList.extract_max());
if ( gp ) {
// Deal with gp
//...
}
// Destructor of gp releases internal HP guard and then frees the pointer
}

◆ extract_min()

template<typename GC , typename Key , typename T , typename Traits = skip_list::traits>
guarded_ptr cds::container::SkipListMap< GC, Key, T, Traits >::extract_min ( )
inline

Extracts an item with minimal key from the map.

The function searches an item with minimal key, unlinks it, and returns an guarded pointer to the item found. If the skip-list is empty the function returns an empty guarded pointer.

The item extracted is freed automatically by garbage collector GC when returned guarded_ptr object will be destroyed or released.

Note
Each guarded_ptr object uses the GC's guard that can be limited resource.

Usage:

typedef cds::continer::SkipListMap< cds::gc::HP, int, foo, my_traits > skip_list;
skip_list theList;
// ...
{
skip_list::guarded_ptr gp( theList.extract_min());
if ( gp ) {
// Deal with gp
//...
}
// Destructor of gp releases internal HP guard and then frees the pointer
}

◆ extract_with()

template<typename GC , typename Key , typename T , typename Traits = skip_list::traits>
template<typename K , typename Less >
guarded_ptr cds::container::SkipListMap< GC, Key, T, Traits >::extract_with ( K const &  key,
Less  pred 
)
inline

Extracts the item from the map with comparing functor pred.

The function is an analog of extract(K const&) but pred predicate is used for key comparing.

Less functor has the semantics like std::less but should take arguments of type key_type and K in any order. pred must imply the same element order as the comparator used for building the map.

◆ find()

template<typename GC , typename Key , typename T , typename Traits = skip_list::traits>
template<typename K , typename Func >
bool cds::container::SkipListMap< GC, Key, T, Traits >::find ( K const &  key,
Func  f 
)
inline

Find the key key.

The function searches the item with key equal to key and calls the functor f for item found. The interface of Func functor is:

struct functor {
void operator()( value_type& item );
};

where item is the item found.

The functor may change item.second.

The function returns true if key is found, false otherwise.

◆ find_with()

template<typename GC , typename Key , typename T , typename Traits = skip_list::traits>
template<typename K , typename Less , typename Func >
bool cds::container::SkipListMap< GC, Key, T, Traits >::find_with ( K const &  key,
Less  pred,
Func  f 
)
inline

Finds the key val using pred predicate for searching.

The function is an analog of find(K const&, Func) but pred is used for key comparing. Less functor has the interface like std::less. Less must imply the same element order as the comparator used for building the map.

◆ get()

template<typename GC , typename Key , typename T , typename Traits = skip_list::traits>
template<typename K >
guarded_ptr cds::container::SkipListMap< GC, Key, T, Traits >::get ( K const &  key)
inline

Finds the key key and return the item found.

The function searches the item with key equal to key and returns a guarded pointer to the item found. If key is not found the function returns an empty guarded pointer.

It is safe when a concurrent thread erases the item returned as guarded_ptr. In this case the item will be freed later by garbage collector GC automatically when guarded_ptr object will be destroyed or released.

Note
Each guarded_ptr object uses one GC's guard which can be limited resource.

Usage:

skip_list theList;
// ...
{
skip_list::guarded_ptr gp( theList.get( 5 ));
if ( gp ) {
// Deal with gp
//...
}
// Destructor of guarded_ptr releases internal HP guard
}

Note the compare functor specified for class Traits template parameter should accept a parameter of type K that can be not the same as value_type.

◆ get_with()

template<typename GC , typename Key , typename T , typename Traits = skip_list::traits>
template<typename K , typename Less >
guarded_ptr cds::container::SkipListMap< GC, Key, T, Traits >::get_with ( K const &  key,
Less  pred 
)
inline

Finds the key key and return the item found.

The function is an analog of get( K const&) but pred is used for comparing the keys.

Less functor has the semantics like std::less but should take arguments of type key_type and K in any order. pred must imply the same element order as the comparator used for building the map.

◆ insert() [1/2]

template<typename GC , typename Key , typename T , typename Traits = skip_list::traits>
template<typename K >
bool cds::container::SkipListMap< GC, Key, T, Traits >::insert ( K const &  key)
inline

Inserts new node with key and default value.

The function creates a node with key and default value, and then inserts the node created into the map.

Preconditions:

  • The key_type should be constructible from a value of type K. In trivial case, K is equal to key_type.
  • The mapped_type should be default-constructible.

Returns true if inserting successful, false otherwise.

◆ insert() [2/2]

template<typename GC , typename Key , typename T , typename Traits = skip_list::traits>
template<typename K , typename V >
bool cds::container::SkipListMap< GC, Key, T, Traits >::insert ( K const &  key,
V const &  val 
)
inline

Inserts new node.

The function creates a node with copy of val value and then inserts the node created into the map.

Preconditions:

  • The key_type should be constructible from key of type K.
  • The value_type should be constructible from val of type V.

Returns true if val is inserted into the set, false otherwise.

◆ insert_with()

template<typename GC , typename Key , typename T , typename Traits = skip_list::traits>
template<typename K , typename Func >
bool cds::container::SkipListMap< GC, Key, T, Traits >::insert_with ( K const &  key,
Func  func 
)
inline

Inserts new node and initialize it by a functor.

This function inserts new node with key key and if inserting is successful then it calls func functor with signature

struct functor {
void operator()( value_type& item );
};

The argument item of user-defined functor func is the reference to the map's item inserted:

  • item.first is a const reference to item's key that cannot be changed.
  • item.second is a reference to item's value that may be changed.

key_type should be constructible from value of type K.

The function allows to split creating of new item into two part:

  • create item from key;
  • insert new item into the map;
  • if inserting is successful, initialize the value of item by calling func functor

This can be useful if complete initialization of object of value_type is heavyweight and it is preferable that the initialization should be completed only if inserting is successful.

◆ update()

template<typename GC , typename Key , typename T , typename Traits = skip_list::traits>
template<typename K , typename Func >
std::pair<bool, bool> cds::container::SkipListMap< GC, Key, T, Traits >::update ( K const &  key,
Func  func,
bool  bInsert = true 
)
inline

Updates data by key.

The operation performs inserting or changing data with lock-free manner.

If the key not found in the map, then the new item created from key will be inserted into the map iff bInsert is true (note that in this case the key_type should be constructible from type K). Otherwise, if key is found, the functor func is called with item found. The functor Func signature:

struct my_functor {
void operator()( bool bNew, value_type& item );
};

where:

  • bNew - true if the item has been inserted, false otherwise
  • item - item of the map

The functor may change any fields of the item.second that is value_type.

Returns std::pair<bool, bool> where first is true if operation is successful, second is true if new item has been added or false if key already exists.

Warning
See insert item troubleshooting

The documentation for this class was generated from the following files:

cds 2.3.2 Developed by Maxim Khizhinsky aka khizmax and other contributors 2007 - 2017
Autogenerated Sun Dec 31 2017 12:10:26 by Doxygen 1.8.13