From: khizmax Date: Sun, 21 Aug 2016 15:39:52 +0000 (+0300) Subject: Added MichaelMap based on IterableList X-Git-Tag: v2.2.0~139^2~2 X-Git-Url: http://plrg.eecs.uci.edu/git/?p=libcds.git;a=commitdiff_plain;h=a5b3d0d729f26fd307b3d6b8d58675af4442d859 Added MichaelMap based on IterableList --- diff --git a/cds/container/details/iterable_list_base.h b/cds/container/details/iterable_list_base.h index 2d6e9fb5..4dab48eb 100644 --- a/cds/container/details/iterable_list_base.h +++ b/cds/container/details/iterable_list_base.h @@ -174,8 +174,8 @@ namespace cds { namespace container { }; }; - template - struct is_iterable_list< IterableKVList> + template + struct is_iterable_list< IterableKVList> { enum { value = true diff --git a/cds/container/impl/iterable_kvlist.h b/cds/container/impl/iterable_kvlist.h index 92ec7728..e0a1308b 100644 --- a/cds/container/impl/iterable_kvlist.h +++ b/cds/container/impl/iterable_kvlist.h @@ -131,7 +131,7 @@ namespace cds { namespace container { typedef typename maker::mapped_type mapped_type; typedef typename maker::value_type value_type; #endif - + typedef Traits traits; ///< List traits typedef typename base_class::gc gc; ///< Garbage collector used typedef typename base_class::back_off back_off; ///< Back-off strategy used typedef typename maker::data_allocator_type allocator_type; ///< Allocator type used for allocate/deallocate data @@ -145,6 +145,22 @@ namespace cds { namespace container { /// Guarded pointer typedef typename base_class::guarded_ptr guarded_ptr; + //@cond + // Rebind traits (split-list support) + template + struct rebind_traits { + typedef IterableKVList< + gc + , key_type, mapped_type + , typename cds::opt::make_options< traits, Options...>::type + > type; + }; + + // Stat selector + template + using select_stat_wrapper = typename base_class::template select_stat_wrapper< Stat >; + //@endcond + protected: //@cond typedef typename base_class::head_type head_type; @@ -326,7 +342,7 @@ namespace cds { namespace container { The functor may change non-key fields of \p val; however, \p func must guarantee that during changing no any other modifications could be made on this item by concurrent threads. - Returns std::pair where \p first is true if operation is successful, + @return std::pair where \p first is true if operation is successful, \p second is true if new item has been added or \p false if the item with such \p key already exists. @@ -650,21 +666,21 @@ namespace cds { namespace container { // Split-list support template - bool insert_at( head_type& refHead, K const& key ) + bool insert_at( head_type& refHead, K&& key ) { - return base_class::insert_at( refHead, value_type( key_type( key ), mapped_type() )); + return base_class::insert_at( refHead, value_type( key_type( std::forward( key )), mapped_type() )); } template - bool insert_at( head_type& refHead, const K& key, V const& val ) + bool insert_at( head_type& refHead, K&& key, V&& val ) { - return base_class::insert_at( refHead, value_type( key_type( key ), val )); + return base_class::insert_at( refHead, value_type( key_type( std::forward( key )), std::forward( val ))); } template - bool insert_with_at( head_type& refHead, K const& key, Func f ) + bool insert_with_at( head_type& refHead, K&& key, Func f ) { - return base_class::insert_at( refHead, value_type( key_type( key ), mapped_type()), f ); + return base_class::insert_at( refHead, value_type( key_type( std::forward( key )), mapped_type()), f ); } template @@ -674,9 +690,9 @@ namespace cds { namespace container { } template - std::pair update_at( head_type& refHead, const K& key, Func f, bool bAllowInsert ) + std::pair update_at( head_type& refHead, K&& key, Func f, bool bAllowInsert ) { - return base_class::update_at( refHead, value_type( key_type( key ), mapped_type()), f, bAllowInsert ); + return base_class::update_at( refHead, value_type( key_type( std::forward( key )), mapped_type()), f, bAllowInsert ); } template diff --git a/cds/container/impl/lazy_kvlist.h b/cds/container/impl/lazy_kvlist.h index 5b6599c3..2dad6532 100644 --- a/cds/container/impl/lazy_kvlist.h +++ b/cds/container/impl/lazy_kvlist.h @@ -118,7 +118,8 @@ namespace cds { namespace container { //@endcond public: - typedef GC gc; ///< Garbage collector + typedef GC gc; ///< Garbage collector + typedef Traits traits; ///< Traits #ifdef CDS_DOXYGEN_INVOKED typedef Key key_type ; ///< Key type typedef Value mapped_type ; ///< Type of value stored in the list @@ -137,6 +138,22 @@ namespace cds { namespace container { static CDS_CONSTEXPR const size_t c_nHazardPtrCount = base_class::c_nHazardPtrCount; ///< Count of hazard pointer required for the algorithm + //@cond + // Rebind traits (split-list support) + template + struct rebind_traits { + typedef LazyKVList< + gc + , key_type, mapped_type + , typename cds::opt::make_options< traits, Options...>::type + > type; + }; + + // Stat selector + template + using select_stat_wrapper = typename base_class::template select_stat_wrapper< Stat >; + //@endcond + protected: //@cond typedef typename base_class::value_type node_type; @@ -397,9 +414,9 @@ namespace cds { namespace container { Returns \p true if inserting successful, \p false otherwise. */ template - bool insert( const K& key ) + bool insert( K&& key ) { - return insert_at( head(), key ); + return insert_at( head(), std::forward( key )); } /// Inserts new node with a key and a value @@ -413,12 +430,12 @@ namespace cds { namespace container { Returns \p true if inserting successful, \p false otherwise. */ template - bool insert( const K& key, const V& val ) + bool insert( K&& key, V&& val ) { // We cannot use insert with functor here // because we cannot lock inserted node for updating // Therefore, we use separate function - return insert_at( head(), key, val ); + return insert_at( head(), std::forward( key ), std::forward( val )); } /// Inserts new node and initializes it by a functor @@ -446,9 +463,9 @@ namespace cds { namespace container { it is preferable that the initialization should be completed only if inserting is successful. */ template - bool insert_with( const K& key, Func func ) + bool insert_with( K&& key, Func func ) { - return insert_with_at( head(), key, func ); + return insert_with_at( head(), std::forward( key ), func ); } /// Inserts data of type \ref mapped_type constructed with std::forward(args)... @@ -489,9 +506,9 @@ namespace cds { namespace container { already exists. */ template - std::pair update( const K& key, Func f, bool bAllowInsert = true ) + std::pair update( K&& key, Func f, bool bAllowInsert = true ) { - return update_at( head(), key, f, bAllowInsert ); + return update_at( head(), std::forward( key ), f, bAllowInsert ); } //@cond template @@ -781,21 +798,21 @@ namespace cds { namespace container { } template - bool insert_at( head_type& refHead, const K& key ) + bool insert_at( head_type& refHead, K&& key ) { - return insert_node_at( refHead, alloc_node( key )); + return insert_node_at( refHead, alloc_node( std::forward( key ))); } template - bool insert_at( head_type& refHead, const K& key, const V& val ) + bool insert_at( head_type& refHead, K&& key, V&& val ) { - return insert_node_at( refHead, alloc_node( key, val )); + return insert_node_at( refHead, alloc_node( std::forward( key ), std::forward( val ))); } template - bool insert_with_at( head_type& refHead, const K& key, Func f ) + bool insert_with_at( head_type& refHead, K&& key, Func f ) { - scoped_node_ptr pNode( alloc_node( key )); + scoped_node_ptr pNode( alloc_node( std::forward( key ))); if ( base_class::insert_at( &refHead, *pNode, [&f](node_type& node){ f( node.m_Data ); } )) { pNode.release(); @@ -829,9 +846,9 @@ namespace cds { namespace container { } template - std::pair update_at( head_type& refHead, const K& key, Func f, bool bAllowInsert ) + std::pair update_at( head_type& refHead, K&& key, Func f, bool bAllowInsert ) { - scoped_node_ptr pNode( alloc_node( key )); + scoped_node_ptr pNode( alloc_node( std::forward( key ))); std::pair ret = base_class::update_at( &refHead, *pNode, [&f]( bool bNew, node_type& node, node_type& ){ f( bNew, node.m_Data ); }, diff --git a/cds/container/impl/michael_kvlist.h b/cds/container/impl/michael_kvlist.h index 12a23426..4d444ed5 100644 --- a/cds/container/impl/michael_kvlist.h +++ b/cds/container/impl/michael_kvlist.h @@ -131,6 +131,7 @@ namespace cds { namespace container { #endif typedef typename base_class::gc gc; ///< Garbage collector used + typedef Traits traits; ///< List traits 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 @@ -140,6 +141,22 @@ namespace cds { namespace container { static CDS_CONSTEXPR const size_t c_nHazardPtrCount = base_class::c_nHazardPtrCount; ///< Count of hazard pointer required for the algorithm + //@cond + // Rebind traits (split-list support) + template + struct rebind_traits { + typedef MichaelKVList< + gc + , key_type, mapped_type + , typename cds::opt::make_options< traits, Options...>::type + > type; + }; + + // Stat selector + template + using select_stat_wrapper = typename base_class::template select_stat_wrapper< Stat >; + //@endcond + protected: //@cond typedef typename base_class::value_type node_type; @@ -386,9 +403,9 @@ namespace cds { namespace container { Returns \p true if inserting successful, \p false otherwise. */ template - bool insert( const K& key ) + bool insert( K&& key ) { - return insert_at( head(), key ); + return insert_at( head(), std::forward( key )); } /// Inserts new node with a key and a value @@ -402,12 +419,12 @@ namespace cds { namespace container { Returns \p true if inserting successful, \p false otherwise. */ template - bool insert( const K& key, const V& val ) + bool insert( K&& key, V&& val ) { // We cannot use insert with functor here // because we cannot lock inserted node for updating // Therefore, we use separate function - return insert_at( head(), key, val ); + return insert_at( head(), std::forward( key ), std::forward( val )); } /// Inserts new node and initialize it by a functor @@ -439,9 +456,9 @@ namespace cds { namespace container { @warning See \ref cds_intrusive_item_creating "insert item troubleshooting" */ template - bool insert_with( const K& key, Func func ) + bool insert_with( K&& key, Func func ) { - return insert_with_at( head(), key, func ); + return insert_with_at( head(), std::forward( key ), func ); } /// Updates data by \p key @@ -474,9 +491,9 @@ namespace cds { namespace container { @warning See \ref cds_intrusive_item_creating "insert item troubleshooting" */ template - std::pair update( K const& key, Func f, bool bAllowInsert = true ) + std::pair update( K&& key, Func f, bool bAllowInsert = true ) { - return update_at( head(), key, f, bAllowInsert ); + return update_at( head(), std::forward( key ), f, bAllowInsert ); } //@cond template @@ -783,21 +800,21 @@ namespace cds { namespace container { } template - bool insert_at( head_type& refHead, const K& key ) + bool insert_at( head_type& refHead, K&& key ) { - return insert_node_at( refHead, alloc_node( key )); + return insert_node_at( refHead, alloc_node( std::forward( key ))); } template - bool insert_at( head_type& refHead, const K& key, const V& val ) + bool insert_at( head_type& refHead, K&& key, V&& val ) { - return insert_node_at( refHead, alloc_node( key, val )); + return insert_node_at( refHead, alloc_node( std::forward( key ), std::forward( val ))); } template - bool insert_with_at( head_type& refHead, const K& key, Func f ) + bool insert_with_at( head_type& refHead, K&& key, Func f ) { - scoped_node_ptr pNode( alloc_node( key )); + scoped_node_ptr pNode( alloc_node( std::forward( key ))); if ( base_class::insert_at( refHead, *pNode, [&f](node_type& node){ f( node.m_Data ); })) { pNode.release(); @@ -813,9 +830,9 @@ namespace cds { namespace container { } template - std::pair update_at( head_type& refHead, const K& key, Func f, bool bAllowInsert ) + std::pair update_at( head_type& refHead, K&& key, Func f, bool bAllowInsert ) { - scoped_node_ptr pNode( alloc_node( key )); + scoped_node_ptr pNode( alloc_node( std::forward( key ))); std::pair ret = base_class::update_at( refHead, *pNode, [&f]( bool bNew, node_type& node, node_type& ){ f( bNew, node.m_Data ); }, diff --git a/cds/container/michael_map.h b/cds/container/michael_map.h index 38bce26d..123ef869 100644 --- a/cds/container/michael_map.h +++ b/cds/container/michael_map.h @@ -32,6 +32,7 @@ #define CDSLIB_CONTAINER_MICHAEL_MAP_H #include +#include #include namespace cds { namespace container { @@ -52,10 +53,10 @@ namespace cds { namespace container { - \p GC - Garbage collector used. You may use any \ref cds_garbage_collector "Garbage collector" from the \p libcds library. Note the \p GC must be the same as the GC used for \p OrderedList - - \p OrderedList - ordered key-value list implementation used as bucket for hash map, for example, \p MichaelKVList - or \p LazyKVList. The ordered list implementation specifies the \p Key and \p Value types stored in the hash-map, - the reclamation schema \p GC used by hash-map, the comparison functor for the type \p Key and other features - specific for the ordered list. + - \p OrderedList - ordered key-value list implementation used as bucket for hash map, for example, \p MichaelKVList, + \p LazyKVList, \p IterableKVList. The ordered list implementation specifies the \p Key and \p Value types + stored in the hash-map, the reclamation schema \p GC used by hash-map, the comparison functor for the type \p Key + and other features specific for the ordered list. - \p Traits - map traits, default is \p michael_map::traits. Instead of defining \p Traits struct you may use option-based syntax with \p michael_map::make_traits metafunction. @@ -147,60 +148,72 @@ namespace cds { namespace container { class MichaelHashMap { public: - typedef GC gc; ///< Garbage collector - typedef OrderedList bucket_type; ///< type of ordered list to be used as a bucket - typedef Traits traits; ///< Map traits + typedef GC gc; ///< Garbage collector + typedef OrderedList ordered_list; ///< type of ordered list to be used as a bucket + typedef Traits traits; ///< Map traits - typedef typename bucket_type::key_type key_type; ///< key type - typedef typename bucket_type::mapped_type mapped_type; ///< value type - typedef typename bucket_type::value_type value_type; ///< key/value pair stored in the map + typedef typename ordered_list::key_type key_type; ///< key type + typedef typename ordered_list::mapped_type mapped_type; ///< value type + typedef typename ordered_list::value_type value_type; ///< key/value pair stored in the map + typedef typename traits::allocator allocator; ///< Bucket table allocator - typedef typename bucket_type::key_comparator key_comparator; ///< key compare functor + typedef typename ordered_list::key_comparator key_comparator; ///< key compare functor + typedef typename ordered_list::stat stat; ///< Internal statistics /// Hash functor for \ref key_type and all its derivatives that you use typedef typename cds::opt::v::hash_selector< typename traits::hash >::type hash; typedef typename traits::item_counter item_counter; ///< Item counter type - /// Bucket table allocator - typedef cds::details::Allocator< bucket_type, typename traits::allocator > bucket_table_allocator; - typedef typename bucket_type::guarded_ptr guarded_ptr; ///< Guarded pointer + // GC and OrderedList::gc must be the same + static_assert( std::is_same::value, "GC and OrderedList::gc must be the same"); - static CDS_CONSTEXPR const size_t c_nHazardPtrCount = bucket_type::c_nHazardPtrCount; ///< Count of hazard pointer required + // atomicity::empty_item_counter is not allowed as a item counter + static_assert( !std::is_same::value, + "atomicity::empty_item_counter is not allowed as a item counter"); - protected: - item_counter m_ItemCounter; ///< Item counter - hash m_HashFunctor; ///< Hash functor - bucket_type * m_Buckets; ///< bucket table + static CDS_CONSTEXPR const size_t c_nHazardPtrCount = ordered_list::c_nHazardPtrCount; ///< Count of hazard pointer required + +#ifdef CDS_DOXYGEN_INVOKED + /// Wrapped internal statistics for \p ordered_list + typedef implementatin_specific bucket_stat; +#else + typedef typename ordered_list::template select_stat_wrapper< typename ordered_list::stat > bucket_stat; +#endif + +#ifdef CDS_DOXYGEN_INVOKED + /// Internal bucket type - rebind \p ordered_list with empty item counter and wrapped internal statistics + typedef modified_ordered_list internal_bucket_type; +#else + typedef typename ordered_list::template rebind_traits< + cds::opt::item_counter< cds::atomicity::empty_item_counter > + , cds::opt::stat< typename bucket_stat::wrapped_stat > + >::type internal_bucket_type; +#endif + + /// Guarded pointer - a result of \p get() and \p extract() functions + typedef typename internal_bucket_type::guarded_ptr guarded_ptr; - private: //@cond - const size_t m_nHashBitmask; + /// Bucket table allocator + typedef typename allocator::template rebind< internal_bucket_type >::other bucket_table_allocator; //@endcond protected: //@cond - /// Calculates hash value of \p key - template - size_t hash_value( Q const& key ) const - { - return m_HashFunctor( key ) & m_nHashBitmask; - } - - /// Returns the bucket (ordered list) for \p key - template - bucket_type& bucket( Q const& key ) - { - return m_Buckets[ hash_value( key ) ]; - } + const size_t m_nHashBitmask; + internal_bucket_type * m_Buckets; ///< bucket table + item_counter m_ItemCounter; ///< Item counter + hash m_HashFunctor; ///< Hash functor + typename bucket_stat::stat m_Stat; ///< Internal statistics //@endcond protected: //@cond /// Forward iterator template - class iterator_type: private cds::intrusive::michael_set::details::iterator< bucket_type, IsConst > + class iterator_type: private cds::intrusive::michael_set::details::iterator< internal_bucket_type, IsConst > { - typedef cds::intrusive::michael_set::details::iterator< bucket_type, IsConst > base_class; + typedef cds::intrusive::michael_set::details::iterator< internal_bucket_type, IsConst > base_class; friend class MichaelHashMap; protected: @@ -288,38 +301,44 @@ namespace cds { namespace container { //@{ /// Forward iterator /** - The iteration is unordered. - The iterator object is thread-safe: the element pointed by the iterator object is guarded, - so, the element cannot be reclaimed while the iterator object is alive. - However, passing an iterator object between threads is dangerous. - - @warning Due to concurrent nature of Michael's map it is not guarantee that you can iterate - all elements in the map: any concurrent deletion can exclude the element - pointed by the iterator from the map, and your iteration can be terminated - before end of the map. Therefore, such iteration is more suitable for debugging purpose only. - - Remember, each iterator object requires an additional hazard pointer, that may be - a limited resource for \p GC like \p gc::HP (for \p gc::DHP the count of - guards is unlimited). - - The iterator class supports the following minimalistic interface: + The forward iterator for Michael's map has some features: + - it has no post-increment operator + - to protect the value, the iterator contains a GC-specific guard + another guard is required locally for increment operator. + For some GC (like as \p gc::HP), a guard is a limited resource per thread, so an exception (or assertion) "no free guard" + may be thrown if the limit of guard count per thread is exceeded. + - The iterator cannot be moved across thread boundary because it contains thread-private GC's guard. + + Iterator thread safety depends on type of \p OrderedList: + - for \p MichaelKVList and \p LazyKVList: iterator guarantees safety even if you delete the item that iterator points to + because that item is guarded by hazard pointer. + However, in case of concurrent deleting operations it is no guarantee that you iterate all item in the map. + Moreover, a crash is possible when you try to iterate the next element that has been deleted by concurrent thread. + Use this iterator on the concurrent container for debugging purpose only. + - for \p IterableList: iterator is thread-safe. You may use it freely in concurrent environment. + + The iterator interface: \code - struct iterator { - // Default ctor + class iterator { + public: + // Default constructor iterator(); - // Copy ctor - iterator( iterator const& s); + // Copy construtor + iterator( iterator const& src ); + // Dereference operator value_type * operator ->() const; + + // Dereference operator value_type& operator *() const; - // Pre-increment + // Preincrement operator iterator& operator ++(); - // Copy assignment + // Assignment operator iterator& operator = (iterator const& src); + // Equality operators bool operator ==(iterator const& i ) const; bool operator !=(iterator const& i ) const; }; @@ -338,7 +357,7 @@ namespace cds { namespace container { */ iterator begin() { - return iterator( m_Buckets[0].begin(), m_Buckets, m_Buckets + bucket_count() ); + return iterator( bucket_begin()->begin(), bucket_begin(), bucket_end() ); } /// Returns an iterator that addresses the location succeeding the last element in a map @@ -349,7 +368,7 @@ namespace cds { namespace container { */ iterator end() { - return iterator( m_Buckets[bucket_count() - 1].end(), m_Buckets + bucket_count() - 1, m_Buckets + bucket_count() ); + return iterator( bucket_end()[-1].end(), bucket_end() - 1, bucket_end() ); } /// Returns a forward const iterator addressing the first element in a map @@ -375,18 +394,6 @@ namespace cds { namespace container { } //@} - private: - //@cond - const_iterator get_const_begin() const - { - return const_iterator( const_cast(m_Buckets[0]).begin(), m_Buckets, m_Buckets + bucket_count() ); - } - const_iterator get_const_end() const - { - return const_iterator( const_cast(m_Buckets[bucket_count() - 1]).end(), m_Buckets + bucket_count() - 1, m_Buckets + bucket_count() ); - } - //@endcond - public: /// Initializes the map /** @anchor cds_nonintrusive_MichaelHashMap_hp_ctor @@ -401,23 +408,22 @@ namespace cds { namespace container { MichaelHashMap( size_t nMaxItemCount, ///< estimation of max item count in the hash map size_t nLoadFactor ///< load factor: estimation of max number of items in the bucket - ) : m_nHashBitmask( michael_map::details::init_hash_bitmask( nMaxItemCount, nLoadFactor )) + ) + : m_nHashBitmask( michael_map::details::init_hash_bitmask( nMaxItemCount, nLoadFactor )) + , m_Buckets( bucket_table_allocator().allocate( bucket_count() ) ) { - // GC and OrderedList::gc must be the same - static_assert( std::is_same::value, "GC and OrderedList::gc must be the same"); - - // atomicity::empty_item_counter is not allowed as a item counter - static_assert( !std::is_same::value, - "atomicity::empty_item_counter is not allowed as a item counter"); - - m_Buckets = bucket_table_allocator().NewArray( bucket_count() ); + for ( auto it = m_Buckets, itEnd = m_Buckets + bucket_count(); it != itEnd; ++it ) + construct_bucket( it ); } /// Clears hash map and destroys it ~MichaelHashMap() { clear(); - bucket_table_allocator().Delete( m_Buckets, bucket_count() ); + + for ( auto it = m_Buckets, itEnd = m_Buckets + bucket_count(); it != itEnd; ++it ) + it->~internal_bucket_type(); + bucket_table_allocator().deallocate( m_Buckets, bucket_count() ); } /// Inserts new node with key and default value @@ -432,9 +438,9 @@ namespace cds { namespace container { Returns \p true if inserting successful, \p false otherwise. */ template - bool insert( const K& key ) + bool insert( K&& key ) { - const bool bRet = bucket( key ).insert( key ); + const bool bRet = bucket( key ).insert( std::forward( key )); if ( bRet ) ++m_ItemCounter; return bRet; @@ -452,9 +458,9 @@ namespace cds { namespace container { Returns \p true if \p val is inserted into the map, \p false otherwise. */ template - bool insert( K const& key, V const& val ) + bool insert( K&& key, V&& val ) { - const bool bRet = bucket( key ).insert( key, val ); + const bool bRet = bucket( key ).insert( std::forward( key ), std::forward( val )); if ( bRet ) ++m_ItemCounter; return bRet; @@ -492,9 +498,9 @@ namespace cds { namespace container { synchronization. */ template - bool insert_with( const K& key, Func func ) + bool insert_with( K&& key, Func func ) { - const bool bRet = bucket( key ).insert_with( key, func ); + const bool bRet = bucket( key ).insert_with( std::forward( key ), func ); if ( bRet ) ++m_ItemCounter; return bRet; @@ -509,7 +515,9 @@ namespace cds { namespace container { (note that in this case the \ref key_type should be constructible from type \p K). Otherwise, if \p key is found, the functor \p func is called with item found. - The functor \p Func signature is: + The functor \p func signature depends of \p OrderedList: + + for \p MichaelKVList, \p LazyKVList \code struct my_functor { void operator()( bool bNew, value_type& item ); @@ -521,18 +529,30 @@ namespace cds { namespace container { The functor may change any fields of the \p item.second that is \p mapped_type. - Returns std::pair where \p first is true if operation is successful, + for \p IterableKVList + \code + void func( value_type& val, value_type * old ); + \endcode + where + - \p val - a new data constructed from \p key + - \p old - old value that will be retired. If new item has been inserted then \p old is \p nullptr. + + The functor may change non-key fields of \p val; however, \p func must guarantee + that during changing no any other modifications could be made on this item by concurrent threads. + + @return std::pair where \p first is true if operation is successful, \p second is true if new item has been added or \p false if the item with \p key already exists. - @warning For \ref cds_nonintrusive_MichaelKVList_gc "MichaelKVList" as the bucket see \ref cds_intrusive_item_creating "insert item troubleshooting". + @warning For \ref cds_nonintrusive_MichaelKVList_gc "MichaelKVList" and \ref cds_nonintrusive_IterableKVList_gc "IterableKVList" + as the bucket see \ref cds_intrusive_item_creating "insert item troubleshooting". \ref cds_nonintrusive_LazyKVList_gc "LazyKVList" provides exclusive access to inserted item and does not require any node-level synchronization. */ template - std::pair update( K const& key, Func func, bool bAllowInsert = true ) + std::pair update( K&& key, Func func, bool bAllowInsert = true ) { - std::pair bRet = bucket( key ).update( key, func, bAllowInsert ); + std::pair bRet = bucket( key ).update( std::forward( key ), func, bAllowInsert ); if ( bRet.first && bRet.second ) ++m_ItemCounter; return bRet; @@ -549,6 +569,34 @@ namespace cds { namespace container { } //@endcond + /// Inserts or updates the node (only for \p IterableKVList) + /** + The operation performs inserting or changing data with lock-free manner. + + If the item \p val is not found in the map, then \p val is inserted iff \p bAllowInsert is \p true. + Otherwise, the current element is changed to \p val, the old element will be retired later. + + Returns std::pair where \p first is \p true if operation is successful, + \p second is \p true if \p val has been added or \p false if the item with that key + already in the map. + */ + template +#ifdef CDS_DOXYGEN_INVOKED + std::pair +#else + typename std::enable_if< + std::is_same< Q, Q>::value && is_iterable_list< ordered_list >::value, + std::pair + >::type +#endif + upsert( Q&& key, V&& val, bool bAllowInsert = true ) + { + std::pair bRet = bucket( val ).upsert( std::forward( key ), std::forward( val ), bAllowInsert ); + if ( bRet.second ) + ++m_ItemCounter; + return bRet; + } + /// For key \p key inserts data of type \p mapped_type created from \p args /** \p key_type should be constructible from type \p K @@ -845,6 +893,63 @@ namespace cds { namespace container { { return m_nHashBitmask + 1; } + + /// Returns const reference to internal statistics + stat const& statistics() const + { + return m_Stat; + } + + protected: + //@cond + /// Calculates hash value of \p key + template + size_t hash_value( Q const& key ) const + { + return m_HashFunctor( key ) & m_nHashBitmask; + } + + /// Returns the bucket (ordered list) for \p key + template + internal_bucket_type& bucket( Q const& key ) + { + return m_Buckets[hash_value( key )]; + } + //@endcond + + private: + //@cond + internal_bucket_type* bucket_begin() const + { + return m_Buckets; + } + + internal_bucket_type* bucket_end() const + { + return m_Buckets + bucket_count(); + } + + const_iterator get_const_begin() const + { + return const_iterator( bucket_begin()->cbegin(), bucket_begin(), bucket_end() ); + } + const_iterator get_const_end() const + { + return const_iterator( (bucket_end() - 1)->cend(), bucket_end() - 1, bucket_end() ); + } + + template + typename std::enable_if< Stat::empty >::type construct_bucket( internal_bucket_type* bucket ) + { + new (bucket) internal_bucket_type; + } + + template + typename std::enable_if< !Stat::empty >::type construct_bucket( internal_bucket_type* bucket ) + { + new (bucket) internal_bucket_type( m_Stat ); + } + //@endcond }; }} // namespace cds::container diff --git a/cds/container/michael_set.h b/cds/container/michael_set.h index bff19031..c324710b 100644 --- a/cds/container/michael_set.h +++ b/cds/container/michael_set.h @@ -463,8 +463,7 @@ namespace cds { namespace container { The operation performs inserting or changing data with lock-free manner. If the item \p val is not found in the set, then \p val is inserted iff \p bAllowInsert is \p true. - Otherwise, the current element is changed to \p val, the old element will be retired later - by call \p Traits::disposer. + Otherwise, the current element is changed to \p val, the old element will be retired later. Returns std::pair where \p first is \p true if operation is successful, \p second is \p true if \p val has been added or \p false if the item with that key diff --git a/projects/Win/vc14/gtest-map.vcxproj b/projects/Win/vc14/gtest-map.vcxproj index d81d5d90..0a081884 100644 --- a/projects/Win/vc14/gtest-map.vcxproj +++ b/projects/Win/vc14/gtest-map.vcxproj @@ -35,6 +35,8 @@ + + @@ -204,6 +206,8 @@ + + diff --git a/projects/Win/vc14/gtest-map.vcxproj.filters b/projects/Win/vc14/gtest-map.vcxproj.filters index 46d394f8..e25cb3a7 100644 --- a/projects/Win/vc14/gtest-map.vcxproj.filters +++ b/projects/Win/vc14/gtest-map.vcxproj.filters @@ -171,6 +171,12 @@ Source Files\FeldmanHashMap + + Source Files\MichaelMap + + + Source Files\MichaelMap + @@ -215,5 +221,11 @@ Header Files + + Header Files + + + Header Files + \ No newline at end of file diff --git a/test/unit/map/CMakeLists.txt b/test/unit/map/CMakeLists.txt index d4a4565a..b8a09ccd 100644 --- a/test/unit/map/CMakeLists.txt +++ b/test/unit/map/CMakeLists.txt @@ -9,6 +9,8 @@ set(CDSGTEST_MAP_SOURCES feldman_hashset_rcu_gpt.cpp feldman_hashset_rcu_shb.cpp feldman_hashset_rcu_sht.cpp + michael_iterable_hp.cpp + michael_iterable_dhp.cpp michael_lazy_hp.cpp michael_lazy_dhp.cpp michael_lazy_nogc.cpp diff --git a/test/unit/map/michael_iterable_dhp.cpp b/test/unit/map/michael_iterable_dhp.cpp new file mode 100644 index 00000000..70637b1a --- /dev/null +++ b/test/unit/map/michael_iterable_dhp.cpp @@ -0,0 +1,197 @@ +/* + This file is a part of libcds - Concurrent Data Structures library + + (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016 + + 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. +*/ + +#include "test_michael_iterable_hp.h" + +#include +#include + +namespace { + + namespace cc = cds::container; + typedef cds::gc::DHP gc_type; + + class MichaelIterableMap_DHP: public cds_test::michael_iterable_hp + { + protected: + typedef cds_test::michael_iterable_hp base_class; + + void SetUp() + { + typedef cc::IterableKVList< gc_type, key_type, value_type > list_type; + typedef cc::MichaelHashMap< gc_type, list_type > map_type; + + cds::gc::dhp::GarbageCollector::Construct( 16, map_type::c_nHazardPtrCount ); + cds::threading::Manager::attachThread(); + } + + void TearDown() + { + cds::threading::Manager::detachThread(); + cds::gc::dhp::GarbageCollector::Destruct(); + } + }; + + TEST_F( MichaelIterableMap_DHP, compare ) + { + typedef cc::IterableKVList< gc_type, key_type, value_type, + typename cc::iterable_list::make_traits< + cds::opt::compare< cmp > + >::type + > list_type; + + typedef cc::MichaelHashMap< gc_type, list_type, + typename cc::michael_map::make_traits< + cds::opt::hash< hash1 > + >::type + > map_type; + + map_type m( kSize, 2 ); + test( m ); + } + + TEST_F( MichaelIterableMap_DHP, less ) + { + typedef cc::IterableKVList< gc_type, key_type, value_type, + typename cc::iterable_list::make_traits< + cds::opt::less< less > + >::type + > list_type; + + typedef cc::MichaelHashMap< gc_type, list_type, + typename cc::michael_map::make_traits< + cds::opt::hash< hash1 > + >::type + > map_type; + + map_type m( kSize, 1 ); + test( m ); + } + + TEST_F( MichaelIterableMap_DHP, cmpmix ) + { + typedef cc::IterableKVList< gc_type, key_type, value_type, + typename cc::iterable_list::make_traits< + cds::opt::less< less > + ,cds::opt::compare< cmp > + >::type + > list_type; + + typedef cc::MichaelHashMap< gc_type, list_type, + typename cc::michael_map::make_traits< + cds::opt::hash< hash1 > + >::type + > map_type; + + map_type m( kSize, 2 ); + test( m ); + } + + TEST_F( MichaelIterableMap_DHP, backoff ) + { + struct list_traits: public cc::iterable_list::traits + { + typedef cmp compare; + typedef cds::backoff::exponential back_off; + }; + typedef cc::IterableKVList< gc_type, key_type, value_type, list_traits > list_type; + + struct map_traits: public cc::michael_map::traits + { + typedef hash1 hash; + typedef cds::atomicity::item_counter item_counter; + }; + typedef cc::MichaelHashMap< gc_type, list_type, map_traits > map_type; + + map_type m( kSize, 4 ); + test( m ); + } + + TEST_F( MichaelIterableMap_DHP, seq_cst ) + { + struct list_traits: public cc::iterable_list::traits + { + typedef cmp compare; + typedef cds::backoff::yield back_off; + typedef cds::opt::v::sequential_consistent memory_model; + }; + typedef cc::IterableKVList< gc_type, key_type, value_type, list_traits > list_type; + + struct map_traits: public cc::michael_map::traits + { + typedef hash1 hash; + }; + typedef cc::MichaelHashMap< gc_type, list_type, map_traits > map_type; + + map_type s( kSize, 8 ); + test( s ); + } + + TEST_F( MichaelIterableMap_DHP, stat ) + { + struct list_traits: public cc::iterable_list::traits + { + typedef cmp compare; + typedef cds::backoff::yield back_off; + typedef cc::iterable_list::stat<> stat; + }; + typedef cc::IterableKVList< gc_type, key_type, value_type, list_traits > list_type; + + struct map_traits: public cc::michael_map::traits + { + typedef hash1 hash; + }; + typedef cc::MichaelHashMap< gc_type, list_type, map_traits > map_type; + + map_type s( kSize, 8 ); + test( s ); + } + + TEST_F( MichaelIterableMap_DHP, wrapped_stat ) + { + struct list_traits: public cc::iterable_list::traits + { + typedef cmp compare; + typedef cc::iterable_list::wrapped_stat<> stat; + }; + typedef cc::IterableKVList< gc_type, key_type, value_type, list_traits > list_type; + + struct map_traits: public cc::michael_map::traits + { + typedef hash1 hash; + }; + typedef cc::MichaelHashMap< gc_type, list_type, map_traits > map_type; + + map_type s( kSize, 8 ); + test( s ); + } + +} // namespace + diff --git a/test/unit/map/michael_iterable_hp.cpp b/test/unit/map/michael_iterable_hp.cpp new file mode 100644 index 00000000..1f0f0339 --- /dev/null +++ b/test/unit/map/michael_iterable_hp.cpp @@ -0,0 +1,198 @@ +/* + This file is a part of libcds - Concurrent Data Structures library + + (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016 + + 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. +*/ + +#include "test_michael_iterable_hp.h" + +#include +#include + +namespace { + + namespace cc = cds::container; + typedef cds::gc::HP gc_type; + + class MichaelIterableMap_HP: public cds_test::michael_iterable_hp + { + protected: + typedef cds_test::michael_iterable_hp base_class; + + void SetUp() + { + typedef cc::IterableKVList< gc_type, key_type, value_type > list_type; + typedef cc::MichaelHashMap< gc_type, list_type > map_type; + + // +3 - for guarded_ptr and iterator + cds::gc::hp::GarbageCollector::Construct( map_type::c_nHazardPtrCount + 3, 1, 16 ); + cds::threading::Manager::attachThread(); + } + + void TearDown() + { + cds::threading::Manager::detachThread(); + cds::gc::hp::GarbageCollector::Destruct( true ); + } + }; + + TEST_F( MichaelIterableMap_HP, compare ) + { + typedef cc::IterableKVList< gc_type, key_type, value_type, + typename cc::iterable_list::make_traits< + cds::opt::compare< cmp > + >::type + > list_type; + + typedef cc::MichaelHashMap< gc_type, list_type, + typename cc::michael_map::make_traits< + cds::opt::hash< hash1 > + >::type + > map_type; + + map_type m( kSize, 2 ); + test( m ); + } + + TEST_F( MichaelIterableMap_HP, less ) + { + typedef cc::IterableKVList< gc_type, key_type, value_type, + typename cc::iterable_list::make_traits< + cds::opt::less< less > + >::type + > list_type; + + typedef cc::MichaelHashMap< gc_type, list_type, + typename cc::michael_map::make_traits< + cds::opt::hash< hash1 > + >::type + > map_type; + + map_type m( kSize, 1 ); + test( m ); + } + + TEST_F( MichaelIterableMap_HP, cmpmix ) + { + typedef cc::IterableKVList< gc_type, key_type, value_type, + typename cc::iterable_list::make_traits< + cds::opt::less< less > + ,cds::opt::compare< cmp > + >::type + > list_type; + + typedef cc::MichaelHashMap< gc_type, list_type, + typename cc::michael_map::make_traits< + cds::opt::hash< hash1 > + >::type + > map_type; + + map_type m( kSize, 2 ); + test( m ); + } + + TEST_F( MichaelIterableMap_HP, backoff ) + { + struct list_traits: public cc::iterable_list::traits + { + typedef cmp compare; + typedef cds::backoff::exponential back_off; + }; + typedef cc::IterableKVList< gc_type, key_type, value_type, list_traits > list_type; + + struct map_traits: public cc::michael_map::traits + { + typedef hash1 hash; + typedef cds::atomicity::item_counter item_counter; + }; + typedef cc::MichaelHashMap< gc_type, list_type, map_traits > map_type; + + map_type m( kSize, 4 ); + test( m ); + } + + TEST_F( MichaelIterableMap_HP, seq_cst ) + { + struct list_traits: public cc::iterable_list::traits + { + typedef cmp compare; + typedef cds::backoff::yield back_off; + typedef cds::opt::v::sequential_consistent memory_model; + }; + typedef cc::IterableKVList< gc_type, key_type, value_type, list_traits > list_type; + + struct map_traits: public cc::michael_map::traits + { + typedef hash1 hash; + }; + typedef cc::MichaelHashMap< gc_type, list_type, map_traits > map_type; + + map_type s( kSize, 8 ); + test( s ); + } + + TEST_F( MichaelIterableMap_HP, stat ) + { + struct list_traits: public cc::iterable_list::traits + { + typedef cmp compare; + typedef cds::backoff::yield back_off; + typedef cc::iterable_list::stat<> stat; + }; + typedef cc::IterableKVList< gc_type, key_type, value_type, list_traits > list_type; + + struct map_traits: public cc::michael_map::traits + { + typedef hash1 hash; + }; + typedef cc::MichaelHashMap< gc_type, list_type, map_traits > map_type; + + map_type s( kSize, 8 ); + test( s ); + } + + TEST_F( MichaelIterableMap_HP, wrapped_stat ) + { + struct list_traits: public cc::iterable_list::traits + { + typedef cmp compare; + typedef cc::iterable_list::wrapped_stat<> stat; + }; + typedef cc::IterableKVList< gc_type, key_type, value_type, list_traits > list_type; + + struct map_traits: public cc::michael_map::traits + { + typedef hash1 hash; + }; + typedef cc::MichaelHashMap< gc_type, list_type, map_traits > map_type; + + map_type s( kSize, 8 ); + test( s ); + } + +} // namespace + diff --git a/test/unit/map/michael_lazy_dhp.cpp b/test/unit/map/michael_lazy_dhp.cpp index 02b535e8..27968f5c 100644 --- a/test/unit/map/michael_lazy_dhp.cpp +++ b/test/unit/map/michael_lazy_dhp.cpp @@ -25,7 +25,7 @@ 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. + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "test_map_hp.h" @@ -174,5 +174,45 @@ namespace { test( m ); } + TEST_F( MichaelLazyMap_DHP, stat ) + { + struct list_traits: public cc::lazy_list::traits + { + typedef cmp compare; + typedef cds::backoff::yield back_off; + typedef cc::lazy_list::stat<> stat; + }; + typedef cc::LazyKVList< gc_type, key_type, value_type, list_traits > list_type; + + struct map_traits: public cc::michael_map::traits + { + typedef hash1 hash; + }; + typedef cc::MichaelHashMap< gc_type, list_type, map_traits > map_type; + + map_type m( kSize, 2 ); + test( m ); + } + + TEST_F( MichaelLazyMap_DHP, wrapped_stat ) + { + struct list_traits: public cc::lazy_list::traits + { + typedef cmp compare; + typedef cds::backoff::yield back_off; + typedef cc::lazy_list::wrapped_stat<> stat; + }; + typedef cc::LazyKVList< gc_type, key_type, value_type, list_traits > list_type; + + struct map_traits: public cc::michael_map::traits + { + typedef hash1 hash; + }; + typedef cc::MichaelHashMap< gc_type, list_type, map_traits > map_type; + + map_type m( kSize, 2 ); + test( m ); + } + } // namespace diff --git a/test/unit/map/michael_lazy_hp.cpp b/test/unit/map/michael_lazy_hp.cpp index 5376a2e8..209fc27b 100644 --- a/test/unit/map/michael_lazy_hp.cpp +++ b/test/unit/map/michael_lazy_hp.cpp @@ -25,7 +25,7 @@ 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. + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "test_map_hp.h" @@ -175,5 +175,45 @@ namespace { test( m ); } + TEST_F( MichaelLazyMap_HP, stat ) + { + struct list_traits: public cc::lazy_list::traits + { + typedef cmp compare; + typedef cds::backoff::yield back_off; + typedef cc::lazy_list::stat<> stat; + }; + typedef cc::LazyKVList< gc_type, key_type, value_type, list_traits > list_type; + + struct map_traits: public cc::michael_map::traits + { + typedef hash1 hash; + }; + typedef cc::MichaelHashMap< gc_type, list_type, map_traits > map_type; + + map_type m( kSize, 2 ); + test( m ); + } + + TEST_F( MichaelLazyMap_HP, wrapped_stat ) + { + struct list_traits: public cc::lazy_list::traits + { + typedef cmp compare; + typedef cds::backoff::yield back_off; + typedef cc::lazy_list::wrapped_stat<> stat; + }; + typedef cc::LazyKVList< gc_type, key_type, value_type, list_traits > list_type; + + struct map_traits: public cc::michael_map::traits + { + typedef hash1 hash; + }; + typedef cc::MichaelHashMap< gc_type, list_type, map_traits > map_type; + + map_type m( kSize, 2 ); + test( m ); + } + } // namespace diff --git a/test/unit/map/michael_michael_dhp.cpp b/test/unit/map/michael_michael_dhp.cpp index 94162358..a1cad613 100644 --- a/test/unit/map/michael_michael_dhp.cpp +++ b/test/unit/map/michael_michael_dhp.cpp @@ -25,7 +25,7 @@ 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. + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "test_map_hp.h" @@ -154,5 +154,44 @@ namespace { test( s ); } + TEST_F( MichaelMap_DHP, stat ) + { + struct list_traits: public cc::michael_list::traits + { + typedef cmp compare; + typedef cds::backoff::yield back_off; + typedef cc::michael_list::stat<> stat; + }; + typedef cc::MichaelKVList< gc_type, key_type, value_type, list_traits > list_type; + + struct map_traits: public cc::michael_map::traits + { + typedef hash1 hash; + }; + typedef cc::MichaelHashMap< gc_type, list_type, map_traits > map_type; + + map_type s( kSize, 8 ); + test( s ); + } + + TEST_F( MichaelMap_DHP, wrapped_stat ) + { + struct list_traits: public cc::michael_list::traits + { + typedef cmp compare; + typedef cc::michael_list::wrapped_stat<> stat; + }; + typedef cc::MichaelKVList< gc_type, key_type, value_type, list_traits > list_type; + + struct map_traits: public cc::michael_map::traits + { + typedef hash1 hash; + }; + typedef cc::MichaelHashMap< gc_type, list_type, map_traits > map_type; + + map_type s( kSize, 8 ); + test( s ); + } + } // namespace diff --git a/test/unit/map/michael_michael_hp.cpp b/test/unit/map/michael_michael_hp.cpp index b69aa597..6b48b784 100644 --- a/test/unit/map/michael_michael_hp.cpp +++ b/test/unit/map/michael_michael_hp.cpp @@ -25,7 +25,7 @@ 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. + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "test_map_hp.h" @@ -155,5 +155,45 @@ namespace { test( s ); } + TEST_F( MichaelMap_HP, stat ) + { + struct list_traits: public cc::michael_list::traits + { + typedef cmp compare; + typedef cds::backoff::yield back_off; + typedef cc::michael_list::stat<> stat; + }; + typedef cc::MichaelKVList< gc_type, key_type, value_type, list_traits > list_type; + + struct map_traits: public cc::michael_map::traits + { + typedef hash1 hash; + }; + typedef cc::MichaelHashMap< gc_type, list_type, map_traits > map_type; + + map_type s( kSize, 8 ); + test( s ); + } + + TEST_F( MichaelMap_HP, wrapped_stat ) + { + struct list_traits: public cc::michael_list::traits + { + typedef cmp compare; + typedef cds::backoff::yield back_off; + typedef cc::michael_list::wrapped_stat<> stat; + }; + typedef cc::MichaelKVList< gc_type, key_type, value_type, list_traits > list_type; + + struct map_traits: public cc::michael_map::traits + { + typedef hash1 hash; + }; + typedef cc::MichaelHashMap< gc_type, list_type, map_traits > map_type; + + map_type s( kSize, 8 ); + test( s ); + } + } // namespace diff --git a/test/unit/map/test_map.h b/test/unit/map/test_map.h index 2c3912c2..5aee1d10 100644 --- a/test/unit/map/test_map.h +++ b/test/unit/map/test_map.h @@ -25,7 +25,7 @@ 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. + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef CDSUNIT_MAP_TEST_MAP_H @@ -50,8 +50,8 @@ namespace cds_test { // Precondition: map is empty // Postcondition: map is empty - ASSERT_TRUE( m.empty()); - ASSERT_CONTAINER_SIZE( m, 0 ); + EXPECT_TRUE( m.empty()); + EXPECT_CONTAINER_SIZE( m, 0 ); typedef typename Map::value_type map_pair; size_t const kkSize = kSize; @@ -73,16 +73,16 @@ namespace cds_test { for ( auto const& i : arrKeys ) { value_type const& val( arrVals.at( i.nKey )); - ASSERT_FALSE( m.contains( i.nKey )); - ASSERT_FALSE( m.contains( i )); - ASSERT_FALSE( m.contains( other_item( i.nKey ), other_less())); - ASSERT_FALSE( m.find( i, []( map_pair const& ) { - ASSERT_TRUE( false ); + EXPECT_FALSE( m.contains( i.nKey )); + EXPECT_FALSE( m.contains( i )); + EXPECT_FALSE( m.contains( other_item( i.nKey ), other_less())); + EXPECT_FALSE( m.find( i, []( map_pair const& ) { + EXPECT_TRUE( false ); } )); - ASSERT_FALSE( m.find( i.nKey, []( map_pair const& ) { + EXPECT_FALSE( m.find( i.nKey, []( map_pair const& ) { EXPECT_TRUE( false ); } )); - ASSERT_FALSE( m.find_with( other_item( i.nKey ), other_less(), []( map_pair const& ) { + EXPECT_FALSE( m.find_with( other_item( i.nKey ), other_less(), []( map_pair const& ) { EXPECT_TRUE( false ); } )); @@ -90,65 +90,65 @@ namespace cds_test { switch ( i.nKey % 16 ) { case 0: - ASSERT_TRUE( m.insert( i )); - ASSERT_FALSE( m.insert( i )); - ASSERT_TRUE( m.find( i.nKey, []( map_pair& v ) { + EXPECT_TRUE( m.insert( i )); + EXPECT_FALSE( m.insert( i )); + EXPECT_TRUE( m.find( i.nKey, []( map_pair& v ) { v.second.nVal = v.first.nKey; v.second.strVal = std::to_string( v.first.nKey ); } )); break; case 1: - ASSERT_TRUE( m.insert( i.nKey )); - ASSERT_FALSE( m.insert( i.nKey )); - ASSERT_TRUE( m.find( i.nKey, []( map_pair& v ) { + EXPECT_TRUE( m.insert( i.nKey )); + EXPECT_FALSE( m.insert( i.nKey )); + EXPECT_TRUE( m.find( i.nKey, []( map_pair& v ) { v.second.nVal = v.first.nKey; v.second.strVal = std::to_string( v.first.nKey ); } )); break; case 2: - ASSERT_TRUE( m.insert( std::to_string( i.nKey ))); - ASSERT_FALSE( m.insert( std::to_string( i.nKey ))); - ASSERT_TRUE( m.find( i.nKey, []( map_pair& v ) { + EXPECT_TRUE( m.insert( std::to_string( i.nKey ))); + EXPECT_FALSE( m.insert( std::to_string( i.nKey ))); + EXPECT_TRUE( m.find( i.nKey, []( map_pair& v ) { v.second.nVal = v.first.nKey; v.second.strVal = std::to_string( v.first.nKey ); } )); break; case 3: - ASSERT_TRUE( m.insert( i, val )); - ASSERT_FALSE( m.insert( i, val )); + EXPECT_TRUE( m.insert( i, val )); + EXPECT_FALSE( m.insert( i, val )); break; case 4: - ASSERT_TRUE( m.insert( i.nKey, val.strVal )); - ASSERT_FALSE( m.insert( i.nKey, val.strVal )); + EXPECT_TRUE( m.insert( i.nKey, val.strVal )); + EXPECT_FALSE( m.insert( i.nKey, val.strVal )); break; case 5: - ASSERT_TRUE( m.insert( val.strVal, i.nKey )); - ASSERT_FALSE( m.insert( val.strVal, i.nKey )); + EXPECT_TRUE( m.insert( val.strVal, i.nKey )); + EXPECT_FALSE( m.insert( val.strVal, i.nKey )); break; case 6: - ASSERT_TRUE( m.insert_with( i, []( map_pair& v ) { + EXPECT_TRUE( m.insert_with( i, []( map_pair& v ) { v.second.nVal = v.first.nKey; v.second.strVal = std::to_string( v.first.nKey ); } )); - ASSERT_FALSE( m.insert_with( i, []( map_pair& v ) { + EXPECT_FALSE( m.insert_with( i, []( map_pair& v ) { EXPECT_TRUE( false ); } )); break; case 7: - ASSERT_TRUE( m.insert_with( i.nKey, []( map_pair& v ) { + EXPECT_TRUE( m.insert_with( i.nKey, []( map_pair& v ) { v.second.nVal = v.first.nKey; v.second.strVal = std::to_string( v.first.nKey ); } )); - ASSERT_FALSE( m.insert_with( i.nKey, []( map_pair& v ) { + EXPECT_FALSE( m.insert_with( i.nKey, []( map_pair& v ) { EXPECT_TRUE( false ); } )); break; case 8: - ASSERT_TRUE( m.insert_with( val.strVal, []( map_pair& v ) { + EXPECT_TRUE( m.insert_with( val.strVal, []( map_pair& v ) { v.second.nVal = v.first.nKey; v.second.strVal = std::to_string( v.first.nKey ); } )); - ASSERT_FALSE( m.insert_with( val.strVal, []( map_pair& v ) { + EXPECT_FALSE( m.insert_with( val.strVal, []( map_pair& v ) { EXPECT_TRUE( false ); } )); break; @@ -156,122 +156,122 @@ namespace cds_test { updResult = m.update( i.nKey, []( bool, map_pair& ) { EXPECT_TRUE( false ); }, false ); - ASSERT_FALSE( updResult.first ); - ASSERT_FALSE( updResult.second ); + EXPECT_FALSE( updResult.first ); + EXPECT_FALSE( updResult.second ); updResult = m.update( i.nKey, []( bool bNew, map_pair& v ) { EXPECT_TRUE( bNew ); v.second.nVal = v.first.nKey; }); - ASSERT_TRUE( updResult.first ); - ASSERT_TRUE( updResult.second ); + EXPECT_TRUE( updResult.first ); + EXPECT_TRUE( updResult.second ); updResult = m.update( i.nKey, []( bool bNew, map_pair& v ) { EXPECT_FALSE( bNew ); EXPECT_EQ( v.first.nKey, v.second.nVal ); v.second.strVal = std::to_string( v.second.nVal ); } ); - ASSERT_TRUE( updResult.first ); - ASSERT_FALSE( updResult.second ); + EXPECT_TRUE( updResult.first ); + EXPECT_FALSE( updResult.second ); break; case 10: updResult = m.update( i, []( bool, map_pair& ) { EXPECT_TRUE( false ); }, false ); - ASSERT_FALSE( updResult.first ); - ASSERT_FALSE( updResult.second ); + EXPECT_FALSE( updResult.first ); + EXPECT_FALSE( updResult.second ); updResult = m.update( i, []( bool bNew, map_pair& v ) { EXPECT_TRUE( bNew ); v.second.nVal = v.first.nKey; }); - ASSERT_TRUE( updResult.first ); - ASSERT_TRUE( updResult.second ); + EXPECT_TRUE( updResult.first ); + EXPECT_TRUE( updResult.second ); updResult = m.update( i, []( bool bNew, map_pair& v ) { EXPECT_FALSE( bNew ); EXPECT_EQ( v.first.nKey, v.second.nVal ); v.second.strVal = std::to_string( v.second.nVal ); } ); - ASSERT_TRUE( updResult.first ); - ASSERT_FALSE( updResult.second ); + EXPECT_TRUE( updResult.first ); + EXPECT_FALSE( updResult.second ); break; case 11: updResult = m.update( val.strVal, []( bool, map_pair& ) { EXPECT_TRUE( false ); }, false ); - ASSERT_FALSE( updResult.first ); - ASSERT_FALSE( updResult.second ); + EXPECT_FALSE( updResult.first ); + EXPECT_FALSE( updResult.second ); updResult = m.update( val.strVal, []( bool bNew, map_pair& v ) { EXPECT_TRUE( bNew ); v.second.nVal = v.first.nKey; }); - ASSERT_TRUE( updResult.first ); - ASSERT_TRUE( updResult.second ); + EXPECT_TRUE( updResult.first ); + EXPECT_TRUE( updResult.second ); updResult = m.update( val.strVal, []( bool bNew, map_pair& v ) { EXPECT_FALSE( bNew ); EXPECT_EQ( v.first.nKey, v.second.nVal ); v.second.strVal = std::to_string( v.second.nVal ); } ); - ASSERT_TRUE( updResult.first ); - ASSERT_FALSE( updResult.second ); + EXPECT_TRUE( updResult.first ); + EXPECT_FALSE( updResult.second ); break; case 12: - ASSERT_TRUE( m.emplace( i.nKey )); - ASSERT_FALSE( m.emplace( i.nKey )); - ASSERT_TRUE( m.find( i.nKey, []( map_pair& v ) { + EXPECT_TRUE( m.emplace( i.nKey )); + EXPECT_FALSE( m.emplace( i.nKey )); + EXPECT_TRUE( m.find( i.nKey, []( map_pair& v ) { v.second.nVal = v.first.nKey; v.second.strVal = std::to_string( v.first.nKey ); } )); break; case 13: - ASSERT_TRUE( m.emplace( i, i.nKey )); - ASSERT_FALSE( m.emplace( i, i.nKey )); + EXPECT_TRUE( m.emplace( i, i.nKey )); + EXPECT_FALSE( m.emplace( i, i.nKey )); break; case 14: { std::string str = val.strVal; - ASSERT_TRUE( m.emplace( i, std::move( str ))); - ASSERT_TRUE( str.empty()); + EXPECT_TRUE( m.emplace( i, std::move( str ))); + EXPECT_TRUE( str.empty()); str = val.strVal; - ASSERT_FALSE( m.emplace( i, std::move( str ))); - ASSERT_TRUE( str.empty()); + EXPECT_FALSE( m.emplace( i, std::move( str ))); + EXPECT_TRUE( str.empty()); } break; case 15: { std::string str = val.strVal; - ASSERT_TRUE( m.emplace( i, i.nKey, std::move( str ))); - ASSERT_TRUE( str.empty()); + EXPECT_TRUE( m.emplace( i, i.nKey, std::move( str ))); + EXPECT_TRUE( str.empty()); str = val.strVal; - ASSERT_FALSE( m.emplace( i, i.nKey, std::move( str ))); - ASSERT_TRUE( str.empty()); + EXPECT_FALSE( m.emplace( i, i.nKey, std::move( str ))); + EXPECT_TRUE( str.empty()); } break; } - ASSERT_TRUE( m.contains( i.nKey )); - ASSERT_TRUE( m.contains( i )); - ASSERT_TRUE( m.contains( other_item( i.nKey ), other_less())); - ASSERT_TRUE( m.find( i, []( map_pair const& v ) { + EXPECT_TRUE( m.contains( i.nKey )); + EXPECT_TRUE( m.contains( i )); + EXPECT_TRUE( m.contains( other_item( i.nKey ), other_less())); + EXPECT_TRUE( m.find( i, []( map_pair const& v ) { EXPECT_EQ( v.first.nKey, v.second.nVal ); EXPECT_EQ( std::to_string( v.first.nKey ), v.second.strVal ); } )); - ASSERT_TRUE( m.find( i.nKey, []( map_pair const& v ) { + EXPECT_TRUE( m.find( i.nKey, []( map_pair const& v ) { EXPECT_EQ( v.first.nKey, v.second.nVal ); EXPECT_EQ( std::to_string( v.first.nKey ), v.second.strVal ); } )); - ASSERT_TRUE( m.find_with( other_item( i.nKey ), other_less(), []( map_pair const& v ) { + EXPECT_TRUE( m.find_with( other_item( i.nKey ), other_less(), []( map_pair const& v ) { EXPECT_EQ( v.first.nKey, v.second.nVal ); EXPECT_EQ( std::to_string( v.first.nKey ), v.second.strVal ); } )); } - ASSERT_FALSE( m.empty() ); - ASSERT_CONTAINER_SIZE( m, kkSize ); - ASSERT_FALSE( m.begin() == m.end() ); - ASSERT_FALSE( m.cbegin() == m.cend() ); + EXPECT_FALSE( m.empty() ); + EXPECT_CONTAINER_SIZE( m, kkSize ); + EXPECT_FALSE( m.begin() == m.end() ); + EXPECT_FALSE( m.cbegin() == m.cend() ); shuffle( arrKeys.begin(), arrKeys.end() ); @@ -279,19 +279,19 @@ namespace cds_test { for ( auto const& i : arrKeys ) { value_type const& val( arrVals.at( i.nKey ) ); - ASSERT_TRUE( m.contains( i.nKey )); - ASSERT_TRUE( m.contains( val.strVal ) ); - ASSERT_TRUE( m.contains( i )); - ASSERT_TRUE( m.contains( other_item( i.nKey ), other_less())); - ASSERT_TRUE( m.find( i, []( map_pair const& v ) { + EXPECT_TRUE( m.contains( i.nKey )); + EXPECT_TRUE( m.contains( val.strVal ) ); + EXPECT_TRUE( m.contains( i )); + EXPECT_TRUE( m.contains( other_item( i.nKey ), other_less())); + EXPECT_TRUE( m.find( i, []( map_pair const& v ) { EXPECT_EQ( v.first.nKey, v.second.nVal ); EXPECT_EQ( std::to_string( v.first.nKey ), v.second.strVal ); } )); - ASSERT_TRUE( m.find( i.nKey, []( map_pair const& v ) { + EXPECT_TRUE( m.find( i.nKey, []( map_pair const& v ) { EXPECT_EQ( v.first.nKey, v.second.nVal ); EXPECT_EQ( std::to_string( v.first.nKey ), v.second.strVal ); } )); - ASSERT_TRUE( m.find_with( other_item( i.nKey ), other_less(), []( map_pair const& v ) { + EXPECT_TRUE( m.find_with( other_item( i.nKey ), other_less(), []( map_pair const& v ) { EXPECT_EQ( v.first.nKey, v.second.nVal ); EXPECT_EQ( std::to_string( v.first.nKey ), v.second.strVal ); } )); @@ -299,90 +299,90 @@ namespace cds_test { switch ( i.nKey % 8 ) { case 0: - ASSERT_TRUE( m.erase( i )); - ASSERT_FALSE( m.erase( i )); + EXPECT_TRUE( m.erase( i )); + EXPECT_FALSE( m.erase( i )); break; case 1: - ASSERT_TRUE( m.erase( i.nKey )); - ASSERT_FALSE( m.erase( i.nKey )); + EXPECT_TRUE( m.erase( i.nKey )); + EXPECT_FALSE( m.erase( i.nKey )); break; case 2: - ASSERT_TRUE( m.erase( val.strVal )); - ASSERT_FALSE( m.erase( val.strVal )); + EXPECT_TRUE( m.erase( val.strVal )); + EXPECT_FALSE( m.erase( val.strVal )); break; case 3: - ASSERT_TRUE( m.erase_with( other_item( i.nKey ), other_less())); - ASSERT_FALSE( m.erase_with( other_item( i.nKey ), other_less())); + EXPECT_TRUE( m.erase_with( other_item( i.nKey ), other_less())); + EXPECT_FALSE( m.erase_with( other_item( i.nKey ), other_less())); break; case 4: - ASSERT_TRUE( m.erase( i, []( map_pair& v ) { + EXPECT_TRUE( m.erase( i, []( map_pair& v ) { EXPECT_EQ( v.first.nKey, v.second.nVal ); EXPECT_EQ( std::to_string( v.first.nKey ), v.second.strVal ); })); - ASSERT_FALSE( m.erase( i, []( map_pair& ) { + EXPECT_FALSE( m.erase( i, []( map_pair& ) { EXPECT_TRUE( false ); })); break; case 5: - ASSERT_TRUE( m.erase( i.nKey, []( map_pair& v ) { + EXPECT_TRUE( m.erase( i.nKey, []( map_pair& v ) { EXPECT_EQ( v.first.nKey, v.second.nVal ); EXPECT_EQ( std::to_string( v.first.nKey ), v.second.strVal ); })); - ASSERT_FALSE( m.erase( i.nKey, []( map_pair& ) { + EXPECT_FALSE( m.erase( i.nKey, []( map_pair& ) { EXPECT_TRUE( false ); })); break; case 6: - ASSERT_TRUE( m.erase( val.strVal, []( map_pair& v ) { + EXPECT_TRUE( m.erase( val.strVal, []( map_pair& v ) { EXPECT_EQ( v.first.nKey, v.second.nVal ); EXPECT_EQ( std::to_string( v.first.nKey ), v.second.strVal ); })); - ASSERT_FALSE( m.erase( val.strVal, []( map_pair& ) { + EXPECT_FALSE( m.erase( val.strVal, []( map_pair& ) { EXPECT_TRUE( false ); })); break; case 7: - ASSERT_TRUE( m.erase_with( other_item( i.nKey ), other_less(), []( map_pair& v ) { + EXPECT_TRUE( m.erase_with( other_item( i.nKey ), other_less(), []( map_pair& v ) { EXPECT_EQ( v.first.nKey, v.second.nVal ); EXPECT_EQ( std::to_string( v.first.nKey ), v.second.strVal ); })); - ASSERT_FALSE( m.erase_with( other_item( i.nKey ), other_less(), []( map_pair& ) { + EXPECT_FALSE( m.erase_with( other_item( i.nKey ), other_less(), []( map_pair& ) { EXPECT_TRUE( false ); })); break; } - ASSERT_FALSE( m.contains( i.nKey )); - ASSERT_FALSE( m.contains( i )); - ASSERT_FALSE( m.contains( val.strVal )); - ASSERT_FALSE( m.contains( other_item( i.nKey ), other_less())); - ASSERT_FALSE( m.find( i, []( map_pair const& ) { - ASSERT_TRUE( false ); + EXPECT_FALSE( m.contains( i.nKey )); + EXPECT_FALSE( m.contains( i )); + EXPECT_FALSE( m.contains( val.strVal )); + EXPECT_FALSE( m.contains( other_item( i.nKey ), other_less())); + EXPECT_FALSE( m.find( i, []( map_pair const& ) { + EXPECT_TRUE( false ); } )); - ASSERT_FALSE( m.find( i.nKey, []( map_pair const& ) { + EXPECT_FALSE( m.find( i.nKey, []( map_pair const& ) { EXPECT_TRUE( false ); } )); - ASSERT_FALSE( m.find_with( other_item( i.nKey ), other_less(), []( map_pair const& ) { + EXPECT_FALSE( m.find_with( other_item( i.nKey ), other_less(), []( map_pair const& ) { EXPECT_TRUE( false ); } )); } - ASSERT_TRUE( m.empty() ); - ASSERT_CONTAINER_SIZE( m, 0 ); + EXPECT_TRUE( m.empty() ); + EXPECT_CONTAINER_SIZE( m, 0 ); - ASSERT_TRUE( m.begin() == m.end()); - ASSERT_TRUE( m.cbegin() == m.cend()); + EXPECT_TRUE( m.begin() == m.end()); + EXPECT_TRUE( m.cbegin() == m.cend()); // clear for ( auto const& i : arrKeys ) - ASSERT_TRUE( m.insert( i )); + EXPECT_TRUE( m.insert( i )); - ASSERT_FALSE( m.empty() ); - ASSERT_CONTAINER_SIZE( m, kkSize ); + EXPECT_FALSE( m.empty() ); + EXPECT_CONTAINER_SIZE( m, kkSize ); m.clear(); - ASSERT_TRUE( m.empty() ); - ASSERT_CONTAINER_SIZE( m, 0 ); + EXPECT_TRUE( m.empty() ); + EXPECT_CONTAINER_SIZE( m, 0 ); } }; diff --git a/test/unit/map/test_map_hp.h b/test/unit/map/test_map_hp.h index 22095115..d3305706 100644 --- a/test/unit/map/test_map_hp.h +++ b/test/unit/map/test_map_hp.h @@ -25,7 +25,7 @@ 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. + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef CDSUNIT_MAP_TEST_MAP_HP_H @@ -48,8 +48,8 @@ namespace cds_test { base_class::test( m ); - ASSERT_TRUE( m.empty()); - ASSERT_CONTAINER_SIZE( m, 0 ); + EXPECT_TRUE( m.empty()); + EXPECT_CONTAINER_SIZE( m, 0 ); typedef typename Map::value_type map_pair; size_t const kkSize = base_class::kSize; @@ -68,9 +68,9 @@ namespace cds_test { } for ( auto const& i : arrKeys ) - ASSERT_TRUE( m.insert( i ) ); - ASSERT_FALSE( m.empty() ); - ASSERT_CONTAINER_SIZE( m, kkSize ); + EXPECT_TRUE( m.insert( i ) ); + EXPECT_FALSE( m.empty() ); + EXPECT_CONTAINER_SIZE( m, kkSize ); // iterators size_t nCount = 0; @@ -97,15 +97,15 @@ namespace cds_test { gp = m.get( i.nKey ); ASSERT_FALSE( !gp ); - ASSERT_EQ( gp->first.nKey, i.nKey ); + EXPECT_EQ( gp->first.nKey, i.nKey ); gp.release(); gp = m.get( i ); ASSERT_FALSE( !gp ); - ASSERT_EQ( gp->first.nKey, i.nKey ); + EXPECT_EQ( gp->first.nKey, i.nKey ); gp.release(); gp = m.get_with( other_item( i.nKey ), other_less()); ASSERT_FALSE( !gp ); - ASSERT_EQ( gp->first.nKey, i.nKey ); + EXPECT_EQ( gp->first.nKey, i.nKey ); switch ( i.nKey % 4 ) { case 0: @@ -122,7 +122,7 @@ namespace cds_test { break; } ASSERT_FALSE( !gp ); - ASSERT_EQ( gp->first.nKey, i.nKey ); + EXPECT_EQ( gp->first.nKey, i.nKey ); gp.release(); gp = m.get( i.nKey ); @@ -132,8 +132,8 @@ namespace cds_test { gp = m.get_with( other_item( i.nKey ), other_less() ); ASSERT_TRUE( !gp ); } - ASSERT_TRUE( m.empty() ); - ASSERT_CONTAINER_SIZE( m, 0 ); + EXPECT_TRUE( m.empty() ); + EXPECT_CONTAINER_SIZE( m, 0 ); } }; diff --git a/test/unit/map/test_michael_iterable.h b/test/unit/map/test_michael_iterable.h new file mode 100644 index 00000000..54e22862 --- /dev/null +++ b/test/unit/map/test_michael_iterable.h @@ -0,0 +1,411 @@ +/* + This file is a part of libcds - Concurrent Data Structures library + + (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016 + + 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 CDSUNIT_MAP_TEST_MICHAEL_ITERABLE_MAP_H +#define CDSUNIT_MAP_TEST_MICHAEL_ITERABLE_MAP_H + +#include "test_map_data.h" + +// forward declaration +namespace cds { namespace container {} } + +namespace cds_test { + + class michael_iterable_map: public map_fixture + { + public: + static size_t const kSize = 1000; + + protected: + template + void test( Map& m ) + { + // Precondition: map is empty + // Postcondition: map is empty + + EXPECT_TRUE( m.empty()); + EXPECT_CONTAINER_SIZE( m, 0 ); + + typedef typename Map::value_type map_pair; + size_t const kkSize = kSize; + + std::vector arrKeys; + for ( int i = 0; i < static_cast(kkSize); ++i ) + arrKeys.push_back( key_type( i )); + shuffle( arrKeys.begin(), arrKeys.end()); + + std::vector< value_type > arrVals; + for ( size_t i = 0; i < kkSize; ++i ) { + value_type val; + val.nVal = static_cast( i ); + val.strVal = std::to_string( i ); + arrVals.push_back( val ); + } + + // insert/find + for ( auto const& i : arrKeys ) { + value_type const& val( arrVals.at( i.nKey )); + + EXPECT_FALSE( m.contains( i.nKey )); + EXPECT_FALSE( m.contains( i )); + EXPECT_FALSE( m.contains( other_item( i.nKey ), other_less())); + EXPECT_FALSE( m.find( i, []( map_pair const& ) { + EXPECT_TRUE( false ); + } )); + EXPECT_FALSE( m.find( i.nKey, []( map_pair const& ) { + EXPECT_TRUE( false ); + } )); + EXPECT_FALSE( m.find_with( other_item( i.nKey ), other_less(), []( map_pair const& ) { + EXPECT_TRUE( false ); + } )); + + std::pair< bool, bool > updResult; + + switch ( i.nKey % 17 ) { + case 0: + EXPECT_TRUE( m.insert( i )); + EXPECT_FALSE( m.insert( i )); + EXPECT_TRUE( m.find( i.nKey, []( map_pair& v ) { + v.second.nVal = v.first.nKey; + v.second.strVal = std::to_string( v.first.nKey ); + } )); + break; + case 1: + EXPECT_TRUE( m.insert( i.nKey )); + EXPECT_FALSE( m.insert( i.nKey )); + EXPECT_TRUE( m.find( i.nKey, []( map_pair& v ) { + v.second.nVal = v.first.nKey; + v.second.strVal = std::to_string( v.first.nKey ); + } )); + break; + case 2: + EXPECT_TRUE( m.insert( std::to_string( i.nKey ))); + EXPECT_FALSE( m.insert( std::to_string( i.nKey ))); + EXPECT_TRUE( m.find( i.nKey, []( map_pair& v ) { + v.second.nVal = v.first.nKey; + v.second.strVal = std::to_string( v.first.nKey ); + } )); + break; + case 3: + EXPECT_TRUE( m.insert( i, val )); + EXPECT_FALSE( m.insert( i, val )); + break; + case 4: + EXPECT_TRUE( m.insert( i.nKey, val.strVal )); + EXPECT_FALSE( m.insert( i.nKey, val.strVal )); + break; + case 5: + EXPECT_TRUE( m.insert( val.strVal, i.nKey )); + EXPECT_FALSE( m.insert( val.strVal, i.nKey )); + break; + case 6: + EXPECT_TRUE( m.insert_with( i, []( map_pair& v ) { + v.second.nVal = v.first.nKey; + v.second.strVal = std::to_string( v.first.nKey ); + } )); + EXPECT_FALSE( m.insert_with( i, []( map_pair& v ) { + EXPECT_TRUE( false ); + } )); + break; + case 7: + EXPECT_TRUE( m.insert_with( i.nKey, []( map_pair& v ) { + v.second.nVal = v.first.nKey; + v.second.strVal = std::to_string( v.first.nKey ); + } )); + EXPECT_FALSE( m.insert_with( i.nKey, []( map_pair& v ) { + EXPECT_TRUE( false ); + } )); + break; + case 8: + EXPECT_TRUE( m.insert_with( val.strVal, []( map_pair& v ) { + v.second.nVal = v.first.nKey; + v.second.strVal = std::to_string( v.first.nKey ); + } )); + EXPECT_FALSE( m.insert_with( val.strVal, []( map_pair& v ) { + EXPECT_TRUE( false ); + } )); + break; + case 9: + updResult = m.update( i.nKey, []( map_pair&, map_pair* ) { + EXPECT_TRUE( false ); + }, false ); + EXPECT_FALSE( updResult.first ); + EXPECT_FALSE( updResult.second ); + + updResult = m.update( i.nKey, []( map_pair& v, map_pair* old ) { + EXPECT_TRUE( old == nullptr ); + v.second.nVal = v.first.nKey; + }); + EXPECT_TRUE( updResult.first ); + EXPECT_TRUE( updResult.second ); + + updResult = m.update( i.nKey, []( map_pair& v, map_pair* old ) { + ASSERT_FALSE( old == nullptr ); + EXPECT_EQ( v.first.nKey, old->second.nVal ); + v.second.nVal = old->second.nVal; + v.second.strVal = std::to_string( old->second.nVal ); + } ); + EXPECT_TRUE( updResult.first ); + EXPECT_FALSE( updResult.second ); + break; + case 10: + updResult = m.update( i, []( map_pair&, map_pair* ) { + EXPECT_TRUE( false ); + }, false ); + EXPECT_FALSE( updResult.first ); + EXPECT_FALSE( updResult.second ); + + updResult = m.update( i, []( map_pair& v, map_pair* old ) { + EXPECT_TRUE( old == nullptr ); + v.second.nVal = v.first.nKey; + }); + EXPECT_TRUE( updResult.first ); + EXPECT_TRUE( updResult.second ); + + updResult = m.update( i, []( map_pair& v, map_pair* old ) { + ASSERT_FALSE( old == nullptr ); + EXPECT_EQ( v.first.nKey, old->second.nVal ); + v.second.nVal = old->second.nVal; + v.second.strVal = std::to_string( v.second.nVal ); + } ); + EXPECT_TRUE( updResult.first ); + EXPECT_FALSE( updResult.second ); + break; + case 11: + updResult = m.update( val.strVal, []( map_pair&, map_pair* ) { + EXPECT_TRUE( false ); + }, false ); + EXPECT_FALSE( updResult.first ); + EXPECT_FALSE( updResult.second ); + + updResult = m.update( val.strVal, []( map_pair& v, map_pair* old ) { + EXPECT_TRUE( old == nullptr ); + v.second.nVal = v.first.nKey; + }); + EXPECT_TRUE( updResult.first ); + EXPECT_TRUE( updResult.second ); + + updResult = m.update( val.strVal, []( map_pair& v, map_pair* old ) { + ASSERT_FALSE( old == nullptr ); + EXPECT_EQ( v.first.nKey, old->second.nVal ); + v.second.nVal = old->second.nVal; + v.second.strVal = std::to_string( v.second.nVal ); + } ); + EXPECT_TRUE( updResult.first ); + EXPECT_FALSE( updResult.second ); + break; + case 12: + EXPECT_TRUE( m.emplace( i.nKey )); + EXPECT_FALSE( m.emplace( i.nKey )); + EXPECT_TRUE( m.find( i.nKey, []( map_pair& v ) { + v.second.nVal = v.first.nKey; + v.second.strVal = std::to_string( v.first.nKey ); + } )); + break; + case 13: + EXPECT_TRUE( m.emplace( i, i.nKey )); + EXPECT_FALSE( m.emplace( i, i.nKey )); + break; + case 14: + { + std::string str = val.strVal; + EXPECT_TRUE( m.emplace( i, std::move( str ))); + EXPECT_TRUE( str.empty()); + str = val.strVal; + EXPECT_FALSE( m.emplace( i, std::move( str ))); + EXPECT_TRUE( str.empty()); + } + break; + case 15: + { + std::string str = val.strVal; + EXPECT_TRUE( m.emplace( i, i.nKey, std::move( str ))); + EXPECT_TRUE( str.empty()); + str = val.strVal; + EXPECT_FALSE( m.emplace( i, i.nKey, std::move( str ))); + EXPECT_TRUE( str.empty()); + } + break; + case 16: + { + auto res = m.upsert( i, i.nKey, false ); + EXPECT_FALSE( res.first ); + EXPECT_FALSE( res.second ); + + res = m.upsert( i, i.nKey ); + EXPECT_TRUE( res.first ); + EXPECT_TRUE( res.second ); + + std::string str = val.strVal; + res = m.upsert( i, std::move( str )); + EXPECT_TRUE( res.first ); + EXPECT_FALSE( res.second ); + EXPECT_TRUE( str.empty() ); + } + break; + } + + EXPECT_TRUE( m.contains( i.nKey )); + EXPECT_TRUE( m.contains( i )); + EXPECT_TRUE( m.contains( other_item( i.nKey ), other_less())); + EXPECT_TRUE( m.find( i, []( map_pair const& v ) { + EXPECT_EQ( v.first.nKey, v.second.nVal ); + EXPECT_EQ( std::to_string( v.first.nKey ), v.second.strVal ); + } )); + EXPECT_TRUE( m.find( i.nKey, []( map_pair const& v ) { + EXPECT_EQ( v.first.nKey, v.second.nVal ); + EXPECT_EQ( std::to_string( v.first.nKey ), v.second.strVal ); + } )); + EXPECT_TRUE( m.find_with( other_item( i.nKey ), other_less(), []( map_pair const& v ) { + EXPECT_EQ( v.first.nKey, v.second.nVal ); + EXPECT_EQ( std::to_string( v.first.nKey ), v.second.strVal ); + } )); + } + EXPECT_FALSE( m.empty() ); + EXPECT_CONTAINER_SIZE( m, kkSize ); + EXPECT_FALSE( m.begin() == m.end() ); + EXPECT_FALSE( m.cbegin() == m.cend() ); + + shuffle( arrKeys.begin(), arrKeys.end() ); + + // erase/find + for ( auto const& i : arrKeys ) { + value_type const& val( arrVals.at( i.nKey ) ); + + EXPECT_TRUE( m.contains( i.nKey )); + EXPECT_TRUE( m.contains( val.strVal ) ); + EXPECT_TRUE( m.contains( i )); + EXPECT_TRUE( m.contains( other_item( i.nKey ), other_less())); + EXPECT_TRUE( m.find( i, []( map_pair const& v ) { + EXPECT_EQ( v.first.nKey, v.second.nVal ); + EXPECT_EQ( std::to_string( v.first.nKey ), v.second.strVal ); + } )); + EXPECT_TRUE( m.find( i.nKey, []( map_pair const& v ) { + EXPECT_EQ( v.first.nKey, v.second.nVal ); + EXPECT_EQ( std::to_string( v.first.nKey ), v.second.strVal ); + } )); + EXPECT_TRUE( m.find_with( other_item( i.nKey ), other_less(), []( map_pair const& v ) { + EXPECT_EQ( v.first.nKey, v.second.nVal ); + EXPECT_EQ( std::to_string( v.first.nKey ), v.second.strVal ); + } )); + + + switch ( i.nKey % 8 ) { + case 0: + EXPECT_TRUE( m.erase( i )); + EXPECT_FALSE( m.erase( i )); + break; + case 1: + EXPECT_TRUE( m.erase( i.nKey )); + EXPECT_FALSE( m.erase( i.nKey )); + break; + case 2: + EXPECT_TRUE( m.erase( val.strVal )); + EXPECT_FALSE( m.erase( val.strVal )); + break; + case 3: + EXPECT_TRUE( m.erase_with( other_item( i.nKey ), other_less())); + EXPECT_FALSE( m.erase_with( other_item( i.nKey ), other_less())); + break; + case 4: + EXPECT_TRUE( m.erase( i, []( map_pair& v ) { + EXPECT_EQ( v.first.nKey, v.second.nVal ); + EXPECT_EQ( std::to_string( v.first.nKey ), v.second.strVal ); + })); + EXPECT_FALSE( m.erase( i, []( map_pair& ) { + EXPECT_TRUE( false ); + })); + break; + case 5: + EXPECT_TRUE( m.erase( i.nKey, []( map_pair& v ) { + EXPECT_EQ( v.first.nKey, v.second.nVal ); + EXPECT_EQ( std::to_string( v.first.nKey ), v.second.strVal ); + })); + EXPECT_FALSE( m.erase( i.nKey, []( map_pair& ) { + EXPECT_TRUE( false ); + })); + break; + case 6: + EXPECT_TRUE( m.erase( val.strVal, []( map_pair& v ) { + EXPECT_EQ( v.first.nKey, v.second.nVal ); + EXPECT_EQ( std::to_string( v.first.nKey ), v.second.strVal ); + })); + EXPECT_FALSE( m.erase( val.strVal, []( map_pair& ) { + EXPECT_TRUE( false ); + })); + break; + case 7: + EXPECT_TRUE( m.erase_with( other_item( i.nKey ), other_less(), []( map_pair& v ) { + EXPECT_EQ( v.first.nKey, v.second.nVal ); + EXPECT_EQ( std::to_string( v.first.nKey ), v.second.strVal ); + })); + EXPECT_FALSE( m.erase_with( other_item( i.nKey ), other_less(), []( map_pair& ) { + EXPECT_TRUE( false ); + })); + break; + } + + EXPECT_FALSE( m.contains( i.nKey )); + EXPECT_FALSE( m.contains( i )); + EXPECT_FALSE( m.contains( val.strVal )); + EXPECT_FALSE( m.contains( other_item( i.nKey ), other_less())); + EXPECT_FALSE( m.find( i, []( map_pair const& ) { + EXPECT_TRUE( false ); + } )); + EXPECT_FALSE( m.find( i.nKey, []( map_pair const& ) { + EXPECT_TRUE( false ); + } )); + EXPECT_FALSE( m.find_with( other_item( i.nKey ), other_less(), []( map_pair const& ) { + EXPECT_TRUE( false ); + } )); + } + EXPECT_TRUE( m.empty() ); + EXPECT_CONTAINER_SIZE( m, 0 ); + + EXPECT_TRUE( m.begin() == m.end()); + EXPECT_TRUE( m.cbegin() == m.cend()); + + // clear + for ( auto const& i : arrKeys ) + EXPECT_TRUE( m.insert( i )); + + EXPECT_FALSE( m.empty() ); + EXPECT_CONTAINER_SIZE( m, kkSize ); + + m.clear(); + + EXPECT_TRUE( m.empty() ); + EXPECT_CONTAINER_SIZE( m, 0 ); + } + }; + +} // namespace cds_test + +#endif // #ifndef CDSUNIT_MAP_TEST_MICHAEL_ITERABLE_MAP_H diff --git a/test/unit/map/test_michael_iterable_hp.h b/test/unit/map/test_michael_iterable_hp.h new file mode 100644 index 00000000..19258b62 --- /dev/null +++ b/test/unit/map/test_michael_iterable_hp.h @@ -0,0 +1,142 @@ +/* + This file is a part of libcds - Concurrent Data Structures library + + (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2016 + + 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 CDSUNIT_MAP_TEST_MICHAEL_ITERABLE_MAP_HP_H +#define CDSUNIT_MAP_TEST_MICHAEL_ITERABLE_MAP_HP_H + +#include "test_michael_iterable.h" + +namespace cds_test { + + class michael_iterable_hp: public michael_iterable_map + { + typedef michael_iterable_map base_class; + + protected: + template + void test( Map& m ) + { + // Precondition: map is empty + // Postcondition: map is empty + + base_class::test( m ); + + EXPECT_TRUE( m.empty()); + EXPECT_CONTAINER_SIZE( m, 0 ); + + typedef typename Map::value_type map_pair; + size_t const kkSize = base_class::kSize; + + std::vector arrKeys; + for ( int i = 0; i < static_cast(kkSize); ++i ) + arrKeys.push_back( key_type( i )); + shuffle( arrKeys.begin(), arrKeys.end()); + + std::vector< value_type > arrVals; + for ( size_t i = 0; i < kkSize; ++i ) { + value_type val; + val.nVal = static_cast( i ); + val.strVal = std::to_string( i ); + arrVals.push_back( val ); + } + + for ( auto const& i : arrKeys ) + EXPECT_TRUE( m.insert( i ) ); + EXPECT_FALSE( m.empty() ); + EXPECT_CONTAINER_SIZE( m, kkSize ); + + // iterators + size_t nCount = 0; + for ( auto it = m.begin(); it != m.end(); ++it ) { + EXPECT_EQ( it->second.nVal, 0 ); + it->second.nVal = it->first.nKey * 2; + ++nCount; + } + EXPECT_EQ( nCount, kkSize ); + + nCount = 0; + for ( auto it = m.cbegin(); it != m.cend(); ++it ) { + EXPECT_EQ( it->second.nVal, it->first.nKey * 2 ); + ++nCount; + } + EXPECT_EQ( nCount, kkSize ); + + // get/extract + typedef typename Map::guarded_ptr guarded_ptr; + guarded_ptr gp; + + for ( auto const& i : arrKeys ) { + value_type const& val = arrVals.at( i.nKey ); + + gp = m.get( i.nKey ); + ASSERT_FALSE( !gp ); + EXPECT_EQ( gp->first.nKey, i.nKey ); + gp.release(); + gp = m.get( i ); + ASSERT_FALSE( !gp ); + EXPECT_EQ( gp->first.nKey, i.nKey ); + gp.release(); + gp = m.get_with( other_item( i.nKey ), other_less()); + ASSERT_FALSE( !gp ); + EXPECT_EQ( gp->first.nKey, i.nKey ); + + switch ( i.nKey % 4 ) { + case 0: + gp = m.extract( i.nKey ); + break; + case 1: + gp = m.extract( i ); + break; + case 2: + gp = m.extract( val.strVal ); + break; + case 3: + gp = m.extract_with( other_item( i.nKey ), other_less()); + break; + } + ASSERT_FALSE( !gp ); + EXPECT_EQ( gp->first.nKey, i.nKey ); + gp.release(); + + gp = m.get( i.nKey ); + ASSERT_TRUE( !gp ); + gp = m.get( i ); + ASSERT_TRUE( !gp ); + gp = m.get_with( other_item( i.nKey ), other_less() ); + ASSERT_TRUE( !gp ); + } + EXPECT_TRUE( m.empty() ); + EXPECT_CONTAINER_SIZE( m, 0 ); + } + }; + +} // namespace cds_test + +#endif // #ifndef CDSUNIT_MAP_TEST_MICHAEL_ITERABLE_MAP_HP_H