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

Split-ordered list. More...

#include <cds/intrusive/split_list.h>

Public Types

typedef Traits options
 Traits template parameters.
 
typedef GC gc
 Garbage collector.
 
typedef OrderedList ordered_list
 type of ordered list used as base for split-list
 
typedef ordered_list::value_type value_type
 type of value stored in the split-list
 
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 options::back_off back_off
 back-off strategy for spinning
 
typedef options::memory_model memory_model
 Memory ordering. See cds::opt::memory_model option.
 
typedef iterator_type< false > iterator
 Forward iterator. More...
 
typedef iterator_type< true > const_iterator
 Const forward iterator. More...
 

Public Member Functions

 SplitListSet ()
 Initialize split-ordered list of default capacity. More...
 
 SplitListSet (size_t nItemCount, size_t nLoadFactor=1)
 Initialize split-ordered list. More...
 
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 (const Q &val, Less pred)
 Deletes the item from the set with comparing functor pred. More...
 
template<typename Q , typename Func >
bool erase (Q const &val, Func f)
 Deletes the item from the set. More...
 
template<typename Q , typename Less , typename Func >
bool erase_with (Q const &val, Less pred, Func f)
 Deletes the item from the set with comparing functor pred. 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 with pred predicate for comparing. 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 with pred predicate for comparing. 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 with pred predicate for comparing. More...
 
size_t size () const
 Returns item count in the set.
 
bool empty () const
 Checks if the set is empty. More...
 
void clear ()
 Clears the set (non-atomic) More...
 
iterator begin ()
 Returns a forward iterator addressing the first element in a split-list. More...
 
iterator end ()
 Returns an iterator that addresses the location succeeding the last element in a split-list. More...
 
const_iterator begin () const
 Returns a forward const iterator addressing the first element in a split-list.
 
const_iterator end () const
 Returns an const iterator that addresses the location succeeding the last element in a split-list.
 

Protected Types

typedef ordered_list::node_type list_node_type
 Node type as declared in ordered list.
 
typedef split_list::node
< list_node_type
node_type
 split-list node type
 
typedef node_type dummy_node_type
 dummy node type
 
typedef
split_list::node_traits
< typename
ordered_list::node_traits > 
node_traits
 Split-list node traits. More...
 

Protected Attributes

ordered_list_wrapper m_List
 Ordered list containing split-list items.
 
bucket_table m_Buckets
 bucket table
 
std::atomic< size_t > m_nBucketCountLog2
 log2( current bucket count )
 
item_counter m_ItemCounter
 Item counter.
 
hash m_HashFunctor
 Hash functor.
 

Detailed Description

template<class GC, class OrderedList, class Traits = split_list::type_traits>
class cds::intrusive::SplitListSet< GC, OrderedList, Traits >

Split-ordered list.

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

The split-ordered list is a lock-free implementation of an extensible unbounded hash table. It uses original recursive split-ordering algorithm discovered by Ori Shalev and Nir Shavit that allows to split buckets without moving an item on resizing.

Short description [from [2003] Ori Shalev, Nir Shavit "Split-Ordered Lists - Lock-free Resizable Hash Tables"]

The algorithm keeps all the items in one lock-free linked list, and gradually assigns the bucket pointers to the places in the list where a sublist of “correct” items can be found. A bucket is initialized upon first access by assigning it to a new “dummy” node (dashed contour) in the list, preceding all items that should be in that bucket. A newly created bucket splits an older bucket’s chain, reducing the access cost to its items. The table uses a modulo 2**i hash (there are known techniques for “pre-hashing” before a modulo 2**i hash to overcome possible binary correlations among values). The table starts at size 2 and repeatedly doubles in size.

Unlike moving an item, the operation of directing a bucket pointer can be done in a single CAS operation, and since items are not moved, they are never “lost”. However, to make this approach work, one must be able to keep the items in the list sorted in such a way that any bucket’s sublist can be “split” by directing a new bucket pointer within it. This operation must be recursively repeatable, as every split bucket may be split again and again as the hash table grows. To achieve this goal the authors introduced recursive split-ordering, a new ordering on keys that keeps items in a given bucket adjacent in the list throughout the repeated splitting process.

Magically, yet perhaps not surprisingly, recursive split-ordering is achieved by simple binary reversal: reversing the bits of the hash key so that the new key’s most significant bits (MSB) are those that were originally its least significant. The split-order keys of regular nodes are exactly the bit-reverse image of the original keys after turning on their MSB. For example, items 9 and 13 are in the 1 mod 4 bucket, which can be recursively split in two by inserting a new node between them.

To insert (respectively delete or search for) an item in the hash table, hash its key to the appropriate bucket using recursive split-ordering, follow the pointer to the appropriate location in the sorted items list, and traverse the list until the key’s proper location in the split-ordering (respectively until the key or a key indicating the item is not in the list is found). Because of the combinatorial structure induced by the split-ordering, this will require traversal of no more than an expected constant number of items.

The design is modular: to implement the ordered items list, you can use one of several non-blocking list-based set algorithms: MichaelList, LazyList.

Implementation

Template parameters are:

There are several specialization of the split-list class for different GC:

Hash functor

Some member functions of split-ordered list 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 choose ordered list type to use in your split-list set:

// For gc::HP-based MichaelList implementation
#include <cds/intrusive/michael_list_hp.h>
// cds::intrusive::SplitListSet declaration
#include <cds/intrusive/split_list.h>
// Type of set items
// Note you should declare your struct based on cds::intrusive::split_list::node
// which is a wrapper for ordered-list node struct.
// In our case, the node type for HP-based MichaelList is cds::intrusive::michael_list::node< cds::gc::HP >
struct Foo: public cds::intrusive::split_list::node< 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 base ordered-list type for split-list
// It may be any ordered list type like MichaelList, LazyList
// hook option
// item comparator option
>::type
> Foo_list ;

