cds  2.2.0
cds::intrusive::CuckooSet< T, Traits > Class Template Reference

Cuckoo hash set. More...

#include <cds/intrusive/cuckoo_set.h>

Inheritance diagram for cds::intrusive::CuckooSet< T, Traits >:
cds::container::CuckooSet< T, Traits >

Public Types

typedef T value_type
 The value type stored in the set.
 
typedef Traits traits
 Set traits.
 
typedef traits::hook hook
 hook type
 
typedef hook::node_type node_type
 node type
 
typedef get_node_traits< value_type, node_type, hook >::type node_traits
 node traits
 
typedef traits::hash hash
 hash functor tuple wrapped for internal use
 
typedef hash::hash_tuple_type hash_tuple_type
 Type of hash tuple.
 
typedef traits::stat stat
 internal statistics type
 
typedef traits::mutex_policy original_mutex_policy
 Concurrent access policy, see cuckoo::traits::mutex_policy.
 
typedef opt::details::make_equal_to< value_type, traits, !c_isSorted >::type key_equal_to
 Key equality functor; used only for unordered probe-set.
 
typedef opt::details::make_comparator< value_type, traits >::type key_comparator
 key comparing functor based on opt::compare and opt::less option setter. Used only for ordered probe set
 
typedef traits::allocator allocator
 allocator type
 
typedef traits::item_counter item_counter
 item counter type
 
typedef traits::disposer disposer
 node disposer
 

Public Member Functions

 CuckooSet ()
 Default constructor. More...
 
 CuckooSet (size_t nInitialSize, unsigned int nProbesetSize, unsigned int nProbesetThreshold=0)
 Constructs the set object with given probe set size and threshold. More...
 
 CuckooSet (hash_tuple_type const &h)
 Constructs the set object with given hash functor tuple. More...
 
 CuckooSet (size_t nInitialSize, unsigned int nProbesetSize, unsigned int nProbesetThreshold, hash_tuple_type const &h)
 Constructs the set object with given probe set properties and hash functor tuple. More...
 
 CuckooSet (hash_tuple_type &&h)
 Constructs the set object with given hash functor tuple (move semantics) More...
 
 CuckooSet (size_t nInitialSize, unsigned int nProbesetSize, unsigned int nProbesetThreshold, hash_tuple_type &&h)
 Constructs the set object with given probe set properties and hash functor tuple (move semantics) More...
 
 ~CuckooSet ()
 Destructor.
 
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 > update (value_type &val, Func func, bool bAllowInsert=true)
 Updates the node. More...
 
bool unlink (value_type &val)
 Unlink the item val from the set. More...
 
template<typename Q >
value_typeerase (Q const &val)
 Deletes the item from the set. More...
 
template<typename Q , typename Predicate >
value_typeerase_with (Q const &val, Predicate pred)
 Deletes the item from the set using pred predicate for searching. More...
 
template<typename Q , typename Func >
value_typeerase (Q const &val, Func f)
 Delete the item from the set. More...
 
template<typename Q , typename Predicate , typename Func >
value_typeerase_with (Q const &val, Predicate 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)
 Find the key val. More...
 
template<typename Q , typename Predicate , typename Func >
bool find_with (Q &val, Predicate pred, Func f)
 Find the key val using pred predicate for comparing. More...
 
template<typename Q >
bool contains (Q const &key)
 Checks whether the set contains key. More...
 
template<typename Q , typename Predicate >
bool contains (Q const &key, Predicate pred)
 Checks whether the set contains key using pred predicate for searching. More...
 
void clear ()
 Clears the set. More...
 
template<typename Disposer >
void clear_and_dispose (Disposer oDisposer)
 Clears the set and calls disposer for each item. 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...
 
size_t lock_count () const
 Returns lock array size.
 
stat const & statistics () const
 Returns const reference to internal statistics.
 
mutex_policy::statistics_type const & mutex_policy_statistics () const
 Returns const reference to mutex policy internal statistics.
 

Static Public Attributes

static bool const c_isSorted
 Probe set should be ordered or not. More...
 
static size_t const c_nArity = hash::size
 the arity of cuckoo hashing: the number of hash functors provided; minimum 2.
 
static unsigned int const c_nDefaultProbesetSize = 4
 default probeset size
 
static size_t const c_nDefaultInitialSize = 16
 default initial size
 
static unsigned int const c_nRelocateLimit = c_nArity * 2 - 1
 Count of attempts to relocate before giving up.
 

Protected Attributes

