-//$$CDS-header$$
+/*
+ This file is a part of libcds - Concurrent Data Structures library
-#ifndef __CDS_INTRUSIVE_STRIPED_SET_H
-#define __CDS_INTRUSIVE_STRIPED_SET_H
+ (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017
-#include <cds/intrusive/base.h>
+ Source code repo: http://github.com/khizmax/libcds/
+ Download: http://sourceforge.net/projects/libcds/files/
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice, this
+ list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+#ifndef CDSLIB_INTRUSIVE_STRIPED_SET_H
+#define CDSLIB_INTRUSIVE_STRIPED_SET_H
+
+#include <cds/intrusive/details/base.h>
#include <cds/intrusive/striped_set/adapter.h>
#include <cds/intrusive/striped_set/striping_policy.h>
namespace cds { namespace intrusive {
/// StripedSet related definitions
namespace striped_set {
+
+ /** @defgroup cds_striped_resizing_policy Resizing policy for striped/refinable set/map
+
+ Resizing policy for \p intrusive::StripedSet, \p container::StripedSet and \p container::StripedMap.
+ */
+
} // namespace striped_set
/// Striped hash set
- \p Options - options
The \p %StripedSet class does not exactly dictate the type of container that should be used as a \p Container bucket.
- Instead, the class supports different intrusive container type for the bucket, for exampe, \p boost::intrusive::list, \p boost::intrusive::set and others.
+ Instead, the class supports different intrusive container type for the bucket, for exampe,
+ \p boost::intrusive::list, \p boost::intrusive::set and others.
Remember that \p %StripedSet class algorithm ensures sequential blocking access to its bucket through the mutex type you specify
among \p Options template arguments.
The \p 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 <tt>opt::v::hash_selector <opt::none></tt> 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 \p std::less<T>.
- - cds::opt::item_counter - item counter type. Default is \p 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 type like atomicity::empty_item_counter
+ - \p opt::mutex_policy - concurrent access policy.
+ Available policies: \p striped_set::striping, \p striped_set::refinable.
+ Default is \p %striped_set::striping.
+ - \p cds::opt::hash - hash functor. Default option value see <tt>opt::v::hash_selector <opt::none></tt>
+ which selects default hash functor for your compiler.
+ - \p cds::opt::compare - key comparison functor. No default functor is provided.
+ If the option is not specified, the \p opt::less is used.
+ - \p cds::opt::less - specifies binary predicate used for key comparison. Default is \p std::less<T>.
+ - \p cds::opt::item_counter - item counter type. Default is \p 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 \p 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 that is a functor that decides when to resize the hash set.
+ - \p cds::opt::allocator - the allocator type using for memory allocation of bucket table and lock array. Default is \ref CDS_DEFAULT_ALLOCATOR.
+ - \p 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 \p boost::intrusive::list the resizing policy is <tt>cds::container::striped_set::load_factor_resizing <4></tt>;
- for other type of containers like \p boost::intrusive::set the resizing policy is cds::container::striped_set::no_resizing.
- See cds::container::striped_set namespace for list of all possible types of the option.
+ for sequential containers like \p boost::intrusive::list the resizing policy is <tt>cds::container::striped_set::load_factor_resizing<4> </tt>;
+ for other type of containers like \p boost::intrusive::set the resizing policy is cds::container::striped_set::no_resizing.
+ See \ref cds_striped_resizing_policy "available resizing policy".
Note that the choose of resizing policy depends of \p Container type:
- for sequential containers like \p boost::intrusive::list right choosing of the policy can significantly improve performance.
+ for sequential containers like \p boost::intrusive::list the right policy can significantly improve performance.
For other, non-sequential types of \p Container (like a \p boost::intrusive::set) the resizing policy is not so important.
- - cds::opt::buffer - a buffer type used only for boost::intrusive::unordered_set.
- Default is cds::opt::v::static_buffer< cds::any_type, 256 >.
+ - \p cds::opt::buffer - an initialized buffer type used only for \p boost::intrusive::unordered_set.
+ Default is <tt>cds::opt::v::initialized_static_buffer< cds::any_type, 256 > </tt>.
- opt::compare or opt::less options are used in some \p Container class for ordering.
- opt::compare option has the highest priority: if opt::compare is specified, opt::less is not used.
+ \p opt::compare or \p opt::less options are used in some \p Container class for ordering.
+ \p %opt::compare option has the highest priority: if \p %opt::compare is specified, \p %opt::less is not used.
- You can pass other option that would be passed to <tt>adapt</tt> metafunction, see below.
+ You can pass other option that would be passed to \p adapt metafunction, see below.
<b>Internal details</b>
- The \p %StripedSet class cannot utilize the \p Container 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
+ The \p %StripedSet class cannot utilize the \p Container specified directly, but only its adapted variant which
+ supports an unified interface. Internally, the adaptation is made via \p intrusive::striped_set::adapt metafunction that wraps bucket container
and provides the unified bucket interface suitable for \p %StripedSet. Such adaptation is completely transparent for you -
you don't need to call \p adapt metafunction directly, \p %StripedSet class's internal machinery itself invokes appropriate
\p adapt metafunction specialization to adjust your \p Container container class to \p %StripedSet bucket's internal interface.
</td>
<td>
The list is ordered.
- Template argument pack \p Options <b>must</b> contain cds::opt::less or cds::opt::compare for type \p T stored in the list
+ Template argument pack \p Options <b>must</b> contain \p cds::opt::less or \p cds::opt::compare for type \p T stored in the list
</td>
</tr>
<tr>
<td>
Note that \p boost::intrusive::compare option using in \p boost::intrusive::set
should support \p T type stored in the set and any type \p Q that you can use
- in \p erase and \p find member functions.
+ in \p erase() and \p find() member functions.
</td>
</tr>
<tr>
\endcode
</td>
<td>
- You should provide two different hash function h1 and h2 - one for boost::intrusive::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 should provide two different hash function \p h1 and \p h2 - one for \p boost::intrusive::unordered_set
+ and other for \p %StripedSet. For the best result, \p h1 and \p h2 must be orthogonal i.e. <tt>h1(X) != h2(X)</tt> for any value \p X
- The option opt::buffer is used for boost::intrusive::bucket_traits. Default is cds::opt::v::static_buffer< cds::any_type, 256 >.
+ The option \p opt::buffer is used for \p boost::intrusive::bucket_traits.
+ Default is <tt> cds::opt::v::initialized_static_buffer< cds::any_type, 256 > </tt>.
The resizing policy should correlate with the buffer capacity.
- The default resizing policy is cds::container::striped_set::load_factor_resizing<256> what gives load factor 1 for
+ The default resizing policy is <tt>cds::container::striped_set::load_factor_resizing<256> </tt> what gives load factor 1 for
default bucket buffer that is the best for \p boost::intrusive::unordered_set.
</td>
</tr>
<td>
Note that \p boost::intrusive::compare option using in \p boost::intrusive::avl_set
should support \p T type stored in the set and any type \p Q that you can use
- in \p erase and \p find member functions.
+ in \p erase() and \p find() member functions.
</td>
</tr>
<tr>
<td>
Note that \p boost::intrusive::compare option using in \p boost::intrusive::sg_set
should support \p T type stored in the set and any type \p Q that you can use
- in \p erase and \p find member functions.
+ in \p erase() and \p find() member functions.
</td>
</tr>
<tr>
<td>
Note that \p boost::intrusive::compare option using in \p boost::intrusive::splay_set
should support \p T type stored in the set and any type \p Q that you can use
- in \p erase and \p find member functions.
+ in \p erase() and \p find() member functions.
</td>
</tr>
<tr>
<td>
Note that \p boost::intrusive::compare option using in \p boost::intrusive::treap_set
should support \p T type stored in the set and any type \p Q that you can use
- in \p erase and \p find member functions.
+ in \p erase() and \p find() member functions.
</td>
</tr>
</table>
There are two possibility:
- either your \p MyBestContainer class has native support of bucket's interface;
in this case, you can use default \p intrusive::striped_set::adapt metafunction;
- - or your \p MyBestContainer class does not support bucket's interface, which means, that you should develop a specialization
+ - or your \p MyBestContainer class does not support bucket's interface, which means, that you should create a specialization of
<tt>cds::intrusive::striped_set::adapt<MyBestContainer> </tt> metafunction providing necessary interface.
The <tt>intrusive::striped_set::adapt< Container, OptionPack ></tt> metafunction has two template argument:
any option from \p OptionPack for its internal use. For example, a \p compare option can be passed to \p adapt
metafunction via \p OptionPack argument of \p %StripedSet declaration.
- See intrusive::striped_set::adapt metafunction for the description of interface that the bucket container must provide
+ See \p intrusive::striped_set::adapt metafunction for the description of interface that the bucket container must provide
to be \p %StripedSet compatible.
*/
- template <class Container, CDS_DECL_OPTIONS9>
+ template <class Container, typename... Options>
class StripedSet
{
public:
};
typedef typename cds::opt::make_options<
- typename cds::opt::find_type_traits< default_options, CDS_OPTIONS9 >::type
- ,CDS_OPTIONS9
+ typename cds::opt::find_type_traits< default_options, Options... >::type
+ ,Options...
>::type options;
//@endcond
typedef Container underlying_container_type ; ///< original intrusive container type for the bucket
- typedef typename cds::intrusive::striped_set::adapt< underlying_container_type, CDS_OPTIONS9 >::type bucket_type ; ///< container type adapted for hash set
+ typedef typename cds::intrusive::striped_set::adapt< underlying_container_type, Options... >::type bucket_type ; ///< container type adapted for hash set
typedef typename bucket_type::value_type value_type ; ///< value type stored in the set
typedef typename options::hash hash ; ///< Hash functor
typedef cds::details::Allocator< bucket_type, allocator_type > bucket_allocator; ///< bucket allocator type based on allocator_type
protected:
- bucket_type * m_Buckets ; ///< Bucket table
- 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
+ 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
typedef typename mutex_policy::scoped_cell_lock scoped_cell_lock;
typedef typename mutex_policy::scoped_full_lock scoped_full_lock;
typedef typename mutex_policy::scoped_resize_lock scoped_resize_lock;
-
-# ifndef CDS_CXX11_LAMBDA_SUPPORT
- struct empty_insert_functor {
- void operator()( value_type& )
- {}
- };
-
- struct empty_erase_functor {
- void operator()( value_type const& )
- {}
- };
-
- struct empty_find_functor {
- template <typename Q>
- void operator()( value_type& item, Q& val )
- {}
- };
-# endif
//@endcond
protected:
void alloc_bucket_table( size_t nSize )
{
assert( cds::beans::is_power2( nSize ));
- m_nBucketMask = nSize - 1;
+ m_nBucketMask.store( nSize - 1, atomics::memory_order_release );
m_Buckets = bucket_allocator().NewArray( nSize );
}
bucket_type * bucket( size_t nHash ) const CDS_NOEXCEPT
{
- return m_Buckets + (nHash & m_nBucketMask);
+ return m_Buckets + (nHash & m_nBucketMask.load( atomics::memory_order_relaxed ));
}
template <typename Q, typename Func>
for ( bucket_iterator it = pCur->begin(); it != itEnd; it = itNext ) {
itNext = it;
++itNext;
- bucket( m_Hash( *it ) )->move_item( *pCur, it );
+ bucket( m_Hash( *it ))->move_item( *pCur, it );
}
pCur->clear();
}
void resize()
{
- size_t nOldCapacity = bucket_count();
- size_t volatile& refBucketMask = m_nBucketMask;
+ size_t nOldCapacity = bucket_count( atomics::memory_order_acquire );
scoped_resize_lock al( m_MutexPolicy );
- if ( al.success() ) {
- if ( nOldCapacity != refBucketMask + 1 ) {
+ if ( al.success()) {
+ if ( nOldCapacity != bucket_count( atomics::memory_order_acquire )) {
// someone resized already
return;
}
/// Default ctor. The initial capacity is 16.
StripedSet()
: m_Buckets( nullptr )
- , m_nBucketMask( c_nMinimalCapacity - 1 )
- , m_MutexPolicy( c_nMinimalCapacity )
+ , m_nBucketMask( c_nMinimalCapacity - 1 )
+ , m_MutexPolicy( c_nMinimalCapacity )
{
- alloc_bucket_table( m_nBucketMask + 1 );
+ alloc_bucket_table( bucket_count());
}
/// Ctor with initial capacity specified
StripedSet(
size_t nCapacity ///< Initial size of bucket table and lock array. Must be power of two, the minimum is 16.
)
- : m_Buckets( nullptr )
- , m_nBucketMask( calc_init_capacity(nCapacity) - 1 )
- , m_MutexPolicy( m_nBucketMask + 1 )
+ : m_Buckets( nullptr )
+ , m_nBucketMask( calc_init_capacity(nCapacity) - 1 )
+ , m_MutexPolicy( bucket_count())
{
- alloc_bucket_table( m_nBucketMask + 1 );
+ alloc_bucket_table( bucket_count());
}
/// Ctor with resizing policy (copy semantics)
)
: m_Buckets( nullptr )
, m_nBucketMask( ( nCapacity ? calc_init_capacity(nCapacity) : c_nMinimalCapacity ) - 1 )
- , m_MutexPolicy( m_nBucketMask + 1 )
+ , m_MutexPolicy( bucket_count())
, m_ResizingPolicy( resizingPolicy )
{
- alloc_bucket_table( m_nBucketMask + 1 );
+ alloc_bucket_table( bucket_count());
}
-#ifdef CDS_RVALUE_SUPPORT
/// Ctor with resizing policy (move semantics)
/**
This constructor initializes m_ResizingPolicy member moving \p resizingPolicy parameter
- Move semantics is used. Available only for the compilers that supports C++11 rvalue reference.
+ Move semantics is used.
*/
StripedSet(
size_t nCapacity ///< Initial size of bucket table and lock array. Must be power of two, the minimum is 16.
)
: m_Buckets( nullptr )
, m_nBucketMask( ( nCapacity ? calc_init_capacity(nCapacity) : c_nMinimalCapacity ) - 1 )
- , m_MutexPolicy( m_nBucketMask + 1 )
- , m_ResizingPolicy( resizingPolicy )
+ , m_MutexPolicy( bucket_count())
+ , m_ResizingPolicy( std::forward<resizing_policy>( resizingPolicy ))
{
- alloc_bucket_table( m_nBucketMask + 1 );
+ alloc_bucket_table( bucket_count());
}
-#endif
/// Destructor destroys internal data
~StripedSet()
{
- free_bucket_table( m_Buckets, m_nBucketMask + 1 );
+ free_bucket_table( m_Buckets, bucket_count());
}
public:
*/
bool insert( value_type& val )
{
-# ifdef CDS_CXX11_LAMBDA_SUPPORT
return insert( val, []( value_type& ) {} );
-# else
- return insert( val, empty_insert_functor() );
-# endif
}
/// Inserts new node
void func( value_type& val );
\endcode
where \p val is the item inserted.
-
- The user-defined functor is called only if the inserting is success and can be passed by reference
- using <tt>boost::ref</tt>
*/
template <typename Func>
bool insert( value_type& val, Func f )
return bOk;
}
- /// Ensures that the \p val exists in the set
+ /// Updates the node
/**
The operation performs inserting or changing data with lock-free manner.
- If the item \p val not found in the set, then \p val is inserted into the set.
+ If the item \p val is not found in the set, then \p val is inserted
+ iff \p bAllowInsert is \p true.
Otherwise, the functor \p func is called with item found.
The functor signature is:
\code
with arguments:
- \p bNew - \p true if the item has been inserted, \p false otherwise
- \p item - item of the set
- - \p val - argument \p val passed into the \p ensure function
+ - \p val - argument \p val passed into the \p update() function
If new item has been inserted (i.e. \p bNew is \p true) then \p item and \p val arguments
refers to the same thing.
The functor may change non-key fields of the \p item.
- You may pass \p func argument by reference using <tt>boost::ref</tt> or cds::ref.
-
- Returns <tt> std::pair<bool, bool> </tt> where \p first is \p true if operation is successful,
- \p second is \p true if new item has been added or \p false if the item with \p key
+ Returns std::pair<bool, bool> where \p first is \p true if operation is successful,
+ \p second is \p true if new item has been added or \p false if the item with \p val
already is in the set.
*/
template <typename Func>
- std::pair<bool, bool> ensure( value_type& val, Func func )
+ std::pair<bool, bool> update( value_type& val, Func func, bool bAllowInsert = true )
{
std::pair<bool, bool> result;
bool bResize;
scoped_cell_lock sl( m_MutexPolicy, nHash );
pBucket = bucket( nHash );
- result = pBucket->ensure( val, func );
+ result = pBucket->update( val, func, bAllowInsert );
bResize = result.first && result.second && m_ResizingPolicy( ++m_ItemCounter, *this, *pBucket );
}
resize();
return result;
}
+ //@cond
+ template <typename Func>
+ std::pair<bool, bool> ensure( value_type& val, Func func )
+ {
+ return update( val, func, true );
+ }
+ //@endcond
/// Unlink the item \p val from the set
/**
template <typename Q>
value_type * erase( Q const& val )
{
-# ifdef CDS_CXX11_LAMBDA_SUPPORT
return erase( val, [](value_type const&) {} );
-# else
- return erase( val, empty_erase_functor() );
-# endif
}
/// Deletes the item from the set using \p pred predicate for searching
template <typename Q, typename Less>
value_type * erase_with( Q const& val, Less pred )
{
-# ifdef CDS_CXX11_LAMBDA_SUPPORT
return erase_with( val, pred, [](value_type const&) {} );
-# else
- return erase_with( val, pred, empty_erase_functor() );
-# endif
}
/// Deletes the item from the set
void operator()( value_type const& item );
};
\endcode
- The functor may be passed by reference with <tt>boost:ref</tt>
If the item with key equal to \p val is not found the function return \p false.
\endcode
where \p item is the item found, \p val is the <tt>find</tt> function argument.
- You can pass \p f argument by reference using <tt>boost::ref</tt> or cds::ref.
-
The functor may change non-key fields of \p item.
The \p val argument is non-const since it can be used as \p f functor destination i.e., the functor
\endcode
where \p item is the item found, \p val is the <tt>find</tt> function argument.
- You can pass \p f argument by reference using <tt>boost::ref</tt> or cds::ref.
-
The functor may change non-key fields of \p item.
The \p val argument is non-const since it can be used as \p f functor destination i.e., the functor
return find_with_( val, pred, f );
}
- /// Find the key \p val
- /** \anchor cds_intrusive_StripedSet_find_val
- The function searches the item with key equal to \p val
+ /// Checks whether the set contains \p key
+ /**
+ The function searches the item with key equal to \p key
and returns \p true if it is found, and \p false otherwise.
Note the hash functor specified for class \p Traits template parameter
should accept a parameter of type \p Q that can be not the same as \p value_type.
+ Otherwise, you may use \p contains( Q const&, Less pred ) functions with explicit predicate for key comparing.
*/
template <typename Q>
+ bool contains( Q const& key )
+ {
+ return find( key, [](value_type&, Q const& ) {} );
+ }
+ //@cond
+ template <typename Q>
+ CDS_DEPRECATED("use contains()")
bool find( Q const& val )
{
-# ifdef CDS_CXX11_LAMBDA_SUPPORT
- return find( val, [](value_type&, Q const& ) {} );
-# else
- return find( val, empty_find_functor() );
-# endif
+ return contains( val );
}
+ //@endcond
- /// Find the key \p val using \p pred predicate
+ /// Checks whether the set contains \p key using \p pred predicate for searching
/**
- The function is an analog of \ref cds_intrusive_StripedSet_find_val "find(Q const&)"
- but \p pred is used for key comparing
- \p Less has the interface like \p std::less.
- \p pred must imply the same element order as the comparator used for building the set.
+ The function is an analog of <tt>contains( key )</tt> but \p pred is used for key comparing.
+ \p Less functor has the interface like \p std::less.
+ \p Less must imply the same element order as the comparator used for building the set.
*/
template <typename Q, typename Less>
+ bool contains( Q const& key, Less pred )
+ {
+ return find_with( key, pred, [](value_type& , Q const& ) {} );
+ }
+ //@cond
+ template <typename Q, typename Less>
+ CDS_DEPRECATED("use contains()")
bool find_with( Q const& val, Less pred )
{
-# ifdef CDS_CXX11_LAMBDA_SUPPORT
- return find_with( val, pred, [](value_type& , Q const& ) {} );
-# else
- return find_with( val, pred, empty_find_functor() );
-# endif
+ return contains( val, pred );
}
+ //@endcond
/// Clears the set
/**
*/
size_t bucket_count() const
{
- return m_nBucketMask + 1;
+ return m_nBucketMask.load( atomics::memory_order_relaxed ) + 1;
+ }
+ //@cond
+ size_t bucket_count( atomics::memory_order load_mo ) const
+ {
+ return m_nBucketMask.load( load_mo ) + 1;
}
+ //@endcond
/// Returns lock array size
size_t lock_count() const
};
}} // namespace cds::itrusive
-#endif // #ifndef __CDS_INTRUSIVE_STRIPED_SET_H
+#endif // #ifndef CDSLIB_INTRUSIVE_STRIPED_SET_H