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 {
14 template <class Set, typename... Options>
15 class adapt_boost_unordered_set
18 typedef Set container_type; ///< underlying intrusive container type
21 class adapted_container
24 typedef typename container_type::value_type value_type; ///< value type stored in the container
25 typedef typename container_type::iterator iterator; ///< container iterator
26 typedef typename container_type::const_iterator const_iterator; ///< container const iterator
28 typedef typename opt::value <
29 typename opt::find_option <
30 opt::buffer< opt::v::static_buffer< cds::any_type, 256 > >,
33 > ::buffer initial_buffer_type;
34 typedef typename initial_buffer_type::template rebind< typename container_type::bucket_type >::other buffer_type;
35 typedef cds::intrusive::striped_set::load_factor_resizing<256> default_resizing_policy;
38 template <typename Compare>
39 struct equal_from_compare
42 equal_from_compare( Compare& cmp )
46 equal_from_compare( equal_from_compare const& src )
50 template <typename A, typename B>
51 bool operator()( A& a, B& b ) const
53 return !m_cmp( a, b ) && !m_cmp( b, a );
56 template <typename A, typename B>
57 bool operator()( A& a, B& b )
59 return !m_cmp( a, b ) && !m_cmp( b, a );
63 buffer_type m_Buckets; // buffer should be declared first since it is used in m_Set ctor.
68 : m_Set( typename container_type::bucket_traits( m_Buckets.buffer(), m_Buckets.capacity() ) )
71 container_type& base_container()
76 template <typename Func>
77 bool insert( value_type& val, Func f )
79 std::pair<iterator, bool> res = m_Set.insert( val );
85 template <typename Func>
86 std::pair<bool, bool> ensure( value_type& val, Func f )
88 std::pair<iterator, bool> res = m_Set.insert( val );
89 f( res.second, *res.first, val );
90 return std::make_pair( true, res.second );
93 bool unlink( value_type& val )
95 iterator it = m_Set.find( value_type( val ) );
96 if ( it == m_Set.end() || &(*it) != &val )
102 template <typename Q, typename Func>
103 value_type * erase( Q const& key, Func f )
105 iterator it = m_Set.find( key, typename container_type::hasher(), typename container_type::key_equal() );
106 if ( it == m_Set.end() )
108 value_type& val = *it;
114 template <typename Q, typename Less, typename Func>
115 value_type * erase( Q const& key, Less pred, Func f )
117 iterator it = m_Set.find( key, typename container_type::hasher(), equal_from_compare<Less>( pred ) );
118 if ( it == m_Set.end() )
120 value_type& val = *it;
126 template <typename Q, typename Func>
127 bool find( Q& key, Func f )
129 iterator it = m_Set.find( key, typename container_type::hasher(), typename container_type::key_equal() );
130 if ( it == m_Set.end() )
136 template <typename Q, typename Less, typename Func>
137 bool find( Q& key, Less pred, Func f )
139 iterator it = m_Set.find( key, typename container_type::hasher(), equal_from_compare<Less>( pred ) );
140 if ( it == m_Set.end() )
151 template <typename Disposer>
152 void clear( Disposer disposer )
154 m_Set.clear_and_dispose( disposer );
157 iterator begin() { return m_Set.begin(); }
158 const_iterator begin() const { return m_Set.begin(); }
159 iterator end() { return m_Set.end(); }
160 const_iterator end() const { return m_Set.end(); }
164 return (size_t)m_Set.size();
167 void move_item( adapted_container& from, iterator itWhat )
169 value_type& val = *itWhat;
170 from.base_container().erase( itWhat );
171 insert( val, []( value_type& ) {} );
176 typedef adapted_container type; ///< Result of the metafunction
178 } // namespace details
180 #if CDS_COMPILER == CDS_COMPILER_INTEL && CDS_COMPILER_VERSION <= 1500
181 template <typename T,
182 typename O1, typename O2, typename O3, typename O4, typename O5,
183 typename O6, typename O7, typename O8, typename O9, typename O10,
186 class adapt < boost::intrusive::unordered_set< T, O1, O2, O3, O4, O5, O6, O7, O8, O9, O10 >, Options... >
187 : public details::adapt_boost_unordered_set < boost::intrusive::unordered_set< T, O1, O2, O3, O4, O5, O6, O7, O8, O9, O10 >, Options... >
190 template <typename T, typename... BIOptons, typename... Options>
191 class adapt < boost::intrusive::unordered_set< T, BIOptons... >, Options... >
192 : public details::adapt_boost_unordered_set < boost::intrusive::unordered_set< T, BIOptons... >, Options... >
196 }}} // namespace cds::intrusive::striped_set
199 #endif // #ifndef __CDS_INTRUSIVE_STRIPED_SET_BOOST_UNORDERED_SET_ADAPTER_H