bucket_entry * m_BucketTable [c_nArity]
 Bucket tables.
 
size_t m_nBucketMask
 Hash bitmask; bucket table size minus 1.
 
unsigned int const m_nProbesetSize
 Probe set size.
 
unsigned int const m_nProbesetThreshold
 Probe set threshold.
 
hash m_Hash
 Hash functor tuple.
 
mutex_policy m_MutexPolicy
 concurrent access policy
 
item_counter m_ItemCounter
 item counter
 
stat m_Stat
 internal statistics
 

Detailed Description

template<typename T, typename Traits = cuckoo::traits>
class cds::intrusive::CuckooSet< T, Traits >

Cuckoo hash set.

Source

  • [2007] M.Herlihy, N.Shavit, M.Tzafrir "Concurrent Cuckoo Hashing. Technical report"
  • [2008] Maurice Herlihy, Nir Shavit "The Art of Multiprocessor Programming"

About Cuckoo hashing

[From "The Art of Multiprocessor Programming"] Cuckoo hashing is a hashing algorithm in which a newly added item displaces any earlier item occupying the same slot. For brevity, a table is a k-entry array of items. For a hash set f size N = 2k we use a two-entry array of tables, and two independent hash functions, h0, h1: KeyRange -> 0,...,k-1 mapping the set of possible keys to entries in he array. To test whether a value x is in the set, find(x) tests whether either table[0][h0(x)] or table[1][h1(x)] is equal to x. Similarly, erase(x)checks whether x is in either table[0][h0(x)] or table[1][h1(x)], ad removes it if found.

The insert(x) successively "kicks out" conflicting items until every key has a slot. To add x, the method swaps x with y, the current occupant of table[0][h0(x)]. If the prior value was nullptr, it is done. Otherwise, it swaps the newly nest-less value y for the current occupant of table[1][h1(y)] in the same way. As before, if the prior value was nullptr, it is done. Otherwise, the method continues swapping entries (alternating tables) until it finds an empty slot. We might not find an empty slot, either because the table is full, or because the sequence of displacement forms a cycle. We therefore need an upper limit on the number of successive displacements we are willing to undertake. When this limit is exceeded, we resize the hash table, choose new hash functions and start over.

For concurrent cuckoo hashing, rather than organizing the set as a two-dimensional table of items, we use two-dimensional table of probe sets, where a probe set is a constant-sized set of items with the same hash code. Each probe set holds at most PROBE_SIZE items, but the algorithm tries to ensure that when the set is quiescent (i.e no method call in progress) each probe set holds no more than THRESHOLD < PROBE_SET items. While method calls are in-flight, a probe set may temporarily hold more than THRESHOLD but never more than PROBE_SET items.

In current implementation, a probe set can be defined either as a (single-linked) list or as a fixed-sized vector, optionally ordered.

In description above two-table cuckoo hashing (k = 2) has been considered. We can generalize this approach for k >= 2 when we have k hash functions h[0], ... h[k-1] and k tables table[0], ... table[k-1].

The search in probe set is linear, the complexity is O(PROBE_SET) . The probe set may be ordered or not. Ordered probe set can be more efficient since the average search complexity is O(PROBE_SET/2). However, the overhead of sorting can eliminate a gain of ordered search.

The probe set is ordered if compare or less is specified in Traits template parameter. Otherwise, the probe set is unordered and Traits should provide equal_to predicate.

The cds::intrusive::cuckoo namespace contains CuckooSet-related declarations.

Template arguments:

How to use

You should incorporate cuckoo::node into your struct T and provide appropriate cuckoo::traits::hook in your Traits template parameters. Usually, for Traits you define a struct based on cuckoo::traits.

Example for base hook and list-based probe-set:

