cds  2.2.0
cds::container::MichaelHashMap< GC, OrderedList, Traits > Class Template Reference

Michael's hash map. More...

#include <cds/container/michael_map.h>

Public Types

typedef GC gc
 Garbage collector.
 
typedef OrderedList ordered_list
 type of ordered list to be used as a bucket
 
typedef Traits traits
 Map traits.
 
typedef ordered_list::key_type key_type
 key type
 
typedef ordered_list::mapped_type mapped_type
 value type
 
typedef ordered_list::value_type value_type
 key/value pair stored in the map
 
typedef traits::allocator allocator
 Bucket table allocator.
 
typedef ordered_list::key_comparator key_comparator
 key compare functor
 
typedef ordered_list::stat stat
 
typedef ordered_list::guarded_ptr guarded_ptr
 Guarded pointer - a result of get() and extract() functions.
 
typedef cds::opt::v::hash_selector< typename traits::hash >::type hash
 Hash functor for key_type and all its derivatives that you use.
 
typedef traits::item_counter item_counter
 Item counter type.
 

Public Member Functions

 MichaelHashMap (size_t nMaxItemCount, size_t nLoadFactor)
 Initializes the map. More...
 
 ~MichaelHashMap ()
 Clears hash map and destroys it.
 
template<typename K >
bool insert (K &&key)
 Inserts new node with key and default value. More...
 
template<typename K , typename V >
bool insert (K &&key, V &&val)
 Inserts new node. More...
 
template<typename K , typename Func >
bool insert_with (K &&key, Func func)
 Inserts new node and initialize it by a functor. More...
 
template<typename K , typename Func >
std::pair< bool, bool > update (K &&key, Func func, bool bAllowInsert=true)
 Updates data by key. More...
 
template<typename Q , typename V >
std::pair< bool, bool > upsert (Q &&key, V &&val, bool bAllowInsert=true)
 Inserts or updates the node (only for IterableKVList) More...
 
template<typename K , typename... Args>
bool emplace (K &&key, Args &&... args)
 For key key inserts data of type mapped_type created from args. More...
 
template<typename K >
bool erase (K const &key)
 Deletes 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)
 Deletes 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 with specified key. More...
 
template<typename K , typename Less >
guarded_ptr extract_with (K const &key, Less pred)
 Extracts the item using compare functor pred. More...
 
template<typename K , typename Func >
bool find (K const &key, Func f)
 Finds the key key. More...
 
template<typename K >
iterator find (K const &key)
 Finds key and returns iterator pointed to the item found (only for IterableList) 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 , typename Less >
iterator find_with (K const &key, Less pred)
 Finds key using pred predicate and returns iterator pointed to the item found (only for IterableList) 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 map contains key using pred predicate for searching. More...
 
template<typename K >
guarded_ptr get (K const &key)
 Finds key and return the item found. More...
 
template<typename K , typename Less >
guarded_ptr get_with (K const &key, Less pred)
 Finds key and return the item found. More...
 
void clear ()
 Clears the map (not atomic)
 
bool empty () const
 Checks if the map is empty. More...
 
size_t size () const
 Returns item count in the map.
 
size_t bucket_count () const
 Returns the size of hash table. More...
 
stat const & statistics () const
 Returns const reference to internal statistics.
 

Static Public Attributes

static constexpr const size_t c_nHazardPtrCount = ordered_list::c_nHazardPtrCount
 Count of hazard pointer required.
 

Forward iterators (only for debugging purpose)

typedef iterator_type< false > iterator
 Forward iterator. More...
 
typedef iterator_type< true > const_iterator
 Const forward iterator.
 
iterator begin ()
 Returns a forward iterator addressing the first element in a map. More...
 
iterator end ()
 Returns an iterator that addresses the location succeeding the last element in a map. More...
 
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.
 
const_iterator end () const
 Returns an const iterator that addresses the location succeeding the last element in a map.
 
const_iterator cend () const
 Returns an const iterator that addresses the location succeeding the last element in a map.
 

Detailed Description

template<class GC, class OrderedList, class Traits = michael_map::traits>
class cds::container::MichaelHashMap< GC, OrderedList, Traits >

Michael's hash map.

Source:

  • [2002] Maged Michael "High performance dynamic lock-free hash tables and list-based sets"

Michael's hash table algorithm is based on lock-free ordered list and it is very simple. The main structure is an array T of size M. Each element in T is basically a pointer to a hash bucket, implemented as a singly linked list. The array of buckets cannot be dynamically expanded. However, each bucket may contain unbounded number of items.

