cds  1.4.0
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes
cds::intrusive::MichaelHashSet< GC, OrderedList, Traits > Class Template Reference

Michael's hash set. More...

#include <cds/intrusive/michael_set.h>

Public Types

typedef OrderedList ordered_list
 type of ordered list used as a bucket implementation
 
typedef ordered_list bucket_type
 bucket type
 
typedef Traits options
 Traits template parameters.
 
typedef ordered_list::value_type value_type
 type of value stored in the list
 
typedef GC gc
 Garbage collector.
 
typedef
ordered_list::key_comparator 
key_comparator
 key comparision functor
 
typedef ordered_list::disposer disposer
 Node disposer functor.
 
typedef
cds::opt::v::hash_selector
< typename options::hash >
::type 
hash
 Hash functor for value_type and all its derivatives that you use.
 
typedef options::item_counter item_counter
 Item counter type.
 
typedef
cds::details::Allocator
< bucket_type, typename
options::allocator > 
bucket_table_allocator
 Bucket table allocator.
 
typedef
michael_set::details::iterator
< bucket_type, false > 
iterator
 Forward iterator. More...
 
typedef
michael_set::details::iterator
< bucket_type, true > 
const_iterator
 Const forward iterator. More...
 

Public Member Functions

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...
 
 MichaelHashSet (size_t nMaxItemCount, size_t nLoadFactor)
 Initializes hash set. More...
 
 ~MichaelHashSet ()
 Clears hash set object and destroys it.
 
bool insert (value_type &val)
 Inserts new node. More...
 
template<typename Func >
bool insert (value_type &val, Func f)
 Inserts new node. More...
 
template<typename Func >
std::pair< bool, bool > ensure (value_type &val, Func func)
 Ensures that the val exists in the set. More...
 
bool unlink (value_type &val)
 Unlinks the item val from the set. More...
 
template<typename Q >
bool erase (Q const &val)
 Deletes the item from the set. More...
 
template<typename Q , typename Less >
bool erase_with (Q const &val, Less pred)
 Deletes the item from the set using pred predicate for searching. More...
 
template<typename Q , typename Func >
bool erase (const Q &val, Func f)
 Deletes the item from the set. More...
 
template<typename Q , typename Less , typename Func >
bool erase_with (const Q &val, Less pred, Func f)
 Deletes the item from the set using pred predicate for searching. More...
 
template<typename Q , typename Func >
bool find (Q &val, Func f)
 Finds the key val. More...
 
template<typename Q , typename Less , typename Func >
bool find_with (Q &val, Less pred, Func f)
 Finds the key val using pred predicate for searching. More...
 
template<typename Q , typename Func >
bool find (Q const &val, Func f)
 Finds the key val. More...
 
template<typename Q , typename Less , typename Func >
bool find_with (Q const &val, Less pred, Func f)
 Finds the key val using pred predicate for searching. More...
 
template<typename Q >
bool find (Q const &val)
 Finds the key val. More...
 
template<typename Q , typename Less >
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.
 
size_t bucket_count () const
 Returns the size of hash table. More...
 
const_iterator begin () const
 Returns a forward const iterator addressing the first element in a set.
 
const_iterator cbegin ()
 
const_iterator end () const
 Returns an const iterator that addresses the location succeeding the last element in a set.
 
const_iterator cend ()
 

Protected Member Functions

template<typename Q >
size_t hash_value (const Q &key) const
 Calculates hash value of key.
 
template<typename Q >
bucket_typebucket (const Q &key)
 Returns the bucket (ordered list) for key.
 

Protected Attributes

item_counter m_ItemCounter
 Item counter.
 
hash m_HashFunctor
 Hash functor.
 
bucket_typem_Buckets
 bucket table
 

Private Member Functions

const_iterator get_const_begin () const
 
const_iterator get_const_end () const
 

Detailed Description

template<class GC, class OrderedList, class Traits = michael_set::type_traits>
class cds::intrusive::MichaelHashSet< GC, OrderedList, Traits >

Michael's hash set.

Source:

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:

There are several specializations of MichaelHashSet for each GC. You should include:

Hash functor

Some member functions of Michael's hash set accept the key parameter of type Q which differs from value_type. It is expected that type Q contains full key of value_type, and for equal keys of type Q and value_type the hash values of these keys must be equal too. The hash functor Traits::hash should accept parameters of both type:

// Our node type
struct Foo {
std::string key_ ; // key field
// ... other fields
} ;
// Hash functor
struct fooHash {
size_t operator()( const std::string& s ) const
{
return std::hash( s ) ;
}
size_t operator()( const Foo& f ) const
{
return (*this)( f.key_ ) ;
}
};

How to use

First, you should define ordered list type to use in your hash set:

// For gc::HP-based MichaelList implementation
#include <cds/intrusive/michael_list_hp.h>
// cds::intrusive::MichaelHashSet declaration
#include <cds/intrusive/michael_set.h>
// Type of hash-set items
struct Foo: public cds::intrusive::michael_list::node< cds::gc::HP >
{
std::string key_ ; // key field
unsigned val_ ; // value field
// ... other value fields
};
// Declare comparator for the item
struct FooCmp
{
int operator()( const Foo& f1, const Foo& f2 ) const
{
return f1.key_.compare( f2.key_ ) ;
}
};
// Declare bucket type for Michael's hash set
// The bucket type is any ordered list type like MichaelList, LazyList
typedef cds::intrusive::MichaelList< cds::gc::HP, Foo,
// hook option
cds::intrusive::opt::hook< cds::intrusive::michael_list::base_hook< cds::opt::gc< cds::gc::HP > > >
// item comparator option
>::type
> Foo_bucket ;

Second, you should declare Michael's hash set container:

// Declare hash functor
// Note, the hash functor accepts parameter type Foo and std::string
struct FooHash {
size_t operator()( const Foo& f ) const
{
return cds::opt::v::hash<std::string>()( f.key_ ) ;
}
size_t operator()( const std::string& f ) const
{
return cds::opt::v::hash<std::string>()( f ) ;
}
};
// Michael's set typedef
cds::gc::HP
,Foo_bucket
>::type
> Foo_set ;

Now, you can use Foo_set in your application.

Like other intrusive containers, you may build several containers on single item structure:

#include <cds/intrusive/michael_list_hp.h>
#include <cds/intrusive/michael_list_ptb.h>
#include <cds/intrusive/michael_set.h>
struct tag_key1_idx ;
struct tag_key2_idx ;
// Your two-key data
// The first key is maintained by gc::HP, second key is maintained by gc::PTB garbage collectors
struct Foo
: public cds::intrusive::michael_list::node< cds::gc::HP, tag_key1_idx >
, public cds::intrusive::michael_list::node< cds::gc::PTB, tag_key2_idx >
{
std::string key1_ ; // first key field
unsigned int key2_ ; // second key field
// ... value fields and fields for controlling item's lifetime
};
// Declare comparators for the item
struct Key1Cmp
{
int operator()( const Foo& f1, const Foo& f2 ) const { return f1.key1_.compare( f2.key1_ ) ; }
};
struct Key2Less
{
bool operator()( const Foo& f1, const Foo& f2 ) const { return f1.key2_ < f2.key1_ ; }
};
// Declare bucket type for Michael's hash set indexed by key1_ field and maintained by gc::HP
typedef cds::intrusive::MichaelList< cds::gc::HP, Foo,
// hook option
cds::intrusive::opt::hook< cds::intrusive::michael_list::base_hook< cds::opt::gc< cds::gc::HP >, tag_key1_idx > >
// item comparator option
>::type
> Key1_bucket ;
// Declare bucket type for Michael's hash set indexed by key2_ field and maintained by gc::PTB
typedef cds::intrusive::MichaelList< cds::gc::PTB, Foo,
// hook option
cds::intrusive::opt::hook< cds::intrusive::michael_list::base_hook< cds::opt::gc< cds::gc::PTB >, tag_key2_idx > >
// item comparator option
>::type
> Key2_bucket ;
// Declare hash functor
struct Key1Hash {
size_t operator()( const Foo& f ) const { return cds::opt::v::hash<std::string>()( f.key1_ ) ; }
size_t operator()( const std::string& s ) const { return cds::opt::v::hash<std::string>()( s ) ; }
};
inline size_t Key2Hash( const Foo& f ) { return (size_t) f.key2_ ; }
// Michael's set indexed by key1_ field
cds::gc::HP
,Key1_bucket
>::type
> key1_set ;
// Michael's set indexed by key2_ field
cds::gc::PTB
,Key2_bucket
>::type
> key2_set ;

Member Typedef Documentation