#include <cds/intrusive/cuckoo_set.h>
// Data stored in cuckoo set
// We use list as probe-set container and store hash values in the node
// (since we use two hash functions we should store 2 hash values per node)
struct my_data: public cds::intrusive::cuckoo::node< cds::intrusive::cuckoo::list, 2 >
{
// key field
std::string strKey;
// other data
// ...
};
// Provide equal_to functor for my_data since we will use unordered probe-set
struct my_data_equal_to {
bool operator()( const my_data& d1, const my_data& d2 ) const
{
return d1.strKey.compare( d2.strKey ) == 0;
}
bool operator()( const my_data& d, const std::string& s ) const
{
return d.strKey.compare(s) == 0;
}
bool operator()( const std::string& s, const my_data& d ) const
{
return s.compare( d.strKey ) == 0;
}
};
// Provide two hash functor for my_data
struct hash1 {
size_t operator()(std::string const& s) const
{
return cds::opt::v::hash<std::string>( s );
}
size_t operator()( my_data const& d ) const
{
return (*this)( d.strKey );
}
};
struct hash2: private hash1 {
size_t operator()(std::string const& s) const
{
size_t h = ~( hash1::operator()(s));
return ~h + 0x9e3779b9 + (h << 6) + (h >> 2);
}
size_t operator()( my_data const& d ) const
{
return (*this)( d.strKey );
}
};
// Declare type traits
struct my_traits: public cds::intrusive::cuckoo::traits
{
> hook;
typedef my_data_equa_to equal_to;
};
// Declare CuckooSet type
// Equal option-based declaration
typedef cds::intrusive::CuckooSet< my_data,
> >
,cds::opt::hash< std::tuple< hash1, hash2 > >
,cds::opt::equal_to< my_data_equal_to >
>::type
> opt_cuckoo_set;

If we provide compare function instead of equal_to for my_data we get as a result a cuckoo set with ordered probe set that may improve performance. Example for base hook and ordered vector-based probe-set:

#include <cds/intrusive/cuckoo_set.h>
// Data stored in cuckoo set
// We use a vector of capacity 4 as probe-set container and store hash values in the node
// (since we use two hash functions we should store 2 hash values per node)
struct my_data: public cds::intrusive::cuckoo::node< cds::intrusive::cuckoo::vector<4>, 2 >
{
// key field
std::string strKey;
// other data
// ...
};
// Provide compare functor for my_data since we want to use ordered probe-set
struct my_data_compare {
int operator()( const my_data& d1, const my_data& d2 ) const
{
return d1.strKey.compare( d2.strKey );
}
int operator()( const my_data& d, const std::string& s ) const
{
return d.strKey.compare(s);
}
int operator()( const std::string& s, const my_data& d ) const
{
return s.compare( d.strKey );
}
};
// Provide two hash functor for my_data
struct hash1 {
size_t operator()(std::string const& s) const
{
return cds::opt::v::hash<std::string>( s );
}
size_t operator()( my_data const& d ) const
{
return (*this)( d.strKey );
}
};
struct hash2: private hash1 {
size_t operator()(std::string const& s) const
{
size_t h = ~( hash1::operator()(s));
return ~h + 0x9e3779b9 + (h << 6) + (h >> 2);
}
size_t operator()( my_data const& d ) const
{
return (*this)( d.strKey );
}
};
// Declare type traits
struct my_traits: public cds::intrusive::cuckoo::traits
{
cds::intrusive::cuckoo::probeset_type< my_data::probeset_type >
,cds::intrusive::cuckoo::store_hash< my_data::hash_array_size >
> hook;
typedef my_data_compare compare;
};
// Declare CuckooSet type
// Equal option-based declaration
typedef cds::intrusive::CuckooSet< my_data,
cds::intrusive::cuckoo::probeset_type< my_data::probeset_type >
,cds::intrusive::cuckoo::store_hash< my_data::hash_array_size >
> >
,cds::opt::hash< std::tuple< hash1, hash2 > >
,cds::opt::compare< my_data_compare >
>::type
> opt_cuckoo_set;

Constructor & Destructor Documentation

§ CuckooSet() [1/6]

template<typename T, typename Traits = cuckoo::traits>
cds::intrusive::CuckooSet< T, Traits >::CuckooSet ( )
inline

Default constructor.

Initial size = c_nDefaultInitialSize

Probe set size:

  • c_nDefaultProbesetSize if probeset_type is cuckoo::list
  • Capacity if probeset_type is cuckoo::vector<Capacity>

Probe set threshold = probe set size - 1

§ CuckooSet() [2/6]

template<typename T, typename Traits = cuckoo::traits>
cds::intrusive::CuckooSet< T, Traits >::CuckooSet ( size_t  nInitialSize,
unsigned int  nProbesetSize,
unsigned int  nProbesetThreshold = 0 
)
inline

Constructs the set object with given probe set size and threshold.

If probe set type is cuckoo::vector<Capacity> vector then nProbesetSize is ignored since it should be equal to vector's Capacity.

Parameters
nInitialSizeInitial set size; if 0 - use default initial size c_nDefaultInitialSize
nProbesetSizeprobe set size
nProbesetThresholdprobe set threshold, nProbesetThreshold < nProbesetSize. If 0, nProbesetThreshold = nProbesetSize - 1