Template parameters are:

  • GC - Garbage collector used. You may use any Garbage collector from the libcds library. Note the GC must be the same as the GC used for OrderedList
  • OrderedList - ordered key-value list implementation used as bucket for hash map, for example, MichaelKVList, LazyKVList, IterableKVList. The ordered list implementation specifies the Key and Value types stored in the hash-map, the reclamation schema GC used by hash-map, the comparison functor for the type Key and other features specific for the ordered list.
  • Traits - map traits, default is michael_map::traits. Instead of defining Traits struct you may use option-based syntax with michael_map::make_traits metafunction.

Many of the class function take a key argument of type K that in general is not key_type. key_type and an argument of template type K must meet the following requirements:

  • key_type should be constructible from value of type K;
  • the hash functor should be able to calculate correct hash value from argument key of type K: hash( key_type(key)) == hash( key )
  • values of type key_type and K should be comparable

There are the specializations:

How to use

Suppose, you want to make int to int map for Hazard Pointer garbage collector. You should choose suitable ordered list class that will be used as a bucket for the map; it may be MichaelKVList.

#include <cds/container/michael_kvlist_hp.h> // MichaelKVList for gc::HP
#include <cds/container/michael_map.h> // MichaelHashMap
// List traits based on std::less predicate
struct list_traits: public cds::container::michael_list::traits
{
typedef std::less<int> less;
};
// Ordered list
// Map traits
struct map_traits: public cds::container::michael_map::traits
{
struct hash {
size_t operator()( int i ) const
{
return cds::opt::v::hash<int>()( i );
}
}
};
// Your map
// Now you can use int2int_map class
int main()
{
int2int_map theMap;
theMap.insert( 100 );
...
}

You may use option-based declaration:

#include <cds/container/michael_kvlist_hp.h> // MichaelKVList for gc::HP
#include <cds/container/michael_map.h> // MichaelHashMap
// Ordered list
typedef cds::container::MichaelKVList< cds::gc::HP, int, int,
cds::container::opt::less< std::less<int> > // item comparator option
>::type
> int2int_list;
// Map
typedef cds::container::MichaelHashMap< cds::gc::HP, int2int_list,
cc::opt::hash< cds::opt::v::hash<int> >
>
> int2int_map;

Member Typedef Documentation

§ iterator

template<class GC , class OrderedList , class Traits = michael_map::traits>
typedef iterator_type< false > cds::container::MichaelHashMap< GC, OrderedList, Traits >::iterator

Forward iterator.

The forward iterator for Michael's map has some features:

  • 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 thread safety depends on type of OrderedList:

  • for MichaelKVList and LazyKVList: iterator guarantees safety even if you delete the item that iterator points to because that item is guarded by hazard pointer. However, in case of concurrent deleting operations it is no guarantee that you iterate all item in the map. Moreover, a crash is possible when you try to iterate the next element that has been deleted by concurrent thread. Use this iterator on the concurrent container for debugging purpose only.
  • for IterableList: iterator is thread-safe. You may use it freely in concurrent environment.

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;
};
Note
The iterator object returned by end(), cend() member functions points to nullptr and should not be dereferenced.

§ stat

template<class GC , class OrderedList , class Traits = michael_map::traits>
typedef ordered_list::stat cds::container::MichaelHashMap< GC, OrderedList, Traits >::stat

Internal statistics

Constructor & Destructor Documentation

§ MichaelHashMap()

template<class GC , class OrderedList , class Traits = michael_map::traits>
cds::container::MichaelHashMap< GC, OrderedList, Traits >::MichaelHashMap ( size_t  nMaxItemCount,
size_t  nLoadFactor 
)
inline

Initializes the map.

The Michael's hash map is non-expandable container. You should point the average count of items nMaxItemCount when you create an object. nLoadFactor parameter defines average count of items per bucket and it should be small number between 1 and 10. Remember, since the bucket implementation is an ordered list, searching in the bucket is linear [O(nLoadFactor)]. Note, that many popular STL hash map implementation uses load factor 1.

The ctor defines hash table size as rounding nMacItemCount / nLoadFactor up to nearest power of two.

Parameters
nMaxItemCountestimation of max item count in the hash map
nLoadFactorload factor: estimation of max number of items in the bucket

Member Function Documentation

§ begin()

template<class GC , class OrderedList , class Traits = michael_map::traits>
iterator cds::container::MichaelHashMap< GC, OrderedList, Traits >::begin ( )
inline

