cds  1.4.0
Public Types | Public Member Functions
cds::container::SplitListMap< GC, Key, Value, Traits > Class Template Reference

Split-ordered list map. More...

#include <cds/container/split_list_map.h>

Inheritance diagram for cds::container::SplitListMap< GC, Key, Value, Traits >:
cds::container::SplitListSet< GC, std::pair< Key const, Value >, split_list::details::wrap_map_traits< Key, Value, Traits > > cds::intrusive::SplitListSet< GC, split_list::details::wrap_map_traits< Key, Value, Traits >::ordered_list, split_list::details::wrap_map_traits< Key, Value, Traits > >

Public Types

typedef base_class::gc gc
 Garbage collector.
 
typedef Key key_type
 key type
 
typedef Value mapped_type
 type of value stored in the map
 
typedef std::pair< key_type
const, mapped_type
value_type
 key-value pair type
 
typedef base_class::ordered_list ordered_list
 Underlying ordered list class.
 
typedef base_class::key_comparator key_comparator
 key compare functor
 
typedef base_class::hash hash
 Hash functor for key_type.
 
typedef base_class::item_counter item_counter
 Item counter type.
 
typedef base_class::iterator iterator
 Forward iterator (see SplitListSet::iterator) More...
 
typedef base_class::const_iterator const_iterator
 Const forward iterator (see SplitListSet::const_iterator)
 

