2 This file is a part of libcds - Concurrent Data Structures library
4 (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017
6 Source code repo: http://github.com/khizmax/libcds/
7 Download: http://sourceforge.net/projects/libcds/files/
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions are met:
12 * Redistributions of source code must retain the above copyright notice, this
13 list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 this list of conditions and the following disclaimer in the documentation
17 and/or other materials provided with the distribution.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #ifndef CDSLIB_CONTAINER_STRIPED_SET_STD_VECTOR_ADAPTER_H
32 #define CDSLIB_CONTAINER_STRIPED_SET_STD_VECTOR_ADAPTER_H
34 #include <functional> // ref
36 #include <algorithm> // std::lower_bound
37 #include <utility> // std::pair
38 #include <cds/container/striped_set/adapter.h> // lower_bound
41 namespace cds { namespace container {
42 namespace striped_set {
44 // Copy policy for std::vector
45 template <typename T, typename Alloc>
46 struct copy_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, *itWhat );
57 // Swap policy for std::vector
58 template <typename T, typename Alloc>
59 struct swap_item_policy< std::vector< T, Alloc > >
61 typedef std::vector< T, Alloc > vector_type;
62 typedef typename vector_type::iterator iterator;
64 void operator()( vector_type& vec, iterator itInsert, iterator itWhat )
66 typename vector_type::value_type newVal;
67 itInsert = vec.insert( itInsert, newVal );
68 std::swap( *itInsert, *itWhat );
72 // Move policy for std::vector
73 template <typename T, typename Alloc>
74 struct move_item_policy< std::vector< T, Alloc > >
76 typedef std::vector< T, Alloc > vector_type;
77 typedef typename vector_type::iterator iterator;
79 void operator()( vector_type& vec, iterator itInsert, iterator itWhat )
81 vec.insert( itInsert, std::move( *itWhat ));
85 } // namespace striped_set
86 }} // namespace cds::container
88 namespace cds { namespace intrusive { namespace striped_set {
90 /// std::vector adapter for hash set bucket
91 template <typename T, class Alloc, typename... Options>
92 class adapt< std::vector<T, Alloc>, Options... >
95 typedef std::vector<T, Alloc> container_type ; ///< underlying container type
98 /// Adapted container type
99 class adapted_container: public cds::container::striped_set::adapted_sequential_container
102 typedef typename container_type::value_type value_type ; ///< value type stored in the container
103 typedef typename container_type::iterator iterator ; ///< container iterator
104 typedef typename container_type::const_iterator const_iterator ; ///< container const iterator
106 static bool const has_find_with = true;
107 static bool const has_erase_with = true;
111 typedef typename cds::opt::details::make_comparator_from_option_list< value_type, Options... >::type key_comparator;
113 typedef typename cds::opt::select<
114 typename cds::opt::value<
115 typename cds::opt::find_option<
116 cds::opt::copy_policy< cds::container::striped_set::move_item >
120 , cds::container::striped_set::copy_item, cds::container::striped_set::copy_item_policy<container_type>
121 , cds::container::striped_set::swap_item, cds::container::striped_set::swap_item_policy<container_type>
122 , cds::container::striped_set::move_item, cds::container::striped_set::move_item_policy<container_type>
125 struct find_predicate
127 bool operator()( value_type const& i1, value_type const& i2) const
129 return key_comparator()( i1, i2 ) < 0;
132 template <typename Q>
133 bool operator()( Q const& i1, value_type const& i2) const
135 return key_comparator()( i1, i2 ) < 0;
138 template <typename Q>
139 bool operator()( value_type const& i1, Q const& i2) const
141 return key_comparator()( i1, i2 ) < 0;
148 container_type m_Vector;
153 template <typename Q, typename Func>
154 bool insert( const Q& val, Func f )
156 iterator it = std::lower_bound( m_Vector.begin(), m_Vector.end(), val, find_predicate());
157 if ( it == m_Vector.end() || key_comparator()( val, *it ) != 0 ) {
158 value_type newItem( val );
159 it = m_Vector.insert( it, newItem );
166 template <typename... Args>
167 bool emplace( Args&&... args )
169 value_type val( std::forward<Args>(args)... );
170 iterator it = std::lower_bound( m_Vector.begin(), m_Vector.end(), val, find_predicate());
171 if ( it == m_Vector.end() || key_comparator()( val, *it ) != 0 ) {
172 it = m_Vector.emplace( it, std::move( val ));
178 template <typename Q, typename Func>
179 std::pair<bool, bool> update( const Q& val, Func func, bool bAllowInsert )
181 iterator it = std::lower_bound( m_Vector.begin(), m_Vector.end(), val, find_predicate());
182 if ( it == m_Vector.end() || key_comparator()( val, *it ) != 0 ) {
185 return std::make_pair( false, false );
187 value_type newItem( val );
188 it = m_Vector.insert( it, newItem );
189 func( true, *it, val );
190 return std::make_pair( true, true );
194 func( false, *it, val );
195 return std::make_pair( true, false );
199 template <typename Q, typename Func>
200 bool erase( const Q& key, Func f )
202 iterator it = std::lower_bound( m_Vector.begin(), m_Vector.end(), key, find_predicate());
203 if ( it == m_Vector.end() || key_comparator()( key, *it ) != 0 )
208 m_Vector.erase( it );
212 template <typename Q, typename Less, typename Func>
213 bool erase( const Q& key, Less pred, Func f )
215 iterator it = std::lower_bound( m_Vector.begin(), m_Vector.end(), key, pred );
216 if ( it == m_Vector.end() || pred( key, *it ) || pred( *it, key ))
221 m_Vector.erase( it );
225 template <typename Q, typename Func>
226 bool find( Q& val, Func f )
228 iterator it = std::lower_bound( m_Vector.begin(), m_Vector.end(), val, find_predicate());
229 if ( it == m_Vector.end() || key_comparator()( val, *it ) != 0 )
237 template <typename Q, typename Less, typename Func>
238 bool find( Q& val, Less pred, Func f )
240 iterator it = std::lower_bound( m_Vector.begin(), m_Vector.end(), val, pred );
241 if ( it == m_Vector.end() || pred( val, *it ) || pred( *it, val ))
255 iterator begin() { return m_Vector.begin(); }
256 const_iterator begin() const { return m_Vector.begin(); }
257 iterator end() { return m_Vector.end(); }
258 const_iterator end() const { return m_Vector.end(); }
260 void move_item( adapted_container& /*from*/, iterator itWhat )
262 iterator it = std::lower_bound( m_Vector.begin(), m_Vector.end(), *itWhat, find_predicate());
263 assert( it == m_Vector.end() || key_comparator()( *itWhat, *it ) != 0 );
265 copy_item()( m_Vector, it, itWhat );
270 return m_Vector.size();
275 typedef adapted_container type ; ///< Result of \p adapt metafunction
278 }}} // namespace cds::intrusive::striped_set
282 #endif // #ifndef CDSLIB_CONTAINER_STRIPED_SET_STD_VECTOR_ADAPTER_H