3 #ifndef __CDS_CONTAINER_STRIPED_SET_STD_VECTOR_ADAPTER_H
4 #define __CDS_CONTAINER_STRIPED_SET_STD_VECTOR_ADAPTER_H
6 #include <cds/container/striped_set/adapter.h> // lower_bound
9 #include <algorithm> // std::lower_bound
10 #include <utility> // std::pair
13 namespace cds { namespace container {
14 namespace striped_set {
16 // Copy policy for std::vector
17 template <typename T, typename Alloc>
18 struct copy_item_policy< std::vector< T, Alloc > >
20 typedef std::vector< T, Alloc > vector_type;
21 typedef typename vector_type::iterator iterator;
23 void operator()( vector_type& vec, iterator itInsert, iterator itWhat )
25 vec.insert( itInsert, *itWhat );
29 // Swap policy for std::vector
30 template <typename T, typename Alloc>
31 struct swap_item_policy< std::vector< T, Alloc > >
33 typedef std::vector< T, Alloc > vector_type;
34 typedef typename vector_type::iterator iterator;
36 void operator()( vector_type& vec, iterator itInsert, iterator itWhat )
38 typename vector_type::value_type newVal;
39 itInsert = vec.insert( itInsert, newVal );
40 std::swap( *itInsert, *itWhat );
44 // Move policy for std::vector
45 template <typename T, typename Alloc>
46 struct move_item_policy< std::vector< T, Alloc > >
48 typedef std::vector< T, Alloc > vector_type;
49 typedef typename vector_type::iterator iterator;
51 void operator()( vector_type& vec, iterator itInsert, iterator itWhat )
53 vec.insert( itInsert, std::move( *itWhat ));
57 } // namespace striped_set
58 }} // namespace cds::container
60 namespace cds { namespace intrusive { namespace striped_set {
62 /// std::vector adapter for hash set bucket
63 template <typename T, class Alloc, CDS_SPEC_OPTIONS>
64 class adapt< std::vector<T, Alloc>, CDS_OPTIONS >
67 typedef std::vector<T, Alloc> container_type ; ///< underlying container type
70 /// Adapted container type
71 class adapted_container: public cds::container::striped_set::adapted_sequential_container
74 typedef typename container_type::value_type value_type ; ///< value type stored in the container
75 typedef typename container_type::iterator iterator ; ///< container iterator
76 typedef typename container_type::const_iterator const_iterator ; ///< container const iterator
78 static bool const has_find_with = true;
79 static bool const has_erase_with = true;
83 typedef typename cds::opt::details::make_comparator_from_option_list< value_type, CDS_OPTIONS >::type key_comparator;
85 typedef typename cds::opt::select<
86 typename cds::opt::value<
87 typename cds::opt::find_option<
88 cds::opt::copy_policy< cds::container::striped_set::move_item >
92 , cds::container::striped_set::copy_item, cds::container::striped_set::copy_item_policy<container_type>
93 , cds::container::striped_set::swap_item, cds::container::striped_set::swap_item_policy<container_type>
94 , cds::container::striped_set::move_item, cds::container::striped_set::move_item_policy<container_type>
99 bool operator()( value_type const& i1, value_type const& i2) const
101 return key_comparator()( i1, i2 ) < 0;
104 template <typename Q>
105 bool operator()( Q const& i1, value_type const& i2) const
107 return key_comparator()( i1, i2 ) < 0;
110 template <typename Q>
111 bool operator()( value_type const& i1, Q const& i2) const
113 return key_comparator()( i1, i2 ) < 0;
120 container_type m_Vector;
125 template <typename Q, typename Func>
126 bool insert( const Q& val, Func f )
128 iterator it = std::lower_bound( m_Vector.begin(), m_Vector.end(), val, find_predicate() );
129 if ( it == m_Vector.end() || key_comparator()( val, *it ) != 0 ) {
130 value_type newItem( val );
131 it = m_Vector.insert( it, newItem );
132 cds::unref( f )( *it );
138 # ifdef CDS_EMPLACE_SUPPORT
139 template <typename... Args>
140 bool emplace( Args&&... args )
142 #if CDS_COMPILER == CDS_COMPILER_MSVC && CDS_COMPILER_VERSION == CDS_COMPILER_MSVC12
143 // MS VC++ 2013 internal compiler error
144 // Use assignment workaround, see http://connect.microsoft.com/VisualStudio/feedback/details/804941/visual-studio-2013-rc-c-internal-compiler-error-with-std-forward
145 value_type val = value_type(std::forward<Args>(args)...);
147 value_type val( std::forward<Args>(args)... );
149 iterator it = std::lower_bound( m_Vector.begin(), m_Vector.end(), val, find_predicate() );
150 if ( it == m_Vector.end() || key_comparator()( val, *it ) != 0 ) {
151 it = m_Vector.emplace( it, std::move( val ) );
158 template <typename Q, typename Func>
159 std::pair<bool, bool> ensure( const Q& val, Func func )
161 iterator it = std::lower_bound( m_Vector.begin(), m_Vector.end(), val, find_predicate() );
162 if ( it == m_Vector.end() || key_comparator()( val, *it ) != 0 ) {
164 value_type newItem( val );
165 it = m_Vector.insert( it, newItem );
166 cds::unref( func )( true, *it, val );
167 return std::make_pair( true, true );
171 cds::unref( func )( false, *it, val );
172 return std::make_pair( true, false );
176 template <typename Q, typename Func>
177 bool erase( const Q& key, Func f )
179 iterator it = std::lower_bound( m_Vector.begin(), m_Vector.end(), key, find_predicate() );
180 if ( it == m_Vector.end() || key_comparator()( key, *it ) != 0 )
184 cds::unref( f )( *it );
185 m_Vector.erase( it );
189 template <typename Q, typename Less, typename Func>
190 bool erase( const Q& key, Less pred, Func f )
192 iterator it = std::lower_bound( m_Vector.begin(), m_Vector.end(), key, pred );
193 if ( it == m_Vector.end() || pred( key, *it ) || pred( *it, key ) )
197 cds::unref( f )( *it );
198 m_Vector.erase( it );
202 template <typename Q, typename Func>
203 bool find( Q& val, Func f )
205 iterator it = std::lower_bound( m_Vector.begin(), m_Vector.end(), val, find_predicate() );
206 if ( it == m_Vector.end() || key_comparator()( val, *it ) != 0 )
210 cds::unref( f )( *it, val );
214 template <typename Q, typename Less, typename Func>
215 bool find( Q& val, Less pred, Func f )
217 iterator it = std::lower_bound( m_Vector.begin(), m_Vector.end(), val, pred );
218 if ( it == m_Vector.end() || pred( val, *it ) || pred( *it, val ) )
222 cds::unref( f )( *it, val );
232 iterator begin() { return m_Vector.begin(); }
233 const_iterator begin() const { return m_Vector.begin(); }
234 iterator end() { return m_Vector.end(); }
235 const_iterator end() const { return m_Vector.end(); }
237 void move_item( adapted_container& /*from*/, iterator itWhat )
239 iterator it = std::lower_bound( m_Vector.begin(), m_Vector.end(), *itWhat, find_predicate() );
240 assert( it == m_Vector.end() || key_comparator()( *itWhat, *it ) != 0 );
242 copy_item()( m_Vector, it, itWhat );
247 return m_Vector.size();
252 typedef adapted_container type ; ///< Result of \p adapt metafunction
255 }}} // namespace cds::intrusive::striped_set
259 #endif // #ifndef __CDS_CONTAINER_STRIPED_SET_STD_VECTOR_ADAPTER_H