template<class GC , class OrderedList , class Traits = michael_set::type_traits>
typedef michael_set::details::iterator< bucket_type, true > cds::intrusive::MichaelHashSet< GC, OrderedList, Traits >::const_iterator

Const forward iterator.

For iterator's features and requirements see iterator

template<class GC , class OrderedList , class Traits = michael_set::type_traits>
typedef michael_set::details::iterator< bucket_type, false > cds::intrusive::MichaelHashSet< GC, OrderedList, Traits >::iterator

Forward iterator.

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

  • it has no post-increment operator
  • it iterates items in unordered fashion
  • The iterator cannot be moved across thread boundary since it may contain GC's guard that is thread-private GC data.
  • Iterator ensures thread-safety even if you delete the item that iterator points to. However, in case of concurrent deleting operations it is no guarantee that you iterate all item in the set.

Therefore, the use of iterators in concurrent environment is not good idea. Use the iterator for the concurrent container for debug purpose only.

Constructor & Destructor Documentation

template<class GC , class OrderedList , class Traits = michael_set::type_traits>
cds::intrusive::MichaelHashSet< GC, OrderedList, Traits >::MichaelHashSet ( size_t  nMaxItemCount,
size_t  nLoadFactor 
)
inline

Initializes hash set.

The Michael's hash set 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. Small integer up to 10, default is 1.

Member Function Documentation

template<class GC , class OrderedList , class Traits = michael_set::type_traits>
iterator cds::intrusive::MichaelHashSet< GC, OrderedList, Traits >::begin ( )
inline

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

For empty set

begin() == end()
template<class GC , class OrderedList , class Traits = michael_set::type_traits>
size_t cds::intrusive::MichaelHashSet< GC, OrderedList, Traits >::bucket_count ( ) const
inline

Returns the size of hash table.

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

template<class GC , class OrderedList , class Traits = michael_set::type_traits>
void cds::intrusive::MichaelHashSet< GC, OrderedList, Traits >::clear ( )
inline

Clears the set (non-atomic)

The function unlink all items from the set. The function is not atomic. It cleans up each bucket and then resets the item counter to zero. If there are a thread that performs insertion while clear is working the result is undefined in general case: empty() may return true but the set may contain item(s). Therefore, clear may be used only for debugging purposes.

For each item the disposer is called after unlinking.

template<class GC , class OrderedList , class Traits = michael_set::type_traits>
bool cds::intrusive::MichaelHashSet< GC, OrderedList, Traits >::empty ( ) const
inline

Checks if the set is empty.

Emptiness is checked by item counting: if item count is zero then the set is empty. Thus, the correct item counting feature is an important part of Michael's set implementation.

template<class GC , class OrderedList , class Traits = michael_set::type_traits>
iterator cds::intrusive::MichaelHashSet< GC, 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()
template<class GC , class OrderedList , class Traits = michael_set::type_traits>
template<typename Func >
std::pair<bool, bool> cds::intrusive::MichaelHashSet< GC, OrderedList, Traits >::ensure ( value_type val,
Func  func 
)
inline

Ensures that the val exists in the set.

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

If the item val not found in the set, then val is inserted into the set. Otherwise, the functor func is called with item found. The functor signature is:

void func( bool bNew, value_type& item, value_type& val ) ;

with arguments:

  • bNew - true if the item has been inserted, false otherwise
  • item - item of the set
  • val - argument val passed into the ensure function If new item has been inserted (i.e. bNew is true) then item and val arguments refers to the same thing.

The functor may change non-key fields of the item; 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 or cds::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 set.

template<class GC , class OrderedList , class Traits = michael_set::type_traits>
template<typename Q >
bool cds::intrusive::MichaelHashSet< GC, OrderedList, Traits >::erase ( Q const &  val)
inline

Deletes the item from the set.

The function searches an item with key equal to val in the set, unlinks it from the set, and returns true. If the item with key equal to val is not found the function return false.

Note the hash functor should accept a parameter of type Q that can be not the same as value_type.

template<class GC , class OrderedList , class Traits = michael_set::type_traits>
template<typename Q , typename Func >
bool cds::intrusive::MichaelHashSet< GC, OrderedList, Traits >::erase ( const Q &  val,
Func  f 
)
inline

Deletes the item from the set.

The function searches an item with key equal to val in the set, call f functor with item found, and unlinks it from the set. The disposer specified in OrderedList class template parameter is called by garbage collector GC asynchronously.

The Func interface is

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

The functor may be passed by reference with boost:ref

