-//$$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_STRIPED_SET_ADAPTER_H
#define CDSLIB_CONTAINER_STRIPED_SET_ADAPTER_H
variadic template and move semantics
<hr>
- <b>Ensures that the \p item exists in the container</b>
- \code template <typename Q, typename Func> std::pair<bool, bool> ensure( const Q& val, Func func ) \endcode
+ <b>Updates \p item</b>
+ \code template <typename Q, typename Func> std::pair<bool, bool> 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 );
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 <tt> std::pair<bool, bool> </tt> where \p first is true if operation is successfull,
+ Returns <tt> std::pair<bool, bool> </tt> 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.
<hr>
//@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 <size_t LoadFactor>
+ using load_factor_resizing = cds::intrusive::striped_set::load_factor_resizing<LoadFactor>;
- using cds::intrusive::striped_set::striping;
- using cds::intrusive::striped_set::refinable;
- //@endcond
+ ///@copydoc cds::intrusive::striped_set::rational_load_factor_resizing
+ template <size_t Numerator, size_t Denominator = 1>
+ using rational_load_factor_resizing = cds::intrusive::striped_set::rational_load_factor_resizing<Numerator, Denominator>;
+
+ ///@copydoc cds::intrusive::striped_set::single_bucket_size_threshold
+ template <size_t Threshold>
+ using single_bucket_size_threshold = cds::intrusive::striped_set::single_bucket_size_threshold<Threshold>;
+
+ ///@copydoc cds::intrusive::striped_set::no_resizing
+ typedef cds::intrusive::striped_set::no_resizing no_resizing;
+
+ ///@copydoc cds::intrusive::striped_set::striping
+ template <class Lock = std::mutex, class Alloc = CDS_DEFAULT_ALLOCATOR >
+ using striping = cds::intrusive::striped_set::striping<Lock, Alloc>;
+
+ ///@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<RecursiveLock, BackOff, Alloc >;
//@cond
namespace details {
}
template <typename Q, typename Func>
- std::pair<bool, bool> ensure( const Q& val, Func func )
+ std::pair<bool, bool> update( const Q& val, Func func, bool bAllowInsert )
{
- std::pair<iterator, bool> res = m_Set.insert( value_type(val) );
- func( res.second, const_cast<value_type&>(*res.first), val );
- return std::make_pair( true, res.second );
+ if ( bAllowInsert ) {
+ std::pair<iterator, bool> res = m_Set.insert( value_type(val) );
+ func( res.second, const_cast<value_type&>(*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<value_type&>(*it), val );
+ return std::make_pair( true, false );
+ }
}
template <typename Q, typename Func>
template <typename Q, typename Func>
bool insert( const Q& key, Func f )
{
- std::pair<iterator, bool> res = m_Map.insert( value_type( key, mapped_type() ) );
+ std::pair<iterator, bool> res = m_Map.insert( value_type( key_type( key ), mapped_type() ) );
if ( res.second )
f( *res.first );
return res.second;
template <typename Q, typename... Args>
bool emplace( Q&& key, Args&&... args )
{
- std::pair<iterator, bool> res = m_Map.emplace( std::forward<Q>(key), std::move( mapped_type( std::forward<Args>(args)...)));
+ std::pair<iterator, bool> res = m_Map.emplace( key_type( std::forward<Q>( key )), mapped_type( std::forward<Args>( args )...));
return res.second;
}
template <typename Q, typename Func>
- std::pair<bool, bool> ensure( const Q& val, Func func )
+ std::pair<bool, bool> update( const Q& key, Func func, bool bAllowInsert )
{
- std::pair<iterator, bool> 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<iterator, bool> 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 <typename Q, typename Func>
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 );
template <typename Q, typename Func>
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 );