X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=cds%2Fcontainer%2Fstriped_set%2Fadapter.h;h=3a6bc81d886e7a60e5000d738d66b725484e589e;hb=e891296f3970a4d973ad69d732cb646dbbe820c5;hp=a7da7a61133f8e90c7904d4c26809aea75357a9e;hpb=ba8f90c18cb0f1087573692b6e7aacc183233233;p=libcds.git diff --git a/cds/container/striped_set/adapter.h b/cds/container/striped_set/adapter.h index a7da7a61..3a6bc81d 100644 --- a/cds/container/striped_set/adapter.h +++ b/cds/container/striped_set/adapter.h @@ -1,7 +1,35 @@ -//$$CDS-header$$ - -#ifndef __CDS_CONTAINER_STRIPED_SET_ADAPTER_H -#define __CDS_CONTAINER_STRIPED_SET_ADAPTER_H +/* + 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_STRIPED_SET_ADAPTER_H +#define CDSLIB_CONTAINER_STRIPED_SET_ADAPTER_H #include #include @@ -73,12 +101,12 @@ namespace cds { namespace container { variadic template and move semantics
- Ensures that the \p item exists in the container - \code template std::pair ensure( const Q& val, Func func ) \endcode + Updates \p item + \code template std::pair update( const Q& val, Func func, bool bAllowInsert ) \endcode The operation performs inserting or changing data. If the \p val key not found in the container, then the new item created from \p val - is inserted. Otherwise, the functor \p func is called with the item found. + is inserted iff \p bAllowInsert is \p true. Otherwise, the functor \p func is called with the item found. The \p Func functor has interface: \code void func( bool bNew, value_type& item, const Q& val ); @@ -93,14 +121,14 @@ namespace cds { namespace container { where arguments are: - \p bNew - \p true if the item has been inserted, \p false otherwise - \p item - container's item - - \p val - argument \p val passed into the \p ensure function + - \p val - argument \p val passed into the \p update() function The functor can change non-key fields of the \p item. The type \p Q can differ from \ref value_type of items storing in the container. Therefore, the \p value_type should be comparable with type \p Q and constructible from type \p Q, - Returns std::pair where \p first is true if operation is successfull, + Returns 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 val key already exists.
@@ -177,14 +205,34 @@ namespace cds { namespace container { //@cond using cds::intrusive::striped_set::adapted_sequential_container; using cds::intrusive::striped_set::adapted_container; + //@endcond - using cds::intrusive::striped_set::load_factor_resizing; - using cds::intrusive::striped_set::single_bucket_size_threshold; - using cds::intrusive::striped_set::no_resizing; + ///@copydoc cds::intrusive::striped_set::load_factor_resizing + template + using load_factor_resizing = cds::intrusive::striped_set::load_factor_resizing; - using cds::intrusive::striped_set::striping; - using cds::intrusive::striped_set::refinable; - //@endcond + ///@copydoc cds::intrusive::striped_set::rational_load_factor_resizing + template + using rational_load_factor_resizing = cds::intrusive::striped_set::rational_load_factor_resizing; + + ///@copydoc cds::intrusive::striped_set::single_bucket_size_threshold + template + using single_bucket_size_threshold = cds::intrusive::striped_set::single_bucket_size_threshold; + + ///@copydoc cds::intrusive::striped_set::no_resizing + typedef cds::intrusive::striped_set::no_resizing no_resizing; + + ///@copydoc cds::intrusive::striped_set::striping + template + using striping = cds::intrusive::striped_set::striping; + + ///@copydoc cds::intrusive::striped_set::refinable + template < + class RecursiveLock = std::recursive_mutex, + typename BackOff = cds::backoff::yield, + class Alloc = CDS_DEFAULT_ALLOCATOR + > + using refinable = cds::intrusive::striped_set::refinable; //@cond namespace details { @@ -272,11 +320,20 @@ namespace cds { namespace container { } template - std::pair ensure( const Q& val, Func func ) + std::pair update( const Q& val, Func func, bool bAllowInsert ) { - std::pair res = m_Set.insert( value_type(val) ); - func( res.second, const_cast(*res.first), val ); - return std::make_pair( true, res.second ); + if ( bAllowInsert ) { + std::pair res = m_Set.insert( value_type(val) ); + func( res.second, const_cast(*res.first), val ); + return std::make_pair( true, res.second ); + } + else { + auto it = m_Set.find( value_type( val )); + if ( it == m_Set.end() ) + return std::make_pair( false, false ); + func( false, const_cast(*it), val ); + return std::make_pair( true, false ); + } } template @@ -395,7 +452,7 @@ namespace cds { namespace container { template bool insert( const Q& key, Func f ) { - std::pair res = m_Map.insert( value_type( key, mapped_type() ) ); + std::pair res = m_Map.insert( value_type( key_type( key ), mapped_type() ) ); if ( res.second ) f( *res.first ); return res.second; @@ -404,22 +461,31 @@ namespace cds { namespace container { template bool emplace( Q&& key, Args&&... args ) { - std::pair res = m_Map.emplace( std::forward(key), std::move( mapped_type( std::forward(args)...))); + std::pair res = m_Map.emplace( key_type( std::forward( key )), mapped_type( std::forward( args )...)); return res.second; } template - std::pair ensure( const Q& val, Func func ) + std::pair update( const Q& key, Func func, bool bAllowInsert ) { - std::pair res = m_Map.insert( value_type( val, mapped_type() )); - func( res.second, *res.first ); - return std::make_pair( true, res.second ); + if ( bAllowInsert ) { + std::pair res = m_Map.insert( value_type( key_type( key ), mapped_type() )); + func( res.second, *res.first ); + return std::make_pair( true, res.second ); + } + else { + auto it = m_Map.find( key_type( key )); + if ( it == end() ) + return std::make_pair( false, false ); + func( false, *it ); + return std::make_pair( true, false ); + } } template bool erase( const Q& key, Func f ) { - iterator it = m_Map.find( key_type(key) ); + iterator it = m_Map.find( key_type( key )); if ( it == m_Map.end() ) return false; f( *it ); @@ -430,7 +496,7 @@ namespace cds { namespace container { template bool find( Q& val, Func f ) { - iterator it = m_Map.find( key_type(val) ); + iterator it = m_Map.find( key_type( val )); if ( it == m_Map.end() ) return false; f( *it, val ); @@ -466,4 +532,4 @@ namespace cds { namespace container { }} // namespace cds::container -#endif // #ifndef __CDS_CONTAINER_STRIPED_SET_ADAPTER_H +#endif // #ifndef CDSLIB_CONTAINER_STRIPED_SET_ADAPTER_H