§ CuckooSet() [3/6]

template<typename T, typename Traits = cuckoo::traits>
cds::intrusive::CuckooSet< T, Traits >::CuckooSet ( hash_tuple_type const &  h)
inline

Constructs the set object with given hash functor tuple.

The probe set size and threshold are set as default, see CuckooSet()

Parameters
hhash functor tuple of type std::tuple<H1, H2, ... Hn> where n == c_nArity

§ CuckooSet() [4/6]

template<typename T, typename Traits = cuckoo::traits>
cds::intrusive::CuckooSet< T, Traits >::CuckooSet ( size_t  nInitialSize,
unsigned int  nProbesetSize,
unsigned int  nProbesetThreshold,
hash_tuple_type const &  h 
)
inline

Constructs the set object with given probe set properties and hash functor tuple.

If probe set type is cuckoo::vector<Capacity> vector then nProbesetSize should be equal to vector's Capacity.

Parameters
nInitialSizeInitial set size; if 0 - use default initial size c_nDefaultInitialSize
nProbesetSizeprobe set size, positive integer
nProbesetThresholdprobe set threshold, nProbesetThreshold < nProbesetSize. If 0, nProbesetThreshold = nProbesetSize - 1
hhash functor tuple of type std::tuple<H1, H2, ... Hn> where n == c_nArity

§ CuckooSet() [5/6]

template<typename T, typename Traits = cuckoo::traits>
cds::intrusive::CuckooSet< T, Traits >::CuckooSet ( hash_tuple_type &&  h)
inline

Constructs the set object with given hash functor tuple (move semantics)

The probe set size and threshold are set as default, see CuckooSet()

Parameters
hhash functor tuple of type std::tuple<H1, H2, ... Hn> where n == c_nArity

§ CuckooSet() [6/6]

template<typename T, typename Traits = cuckoo::traits>
cds::intrusive::CuckooSet< T, Traits >::CuckooSet ( size_t  nInitialSize,
unsigned int  nProbesetSize,
unsigned int  nProbesetThreshold,
hash_tuple_type &&  h 
)
inline

Constructs the set object with given probe set properties and hash functor tuple (move semantics)

If probe set type is cuckoo::vector<Capacity> vector then nProbesetSize should be equal to vector's Capacity.

Parameters
nInitialSizeInitial set size; if 0 - use default initial size c_nDefaultInitialSize
nProbesetSizeprobe set size, positive integer
nProbesetThresholdprobe set threshold, nProbesetThreshold < nProbesetSize. If 0, nProbesetThreshold = nProbesetSize - 1
hhash functor tuple of type std::tuple<H1, H2, ... Hn> where n == c_nArity

Member Function Documentation

§ bucket_count()

template<typename T, typename Traits = cuckoo::traits>
size_t cds::intrusive::CuckooSet< T, Traits >::bucket_count ( ) const
inline

Returns the size of hash table.

The hash table size is non-constant and can be increased via resizing.

§ clear()

template<typename T, typename Traits = cuckoo::traits>
void cds::intrusive::CuckooSet< T, Traits >::clear ( )
inline

Clears the set.

The function unlinks all items from the set. For any item disposer is called

§ clear_and_dispose()

template<typename T, typename Traits = cuckoo::traits>
template<typename Disposer >
void cds::intrusive::CuckooSet< T, Traits >::clear_and_dispose ( Disposer  oDisposer)
inline

Clears the set and calls disposer for each item.

The function unlinks all items from the set calling oDisposer for each item. Disposer functor interface is:

struct Disposer{
void operator()( value_type * p );
};

The disposer specified in Traits is not called.

§ contains() [1/2]

template<typename T, typename Traits = cuckoo::traits>
template<typename Q >
bool cds::intrusive::CuckooSet< T, Traits >::contains ( Q const &  key)
inline

