-//$$CDS-header$$
+/*
+ This file is a part of libcds - Concurrent Data Structures library
-#ifndef __CDS_CONTAINER_MICHAEL_LIST_NOGC_H
-#define __CDS_CONTAINER_MICHAEL_LIST_NOGC_H
+ (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_LIST_NOGC_H
+#define CDSLIB_CONTAINER_MICHAEL_LIST_NOGC_H
#include <memory>
#include <cds/container/details/michael_list_base.h>
} // namespace details
//@endcond
- /// Michael's lock-free ordered single-linked list (template specialization for \pgc::nogc)
+ /// Michael's lock-free ordered single-linked list (template specialization for \p gc::nogc)
/** @ingroup cds_nonintrusive_list
\anchor cds_nonintrusive_MichaelList_nogc
See \ref cds_nonintrusive_MichaelList_gc "MichaelList" for description of template parameters.
*/
- template <typename T,
+ template <typename T,
#ifdef CDS_DOXYGEN_INVOKED
class Traits = michael_list::traits
#else
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 <typename... Options>
+ struct rebind_traits {
+ typedef MichaelList<
+ gc
+ , value_type
+ , typename cds::opt::make_options< traits, Options...>::type
+ > type;
+ };
+
+ // Stat selector
+ template <typename Stat>
+ using select_stat_wrapper = typename base_class::template select_stat_wrapper< Stat >;
+ //@endcond
protected:
//@cond
typedef typename maker::intrusive_traits::compare intrusive_key_comparator;
typedef typename base_class::atomic_node_ptr head_type;
- //@endcond
-
- protected:
- //@cond
- static node_type * alloc_node()
- {
- return cxx_allocator().New();
- }
-
- static node_type * alloc_node( value_type const& v )
- {
- return cxx_allocator().New( v );
- }
-
- template <typename... Args>
- static node_type * alloc_node( Args&&... args )
- {
- return cxx_allocator().MoveNew( std::forward<Args>(args)... );
- }
-
- static void free_node( node_type * pNode )
- {
- cxx_allocator().Delete( pNode );
- }
struct node_disposer {
void operator()( node_type * pNode )
free_node( pNode );
}
};
- 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;
- }
+ typedef std::unique_ptr< node_type, node_disposer > scoped_node_ptr;
//@endcond
protected:
//@endcond
public:
+ ///@name Forward iterators
+ //@{
/// Returns a forward iterator addressing the first element in a list
/**
For empty list \code begin() == end() \endcode
*/
iterator begin()
{
- return iterator( head() );
+ return iterator( head());
}
/// Returns an iterator that addresses the location succeeding the last element in a list
}
/// Returns a forward const iterator addressing the first element in a list
- //@{
const_iterator begin() const
{
- return const_iterator( head() );
+ return const_iterator( head());
}
- const_iterator cbegin()
+
+ /// 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();
}
- const_iterator cend()
- {
- return const_iterator();
- }
- //@}
- protected:
- //@cond
- iterator node_to_iterator( node_type * pNode )
+ /// Returns an const iterator that addresses the location succeeding the last element in a list
+ const_iterator cend() const
{
- if ( pNode )
- return iterator( *pNode );
- return end();
+ return const_iterator();
}
- //@endcond
+ //@}
public:
/// Default constructor
MichaelList()
{}
- /// List desctructor
+ //@cond
+ template <typename Stat, typename = std::enable_if<std::is_same<stat, michael_list::wrapped_stat<Stat>>::value >>
+ explicit MichaelList( Stat& st )
+ : base_class( st )
+ {}
+ //@endcond
+
+ /// List destructor
/**
Clears the list
*/
Return an iterator pointing to inserted item if success \ref end() otherwise
*/
template <typename Q>
- iterator insert( const Q& val )
+ iterator insert( Q&& val )
{
- return node_to_iterator( insert_at( head(), val ) );
+ return node_to_iterator( insert_at( head(), std::forward<Q>( val )));
}
- /// Ensures that the item \p val exists in the list
+ /// Updates the item
/**
- The operation inserts new item if the key \p val is not found in the list.
- Otherwise, the function returns an iterator that points to item found.
+ 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.
- Returns <tt> std::pair<iterator, bool> </tt> where \p first is an iterator pointing to
+ Returns <tt> std::pair<iterator, bool> </tt> where \p first is an iterator pointing to
item found or inserted, \p second is true if new item has been added or \p false if the item
already is in the list.
*/
template <typename Q>
- std::pair<iterator, bool> ensure( const Q& val )
+ std::pair<iterator, bool> update( Q&& key, bool bAllowInsert = true )
{
- std::pair< node_type *, bool > ret = ensure_at( head(), val );
+ std::pair< node_type *, bool > ret = update_at( head(), std::forward<Q>( key ), bAllowInsert );
return std::make_pair( node_to_iterator( ret.first ), ret.second );
}
+ //@cond
+ template <typename Q>
+ CDS_DEPRECATED("ensure() is deprecated, use update()")
+ std::pair<iterator, bool> ensure( const Q& val )
+ {
+ return update( val, true );
+ }
+ //@endcond
/// Inserts data of type \ref value_type constructed with <tt>std::forward<Args>(args)...</tt>
/**
return node_to_iterator( emplace_at( head(), std::forward<Args>(args)... ));
}
- /// Find the key \p key
- /** \anchor cds_nonintrusive_MichaelList_nogc_find_val
+ /// Checks whether the list contains \p key
+ /**
The function searches the item with key equal to \p key
and returns an iterator pointed to item found if the key is found,
- and \p end() otherwise
+ and \ref end() otherwise
*/
template <typename Q>
+ iterator contains( Q const& key )
+ {
+ return node_to_iterator( find_at( head(), key, intrusive_key_comparator()));
+ }
+ //@cond
+ template <typename Q>
+ CDS_DEPRECATED("deprecated, use contains()")
iterator find( Q const& key )
{
- return node_to_iterator( find_at( head(), key, intrusive_key_comparator() ));
+ return contains( key );
}
+ //@endcond
- /// Finds the key \p key using \p pred predicate for searching
+ /// Checks whether the map contains \p key using \p pred predicate for searching
/**
- The function is an analog of \ref cds_nonintrusive_MichaelList_nogc_find_val "find(Q const&)"
- but \p pred is used for key comparing.
+ The function is an analog of <tt>contains( key )</tt> but \p pred is used for key comparing.
\p Less functor has the interface like \p std::less.
- \p pred must imply the same element order as the comparator used for building the list.
+ \p Less must imply the same element order as the comparator used for building the list.
*/
template <typename Q, typename Less>
+ iterator contains( Q const& key, Less pred )
+ {
+ CDS_UNUSED( pred );
+ return node_to_iterator( find_at( head(), key, typename maker::template less_wrapper<Less>::type()) );
+ }
+ //@cond
+ template <typename Q, typename Less>
+ CDS_DEPRECATED("deprecated, use contains()")
iterator find_with( Q const& key, Less pred )
{
- return node_to_iterator( find_at( head(), key, typename maker::template less_wrapper<Less>::type() ) );
+ return contains( key, pred );
}
+ //@endcond
/// Check if the list is empty
bool empty() const
return base_class::size();
}
+ /// Returns const reference to internal statistics
+ stat const& statistics() const
+ {
+ return base_class::statistics();
+ }
+
/// Clears the list
void clear()
{
protected:
//@cond
+ static value_type& node_to_value( node_type& n )
+ {
+ return n.m_Value;
+ }
+
+ static node_type * alloc_node()
+ {
+ return cxx_allocator().New();
+ }
+
+ static node_type * alloc_node( value_type const& v )
+ {
+ return cxx_allocator().New( v );
+ }
+
+ template <typename... Args>
+ static node_type * alloc_node( Args&&... args )
+ {
+ return cxx_allocator().MoveNew( std::forward<Args>( 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();
+ }
+
+ iterator insert_node( node_type * pNode )
+ {
+ return node_to_iterator( insert_node_at( head(), pNode ));
+ }
+
node_type * insert_node_at( head_type& refHead, node_type * pNode )
{
assert( pNode != nullptr );
}
template <typename Q>
- node_type * insert_at( head_type& refHead, const Q& val )
+ node_type * insert_at( head_type& refHead, Q&& val )
{
- return insert_node_at( refHead, alloc_node( val ));
+ return insert_node_at( refHead, alloc_node( std::forward<Q>( val )));
}
template <typename Q>
- std::pair< node_type *, bool > ensure_at( head_type& refHead, const Q& val )
+ std::pair< node_type *, bool > update_at( head_type& refHead, Q&& val, bool bAllowInsert )
{
- scoped_node_ptr pNode( alloc_node( val ));
+ scoped_node_ptr pNode( alloc_node( std::forward<Q>( val )));
node_type * pItemFound = nullptr;
- std::pair<bool, bool> ret = base_class::ensure_at( refHead, *pNode, [&pItemFound](bool, node_type& item, node_type&) { pItemFound = &item; });
- assert( pItemFound != nullptr );
+ std::pair<bool, bool> ret = base_class::update_at( refHead, *pNode,
+ [&pItemFound](bool, node_type& item, node_type&) { pItemFound = &item; },
+ bAllowInsert );
- if ( ret.first && ret.second )
+ if ( ret.second )
pNode.release();
return std::make_pair( pItemFound, ret.second );
}
{
return base_class::find_at( refHead, key, cmp );
}
-
//@endcond
};
}} // namespace cds::container
-#endif // #ifndef __CDS_CONTAINER_MICHAEL_LIST_NOGC_H
+#endif // #ifndef CDSLIB_CONTAINER_MICHAEL_LIST_NOGC_H