buckets.hpp 26 KB

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