Returns a forward iterator addressing the first element in a map.

For empty map

begin() == end()

§ bucket_count()

template<class GC , class OrderedList , class Traits = michael_map::traits>
size_t cds::container::MichaelHashMap< GC, OrderedList, Traits >::bucket_count ( ) const
inline

Returns the size of hash table.

Since MichaelHashMap cannot dynamically extend the hash table size, the value returned is an constant depending on object initialization parameters; see MichaelHashMap::MichaelHashMap for explanation.

§ contains() [1/2]

template<class GC , class OrderedList , class Traits = michael_map::traits>
template<typename K >
bool cds::container::MichaelHashMap< GC, OrderedList, 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<class GC , class OrderedList , class Traits = michael_map::traits>
template<typename K , typename Less >
bool cds::container::MichaelHashMap< GC, OrderedList, Traits >::contains ( K const &  key,
Less  pred 
)
inline

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

The function is an analog of 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 map.

§ emplace()

template<class GC , class OrderedList , class Traits = michael_map::traits>
template<typename K , typename... Args>
bool cds::container::MichaelHashMap< GC, OrderedList, Traits >::emplace ( K &&  key,
Args &&...  args 
)
inline

For key key inserts data of type mapped_type created from args.

key_type should be constructible from type K

Returns true if inserting successful, false otherwise.

§ empty()

template<class GC , class OrderedList , class Traits = michael_map::traits>
bool cds::container::MichaelHashMap< GC, OrderedList, Traits >::empty ( ) const
inline

Checks if the map is empty.

Emptiness is checked by item counting: if item count is zero then the map is empty. Thus, the correct item counting is an important part of the map implementation.

§ end()

template<class GC , class OrderedList , class Traits = michael_map::traits>
iterator cds::container::MichaelHashMap< GC, OrderedList, Traits >::end ( )
inline

Returns an iterator that addresses the location succeeding the last element in a map.

Do not use the value returned by end function to access any item. The returned value can be used only to control reaching the end of the map. For empty map

begin() == end()

§ erase() [1/2]

template<class GC , class OrderedList , class Traits = michael_map::traits>
template<typename K >
bool cds::container::MichaelHashMap< GC, OrderedList, Traits >::erase ( K const &  key)
inline

Deletes key from the map.

Return true if key is found and deleted, false otherwise

§ erase() [2/2]

template<class GC , class OrderedList , class Traits = michael_map::traits>
template<typename K , typename Func >
bool cds::container::MichaelHashMap< GC, OrderedList, Traits >::erase ( K const &  key,
Func  f 
)
inline

Deletes 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<class GC , class OrderedList , class Traits = michael_map::traits>
template<typename K , typename Less >
bool cds::container::MichaelHashMap< GC, OrderedList, 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<class GC , class OrderedList , class Traits = michael_map::traits>
template<typename K , typename Less , typename Func >
bool cds::container::MichaelHashMap< GC, OrderedList, 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<class GC , class OrderedList , class Traits = michael_map::traits>
template<typename K >
guarded_ptr cds::container::MichaelHashMap< GC, OrderedList, Traits >::extract ( K const &  key)
inline

Extracts the item with specified key.

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

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

The extracted item is freed 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:

michael_map theMap;
// ...
{
michael_map::guarded_ptr gp( theMap.extract( 5 ));
if ( gp ) {
// Deal with gp
// ...
}
// Destructor of gp releases internal HP guard
}

§ extract_with()

template<class GC , class OrderedList , class Traits = michael_map::traits>
template<typename K , typename Less >
guarded_ptr cds::container::MichaelHashMap< GC, OrderedList, Traits >::extract_with ( K const &  key,
Less  pred 
)
inline

Extracts the item using compare 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() [1/2]

template<class GC , class OrderedList , class Traits = michael_map::traits>
template<typename K , typename Func >
bool cds::container::MichaelHashMap< GC, OrderedList, Traits >::find ( K const &  key,
Func  f 
)
inline

Finds 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. Note that the functor is only guarantee that item cannot be disposed during functor is executing. The functor does not serialize simultaneous access to the map's item. If such access is possible you must provide your own synchronization schema on item level to exclude unsafe item modifications.

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

§ find() [2/2]

template<class GC , class OrderedList , class Traits = michael_map::traits>
template<typename K >
iterator cds::container::MichaelHashMap< GC, OrderedList, Traits >::find ( K const &  key)
inline