If the item with key equal to val is not found the function return false.

Note the hash functor should accept a parameter of type Q that can be not the same as value_type.

template<class GC , class OrderedList , class Traits = michael_set::type_traits>
template<typename Q , typename Less >
bool cds::intrusive::MichaelHashSet< GC, OrderedList, Traits >::erase_with ( Q const &  val,
Less  pred 
)
inline

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

The function is an analog of erase(Q const&) 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.

template<class GC , class OrderedList , class Traits = michael_set::type_traits>
template<typename Q , typename Less , typename Func >
bool cds::intrusive::MichaelHashSet< GC, OrderedList, Traits >::erase_with ( const Q &  val,
Less  pred,
Func  f 
)
inline

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

The function is an analog of erase(Q const&, Func) 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.

template<class GC , class OrderedList , class Traits = michael_set::type_traits>
template<typename Q , typename Func >
bool cds::intrusive::MichaelHashSet< GC, OrderedList, Traits >::find ( Q &  val,
Func  f 
)
inline

Finds the key val.

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

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

where item is the item found, val is the find function argument.

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

The functor may change non-key fields of item. 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 set item. If such access is possible you must provide your own synchronization schema on item level to exclude unsafe item modifications.

The val argument is non-const since it can be used as f functor destination i.e., the functor may modify both arguments.

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

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

template<class GC , class OrderedList , class Traits = michael_set::type_traits>
template<typename Q , typename Func >
bool cds::intrusive::MichaelHashSet< GC, OrderedList, Traits >::find ( Q const &  val,
Func  f 
)
inline

Finds the key val.

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

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

where item is the item found, val is the find function argument.

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

The functor may change non-key fields of item. 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 set item. If such access is possible you must provide your own synchronization schema on item level to exclude unsafe item modifications.

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

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

template<class GC , class OrderedList , class Traits = michael_set::type_traits>
template<typename Q >
bool cds::intrusive::MichaelHashSet< GC, OrderedList, Traits >::find ( Q const &  val)
inline

Finds the key val.

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

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

template<class GC , class OrderedList , class Traits = michael_set::type_traits>
template<typename Q , typename Less , typename Func >
bool cds::intrusive::MichaelHashSet< GC, OrderedList, Traits >::find_with ( Q &  val,
Less  pred,
Func  f 
)
inline

Finds the key val using pred predicate for searching.

The function is an analog of find(Q&, Func) 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.

template<class GC , class OrderedList , class Traits = michael_set::type_traits>
template<typename Q , typename Less , typename Func >
bool cds::intrusive::MichaelHashSet< GC, OrderedList, Traits >::find_with ( Q const &  val,
Less  pred,
Func  f 
)
inline

Finds the key val using pred predicate for searching.

The function is an analog of find(Q const&, Func) 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.

template<class GC , class OrderedList , class Traits = michael_set::type_traits>
template<typename Q , typename Less >
bool cds::intrusive::MichaelHashSet< GC, OrderedList, Traits >::find_with ( Q const &  val,
Less  pred 
)
inline

Finds the key val using pred predicate for searching.

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

template<class GC , class OrderedList , class Traits = michael_set::type_traits>
bool cds::intrusive::MichaelHashSet< GC, OrderedList, Traits >::insert ( value_type val)
inline

Inserts new node.

The function inserts val in the set if it does not contain an item with key equal to val.

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

template<class GC , class OrderedList , class Traits = michael_set::type_traits>
template<typename Func >
bool cds::intrusive::MichaelHashSet< GC, OrderedList, Traits >::insert ( value_type val,
Func  f 
)
inline

Inserts new node.

This function is intended for derived non-intrusive containers.

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

  • create item with key only
  • insert new item into the set
  • if inserting is success, calls f functor to initialize value-field of val.

The functor signature is:

void func( value_type& val ) ;

where val is the item inserted. User-defined functor f should guarantee that during changing val no any other changes could be made on this set's item by concurrent threads. The user-defined functor is called only if the inserting is success and can be passed by reference using boost::ref

template<class GC , class OrderedList , class Traits = michael_set::type_traits>
bool cds::intrusive::MichaelHashSet< GC, OrderedList, Traits >::unlink ( value_type val)
inline

Unlinks the item val from the set.

The function searches the item val in the set and unlink it from the set if it is found and is equal to val.

The function returns true if success and false otherwise.


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:59 by Doxygen 1.8.3.1