cds
2.3.2
|
Striped hash set. More...
#include <cds/container/striped_set.h>
Public Types | |
typedef Container | underlying_container_type |
original intrusive container type for the bucket | |
typedef base_class::bucket_type | bucket_type |
container type adapted for hash set | |
typedef bucket_type::value_type | value_type |
value type stored in the set | |
typedef base_class::hash | hash |
Hash functor. | |
typedef base_class::item_counter | item_counter |
Item counter. | |
typedef base_class::resizing_policy | resizing_policy |
Resizing policy. | |
typedef base_class::allocator_type | allocator_type |
allocator type specified in options. | |
typedef base_class::mutex_policy | mutex_policy |
Mutex policy. | |
Public Member Functions | |
StripedSet () | |
Default ctor. The initial capacity is 16. | |
StripedSet (size_t nCapacity) | |
Ctor with initial capacity specified. More... | |
StripedSet (size_t nCapacity, resizing_policy const &resizingPolicy) | |
Ctor with resizing policy (copy semantics) More... | |
StripedSet (size_t nCapacity, resizing_policy &&resizingPolicy) | |
Ctor with resizing policy (move semantics) More... | |
~StripedSet () | |
Destructor destroys internal data. | |
template<typename Q > | |
bool | insert (Q const &val) |
Inserts new node. More... | |
template<typename Q , typename Func > | |
bool | insert (Q const &val, Func f) |
Inserts new node. More... | |
template<typename... Args> | |
bool | emplace (Args &&... args) |
Inserts data of type value_type constructed with std::forward<Args>(args)... More... | |
template<typename Q , typename Func > | |
std::pair< bool, bool > | update (Q const &val, Func func, bool bAllowInsert=true) |
Updates the node. More... | |
template<typename Q > | |
bool | erase (Q const &key) |
Delete key from the set. More... | |
template<typename Q , typename Less , typename Bucket = bucket_type, typename = typename std::enable_if< Bucket::has_erase_with >::type> | |
bool | erase_with (Q const &key, Less pred) |
Deletes the item from the set using pred predicate for searching. More... | |
template<typename Q , typename Func > | |
bool | erase (Q const &key, Func f) |
Delete key from the set. More... | |
template<typename Q , typename Less , typename Func , typename Bucket = bucket_type, typename = typename std::enable_if< Bucket::has_erase_with >::type> | |
bool | erase_with (Q const &key, 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) |
Find the key val . More... | |
template<typename Q , typename Less , typename Func , typename Bucket = bucket_type, typename = typename std::enable_if< Bucket::has_find_with >::type> | |
bool | find_with (Q &val, Less pred, Func f) |
Find the key val using pred predicate. More... | |
template<typename Q , typename Func > | |
bool | find (Q const &val, Func f) |
Find the key val . More... | |
template<typename Q , typename Less , typename Func , typename Bucket = bucket_type, typename = typename std::enable_if< Bucket::has_find_with >::type> | |
bool | find_with (Q const &val, Less pred, Func f) |
Find the key val using pred predicate. More... | |
template<typename Q > | |
bool | contains (Q const &key) |
Checks whether the set contains key . More... | |
template<typename Q , typename Less , typename Bucket = bucket_type, typename = typename std::enable_if< Bucket::has_find_with >::type> | |
bool | contains (Q const &key, Less pred) |
Checks whether the map contains key using pred predicate for searching. More... | |
void | clear () |
Clears the set. 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. | |
resizing_policy & | get_resizing_policy () |
Returns resizing policy object. | |
resizing_policy const & | get_resizing_policy () const |
Returns resizing policy (const version) | |
Additional Inherited Members | |
Protected Types inherited from cds::intrusive::StripedSet< Container, Options... > | |
typedef Container | underlying_container_type |
original intrusive container type for the bucket | |
typedef cds::intrusive::striped_set::adapt< underlying_container_type, Options... >::type | bucket_type |
container type adapted for hash set | |
typedef bucket_type::value_type | value_type |
value type stored in the set | |
typedef options::hash | hash |
Hash functor. | |
typedef options::item_counter | item_counter |
Item counter. | |
typedef cds::opt::select_default< typename options::resizing_policy, typename bucket_type::default_resizing_policy >::type | resizing_policy |
Resizing policy. | |
typedef options::allocator | allocator_type |
allocator type specified in options. | |
typedef options::mutex_policy | mutex_policy |
Mutex policy. | |
typedef cds::details::Allocator< bucket_type, allocator_type > | bucket_allocator |
bucket allocator type based on allocator_type | |
Protected Member Functions inherited from cds::intrusive::StripedSet< Container, Options... > | |
StripedSet () | |
Default ctor. The initial capacity is 16. | |
StripedSet (size_t nCapacity) | |
Ctor with initial capacity specified. More... | |
StripedSet (size_t nCapacity, resizing_policy const &resizingPolicy) | |
Ctor with resizing policy (copy semantics) More... | |
StripedSet (size_t nCapacity, resizing_policy &&resizingPolicy) | |
Ctor with resizing policy (move semantics) More... | |
~StripedSet () | |
Destructor destroys internal data. | |
bool | insert (value_type &val) |
Inserts new node. More... | |
bool | insert (value_type &val, Func f) |
Inserts new node. More... | |
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... | |
value_type * | erase (Q const &val) |
Deletes the item from the set. More... | |
value_type * | erase (Q const &val, Func f) |
Deletes the item from the set. More... | |
value_type * | erase_with (Q const &val, Less pred) |
Deletes the item from the set using pred predicate for searching. More... | |
value_type * | erase_with (Q const &val, Less pred, Func f) |
Deletes the item from the set using pred predicate for searching. More... | |
bool | find (Q &val, Func f) |
Find the key val . More... | |
bool | find (Q const &val, Func f) |
Find the key val . More... | |
bool | find_with (Q &val, Less pred, Func f) |
Find the key val using pred predicate. More... | |
bool | find_with (Q const &val, Less pred, Func f) |
Find the key val using pred predicate. More... | |
bool | contains (Q const &key) |
Checks whether the set contains key . More... | |
bool | contains (Q const &key, Less pred) |
Checks whether the set contains key using pred predicate for searching. More... | |
void | clear () |
Clears the set. More... | |
void | clear_and_dispose (Disposer disposer) |
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. | |
resizing_policy & | get_resizing_policy () |
Returns resizing policy object. | |
resizing_policy const & | get_resizing_policy () const |
Returns resizing policy (const version) | |
Protected Attributes inherited from cds::intrusive::StripedSet< Container, Options... > | |
bucket_type * | m_Buckets |
Bucket table. | |
atomics::atomic< size_t > | m_nBucketMask |
Bucket table size - 1. m_nBucketMask + 1 should be power of two. | |
item_counter | m_ItemCounter |
Item counter. | |
hash | m_Hash |
Hash functor. | |
mutex_policy | m_MutexPolicy |
Mutex policy. | |
resizing_policy | m_ResizingPolicy |
Resizing policy. | |
Static Protected Attributes inherited from cds::intrusive::StripedSet< Container, Options... > | |
static const size_t | c_nMinimalCapacity |
Minimal capacity. | |
Striped hash set.
Source
Lock striping is very simple technique. The set consists of the bucket table and the array of locks. Initially, the capacity of lock array and bucket table is the same. When set is resized, bucket table capacity will be doubled but lock array will not. The lock i
protects each bucket j
, where j = i mod L
, where L
- the size of lock array.
Template arguments:
Container
- the container class that is used as bucket table entry. The Container
class should support an uniform interface described below.Options
- optionsThe StripedSet
class does not exactly dictate the type of container that should be used as a Container
bucket. Instead, the class supports different container type for the bucket, for exampe, std::list
, std::set
and others.
Remember that StripedSet
class algorithm ensures sequential blocking access to its bucket through the mutex type you specify among Options
template arguments.
The Options
are:
opt::mutex_policy
- concurrent access policy. Available policies: intrusive::striped_set::striping
, intrusive::striped_set::refinable
. Default is striped_set::striping
.opt::hash
- hash functor. Default option value see opt::v::hash_selector<opt::none>
which selects default hash functor for your compiler.opt::compare
- key comparison functor. No default functor is provided. If the option is not specified, the opt::less
is used.opt::less
- specifies binary predicate used for key comparison. Default is std::less<T>
.opt::item_counter
- item counter type. Default is atomicity::item_counter
since some operation on the counter is performed without locks. Note that item counting is an essential part of the set algorithm, so dummy counter like as atomicity::empty_item_counter
is not suitable.opt::allocator
- the allocator type using for memory allocation of bucket table and lock array. Default is CDS_DEFAULT_ALLOCATOR.opt::resizing_policy
- the resizing policy that is a functor that decides when to resize the hash set. Default option value depends on bucket container type: for sequential containers like std::list
, std::vector
the resizing policy is striped_set::load_factor_resizing<4>
; for other type of containers like std::set
, std::unordered_set
the resizing policy is striped_set::no_resizing
. See available resizing policy. Note that the choose of resizing policy depends of Container
type: for sequential containers like std::list
, std::vector
and so on, right choosing of the policy can significantly improve performance. For other, non-sequential types of Container
(like a std::set
) the resizing policy is not so important.opt::copy_policy
- the copy policy which is used to copy items from the old set to the new one when resizing. The policy can be optionally used in adapted bucket container for performance reasons of resizing. The detail of copy algorithm depends on type of bucket container and explains below.opt::compare
or opt::less
options are used in some Container
class for searching an item. opt::compare
option has the highest priority: if opt::compare
is specified, opt::less
is not used.
You can pass other option that would be passed to adapt
metafunction, see below.
Internal details
The StripedSet
class cannot utilize the Container
container specified directly, but only its adapted variant which supports an unified interface. Internally, the adaptation is made via striped_set::adapt metafunction that wraps bucket container and provides the unified bucket interface suitable for StripedSet
. Such adaptation is completely transparent for you - you don't need to call adapt
metafunction directly, StripedSet
class's internal machinery itself invokes appropriate adapt
metafunction to adjust your Container
container class to StripedSet
bucket's internal interface. All you need is to include a right header before striped_hash_set.h
.
By default, striped_set::adapt<AnyContainer, Options...>
metafunction does not make any wrapping to AnyContainer
, so, the result striped_set::adapt<AnyContainer, Options...>::type
is the same as AnyContainer
. However, there are a lot of specializations of striped_set::adapt
for well-known containers, see table below. Any of this specialization wraps corresponding container making it suitable for the set's bucket. Remember, you should include the proper header file for adapt
before including striped_hash_set.h
.
Container | .h-file for adapt | Example | Notes |
---|---|---|---|
std::list | <cds/container/striped_set/std_list.h> | #include <cds/container/striped_set/std_list.h> #include <cds/container/striped_hash_set.h> typedef cds::container::StripedSet< std::list<T>, > striped_set; | The list is ordered. Template argument pack Options must contain cds::opt::less or cds::opt::compare for type T stored in the list |
std::vector | <cds/container/striped_set/std_vector.h> | #include <cds/container/striped_set/std_vector.h> #include <cds/container/striped_hash_set.h> typedef cds::container::StripedSet< std::vector<T>, > striped_set; | The vector is ordered. Template argument pack Options must contain cds::opt::less or cds::opt::compare for type T stored in the list |
std::set | <cds/container/striped_set/std_set.h> | #include <cds/container/striped_set/std_set.h> #include <cds/container/striped_hash_set.h> typedef cds::container::StripedSet< std::set< T, std::less<T> > > striped_set; | |
std::unordered_set | <cds/container/striped_set/std_hash_set.h> | #include <cds/container/striped_set/std_hash_set.h> #include <cds/container/striped_hash_set.h> typedef cds::container::StripedSet< std::unordered_set< T, hash<T>, equal<T> > > striped_set; | You should provide two different hash function h1 and h2 - one for std::unordered_set and other for StripedSet . For the best result, h1 and h2 must be orthogonal i.e. h1(X) != h2(X) for any value X . |
boost::container::slist | <cds/container/striped_set/boost_slist.h> | #include <cds/container/striped_set/boost_slist.h> #include <cds/container/striped_hash_set.h> typedef cds::container::StripedSet< boost::container::slist<T> > striped_set; | The list is ordered. Options must contain cds::opt::less or cds::opt::compare . |
boost::container::list | <cds/container/striped_set/boost_list.h> | #include <cds/container/striped_set/boost_list.h> #include <cds/container/striped_hash_set.h> typedef cds::container::StripedSet< boost::container::list<T> > striped_set; | The list is ordered. Options must contain cds::opt::less or cds::opt::compare . |
boost::container::vector | <cds/container/striped_set/boost_vector.h> | #include <cds/container/striped_set/boost_vector.h> #include <cds/container/striped_hash_set.h> typedef cds::container::StripedSet< boost::container::vector<T>, > striped_set; | The vector is ordered. Template argument pack Options must contain cds::opt::less or cds::opt::compare for type T stored in the vector |
boost::container::stable_vector | <cds/container/striped_set/boost_stable_vector.h> | #include <cds/container/striped_set/boost_stable_vector.h> #include <cds/container/striped_hash_set.h> typedef cds::container::StripedSet< boost::container::stable_vector<T>, > striped_set; | The vector is ordered. Template argument pack Options must contain cds::opt::less or cds::opt::compare for type T stored in the vector |
boost::container::set | <cds/container/striped_set/boost_set.h> | #include <cds/container/striped_set/boost_set.h> #include <cds/container/striped_hash_set.h> typedef cds::container::StripedSet< boost::container::set< T, std::less<T> > > striped_set; | |
boost::container::flat_set | <cds/container/striped_set/boost_flat_set.h> | #include <cds/container/striped_set/boost_flat_set.h> #include <cds/container/striped_hash_set.h> typedef cds::container::StripedSet< boost::container::flat_set< T, std::less<T> > > striped_set; | |
boost::unordered_set | <cds/container/striped_set/boost_unordered_set.h> | #include <cds/container/striped_set/boost_unordered_set.h> #include <cds/container/striped_hash_set.h> typedef cds::container::StripedSet< boost::unordered_set< T, hash<T>, equal<T> > > striped_set; | You should provide two different hash function h1 and h2 - one for boost::unordered_set and other for StripedSet . For the best result, h1 and h2 must be orthogonal i.e. h1(X) != h2(X) for any value X . |
You can use another container type as set's bucket. Suppose, you have a container class MyBestContainer
and you want to integrate it with StripedSet
as bucket type. There are two possibility:
MyBestContainer
class has native support of bucket's interface; in this case, you can use default striped_set::adapt
metafunction;MyBestContainer
class does not support bucket's interface, which means, that you should develop a specialization cds::container::striped_set::adapt<MyBestContainer>
metafunction providing necessary interface.The striped_set::adapt< Container, Options... >
metafunction has two template argument:
Container
is the class that should be used as the bucket, for example, std::list< T >
.Options
pack is the options from StripedSet
declaration. The adapt
metafunction can use any option from Options
for its internal use. For example, a compare
option can be passed to adapt
metafunction via Options
argument of StripedSet
declaration.See striped_set::adapt metafunction for the description of interface that the bucket container must provide to be StripedSet compatible.
Copy policy There are three predefined copy policy:
cds::container::striped_set::copy_item
- copy item from old bucket to new one when resizing using copy ctor. It is default policy for any compiler that do not support move semanticscds::container::striped_set::move_item
- move item from old bucket to new one when resizing using move semantics. It is default policy for any compiler that support move semantics. If compiler does not support move semantics, the move policy is the same as copy_item
cds::container::striped_set::swap_item
- copy item from old bucket to new one when resizing using std::swap
. Not all containers support this copy policy, see details in table below.You can define your own copy policy specifically for your case. Note, right copy policy can significantly improve the performance of resizing.
Container | Policies |
---|---|
| struct copy_item { void operator()( std::list<T>& list, std::list<T>::iterator itInsert, std::list<T>::iterator itWhat ) { list.insert( itInsert, *itWhat ); } } // The type T stored in the list must be swappable struct swap_item { void operator()( std::list<T>& list, std::list<T>::iterator itInsert, std::list<T>::iterator itWhat ) { std::swap( *list.insert( itInsert, T()), *itWhat ); } } struct move_item { void operator()( std::list<T>& list, std::list<T>::iterator itInsert, std::list<T>::iterator itWhat ) { list.insert( itInsert, std::move( *itWhat )); } } |
| struct copy_item { void operator()( std::set<T>& set, std::set<T>::iterator itWhat ) { set.insert( *itWhat ); } } struct move_item { void operator()( std::set<T>& set, std::set<T>::iterator itWhat ) { set.insert( std::move( *itWhat )); } } |
| struct copy_item { void operator()( bc::slist<T>& list, bc::slist<T>::iterator itInsert, bc::slist<T>::iterator itWhat ) { list.insert_after( itInsert, *itWhat ); } } // The type T stored in the list must be swappable struct swap_item { void operator()( bc::slist<T>& list, bc::slist<T>::iterator itInsert, bc::slist<T>::iterator itWhat ) { std::swap( *list.insert_after( itInsert, T()), *itWhat ); } } struct move_item { void operator()( bc::slist<T>& list, bc::slist<T>::iterator itInsert, bc::slist<T>::iterator itWhat ) { list.insert_after( itInsert, std::move( *itWhat )); } } |
Advanced functions
libcds provides some advanced functions like erase_with()
, find_with()
, that cannot be supported by all underlying containers. The table below shows whether underlying container supports those functions (the sign "+" means "container supports the function"):
Container | find_with | erse_with |
---|---|---|
std::list | + | + |
std::vector | + | + |
std::set | - | - |
std::unordered_set | - | - |
boost::container::slist | + | + |
boost::container::list | + | + |
boost::container::vector | + | + |
boost::container::stable_vector | + | + |
boost::container::set | - | - |
boost::container::flat_set | - | - |
boost::unordered_set | - | - |
|
inline |
Ctor with initial capacity specified.
nCapacity | Initial size of bucket table and lock array. Must be power of two, the minimum is 16. |
|
inline |
Ctor with resizing policy (copy semantics)
This constructor initializes m_ResizingPolicy member with copy of resizingPolicy
parameter
nCapacity | Initial size of bucket table and lock array. Must be power of two, the minimum is 16. |
resizingPolicy | Resizing policy |
|
inline |
Ctor with resizing policy (move semantics)
This constructor initializes m_ResizingPolicy member moving resizingPolicy
parameter Move semantics is used. Available only for the compilers that supports C++11 rvalue reference.
nCapacity | Initial size of bucket table and lock array. Must be power of two, the minimum is 16. |
resizingPolicy | Resizing policy |
|
inline |
Returns the size of hash table.
The hash table size is non-constant and can be increased via resizing.
|
inline |
Clears the set.
The function erases all items from the set.
|
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.
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
. Otherwise, you may use contains( Q const&, Less pred )
functions with explicit predicate for key comparing.
|
inline |
Checks whether the map contains key
using pred
predicate for searching.
The function is similar to 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.
|
inline |
Inserts data of type value_type
constructed with std::forward<Args>(args)...
Returns true
if inserting successful, false
otherwise.
|
inline |
Checks if the set is empty.
Emptiness is checked by item counting: if item count is zero then the set is empty.
|
inline |
|
inline |
|
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.
erase_with
feature.
|
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.
erase_with
feature.
|
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:
where item
is the item found, val
is the find
function argument.
The functor can 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 can modify both arguments.
The type Q
can differ from value_type
of items storing in the container. Therefore, the value_type
should be comparable with type Q
.
The function returns true
if val
is found, false
otherwise.
|
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:
where item
is the item found, val
is the find
function argument.
The functor can change non-key fields of item
.
The type Q
can differ from value_type
of items storing in the container. Therefore, the value_type
should be comparable with type Q
.
The function returns true
if val
is found, false
otherwise.
|
inline |
Find the key val
using pred
predicate.
The function is an analog of find(Q&, Func) but pred
is used for key comparing Less
has the interface like std::less
. pred
must imply the same element order as the comparator used for building the set.
find_with
feature.
|
inline |
Find the key val
using pred
predicate.
The function is an analog of find(Q const&, Func) but pred
is used for key comparing Less
has the interface like std::less
. pred
must imply the same element order as the comparator used for building the set.
find_with
feature.
|
inline |
Inserts new node.
The function creates a node with copy of val
value and then inserts the node created into the set.
The type Q
should contain as minimum the complete key for the node. The object of value_type
should be constructible from a value of type Q
. In trivial case, Q
is equal to value_type
.
Returns true
if val
is inserted into the set, false
otherwise.
|
inline |
Inserts new node.
The function allows to split creating of new item into two part:
f
functor to initialize value-field of new item .The functor signature is:
where item
is the item inserted.
The type Q
can differ from value_type
of items storing in the set. Therefore, the value_type
should be constructible from type Q
.
The user-defined functor is called only if the inserting is success.
|
inline |
Updates the node.
The operation performs inserting or changing data with lock-free manner.
If key
is not found in the set, then key
is inserted iff bAllowInsert
is true
. Otherwise, the functor func
is called with item found.
The functor signature is:
with arguments:
bNew
- true
if the item has been inserted, false
otherwiseitem
- item of the setval
- argument val
passed into the update
() functionThe functor may change non-key fields of the item
.
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 is in the map.