3 #ifndef __CDS_INTRUSIVE_STRIPED_SET_BOOST_UNORDERED_SET_ADAPTER_H
4 #define __CDS_INTRUSIVE_STRIPED_SET_BOOST_UNORDERED_SET_ADAPTER_H
6 #include <boost/intrusive/unordered_set.hpp>
7 #include <cds/intrusive/striped_set/adapter.h>
8 #include <cds/opt/buffer.h>
11 namespace cds { namespace intrusive { namespace striped_set {
13 template <typename T, CDS_BOOST_INTRUSIVE_DECL_OPTIONS10, CDS_SPEC_OPTIONS>
14 class adapt< boost::intrusive::unordered_set< T, CDS_BOOST_INTRUSIVE_OPTIONS10 >, CDS_OPTIONS >
17 typedef boost::intrusive::unordered_set< T, CDS_BOOST_INTRUSIVE_OPTIONS10 > container_type ; ///< underlying intrusive container type
20 class adapted_container
23 typedef typename container_type::value_type value_type ; ///< value type stored in the container
24 typedef typename container_type::iterator iterator ; ///< container iterator
25 typedef typename container_type::const_iterator const_iterator ; ///< container const iterator
27 typedef typename opt::value<
28 typename opt::find_option<
29 opt::buffer< opt::v::static_buffer< cds::any_type, 256 > >,
32 >::buffer initial_buffer_type;
33 typedef typename initial_buffer_type::template rebind< typename container_type::bucket_type >::other buffer_type;
34 typedef cds::intrusive::striped_set::load_factor_resizing<256> default_resizing_policy;
37 # ifndef CDS_CXX11_LAMBDA_SUPPORT
38 struct empty_insert_functor {
39 void operator()( value_type& )
44 template <typename Compare>
45 struct equal_from_compare
48 equal_from_compare( Compare& cmp )
52 equal_from_compare( equal_from_compare const& src )
56 template <typename A, typename B>
57 bool operator()( A& a, B& b ) const
59 return !m_cmp( a, b ) && !m_cmp( b, a );
62 template <typename A, typename B>
63 bool operator()( A& a, B& b )
65 return !m_cmp( a, b ) && !m_cmp( b, a );
69 buffer_type m_Buckets ; // buffer should be declared first since it is used in m_Set ctor.
74 : m_Set( typename container_type::bucket_traits( m_Buckets.buffer(), m_Buckets.capacity() ))
77 container_type& base_container()
82 template <typename Func>
83 bool insert( value_type& val, Func f )
85 std::pair<iterator, bool> res = m_Set.insert( val );
91 template <typename Func>
92 std::pair<bool, bool> ensure( value_type& val, Func f )
94 std::pair<iterator, bool> res = m_Set.insert( val );
95 cds::unref(f)( res.second, *res.first, val );
96 return std::make_pair( true, res.second );
99 bool unlink( value_type& val )
101 iterator it = m_Set.find( value_type(val) );
102 if ( it == m_Set.end() || &(*it) != &val )
108 template <typename Q, typename Func>
109 value_type * erase( Q const& key, Func f )
111 iterator it = m_Set.find( key, typename container_type::hasher(), typename container_type::key_equal() );
112 if ( it == m_Set.end() )
114 value_type& val = *it;
115 cds::unref(f)( val );
120 template <typename Q, typename Less, typename Func>
121 value_type * erase( Q const& key, Less pred, Func f )
123 iterator it = m_Set.find( key, typename container_type::hasher(), equal_from_compare<Less>(pred) );
124 if ( it == m_Set.end() )
126 value_type& val = *it;
127 cds::unref(f)( val );
132 template <typename Q, typename Func>
133 bool find( Q& key, Func f )
135 iterator it = m_Set.find( key, typename container_type::hasher(), typename container_type::key_equal() );
136 if ( it == m_Set.end() )
138 cds::unref(f)( *it, key );
142 template <typename Q, typename Less, typename Func>
143 bool find( Q& key, Less pred, Func f )
145 iterator it = m_Set.find( key, typename container_type::hasher(), equal_from_compare<Less>(pred) );
146 if ( it == m_Set.end() )
148 cds::unref(f)( *it, key );
157 template <typename Disposer>
158 void clear( Disposer disposer )
160 m_Set.clear_and_dispose( disposer );
163 iterator begin() { return m_Set.begin(); }
164 const_iterator begin() const { return m_Set.begin(); }
165 iterator end() { return m_Set.end(); }
166 const_iterator end() const { return m_Set.end(); }
170 return (size_t) m_Set.size();
173 void move_item( adapted_container& from, iterator itWhat )
175 value_type& val = *itWhat;
176 from.base_container().erase( itWhat );
177 # ifdef CDS_CXX11_LAMBDA_SUPPORT
178 insert( val, []( value_type& ) {} );
180 insert( val, empty_insert_functor() );
186 typedef adapted_container type ; ///< Result of the metafunction
189 }}} // namespace cds::intrusive::striped_set
192 #endif // #ifndef __CDS_INTRUSIVE_STRIPED_SET_BOOST_UNORDERED_SET_ADAPTER_H