#define __CDS_CONTAINER_IMPL_MICHAEL_KVLIST_H
#include <memory>
-#include <functional> // ref
#include <cds/container/details/guarded_ptr_cast.h>
namespace cds { namespace container {
- /// Michael's ordered list (key-value pair)
+ /// Michael's ordered list fo key-value pair
/** @ingroup cds_nonintrusive_list
\anchor cds_nonintrusive_MichaelKVList_gc
constant key and alterable value.
Usually, ordered single-linked list is used as a building block for the hash table implementation.
- The complexity of searching is <tt>O(N)</tt>.
+ The complexity of searching is <tt>O(N)</tt> where \p N is the item count in the list, not in the
+ hash table.
Template arguments:
- \p GC - garbage collector used
- \p Key - key type of an item stored in the list. It should be copy-constructible
- \p Value - value type stored in a list
- - \p Traits - type traits, default is michael_list::type_traits
+ - \p Traits - type traits, default is \p michael_list::traits
- It is possible to declare option-based list with cds::container::michael_list::make_traits metafunction istead of \p Traits template
- argument. For example, the following traits-based declaration of gc::HP Michael's list
+ It is possible to declare option-based list with \p cds::container::michael_list::make_traits metafunction istead of \p Traits template
+ argument. For example, the following traits-based declaration of \p gc::HP Michael's list
\code
#include <cds/container/michael_kvlist_hp.h>
// Declare comparator for the item
}
};
- // Declare type_traits
- struct my_traits: public cds::container::michael_list::type_traits
+ // Declare traits
+ struct my_traits: public cds::container::michael_list::traits
{
typedef my_compare compare;
};
// Declare traits-based list
typedef cds::container::MichaelKVList< cds::gc::HP, int, int, my_traits > traits_based_list;
\endcode
-
is equivalent for the following option-based list
\code
#include <cds/container/michael_kvlist_hp.h>
> option_based_list;
\endcode
- Template argument list \p Options of cds::container::michael_list::make_traits metafunction are:
- - 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 \p std::less<T>.
- - opt::back_off - back-off strategy used. If the option is not specified, the cds::backoff::empty is used.
- - opt::item_counter - the type of item counting feature. Default is \ref atomicity::empty_item_counter that is no item counting.
- - opt::allocator - the allocator used for creating and freeing list's item. Default is \ref CDS_DEFAULT_ALLOCATOR macro.
- - opt::memory_model - C++ memory ordering model. Can be opt::v::relaxed_ordering (relaxed memory model, the default)
- or opt::v::sequential_consistent (sequentially consisnent memory model).
-
\par Usage
There are different specializations of this template for each garbage collecting schema used.
You should include appropriate .h-file depending on GC you are using:
typename Key,
typename Value,
#ifdef CDS_DOXYGEN_INVOKED
- typename Traits = michael_list::type_traits
+ typename Traits = michael_list::traits
#else
typename Traits
#endif
#endif
{
//@cond
- typedef details::make_michael_kvlist< GC, Key, Value, Traits > options;
- typedef typename options::type base_class;
+ typedef details::make_michael_kvlist< GC, Key, Value, Traits > maker;
+ typedef typename maker::type base_class;
//@endcond
public:
typedef Value mapped_type ; ///< Type of value stored in the list
typedef std::pair<key_type const, mapped_type> value_type ; ///< key/value pair stored in the list
#else
- typedef typename options::key_type key_type;
- typedef typename options::value_type mapped_type;
- typedef typename options::pair_type value_type;
+ typedef typename maker::key_type key_type;
+ typedef typename maker::value_type mapped_type;
+ typedef typename maker::pair_type value_type;
#endif
- typedef typename base_class::gc gc ; ///< Garbage collector used
- typedef typename base_class::back_off back_off ; ///< Back-off strategy used
- typedef typename options::allocator_type allocator_type ; ///< Allocator type used for allocate/deallocate the nodes
- typedef typename base_class::item_counter item_counter ; ///< Item counting policy used
- typedef typename options::key_comparator key_comparator ; ///< key comparison functor
- typedef typename base_class::memory_model memory_model ; ///< Memory ordering. See cds::opt::memory_model option
+ typedef typename base_class::gc gc; ///< Garbage collector used
+ typedef typename base_class::back_off back_off; ///< Back-off strategy used
+ typedef typename maker::allocator_type allocator_type; ///< Allocator type used for allocate/deallocate the nodes
+ typedef typename base_class::item_counter item_counter; ///< Item counting policy used
+ typedef typename maker::key_comparator key_comparator; ///< key comparison functor
+ typedef typename base_class::memory_model memory_model; ///< Memory ordering. See cds::opt::memory_model option
protected:
//@cond
- typedef typename base_class::value_type node_type;
- typedef typename options::cxx_allocator cxx_allocator;
- typedef typename options::node_deallocator node_deallocator;
- typedef typename options::type_traits::compare intrusive_key_comparator;
+ typedef typename base_class::value_type node_type;
+ typedef typename maker::cxx_allocator cxx_allocator;
+ typedef typename maker::node_deallocator node_deallocator;
+ typedef typename maker::intrusive_traits::compare intrusive_key_comparator;
typedef typename base_class::atomic_node_ptr head_type;
//@endcond
The function creates a node with \p key and default value, and then inserts the node created into the list.
Preconditions:
- - The \ref key_type should be constructible from value of type \p K.
- In trivial case, \p K is equal to \ref key_type.
- - The \ref mapped_type should be default-constructible.
+ - The \p key_type should be constructible from value of type \p K.
+ In trivial case, \p K is equal to \p key_type.
+ - The \p mapped_type should be default-constructible.
Returns \p true if inserting successful, \p false otherwise.
*/
The function creates a node with \p key and value \p val, and then inserts the node created into the list.
Preconditions:
- - The \ref key_type should be constructible from \p key of type \p K.
- - The \ref mapped_type should be constructible from \p val of type \p V.
+ - The \p key_type should be constructible from \p key of type \p K.
+ - The \p mapped_type should be constructible from \p val of type \p V.
Returns \p true if inserting successful, \p false otherwise.
*/
\endcode
The argument \p item of user-defined functor \p func is the reference
- to the list's item inserted. <tt>item.second</tt> is a reference to item's value that may be changed.
+ to the item inserted. <tt>item.second</tt> is a reference to item's value that may be changed.
User-defined functor \p func should guarantee that during changing item's value no any other changes
could be made on this list's item by concurrent threads.
- The user-defined functor can be passed by reference using \p std::ref
- and it is called only if inserting is successful.
+ The user-defined functor is called only if inserting is successful.
- The key_type should be constructible from value of type \p K.
+ The \p key_type should be constructible from value of type \p K.
The function allows to split creating of new item into two part:
- - create item from \p key;
- - insert new item into the list;
+ - create a new item from \p key;
+ - insert the new item into the list;
- if inserting is successful, initialize the value of item by calling \p func functor
This can be useful if complete initialization of object of \p mapped_type is heavyweight and
The operation performs inserting or changing data with lock-free manner.
If the \p key not found in the list, then the new item created from \p key
- is inserted into the list (note that in this case the \ref key_type should be
+ is inserted into the list (note that in this case the \p key_type should be
copy-constructible from type \p K).
Otherwise, the functor \p func is called with item found.
The functor \p Func may be a function with signature:
- \p bNew - \p true if the item has been inserted, \p false otherwise
- \p item - item of the list
- The functor may change any fields of the \p item.second that is \ref mapped_type;
+ The functor may change any fields of the \p item.second of \p mapped_type;
however, \p func must guarantee that during changing no any other modifications
could be made on this item by concurrent threads.
- You may pass \p func argument by reference using \p std::ref
-
Returns <tt> std::pair<bool, bool> </tt> where \p first is true if operation is successfull,
\p second is true if new item has been added or \p false if the item with \p key
already is in the list.
return ensure_at( head(), key, f );
}
- /// Inserts data of type \ref mapped_type constructed with <tt>std::forward<Args>(args)...</tt>
+ /// Inserts a new node using move semantics
/**
+ \p key_type field of new item is constructed from \p key argument,
+ \p mapped_type field is done from \p args.
+
Returns \p true if inserting successful, \p false otherwise.
*/
template <typename K, typename... Args>
template <typename K, typename Less>
bool erase_with( K const& key, Less pred )
{
- return erase_at( head(), key, typename options::template less_wrapper<Less>::type() );
+ return erase_at( head(), key, typename maker::template less_wrapper<Less>::type() );
}
/// Deletes \p key from the list
template <typename K, typename Less, typename Func>
bool erase_with( K const& key, Less pred, Func f )
{
- return erase_at( head(), key, typename options::template less_wrapper<Less>::type(), f );
+ return erase_at( head(), key, typename maker::template less_wrapper<Less>::type(), f );
}
/// Extracts the item from the list with specified \p key
template <typename K, typename Less>
bool extract_with( guarded_ptr& dest, K const& key, Less pred )
{
- return extract_at( head(), dest.guard(), key, typename options::template less_wrapper<Less>::type() );
+ return extract_at( head(), dest.guard(), key, typename maker::template less_wrapper<Less>::type() );
}
/// Finds the key \p key
template <typename Q, typename Less>
bool find_with( Q const& key, Less pred )
{
- return find_at( head(), key, typename options::template less_wrapper<Less>::type() );
+ return find_at( head(), key, typename maker::template less_wrapper<Less>::type() );
}
/// Finds the key \p key and performs an action with it
template <typename Q, typename Less, typename Func>
bool find_with( Q const& key, Less pred, Func f )
{
- return find_at( head(), key, typename options::template less_wrapper<Less>::type(), f );
+ return find_at( head(), key, typename maker::template less_wrapper<Less>::type(), f );
}
/// Finds the \p key and return the item found
template <typename K, typename Less>
bool get_with( guarded_ptr& ptr, K const& key, Less pred )
{
- return get_at( head(), ptr.guard(), key, typename options::template less_wrapper<Less>::type() );
+ return get_at( head(), ptr.guard(), key, typename maker::template less_wrapper<Less>::type() );
}
/// Checks if the list is empty
/// Returns list's item count
/**
- The value returned depends on opt::item_counter option. For atomicity::empty_item_counter,
+ The value returned depends on item counter provided by \p Traits. For \p atomicity::empty_item_counter,
this function always returns 0.
- <b>Warning</b>: even if you use real item counter and it returns 0, this fact is not mean that the list
- is empty. To check list emptyness use \ref empty() method.
+ @note Even if you use real item counter and it returns 0, this fact is not mean that the list
+ is empty. To check list emptyness use \p empty() method.
*/
size_t size() const
{
}
/// Clears the list
- /**
- Post-condition: the list is empty
- */
void clear()
{
base_class::clear();