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

Michael's hash map (template specialization for cds::gc::nogc) More...

#include <cds/container/michael_map_nogc.h>

Public Types

typedef cds::gc::nogc gc
 No garbage collector.
 
typedef OrderedList ordered_list
 type of ordered list used as a bucket implementation
 
typedef Traits traits
 Map traits.
 
typedef ordered_list::key_type key_type
 key type
 
typedef ordered_list::mapped_type mapped_type
 type of value to be stored in the map
 
typedef ordered_list::value_type value_type
 Pair used as the some functor's argument.
 
typedef ordered_list::key_comparator key_comparator
 key comparing functor
 
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.
 
typedef traits::allocator allocator
 Bucket table allocator.
 
typedef ordered_list::stat stat
 Internal statistics.
 

Public Member Functions

 MichaelHashMap (size_t nMaxItemCount, size_t nLoadFactor)
 Initialize the map. More...
 
 ~MichaelHashMap ()
 Clears hash set and destroys it.
 
template<typename K >
iterator insert (const K &key)
 Inserts new node with key and default value. More...
 
template<typename K , typename V >
iterator insert (K const &key, V const &val)
 Inserts new node. More...
 
template<typename K , typename Func >
iterator insert_with (const K &key, Func func)
 Inserts new node and initialize it by a functor. More...
 
template<typename K , typename... Args>
iterator emplace (K &&key, Args &&... args)
 For key key inserts data of type mapped_type created from args. More...
 
template<typename K >
std::pair< iterator, bool > update (const K &key, bool bAllowInsert=true)
 Updates the item. More...
 
template<typename K >
iterator contains (K const &key)
 Checks whether the map contains key. More...
 
template<typename K , typename Less >
iterator contains (K const &key, Less pred)
 Checks whether the map contains key using pred predicate for searching. More...
 
void clear ()
 Clears the map (not atomic)
 
bool empty () const
 Checks whether the map is empty. More...
 
size_t size () const
 Returns item count in the map.
 
stat const & statistics () const
 Returns const reference to internal statistics.
 
size_t bucket_count () const
 Returns the size of hash table. More...
 

Forward iterators

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 set. More...
 
iterator end ()
 Returns an iterator that addresses the location succeeding the last element in a set. More...
 
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.
 
const_iterator end () const
 Returns an const iterator that addresses the location succeeding the last element in a set.
 
const_iterator cend () const
 Returns an const iterator that addresses the location succeeding the last element in a set.
 

Detailed Description

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

Michael's hash map (template specialization for cds::gc::nogc)

This specialization is so-called append-only when no item reclamation may be performed. The class does not support deleting of map item.

See MichaelHashMap for description of template parameters.

Member Typedef Documentation

§ iterator

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

Forward iterator.

The forward iterator for Michael's map is based on OrderedList forward iterator and has some features:

  • it has no post-increment operator
  • it iterates items in unordered fashion

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;
};

Constructor & Destructor Documentation

§ MichaelHashMap()

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

Initialize 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 set
nLoadFactorload factor: estimation of max number of items in the bucket

Member Function Documentation

§ begin()

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

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

For empty set

begin() == end()

§ bucket_count()

template<class OrderedList , class Traits = michael_map::traits>
size_t cds::container::MichaelHashMap< cds::gc::nogc, 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 OrderedList , class Traits = michael_map::traits>
template<typename K >
iterator cds::container::MichaelHashMap< cds::gc::nogc, 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 an iterator pointed to item found and end() otherwise

§ contains() [2/2]

template<class OrderedList , class Traits = michael_map::traits>
template<typename K , typename Less >
iterator cds::container::MichaelHashMap< cds::gc::nogc, 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. pred must imply the same element order as the comparator used for building the map. Hash functor specified in Traits should accept parameters of type K.

§ emplace()

template<class OrderedList , class Traits = michael_map::traits>
template<typename K , typename... Args>
iterator cds::container::MichaelHashMap< cds::gc::nogc, 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 an iterator pointed to inserted value, or end() if inserting is failed

§ empty()

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

Checks whether 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 feature is an important part of Michael's map implementation.

§ end()

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

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

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 set. For empty set

begin() == end()

§ insert() [1/2]

template<class OrderedList , class Traits = michael_map::traits>
template<typename K >
iterator cds::container::MichaelHashMap< cds::gc::nogc, OrderedList, Traits >::insert ( const 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 an iterator pointed to inserted value, or end() if inserting is failed

§ insert() [2/2]

template<class OrderedList , class Traits = michael_map::traits>
template<typename K , typename V >
iterator cds::container::MichaelHashMap< cds::gc::nogc, OrderedList, 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 mapped_type should be constructible from val of type V.

Returns an iterator pointed to inserted value, or end() if inserting is failed

§ insert_with()

template<class OrderedList , class Traits = michael_map::traits>
template<typename K , typename Func >
iterator cds::container::MichaelHashMap< cds::gc::nogc, OrderedList, Traits >::insert_with ( const 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.second is a reference to item's value that may be changed.

The user-defined functor it is called only if the 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 f 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.

Returns an iterator pointed to inserted value, or end() if inserting is failed

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 OrderedList , class Traits = michael_map::traits>
template<typename K >
std::pair<iterator, bool> cds::container::MichaelHashMap< cds::gc::nogc, OrderedList, Traits >::update ( const K &  key,
bool  bAllowInsert = true 
)
inline

Updates the item.

If key is not in the map and bAllowInsert is true, the function inserts a new item. Otherwise, the function returns an iterator pointing to the item found.

Returns std::pair<iterator, bool> where first is an iterator pointing to item found or inserted (if inserting is not allowed and key is not found, the iterator will be end()),

second is true if new item has been added or false if the item already is in the map.

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.

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