Finds key and returns iterator pointed to the item found (only for IterableList)

If key is not found the function returns end().

Note
This function is supported only for map based on IterableList

§ find_with() [1/2]

template<class GC , class OrderedList , class Traits = michael_map::traits>
template<typename K , typename Less , typename Func >
bool cds::container::MichaelHashMap< GC, OrderedList, 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.

§ find_with() [2/2]

template<class GC , class OrderedList , class Traits = michael_map::traits>
template<typename K , typename Less >
iterator cds::container::MichaelHashMap< GC, OrderedList, Traits >::find_with ( K const &  key,
Less  pred 
)
inline

Finds key using pred predicate and returns iterator pointed to the item found (only for IterableList)

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

If key is not found the function returns end().

Note
This function is supported only for map based on IterableList

§ get()

template<class GC , class OrderedList , class Traits = michael_map::traits>
template<typename K >
guarded_ptr cds::container::MichaelHashMap< GC, OrderedList, Traits >::get ( K const &  key)
inline

Finds key and return the item found.

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

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

Usage:

typedef cds::container::MichaeHashMap< your_template_params > michael_map;
michael_map theMap;
// ...
{
michael_map::guarded_ptr gp( theMap.get( 5 ));
if ( gp ) {
// Deal with gp
//...
}
// Destructor of guarded_ptr releases internal HP guard
}

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

§ get_with()

template<class GC , class OrderedList , class Traits = michael_map::traits>
template<typename K , typename Less >
guarded_ptr cds::container::MichaelHashMap< GC, OrderedList, Traits >::get_with ( K const &  key,
Less  pred 
)
inline

Finds 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<class GC , class OrderedList , class Traits = michael_map::traits>
template<typename K >
bool cds::container::MichaelHashMap< GC, OrderedList, Traits >::insert ( K &&  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 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<class GC , class OrderedList , class Traits = michael_map::traits>
template<typename K , typename V >
bool cds::container::MichaelHashMap< GC, OrderedList, Traits >::insert ( K &&  key,
V &&  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 mapped_type should be constructible from val of type V.

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

§ insert_with()

template<class GC , class OrderedList , class Traits = michael_map::traits>
template<typename K , typename Func >
bool cds::container::MichaelHashMap< GC, OrderedList, Traits >::insert_with ( K &&  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.

The user-defined functor is called only if inserting is successful.

The 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 mapped_type is heavyweight and it is preferable that the initialization should be completed only if inserting is successful.

Warning
For MichaelKVList as the bucket see insert item troubleshooting. LazyKVList provides exclusive access to inserted item and does not require any node-level synchronization.

§ update()

template<class GC , class OrderedList , class Traits = michael_map::traits>
template<typename K , typename Func >
std::pair<bool, bool> cds::container::MichaelHashMap< GC, OrderedList, Traits >::update ( K &&  key,
Func  func,
bool  bAllowInsert = true 
)
inline

Updates data by key.

The operation performs inserting or replacing the element 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 bAllowInsert 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 depends on OrderedList:

for MichaelKVList, LazyKVList

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

with arguments:

  • bNew - true if the item has been inserted, false otherwise
  • item - the item found or inserted

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

for IterableKVList

void func( value_type& val, value_type * old );

where

  • val - a new data constructed from key
  • old - old value that will be retired. If new item has been inserted then old is nullptr.

The functor may change non-key fields of val; however, func must guarantee that during changing no any other modifications could be made on this item by concurrent threads.

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 the item with key already exists.
Warning
For MichaelKVList and IterableKVList as the bucket see insert item troubleshooting. LazyKVList provides exclusive access to inserted item and does not require any node-level synchronization.

§ upsert()

template<class GC , class OrderedList , class Traits = michael_map::traits>
template<typename Q , typename V >
std::pair<bool, bool> cds::container::MichaelHashMap< GC, OrderedList, Traits >::upsert ( Q &&  key,
V &&  val,
bool  bAllowInsert = true 
)
inline

Inserts or updates the node (only for IterableKVList)

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

If key is not found in the map, then key is inserted iff bAllowInsert is true. Otherwise, the current element is changed to val, the old element will be retired later.

Returns std::pair<bool, bool> where first is true if operation is successful, second is true if val has been added or false if the item with that key already in the map.


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

cds 2.2.0 Developed by Maxim Khizhinsky aka khizmax 2007 - 2017
Autogenerated Wed Jan 4 2017 08:49:38 by Doxygen 1.8.12