|
cds
2.3.2
|
Striped hash set. More...
#include <cds/intrusive/striped_set.h>
Public Types | |
| 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 | |
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. | |
| 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_type * | erase (Q const &val) |
| Deletes the item from the set. More... | |
| template<typename Q , typename Less > | |
| value_type * | erase_with (Q const &val, Less pred) |
Deletes the item from the set using pred predicate for searching. More... | |
| template<typename Q , typename Func > | |
| value_type * | erase (Q const &val, Func f) |
| Deletes the item from the set. More... | |
| template<typename Q , typename Less , typename Func > | |
| value_type * | erase_with (Q const &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) |
Find the key val. More... | |
| template<typename Q , typename Less , typename Func > | |
| 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 > | |
| 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 > | |
| 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... | |
| template<typename Disposer > | |
| 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 | |
| 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 | |
| static const size_t | c_nMinimalCapacity = 16 |
| 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 intrusive container type for the bucket, for exampe, boost::intrusive::list, boost::intrusive::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: striped_set::striping, striped_set::refinable. Default is striped_set::striping.cds::opt::hash - hash functor. Default option value see opt::v::hash_selector <opt::none> which selects default hash functor for your compiler.cds::opt::compare - key comparison functor. No default functor is provided. If the option is not specified, the opt::less is used.cds::opt::less - specifies binary predicate used for key comparison. Default is std::less<T>.cds::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 atomicity::empty_item_counter is not suitable.cds::opt::allocator - the allocator type using for memory allocation of bucket table and lock array. Default is CDS_DEFAULT_ALLOCATOR.cds::opt::resizing_policy - the resizing policy - a functor that decides when to resize the hash set. Default option value depends on bucket container type: for sequential containers like boost::intrusive::list the resizing policy is cds::container::striped_set::load_factor_resizing<4> ; for other type of containers like boost::intrusive::set the resizing policy is cds::container::striped_set::no_resizing. See available resizing policy. Note that the choose of resizing policy depends of Container type: for sequential containers like boost::intrusive::list the right policy can significantly improve performance. For other, non-sequential types of Container (like a boost::intrusive::set) the resizing policy is not so important.cds::opt::buffer - an initialized buffer type used only for boost::intrusive::unordered_set. Default is cds::opt::v::initialized_static_buffer< cds::any_type, 256 > .
opt::compare or opt::less options are used in some Container class for ordering. 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 specified directly, but only its adapted variant which supports an unified interface. Internally, the adaptation is made via intrusive::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 specialization to adjust your Container container class to StripedSet bucket's internal interface. All you need is to include a right header before striped_set.h.
By default, intrusive::striped_set::adapt<AnyContainer, OptionPack> metafunction does not make any wrapping to AnyContainer, so, the result intrusive::striped_set::adapt<AnyContainer, OptionPack>::type is the same as AnyContainer. However, there are a lot of specializations of intrusive::striped_set::adapt for boost::intrusive 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_set.h.
boost::intrusive::constant_time_size<true> option for all boost::intrusive container that supports this option. Fast item counting feature is essential part of StripedSet resizing algorithm.| Container | .h-file for adapt | Example | Notes |
|---|---|---|---|
boost::intrusive::list | <cds/intrusive/striped_set/boost_list.h> | #include <cds/intrusive/striped_set/boost_list.h> #include <cds/intrusive/striped_set.h> typedef cds::intrusive::StripedSet< boost::intrusive::list<T, boost::intrusive::constant_time_size<true> >, > 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 |
boost::intrusive::slist | <cds/intrusive/striped_set/boost_slist.h> | #include <cds/intrusive/striped_set/boost_slist.h> #include <cds/intrusive/striped_set.h> typedef cds::intrusive::StripedSet< boost::intrusive::slist<T, boost::intrusive::constant_time_size<true> >, > 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 |
boost::intrusive::set | <cds/intrusive/striped_set/boost_set.h> | #include <cds/intrusive/striped_set/boost_set.h> #include <cds/intrusive/striped_set.h> typedef cds::intrusive::StripedSet< boost::intrusive::set<T, boost::intrusive::constant_time_size<true> > > striped_set; | Note that boost::intrusive::compare option using in boost::intrusive::set should support T type stored in the set and any type Q that you can use in erase() and find() member functions. |
boost::intrusive::unordered_set | <cds/intrusive/striped_set/boost_unordered_set.h> | #include <cds/intrusive/striped_set/boost_unordered_set.h> #include <cds/intrusive/striped_set.h> typedef cds::intrusive::StripedSet< boost::intrusive::unordered_set<T ,boost::intrusive::constant_time_size<true> ,boost::intrusive::hash< user_provided_hash_functor > > > striped_set; | You should provide two different hash function The option |
boost::intrusive::avl_set | <cds/intrusive/striped_set/boost_avl_set.h> | #include <cds/intrusive/striped_set/boost_avl_set.h> #include <cds/intrusive/striped_set.h> typedef cds::intrusive::StripedSet< boost::intrusive::avl_set<T, boost::intrusive::constant_time_size<true> > > striped_set; | Note that boost::intrusive::compare option using in boost::intrusive::avl_set should support T type stored in the set and any type Q that you can use in erase() and find() member functions. |
boost::intrusive::sg_set | <cds/intrusive/striped_set/boost_sg_set.h> | #include <cds/intrusive/striped_set/boost_sg_set.h> #include <cds/intrusive/striped_set.h> typedef cds::intrusive::StripedSet< boost::intrusive::sg_set<T, boost::intrusive::constant_time_size<true> > > striped_set; | Note that boost::intrusive::compare option using in boost::intrusive::sg_set should support T type stored in the set and any type Q that you can use in erase() and find() member functions. |
boost::intrusive::splay_set | <cds/intrusive/striped_set/boost_splay_set.h> | #include <cds/intrusive/striped_set/boost_splay_set.h> #include <cds/intrusive/striped_set.h> typedef cds::intrusive::StripedSet< boost::intrusive::splay_set<T, boost::intrusive::constant_time_size<true> > > striped_set; | Note that boost::intrusive::compare option using in boost::intrusive::splay_set should support T type stored in the set and any type Q that you can use in erase() and find() member functions. |
boost::intrusive::treap_set | <cds/intrusive/striped_set/boost_treap_set.h> | #include <cds/intrusive/striped_set/boost_treap_set.h> #include <cds/intrusive/striped_set.h> typedef cds::intrusive::StripedSet< boost::intrusive::treap_set<T, boost::intrusive::constant_time_size<true> > > striped_set; | Note that boost::intrusive::compare option using in boost::intrusive::treap_set should support T type stored in the set and any type Q that you can use in erase() and find() member functions. |
You can use another intrusive container type as striped 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 intrusive::striped_set::adapt metafunction;MyBestContainer class does not support bucket's interface, which means, that you should create a specialization of cds::intrusive::striped_set::adapt<MyBestContainer> metafunction providing necessary interface.The intrusive::striped_set::adapt< Container, OptionPack > metafunction has two template argument:
Container is the class that should be used as the bucket, for example, boost::intrusive::list< T >.OptionPack is the packed options from StripedSet declaration. The adapt metafunction can use any option from OptionPack for its internal use. For example, a compare option can be passed to adapt metafunction via OptionPack argument of StripedSet declaration.See intrusive::striped_set::adapt metafunction for the description of interface that the bucket container must provide to be StripedSet compatible.
|
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.
| 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 unlinks all items from the set.
|
inline |
Clears the set and calls disposer for each item.
The function unlinks all items from the set calling disposer for each item. Disposer functor interface is:
|
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 set contains key using pred predicate for searching.
The function is an analog of contains( key ) but pred is used for key comparing. Less functor has the interface like std::less. Less must imply the same element order as the comparator used for building the set.
|
inline |
Checks if the set is empty.
Emptiness is checked by item counting: if item count is zero then the set is empty.
|
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.
|
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, unlinks it from the set, and returns a pointer to unlinked item.
The Func interface is
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.
|
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 has the interface like std::less. pred must imply the same element order as the comparator used for building the set.
|
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 has the interface like std::less. pred must imply the same element order as the comparator used for building the set.
|
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 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.
|
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 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.
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.
|
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.
|
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.
|
inline |
Inserts new node.
The function allows to split creating of new item into two part:
f functor to initialize value-field of val.The functor signature is:
where val is the item inserted.
|
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.
|
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 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() 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.
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 val already is in the set.