Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 1 | |
| 2 | // Copyright (C) 2003-2004 Jeremy B. Maitin-Shepard. |
| 3 | // Copyright (C) 2005-2011 Daniel James |
| 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 7 | #ifndef NDNBOOST_UNORDERED_DETAIL_ALL_HPP_INCLUDED |
| 8 | #define NDNBOOST_UNORDERED_DETAIL_ALL_HPP_INCLUDED |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 9 | |
| 10 | #include <ndnboost/unordered/detail/buckets.hpp> |
| 11 | #include <ndnboost/unordered/detail/util.hpp> |
| 12 | #include <ndnboost/type_traits/aligned_storage.hpp> |
| 13 | #include <ndnboost/type_traits/alignment_of.hpp> |
| 14 | #include <cmath> |
| 15 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 16 | #if defined(NDNBOOST_MSVC) |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 17 | #pragma warning(push) |
| 18 | #pragma warning(disable:4127) // conditional expression is constant |
| 19 | #endif |
| 20 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 21 | #if defined(NDNBOOST_UNORDERED_DEPRECATED_EQUALITY) |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 22 | |
| 23 | #if defined(__EDG__) |
| 24 | #elif defined(_MSC_VER) || defined(__BORLANDC__) || defined(__DMC__) |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 25 | #pragma message("Warning: NDNBOOST_UNORDERED_DEPRECATED_EQUALITY is no longer supported.") |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 26 | #elif defined(__GNUC__) || defined(__HP_aCC) || \ |
| 27 | defined(__SUNPRO_CC) || defined(__IBMCPP__) |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 28 | #warning "NDNBOOST_UNORDERED_DEPRECATED_EQUALITY is no longer supported." |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 29 | #endif |
| 30 | |
| 31 | #endif |
| 32 | |
| 33 | namespace ndnboost { namespace unordered { namespace detail { |
| 34 | |
| 35 | //////////////////////////////////////////////////////////////////////////// |
| 36 | // convert double to std::size_t |
| 37 | |
| 38 | inline std::size_t double_to_size(double f) |
| 39 | { |
| 40 | return f >= static_cast<double>( |
| 41 | (std::numeric_limits<std::size_t>::max)()) ? |
| 42 | (std::numeric_limits<std::size_t>::max)() : |
| 43 | static_cast<std::size_t>(f); |
| 44 | } |
| 45 | |
| 46 | // The space used to store values in a node. |
| 47 | |
| 48 | template <typename ValueType> |
| 49 | struct value_base |
| 50 | { |
| 51 | typedef ValueType value_type; |
| 52 | |
| 53 | typename ndnboost::aligned_storage< |
| 54 | sizeof(value_type), |
| 55 | ndnboost::alignment_of<value_type>::value>::type data_; |
| 56 | |
| 57 | void* address() { |
| 58 | return this; |
| 59 | } |
| 60 | |
| 61 | value_type& value() { |
| 62 | return *(ValueType*) this; |
| 63 | } |
| 64 | |
| 65 | value_type* value_ptr() { |
| 66 | return (ValueType*) this; |
| 67 | } |
| 68 | |
| 69 | private: |
| 70 | |
| 71 | value_base& operator=(value_base const&); |
| 72 | }; |
| 73 | |
| 74 | template <typename NodeAlloc> |
| 75 | struct copy_nodes |
| 76 | { |
| 77 | typedef ndnboost::unordered::detail::allocator_traits<NodeAlloc> |
| 78 | node_allocator_traits; |
| 79 | |
| 80 | node_constructor<NodeAlloc> constructor; |
| 81 | |
| 82 | explicit copy_nodes(NodeAlloc& a) : constructor(a) {} |
| 83 | |
| 84 | typename node_allocator_traits::pointer create( |
| 85 | typename node_allocator_traits::value_type::value_type const& v) |
| 86 | { |
| 87 | constructor.construct_with_value2(v); |
| 88 | return constructor.release(); |
| 89 | } |
| 90 | }; |
| 91 | |
| 92 | template <typename NodeAlloc> |
| 93 | struct move_nodes |
| 94 | { |
| 95 | typedef ndnboost::unordered::detail::allocator_traits<NodeAlloc> |
| 96 | node_allocator_traits; |
| 97 | |
| 98 | node_constructor<NodeAlloc> constructor; |
| 99 | |
| 100 | explicit move_nodes(NodeAlloc& a) : constructor(a) {} |
| 101 | |
| 102 | typename node_allocator_traits::pointer create( |
| 103 | typename node_allocator_traits::value_type::value_type& v) |
| 104 | { |
| 105 | constructor.construct_with_value2(ndnboost::move(v)); |
| 106 | return constructor.release(); |
| 107 | } |
| 108 | }; |
| 109 | |
| 110 | template <typename Buckets> |
| 111 | struct assign_nodes |
| 112 | { |
| 113 | node_holder<typename Buckets::node_allocator> holder; |
| 114 | |
| 115 | explicit assign_nodes(Buckets& b) : holder(b) {} |
| 116 | |
| 117 | typename Buckets::node_pointer create( |
| 118 | typename Buckets::value_type const& v) |
| 119 | { |
| 120 | return holder.copy_of(v); |
| 121 | } |
| 122 | }; |
| 123 | |
| 124 | template <typename Buckets> |
| 125 | struct move_assign_nodes |
| 126 | { |
| 127 | node_holder<typename Buckets::node_allocator> holder; |
| 128 | |
| 129 | explicit move_assign_nodes(Buckets& b) : holder(b) {} |
| 130 | |
| 131 | typename Buckets::node_pointer create( |
| 132 | typename Buckets::value_type& v) |
| 133 | { |
| 134 | return holder.move_copy_of(v); |
| 135 | } |
| 136 | }; |
| 137 | |
| 138 | template <typename Types> |
| 139 | struct table : |
| 140 | ndnboost::unordered::detail::functions< |
| 141 | typename Types::hasher, |
| 142 | typename Types::key_equal> |
| 143 | { |
| 144 | private: |
| 145 | table(table const&); |
| 146 | table& operator=(table const&); |
| 147 | public: |
| 148 | typedef typename Types::node node; |
| 149 | typedef typename Types::bucket bucket; |
| 150 | typedef typename Types::hasher hasher; |
| 151 | typedef typename Types::key_equal key_equal; |
| 152 | typedef typename Types::key_type key_type; |
| 153 | typedef typename Types::extractor extractor; |
| 154 | typedef typename Types::value_type value_type; |
| 155 | typedef typename Types::table table_impl; |
| 156 | typedef typename Types::link_pointer link_pointer; |
| 157 | typedef typename Types::policy policy; |
| 158 | |
| 159 | typedef ndnboost::unordered::detail::functions< |
| 160 | typename Types::hasher, |
| 161 | typename Types::key_equal> functions; |
| 162 | typedef typename functions::set_hash_functions set_hash_functions; |
| 163 | |
| 164 | typedef typename Types::allocator allocator; |
| 165 | typedef typename ndnboost::unordered::detail:: |
| 166 | rebind_wrap<allocator, node>::type node_allocator; |
| 167 | typedef typename ndnboost::unordered::detail:: |
| 168 | rebind_wrap<allocator, bucket>::type bucket_allocator; |
| 169 | typedef ndnboost::unordered::detail::allocator_traits<node_allocator> |
| 170 | node_allocator_traits; |
| 171 | typedef ndnboost::unordered::detail::allocator_traits<bucket_allocator> |
| 172 | bucket_allocator_traits; |
| 173 | typedef typename node_allocator_traits::pointer |
| 174 | node_pointer; |
| 175 | typedef typename node_allocator_traits::const_pointer |
| 176 | const_node_pointer; |
| 177 | typedef typename bucket_allocator_traits::pointer |
| 178 | bucket_pointer; |
| 179 | typedef ndnboost::unordered::detail::node_constructor<node_allocator> |
| 180 | node_constructor; |
| 181 | |
| 182 | typedef ndnboost::unordered::iterator_detail:: |
| 183 | iterator<node> iterator; |
| 184 | typedef ndnboost::unordered::iterator_detail:: |
| 185 | c_iterator<node, const_node_pointer> c_iterator; |
| 186 | typedef ndnboost::unordered::iterator_detail:: |
| 187 | l_iterator<node, policy> l_iterator; |
| 188 | typedef ndnboost::unordered::iterator_detail:: |
| 189 | cl_iterator<node, const_node_pointer, policy> cl_iterator; |
| 190 | |
| 191 | //////////////////////////////////////////////////////////////////////// |
| 192 | // Members |
| 193 | |
| 194 | ndnboost::unordered::detail::compressed<bucket_allocator, node_allocator> |
| 195 | allocators_; |
| 196 | std::size_t bucket_count_; |
| 197 | std::size_t size_; |
| 198 | float mlf_; |
| 199 | std::size_t max_load_; |
| 200 | bucket_pointer buckets_; |
| 201 | |
| 202 | //////////////////////////////////////////////////////////////////////// |
| 203 | // Data access |
| 204 | |
| 205 | bucket_allocator const& bucket_alloc() const |
| 206 | { |
| 207 | return allocators_.first(); |
| 208 | } |
| 209 | |
| 210 | node_allocator const& node_alloc() const |
| 211 | { |
| 212 | return allocators_.second(); |
| 213 | } |
| 214 | |
| 215 | bucket_allocator& bucket_alloc() |
| 216 | { |
| 217 | return allocators_.first(); |
| 218 | } |
| 219 | |
| 220 | node_allocator& node_alloc() |
| 221 | { |
| 222 | return allocators_.second(); |
| 223 | } |
| 224 | |
| 225 | std::size_t max_bucket_count() const |
| 226 | { |
| 227 | // -1 to account for the start bucket. |
| 228 | return policy::prev_bucket_count( |
| 229 | bucket_allocator_traits::max_size(bucket_alloc()) - 1); |
| 230 | } |
| 231 | |
| 232 | bucket_pointer get_bucket(std::size_t bucket_index) const |
| 233 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 234 | NDNBOOST_ASSERT(buckets_); |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 235 | return buckets_ + static_cast<std::ptrdiff_t>(bucket_index); |
| 236 | } |
| 237 | |
| 238 | link_pointer get_previous_start() const |
| 239 | { |
| 240 | return get_bucket(bucket_count_)->first_from_start(); |
| 241 | } |
| 242 | |
| 243 | link_pointer get_previous_start(std::size_t bucket_index) const |
| 244 | { |
| 245 | return get_bucket(bucket_index)->next_; |
| 246 | } |
| 247 | |
| 248 | iterator begin() const |
| 249 | { |
| 250 | return size_ ? iterator(get_previous_start()->next_) : iterator(); |
| 251 | } |
| 252 | |
| 253 | iterator begin(std::size_t bucket_index) const |
| 254 | { |
| 255 | if (!size_) return iterator(); |
| 256 | link_pointer prev = get_previous_start(bucket_index); |
| 257 | return prev ? iterator(prev->next_) : iterator(); |
| 258 | } |
| 259 | |
| 260 | std::size_t hash_to_bucket(std::size_t hash) const |
| 261 | { |
| 262 | return policy::to_bucket(bucket_count_, hash); |
| 263 | } |
| 264 | |
| 265 | float load_factor() const |
| 266 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 267 | NDNBOOST_ASSERT(bucket_count_ != 0); |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 268 | return static_cast<float>(size_) |
| 269 | / static_cast<float>(bucket_count_); |
| 270 | } |
| 271 | |
| 272 | std::size_t bucket_size(std::size_t index) const |
| 273 | { |
| 274 | iterator it = begin(index); |
| 275 | if (!it.node_) return 0; |
| 276 | |
| 277 | std::size_t count = 0; |
| 278 | while(it.node_ && hash_to_bucket(it.node_->hash_) == index) |
| 279 | { |
| 280 | ++count; |
| 281 | ++it; |
| 282 | } |
| 283 | |
| 284 | return count; |
| 285 | } |
| 286 | |
| 287 | //////////////////////////////////////////////////////////////////////// |
| 288 | // Load methods |
| 289 | |
| 290 | std::size_t max_size() const |
| 291 | { |
| 292 | using namespace std; |
| 293 | |
| 294 | // size < mlf_ * count |
| 295 | return ndnboost::unordered::detail::double_to_size(ceil( |
| 296 | static_cast<double>(mlf_) * |
| 297 | static_cast<double>(max_bucket_count()) |
| 298 | )) - 1; |
| 299 | } |
| 300 | |
| 301 | void recalculate_max_load() |
| 302 | { |
| 303 | using namespace std; |
| 304 | |
| 305 | // From 6.3.1/13: |
| 306 | // Only resize when size >= mlf_ * count |
| 307 | max_load_ = buckets_ ? ndnboost::unordered::detail::double_to_size(ceil( |
| 308 | static_cast<double>(mlf_) * |
| 309 | static_cast<double>(bucket_count_) |
| 310 | )) : 0; |
| 311 | |
| 312 | } |
| 313 | |
| 314 | void max_load_factor(float z) |
| 315 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 316 | NDNBOOST_ASSERT(z > 0); |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 317 | mlf_ = (std::max)(z, minimum_max_load_factor); |
| 318 | recalculate_max_load(); |
| 319 | } |
| 320 | |
| 321 | std::size_t min_buckets_for_size(std::size_t size) const |
| 322 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 323 | NDNBOOST_ASSERT(mlf_ >= minimum_max_load_factor); |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 324 | |
| 325 | using namespace std; |
| 326 | |
| 327 | // From 6.3.1/13: |
| 328 | // size < mlf_ * count |
| 329 | // => count > size / mlf_ |
| 330 | // |
| 331 | // Or from rehash post-condition: |
| 332 | // count > size / mlf_ |
| 333 | |
| 334 | return policy::new_bucket_count( |
| 335 | ndnboost::unordered::detail::double_to_size(floor( |
| 336 | static_cast<double>(size) / |
| 337 | static_cast<double>(mlf_))) + 1); |
| 338 | } |
| 339 | |
| 340 | //////////////////////////////////////////////////////////////////////// |
| 341 | // Constructors |
| 342 | |
| 343 | table(std::size_t num_buckets, |
| 344 | hasher const& hf, |
| 345 | key_equal const& eq, |
| 346 | node_allocator const& a) : |
| 347 | functions(hf, eq), |
| 348 | allocators_(a,a), |
| 349 | bucket_count_(policy::new_bucket_count(num_buckets)), |
| 350 | size_(0), |
| 351 | mlf_(1.0f), |
| 352 | max_load_(0), |
| 353 | buckets_() |
| 354 | {} |
| 355 | |
| 356 | table(table const& x, node_allocator const& a) : |
| 357 | functions(x), |
| 358 | allocators_(a,a), |
| 359 | bucket_count_(x.min_buckets_for_size(x.size_)), |
| 360 | size_(0), |
| 361 | mlf_(x.mlf_), |
| 362 | max_load_(0), |
| 363 | buckets_() |
| 364 | {} |
| 365 | |
| 366 | table(table& x, ndnboost::unordered::detail::move_tag m) : |
| 367 | functions(x, m), |
| 368 | allocators_(x.allocators_, m), |
| 369 | bucket_count_(x.bucket_count_), |
| 370 | size_(x.size_), |
| 371 | mlf_(x.mlf_), |
| 372 | max_load_(x.max_load_), |
| 373 | buckets_(x.buckets_) |
| 374 | { |
| 375 | x.buckets_ = bucket_pointer(); |
| 376 | x.size_ = 0; |
| 377 | x.max_load_ = 0; |
| 378 | } |
| 379 | |
| 380 | table(table& x, node_allocator const& a, |
| 381 | ndnboost::unordered::detail::move_tag m) : |
| 382 | functions(x, m), |
| 383 | allocators_(a, a), |
| 384 | bucket_count_(x.bucket_count_), |
| 385 | size_(0), |
| 386 | mlf_(x.mlf_), |
| 387 | max_load_(x.max_load_), |
| 388 | buckets_() |
| 389 | {} |
| 390 | |
| 391 | //////////////////////////////////////////////////////////////////////// |
| 392 | // Initialisation. |
| 393 | |
| 394 | void init(table const& x) |
| 395 | { |
| 396 | if (x.size_) { |
| 397 | create_buckets(bucket_count_); |
| 398 | copy_nodes<node_allocator> copy(node_alloc()); |
| 399 | table_impl::fill_buckets(x.begin(), *this, copy); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | void move_init(table& x) |
| 404 | { |
| 405 | if(node_alloc() == x.node_alloc()) { |
| 406 | move_buckets_from(x); |
| 407 | } |
| 408 | else if(x.size_) { |
| 409 | // TODO: Could pick new bucket size? |
| 410 | create_buckets(bucket_count_); |
| 411 | |
| 412 | move_nodes<node_allocator> move(node_alloc()); |
| 413 | node_holder<node_allocator> nodes(x); |
| 414 | table_impl::fill_buckets(nodes.begin(), *this, move); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | //////////////////////////////////////////////////////////////////////// |
| 419 | // Create buckets |
| 420 | |
| 421 | void create_buckets(std::size_t new_count) |
| 422 | { |
| 423 | ndnboost::unordered::detail::array_constructor<bucket_allocator> |
| 424 | constructor(bucket_alloc()); |
| 425 | |
| 426 | // Creates an extra bucket to act as the start node. |
| 427 | constructor.construct(bucket(), new_count + 1); |
| 428 | |
| 429 | if (buckets_) |
| 430 | { |
| 431 | // Copy the nodes to the new buckets, including the dummy |
| 432 | // node if there is one. |
| 433 | (constructor.get() + |
| 434 | static_cast<std::ptrdiff_t>(new_count))->next_ = |
| 435 | (buckets_ + static_cast<std::ptrdiff_t>( |
| 436 | bucket_count_))->next_; |
| 437 | destroy_buckets(); |
| 438 | } |
| 439 | else if (bucket::extra_node) |
| 440 | { |
| 441 | node_constructor a(node_alloc()); |
| 442 | a.construct(); |
| 443 | |
| 444 | (constructor.get() + |
| 445 | static_cast<std::ptrdiff_t>(new_count))->next_ = |
| 446 | a.release(); |
| 447 | } |
| 448 | |
| 449 | bucket_count_ = new_count; |
| 450 | buckets_ = constructor.release(); |
| 451 | recalculate_max_load(); |
| 452 | } |
| 453 | |
| 454 | //////////////////////////////////////////////////////////////////////// |
| 455 | // Swap and Move |
| 456 | |
| 457 | void swap_allocators(table& other, false_type) |
| 458 | { |
| 459 | // According to 23.2.1.8, if propagate_on_container_swap is |
| 460 | // false the behaviour is undefined unless the allocators |
| 461 | // are equal. |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 462 | NDNBOOST_ASSERT(node_alloc() == other.node_alloc()); |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | void swap_allocators(table& other, true_type) |
| 466 | { |
| 467 | allocators_.swap(other.allocators_); |
| 468 | } |
| 469 | |
| 470 | // Only swaps the allocators if propagate_on_container_swap |
| 471 | void swap(table& x) |
| 472 | { |
| 473 | set_hash_functions op1(*this, x); |
| 474 | set_hash_functions op2(x, *this); |
| 475 | |
| 476 | // I think swap can throw if Propagate::value, |
| 477 | // since the allocators' swap can throw. Not sure though. |
| 478 | swap_allocators(x, |
| 479 | ndnboost::unordered::detail::integral_constant<bool, |
| 480 | allocator_traits<node_allocator>:: |
| 481 | propagate_on_container_swap::value>()); |
| 482 | |
| 483 | ndnboost::swap(buckets_, x.buckets_); |
| 484 | ndnboost::swap(bucket_count_, x.bucket_count_); |
| 485 | ndnboost::swap(size_, x.size_); |
| 486 | std::swap(mlf_, x.mlf_); |
| 487 | std::swap(max_load_, x.max_load_); |
| 488 | op1.commit(); |
| 489 | op2.commit(); |
| 490 | } |
| 491 | |
| 492 | void move_buckets_from(table& other) |
| 493 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 494 | NDNBOOST_ASSERT(node_alloc() == other.node_alloc()); |
| 495 | NDNBOOST_ASSERT(!buckets_); |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 496 | buckets_ = other.buckets_; |
| 497 | bucket_count_ = other.bucket_count_; |
| 498 | size_ = other.size_; |
| 499 | other.buckets_ = bucket_pointer(); |
| 500 | other.size_ = 0; |
| 501 | other.max_load_ = 0; |
| 502 | } |
| 503 | |
| 504 | //////////////////////////////////////////////////////////////////////// |
| 505 | // Delete/destruct |
| 506 | |
| 507 | ~table() |
| 508 | { |
| 509 | delete_buckets(); |
| 510 | } |
| 511 | |
| 512 | void delete_node(link_pointer prev) |
| 513 | { |
| 514 | node_pointer n = static_cast<node_pointer>(prev->next_); |
| 515 | prev->next_ = n->next_; |
| 516 | |
| 517 | ndnboost::unordered::detail::destroy_value_impl(node_alloc(), |
| 518 | n->value_ptr()); |
| 519 | node_allocator_traits::destroy(node_alloc(), |
| 520 | ndnboost::addressof(*n)); |
| 521 | node_allocator_traits::deallocate(node_alloc(), n, 1); |
| 522 | --size_; |
| 523 | } |
| 524 | |
| 525 | std::size_t delete_nodes(link_pointer prev, link_pointer end) |
| 526 | { |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 527 | NDNBOOST_ASSERT(prev->next_ != end); |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 528 | |
| 529 | std::size_t count = 0; |
| 530 | |
| 531 | do { |
| 532 | delete_node(prev); |
| 533 | ++count; |
| 534 | } while (prev->next_ != end); |
| 535 | |
| 536 | return count; |
| 537 | } |
| 538 | |
| 539 | void delete_buckets() |
| 540 | { |
| 541 | if(buckets_) { |
| 542 | if (size_) delete_nodes(get_previous_start(), link_pointer()); |
| 543 | |
| 544 | if (bucket::extra_node) { |
| 545 | node_pointer n = static_cast<node_pointer>( |
| 546 | get_bucket(bucket_count_)->next_); |
| 547 | node_allocator_traits::destroy(node_alloc(), |
| 548 | ndnboost::addressof(*n)); |
| 549 | node_allocator_traits::deallocate(node_alloc(), n, 1); |
| 550 | } |
| 551 | |
| 552 | destroy_buckets(); |
| 553 | buckets_ = bucket_pointer(); |
| 554 | max_load_ = 0; |
| 555 | } |
| 556 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 557 | NDNBOOST_ASSERT(!size_); |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | void clear() |
| 561 | { |
| 562 | if (!size_) return; |
| 563 | |
| 564 | delete_nodes(get_previous_start(), link_pointer()); |
| 565 | clear_buckets(); |
| 566 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 567 | NDNBOOST_ASSERT(!size_); |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | void clear_buckets() |
| 571 | { |
| 572 | bucket_pointer end = get_bucket(bucket_count_); |
| 573 | for(bucket_pointer it = buckets_; it != end; ++it) |
| 574 | { |
| 575 | it->next_ = node_pointer(); |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | void destroy_buckets() |
| 580 | { |
| 581 | bucket_pointer end = get_bucket(bucket_count_ + 1); |
| 582 | for(bucket_pointer it = buckets_; it != end; ++it) |
| 583 | { |
| 584 | bucket_allocator_traits::destroy(bucket_alloc(), |
| 585 | ndnboost::addressof(*it)); |
| 586 | } |
| 587 | |
| 588 | bucket_allocator_traits::deallocate(bucket_alloc(), |
| 589 | buckets_, bucket_count_ + 1); |
| 590 | } |
| 591 | |
| 592 | //////////////////////////////////////////////////////////////////////// |
| 593 | // Fix buckets after delete |
| 594 | // |
| 595 | |
| 596 | std::size_t fix_bucket(std::size_t bucket_index, link_pointer prev) |
| 597 | { |
| 598 | link_pointer end = prev->next_; |
| 599 | std::size_t bucket_index2 = bucket_index; |
| 600 | |
| 601 | if (end) |
| 602 | { |
| 603 | bucket_index2 = hash_to_bucket( |
| 604 | static_cast<node_pointer>(end)->hash_); |
| 605 | |
| 606 | // If begin and end are in the same bucket, then |
| 607 | // there's nothing to do. |
| 608 | if (bucket_index == bucket_index2) return bucket_index2; |
| 609 | |
| 610 | // Update the bucket containing end. |
| 611 | get_bucket(bucket_index2)->next_ = prev; |
| 612 | } |
| 613 | |
| 614 | // Check if this bucket is now empty. |
| 615 | bucket_pointer this_bucket = get_bucket(bucket_index); |
| 616 | if (this_bucket->next_ == prev) |
| 617 | this_bucket->next_ = link_pointer(); |
| 618 | |
| 619 | return bucket_index2; |
| 620 | } |
| 621 | |
| 622 | //////////////////////////////////////////////////////////////////////// |
| 623 | // Assignment |
| 624 | |
| 625 | void assign(table const& x) |
| 626 | { |
| 627 | if (this != ndnboost::addressof(x)) |
| 628 | { |
| 629 | assign(x, |
| 630 | ndnboost::unordered::detail::integral_constant<bool, |
| 631 | allocator_traits<node_allocator>:: |
| 632 | propagate_on_container_copy_assignment::value>()); |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | void assign(table const& x, false_type) |
| 637 | { |
| 638 | // Strong exception safety. |
| 639 | set_hash_functions new_func_this(*this, x); |
| 640 | new_func_this.commit(); |
| 641 | mlf_ = x.mlf_; |
| 642 | recalculate_max_load(); |
| 643 | |
| 644 | if (!size_ && !x.size_) return; |
| 645 | |
| 646 | if (x.size_ >= max_load_) { |
| 647 | create_buckets(min_buckets_for_size(x.size_)); |
| 648 | } |
| 649 | else { |
| 650 | clear_buckets(); |
| 651 | } |
| 652 | |
| 653 | // assign_nodes takes ownership of the container's elements, |
| 654 | // assigning to them if possible, and deleting any that are |
| 655 | // left over. |
| 656 | assign_nodes<table> assign(*this); |
| 657 | table_impl::fill_buckets(x.begin(), *this, assign); |
| 658 | } |
| 659 | |
| 660 | void assign(table const& x, true_type) |
| 661 | { |
| 662 | if (node_alloc() == x.node_alloc()) { |
| 663 | allocators_.assign(x.allocators_); |
| 664 | assign(x, false_type()); |
| 665 | } |
| 666 | else { |
| 667 | set_hash_functions new_func_this(*this, x); |
| 668 | |
| 669 | // Delete everything with current allocators before assigning |
| 670 | // the new ones. |
| 671 | delete_buckets(); |
| 672 | allocators_.assign(x.allocators_); |
| 673 | |
| 674 | // Copy over other data, all no throw. |
| 675 | new_func_this.commit(); |
| 676 | mlf_ = x.mlf_; |
| 677 | bucket_count_ = min_buckets_for_size(x.size_); |
| 678 | max_load_ = 0; |
| 679 | |
| 680 | // Finally copy the elements. |
| 681 | if (x.size_) { |
| 682 | create_buckets(bucket_count_); |
| 683 | copy_nodes<node_allocator> copy(node_alloc()); |
| 684 | table_impl::fill_buckets(x.begin(), *this, copy); |
| 685 | } |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | void move_assign(table& x) |
| 690 | { |
| 691 | if (this != ndnboost::addressof(x)) |
| 692 | { |
| 693 | move_assign(x, |
| 694 | ndnboost::unordered::detail::integral_constant<bool, |
| 695 | allocator_traits<node_allocator>:: |
| 696 | propagate_on_container_move_assignment::value>()); |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | void move_assign(table& x, true_type) |
| 701 | { |
| 702 | delete_buckets(); |
| 703 | allocators_.move_assign(x.allocators_); |
| 704 | move_assign_no_alloc(x); |
| 705 | } |
| 706 | |
| 707 | void move_assign(table& x, false_type) |
| 708 | { |
| 709 | if (node_alloc() == x.node_alloc()) { |
| 710 | delete_buckets(); |
| 711 | move_assign_no_alloc(x); |
| 712 | } |
| 713 | else { |
| 714 | set_hash_functions new_func_this(*this, x); |
| 715 | new_func_this.commit(); |
| 716 | mlf_ = x.mlf_; |
| 717 | recalculate_max_load(); |
| 718 | |
| 719 | if (!size_ && !x.size_) return; |
| 720 | |
| 721 | if (x.size_ >= max_load_) { |
| 722 | create_buckets(min_buckets_for_size(x.size_)); |
| 723 | } |
| 724 | else { |
| 725 | clear_buckets(); |
| 726 | } |
| 727 | |
| 728 | // move_assign_nodes takes ownership of the container's |
| 729 | // elements, assigning to them if possible, and deleting |
| 730 | // any that are left over. |
| 731 | move_assign_nodes<table> assign(*this); |
| 732 | node_holder<node_allocator> nodes(x); |
| 733 | table_impl::fill_buckets(nodes.begin(), *this, assign); |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | void move_assign_no_alloc(table& x) |
| 738 | { |
| 739 | set_hash_functions new_func_this(*this, x); |
| 740 | // No throw from here. |
| 741 | mlf_ = x.mlf_; |
| 742 | max_load_ = x.max_load_; |
| 743 | move_buckets_from(x); |
| 744 | new_func_this.commit(); |
| 745 | } |
| 746 | |
| 747 | // Accessors |
| 748 | |
| 749 | key_type const& get_key(value_type const& x) const |
| 750 | { |
| 751 | return extractor::extract(x); |
| 752 | } |
| 753 | |
| 754 | std::size_t hash(key_type const& k) const |
| 755 | { |
| 756 | return policy::apply_hash(this->hash_function(), k); |
| 757 | } |
| 758 | |
| 759 | // Find Node |
| 760 | |
| 761 | template <typename Key, typename Hash, typename Pred> |
| 762 | iterator generic_find_node( |
| 763 | Key const& k, |
| 764 | Hash const& hf, |
| 765 | Pred const& eq) const |
| 766 | { |
| 767 | return static_cast<table_impl const*>(this)-> |
| 768 | find_node_impl(policy::apply_hash(hf, k), k, eq); |
| 769 | } |
| 770 | |
| 771 | iterator find_node( |
| 772 | std::size_t key_hash, |
| 773 | key_type const& k) const |
| 774 | { |
| 775 | return static_cast<table_impl const*>(this)-> |
| 776 | find_node_impl(key_hash, k, this->key_eq()); |
| 777 | } |
| 778 | |
| 779 | iterator find_node(key_type const& k) const |
| 780 | { |
| 781 | return static_cast<table_impl const*>(this)-> |
| 782 | find_node_impl(hash(k), k, this->key_eq()); |
| 783 | } |
| 784 | |
| 785 | iterator find_matching_node(iterator n) const |
| 786 | { |
| 787 | // TODO: Does this apply to C++11? |
| 788 | // |
| 789 | // For some stupid reason, I decided to support equality comparison |
| 790 | // when different hash functions are used. So I can't use the hash |
| 791 | // value from the node here. |
| 792 | |
| 793 | return find_node(get_key(*n)); |
| 794 | } |
| 795 | |
| 796 | // Reserve and rehash |
| 797 | |
| 798 | void reserve_for_insert(std::size_t); |
| 799 | void rehash(std::size_t); |
| 800 | void reserve(std::size_t); |
| 801 | }; |
| 802 | |
| 803 | //////////////////////////////////////////////////////////////////////////// |
| 804 | // Reserve & Rehash |
| 805 | |
| 806 | // basic exception safety |
| 807 | template <typename Types> |
| 808 | inline void table<Types>::reserve_for_insert(std::size_t size) |
| 809 | { |
| 810 | if (!buckets_) { |
| 811 | create_buckets((std::max)(bucket_count_, |
| 812 | min_buckets_for_size(size))); |
| 813 | } |
| 814 | // According to the standard this should be 'size >= max_load_', |
| 815 | // but I think this is better, defect report filed. |
| 816 | else if(size > max_load_) { |
| 817 | std::size_t num_buckets |
| 818 | = min_buckets_for_size((std::max)(size, |
| 819 | size_ + (size_ >> 1))); |
| 820 | |
| 821 | if (num_buckets != bucket_count_) |
| 822 | static_cast<table_impl*>(this)->rehash_impl(num_buckets); |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | // if hash function throws, basic exception safety |
| 827 | // strong otherwise. |
| 828 | |
| 829 | template <typename Types> |
| 830 | inline void table<Types>::rehash(std::size_t min_buckets) |
| 831 | { |
| 832 | using namespace std; |
| 833 | |
| 834 | if(!size_) { |
| 835 | delete_buckets(); |
| 836 | bucket_count_ = policy::new_bucket_count(min_buckets); |
| 837 | } |
| 838 | else { |
| 839 | min_buckets = policy::new_bucket_count((std::max)(min_buckets, |
| 840 | ndnboost::unordered::detail::double_to_size(floor( |
| 841 | static_cast<double>(size_) / |
| 842 | static_cast<double>(mlf_))) + 1)); |
| 843 | |
| 844 | if(min_buckets != bucket_count_) |
| 845 | static_cast<table_impl*>(this)->rehash_impl(min_buckets); |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | template <typename Types> |
| 850 | inline void table<Types>::reserve(std::size_t num_elements) |
| 851 | { |
| 852 | rehash(static_cast<std::size_t>( |
| 853 | std::ceil(static_cast<double>(num_elements) / mlf_))); |
| 854 | } |
| 855 | }}} |
| 856 | |
Jeff Thompson | 3d613fd | 2013-10-15 15:39:04 -0700 | [diff] [blame] | 857 | #if defined(NDNBOOST_MSVC) |
Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame] | 858 | #pragma warning(pop) |
| 859 | #endif |
| 860 | |
| 861 | #endif |