X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=cds%2Fcontainer%2Fmichael_kvlist_nogc.h;h=e43c87c11d251fc15ae1390c26b620567a2f6176;hb=46d9829c1e2543773c743957866679f97823ffc1;hp=ad0a7c4d2b9662ceeceba7d4b6da90019116488a;hpb=e754c2428919ca026e405c1aaf1587c2f4a104c1;p=libcds.git diff --git a/cds/container/michael_kvlist_nogc.h b/cds/container/michael_kvlist_nogc.h index ad0a7c4d..e43c87c1 100644 --- a/cds/container/michael_kvlist_nogc.h +++ b/cds/container/michael_kvlist_nogc.h @@ -1,4 +1,32 @@ -//$$CDS-header$$ +/* + 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 CDSLIB_CONTAINER_MICHAEL_KVLIST_NOGC_H #define CDSLIB_CONTAINER_MICHAEL_KVLIST_NOGC_H @@ -84,6 +112,23 @@ namespace cds { namespace container { 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 + typedef typename base_class::stat stat; ///< Internal statistics + + //@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 @@ -93,32 +138,6 @@ namespace cds { namespace container { typedef typename maker::intrusive_traits::compare intrusive_key_comparator; typedef typename base_class::atomic_node_ptr head_type; - //@endcond - - protected: - //@cond - template - static node_type * alloc_node(const K& key) - { - return cxx_allocator().New( key ); - } - - template - static node_type * alloc_node( const K& key, const V& val ) - { - return cxx_allocator().New( key, val ); - } - - template - static node_type * alloc_node( K&& key, Args&&... args ) - { - return cxx_allocator().MoveNew( std::forward(key), std::forward(args)... ); - } - - static void free_node( node_type * pNode ) - { - cxx_allocator().Delete( pNode ); - } struct node_disposer { void operator()( node_type * pNode ) @@ -127,16 +146,6 @@ namespace cds { namespace container { } }; typedef std::unique_ptr< node_type, node_disposer > scoped_node_ptr; - - head_type& head() - { - return base_class::m_pHead; - } - - head_type const& head() const - { - return base_class::m_pHead; - } //@endcond protected: @@ -229,17 +238,23 @@ namespace cds { namespace container { //@endcond public: + ///@name Forward iterators + //@{ /// Forward iterator /** - The forward iterator for Michael's list based on gc::nogc has pre- and post-increment operators. + The forward iterator is safe: you may use it in multi-threaded enviromnent without any synchronization. + + The forward iterator for Michael's list based on \p gc::nogc has pre- and post-increment operators. The iterator interface to access item data: - - operator -> - returns a pointer to \ref value_type for iterator - - operator * - returns a reference (a const reference for \p const_iterator) to \ref value_type for iterator + - operator -> - returns a pointer to \p value_type + - operator * - returns a reference (a const reference for \p const_iterator) to \p value_type - const key_type& key() - returns a key reference for iterator - mapped_type& val() - retuns a value reference for iterator (const reference for \p const_iterator) - For both functions the iterator should not be equal to end() + For both functions the iterator should not be equal to \p end(). + + @note \p end() iterator is not dereferenceable */ typedef iterator_type iterator; @@ -255,7 +270,7 @@ namespace cds { namespace container { */ iterator begin() { - return iterator( head() ); + return iterator( head()); } /// Returns an iterator that addresses the location succeeding the last element in a list @@ -272,38 +287,27 @@ namespace cds { namespace container { } /// Returns a forward const iterator addressing the first element in a list - //@{ const_iterator begin() const { - return const_iterator( head() ); + return const_iterator( head()); } + /// Returns a forward const iterator addressing the first element in a list const_iterator cbegin() const { - return const_iterator( head() ); + return const_iterator( head()); } - //@} /// Returns an const iterator that addresses the location succeeding the last element in a list - //@{ const_iterator end() const { return const_iterator(); } + /// Returns an const iterator that addresses the location succeeding the last element in a list const_iterator cend() const { return const_iterator(); } - //@} - - protected: - //@cond - iterator node_to_iterator( node_type * pNode ) - { - if ( pNode ) - return iterator( *pNode ); - return end(); - } - //@endcond + //@} public: /// Default constructor @@ -313,7 +317,14 @@ namespace cds { namespace container { MichaelKVList() {} - /// List desctructor + //@cond + template >::value >> + explicit MichaelKVList( Stat& st ) + : base_class( st ) + {} + //@endcond + + /// List destructor /** Clears the list */ @@ -393,7 +404,8 @@ namespace cds { namespace container { /// Updates the item /** - If \p key is not in the list and \p bAllowInsert is \p true, + If \p key is not in the list and \p bAllowInsert is \p true, + the function inserts a new item. Otherwise, the function returns an iterator pointing to the item found. @@ -434,7 +446,7 @@ namespace cds { namespace container { template iterator contains( Q const& key ) { - return node_to_iterator( find_at( head(), key, intrusive_key_comparator() ) ); + return node_to_iterator( find_at( head(), key, intrusive_key_comparator())); } //@cond template @@ -455,7 +467,7 @@ namespace cds { namespace container { iterator contains( Q const& key, Less pred ) { CDS_UNUSED( pred ); - return node_to_iterator( find_at( head(), key, typename maker::template less_wrapper::type() ) ); + return node_to_iterator( find_at( head(), key, typename maker::template less_wrapper::type())); } //@cond template @@ -485,6 +497,12 @@ namespace cds { namespace container { return base_class::size(); } + /// Returns const reference to internal statistics + stat const& statistics() const + { + return base_class::statistics(); + } + /// Clears the list void clear() { @@ -532,7 +550,8 @@ namespace cds { namespace container { scoped_node_ptr pNode( alloc_node( key )); node_type * pItemFound = nullptr; - std::pair ret = base_class::update_at( refHead, *pNode, + std::pair ret = base_class::update_at( refHead, *pNode, + [&pItemFound](bool, node_type& item, node_type&){ pItemFound = &item; }, bAllowInsert ); @@ -552,6 +571,46 @@ namespace cds { namespace container { { return base_class::find_at( refHead, key, cmp ); } + + template + static node_type * alloc_node( const K& key ) + { + return cxx_allocator().New( key ); + } + + template + static node_type * alloc_node( const K& key, const V& val ) + { + return cxx_allocator().New( key, val ); + } + + template + static node_type * alloc_node( K&& key, Args&&... args ) + { + return cxx_allocator().MoveNew( std::forward( key ), std::forward( args )... ); + } + + static void free_node( node_type * pNode ) + { + cxx_allocator().Delete( pNode ); + } + + head_type& head() + { + return base_class::m_pHead; + } + + head_type const& head() const + { + return base_class::m_pHead; + } + + iterator node_to_iterator( node_type * pNode ) + { + if ( pNode ) + return iterator( *pNode ); + return end(); + } //@endcond };