Checks whether the set 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 T, typename Traits = cuckoo::traits>
template<typename Q , typename Predicate >
bool cds::intrusive::CuckooSet< T, Traits >::contains ( Q const &  key,
Predicate  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. If the set is unordered, Predicate has semantics like std::equal_to. For ordered set Predicate has std::less semantics. In that case pred must imply the same element order as the comparator used for building the set.

§ empty()

template<typename T, typename Traits = cuckoo::traits>
bool cds::intrusive::CuckooSet< T, 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.

§ erase() [1/2]

template<typename T, typename Traits = cuckoo::traits>
template<typename Q >
value_type* cds::intrusive::CuckooSet< T, 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 a pointer to unlinked item.

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

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

§ erase() [2/2]

template<typename T, typename Traits = cuckoo::traits>
template<typename Q , typename Func >
value_type* cds::intrusive::CuckooSet< T, Traits >::erase ( Q const &  val,
Func  f 
)
inline

Delete the item from the set.

The function searches an item with key equal to val in the set, call f functor with item found, unlinks it from the set, and returns a pointer to unlinked item.

The Func interface is

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

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

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

§ erase_with() [1/2]

template<typename T, typename Traits = cuckoo::traits>
template<typename Q , typename Predicate >
value_type* cds::intrusive::CuckooSet< T, Traits >::erase_with ( Q const &  val,
Predicate  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. If cuckoo set is ordered, then Predicate should have the interface and semantics like std::less. If cuckoo set is unordered, then Predicate should have the interface and semantics like std::equal_to. Predicate must imply the same element order as the comparator used for building the set.

§ erase_with() [2/2]

template<typename T, typename Traits = cuckoo::traits>
template<typename Q , typename Predicate , typename Func >
value_type* cds::intrusive::CuckooSet< T, Traits >::erase_with ( Q const &  val,
Predicate  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. If you use ordered cuckoo set, then Predicate should have the interface and semantics like std::less. If you use unordered cuckoo set, then Predicate should have the interface and semantics like std::equal_to. Predicate must imply the same element order as the comparator used for building the set.

§ find()

template<typename T, typename Traits = cuckoo::traits>
template<typename Q , typename Func >
bool cds::intrusive::CuckooSet< T, Traits >::find ( Q &  val,
Func  f 
)
inline

Find 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.

The functor may change non-key fields of item.

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.

§ find_with()

template<typename T, typename Traits = cuckoo::traits>
template<typename Q , typename Predicate , typename Func >
bool cds::intrusive::CuckooSet< T, Traits >::find_with ( Q &  val,
Predicate  pred,
Func  f 
)
inline

Find the key val using pred predicate for comparing.

The function is an analog of find(Q&, Func) but pred is used for key comparison. If you use ordered cuckoo set, then Predicate should have the interface and semantics like std::less. If you use unordered cuckoo set, then Predicate should have the interface and semantics like std::equal_to. pred must imply the same element order as the comparator used for building the set.

§ insert() [1/2]

template<typename T, typename Traits = cuckoo::traits>
bool cds::intrusive::CuckooSet< T, 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 inserted into the set, false otherwise.

§ insert() [2/2]

template<typename T, typename Traits = cuckoo::traits>
template<typename Func >
bool cds::intrusive::CuckooSet< T, Traits >::insert ( value_type val,
Func  f 
)
inline

Inserts new node.

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.

The user-defined functor is called only if the inserting is success.

§ unlink()

template<typename T, typename Traits = cuckoo::traits>
bool cds::intrusive::CuckooSet< T, Traits >::unlink ( value_type val)
inline

Unlink the item val from the set.

The function searches the item val in the set and unlink it if it is found and is equal to val (here, the equality means that val belongs to the set: if item is an item found then unlink is successful iif &val == &item)

The function returns true if success and false otherwise.

§ update()

template<typename T, typename Traits = cuckoo::traits>
template<typename Func >
std::pair<bool, bool> cds::intrusive::CuckooSet< T, Traits >::update ( value_type val,
Func  func,
bool  bAllowInsert = true 
)
inline

Updates the node.

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

If the item val is not found in the set, then val is inserted into the set iff bAllowInsert is true. Otherwise, the functor func is called with item found. The functor func 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 update() function If new item has been inserted (i.e. bNew is true) then item and val arguments refer to the same thing.

The functor may change non-key fields of the item.

Returns std::pair<bool, bool> where first is true if operation is successful, i.e. the node has been inserted or updated, second is true if new item has been added or false if the item with key already exists.

Field Documentation

§ c_isSorted

template<typename T, typename Traits = cuckoo::traits>
bool const cds::intrusive::CuckooSet< T, Traits >::c_isSorted
static
Initial value:
= !( std::is_same< typename traits::compare, opt::none >::value
&& std::is_same< typename traits::less, opt::none >::value )

Probe set should be ordered or not.

If Traits specifies cmpare or less functor then the set is ordered. Otherwise, it is unordered and Traits should provide equal_to functor.


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:49 by Doxygen 1.8.12