annotate DEPENDENCIES/generic/include/boost/unordered/detail/buckets.hpp @ 16:2665513ce2d3

Add boost headers
author Chris Cannam
date Tue, 05 Aug 2014 11:11:38 +0100
parents
children c530137014c0
rev   line source
Chris@16 1
Chris@16 2 // Copyright (C) 2003-2004 Jeremy B. Maitin-Shepard.
Chris@16 3 // Copyright (C) 2005-2011 Daniel James
Chris@16 4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
Chris@16 5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Chris@16 6
Chris@16 7 #ifndef BOOST_UNORDERED_DETAIL_MANAGER_HPP_INCLUDED
Chris@16 8 #define BOOST_UNORDERED_DETAIL_MANAGER_HPP_INCLUDED
Chris@16 9
Chris@16 10 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
Chris@16 11 # pragma once
Chris@16 12 #endif
Chris@16 13
Chris@16 14 #include <boost/unordered/detail/util.hpp>
Chris@16 15 #include <boost/unordered/detail/allocate.hpp>
Chris@16 16 #include <boost/type_traits/aligned_storage.hpp>
Chris@16 17 #include <boost/type_traits/alignment_of.hpp>
Chris@16 18 #include <boost/type_traits/is_nothrow_move_constructible.hpp>
Chris@16 19 #include <boost/type_traits/is_nothrow_move_assignable.hpp>
Chris@16 20 #include <boost/swap.hpp>
Chris@16 21 #include <boost/assert.hpp>
Chris@16 22 #include <boost/limits.hpp>
Chris@16 23 #include <boost/iterator.hpp>
Chris@16 24
Chris@16 25 namespace boost { namespace unordered { namespace detail {
Chris@16 26
Chris@16 27 template <typename Types> struct table;
Chris@16 28 template <typename NodePointer> struct bucket;
Chris@16 29 struct ptr_bucket;
Chris@16 30 template <typename Types> struct table_impl;
Chris@16 31 template <typename Types> struct grouped_table_impl;
Chris@16 32
Chris@16 33 }}}
Chris@16 34
Chris@16 35 // The 'iterator_detail' namespace was a misguided attempt at avoiding ADL
Chris@16 36 // in the detail namespace. It didn't work because the template parameters
Chris@16 37 // were in detail. I'm not changing it at the moment to be safe. I might
Chris@16 38 // do in the future if I change the iterator types.
Chris@16 39 namespace boost { namespace unordered { namespace iterator_detail {
Chris@16 40
Chris@16 41 ////////////////////////////////////////////////////////////////////////////
Chris@16 42 // Iterators
Chris@16 43 //
Chris@16 44 // all no throw
Chris@16 45
Chris@16 46 template <typename Node> struct iterator;
Chris@16 47 template <typename Node, typename ConstNodePointer> struct c_iterator;
Chris@16 48 template <typename Node, typename Policy> struct l_iterator;
Chris@16 49 template <typename Node, typename ConstNodePointer, typename Policy>
Chris@16 50 struct cl_iterator;
Chris@16 51
Chris@16 52 // Local Iterators
Chris@16 53 //
Chris@16 54 // all no throw
Chris@16 55
Chris@16 56 template <typename Node, typename Policy>
Chris@16 57 struct l_iterator
Chris@16 58 : public boost::iterator<
Chris@16 59 std::forward_iterator_tag,
Chris@16 60 typename Node::value_type,
Chris@16 61 std::ptrdiff_t,
Chris@16 62 typename Node::node_pointer,
Chris@16 63 typename Node::value_type&>
Chris@16 64 {
Chris@16 65 #if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
Chris@16 66 template <typename Node2, typename ConstNodePointer, typename Policy2>
Chris@16 67 friend struct boost::unordered::iterator_detail::cl_iterator;
Chris@16 68 private:
Chris@16 69 #endif
Chris@16 70 typedef typename Node::node_pointer node_pointer;
Chris@16 71 typedef boost::unordered::iterator_detail::iterator<Node> iterator;
Chris@16 72 node_pointer ptr_;
Chris@16 73 std::size_t bucket_;
Chris@16 74 std::size_t bucket_count_;
Chris@16 75
Chris@16 76 public:
Chris@16 77
Chris@16 78 typedef typename Node::value_type value_type;
Chris@16 79
Chris@16 80 l_iterator() BOOST_NOEXCEPT : ptr_() {}
Chris@16 81
Chris@16 82 l_iterator(iterator x, std::size_t b, std::size_t c) BOOST_NOEXCEPT
Chris@16 83 : ptr_(x.node_), bucket_(b), bucket_count_(c) {}
Chris@16 84
Chris@16 85 value_type& operator*() const {
Chris@16 86 return ptr_->value();
Chris@16 87 }
Chris@16 88
Chris@16 89 value_type* operator->() const {
Chris@16 90 return ptr_->value_ptr();
Chris@16 91 }
Chris@16 92
Chris@16 93 l_iterator& operator++() {
Chris@16 94 ptr_ = static_cast<node_pointer>(ptr_->next_);
Chris@16 95 if (ptr_ && Policy::to_bucket(bucket_count_, ptr_->hash_)
Chris@16 96 != bucket_)
Chris@16 97 ptr_ = node_pointer();
Chris@16 98 return *this;
Chris@16 99 }
Chris@16 100
Chris@16 101 l_iterator operator++(int) {
Chris@16 102 l_iterator tmp(*this);
Chris@16 103 ++(*this);
Chris@16 104 return tmp;
Chris@16 105 }
Chris@16 106
Chris@16 107 bool operator==(l_iterator x) const BOOST_NOEXCEPT {
Chris@16 108 return ptr_ == x.ptr_;
Chris@16 109 }
Chris@16 110
Chris@16 111 bool operator!=(l_iterator x) const BOOST_NOEXCEPT {
Chris@16 112 return ptr_ != x.ptr_;
Chris@16 113 }
Chris@16 114 };
Chris@16 115
Chris@16 116 template <typename Node, typename ConstNodePointer, typename Policy>
Chris@16 117 struct cl_iterator
Chris@16 118 : public boost::iterator<
Chris@16 119 std::forward_iterator_tag,
Chris@16 120 typename Node::value_type,
Chris@16 121 std::ptrdiff_t,
Chris@16 122 ConstNodePointer,
Chris@16 123 typename Node::value_type const&>
Chris@16 124 {
Chris@16 125 friend struct boost::unordered::iterator_detail::l_iterator
Chris@16 126 <Node, Policy>;
Chris@16 127 private:
Chris@16 128
Chris@16 129 typedef typename Node::node_pointer node_pointer;
Chris@16 130 typedef boost::unordered::iterator_detail::iterator<Node> iterator;
Chris@16 131 node_pointer ptr_;
Chris@16 132 std::size_t bucket_;
Chris@16 133 std::size_t bucket_count_;
Chris@16 134
Chris@16 135 public:
Chris@16 136
Chris@16 137 typedef typename Node::value_type value_type;
Chris@16 138
Chris@16 139 cl_iterator() BOOST_NOEXCEPT : ptr_() {}
Chris@16 140
Chris@16 141 cl_iterator(iterator x, std::size_t b, std::size_t c) BOOST_NOEXCEPT :
Chris@16 142 ptr_(x.node_), bucket_(b), bucket_count_(c) {}
Chris@16 143
Chris@16 144 cl_iterator(boost::unordered::iterator_detail::l_iterator<
Chris@16 145 Node, Policy> const& x) BOOST_NOEXCEPT :
Chris@16 146 ptr_(x.ptr_), bucket_(x.bucket_), bucket_count_(x.bucket_count_)
Chris@16 147 {}
Chris@16 148
Chris@16 149 value_type const& operator*() const {
Chris@16 150 return ptr_->value();
Chris@16 151 }
Chris@16 152
Chris@16 153 value_type const* operator->() const {
Chris@16 154 return ptr_->value_ptr();
Chris@16 155 }
Chris@16 156
Chris@16 157 cl_iterator& operator++() {
Chris@16 158 ptr_ = static_cast<node_pointer>(ptr_->next_);
Chris@16 159 if (ptr_ && Policy::to_bucket(bucket_count_, ptr_->hash_)
Chris@16 160 != bucket_)
Chris@16 161 ptr_ = node_pointer();
Chris@16 162 return *this;
Chris@16 163 }
Chris@16 164
Chris@16 165 cl_iterator operator++(int) {
Chris@16 166 cl_iterator tmp(*this);
Chris@16 167 ++(*this);
Chris@16 168 return tmp;
Chris@16 169 }
Chris@16 170
Chris@16 171 friend bool operator==(cl_iterator const& x, cl_iterator const& y)
Chris@16 172 BOOST_NOEXCEPT
Chris@16 173 {
Chris@16 174 return x.ptr_ == y.ptr_;
Chris@16 175 }
Chris@16 176
Chris@16 177 friend bool operator!=(cl_iterator const& x, cl_iterator const& y)
Chris@16 178 BOOST_NOEXCEPT
Chris@16 179 {
Chris@16 180 return x.ptr_ != y.ptr_;
Chris@16 181 }
Chris@16 182 };
Chris@16 183
Chris@16 184 template <typename Node>
Chris@16 185 struct iterator
Chris@16 186 : public boost::iterator<
Chris@16 187 std::forward_iterator_tag,
Chris@16 188 typename Node::value_type,
Chris@16 189 std::ptrdiff_t,
Chris@16 190 typename Node::node_pointer,
Chris@16 191 typename Node::value_type&>
Chris@16 192 {
Chris@16 193 #if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
Chris@16 194 template <typename, typename>
Chris@16 195 friend struct boost::unordered::iterator_detail::c_iterator;
Chris@16 196 template <typename, typename>
Chris@16 197 friend struct boost::unordered::iterator_detail::l_iterator;
Chris@16 198 template <typename, typename, typename>
Chris@16 199 friend struct boost::unordered::iterator_detail::cl_iterator;
Chris@16 200 template <typename>
Chris@16 201 friend struct boost::unordered::detail::table;
Chris@16 202 template <typename>
Chris@16 203 friend struct boost::unordered::detail::table_impl;
Chris@16 204 template <typename>
Chris@16 205 friend struct boost::unordered::detail::grouped_table_impl;
Chris@16 206 private:
Chris@16 207 #endif
Chris@16 208 typedef typename Node::node_pointer node_pointer;
Chris@16 209 node_pointer node_;
Chris@16 210
Chris@16 211 public:
Chris@16 212
Chris@16 213 typedef typename Node::value_type value_type;
Chris@16 214
Chris@16 215 iterator() BOOST_NOEXCEPT : node_() {}
Chris@16 216
Chris@16 217 explicit iterator(typename Node::link_pointer x) BOOST_NOEXCEPT :
Chris@16 218 node_(static_cast<node_pointer>(x)) {}
Chris@16 219
Chris@16 220 value_type& operator*() const {
Chris@16 221 return node_->value();
Chris@16 222 }
Chris@16 223
Chris@16 224 value_type* operator->() const {
Chris@16 225 return &node_->value();
Chris@16 226 }
Chris@16 227
Chris@16 228 iterator& operator++() {
Chris@16 229 node_ = static_cast<node_pointer>(node_->next_);
Chris@16 230 return *this;
Chris@16 231 }
Chris@16 232
Chris@16 233 iterator operator++(int) {
Chris@16 234 iterator tmp(node_);
Chris@16 235 node_ = static_cast<node_pointer>(node_->next_);
Chris@16 236 return tmp;
Chris@16 237 }
Chris@16 238
Chris@16 239 bool operator==(iterator const& x) const BOOST_NOEXCEPT {
Chris@16 240 return node_ == x.node_;
Chris@16 241 }
Chris@16 242
Chris@16 243 bool operator!=(iterator const& x) const BOOST_NOEXCEPT {
Chris@16 244 return node_ != x.node_;
Chris@16 245 }
Chris@16 246 };
Chris@16 247
Chris@16 248 template <typename Node, typename ConstNodePointer>
Chris@16 249 struct c_iterator
Chris@16 250 : public boost::iterator<
Chris@16 251 std::forward_iterator_tag,
Chris@16 252 typename Node::value_type,
Chris@16 253 std::ptrdiff_t,
Chris@16 254 ConstNodePointer,
Chris@16 255 typename Node::value_type const&>
Chris@16 256 {
Chris@16 257 friend struct boost::unordered::iterator_detail::iterator<Node>;
Chris@16 258
Chris@16 259 #if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
Chris@16 260 template <typename>
Chris@16 261 friend struct boost::unordered::detail::table;
Chris@16 262 template <typename>
Chris@16 263 friend struct boost::unordered::detail::table_impl;
Chris@16 264 template <typename>
Chris@16 265 friend struct boost::unordered::detail::grouped_table_impl;
Chris@16 266
Chris@16 267 private:
Chris@16 268 #endif
Chris@16 269 typedef typename Node::node_pointer node_pointer;
Chris@16 270 typedef boost::unordered::iterator_detail::iterator<Node> iterator;
Chris@16 271 node_pointer node_;
Chris@16 272
Chris@16 273 public:
Chris@16 274
Chris@16 275 typedef typename Node::value_type value_type;
Chris@16 276
Chris@16 277 c_iterator() BOOST_NOEXCEPT : node_() {}
Chris@16 278
Chris@16 279 explicit c_iterator(typename Node::link_pointer x) BOOST_NOEXCEPT :
Chris@16 280 node_(static_cast<node_pointer>(x)) {}
Chris@16 281
Chris@16 282 c_iterator(iterator const& x) BOOST_NOEXCEPT : node_(x.node_) {}
Chris@16 283
Chris@16 284 value_type const& operator*() const {
Chris@16 285 return node_->value();
Chris@16 286 }
Chris@16 287
Chris@16 288 value_type const* operator->() const {
Chris@16 289 return &node_->value();
Chris@16 290 }
Chris@16 291
Chris@16 292 c_iterator& operator++() {
Chris@16 293 node_ = static_cast<node_pointer>(node_->next_);
Chris@16 294 return *this;
Chris@16 295 }
Chris@16 296
Chris@16 297 c_iterator operator++(int) {
Chris@16 298 c_iterator tmp(node_);
Chris@16 299 node_ = static_cast<node_pointer>(node_->next_);
Chris@16 300 return tmp;
Chris@16 301 }
Chris@16 302
Chris@16 303 friend bool operator==(c_iterator const& x, c_iterator const& y)
Chris@16 304 BOOST_NOEXCEPT
Chris@16 305 {
Chris@16 306 return x.node_ == y.node_;
Chris@16 307 }
Chris@16 308
Chris@16 309 friend bool operator!=(c_iterator const& x, c_iterator const& y)
Chris@16 310 BOOST_NOEXCEPT
Chris@16 311 {
Chris@16 312 return x.node_ != y.node_;
Chris@16 313 }
Chris@16 314 };
Chris@16 315 }}}
Chris@16 316
Chris@16 317 namespace boost { namespace unordered { namespace detail {
Chris@16 318
Chris@16 319 ///////////////////////////////////////////////////////////////////
Chris@16 320 //
Chris@16 321 // Node construction
Chris@16 322
Chris@16 323 template <typename NodeAlloc>
Chris@16 324 struct node_constructor
Chris@16 325 {
Chris@16 326 private:
Chris@16 327
Chris@16 328 typedef NodeAlloc node_allocator;
Chris@16 329 typedef boost::unordered::detail::allocator_traits<NodeAlloc>
Chris@16 330 node_allocator_traits;
Chris@16 331 typedef typename node_allocator_traits::value_type node;
Chris@16 332 typedef typename node_allocator_traits::pointer node_pointer;
Chris@16 333 typedef typename node::value_type value_type;
Chris@16 334
Chris@16 335 protected:
Chris@16 336
Chris@16 337 node_allocator& alloc_;
Chris@16 338 node_pointer node_;
Chris@16 339 bool node_constructed_;
Chris@16 340 bool value_constructed_;
Chris@16 341
Chris@16 342 public:
Chris@16 343
Chris@16 344 node_constructor(node_allocator& n) :
Chris@16 345 alloc_(n),
Chris@16 346 node_(),
Chris@16 347 node_constructed_(false),
Chris@16 348 value_constructed_(false)
Chris@16 349 {
Chris@16 350 }
Chris@16 351
Chris@16 352 ~node_constructor();
Chris@16 353
Chris@16 354 void construct();
Chris@16 355
Chris@16 356 template <BOOST_UNORDERED_EMPLACE_TEMPLATE>
Chris@16 357 void construct_with_value(BOOST_UNORDERED_EMPLACE_ARGS)
Chris@16 358 {
Chris@16 359 construct();
Chris@16 360 boost::unordered::detail::func::construct_value_impl(
Chris@16 361 alloc_, node_->value_ptr(), BOOST_UNORDERED_EMPLACE_FORWARD);
Chris@16 362 value_constructed_ = true;
Chris@16 363 }
Chris@16 364
Chris@16 365 template <typename A0>
Chris@16 366 void construct_with_value2(BOOST_FWD_REF(A0) a0)
Chris@16 367 {
Chris@16 368 construct();
Chris@16 369 boost::unordered::detail::func::construct_value_impl(
Chris@16 370 alloc_, node_->value_ptr(),
Chris@16 371 BOOST_UNORDERED_EMPLACE_ARGS1(boost::forward<A0>(a0)));
Chris@16 372 value_constructed_ = true;
Chris@16 373 }
Chris@16 374
Chris@16 375 value_type const& value() const {
Chris@16 376 BOOST_ASSERT(node_ && node_constructed_ && value_constructed_);
Chris@16 377 return node_->value();
Chris@16 378 }
Chris@16 379
Chris@16 380 // no throw
Chris@16 381 node_pointer release()
Chris@16 382 {
Chris@16 383 BOOST_ASSERT(node_ && node_constructed_);
Chris@16 384 node_pointer p = node_;
Chris@16 385 node_ = node_pointer();
Chris@16 386 return p;
Chris@16 387 }
Chris@16 388
Chris@16 389 private:
Chris@16 390 node_constructor(node_constructor const&);
Chris@16 391 node_constructor& operator=(node_constructor const&);
Chris@16 392 };
Chris@16 393
Chris@16 394 template <typename Alloc>
Chris@16 395 node_constructor<Alloc>::~node_constructor()
Chris@16 396 {
Chris@16 397 if (node_) {
Chris@16 398 if (value_constructed_) {
Chris@16 399 boost::unordered::detail::func::destroy_value_impl(alloc_,
Chris@16 400 node_->value_ptr());
Chris@16 401 }
Chris@16 402
Chris@16 403 if (node_constructed_) {
Chris@16 404 node_allocator_traits::destroy(alloc_,
Chris@16 405 boost::addressof(*node_));
Chris@16 406 }
Chris@16 407
Chris@16 408 node_allocator_traits::deallocate(alloc_, node_, 1);
Chris@16 409 }
Chris@16 410 }
Chris@16 411
Chris@16 412 template <typename Alloc>
Chris@16 413 void node_constructor<Alloc>::construct()
Chris@16 414 {
Chris@16 415 if(!node_) {
Chris@16 416 node_constructed_ = false;
Chris@16 417 value_constructed_ = false;
Chris@16 418
Chris@16 419 node_ = node_allocator_traits::allocate(alloc_, 1);
Chris@16 420
Chris@16 421 node_allocator_traits::construct(alloc_,
Chris@16 422 boost::addressof(*node_), node());
Chris@16 423 node_->init(node_);
Chris@16 424 node_constructed_ = true;
Chris@16 425 }
Chris@16 426 else {
Chris@16 427 BOOST_ASSERT(node_constructed_);
Chris@16 428
Chris@16 429 if (value_constructed_)
Chris@16 430 {
Chris@16 431 boost::unordered::detail::func::destroy_value_impl(alloc_,
Chris@16 432 node_->value_ptr());
Chris@16 433 value_constructed_ = false;
Chris@16 434 }
Chris@16 435 }
Chris@16 436 }
Chris@16 437
Chris@16 438 ///////////////////////////////////////////////////////////////////
Chris@16 439 //
Chris@16 440 // Node Holder
Chris@16 441 //
Chris@16 442 // Temporary store for nodes. Deletes any that aren't used.
Chris@16 443
Chris@16 444 template <typename NodeAlloc>
Chris@16 445 struct node_holder : private node_constructor<NodeAlloc>
Chris@16 446 {
Chris@16 447 private:
Chris@16 448 typedef node_constructor<NodeAlloc> base;
Chris@16 449
Chris@16 450 typedef NodeAlloc node_allocator;
Chris@16 451 typedef boost::unordered::detail::allocator_traits<NodeAlloc>
Chris@16 452 node_allocator_traits;
Chris@16 453 typedef typename node_allocator_traits::value_type node;
Chris@16 454 typedef typename node_allocator_traits::pointer node_pointer;
Chris@16 455 typedef typename node::value_type value_type;
Chris@16 456 typedef typename node::link_pointer link_pointer;
Chris@16 457 typedef boost::unordered::iterator_detail::iterator<node> iterator;
Chris@16 458
Chris@16 459 node_pointer nodes_;
Chris@16 460
Chris@16 461 public:
Chris@16 462
Chris@16 463 template <typename Table>
Chris@16 464 explicit node_holder(Table& b) :
Chris@16 465 base(b.node_alloc()),
Chris@16 466 nodes_()
Chris@16 467 {
Chris@16 468 if (b.size_) {
Chris@16 469 typename Table::link_pointer prev = b.get_previous_start();
Chris@16 470 nodes_ = static_cast<node_pointer>(prev->next_);
Chris@16 471 prev->next_ = link_pointer();
Chris@16 472 b.size_ = 0;
Chris@16 473 }
Chris@16 474 }
Chris@16 475
Chris@16 476 ~node_holder();
Chris@16 477
Chris@16 478 void node_for_assignment()
Chris@16 479 {
Chris@16 480 if (!this->node_ && nodes_) {
Chris@16 481 this->node_ = nodes_;
Chris@16 482 nodes_ = static_cast<node_pointer>(nodes_->next_);
Chris@16 483 this->node_->init(this->node_);
Chris@16 484 this->node_->next_ = link_pointer();
Chris@16 485
Chris@16 486 this->node_constructed_ = true;
Chris@16 487 this->value_constructed_ = true;
Chris@16 488 }
Chris@16 489 }
Chris@16 490
Chris@16 491 template <typename T>
Chris@16 492 inline void assign_impl(T const& v) {
Chris@16 493 if (this->node_ && this->value_constructed_) {
Chris@16 494 this->node_->value() = v;
Chris@16 495 }
Chris@16 496 else {
Chris@16 497 this->construct_with_value2(v);
Chris@16 498 }
Chris@16 499 }
Chris@16 500
Chris@16 501 template <typename T1, typename T2>
Chris@16 502 inline void assign_impl(std::pair<T1 const, T2> const& v) {
Chris@16 503 this->construct_with_value2(v);
Chris@16 504 }
Chris@16 505
Chris@16 506 template <typename T>
Chris@16 507 inline void move_assign_impl(T& v) {
Chris@16 508 if (this->node_ && this->value_constructed_) {
Chris@16 509 this->node_->value() = boost::move(v);
Chris@16 510 }
Chris@16 511 else {
Chris@16 512 this->construct_with_value2(boost::move(v));
Chris@16 513 }
Chris@16 514 }
Chris@16 515
Chris@16 516 template <typename T1, typename T2>
Chris@16 517 inline void move_assign_impl(std::pair<T1 const, T2>& v) {
Chris@16 518 this->construct_with_value2(boost::move(v));
Chris@16 519 }
Chris@16 520
Chris@16 521 node_pointer copy_of(value_type const& v)
Chris@16 522 {
Chris@16 523 node_for_assignment();
Chris@16 524 assign_impl(v);
Chris@16 525 return base::release();
Chris@16 526 }
Chris@16 527
Chris@16 528 node_pointer move_copy_of(value_type& v)
Chris@16 529 {
Chris@16 530 node_for_assignment();
Chris@16 531 move_assign_impl(v);
Chris@16 532 return base::release();
Chris@16 533 }
Chris@16 534
Chris@16 535 iterator begin() const
Chris@16 536 {
Chris@16 537 return iterator(nodes_);
Chris@16 538 }
Chris@16 539 };
Chris@16 540
Chris@16 541 template <typename Alloc>
Chris@16 542 node_holder<Alloc>::~node_holder()
Chris@16 543 {
Chris@16 544 while (nodes_) {
Chris@16 545 node_pointer p = nodes_;
Chris@16 546 nodes_ = static_cast<node_pointer>(p->next_);
Chris@16 547
Chris@16 548 boost::unordered::detail::func::destroy_value_impl(this->alloc_,
Chris@16 549 p->value_ptr());
Chris@16 550 node_allocator_traits::destroy(this->alloc_, boost::addressof(*p));
Chris@16 551 node_allocator_traits::deallocate(this->alloc_, p, 1);
Chris@16 552 }
Chris@16 553 }
Chris@16 554
Chris@16 555 ///////////////////////////////////////////////////////////////////
Chris@16 556 //
Chris@16 557 // Bucket
Chris@16 558
Chris@16 559 template <typename NodePointer>
Chris@16 560 struct bucket
Chris@16 561 {
Chris@16 562 typedef NodePointer link_pointer;
Chris@16 563 link_pointer next_;
Chris@16 564
Chris@16 565 bucket() : next_() {}
Chris@16 566
Chris@16 567 link_pointer first_from_start()
Chris@16 568 {
Chris@16 569 return next_;
Chris@16 570 }
Chris@16 571
Chris@16 572 enum { extra_node = true };
Chris@16 573 };
Chris@16 574
Chris@16 575 struct ptr_bucket
Chris@16 576 {
Chris@16 577 typedef ptr_bucket* link_pointer;
Chris@16 578 link_pointer next_;
Chris@16 579
Chris@16 580 ptr_bucket() : next_(0) {}
Chris@16 581
Chris@16 582 link_pointer first_from_start()
Chris@16 583 {
Chris@16 584 return this;
Chris@16 585 }
Chris@16 586
Chris@16 587 enum { extra_node = false };
Chris@16 588 };
Chris@16 589
Chris@16 590 ///////////////////////////////////////////////////////////////////
Chris@16 591 //
Chris@16 592 // Hash Policy
Chris@16 593
Chris@16 594 template <typename SizeT>
Chris@16 595 struct prime_policy
Chris@16 596 {
Chris@16 597 template <typename Hash, typename T>
Chris@16 598 static inline SizeT apply_hash(Hash const& hf, T const& x) {
Chris@16 599 return hf(x);
Chris@16 600 }
Chris@16 601
Chris@16 602 static inline SizeT to_bucket(SizeT bucket_count, SizeT hash) {
Chris@16 603 return hash % bucket_count;
Chris@16 604 }
Chris@16 605
Chris@16 606 static inline SizeT new_bucket_count(SizeT min) {
Chris@16 607 return boost::unordered::detail::next_prime(min);
Chris@16 608 }
Chris@16 609
Chris@16 610 static inline SizeT prev_bucket_count(SizeT max) {
Chris@16 611 return boost::unordered::detail::prev_prime(max);
Chris@16 612 }
Chris@16 613 };
Chris@16 614
Chris@16 615 template <typename SizeT>
Chris@16 616 struct mix64_policy
Chris@16 617 {
Chris@16 618 template <typename Hash, typename T>
Chris@16 619 static inline SizeT apply_hash(Hash const& hf, T const& x) {
Chris@16 620 SizeT key = hf(x);
Chris@16 621 key = (~key) + (key << 21); // key = (key << 21) - key - 1;
Chris@16 622 key = key ^ (key >> 24);
Chris@16 623 key = (key + (key << 3)) + (key << 8); // key * 265
Chris@16 624 key = key ^ (key >> 14);
Chris@16 625 key = (key + (key << 2)) + (key << 4); // key * 21
Chris@16 626 key = key ^ (key >> 28);
Chris@16 627 key = key + (key << 31);
Chris@16 628 return key;
Chris@16 629 }
Chris@16 630
Chris@16 631 static inline SizeT to_bucket(SizeT bucket_count, SizeT hash) {
Chris@16 632 return hash & (bucket_count - 1);
Chris@16 633 }
Chris@16 634
Chris@16 635 static inline SizeT new_bucket_count(SizeT min) {
Chris@16 636 if (min <= 4) return 4;
Chris@16 637 --min;
Chris@16 638 min |= min >> 1;
Chris@16 639 min |= min >> 2;
Chris@16 640 min |= min >> 4;
Chris@16 641 min |= min >> 8;
Chris@16 642 min |= min >> 16;
Chris@16 643 min |= min >> 32;
Chris@16 644 return min + 1;
Chris@16 645 }
Chris@16 646
Chris@16 647 static inline SizeT prev_bucket_count(SizeT max) {
Chris@16 648 max |= max >> 1;
Chris@16 649 max |= max >> 2;
Chris@16 650 max |= max >> 4;
Chris@16 651 max |= max >> 8;
Chris@16 652 max |= max >> 16;
Chris@16 653 max |= max >> 32;
Chris@16 654 return (max >> 1) + 1;
Chris@16 655 }
Chris@16 656 };
Chris@16 657
Chris@16 658 template <int digits, int radix>
Chris@16 659 struct pick_policy_impl {
Chris@16 660 typedef prime_policy<std::size_t> type;
Chris@16 661 };
Chris@16 662
Chris@16 663 template <>
Chris@16 664 struct pick_policy_impl<64, 2> {
Chris@16 665 typedef mix64_policy<std::size_t> type;
Chris@16 666 };
Chris@16 667
Chris@16 668 struct pick_policy :
Chris@16 669 pick_policy_impl<
Chris@16 670 std::numeric_limits<std::size_t>::digits,
Chris@16 671 std::numeric_limits<std::size_t>::radix> {};
Chris@16 672
Chris@16 673 ////////////////////////////////////////////////////////////////////////////
Chris@16 674 // Functions
Chris@16 675
Chris@16 676 // Assigning and swapping the equality and hash function objects
Chris@16 677 // needs strong exception safety. To implement that normally we'd
Chris@16 678 // require one of them to be known to not throw and the other to
Chris@16 679 // guarantee strong exception safety. Unfortunately they both only
Chris@16 680 // have basic exception safety. So to acheive strong exception
Chris@16 681 // safety we have storage space for two copies, and assign the new
Chris@16 682 // copies to the unused space. Then switch to using that to use
Chris@16 683 // them. This is implemented in 'set_hash_functions' which
Chris@16 684 // atomically assigns the new function objects in a strongly
Chris@16 685 // exception safe manner.
Chris@16 686
Chris@16 687 template <class H, class P, bool NoThrowMoveAssign>
Chris@16 688 class set_hash_functions;
Chris@16 689
Chris@16 690 template <class H, class P>
Chris@16 691 class functions
Chris@16 692 {
Chris@16 693 public:
Chris@16 694 static const bool nothrow_move_assignable =
Chris@16 695 boost::is_nothrow_move_assignable<H>::value &&
Chris@16 696 boost::is_nothrow_move_assignable<P>::value;
Chris@16 697 static const bool nothrow_move_constructible =
Chris@16 698 boost::is_nothrow_move_constructible<H>::value &&
Chris@16 699 boost::is_nothrow_move_constructible<P>::value;
Chris@16 700
Chris@16 701 private:
Chris@16 702 friend class boost::unordered::detail::set_hash_functions<H, P,
Chris@16 703 nothrow_move_assignable>;
Chris@16 704 functions& operator=(functions const&);
Chris@16 705
Chris@16 706 typedef compressed<H, P> function_pair;
Chris@16 707
Chris@16 708 typedef typename boost::aligned_storage<
Chris@16 709 sizeof(function_pair),
Chris@16 710 boost::alignment_of<function_pair>::value>::type aligned_function;
Chris@16 711
Chris@16 712 bool current_; // The currently active functions.
Chris@16 713 aligned_function funcs_[2];
Chris@16 714
Chris@16 715 function_pair const& current() const {
Chris@16 716 return *static_cast<function_pair const*>(
Chris@16 717 static_cast<void const*>(&funcs_[current_]));
Chris@16 718 }
Chris@16 719
Chris@16 720 function_pair& current() {
Chris@16 721 return *static_cast<function_pair*>(
Chris@16 722 static_cast<void*>(&funcs_[current_]));
Chris@16 723 }
Chris@16 724
Chris@16 725 void construct(bool which, H const& hf, P const& eq)
Chris@16 726 {
Chris@16 727 new((void*) &funcs_[which]) function_pair(hf, eq);
Chris@16 728 }
Chris@16 729
Chris@16 730 void construct(bool which, function_pair const& f,
Chris@16 731 boost::unordered::detail::false_type =
Chris@16 732 boost::unordered::detail::false_type())
Chris@16 733 {
Chris@16 734 new((void*) &funcs_[which]) function_pair(f);
Chris@16 735 }
Chris@16 736
Chris@16 737 void construct(bool which, function_pair& f,
Chris@16 738 boost::unordered::detail::true_type)
Chris@16 739 {
Chris@16 740 new((void*) &funcs_[which]) function_pair(f,
Chris@16 741 boost::unordered::detail::move_tag());
Chris@16 742 }
Chris@16 743
Chris@16 744 void destroy(bool which)
Chris@16 745 {
Chris@16 746 boost::unordered::detail::func::destroy((function_pair*)(&funcs_[which]));
Chris@16 747 }
Chris@16 748
Chris@16 749 public:
Chris@16 750
Chris@16 751 typedef boost::unordered::detail::set_hash_functions<H, P,
Chris@16 752 nothrow_move_assignable> set_hash_functions;
Chris@16 753
Chris@16 754 functions(H const& hf, P const& eq)
Chris@16 755 : current_(false)
Chris@16 756 {
Chris@16 757 construct(current_, hf, eq);
Chris@16 758 }
Chris@16 759
Chris@16 760 functions(functions const& bf)
Chris@16 761 : current_(false)
Chris@16 762 {
Chris@16 763 construct(current_, bf.current());
Chris@16 764 }
Chris@16 765
Chris@16 766 functions(functions& bf, boost::unordered::detail::move_tag)
Chris@16 767 : current_(false)
Chris@16 768 {
Chris@16 769 construct(current_, bf.current(),
Chris@16 770 boost::unordered::detail::integral_constant<bool,
Chris@16 771 nothrow_move_constructible>());
Chris@16 772 }
Chris@16 773
Chris@16 774 ~functions() {
Chris@16 775 this->destroy(current_);
Chris@16 776 }
Chris@16 777
Chris@16 778 H const& hash_function() const {
Chris@16 779 return current().first();
Chris@16 780 }
Chris@16 781
Chris@16 782 P const& key_eq() const {
Chris@16 783 return current().second();
Chris@16 784 }
Chris@16 785 };
Chris@16 786
Chris@16 787 template <class H, class P>
Chris@16 788 class set_hash_functions<H, P, false>
Chris@16 789 {
Chris@16 790 set_hash_functions(set_hash_functions const&);
Chris@16 791 set_hash_functions& operator=(set_hash_functions const&);
Chris@16 792
Chris@16 793 typedef functions<H, P> functions_type;
Chris@16 794
Chris@16 795 functions_type& functions_;
Chris@16 796 bool tmp_functions_;
Chris@16 797
Chris@16 798 public:
Chris@16 799
Chris@16 800 set_hash_functions(functions_type& f, H const& h, P const& p)
Chris@16 801 : functions_(f),
Chris@16 802 tmp_functions_(!f.current_)
Chris@16 803 {
Chris@16 804 f.construct(tmp_functions_, h, p);
Chris@16 805 }
Chris@16 806
Chris@16 807 set_hash_functions(functions_type& f, functions_type const& other)
Chris@16 808 : functions_(f),
Chris@16 809 tmp_functions_(!f.current_)
Chris@16 810 {
Chris@16 811 f.construct(tmp_functions_, other.current());
Chris@16 812 }
Chris@16 813
Chris@16 814 ~set_hash_functions()
Chris@16 815 {
Chris@16 816 functions_.destroy(tmp_functions_);
Chris@16 817 }
Chris@16 818
Chris@16 819 void commit()
Chris@16 820 {
Chris@16 821 functions_.current_ = tmp_functions_;
Chris@16 822 tmp_functions_ = !tmp_functions_;
Chris@16 823 }
Chris@16 824 };
Chris@16 825
Chris@16 826 template <class H, class P>
Chris@16 827 class set_hash_functions<H, P, true>
Chris@16 828 {
Chris@16 829 set_hash_functions(set_hash_functions const&);
Chris@16 830 set_hash_functions& operator=(set_hash_functions const&);
Chris@16 831
Chris@16 832 typedef functions<H, P> functions_type;
Chris@16 833
Chris@16 834 functions_type& functions_;
Chris@16 835 H hash_;
Chris@16 836 P pred_;
Chris@16 837
Chris@16 838 public:
Chris@16 839
Chris@16 840 set_hash_functions(functions_type& f, H const& h, P const& p) :
Chris@16 841 functions_(f),
Chris@16 842 hash_(h),
Chris@16 843 pred_(p) {}
Chris@16 844
Chris@16 845 set_hash_functions(functions_type& f, functions_type const& other) :
Chris@16 846 functions_(f),
Chris@16 847 hash_(other.hash_function()),
Chris@16 848 pred_(other.key_eq()) {}
Chris@16 849
Chris@16 850 void commit()
Chris@16 851 {
Chris@16 852 functions_.current().first() = boost::move(hash_);
Chris@16 853 functions_.current().second() = boost::move(pred_);
Chris@16 854 }
Chris@16 855 };
Chris@16 856
Chris@16 857 ////////////////////////////////////////////////////////////////////////////
Chris@16 858 // rvalue parameters when type can't be a BOOST_RV_REF(T) parameter
Chris@16 859 // e.g. for int
Chris@16 860
Chris@16 861 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
Chris@16 862 # define BOOST_UNORDERED_RV_REF(T) BOOST_RV_REF(T)
Chris@16 863 #else
Chris@16 864 struct please_ignore_this_overload {
Chris@16 865 typedef please_ignore_this_overload type;
Chris@16 866 };
Chris@16 867
Chris@16 868 template <typename T>
Chris@16 869 struct rv_ref_impl {
Chris@16 870 typedef BOOST_RV_REF(T) type;
Chris@16 871 };
Chris@16 872
Chris@16 873 template <typename T>
Chris@16 874 struct rv_ref :
Chris@16 875 boost::detail::if_true<
Chris@16 876 boost::is_class<T>::value
Chris@16 877 >::BOOST_NESTED_TEMPLATE then <
Chris@16 878 boost::unordered::detail::rv_ref_impl<T>,
Chris@16 879 please_ignore_this_overload
Chris@16 880 >::type
Chris@16 881 {};
Chris@16 882
Chris@16 883 # define BOOST_UNORDERED_RV_REF(T) \
Chris@16 884 typename boost::unordered::detail::rv_ref<T>::type
Chris@16 885 #endif
Chris@16 886 }}}
Chris@16 887
Chris@16 888 #endif