Second, you should declare split-list 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& s ) const
{
return cds::opt::v::hash<std::string>()( s ) ;
}
};
// Split-list set typedef
cds::gc::HP
,Foo_list
>::type
> Foo_set ;

Now, you can use Foo_set in your application.

Foo_set fooSet ;
Foo * foo = new Foo ;
foo->key_ = "First" ;
fooSet.insert( *foo ) ;
// and so on ...

Member Typedef Documentation

template<class GC, class OrderedList, class Traits = split_list::type_traits>
typedef iterator_type<true> cds::intrusive::SplitListSet< GC, OrderedList, Traits >::const_iterator

Const forward iterator.

For iterator's features and requirements see iterator

template<class GC, class OrderedList, class Traits = split_list::type_traits>
typedef iterator_type<false> cds::intrusive::SplitListSet< GC, OrderedList, Traits >::iterator

Forward iterator.

The forward iterator for a split-list has some features:

  • it has no post-increment operator
  • it depends on iterator of underlying OrderedList
  • 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 split-list.

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

template<class GC, class OrderedList, class Traits = split_list::type_traits>
typedef split_list::node_traits<typename ordered_list::node_traits> cds::intrusive::SplitListSet< GC, OrderedList, Traits >::node_traits
protected

Split-list node traits.

This traits is intended for converting between underlying ordered list node type list_node_type and split-list node type node_type

Constructor & Destructor Documentation

template<class GC, class OrderedList, class Traits = split_list::type_traits>
cds::intrusive::SplitListSet< GC, OrderedList, Traits >::SplitListSet ( )
inline

Initialize split-ordered list of default capacity.

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

template<class GC, class OrderedList, class Traits = split_list::type_traits>
cds::intrusive::SplitListSet< GC, OrderedList, Traits >::SplitListSet ( size_t  nItemCount,
size_t  nLoadFactor = 1 
)
inline

Initialize split-ordered list.

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

Member Function Documentation

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

Returns a forward iterator addressing the first element in a split-list.

For empty list

begin() == end()
template<class GC, class OrderedList, class Traits = split_list::type_traits>
void cds::intrusive::SplitListSet< GC, OrderedList, Traits >::clear ( )
inline

Clears the set (non-atomic)

The function unlink all items from the set. The function is not atomic. 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 = split_list::type_traits>
bool cds::intrusive::SplitListSet< 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 split-list set implementation.

template<class GC, class OrderedList, class Traits = split_list::type_traits>
iterator cds::intrusive::SplitListSet< GC, OrderedList, Traits >::end ( )
inline

Returns an iterator that addresses the location succeeding the last element in a split-list.

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 split-list. For empty list

begin() == end()
template<class GC, class OrderedList, class Traits = split_list::type_traits>
template<typename Func >
std::pair<bool, bool> cds::intrusive::SplitListSet< 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 is 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 can 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 can pass func argument by value or 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 = split_list::type_traits>
template<typename Q >
bool cds::intrusive::SplitListSet< 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.

Difference between erase and unlink functions: erase finds a key and deletes the item found. unlink finds an item by key and deletes it only if val is an item of that set, i.e. the pointer to item found is equal to &val .

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 = split_list::type_traits>
template<typename Q , typename Func >
bool cds::intrusive::SplitListSet< GC, OrderedList, Traits >::erase ( Q const &  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, unlinks it from the set, and returns true. The disposer specified by 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 can 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 = split_list::type_traits>
template<typename Q , typename Less >
bool cds::intrusive::SplitListSet< GC, OrderedList, Traits >::erase_with ( const Q &  val,
Less  pred 
)
inline

Deletes the item from the set with comparing functor pred.

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

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

Deletes the item from the set with comparing functor pred.

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

template<class GC, class OrderedList, class Traits = split_list::type_traits>
template<typename Q , typename Func >
bool cds::intrusive::SplitListSet< 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 value or by reference using boost::ref or cds::ref.

The functor can 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 can 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 = split_list::type_traits>
template<typename Q , typename Func >
bool cds::intrusive::SplitListSet< 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 const& val ) ;
};

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

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

The functor can 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 = split_list::type_traits>
template<typename Q >
bool cds::intrusive::SplitListSet< 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. Otherwise, you may use find_with functions with explicit predicate for key comparing.

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

Finds the key val with pred predicate for comparing.

The function is an analog of find(Q&, Func) but cmp is used for key compare. Less has the interface like std::less. cmp must imply the same element order as the comparator used for building the set.

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

Finds the key val with pred predicate for comparing.

The function is an analog of find(Q const&, Func) but cmp is used for key compare. Less has the interface like std::less. cmp must imply the same element order as the comparator used for building the set.

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

Finds the key val with pred predicate for comparing.

The function is an analog of find(Q const&) but cmp is used for key compare. Less has the interface like std::less. cmp must imply the same element order as the comparator used for building the set.

template<class GC, class OrderedList, class Traits = split_list::type_traits>
bool cds::intrusive::SplitListSet< 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 = split_list::type_traits>
template<typename Func >
bool cds::intrusive::SplitListSet< 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 may be passed by reference using boost::ref

template<class GC, class OrderedList, class Traits = split_list::type_traits>
bool cds::intrusive::SplitListSet< 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 unlinks it from the set if it is found and is equal to val.

Difference between erase and unlink functions: erase finds a key and deletes the item found. unlink finds an item by key and deletes it only if val is an item of that set, i.e. the pointer to item found 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:38:00 by Doxygen 1.8.3.1