3 #ifndef CDSLIB_CONTAINER_STRIPED_SET_STD_VECTOR_ADAPTER_H
4 #define CDSLIB_CONTAINER_STRIPED_SET_STD_VECTOR_ADAPTER_H
6 #include <functional> // ref
8 #include <algorithm> // std::lower_bound
9 #include <utility> // std::pair
10 #include <cds/container/striped_set/adapter.h> // lower_bound
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, typename... Options>
64 class adapt< std::vector<T, Alloc>, 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, 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 );
138 template <typename... Args>
139 bool emplace( Args&&... args )
141 #if CDS_COMPILER == CDS_COMPILER_MSVC && CDS_COMPILER_VERSION == CDS_COMPILER_MSVC12
142 // MS VC++ 2013 internal compiler error
143 // Use assignment workaround, see http://connect.microsoft.com/VisualStudio/feedback/details/804941/visual-studio-2013-rc-c-internal-compiler-error-with-std-forward
144 value_type val = value_type(std::forward<Args>(args)...);
146 value_type val( std::forward<Args>(args)... );
148 iterator it = std::lower_bound( m_Vector.begin(), m_Vector.end(), val, find_predicate() );
149 if ( it == m_Vector.end() || key_comparator()( val, *it ) != 0 ) {
150 it = m_Vector.emplace( it, std::move( val ) );
156 template <typename Q, typename Func>
157 std::pair<bool, bool> update( const Q& val, Func func, bool bAllowInsert )
159 iterator it = std::lower_bound( m_Vector.begin(), m_Vector.end(), val, find_predicate() );
160 if ( it == m_Vector.end() || key_comparator()( val, *it ) != 0 ) {
163 return std::make_pair( false, false );
165 value_type newItem( val );
166 it = m_Vector.insert( it, newItem );
167 func( true, *it, val );
168 return std::make_pair( true, true );
172 func( false, *it, val );
173 return std::make_pair( true, false );
177 template <typename Q, typename Func>
178 bool erase( const Q& key, Func f )
180 iterator it = std::lower_bound( m_Vector.begin(), m_Vector.end(), key, find_predicate() );
181 if ( it == m_Vector.end() || key_comparator()( key, *it ) != 0 )
186 m_Vector.erase( it );
190 template <typename Q, typename Less, typename Func>
191 bool erase( const Q& key, Less pred, Func f )
193 iterator it = std::lower_bound( m_Vector.begin(), m_Vector.end(), key, pred );
194 if ( it == m_Vector.end() || pred( key, *it ) || pred( *it, key ) )
199 m_Vector.erase( it );
203 template <typename Q, typename Func>
204 bool find( Q& val, Func f )
206 iterator it = std::lower_bound( m_Vector.begin(), m_Vector.end(), val, find_predicate() );
207 if ( it == m_Vector.end() || key_comparator()( val, *it ) != 0 )
215 template <typename Q, typename Less, typename Func>
216 bool find( Q& val, Less pred, Func f )
218 iterator it = std::lower_bound( m_Vector.begin(), m_Vector.end(), val, pred );
219 if ( it == m_Vector.end() || pred( val, *it ) || pred( *it, val ) )
233 iterator begin() { return m_Vector.begin(); }
234 const_iterator begin() const { return m_Vector.begin(); }
235 iterator end() { return m_Vector.end(); }
236 const_iterator end() const { return m_Vector.end(); }
238 void move_item( adapted_container& /*from*/, iterator itWhat )
240 iterator it = std::lower_bound( m_Vector.begin(), m_Vector.end(), *itWhat, find_predicate() );
241 assert( it == m_Vector.end() || key_comparator()( *itWhat, *it ) != 0 );
243 copy_item()( m_Vector, it, itWhat );
248 return m_Vector.size();
253 typedef adapted_container type ; ///< Result of \p adapt metafunction
256 }}} // namespace cds::intrusive::striped_set
260 #endif // #ifndef CDSLIB_CONTAINER_STRIPED_SET_STD_VECTOR_ADAPTER_H