Public Member Functions

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...
 
 SplitListMap ()
 Initializes split-ordered map of default capacity. More...
 
 SplitListMap (size_t nItemCount, size_t nLoadFactor=1)
 Initializes split-ordered map. More...
 
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_key (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 mapped_type constructed with std::forward<Args>(args)... More...
 
template<typename K , typename Func >
std::pair< bool, bool > ensure (K const &key, Func func)
 Ensures that the key exists in the map. 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 , typename Func >
bool find (K const &key, Func f)
 Finds 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 find (K const &key)
 Finds the key key. More...
 
template<typename K , typename Less >
bool find_with (K const &key, Less pred)
 Finds the key val using pred predicate for searching. More...
 
void clear ()
 Clears the map (non-atomic) More...
 
bool empty () const
 Checks if the map is empty. More...
 
size_t size () const
 Returns item count in the map.
 
const_iterator begin () const
 Returns a forward const iterator addressing the first element in a map.
 
const_iterator cbegin ()
 
const_iterator end () const
 Returns an const iterator that addresses the location succeeding the last element in a map.
 
const_iterator cend ()
 

Additional Inherited Members

- Protected Types inherited from cds::container::SplitListSet< GC, std::pair< Key const, Value >, split_list::details::wrap_map_traits< Key, Value, Traits > >
typedef options::gc gc
 Garbage collector.
 
typedef options::value_type value_type
 type of value stored in the list
 
typedef options::ordered_list ordered_list
 Underlying ordered list class.
 
typedef base_class::key_comparator key_comparator
 key compare functor
 
typedef base_class::hash hash
 Hash functor for value_type and all its derivatives that you use.
 
typedef base_class::item_counter item_counter
 Item counter type.
 
typedef iterator_type< false > iterator
 Forward iterator.
 
typedef iterator_type< true > const_iterator
 Const forward iterator.
 
- Protected Member Functions inherited from cds::container::SplitListSet< GC, std::pair< Key const, Value >, split_list::details::wrap_map_traits< Key, Value, Traits > >
 SplitListSet ()
 Initializes split-ordered list of default capacity. More...
 
 SplitListSet (size_t nItemCount, size_t nLoadFactor=1)
 Initializes split-ordered list. More...
 
iterator begin ()
 Returns a forward iterator addressing the first element in a set. More...
 
const_iterator begin () const
 Returns a forward const iterator addressing the first element in a set.
 
iterator end ()
 Returns an iterator that addresses the location succeeding the last element in a set. More...
 
const_iterator end () const
 Returns an const iterator that addresses the location succeeding the last element in a set.
 
bool insert (Q const &val)
 Inserts new node. More...
 
bool insert (Q const &val, Func f)
 Inserts new node. More...
 
bool emplace (Args &&...args)
 Inserts data of type value_type constructed with std::forward<Args>(args)... More...
 
std::pair< bool, bool > ensure (Q const &val, Func func)
 Ensures that the item exists in the set. More...
 
bool erase (Q const &key)
 Deletes key from the set. More...
 
bool erase (Q const &key, Func f)
 Deletes key from the set. More...
 
bool erase_with (Q const &key, Less pred)
 Deletes the item from the set using pred predicate for searching. More...
 
bool erase_with (Q const &key, Less pred, Func f)
 Deletes the item from the set using pred predicate for searching. More...
 
bool find (Q &val, Func f)
 Finds the key val. More...
 
bool find (Q const &val, Func f)
 Finds the key val. More...
 
bool find (Q const &val)
 Finds the key val. More...
 
bool find_with (Q &val, Less pred, Func f)
 Finds the key val using pred predicate for searching. More...
 
bool find_with (Q const &val, Less pred, Func f)
 Finds the key val using pred predicate for searching. More...
 
bool find_with (Q const &val, Less pred)
 Finds the key val using pred predicate for searching. More...
 
void clear ()
 Clears the set (non-atomic) More...
 
bool empty () const
 Checks if the set is empty. More...
 
size_t size () const
 Returns item count in the set.
 

Detailed Description

template<class GC, typename Key, typename Value, class Traits = split_list::type_traits>
class cds::container::SplitListMap< GC, Key, Value, Traits >

Split-ordered list map.

Hash table implementation based on split-ordered list algorithm discovered by Ori Shalev and Nir Shavit, see

See intrusive::SplitListSet for a brief description of the split-list algorithm.

Template parameters:

There are the specializations:

Usage

You should decide what garbage collector you want, and what ordered list you want to use. Split-ordered list is original data structure based on an ordered list. Suppose, you want construct split-list map based on gc::HP GC and MichaelList as ordered list implementation. Your map should map int key to std::string value. So, you beginning your program with following include:

#include <cds/container/michael_list_hp.h>
#include <cds/container/split_list_map.h>
namespace cc = cds::container ;

The inclusion order is important: first, include file for ordered-list implementation (for this example, cds/container/michael_list_hp.h), then the header for split-list map cds/container/split_list_map.h.

Now, you should declare traits for split-list map. The main parts of traits are a hash functor for the map key and a comparing functor for ordered list. We use std::hash<int> as hash functor and std::less<int> predicate as comparing functor.

The second attention: instead of using MichaelList in SplitListMap traits we use a tag cds::contaner::michael_list_tag for the Michael's list. The split-list requires significant support from underlying ordered list class and it is not good idea to dive you into deep implementation details of split-list and ordered list interrelations. The tag paradigm simplifies split-list interface.

// SplitListMap traits
struct foo_set_traits: public cc::split_list::type_traits
{
typedef cc::michael_list_tag ordered_list ; // what type of ordered list we want to use
typedef std::hash<int> hash ; // hash functor for the key stored in split-list map
// Type traits for our MichaelList class
struct ordered_list_traits: public cc::michael_list::type_traits
{
typedef std::less<int> less ; // use our std::less predicate as comparator to order list nodes
} ;
};

Now you are ready to declare our map class based on SplitListMap:

typedef cc::SplitListMap< cds::gc::PTB, int, std::string, foo_set_traits > int_string_map ;

You may use the modern option-based declaration instead of classic type-traits-based one:

typedef cc:SplitListMap<
cs::gc::PTB // GC used
,int // key type
,std::string // value type
,cc::split_list::make_traits< // metafunction to build split-list traits
cc::split_list::ordered_list<cc::michael_list_tag> // tag for underlying ordered list implementation
,cc::opt::hash< std::hash<int> > // hash functor
,cc::split_list::ordered_list_traits< // ordered list traits desired
cc::michael_list::make_traits< // metafunction to build lazy list traits
cc::opt::less< std::less<int> > // less-based compare functor
>::type
>
>::type
> int_string_map ;

In case of option-based declaration using split_list::make_traits metafunction the struct foo_set_traits is not required.

Now, the map of type int_string_map is ready to use in your program.

Note that in this example we show only mandatory type_traits parts, optional ones is the default and they are inherited from cds::container::split_list::type_traits. The cds library contains many other options for deep tuning of behavior of the split-list and ordered-list containers.

Member Typedef Documentation

template<class GC , typename Key , typename Value , class Traits = split_list::type_traits>
typedef base_class::iterator cds::container::SplitListMap< GC, Key, Value, Traits >::iterator

Forward iterator (see SplitListSet::iterator)

Remember, the iterator operator -> and operator * returns value_type pointer and reference. To access item key and value use it->first and it->second respectively.

Constructor & Destructor Documentation

template<class GC , typename Key , typename Value , class Traits = split_list::type_traits>
cds::container::SplitListMap< GC, Key, Value, Traits >::SplitListMap ( )
inline

Initializes split-ordered map of default capacity.

The default capacity is defined in bucket table constructor. See intrusive::split_list::expandable_bucket_table, intrusive::split_list::static_bucket_table which selects by intrusive::split_list::dynamic_bucket_table option.

template<class GC , typename Key , typename Value , class Traits = split_list::type_traits>
cds::container::SplitListMap< GC, Key, Value, Traits >::SplitListMap ( size_t  nItemCount,
size_t  nLoadFactor = 1 
)
inline

Initializes split-ordered map.

Parameters
nItemCountestimate average item count
nLoadFactorload factor - average item count per bucket. Small integer up to 10, default is 1.

Member Function Documentation

template<class GC , typename Key , typename Value , class Traits = split_list::type_traits>
iterator cds::container::SplitListMap< GC, Key, Value, Traits >::begin ( )
inline

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

For empty map

begin() == end()
template<class GC , typename Key , typename Value , class Traits = split_list::type_traits>
void cds::container::SplitListMap< GC, Key, Value, Traits >::clear ( )
inline

Clears the map (non-atomic)

The function unlink all items from the map. The function is not atomic and not lock-free and should be used for debugging only.

template<class GC , typename Key , typename Value , class Traits = split_list::type_traits>
template<typename K , typename... Args>
bool cds::container::SplitListMap< GC, Key, Value, Traits >::emplace ( K &&  key,
Args &&...  args 
)
inline

For key key inserts data of type mapped_type constructed with std::forward<Args>(args)...

key_type should be constructible from type K

Returns true if inserting successful, false otherwise.

This function is available only for compiler that supports variadic template and move semantics

template<class GC , typename Key , typename Value , class Traits = split_list::type_traits>
bool cds::container::SplitListMap< GC, Key, Value, 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.

template<class GC , typename Key , typename Value , class Traits = split_list::type_traits>
iterator cds::container::SplitListMap< GC, Key, Value, 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()
template<class GC , typename Key , typename Value , class Traits = split_list::type_traits>
template<typename K , typename Func >
std::pair<bool, bool> cds::container::SplitListMap< GC, Key, Value, Traits >::ensure ( K const &  key,
Func  func 
)
inline

Ensures that the key exists in the map.

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 is inserted into the map (note that in this case the key_type should be constructible from type K). Otherwise, the functor func is called with item found. The functor Func may be a function with signature:

void func( bool bNew, value_type& item ) ;

or a functor:

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

with arguments:

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

The functor may change any fields of the item.second that is mapped_type; however, func must guarantee that during changing no any other modifications could be made on this item by concurrent threads.

You may pass func argument by reference using boost::ref.

Returns std::pair<bool, bool> where first is true if operation is successfull, second is true if new item has been added or false if the item with key already is in the list.

template<class GC , typename Key , typename Value , class Traits = split_list::type_traits>
template<typename K >
bool cds::container::SplitListMap< GC, Key, Value, Traits >::erase ( K const &  key)
inline

Deletes key from the map.

Return true if key is found and deleted, false otherwise

template<class GC , typename Key , typename Value , class Traits = split_list::type_traits>
template<typename K , typename Func >
bool cds::container::SplitListMap< GC, Key, Value, 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 is:

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

The functor may be passed by reference using boost:ref

Return true if key is found and deleted, false otherwise

template<class GC , typename Key , typename Value , class Traits = split_list::type_traits>
template<typename K , typename Less >
bool cds::container::SplitListMap< GC, Key, Value, 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.

template<class GC , typename Key , typename Value , class Traits = split_list::type_traits>
template<typename K , typename Less , typename Func >
bool cds::container::SplitListMap< GC, Key, Value, 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.

template<class GC , typename Key , typename Value , class Traits = split_list::type_traits>
template<typename K , typename Func >
bool cds::container::SplitListMap< GC, Key, Value, 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.

You may pass f argument by reference using boost::ref or cds::ref.

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.

template<class GC , typename Key , typename Value , class Traits = split_list::type_traits>
template<typename K >
bool cds::container::SplitListMap< GC, Key, Value, Traits >::find ( K const &  key)
inline

Finds the key key.

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

template<class GC , typename Key , typename Value , class Traits = split_list::type_traits>
template<typename K , typename Less , typename Func >
bool cds::container::SplitListMap< GC, Key, Value, 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.

template<class GC , typename Key , typename Value , class Traits = split_list::type_traits>
template<typename K , typename Less >
bool cds::container::SplitListMap< GC, Key, Value, Traits >::find_with ( K const &  key,
Less  pred 
)
inline

Finds the key val using pred predicate for searching.

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

template<class GC , typename Key , typename Value , class Traits = split_list::type_traits>
template<typename K >
bool cds::container::SplitListMap< GC, Key, Value, 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 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.

template<class GC , typename Key , typename Value , class Traits = split_list::type_traits>
template<typename K , typename V >
bool cds::container::SplitListMap< GC, Key, Value, 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 true if val is inserted into the map, false otherwise.

template<class GC , typename Key , typename Value , class Traits = split_list::type_traits>
template<typename K , typename Func >
bool cds::container::SplitListMap< GC, Key, Value, Traits >::insert_key ( 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.

It should be keep in mind that concurrent modifications of item.second may be possible. User-defined functor func should guarantee that during changing item's value no any other changes could be made on this item by concurrent threads.

The user-defined functor can be passed by reference using boost::ref and it 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.


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

cds 1.4.0 Developed by Maxim Khiszinsky aka khizmax 2007 - 2012
Autogenerated Mon May 20 2013 00:37:57 by Doxygen 1.8.3.1