| 1 | // |
| 2 | // Boost.Pointer Container |
| 3 | // |
| 4 | // Copyright Thorsten Ottosen 2003-2005. Use, modification and |
| 5 | // distribution is subject to the Boost Software License, Version |
| 6 | // 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
| 7 | // http://www.boost.org/LICENSE_1_0.txt) |
| 8 | // |
| 9 | // For more information, see http://www.boost.org/libs/ptr_container/ |
| 10 | // |
| 11 | |
| 12 | |
| 13 | #ifndef BOOST_PTR_CONTAINER_DETAIL_ASSOCIATIVE_PTR_CONTAINER_HPP |
| 14 | #define BOOST_PTR_CONTAINER_DETAIL_ASSOCIATIVE_PTR_CONTAINER_HPP |
| 15 | |
| 16 | #if defined(_MSC_VER) && (_MSC_VER >= 1200) |
| 17 | # pragma once |
| 18 | #endif |
| 19 | |
| 20 | #include <boost/ptr_container/detail/reversible_ptr_container.hpp> |
| 21 | #include <boost/ptr_container/detail/ptr_container_disable_deprecated.hpp> |
| 22 | |
| 23 | #if defined(BOOST_PTR_CONTAINER_DISABLE_DEPRECATED) |
| 24 | #pragma GCC diagnostic push |
| 25 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 26 | #endif |
| 27 | |
| 28 | namespace boost |
| 29 | { |
| 30 | |
| 31 | namespace ptr_container_detail |
| 32 | { |
| 33 | template |
| 34 | < |
| 35 | class Config, |
| 36 | class CloneAllocator |
| 37 | > |
| 38 | class associative_ptr_container : |
| 39 | public reversible_ptr_container<Config,CloneAllocator> |
| 40 | { |
| 41 | typedef reversible_ptr_container<Config,CloneAllocator> |
| 42 | base_type; |
| 43 | |
| 44 | typedef BOOST_DEDUCED_TYPENAME base_type::scoped_deleter |
| 45 | scoped_deleter; |
| 46 | |
| 47 | typedef BOOST_DEDUCED_TYPENAME Config::container_type |
| 48 | container_type; |
| 49 | public: // typedefs |
| 50 | typedef BOOST_DEDUCED_TYPENAME Config::key_type |
| 51 | key_type; |
| 52 | typedef BOOST_DEDUCED_TYPENAME Config::key_compare |
| 53 | key_compare; |
| 54 | typedef BOOST_DEDUCED_TYPENAME Config::value_compare |
| 55 | value_compare; |
| 56 | typedef BOOST_DEDUCED_TYPENAME Config::hasher |
| 57 | hasher; |
| 58 | typedef BOOST_DEDUCED_TYPENAME Config::key_equal |
| 59 | key_equal; |
| 60 | typedef BOOST_DEDUCED_TYPENAME Config::iterator |
| 61 | iterator; |
| 62 | typedef BOOST_DEDUCED_TYPENAME Config::const_iterator |
| 63 | const_iterator; |
| 64 | typedef BOOST_DEDUCED_TYPENAME Config::local_iterator |
| 65 | local_iterator; |
| 66 | typedef BOOST_DEDUCED_TYPENAME Config::const_local_iterator |
| 67 | const_local_iterator; |
| 68 | typedef BOOST_DEDUCED_TYPENAME base_type::size_type |
| 69 | size_type; |
| 70 | typedef BOOST_DEDUCED_TYPENAME base_type::reference |
| 71 | reference; |
| 72 | typedef BOOST_DEDUCED_TYPENAME base_type::const_reference |
| 73 | const_reference; |
| 74 | |
| 75 | public: // foundation |
| 76 | associative_ptr_container() |
| 77 | { } |
| 78 | |
| 79 | template< class SizeType > |
| 80 | associative_ptr_container( SizeType n, unordered_associative_container_tag tag ) |
| 81 | : base_type( n, tag ) |
| 82 | { } |
| 83 | |
| 84 | template< class Compare, class Allocator > |
| 85 | associative_ptr_container( const Compare& comp, |
| 86 | const Allocator& a ) |
| 87 | : base_type( comp, a, container_type() ) |
| 88 | { } |
| 89 | |
| 90 | template< class Hash, class Pred, class Allocator > |
| 91 | associative_ptr_container( const Hash& hash, |
| 92 | const Pred& pred, |
| 93 | const Allocator& a ) |
| 94 | : base_type( hash, pred, a ) |
| 95 | { } |
| 96 | |
| 97 | template< class InputIterator, class Compare, class Allocator > |
| 98 | associative_ptr_container( InputIterator first, InputIterator last, |
| 99 | const Compare& comp, |
| 100 | const Allocator& a ) |
| 101 | : base_type( first, last, comp, a, container_type() ) |
| 102 | { } |
| 103 | |
| 104 | template< class InputIterator, class Hash, class Pred, class Allocator > |
| 105 | associative_ptr_container( InputIterator first, InputIterator last, |
| 106 | const Hash& hash, |
| 107 | const Pred& pred, |
| 108 | const Allocator& a ) |
| 109 | : base_type( first, last, hash, pred, a ) |
| 110 | { } |
| 111 | |
| 112 | #ifndef BOOST_NO_AUTO_PTR |
| 113 | template< class PtrContainer > |
| 114 | explicit associative_ptr_container( std::auto_ptr<PtrContainer> r ) |
| 115 | : base_type( r ) |
| 116 | { } |
| 117 | #endif |
| 118 | #ifndef BOOST_NO_CXX11_SMART_PTR |
| 119 | template< class PtrContainer > |
| 120 | explicit associative_ptr_container( std::unique_ptr<PtrContainer> r ) |
| 121 | : base_type( std::move( r ) ) |
| 122 | { } |
| 123 | #endif |
| 124 | |
| 125 | associative_ptr_container( const associative_ptr_container& r ) |
| 126 | : base_type( r.begin(), r.end(), container_type() ) |
| 127 | { } |
| 128 | |
| 129 | template< class C, class V > |
| 130 | associative_ptr_container( const associative_ptr_container<C,V>& r ) |
| 131 | : base_type( r.begin(), r.end(), container_type() ) |
| 132 | { } |
| 133 | |
| 134 | #ifndef BOOST_NO_AUTO_PTR |
| 135 | template< class PtrContainer > |
| 136 | associative_ptr_container& operator=( std::auto_ptr<PtrContainer> r ) // nothrow |
| 137 | { |
| 138 | base_type::operator=( r ); |
| 139 | return *this; |
| 140 | } |
| 141 | #endif |
| 142 | #ifndef BOOST_NO_CXX11_SMART_PTR |
| 143 | template< class PtrContainer > |
| 144 | associative_ptr_container& operator=( std::unique_ptr<PtrContainer> r ) // nothrow |
| 145 | { |
| 146 | base_type::operator=( std::move( r ) ); |
| 147 | return *this; |
| 148 | } |
| 149 | #endif |
| 150 | |
| 151 | associative_ptr_container& operator=( associative_ptr_container r ) // strong |
| 152 | { |
| 153 | this->swap( r ); |
| 154 | return *this; |
| 155 | } |
| 156 | |
| 157 | public: // associative container interface |
| 158 | key_compare key_comp() const |
| 159 | { |
| 160 | return this->base().key_comp(); |
| 161 | } |
| 162 | |
| 163 | value_compare value_comp() const |
| 164 | { |
| 165 | return this->base().value_comp(); |
| 166 | } |
| 167 | |
| 168 | iterator erase( iterator before ) // nothrow |
| 169 | { |
| 170 | BOOST_ASSERT( !this->empty() ); |
| 171 | BOOST_ASSERT( before != this->end() ); |
| 172 | |
| 173 | this->remove( before ); // nothrow |
| 174 | iterator res( before ); // nothrow |
| 175 | ++res; // nothrow |
| 176 | this->base().erase( before.base() ); // nothrow |
| 177 | return res; // nothrow |
| 178 | } |
| 179 | |
| 180 | size_type erase( const key_type& x ) // nothrow |
| 181 | { |
| 182 | iterator i( this->base().find( x ) ); |
| 183 | // nothrow |
| 184 | if( i == this->end() ) // nothrow |
| 185 | return 0u; // nothrow |
| 186 | this->remove( i ); // nothrow |
| 187 | return this->base().erase( x ); // nothrow |
| 188 | } |
| 189 | |
| 190 | iterator erase( iterator first, |
| 191 | iterator last ) // nothrow |
| 192 | { |
| 193 | iterator res( last ); // nothrow |
| 194 | if( res != this->end() ) |
| 195 | ++res; // nothrow |
| 196 | |
| 197 | this->remove( first, last ); // nothrow |
| 198 | this->base().erase( first.base(), last.base() ); // nothrow |
| 199 | return res; // nothrow |
| 200 | } |
| 201 | |
| 202 | #if defined(BOOST_NO_SFINAE) || defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING) |
| 203 | #else |
| 204 | template< class Range > |
| 205 | BOOST_DEDUCED_TYPENAME boost::disable_if< boost::is_convertible<Range&,key_type&>, |
| 206 | iterator >::type |
| 207 | erase( const Range& r ) |
| 208 | { |
| 209 | return erase( boost::begin(r), boost::end(r) ); |
| 210 | } |
| 211 | |
| 212 | #endif |
| 213 | |
| 214 | protected: |
| 215 | |
| 216 | template< class AssociatePtrCont > |
| 217 | void multi_transfer( BOOST_DEDUCED_TYPENAME AssociatePtrCont::iterator object, |
| 218 | AssociatePtrCont& from ) // strong |
| 219 | { |
| 220 | BOOST_ASSERT( (void*)&from != (void*)this ); |
| 221 | BOOST_ASSERT( !from.empty() && "Cannot transfer from empty container" ); |
| 222 | |
| 223 | this->base().insert( *object.base() ); // strong |
| 224 | from.base().erase( object.base() ); // nothrow |
| 225 | } |
| 226 | |
| 227 | template< class AssociatePtrCont > |
| 228 | size_type multi_transfer( BOOST_DEDUCED_TYPENAME AssociatePtrCont::iterator first, |
| 229 | BOOST_DEDUCED_TYPENAME AssociatePtrCont::iterator last, |
| 230 | AssociatePtrCont& from ) // basic |
| 231 | { |
| 232 | BOOST_ASSERT( (void*)&from != (void*)this ); |
| 233 | |
| 234 | size_type res = 0; |
| 235 | for( ; first != last; ) |
| 236 | { |
| 237 | BOOST_ASSERT( first != from.end() ); |
| 238 | this->base().insert( *first.base() ); // strong |
| 239 | BOOST_DEDUCED_TYPENAME AssociatePtrCont::iterator |
| 240 | to_delete( first ); |
| 241 | ++first; |
| 242 | from.base().erase( to_delete.base() ); // nothrow |
| 243 | ++res; |
| 244 | } |
| 245 | |
| 246 | return res; |
| 247 | } |
| 248 | |
| 249 | template< class AssociatePtrCont > |
| 250 | bool single_transfer( BOOST_DEDUCED_TYPENAME AssociatePtrCont::iterator object, |
| 251 | AssociatePtrCont& from ) // strong |
| 252 | { |
| 253 | BOOST_ASSERT( (void*)&from != (void*)this ); |
| 254 | BOOST_ASSERT( !from.empty() && "Cannot transfer from empty container" ); |
| 255 | |
| 256 | std::pair<BOOST_DEDUCED_TYPENAME base_type::ptr_iterator,bool> p = |
| 257 | this->base().insert( *object.base() ); // strong |
| 258 | if( p.second ) |
| 259 | from.base().erase( object.base() ); // nothrow |
| 260 | |
| 261 | return p.second; |
| 262 | } |
| 263 | |
| 264 | template< class AssociatePtrCont > |
| 265 | size_type single_transfer( BOOST_DEDUCED_TYPENAME AssociatePtrCont::iterator first, |
| 266 | BOOST_DEDUCED_TYPENAME AssociatePtrCont::iterator last, |
| 267 | AssociatePtrCont& from ) // basic |
| 268 | { |
| 269 | BOOST_ASSERT( (void*)&from != (void*)this ); |
| 270 | |
| 271 | size_type res = 0; |
| 272 | for( ; first != last; ) |
| 273 | { |
| 274 | BOOST_ASSERT( first != from.end() ); |
| 275 | std::pair<BOOST_DEDUCED_TYPENAME base_type::ptr_iterator,bool> p = |
| 276 | this->base().insert( *first.base() ); // strong |
| 277 | BOOST_DEDUCED_TYPENAME AssociatePtrCont::iterator |
| 278 | to_delete( first ); |
| 279 | ++first; |
| 280 | if( p.second ) |
| 281 | { |
| 282 | from.base().erase( to_delete.base() ); // nothrow |
| 283 | ++res; |
| 284 | } |
| 285 | } |
| 286 | return res; |
| 287 | } |
| 288 | |
| 289 | reference front() |
| 290 | { |
| 291 | BOOST_ASSERT( !this->empty() ); |
| 292 | BOOST_ASSERT( *this->begin().base() != 0 ); |
| 293 | return *this->begin(); |
| 294 | } |
| 295 | |
| 296 | const_reference front() const |
| 297 | { |
| 298 | return const_cast<associative_ptr_container*>(this)->front(); |
| 299 | } |
| 300 | |
| 301 | reference back() |
| 302 | { |
| 303 | BOOST_ASSERT( !this->empty() ); |
| 304 | BOOST_ASSERT( *(--this->end()).base() != 0 ); |
| 305 | return *--this->end(); |
| 306 | } |
| 307 | |
| 308 | const_reference back() const |
| 309 | { |
| 310 | return const_cast<associative_ptr_container*>(this)->back(); |
| 311 | } |
| 312 | |
| 313 | protected: // unordered interface |
| 314 | hasher hash_function() const |
| 315 | { |
| 316 | return this->base().hash_function(); |
| 317 | } |
| 318 | |
| 319 | key_equal key_eq() const |
| 320 | { |
| 321 | return this->base().key_eq(); |
| 322 | } |
| 323 | |
| 324 | size_type bucket_count() const |
| 325 | { |
| 326 | return this->base().bucket_count(); |
| 327 | } |
| 328 | |
| 329 | size_type max_bucket_count() const |
| 330 | { |
| 331 | return this->base().max_bucket_count(); |
| 332 | } |
| 333 | |
| 334 | size_type bucket_size( size_type n ) const |
| 335 | { |
| 336 | return this->base().bucket_size( n ); |
| 337 | } |
| 338 | |
| 339 | float load_factor() const |
| 340 | { |
| 341 | return this->base().load_factor(); |
| 342 | } |
| 343 | |
| 344 | float max_load_factor() const |
| 345 | { |
| 346 | return this->base().max_load_factor(); |
| 347 | } |
| 348 | |
| 349 | void max_load_factor( float factor ) |
| 350 | { |
| 351 | return this->base().max_load_factor( factor ); |
| 352 | } |
| 353 | |
| 354 | void rehash( size_type n ) |
| 355 | { |
| 356 | this->base().rehash( n ); |
| 357 | } |
| 358 | |
| 359 | public: |
| 360 | #if BOOST_WORKAROUND(__DECCXX_VER, BOOST_TESTED_AT(70190006)) |
| 361 | iterator begin() |
| 362 | { |
| 363 | return base_type::begin(); |
| 364 | } |
| 365 | |
| 366 | const_iterator begin() const |
| 367 | { |
| 368 | return base_type::begin(); |
| 369 | } |
| 370 | |
| 371 | iterator end() |
| 372 | { |
| 373 | return base_type::end(); |
| 374 | } |
| 375 | |
| 376 | const_iterator end() const |
| 377 | { |
| 378 | return base_type::end(); |
| 379 | } |
| 380 | |
| 381 | const_iterator cbegin() const |
| 382 | { |
| 383 | return base_type::cbegin(); |
| 384 | } |
| 385 | |
| 386 | const_iterator cend() const |
| 387 | { |
| 388 | return base_type::cend(); |
| 389 | } |
| 390 | #else |
| 391 | using base_type::begin; |
| 392 | using base_type::end; |
| 393 | using base_type::cbegin; |
| 394 | using base_type::cend; |
| 395 | #endif |
| 396 | |
| 397 | protected: |
| 398 | local_iterator begin( size_type n ) |
| 399 | { |
| 400 | return local_iterator( this->base().begin( n ) ); |
| 401 | } |
| 402 | |
| 403 | const_local_iterator begin( size_type n ) const |
| 404 | { |
| 405 | return const_local_iterator( this->base().begin( n ) ); |
| 406 | } |
| 407 | |
| 408 | local_iterator end( size_type n ) |
| 409 | { |
| 410 | return local_iterator( this->base().end( n ) ); |
| 411 | } |
| 412 | |
| 413 | const_local_iterator end( size_type n ) const |
| 414 | { |
| 415 | return const_local_iterator( this->base().end( n ) ); |
| 416 | } |
| 417 | |
| 418 | const_local_iterator cbegin( size_type n ) const |
| 419 | { |
| 420 | return const_local_iterator( this->base().cbegin( n ) ); |
| 421 | } |
| 422 | |
| 423 | const_local_iterator cend( size_type n ) |
| 424 | { |
| 425 | return const_local_iterator( this->base().cend( n ) ); |
| 426 | } |
| 427 | |
| 428 | }; // class 'associative_ptr_container' |
| 429 | |
| 430 | } // namespace 'ptr_container_detail' |
| 431 | |
| 432 | } // namespace 'boost' |
| 433 | |
| 434 | #if defined(BOOST_PTR_CONTAINER_DISABLE_DEPRECATED) |
| 435 | #pragma GCC diagnostic pop |
| 436 | #endif |
| 437 | |
| 438 | #endif |
| 439 | |