comparison DEPENDENCIES/generic/include/boost/intrusive/hashtable.hpp @ 16:2665513ce2d3

Add boost headers
author Chris Cannam
date Tue, 05 Aug 2014 11:11:38 +0100
parents
children c530137014c0
comparison
equal deleted inserted replaced
15:663ca0da4350 16:2665513ce2d3
1 /////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2006-2013
4 //
5 // Distributed under the Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8 //
9 // See http://www.boost.org/libs/intrusive for documentation.
10 //
11 /////////////////////////////////////////////////////////////////////////////
12 #ifndef BOOST_INTRUSIVE_HASHTABLE_HPP
13 #define BOOST_INTRUSIVE_HASHTABLE_HPP
14
15 #include <boost/intrusive/detail/config_begin.hpp>
16 //std C++
17 #include <functional> //std::equal_to
18 #include <utility> //std::pair
19 #include <algorithm> //std::swap, std::lower_bound, std::upper_bound
20 #include <cstddef> //std::size_t
21 //boost
22 #include <boost/intrusive/detail/assert.hpp>
23 #include <boost/static_assert.hpp>
24 #include <boost/functional/hash.hpp>
25 #include <boost/pointer_cast.hpp>
26 //General intrusive utilities
27 #include <boost/intrusive/intrusive_fwd.hpp>
28 #include <boost/intrusive/detail/hashtable_node.hpp>
29 #include <boost/intrusive/detail/transform_iterator.hpp>
30 #include <boost/intrusive/link_mode.hpp>
31 #include <boost/intrusive/detail/ebo_functor_holder.hpp>
32 #include <boost/intrusive/detail/clear_on_destructor_base.hpp>
33 #include <boost/intrusive/detail/utilities.hpp>
34 //Implementation utilities
35 #include <boost/intrusive/unordered_set_hook.hpp>
36 #include <boost/intrusive/slist.hpp>
37 #include <boost/intrusive/pointer_traits.hpp>
38 #include <boost/intrusive/detail/mpl.hpp>
39 #include <boost/move/move.hpp>
40
41 namespace boost {
42 namespace intrusive {
43
44 /// @cond
45
46 struct hash_bool_flags
47 {
48 static const std::size_t unique_keys_pos = 1u;
49 static const std::size_t constant_time_size_pos = 2u;
50 static const std::size_t power_2_buckets_pos = 4u;
51 static const std::size_t cache_begin_pos = 8u;
52 static const std::size_t compare_hash_pos = 16u;
53 static const std::size_t incremental_pos = 32u;
54 };
55
56 namespace detail {
57
58 template<class SupposedValueTraits>
59 struct get_slist_impl_from_supposed_value_traits
60 {
61 typedef typename detail::get_real_value_traits
62 <SupposedValueTraits>::type real_value_traits;
63 typedef typename detail::get_node_traits
64 <real_value_traits>::type node_traits;
65 typedef typename get_slist_impl
66 <typename reduced_slist_node_traits
67 <node_traits>::type
68 >::type type;
69 };
70
71 template<class SupposedValueTraits>
72 struct unordered_bucket_impl
73 {
74 typedef typename
75 get_slist_impl_from_supposed_value_traits
76 <SupposedValueTraits>::type slist_impl;
77 typedef detail::bucket_impl<slist_impl> implementation_defined;
78 typedef implementation_defined type;
79 };
80
81 template<class SupposedValueTraits>
82 struct unordered_bucket_ptr_impl
83 {
84 typedef typename detail::get_node_traits
85 <SupposedValueTraits>::type::node_ptr node_ptr;
86 typedef typename unordered_bucket_impl
87 <SupposedValueTraits>::type bucket_type;
88
89 typedef typename pointer_traits
90 <node_ptr>::template rebind_pointer
91 < bucket_type >::type implementation_defined;
92 typedef implementation_defined type;
93 };
94
95 template <class T>
96 struct store_hash_bool
97 {
98 template<bool Add>
99 struct two_or_three {one _[2 + Add];};
100 template <class U> static one test(...);
101 template <class U> static two_or_three<U::store_hash> test (int);
102 static const std::size_t value = sizeof(test<T>(0));
103 };
104
105 template <class T>
106 struct store_hash_is_true
107 {
108 static const bool value = store_hash_bool<T>::value > sizeof(one)*2;
109 };
110
111 template <class T>
112 struct optimize_multikey_bool
113 {
114 template<bool Add>
115 struct two_or_three {one _[2 + Add];};
116 template <class U> static one test(...);
117 template <class U> static two_or_three<U::optimize_multikey> test (int);
118 static const std::size_t value = sizeof(test<T>(0));
119 };
120
121 template <class T>
122 struct optimize_multikey_is_true
123 {
124 static const bool value = optimize_multikey_bool<T>::value > sizeof(one)*2;
125 };
126
127 struct insert_commit_data_impl
128 {
129 std::size_t hash;
130 };
131
132 template<class Node, class SlistNodePtr>
133 inline typename pointer_traits<SlistNodePtr>::template rebind_pointer<Node>::type
134 dcast_bucket_ptr(const SlistNodePtr &p)
135 {
136 typedef typename pointer_traits<SlistNodePtr>::template rebind_pointer<Node>::type node_ptr;
137 return pointer_traits<node_ptr>::pointer_to(static_cast<Node&>(*p));
138 }
139
140 template<class NodeTraits>
141 struct group_functions
142 {
143 typedef NodeTraits node_traits;
144 typedef unordered_group_adapter<node_traits> group_traits;
145 typedef typename node_traits::node_ptr node_ptr;
146 typedef typename node_traits::node node;
147 typedef typename reduced_slist_node_traits
148 <node_traits>::type reduced_node_traits;
149 typedef typename reduced_node_traits::node_ptr slist_node_ptr;
150 typedef typename reduced_node_traits::node slist_node;
151 typedef circular_slist_algorithms<group_traits> group_algorithms;
152
153 static slist_node_ptr get_bucket_before_begin
154 (const slist_node_ptr &bucket_beg, const slist_node_ptr &bucket_end, const node_ptr &p)
155 {
156 //First find the last node of p's group.
157 //This requires checking the first node of the next group or
158 //the bucket node.
159 node_ptr prev_node = p;
160 node_ptr nxt(node_traits::get_next(p));
161 while(!(bucket_beg <= nxt && nxt <= bucket_end) &&
162 (group_traits::get_next(nxt) == prev_node)){
163 prev_node = nxt;
164 nxt = node_traits::get_next(nxt);
165 }
166
167 //If we've reached the bucket node just return it.
168 if(bucket_beg <= nxt && nxt <= bucket_end){
169 return nxt;
170 }
171
172 //Otherwise, iterate using group links until the bucket node
173 node_ptr first_node_of_group = nxt;
174 node_ptr last_node_group = group_traits::get_next(first_node_of_group);
175 slist_node_ptr possible_end = node_traits::get_next(last_node_group);
176
177 while(!(bucket_beg <= possible_end && possible_end <= bucket_end)){
178 first_node_of_group = detail::dcast_bucket_ptr<node>(possible_end);
179 last_node_group = group_traits::get_next(first_node_of_group);
180 possible_end = node_traits::get_next(last_node_group);
181 }
182 return possible_end;
183 }
184
185 static node_ptr get_prev_to_first_in_group(const slist_node_ptr &bucket_node, const node_ptr &first_in_group)
186 {
187 //Just iterate using group links and obtain the node
188 //before "first_in_group)"
189 node_ptr prev_node = detail::dcast_bucket_ptr<node>(bucket_node);
190 node_ptr nxt(node_traits::get_next(prev_node));
191 while(nxt != first_in_group){
192 prev_node = group_traits::get_next(nxt);
193 nxt = node_traits::get_next(prev_node);
194 }
195 return prev_node;
196 }
197
198 static node_ptr get_first_in_group_of_last_in_group(const node_ptr &last_in_group)
199 {
200 //Just iterate using group links and obtain the node
201 //before "last_in_group"
202 node_ptr possible_first = group_traits::get_next(last_in_group);
203 node_ptr possible_first_prev = group_traits::get_next(possible_first);
204 // The deleted node is at the end of the group, so the
205 // node in the group pointing to it is at the beginning
206 // of the group. Find that to change its pointer.
207 while(possible_first_prev != last_in_group){
208 possible_first = possible_first_prev;
209 possible_first_prev = group_traits::get_next(possible_first);
210 }
211 return possible_first;
212 }
213
214 static void erase_from_group(const slist_node_ptr &end_ptr, const node_ptr &to_erase_ptr, detail::true_)
215 {
216 node_ptr nxt_ptr(node_traits::get_next(to_erase_ptr));
217 node_ptr prev_in_group_ptr(group_traits::get_next(to_erase_ptr));
218 bool last_in_group = (end_ptr == nxt_ptr) ||
219 (group_traits::get_next(nxt_ptr) != to_erase_ptr);
220 bool is_first_in_group = node_traits::get_next(prev_in_group_ptr) != to_erase_ptr;
221
222 if(is_first_in_group && last_in_group){
223 group_algorithms::init(to_erase_ptr);
224 }
225 else if(is_first_in_group){
226 group_algorithms::unlink_after(nxt_ptr);
227 }
228 else if(last_in_group){
229 node_ptr first_in_group =
230 get_first_in_group_of_last_in_group(to_erase_ptr);
231 group_algorithms::unlink_after(first_in_group);
232 }
233 else{
234 group_algorithms::unlink_after(nxt_ptr);
235 }
236 }
237
238 static void erase_from_group(const slist_node_ptr&, const node_ptr&, detail::false_)
239 {}
240
241 static node_ptr get_last_in_group(const node_ptr &first_in_group, detail::true_)
242 { return group_traits::get_next(first_in_group); }
243
244 static node_ptr get_last_in_group(const node_ptr &n, detail::false_)
245 { return n; }
246
247 static void init_group(const node_ptr &n, true_)
248 { group_algorithms::init(n); }
249
250 static void init_group(const node_ptr &, false_)
251 {}
252
253 static void insert_in_group(const node_ptr &first_in_group, const node_ptr &n, true_)
254 {
255 if(first_in_group){
256 if(group_algorithms::unique(first_in_group))
257 group_algorithms::link_after(first_in_group, n);
258 else{
259 group_algorithms::link_after(group_algorithms::node_traits::get_next(first_in_group), n);
260 }
261 }
262 else{
263 group_algorithms::init_header(n);
264 }
265 }
266
267 static slist_node_ptr get_previous_and_next_in_group
268 ( const slist_node_ptr &i, node_ptr &nxt_in_group
269 //If first_end_ptr == last_end_ptr, then first_end_ptr is the bucket of i
270 //Otherwise first_end_ptr is the first bucket and last_end_ptr the last one.
271 , const slist_node_ptr &first_end_ptr, const slist_node_ptr &last_end_ptr)
272 {
273 slist_node_ptr prev;
274 node_ptr elem(detail::dcast_bucket_ptr<node>(i));
275
276 //It's the last in group if the next_node is a bucket
277 slist_node_ptr nxt(node_traits::get_next(elem));
278 bool last_in_group = (first_end_ptr <= nxt && nxt <= last_end_ptr)/* ||
279 (group_traits::get_next(nxt) != elem)*/;
280 //It's the first in group if group_previous's next_node is not
281 //itself, as group list does not link bucket
282 node_ptr prev_in_group(group_traits::get_next(elem));
283 bool first_in_group = node_traits::get_next(prev_in_group) != elem;
284
285 if(first_in_group){
286 node_ptr start_pos;
287 if(last_in_group){
288 start_pos = elem;
289 nxt_in_group = node_ptr();
290 }
291 else{
292 start_pos = prev_in_group;
293 nxt_in_group = node_traits::get_next(elem);
294 }
295 slist_node_ptr bucket_node;
296 if(first_end_ptr != last_end_ptr){
297 bucket_node = group_functions::get_bucket_before_begin
298 (first_end_ptr, last_end_ptr, start_pos);
299 }
300 else{
301 bucket_node = first_end_ptr;
302 }
303 prev = group_functions::get_prev_to_first_in_group(bucket_node, elem);
304 }
305 else{
306 if(last_in_group){
307 nxt_in_group = group_functions::get_first_in_group_of_last_in_group(elem);
308 }
309 else{
310 nxt_in_group = node_traits::get_next(elem);
311 }
312 prev = group_traits::get_next(elem);
313 }
314 return prev;
315 }
316
317 static void insert_in_group(const node_ptr&, const node_ptr&, false_)
318 {}
319 };
320
321 template<class BucketType, class SplitTraits>
322 class incremental_rehash_rollback
323 {
324 private:
325 typedef BucketType bucket_type;
326 typedef SplitTraits split_traits;
327
328 incremental_rehash_rollback();
329 incremental_rehash_rollback & operator=(const incremental_rehash_rollback &);
330 incremental_rehash_rollback (const incremental_rehash_rollback &);
331
332 public:
333 incremental_rehash_rollback
334 (bucket_type &source_bucket, bucket_type &destiny_bucket, split_traits &split_traits)
335 : source_bucket_(source_bucket), destiny_bucket_(destiny_bucket)
336 , split_traits_(split_traits), released_(false)
337 {}
338
339 void release()
340 { released_ = true; }
341
342 ~incremental_rehash_rollback()
343 {
344 if(!released_){
345 //If an exception is thrown, just put all moved nodes back in the old bucket
346 //and move back the split mark.
347 destiny_bucket_.splice_after(destiny_bucket_.before_begin(), source_bucket_);
348 split_traits_.decrement();
349 }
350 }
351
352 private:
353 bucket_type &source_bucket_;
354 bucket_type &destiny_bucket_;
355 split_traits &split_traits_;
356 bool released_;
357 };
358
359 template<class NodeTraits>
360 struct node_functions
361 {
362 static void store_hash(typename NodeTraits::node_ptr p, std::size_t h, true_)
363 { return NodeTraits::set_hash(p, h); }
364
365 static void store_hash(typename NodeTraits::node_ptr, std::size_t, false_)
366 {}
367 };
368
369 inline std::size_t hash_to_bucket(std::size_t hash_value, std::size_t bucket_cnt, detail::false_)
370 { return hash_value % bucket_cnt; }
371
372 inline std::size_t hash_to_bucket(std::size_t hash_value, std::size_t bucket_cnt, detail::true_)
373 { return hash_value & (bucket_cnt - 1); }
374
375 template<bool Power2Buckets, bool Incremental>
376 inline std::size_t hash_to_bucket_split(std::size_t hash_value, std::size_t bucket_cnt, std::size_t split)
377 {
378 std::size_t bucket_number = detail::hash_to_bucket(hash_value, bucket_cnt, detail::bool_<Power2Buckets>());
379 if(Incremental)
380 if(bucket_number >= split)
381 bucket_number -= bucket_cnt/2;
382 return bucket_number;
383 }
384
385 } //namespace detail {
386
387 //!This metafunction will obtain the type of a bucket
388 //!from the value_traits or hook option to be used with
389 //!a hash container.
390 template<class ValueTraitsOrHookOption>
391 struct unordered_bucket
392 : public detail::unordered_bucket_impl
393 <typename ValueTraitsOrHookOption::
394 template pack<none>::proto_value_traits
395 >
396 {};
397
398 //!This metafunction will obtain the type of a bucket pointer
399 //!from the value_traits or hook option to be used with
400 //!a hash container.
401 template<class ValueTraitsOrHookOption>
402 struct unordered_bucket_ptr
403 : public detail::unordered_bucket_ptr_impl
404 <typename ValueTraitsOrHookOption::
405 template pack<none>::proto_value_traits
406 >
407 {};
408
409 //!This metafunction will obtain the type of the default bucket traits
410 //!(when the user does not specify the bucket_traits<> option) from the
411 //!value_traits or hook option to be used with
412 //!a hash container.
413 template<class ValueTraitsOrHookOption>
414 struct unordered_default_bucket_traits
415 {
416 typedef typename ValueTraitsOrHookOption::
417 template pack<none>::proto_value_traits supposed_value_traits;
418 typedef typename detail::
419 get_slist_impl_from_supposed_value_traits
420 <supposed_value_traits>::type slist_impl;
421 typedef detail::bucket_traits_impl
422 <slist_impl> implementation_defined;
423 typedef implementation_defined type;
424 };
425
426 struct default_bucket_traits;
427
428 struct hashtable_defaults
429 {
430 typedef detail::default_hashtable_hook proto_value_traits;
431 typedef std::size_t size_type;
432 typedef void equal;
433 typedef void hash;
434 typedef default_bucket_traits bucket_traits;
435 static const bool constant_time_size = true;
436 static const bool power_2_buckets = false;
437 static const bool cache_begin = false;
438 static const bool compare_hash = false;
439 static const bool incremental = false;
440 };
441
442 template<class RealValueTraits, bool IsConst>
443 struct downcast_node_to_value_t
444 : public detail::node_to_value<RealValueTraits, IsConst>
445 {
446 typedef detail::node_to_value<RealValueTraits, IsConst> base_t;
447 typedef typename base_t::result_type result_type;
448 typedef RealValueTraits real_value_traits;
449 typedef typename detail::get_slist_impl
450 <typename detail::reduced_slist_node_traits
451 <typename real_value_traits::node_traits>::type
452 >::type slist_impl;
453 typedef typename detail::add_const_if_c
454 <typename slist_impl::node, IsConst>::type & first_argument_type;
455 typedef typename detail::add_const_if_c
456 < typename RealValueTraits::node_traits::node
457 , IsConst>::type & intermediate_argument_type;
458 typedef typename pointer_traits
459 <typename RealValueTraits::pointer>::
460 template rebind_pointer
461 <const RealValueTraits>::type const_real_value_traits_ptr;
462
463 downcast_node_to_value_t(const const_real_value_traits_ptr &ptr)
464 : base_t(ptr)
465 {}
466
467 result_type operator()(first_argument_type arg) const
468 { return this->base_t::operator()(static_cast<intermediate_argument_type>(arg)); }
469 };
470
471 template<class F, class SlistNodePtr, class NodePtr>
472 struct node_cast_adaptor
473 : private detail::ebo_functor_holder<F>
474 {
475 typedef detail::ebo_functor_holder<F> base_t;
476
477 typedef typename pointer_traits<SlistNodePtr>::element_type slist_node;
478 typedef typename pointer_traits<NodePtr>::element_type node;
479
480 template<class ConvertibleToF, class RealValuTraits>
481 node_cast_adaptor(const ConvertibleToF &c2f, const RealValuTraits *traits)
482 : base_t(base_t(c2f, traits))
483 {}
484
485 typename base_t::node_ptr operator()(const slist_node &to_clone)
486 { return base_t::operator()(static_cast<const node &>(to_clone)); }
487
488 void operator()(SlistNodePtr to_clone)
489 {
490 base_t::operator()(pointer_traits<NodePtr>::pointer_to(static_cast<node &>(*to_clone)));
491 }
492 };
493
494 static const std::size_t hashtable_data_bool_flags_mask =
495 ( hash_bool_flags::cache_begin_pos
496 | hash_bool_flags::constant_time_size_pos
497 | hash_bool_flags::incremental_pos
498 );
499
500 template<class ValueTraits, class BucketTraits>
501 struct bucket_plus_vtraits : public ValueTraits
502 {
503 typedef BucketTraits bucket_traits;
504 typedef ValueTraits value_traits;
505
506 static const bool external_value_traits =
507 detail::external_value_traits_bool_is_true<ValueTraits>::value;
508
509 static const bool external_bucket_traits =
510 detail::external_bucket_traits_bool_is_true<bucket_traits>::value;
511
512 typedef typename detail::get_real_value_traits<ValueTraits>::type real_value_traits;
513
514 static const bool safemode_or_autounlink = is_safe_autounlink<real_value_traits::link_mode>::value;
515
516 typedef typename detail::eval_if_c
517 < external_bucket_traits
518 , detail::eval_bucket_traits<bucket_traits>
519 , detail::identity<bucket_traits>
520 >::type real_bucket_traits;
521 typedef typename
522 detail::get_slist_impl_from_supposed_value_traits
523 <real_value_traits>::type slist_impl;
524
525 template<class BucketTraitsType>
526 bucket_plus_vtraits(const ValueTraits &val_traits, BOOST_FWD_REF(BucketTraitsType) b_traits)
527 : ValueTraits(val_traits), bucket_traits_(::boost::forward<BucketTraitsType>(b_traits))
528 {}
529
530 bucket_plus_vtraits & operator =(const bucket_plus_vtraits &x)
531 {
532 bucket_traits_ = x.bucket_traits_;
533 return *this;
534 }
535
536 //real_value_traits
537 //
538 const real_value_traits &priv_real_value_traits(detail::false_) const
539 { return *this; }
540
541 const real_value_traits &priv_real_value_traits(detail::true_) const
542 { return this->get_value_traits(*this); }
543
544 real_value_traits &priv_real_value_traits(detail::false_)
545 { return *this; }
546
547 real_value_traits &priv_real_value_traits(detail::true_)
548 { return this->get_value_traits(*this); }
549
550 const real_value_traits &priv_real_value_traits() const
551 { return this->priv_real_value_traits(detail::bool_<external_value_traits>()); }
552
553 real_value_traits &priv_real_value_traits()
554 { return this->priv_real_value_traits(detail::bool_<external_value_traits>()); }
555
556 typedef typename pointer_traits<typename real_value_traits::pointer>::
557 template rebind_pointer<const real_value_traits>::type const_real_value_traits_ptr;
558
559 const_real_value_traits_ptr real_value_traits_ptr() const
560 { return pointer_traits<const_real_value_traits_ptr>::pointer_to(this->priv_real_value_traits()); }
561
562 //real_bucket_traits
563 //
564 const real_bucket_traits &priv_real_bucket_traits(detail::false_) const
565 { return this->bucket_traits_; }
566
567 const real_bucket_traits &priv_real_bucket_traits(detail::true_) const
568 { return this->bucket_traits_.get_bucket_traits(*this); }
569
570 real_bucket_traits &priv_real_bucket_traits(detail::false_)
571 { return bucket_traits_; }
572
573 real_bucket_traits &priv_real_bucket_traits(detail::true_)
574 { return this->get_bucket_traits(*this); }
575
576 const real_bucket_traits &priv_real_bucket_traits() const
577 { return this->priv_real_bucket_traits(detail::bool_<external_bucket_traits>()); }
578
579 real_bucket_traits &priv_real_bucket_traits()
580 { return this->priv_real_bucket_traits(detail::bool_<external_bucket_traits>()); }
581
582 //bucket_value_traits
583 //
584 const bucket_plus_vtraits &get_bucket_value_traits() const
585 { return *this; }
586
587 bucket_plus_vtraits &get_bucket_value_traits()
588 { return *this; }
589
590 typedef typename pointer_traits<typename real_value_traits::pointer>::
591 template rebind_pointer<const bucket_plus_vtraits>::type const_bucket_value_traits_ptr;
592
593 const_bucket_value_traits_ptr bucket_value_traits_ptr() const
594 { return pointer_traits<const_bucket_value_traits_ptr>::pointer_to(this->get_bucket_value_traits()); }
595
596 //value traits
597 //
598 const value_traits &priv_value_traits() const
599 { return *this; }
600
601 value_traits &priv_value_traits()
602 { return *this; }
603
604 //bucket_traits
605 //
606 const bucket_traits &priv_bucket_traits() const
607 { return this->bucket_traits_; }
608
609 bucket_traits &priv_bucket_traits()
610 { return this->bucket_traits_; }
611
612 //operations
613 typedef typename detail::unordered_bucket_ptr_impl<real_value_traits>::type bucket_ptr;
614
615 bucket_ptr priv_bucket_pointer() const
616 { return this->priv_real_bucket_traits().bucket_begin(); }
617
618 typename slist_impl::size_type priv_bucket_count() const
619 { return this->priv_real_bucket_traits().bucket_count(); }
620
621 bucket_ptr priv_invalid_bucket() const
622 {
623 const real_bucket_traits &rbt = this->priv_real_bucket_traits();
624 return rbt.bucket_begin() + rbt.bucket_count();
625 }
626
627 typedef typename real_value_traits::node_traits node_traits;
628 typedef unordered_group_adapter<node_traits> group_traits;
629 typedef typename slist_impl::iterator siterator;
630 typedef typename slist_impl::size_type size_type;
631 typedef detail::bucket_impl<slist_impl> bucket_type;
632 typedef detail::group_functions<node_traits> group_functions_t;
633 typedef typename slist_impl::node_algorithms node_algorithms;
634 typedef typename slist_impl::node_ptr slist_node_ptr;
635 typedef typename node_traits::node_ptr node_ptr;
636 typedef typename node_traits::node node;
637 typedef typename real_value_traits::value_type value_type;
638 typedef circular_slist_algorithms<group_traits> group_algorithms;
639
640
641 /*
642 siterator priv_invalid_local_it() const
643 { return this->priv_invalid_bucket()->end(); }
644 */
645 siterator priv_invalid_local_it() const
646 {
647 return this->priv_real_bucket_traits().bucket_begin()->before_begin();
648 }
649
650 ///
651 static siterator priv_get_last(bucket_type &b, detail::true_) //optimize multikey
652 {
653 //First find the last node of p's group.
654 //This requires checking the first node of the next group or
655 //the bucket node.
656 slist_node_ptr end_ptr(b.end().pointed_node());
657 node_ptr possible_end(node_traits::get_next( detail::dcast_bucket_ptr<node>(end_ptr)));
658 node_ptr last_node_group(possible_end);
659
660 while(end_ptr != possible_end){
661 last_node_group = group_traits::get_next(detail::dcast_bucket_ptr<node>(possible_end));
662 possible_end = node_traits::get_next(last_node_group);
663 }
664 return bucket_type::s_iterator_to(*last_node_group);
665 }
666
667 static siterator priv_get_last(bucket_type &b, detail::false_) //NOT optimize multikey
668 { return b.previous(b.end()); }
669
670 static siterator priv_get_previous(bucket_type &b, siterator i, detail::true_) //optimize multikey
671 {
672 node_ptr elem(detail::dcast_bucket_ptr<node>(i.pointed_node()));
673 node_ptr prev_in_group(group_traits::get_next(elem));
674 bool first_in_group = node_traits::get_next(prev_in_group) != elem;
675 typename bucket_type::node &n = first_in_group
676 ? *group_functions_t::get_prev_to_first_in_group(b.end().pointed_node(), elem)
677 : *group_traits::get_next(elem)
678 ;
679 return bucket_type::s_iterator_to(n);
680 }
681
682 static siterator priv_get_previous(bucket_type &b, siterator i, detail::false_) //NOT optimize multikey
683 { return b.previous(i); }
684
685 static void priv_clear_group_nodes(bucket_type &b, detail::true_) //optimize multikey
686 {
687 siterator it(b.begin()), itend(b.end());
688 while(it != itend){
689 node_ptr to_erase(detail::dcast_bucket_ptr<node>(it.pointed_node()));
690 ++it;
691 group_algorithms::init(to_erase);
692 }
693 }
694
695 static void priv_clear_group_nodes(bucket_type &, detail::false_) //NOT optimize multikey
696 {}
697
698 std::size_t priv_get_bucket_num_no_hash_store(siterator it, detail::true_) //optimize multikey
699 {
700 const bucket_ptr f(this->priv_bucket_pointer()), l(f + this->priv_bucket_count() - 1);
701 slist_node_ptr bb = group_functions_t::get_bucket_before_begin
702 ( f->end().pointed_node()
703 , l->end().pointed_node()
704 , detail::dcast_bucket_ptr<node>(it.pointed_node()));
705 //Now get the bucket_impl from the iterator
706 const bucket_type &b = static_cast<const bucket_type&>
707 (bucket_type::slist_type::container_from_end_iterator(bucket_type::s_iterator_to(*bb)));
708 //Now just calculate the index b has in the bucket array
709 return static_cast<size_type>(&b - &*f);
710 }
711
712 std::size_t priv_get_bucket_num_no_hash_store(siterator it, detail::false_) //NO optimize multikey
713 {
714 bucket_ptr f(this->priv_bucket_pointer()), l(f + this->priv_bucket_count() - 1);
715 slist_node_ptr first_ptr(f->cend().pointed_node())
716 , last_ptr(l->cend().pointed_node());
717
718 //The end node is embedded in the singly linked list:
719 //iterate until we reach it.
720 while(!(first_ptr <= it.pointed_node() && it.pointed_node() <= last_ptr)){
721 ++it;
722 }
723 //Now get the bucket_impl from the iterator
724 const bucket_type &b = static_cast<const bucket_type&>
725 (bucket_type::container_from_end_iterator(it));
726
727 //Now just calculate the index b has in the bucket array
728 return static_cast<std::size_t>(&b - &*f);
729 }
730
731 static std::size_t priv_stored_hash(slist_node_ptr n, detail::true_) //store_hash
732 { return node_traits::get_hash(detail::dcast_bucket_ptr<node>(n)); }
733
734 static std::size_t priv_stored_hash(slist_node_ptr, detail::false_) //NO store_hash
735 {
736 //This code should never be reached!
737 BOOST_INTRUSIVE_INVARIANT_ASSERT(0);
738 return 0;
739 }
740
741 node &priv_value_to_node(value_type &v)
742 { return *this->priv_real_value_traits().to_node_ptr(v); }
743
744 const node &priv_value_to_node(const value_type &v) const
745 { return *this->priv_real_value_traits().to_node_ptr(v); }
746
747 value_type &priv_value_from_slist_node(slist_node_ptr n)
748 { return *this->priv_real_value_traits().to_value_ptr(detail::dcast_bucket_ptr<node>(n)); }
749
750 const value_type &priv_value_from_slist_node(slist_node_ptr n) const
751 { return *this->priv_real_value_traits().to_value_ptr(detail::dcast_bucket_ptr<node>(n)); }
752
753 bucket_traits bucket_traits_;
754 };
755
756 template<class VoidOrKeyHash, class ValueTraits, class BucketTraits>
757 struct bucket_hash_t
758 : public detail::ebo_functor_holder
759 <typename get_hash< VoidOrKeyHash
760 , typename bucket_plus_vtraits<ValueTraits,BucketTraits>::real_value_traits::value_type
761 >::type
762 >
763 , bucket_plus_vtraits<ValueTraits, BucketTraits>
764 {
765 typedef typename bucket_plus_vtraits<ValueTraits,BucketTraits>::real_value_traits real_value_traits;
766 typedef typename real_value_traits::value_type value_type;
767 typedef typename real_value_traits::node_traits node_traits;
768 typedef typename get_hash< VoidOrKeyHash, value_type>::type hasher;
769 typedef BucketTraits bucket_traits;
770 typedef bucket_plus_vtraits<ValueTraits, BucketTraits> bucket_plus_vtraits_t;
771
772 template<class BucketTraitsType>
773 bucket_hash_t(const ValueTraits &val_traits, BOOST_FWD_REF(BucketTraitsType) b_traits, const hasher & h)
774 : detail::ebo_functor_holder<hasher>(h), bucket_plus_vtraits_t(val_traits, ::boost::forward<BucketTraitsType>(b_traits))
775 {}
776
777 const hasher &priv_hasher() const
778 { return this->detail::ebo_functor_holder<hasher>::get(); }
779
780 hasher &priv_hasher()
781 { return this->detail::ebo_functor_holder<hasher>::get(); }
782
783 std::size_t priv_stored_or_compute_hash(const value_type &v, detail::true_) const //For store_hash == true
784 { return node_traits::get_hash(this->priv_real_value_traits().to_node_ptr(v)); }
785
786 std::size_t priv_stored_or_compute_hash(const value_type &v, detail::false_) const //For store_hash == false
787 { return this->priv_hasher()(v); }
788 };
789
790 template<class VoidOrKeyHash, class VoidOrKeyEqual, class ValueTraits, class BucketTraits, bool>
791 struct bucket_hash_equal_t
792 : public detail::ebo_functor_holder //equal
793 <typename get_equal_to< VoidOrKeyEqual
794 , typename bucket_plus_vtraits<ValueTraits,BucketTraits>::real_value_traits::value_type
795 >::type
796 >
797 , bucket_hash_t<VoidOrKeyHash, ValueTraits, BucketTraits>
798 {
799 typedef bucket_hash_t<VoidOrKeyHash, ValueTraits, BucketTraits> bucket_hash_type;
800 typedef typename bucket_plus_vtraits<ValueTraits,BucketTraits>::real_value_traits real_value_traits;
801 typedef typename get_equal_to< VoidOrKeyEqual
802 , typename real_value_traits::value_type
803 >::type value_equal;
804 typedef typename bucket_hash_type::hasher hasher;
805 typedef BucketTraits bucket_traits;
806 typedef bucket_hash_t<VoidOrKeyHash, ValueTraits, BucketTraits> buckethash_t;
807 typedef typename bucket_hash_type::real_bucket_traits real_bucket_traits;
808 typedef typename bucket_hash_type::slist_impl slist_impl;
809 typedef typename slist_impl::size_type size_type;
810 typedef typename slist_impl::iterator siterator;
811 typedef detail::bucket_impl<slist_impl> bucket_type;
812 typedef typename detail::unordered_bucket_ptr_impl<real_value_traits>::type bucket_ptr;
813
814 template<class BucketTraitsType>
815 bucket_hash_equal_t(const ValueTraits &val_traits, BOOST_FWD_REF(BucketTraitsType) b_traits, const hasher & h, const value_equal &e)
816 : detail::ebo_functor_holder<value_equal>(e)
817 , buckethash_t(val_traits, ::boost::forward<BucketTraitsType>(b_traits), h)
818 {}
819
820 bucket_ptr priv_get_cache()
821 { return this->priv_bucket_pointer(); }
822
823 void priv_set_cache(const bucket_ptr &)
824 {}
825
826 size_type priv_get_cache_bucket_num()
827 { return 0u; }
828
829 void priv_initialize_cache()
830 {}
831
832 void priv_swap_cache(bucket_hash_equal_t &)
833 {}
834
835 siterator priv_begin() const
836 {
837 size_type n = 0;
838 size_type bucket_cnt = this->priv_bucket_count();
839 for (n = 0; n < bucket_cnt; ++n){
840 bucket_type &b = this->priv_bucket_pointer()[n];
841 if(!b.empty()){
842 return b.begin();
843 }
844 }
845 return this->priv_invalid_local_it();
846 }
847
848 void priv_insertion_update_cache(size_type)
849 {}
850
851 void priv_erasure_update_cache_range(size_type, size_type)
852 {}
853
854 void priv_erasure_update_cache()
855 {}
856
857 const value_equal &priv_equal() const
858 { return this->detail::ebo_functor_holder<value_equal>::get(); }
859
860 value_equal &priv_equal()
861 { return this->detail::ebo_functor_holder<value_equal>::get(); }
862 };
863
864 template<class VoidOrKeyHash, class VoidOrKeyEqual, class ValueTraits, class BucketTraits> //cache_begin == true version
865 struct bucket_hash_equal_t<VoidOrKeyHash, VoidOrKeyEqual, ValueTraits, BucketTraits, true>
866 : public detail::ebo_functor_holder //equal
867 <typename get_equal_to< VoidOrKeyEqual
868 , typename bucket_plus_vtraits<ValueTraits,BucketTraits>::real_value_traits::value_type
869 >::type
870 >
871 , public bucket_hash_t<VoidOrKeyHash, ValueTraits, BucketTraits>
872 {
873 typedef bucket_hash_t<VoidOrKeyHash, ValueTraits, BucketTraits> bucket_hash_type;
874 typedef typename get_equal_to< VoidOrKeyEqual
875 , typename bucket_plus_vtraits<ValueTraits,BucketTraits>::real_value_traits::value_type
876 >::type value_equal;
877 typedef typename bucket_hash_type::hasher hasher;
878 typedef BucketTraits bucket_traits;
879 typedef typename bucket_hash_type::slist_impl::size_type size_type;
880 typedef typename bucket_hash_type::slist_impl::iterator siterator;
881
882 template<class BucketTraitsType>
883 bucket_hash_equal_t(const ValueTraits &val_traits, BOOST_FWD_REF(BucketTraitsType) b_traits, const hasher & h, const value_equal &e)
884 : detail::ebo_functor_holder<value_equal>(e)
885 , bucket_hash_type(val_traits, ::boost::forward<BucketTraitsType>(b_traits), h)
886 {}
887
888 typedef typename detail::unordered_bucket_ptr_impl
889 <typename bucket_hash_type::real_value_traits>::type bucket_ptr;
890
891 bucket_ptr &priv_get_cache()
892 { return cached_begin_; }
893
894 const bucket_ptr &priv_get_cache() const
895 { return cached_begin_; }
896
897 void priv_set_cache(const bucket_ptr &p)
898 { cached_begin_ = p; }
899
900 std::size_t priv_get_cache_bucket_num()
901 { return this->cached_begin_ - this->priv_bucket_pointer(); }
902
903 void priv_initialize_cache()
904 { this->cached_begin_ = this->priv_invalid_bucket(); }
905
906 void priv_swap_cache(bucket_hash_equal_t &other)
907 {
908 std::swap(this->cached_begin_, other.cached_begin_);
909 }
910
911 siterator priv_begin() const
912 {
913 if(this->cached_begin_ == this->priv_invalid_bucket()){
914 return this->priv_invalid_local_it();
915 }
916 else{
917 return this->cached_begin_->begin();
918 }
919 }
920
921 void priv_insertion_update_cache(size_type insertion_bucket)
922 {
923 bucket_ptr p = this->priv_bucket_pointer() + insertion_bucket;
924 if(p < this->cached_begin_){
925 this->cached_begin_ = p;
926 }
927 }
928
929 const value_equal &priv_equal() const
930 { return this->detail::ebo_functor_holder<value_equal>::get(); }
931
932 value_equal &priv_equal()
933 { return this->detail::ebo_functor_holder<value_equal>::get(); }
934
935 void priv_erasure_update_cache_range(size_type first_bucket_num, size_type last_bucket_num)
936 {
937 //If the last bucket is the end, the cache must be updated
938 //to the last position if all
939 if(this->priv_get_cache_bucket_num() == first_bucket_num &&
940 this->priv_bucket_pointer()[first_bucket_num].empty() ){
941 this->priv_set_cache(this->priv_bucket_pointer() + last_bucket_num);
942 this->priv_erasure_update_cache();
943 }
944 }
945
946 void priv_erasure_update_cache()
947 {
948 if(this->cached_begin_ != this->priv_invalid_bucket()){
949 size_type current_n = this->priv_get_cache() - this->priv_bucket_pointer();
950 for( const size_type num_buckets = this->priv_bucket_count()
951 ; current_n < num_buckets
952 ; ++current_n, ++this->priv_get_cache()){
953 if(!this->priv_get_cache()->empty()){
954 return;
955 }
956 }
957 this->priv_initialize_cache();
958 }
959 }
960
961 private:
962 bucket_ptr cached_begin_;
963 };
964
965 template<class SizeType, std::size_t BoolFlags, class VoidOrKeyHash, class VoidOrKeyEqual, class ValueTraits, class BucketTraits>
966 struct hashdata_internal
967 : public detail::size_holder< 0 != (BoolFlags & hash_bool_flags::incremental_pos), SizeType, int> //split_traits
968 , public bucket_hash_equal_t
969 < VoidOrKeyHash, VoidOrKeyEqual, ValueTraits, BucketTraits
970 , 0 != (BoolFlags & hash_bool_flags::cache_begin_pos)
971 >
972 {
973 typedef bucket_hash_equal_t
974 < VoidOrKeyHash, VoidOrKeyEqual, ValueTraits, BucketTraits
975 , 0 != (BoolFlags & hash_bool_flags::cache_begin_pos)
976 > bucket_hash_equal_type;
977
978 typedef typename bucket_hash_equal_type::value_equal value_equal;
979 typedef typename bucket_hash_equal_type::hasher hasher;
980 typedef bucket_plus_vtraits<ValueTraits,BucketTraits> bucket_plus_vtraits_t;
981 typedef typename bucket_plus_vtraits_t::size_type size_type;
982 typedef typename bucket_plus_vtraits_t::bucket_ptr bucket_ptr;
983 static const bool optimize_multikey
984 = detail::optimize_multikey_is_true<typename bucket_plus_vtraits_t::real_value_traits::node_traits>::value;
985
986 typedef detail::bool_<optimize_multikey> optimize_multikey_t;
987
988 template<class BucketTraitsType>
989 hashdata_internal(const ValueTraits &val_traits, BOOST_FWD_REF(BucketTraitsType) b_traits, const hasher & h, const value_equal &e)
990 : bucket_hash_equal_type(val_traits, ::boost::forward<BucketTraitsType>(b_traits), h, e)
991 {}
992
993 typedef detail::size_holder
994 <0 != (BoolFlags & hash_bool_flags::incremental_pos), SizeType, int> split_traits;
995
996 split_traits &priv_split_traits()
997 { return *this; }
998
999 const split_traits &priv_split_traits() const
1000 { return *this; }
1001 };
1002
1003 template<class SizeType, std::size_t BoolFlags, class VoidOrKeyHash, class VoidOrKeyEqual, class ValueTraits, class BucketTraits>
1004 struct hashtable_data_t
1005 : public detail::size_holder< 0 != (BoolFlags & hash_bool_flags::constant_time_size_pos), SizeType> //size_traits
1006 , public hashdata_internal
1007 < SizeType, BoolFlags & (hash_bool_flags::incremental_pos | hash_bool_flags::cache_begin_pos)
1008 , VoidOrKeyHash, VoidOrKeyEqual, ValueTraits, BucketTraits>
1009 {
1010 static const std::size_t bool_flags = BoolFlags;
1011 typedef detail::size_holder
1012 < 0 != (BoolFlags & hash_bool_flags::constant_time_size_pos)
1013 , SizeType> size_traits;
1014
1015 typedef hashdata_internal
1016 < SizeType, BoolFlags & (hash_bool_flags::incremental_pos | hash_bool_flags::cache_begin_pos)
1017 , VoidOrKeyHash, VoidOrKeyEqual, ValueTraits, BucketTraits> internal_type;
1018
1019 typedef ValueTraits value_traits;
1020 typedef typename internal_type::value_equal value_equal;
1021 typedef typename internal_type::hasher hasher;
1022 typedef BucketTraits bucket_traits;
1023 typedef bucket_plus_vtraits
1024 <ValueTraits,BucketTraits> bucket_plus_vtraits_t;
1025
1026 static const bool external_value_traits =
1027 detail::external_value_traits_bool_is_true<ValueTraits>::value;
1028 static const bool external_bucket_traits = bucket_plus_vtraits_t::external_bucket_traits;
1029
1030 typedef typename bucket_plus_vtraits_t::real_value_traits real_value_traits;
1031 typedef typename bucket_plus_vtraits_t::real_bucket_traits real_bucket_traits;
1032
1033 size_traits &priv_size_traits()
1034 { return *this; }
1035
1036 const size_traits &priv_size_traits() const
1037 { return *this; }
1038
1039 template<class BucketTraitsType>
1040 hashtable_data_t( BOOST_FWD_REF(BucketTraitsType) b_traits, const hasher & h
1041 , const value_equal &e, const value_traits &val_traits)
1042 : size_traits()
1043 , internal_type(val_traits, ::boost::forward<BucketTraitsType>(b_traits), h, e)
1044 {}
1045 };
1046
1047 /// @endcond
1048
1049 //! The class template hashtable is an intrusive hash table container, that
1050 //! is used to construct intrusive unordered_set and unordered_multiset containers. The
1051 //! no-throw guarantee holds only, if the VoidOrKeyEqual object and Hasher don't throw.
1052 //!
1053 //! hashtable is a semi-intrusive container: each object to be stored in the
1054 //! container must contain a proper hook, but the container also needs
1055 //! additional auxiliary memory to work: hashtable needs a pointer to an array
1056 //! of type `bucket_type` to be passed in the constructor. This bucket array must
1057 //! have at least the same lifetime as the container. This makes the use of
1058 //! hashtable more complicated than purely intrusive containers.
1059 //! `bucket_type` is default-constructible, copyable and assignable
1060 //!
1061 //! The template parameter \c T is the type to be managed by the container.
1062 //! The user can specify additional options and if no options are provided
1063 //! default options are used.
1064 //!
1065 //! The container supports the following options:
1066 //! \c base_hook<>/member_hook<>/value_traits<>,
1067 //! \c constant_time_size<>, \c size_type<>, \c hash<> and \c equal<>
1068 //! \c bucket_traits<>, power_2_buckets<>, cache_begin<> and incremental<>.
1069 //!
1070 //! hashtable only provides forward iterators but it provides 4 iterator types:
1071 //! iterator and const_iterator to navigate through the whole container and
1072 //! local_iterator and const_local_iterator to navigate through the values
1073 //! stored in a single bucket. Local iterators are faster and smaller.
1074 //!
1075 //! It's not recommended to use non constant-time size hashtables because several
1076 //! key functions, like "empty()", become non-constant time functions. Non
1077 //! constant_time size hashtables are mainly provided to support auto-unlink hooks.
1078 //!
1079 //! hashtables, does not make automatic rehashings nor
1080 //! offers functions related to a load factor. Rehashing can be explicitly requested
1081 //! and the user must provide a new bucket array that will be used from that moment.
1082 //!
1083 //! Since no automatic rehashing is done, iterators are never invalidated when
1084 //! inserting or erasing elements. Iterators are only invalidated when rehashing.
1085 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
1086 template<class T, class ...Options>
1087 #else
1088 template<class ValueTraits, class VoidOrKeyHash, class VoidOrKeyEqual, class SizeType, class BucketTraits, std::size_t BoolFlags>
1089 #endif
1090 class hashtable_impl
1091 : public hashtable_data_t
1092 < SizeType
1093 , BoolFlags & hashtable_data_bool_flags_mask
1094 , VoidOrKeyHash, VoidOrKeyEqual, ValueTraits, BucketTraits>
1095 , private detail::clear_on_destructor_base
1096 < hashtable_impl<ValueTraits, VoidOrKeyHash, VoidOrKeyEqual, SizeType, BucketTraits, BoolFlags>
1097 , true //To always clear the bucket array
1098 //is_safe_autounlink<detail::get_real_value_traits<ValueTraits>::type::link_mode>::value
1099 >
1100 {
1101 template<class C, bool> friend class detail::clear_on_destructor_base;
1102 public:
1103 typedef ValueTraits value_traits;
1104
1105 typedef hashtable_data_t
1106 < SizeType
1107 , BoolFlags & hashtable_data_bool_flags_mask
1108 , VoidOrKeyHash, VoidOrKeyEqual, ValueTraits, BucketTraits> data_type;
1109
1110 /// @cond
1111 static const bool external_value_traits = data_type::external_value_traits;
1112 static const bool external_bucket_traits = data_type::external_bucket_traits;
1113
1114 typedef BucketTraits bucket_traits;
1115 typedef typename data_type::real_bucket_traits real_bucket_traits;
1116 typedef typename data_type::real_value_traits real_value_traits;
1117
1118
1119 typedef typename detail::get_slist_impl
1120 <typename detail::reduced_slist_node_traits
1121 <typename real_value_traits::node_traits>::type
1122 >::type slist_impl;
1123 typedef bucket_plus_vtraits<ValueTraits, BucketTraits> bucket_plus_vtraits_t;
1124 typedef typename bucket_plus_vtraits_t::const_real_value_traits_ptr const_real_value_traits_ptr;
1125
1126
1127 /// @endcond
1128
1129 typedef typename real_value_traits::pointer pointer;
1130 typedef typename real_value_traits::const_pointer const_pointer;
1131 typedef typename real_value_traits::value_type value_type;
1132 typedef typename pointer_traits<pointer>::reference reference;
1133 typedef typename pointer_traits<const_pointer>::reference const_reference;
1134 typedef typename pointer_traits<pointer>::difference_type difference_type;
1135 typedef SizeType size_type;
1136 typedef value_type key_type;
1137 typedef typename data_type::value_equal key_equal;
1138 typedef typename data_type::hasher hasher;
1139 typedef detail::bucket_impl<slist_impl> bucket_type;
1140 typedef typename pointer_traits
1141 <pointer>::template rebind_pointer
1142 < bucket_type >::type bucket_ptr;
1143 typedef typename slist_impl::iterator siterator;
1144 typedef typename slist_impl::const_iterator const_siterator;
1145 typedef hashtable_iterator<bucket_plus_vtraits_t, false> iterator;
1146 typedef hashtable_iterator<bucket_plus_vtraits_t, true> const_iterator;
1147 typedef typename real_value_traits::node_traits node_traits;
1148 typedef typename node_traits::node node;
1149 typedef typename pointer_traits
1150 <pointer>::template rebind_pointer
1151 < node >::type node_ptr;
1152 typedef typename pointer_traits
1153 <pointer>::template rebind_pointer
1154 < const node >::type const_node_ptr;
1155 typedef typename slist_impl::node_algorithms node_algorithms;
1156
1157 static const bool stateful_value_traits = detail::is_stateful_value_traits<real_value_traits>::value;
1158 static const bool store_hash = detail::store_hash_is_true<node_traits>::value;
1159
1160 static const bool unique_keys = 0 != (BoolFlags & hash_bool_flags::unique_keys_pos);
1161 static const bool constant_time_size = 0 != (BoolFlags & hash_bool_flags::constant_time_size_pos);
1162 static const bool cache_begin = 0 != (BoolFlags & hash_bool_flags::cache_begin_pos);
1163 static const bool compare_hash = 0 != (BoolFlags & hash_bool_flags::compare_hash_pos);
1164 static const bool incremental = 0 != (BoolFlags & hash_bool_flags::incremental_pos);
1165 static const bool power_2_buckets = incremental || (0 != (BoolFlags & hash_bool_flags::power_2_buckets_pos));
1166
1167 static const bool optimize_multikey
1168 = detail::optimize_multikey_is_true<node_traits>::value && !unique_keys;
1169
1170 /// @cond
1171 private:
1172
1173 //Configuration error: compare_hash<> can't be specified without store_hash<>
1174 //See documentation for more explanations
1175 BOOST_STATIC_ASSERT((!compare_hash || store_hash));
1176
1177 typedef typename slist_impl::node_ptr slist_node_ptr;
1178 typedef typename pointer_traits
1179 <slist_node_ptr>::template rebind_pointer
1180 < void >::type void_pointer;
1181 //We'll define group traits, but these won't be instantiated if
1182 //optimize_multikey is not true
1183 typedef unordered_group_adapter<node_traits> group_traits;
1184 typedef circular_slist_algorithms<group_traits> group_algorithms;
1185 typedef detail::bool_<store_hash> store_hash_t;
1186 typedef detail::bool_<optimize_multikey> optimize_multikey_t;
1187 typedef detail::bool_<cache_begin> cache_begin_t;
1188 typedef detail::bool_<power_2_buckets> power_2_buckets_t;
1189 typedef detail::size_holder<constant_time_size, size_type> size_traits;
1190 typedef detail::size_holder<incremental, size_type, int> split_traits;
1191 typedef detail::group_functions<node_traits> group_functions_t;
1192 typedef detail::node_functions<node_traits> node_functions_t;
1193
1194 private:
1195 //noncopyable, movable
1196 BOOST_MOVABLE_BUT_NOT_COPYABLE(hashtable_impl)
1197
1198 static const bool safemode_or_autounlink = is_safe_autounlink<real_value_traits::link_mode>::value;
1199
1200 //Constant-time size is incompatible with auto-unlink hooks!
1201 BOOST_STATIC_ASSERT(!(constant_time_size && ((int)real_value_traits::link_mode == (int)auto_unlink)));
1202 //Cache begin is incompatible with auto-unlink hooks!
1203 BOOST_STATIC_ASSERT(!(cache_begin && ((int)real_value_traits::link_mode == (int)auto_unlink)));
1204
1205 template<class Disposer>
1206 node_cast_adaptor< detail::node_disposer<Disposer, real_value_traits, CircularSListAlgorithms>
1207 , slist_node_ptr, node_ptr >
1208 make_node_disposer(const Disposer &disposer) const
1209 {
1210 return node_cast_adaptor
1211 < detail::node_disposer<Disposer, real_value_traits, CircularSListAlgorithms>
1212 , slist_node_ptr, node_ptr >
1213 (disposer, &this->priv_real_value_traits());
1214 }
1215
1216 /// @endcond
1217
1218 public:
1219 typedef detail::insert_commit_data_impl insert_commit_data;
1220
1221 typedef detail::transform_iterator
1222 < typename slist_impl::iterator
1223 , downcast_node_to_value_t
1224 < real_value_traits
1225 , false> > local_iterator;
1226
1227 typedef detail::transform_iterator
1228 < typename slist_impl::iterator
1229 , downcast_node_to_value_t
1230 < real_value_traits
1231 , true> > const_local_iterator;
1232
1233 public:
1234
1235 //! <b>Requires</b>: buckets must not be being used by any other resource.
1236 //!
1237 //! <b>Effects</b>: Constructs an empty unordered_set, storing a reference
1238 //! to the bucket array and copies of the key_hasher and equal_func functors.
1239 //!
1240 //! <b>Complexity</b>: Constant.
1241 //!
1242 //! <b>Throws</b>: If value_traits::node_traits::node
1243 //! constructor throws (this does not happen with predefined Boost.Intrusive hooks)
1244 //! or the copy constructor or invocation of hash_func or equal_func throws.
1245 //!
1246 //! <b>Notes</b>: buckets array must be disposed only after
1247 //! *this is disposed.
1248 explicit hashtable_impl ( const bucket_traits &b_traits
1249 , const hasher & hash_func = hasher()
1250 , const key_equal &equal_func = key_equal()
1251 , const value_traits &v_traits = value_traits())
1252 : data_type(b_traits, hash_func, equal_func, v_traits)
1253 {
1254 this->priv_initialize_buckets();
1255 this->priv_size_traits().set_size(size_type(0));
1256 size_type bucket_sz = this->priv_bucket_count();
1257 BOOST_INTRUSIVE_INVARIANT_ASSERT(bucket_sz != 0);
1258 //Check power of two bucket array if the option is activated
1259 BOOST_INTRUSIVE_INVARIANT_ASSERT
1260 (!power_2_buckets || (0 == (bucket_sz & (bucket_sz-1))));
1261 this->priv_split_traits().set_size(bucket_sz>>1);
1262 }
1263
1264 //! <b>Effects</b>: to-do
1265 //!
1266 hashtable_impl(BOOST_RV_REF(hashtable_impl) x)
1267 : data_type( ::boost::move(x.priv_bucket_traits())
1268 , ::boost::move(x.priv_hasher())
1269 , ::boost::move(x.priv_equal())
1270 , ::boost::move(x.priv_value_traits())
1271 )
1272 {
1273 this->priv_swap_cache(x);
1274 x.priv_initialize_cache();
1275 if(constant_time_size){
1276 this->priv_size_traits().set_size(size_type(0));
1277 this->priv_size_traits().set_size(x.priv_size_traits().get_size());
1278 x.priv_size_traits().set_size(size_type(0));
1279 }
1280 if(incremental){
1281 this->priv_split_traits().set_size(x.priv_split_traits().get_size());
1282 x.priv_split_traits().set_size(size_type(0));
1283 }
1284 }
1285
1286 //! <b>Effects</b>: to-do
1287 //!
1288 hashtable_impl& operator=(BOOST_RV_REF(hashtable_impl) x)
1289 { this->swap(x); return *this; }
1290
1291 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
1292 //! <b>Effects</b>: Detaches all elements from this. The objects in the unordered_set
1293 //! are not deleted (i.e. no destructors are called).
1294 //!
1295 //! <b>Complexity</b>: Linear to the number of elements in the unordered_set, if
1296 //! it's a safe-mode or auto-unlink value. Otherwise constant.
1297 //!
1298 //! <b>Throws</b>: Nothing.
1299 ~hashtable_impl()
1300 {}
1301 #endif
1302
1303 //! <b>Effects</b>: Returns an iterator pointing to the beginning of the unordered_set.
1304 //!
1305 //! <b>Complexity</b>: Amortized constant time.
1306 //! Worst case (empty unordered_set): O(this->bucket_count())
1307 //!
1308 //! <b>Throws</b>: Nothing.
1309 iterator begin()
1310 { return iterator(this->priv_begin(), &this->get_bucket_value_traits()); }
1311
1312 //! <b>Effects</b>: Returns a const_iterator pointing to the beginning
1313 //! of the unordered_set.
1314 //!
1315 //! <b>Complexity</b>: Amortized constant time.
1316 //! Worst case (empty unordered_set): O(this->bucket_count())
1317 //!
1318 //! <b>Throws</b>: Nothing.
1319 const_iterator begin() const
1320 { return this->cbegin(); }
1321
1322 //! <b>Effects</b>: Returns a const_iterator pointing to the beginning
1323 //! of the unordered_set.
1324 //!
1325 //! <b>Complexity</b>: Amortized constant time.
1326 //! Worst case (empty unordered_set): O(this->bucket_count())
1327 //!
1328 //! <b>Throws</b>: Nothing.
1329 const_iterator cbegin() const
1330 { return const_iterator(this->priv_begin(), &this->get_bucket_value_traits()); }
1331
1332 //! <b>Effects</b>: Returns an iterator pointing to the end of the unordered_set.
1333 //!
1334 //! <b>Complexity</b>: Constant.
1335 //!
1336 //! <b>Throws</b>: Nothing.
1337 iterator end()
1338 { return iterator(this->priv_invalid_local_it(), 0); }
1339
1340 //! <b>Effects</b>: Returns a const_iterator pointing to the end of the unordered_set.
1341 //!
1342 //! <b>Complexity</b>: Constant.
1343 //!
1344 //! <b>Throws</b>: Nothing.
1345 const_iterator end() const
1346 { return this->cend(); }
1347
1348 //! <b>Effects</b>: Returns a const_iterator pointing to the end of the unordered_set.
1349 //!
1350 //! <b>Complexity</b>: Constant.
1351 //!
1352 //! <b>Throws</b>: Nothing.
1353 const_iterator cend() const
1354 { return const_iterator(this->priv_invalid_local_it(), 0); }
1355
1356 //! <b>Effects</b>: Returns the hasher object used by the unordered_set.
1357 //!
1358 //! <b>Complexity</b>: Constant.
1359 //!
1360 //! <b>Throws</b>: If hasher copy-constructor throws.
1361 hasher hash_function() const
1362 { return this->priv_hasher(); }
1363
1364 //! <b>Effects</b>: Returns the key_equal object used by the unordered_set.
1365 //!
1366 //! <b>Complexity</b>: Constant.
1367 //!
1368 //! <b>Throws</b>: If key_equal copy-constructor throws.
1369 key_equal key_eq() const
1370 { return this->priv_equal(); }
1371
1372 //! <b>Effects</b>: Returns true if the container is empty.
1373 //!
1374 //! <b>Complexity</b>: if constant-time size and cache_begin options are disabled,
1375 //! average constant time (worst case, with empty() == true: O(this->bucket_count()).
1376 //! Otherwise constant.
1377 //!
1378 //! <b>Throws</b>: Nothing.
1379 bool empty() const
1380 {
1381 if(constant_time_size){
1382 return !this->size();
1383 }
1384 else if(cache_begin){
1385 return this->begin() == this->end();
1386 }
1387 else{
1388 size_type bucket_cnt = this->priv_bucket_count();
1389 const bucket_type *b = boost::intrusive::detail::to_raw_pointer(this->priv_bucket_pointer());
1390 for (size_type n = 0; n < bucket_cnt; ++n, ++b){
1391 if(!b->empty()){
1392 return false;
1393 }
1394 }
1395 return true;
1396 }
1397 }
1398
1399 //! <b>Effects</b>: Returns the number of elements stored in the unordered_set.
1400 //!
1401 //! <b>Complexity</b>: Linear to elements contained in *this if
1402 //! constant_time_size is false. Constant-time otherwise.
1403 //!
1404 //! <b>Throws</b>: Nothing.
1405 size_type size() const
1406 {
1407 if(constant_time_size)
1408 return this->priv_size_traits().get_size();
1409 else{
1410 size_type len = 0;
1411 size_type bucket_cnt = this->priv_bucket_count();
1412 const bucket_type *b = boost::intrusive::detail::to_raw_pointer(this->priv_bucket_pointer());
1413 for (size_type n = 0; n < bucket_cnt; ++n, ++b){
1414 len += b->size();
1415 }
1416 return len;
1417 }
1418 }
1419
1420 //! <b>Requires</b>: the hasher and the equality function unqualified swap
1421 //! call should not throw.
1422 //!
1423 //! <b>Effects</b>: Swaps the contents of two unordered_sets.
1424 //! Swaps also the contained bucket array and equality and hasher functors.
1425 //!
1426 //! <b>Complexity</b>: Constant.
1427 //!
1428 //! <b>Throws</b>: If the swap() call for the comparison or hash functors
1429 //! found using ADL throw. Basic guarantee.
1430 void swap(hashtable_impl& other)
1431 {
1432 using std::swap;
1433 //These can throw
1434 swap(this->priv_equal(), other.priv_equal());
1435 swap(this->priv_hasher(), other.priv_hasher());
1436 //These can't throw
1437 swap(this->priv_bucket_traits(), other.priv_bucket_traits());
1438 swap(this->priv_value_traits(), other.priv_value_traits());
1439 this->priv_swap_cache(other);
1440 if(constant_time_size){
1441 size_type backup = this->priv_size_traits().get_size();
1442 this->priv_size_traits().set_size(other.priv_size_traits().get_size());
1443 other.priv_size_traits().set_size(backup);
1444 }
1445 if(incremental){
1446 size_type backup = this->priv_split_traits().get_size();
1447 this->priv_split_traits().set_size(other.priv_split_traits().get_size());
1448 other.priv_split_traits().set_size(backup);
1449 }
1450 }
1451
1452 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw
1453 //! Cloner should yield to nodes that compare equal and produce the same
1454 //! hash than the original node.
1455 //!
1456 //! <b>Effects</b>: Erases all the elements from *this
1457 //! calling Disposer::operator()(pointer), clones all the
1458 //! elements from src calling Cloner::operator()(const_reference )
1459 //! and inserts them on *this. The hash function and the equality
1460 //! predicate are copied from the source.
1461 //!
1462 //! If store_hash option is true, this method does not use the hash function.
1463 //!
1464 //! If any operation throws, all cloned elements are unlinked and disposed
1465 //! calling Disposer::operator()(pointer).
1466 //!
1467 //! <b>Complexity</b>: Linear to erased plus inserted elements.
1468 //!
1469 //! <b>Throws</b>: If cloner or hasher throw or hash or equality predicate copying
1470 //! throws. Basic guarantee.
1471 template <class Cloner, class Disposer>
1472 void clone_from(const hashtable_impl &src, Cloner cloner, Disposer disposer)
1473 {
1474 this->clear_and_dispose(disposer);
1475 if(!constant_time_size || !src.empty()){
1476 const size_type src_bucket_count = src.bucket_count();
1477 const size_type dst_bucket_count = this->bucket_count();
1478 //Check power of two bucket array if the option is activated
1479 BOOST_INTRUSIVE_INVARIANT_ASSERT
1480 (!power_2_buckets || (0 == (src_bucket_count & (src_bucket_count-1))));
1481 BOOST_INTRUSIVE_INVARIANT_ASSERT
1482 (!power_2_buckets || (0 == (dst_bucket_count & (dst_bucket_count-1))));
1483
1484 //If src bucket count is bigger or equal, structural copy is possible
1485 if(!incremental && (src_bucket_count >= dst_bucket_count)){
1486 //First clone the first ones
1487 const bucket_ptr src_buckets = src.priv_bucket_pointer();
1488 const bucket_ptr dst_buckets = this->priv_bucket_pointer();
1489 size_type constructed;
1490
1491 typedef node_cast_adaptor< detail::node_disposer<Disposer, real_value_traits, CircularSListAlgorithms>
1492 , slist_node_ptr, node_ptr > NodeDisposer;
1493 typedef node_cast_adaptor< detail::node_cloner <Cloner, real_value_traits, CircularSListAlgorithms>
1494 , slist_node_ptr, node_ptr > NodeCloner;
1495 NodeDisposer node_disp(disposer, &this->priv_real_value_traits());
1496
1497 detail::exception_array_disposer<bucket_type, NodeDisposer, size_type>
1498 rollback(dst_buckets[0], node_disp, constructed);
1499 for( constructed = 0
1500 ; constructed < dst_bucket_count
1501 ; ++constructed){
1502 dst_buckets[constructed].clone_from
1503 ( src_buckets[constructed]
1504 , NodeCloner(cloner, &this->priv_real_value_traits()), node_disp);
1505 }
1506 if(src_bucket_count != dst_bucket_count){
1507 //Now insert the remaining ones using the modulo trick
1508 for(//"constructed" comes from the previous loop
1509 ; constructed < src_bucket_count
1510 ; ++constructed){
1511 bucket_type &dst_b =
1512 dst_buckets[detail::hash_to_bucket_split<power_2_buckets, incremental>(constructed, dst_bucket_count, dst_bucket_count)];
1513 bucket_type &src_b = src_buckets[constructed];
1514 for( siterator b(src_b.begin()), e(src_b.end())
1515 ; b != e
1516 ; ++b){
1517 dst_b.push_front(*(NodeCloner(cloner, &this->priv_real_value_traits())(*b.pointed_node())));
1518 }
1519 }
1520 }
1521 this->priv_hasher() = src.priv_hasher();
1522 this->priv_equal() = src.priv_equal();
1523 rollback.release();
1524 this->priv_size_traits().set_size(src.priv_size_traits().get_size());
1525 this->priv_split_traits().set_size(dst_bucket_count);
1526 this->priv_insertion_update_cache(0u);
1527 this->priv_erasure_update_cache();
1528 }
1529 else if(store_hash){
1530 //Unlike previous cloning algorithm, this can throw
1531 //if cloner, hasher or comparison functor throw
1532 const_iterator b(src.begin()), e(src.end());
1533 detail::exception_disposer<hashtable_impl, Disposer>
1534 rollback(*this, disposer);
1535 for(; b != e; ++b){
1536 std::size_t hash_value = this->priv_stored_or_compute_hash(*b, store_hash_t());;
1537 this->priv_insert_equal_with_hash(*cloner(*b), hash_value);
1538 }
1539 rollback.release();
1540 }
1541 else{
1542 //Unlike previous cloning algorithm, this can throw
1543 //if cloner, hasher or comparison functor throw
1544 const_iterator b(src.begin()), e(src.end());
1545 detail::exception_disposer<hashtable_impl, Disposer>
1546 rollback(*this, disposer);
1547 for(; b != e; ++b){
1548 this->insert_equal(*cloner(*b));
1549 }
1550 rollback.release();
1551 }
1552 }
1553 }
1554
1555 //! <b>Requires</b>: value must be an lvalue
1556 //!
1557 //! <b>Effects</b>: Inserts the value into the unordered_set.
1558 //!
1559 //! <b>Returns</b>: An iterator to the inserted value.
1560 //!
1561 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
1562 //!
1563 //! <b>Throws</b>: If the internal hasher or the equality functor throws. Strong guarantee.
1564 //!
1565 //! <b>Note</b>: Does not affect the validity of iterators and references.
1566 //! No copy-constructors are called.
1567 iterator insert_equal(reference value)
1568 {
1569 size_type bucket_num;
1570 std::size_t hash_value;
1571 siterator prev;
1572 siterator it = this->priv_find
1573 (value, this->priv_hasher(), this->priv_equal(), bucket_num, hash_value, prev);
1574 return this->priv_insert_equal_find(value, bucket_num, hash_value, it);
1575 }
1576
1577 //! <b>Requires</b>: Dereferencing iterator must yield an lvalue
1578 //! of type value_type.
1579 //!
1580 //! <b>Effects</b>: Equivalent to this->insert_equal(t) for each element in [b, e).
1581 //!
1582 //! <b>Complexity</b>: Average case O(N), where N is std::distance(b, e).
1583 //! Worst case O(N*this->size()).
1584 //!
1585 //! <b>Throws</b>: If the internal hasher or the equality functor throws. Basic guarantee.
1586 //!
1587 //! <b>Note</b>: Does not affect the validity of iterators and references.
1588 //! No copy-constructors are called.
1589 template<class Iterator>
1590 void insert_equal(Iterator b, Iterator e)
1591 {
1592 for (; b != e; ++b)
1593 this->insert_equal(*b);
1594 }
1595
1596 //! <b>Requires</b>: value must be an lvalue
1597 //!
1598 //! <b>Effects</b>: Tries to inserts value into the unordered_set.
1599 //!
1600 //! <b>Returns</b>: If the value
1601 //! is not already present inserts it and returns a pair containing the
1602 //! iterator to the new value and true. If there is an equivalent value
1603 //! returns a pair containing an iterator to the already present value
1604 //! and false.
1605 //!
1606 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
1607 //!
1608 //! <b>Throws</b>: If the internal hasher or the equality functor throws. Strong guarantee.
1609 //!
1610 //! <b>Note</b>: Does not affect the validity of iterators and references.
1611 //! No copy-constructors are called.
1612 std::pair<iterator, bool> insert_unique(reference value)
1613 {
1614 insert_commit_data commit_data;
1615 std::pair<iterator, bool> ret = this->insert_unique_check
1616 (value, this->priv_hasher(), this->priv_equal(), commit_data);
1617 if(!ret.second)
1618 return ret;
1619 return std::pair<iterator, bool>
1620 (this->insert_unique_commit(value, commit_data), true);
1621 }
1622
1623 //! <b>Requires</b>: Dereferencing iterator must yield an lvalue
1624 //! of type value_type.
1625 //!
1626 //! <b>Effects</b>: Equivalent to this->insert_unique(t) for each element in [b, e).
1627 //!
1628 //! <b>Complexity</b>: Average case O(N), where N is std::distance(b, e).
1629 //! Worst case O(N*this->size()).
1630 //!
1631 //! <b>Throws</b>: If the internal hasher or the equality functor throws. Basic guarantee.
1632 //!
1633 //! <b>Note</b>: Does not affect the validity of iterators and references.
1634 //! No copy-constructors are called.
1635 template<class Iterator>
1636 void insert_unique(Iterator b, Iterator e)
1637 {
1638 for (; b != e; ++b)
1639 this->insert_unique(*b);
1640 }
1641
1642 //! <b>Requires</b>: "hash_func" must be a hash function that induces
1643 //! the same hash values as the stored hasher. The difference is that
1644 //! "hash_func" hashes the given key instead of the value_type.
1645 //!
1646 //! "equal_func" must be a equality function that induces
1647 //! the same equality as key_equal. The difference is that
1648 //! "equal_func" compares an arbitrary key with the contained values.
1649 //!
1650 //! <b>Effects</b>: Checks if a value can be inserted in the unordered_set, using
1651 //! a user provided key instead of the value itself.
1652 //!
1653 //! <b>Returns</b>: If there is an equivalent value
1654 //! returns a pair containing an iterator to the already present value
1655 //! and false. If the value can be inserted returns true in the returned
1656 //! pair boolean and fills "commit_data" that is meant to be used with
1657 //! the "insert_commit" function.
1658 //!
1659 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
1660 //!
1661 //! <b>Throws</b>: If hash_func or equal_func throw. Strong guarantee.
1662 //!
1663 //! <b>Notes</b>: This function is used to improve performance when constructing
1664 //! a value_type is expensive: if there is an equivalent value
1665 //! the constructed object must be discarded. Many times, the part of the
1666 //! node that is used to impose the hash or the equality is much cheaper to
1667 //! construct than the value_type and this function offers the possibility to
1668 //! use that the part to check if the insertion will be successful.
1669 //!
1670 //! If the check is successful, the user can construct the value_type and use
1671 //! "insert_commit" to insert the object in constant-time.
1672 //!
1673 //! "commit_data" remains valid for a subsequent "insert_commit" only if no more
1674 //! objects are inserted or erased from the unordered_set.
1675 //!
1676 //! After a successful rehashing insert_commit_data remains valid.
1677 template<class KeyType, class KeyHasher, class KeyValueEqual>
1678 std::pair<iterator, bool> insert_unique_check
1679 ( const KeyType &key
1680 , KeyHasher hash_func
1681 , KeyValueEqual equal_func
1682 , insert_commit_data &commit_data)
1683 {
1684 size_type bucket_num;
1685 siterator prev;
1686 siterator prev_pos =
1687 this->priv_find(key, hash_func, equal_func, bucket_num, commit_data.hash, prev);
1688 bool success = prev_pos == this->priv_invalid_local_it();
1689 if(success){
1690 prev_pos = prev;
1691 }
1692 return std::pair<iterator, bool>(iterator(prev_pos, &this->get_bucket_value_traits()),success);
1693 }
1694
1695 //! <b>Requires</b>: value must be an lvalue of type value_type. commit_data
1696 //! must have been obtained from a previous call to "insert_check".
1697 //! No objects should have been inserted or erased from the unordered_set between
1698 //! the "insert_check" that filled "commit_data" and the call to "insert_commit".
1699 //!
1700 //! <b>Effects</b>: Inserts the value in the unordered_set using the information obtained
1701 //! from the "commit_data" that a previous "insert_check" filled.
1702 //!
1703 //! <b>Returns</b>: An iterator to the newly inserted object.
1704 //!
1705 //! <b>Complexity</b>: Constant time.
1706 //!
1707 //! <b>Throws</b>: Nothing.
1708 //!
1709 //! <b>Notes</b>: This function has only sense if a "insert_check" has been
1710 //! previously executed to fill "commit_data". No value should be inserted or
1711 //! erased between the "insert_check" and "insert_commit" calls.
1712 //!
1713 //! After a successful rehashing insert_commit_data remains valid.
1714 iterator insert_unique_commit(reference value, const insert_commit_data &commit_data)
1715 {
1716 size_type bucket_num = this->priv_hash_to_bucket(commit_data.hash);
1717 bucket_type &b = this->priv_bucket_pointer()[bucket_num];
1718 this->priv_size_traits().increment();
1719 node_ptr n = pointer_traits<node_ptr>::pointer_to(this->priv_value_to_node(value));
1720 node_functions_t::store_hash(n, commit_data.hash, store_hash_t());
1721 if(safemode_or_autounlink)
1722 BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(n));
1723 this->priv_insertion_update_cache(bucket_num);
1724 group_functions_t::insert_in_group(node_ptr(), n, optimize_multikey_t());
1725 return iterator(b.insert_after(b.before_begin(), *n), &this->get_bucket_value_traits());
1726 }
1727
1728 //! <b>Effects</b>: Erases the element pointed to by i.
1729 //!
1730 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
1731 //!
1732 //! <b>Throws</b>: Nothing.
1733 //!
1734 //! <b>Note</b>: Invalidates the iterators (but not the references)
1735 //! to the erased element. No destructors are called.
1736 void erase(const_iterator i)
1737 { this->erase_and_dispose(i, detail::null_disposer()); }
1738
1739 //! <b>Effects</b>: Erases the range pointed to by b end e.
1740 //!
1741 //! <b>Complexity</b>: Average case O(std::distance(b, e)),
1742 //! worst case O(this->size()).
1743 //!
1744 //! <b>Throws</b>: Nothing.
1745 //!
1746 //! <b>Note</b>: Invalidates the iterators (but not the references)
1747 //! to the erased elements. No destructors are called.
1748 void erase(const_iterator b, const_iterator e)
1749 { this->erase_and_dispose(b, e, detail::null_disposer()); }
1750
1751 //! <b>Effects</b>: Erases all the elements with the given value.
1752 //!
1753 //! <b>Returns</b>: The number of erased elements.
1754 //!
1755 //! <b>Complexity</b>: Average case O(this->count(value)).
1756 //! Worst case O(this->size()).
1757 //!
1758 //! <b>Throws</b>: If the internal hasher or the equality functor throws.
1759 //! Basic guarantee.
1760 //!
1761 //! <b>Note</b>: Invalidates the iterators (but not the references)
1762 //! to the erased elements. No destructors are called.
1763 size_type erase(const_reference value)
1764 { return this->erase(value, this->priv_hasher(), this->priv_equal()); }
1765
1766 //! <b>Requires</b>: "hash_func" must be a hash function that induces
1767 //! the same hash values as the stored hasher. The difference is that
1768 //! "hash_func" hashes the given key instead of the value_type.
1769 //!
1770 //! "equal_func" must be a equality function that induces
1771 //! the same equality as key_equal. The difference is that
1772 //! "equal_func" compares an arbitrary key with the contained values.
1773 //!
1774 //! <b>Effects</b>: Erases all the elements that have the same hash and
1775 //! compare equal with the given key.
1776 //!
1777 //! <b>Returns</b>: The number of erased elements.
1778 //!
1779 //! <b>Complexity</b>: Average case O(this->count(value)).
1780 //! Worst case O(this->size()).
1781 //!
1782 //! <b>Throws</b>: If hash_func or equal_func throw. Basic guarantee.
1783 //!
1784 //! <b>Note</b>: Invalidates the iterators (but not the references)
1785 //! to the erased elements. No destructors are called.
1786 template<class KeyType, class KeyHasher, class KeyValueEqual>
1787 size_type erase(const KeyType& key, KeyHasher hash_func, KeyValueEqual equal_func)
1788 { return this->erase_and_dispose(key, hash_func, equal_func, detail::null_disposer()); }
1789
1790 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
1791 //!
1792 //! <b>Effects</b>: Erases the element pointed to by i.
1793 //! Disposer::operator()(pointer) is called for the removed element.
1794 //!
1795 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
1796 //!
1797 //! <b>Throws</b>: Nothing.
1798 //!
1799 //! <b>Note</b>: Invalidates the iterators
1800 //! to the erased elements.
1801 template<class Disposer>
1802 void erase_and_dispose(const_iterator i, Disposer disposer
1803 /// @cond
1804 , typename detail::enable_if_c<!detail::is_convertible<Disposer, const_iterator>::value >::type * = 0
1805 /// @endcond
1806 )
1807 {
1808 this->priv_erase(i, disposer, optimize_multikey_t());
1809 this->priv_size_traits().decrement();
1810 this->priv_erasure_update_cache();
1811 }
1812
1813 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
1814 //!
1815 //! <b>Effects</b>: Erases the range pointed to by b end e.
1816 //! Disposer::operator()(pointer) is called for the removed elements.
1817 //!
1818 //! <b>Complexity</b>: Average case O(std::distance(b, e)),
1819 //! worst case O(this->size()).
1820 //!
1821 //! <b>Throws</b>: Nothing.
1822 //!
1823 //! <b>Note</b>: Invalidates the iterators
1824 //! to the erased elements.
1825 template<class Disposer>
1826 void erase_and_dispose(const_iterator b, const_iterator e, Disposer disposer)
1827 {
1828 if(b != e){
1829 //Get the bucket number and local iterator for both iterators
1830 siterator first_local_it(b.slist_it());
1831 size_type first_bucket_num = this->priv_get_bucket_num(first_local_it);
1832
1833 const bucket_ptr buck_ptr = this->priv_bucket_pointer();
1834 siterator before_first_local_it
1835 = this->priv_get_previous(buck_ptr[first_bucket_num], first_local_it);
1836 size_type last_bucket_num;
1837 siterator last_local_it;
1838
1839 //For the end iterator, we will assign the end iterator
1840 //of the last bucket
1841 if(e == this->end()){
1842 last_bucket_num = this->bucket_count() - 1;
1843 last_local_it = buck_ptr[last_bucket_num].end();
1844 }
1845 else{
1846 last_local_it = e.slist_it();
1847 last_bucket_num = this->priv_get_bucket_num(last_local_it);
1848 }
1849 this->priv_erase_range(before_first_local_it, first_bucket_num, last_local_it, last_bucket_num, disposer);
1850 this->priv_erasure_update_cache_range(first_bucket_num, last_bucket_num);
1851 }
1852 }
1853
1854 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
1855 //!
1856 //! <b>Effects</b>: Erases all the elements with the given value.
1857 //! Disposer::operator()(pointer) is called for the removed elements.
1858 //!
1859 //! <b>Returns</b>: The number of erased elements.
1860 //!
1861 //! <b>Complexity</b>: Average case O(this->count(value)).
1862 //! Worst case O(this->size()).
1863 //!
1864 //! <b>Throws</b>: If the internal hasher or the equality functor throws.
1865 //! Basic guarantee.
1866 //!
1867 //! <b>Note</b>: Invalidates the iterators (but not the references)
1868 //! to the erased elements. No destructors are called.
1869 template<class Disposer>
1870 size_type erase_and_dispose(const_reference value, Disposer disposer)
1871 { return this->erase_and_dispose(value, this->priv_hasher(), this->priv_equal(), disposer); }
1872
1873 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
1874 //!
1875 //! <b>Effects</b>: Erases all the elements with the given key.
1876 //! according to the comparison functor "equal_func".
1877 //! Disposer::operator()(pointer) is called for the removed elements.
1878 //!
1879 //! <b>Returns</b>: The number of erased elements.
1880 //!
1881 //! <b>Complexity</b>: Average case O(this->count(value)).
1882 //! Worst case O(this->size()).
1883 //!
1884 //! <b>Throws</b>: If hash_func or equal_func throw. Basic guarantee.
1885 //!
1886 //! <b>Note</b>: Invalidates the iterators
1887 //! to the erased elements.
1888 template<class KeyType, class KeyHasher, class KeyValueEqual, class Disposer>
1889 size_type erase_and_dispose(const KeyType& key, KeyHasher hash_func
1890 ,KeyValueEqual equal_func, Disposer disposer)
1891 {
1892 size_type bucket_num;
1893 std::size_t h;
1894 siterator prev;
1895 siterator it = this->priv_find(key, hash_func, equal_func, bucket_num, h, prev);
1896 bool success = it != this->priv_invalid_local_it();
1897 size_type cnt(0);
1898 if(!success){
1899 return 0;
1900 }
1901 else if(optimize_multikey){
1902 siterator last = bucket_type::s_iterator_to
1903 (*node_traits::get_next(group_functions_t::get_last_in_group
1904 (detail::dcast_bucket_ptr<node>(it.pointed_node()), optimize_multikey_t())));
1905 this->priv_erase_range_impl(bucket_num, prev, last, disposer, cnt);
1906 }
1907 else{
1908 //If found erase all equal values
1909 bucket_type &b = this->priv_bucket_pointer()[bucket_num];
1910 for(siterator end_sit = b.end(); it != end_sit; ++cnt, ++it){
1911 slist_node_ptr n(it.pointed_node());
1912 const value_type &v = this->priv_value_from_slist_node(n);
1913 if(compare_hash){
1914 std::size_t vh = this->priv_stored_or_compute_hash(v, store_hash_t());
1915 if(h != vh || !equal_func(key, v)){
1916 break;
1917 }
1918 }
1919 else if(!equal_func(key, v)){
1920 break;
1921 }
1922 this->priv_size_traits().decrement();
1923 }
1924 b.erase_after_and_dispose(prev, it, make_node_disposer(disposer));
1925 }
1926 this->priv_erasure_update_cache();
1927 return cnt;
1928 }
1929
1930 //! <b>Effects</b>: Erases all of the elements.
1931 //!
1932 //! <b>Complexity</b>: Linear to the number of elements on the container.
1933 //! if it's a safe-mode or auto-unlink value_type. Constant time otherwise.
1934 //!
1935 //! <b>Throws</b>: Nothing.
1936 //!
1937 //! <b>Note</b>: Invalidates the iterators (but not the references)
1938 //! to the erased elements. No destructors are called.
1939 void clear()
1940 {
1941 this->priv_clear_buckets();
1942 this->priv_size_traits().set_size(size_type(0));
1943 }
1944
1945 //! <b>Requires</b>: Disposer::operator()(pointer) shouldn't throw.
1946 //!
1947 //! <b>Effects</b>: Erases all of the elements.
1948 //!
1949 //! <b>Complexity</b>: Linear to the number of elements on the container.
1950 //! Disposer::operator()(pointer) is called for the removed elements.
1951 //!
1952 //! <b>Throws</b>: Nothing.
1953 //!
1954 //! <b>Note</b>: Invalidates the iterators (but not the references)
1955 //! to the erased elements. No destructors are called.
1956 template<class Disposer>
1957 void clear_and_dispose(Disposer disposer)
1958 {
1959 if(!constant_time_size || !this->empty()){
1960 size_type num_buckets = this->bucket_count();
1961 bucket_ptr b = this->priv_bucket_pointer();
1962 for(; num_buckets--; ++b){
1963 b->clear_and_dispose(make_node_disposer(disposer));
1964 }
1965 this->priv_size_traits().set_size(size_type(0));
1966 }
1967 this->priv_initialize_cache();
1968 }
1969
1970 //! <b>Effects</b>: Returns the number of contained elements with the given value
1971 //!
1972 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
1973 //!
1974 //! <b>Throws</b>: If the internal hasher or the equality functor throws.
1975 size_type count(const_reference value) const
1976 { return this->count(value, this->priv_hasher(), this->priv_equal()); }
1977
1978 //! <b>Requires</b>: "hash_func" must be a hash function that induces
1979 //! the same hash values as the stored hasher. The difference is that
1980 //! "hash_func" hashes the given key instead of the value_type.
1981 //!
1982 //! "equal_func" must be a equality function that induces
1983 //! the same equality as key_equal. The difference is that
1984 //! "equal_func" compares an arbitrary key with the contained values.
1985 //!
1986 //! <b>Effects</b>: Returns the number of contained elements with the given key
1987 //!
1988 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
1989 //!
1990 //! <b>Throws</b>: If hash_func or equal throw.
1991 template<class KeyType, class KeyHasher, class KeyValueEqual>
1992 size_type count(const KeyType &key, const KeyHasher &hash_func, const KeyValueEqual &equal_func) const
1993 {
1994 size_type bucket_n1, bucket_n2, cnt;
1995 this->priv_equal_range(key, hash_func, equal_func, bucket_n1, bucket_n2, cnt);
1996 return cnt;
1997 }
1998
1999 //! <b>Effects</b>: Finds an iterator to the first element is equal to
2000 //! "value" or end() if that element does not exist.
2001 //!
2002 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
2003 //!
2004 //! <b>Throws</b>: If the internal hasher or the equality functor throws.
2005 iterator find(const_reference value)
2006 { return this->find(value, this->priv_hasher(), this->priv_equal()); }
2007
2008 //! <b>Requires</b>: "hash_func" must be a hash function that induces
2009 //! the same hash values as the stored hasher. The difference is that
2010 //! "hash_func" hashes the given key instead of the value_type.
2011 //!
2012 //! "equal_func" must be a equality function that induces
2013 //! the same equality as key_equal. The difference is that
2014 //! "equal_func" compares an arbitrary key with the contained values.
2015 //!
2016 //! <b>Effects</b>: Finds an iterator to the first element whose key is
2017 //! "key" according to the given hash and equality functor or end() if
2018 //! that element does not exist.
2019 //!
2020 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
2021 //!
2022 //! <b>Throws</b>: If hash_func or equal_func throw.
2023 //!
2024 //! <b>Note</b>: This function is used when constructing a value_type
2025 //! is expensive and the value_type can be compared with a cheaper
2026 //! key type. Usually this key is part of the value_type.
2027 template<class KeyType, class KeyHasher, class KeyValueEqual>
2028 iterator find(const KeyType &key, KeyHasher hash_func, KeyValueEqual equal_func)
2029 {
2030 size_type bucket_n;
2031 std::size_t hash;
2032 siterator prev;
2033 siterator local_it = this->priv_find(key, hash_func, equal_func, bucket_n, hash, prev);
2034 return iterator(local_it, &this->get_bucket_value_traits());
2035 }
2036
2037 //! <b>Effects</b>: Finds a const_iterator to the first element whose key is
2038 //! "key" or end() if that element does not exist.
2039 //!
2040 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
2041 //!
2042 //! <b>Throws</b>: If the internal hasher or the equality functor throws.
2043 const_iterator find(const_reference value) const
2044 { return this->find(value, this->priv_hasher(), this->priv_equal()); }
2045
2046 //! <b>Requires</b>: "hash_func" must be a hash function that induces
2047 //! the same hash values as the stored hasher. The difference is that
2048 //! "hash_func" hashes the given key instead of the value_type.
2049 //!
2050 //! "equal_func" must be a equality function that induces
2051 //! the same equality as key_equal. The difference is that
2052 //! "equal_func" compares an arbitrary key with the contained values.
2053 //!
2054 //! <b>Effects</b>: Finds an iterator to the first element whose key is
2055 //! "key" according to the given hasher and equality functor or end() if
2056 //! that element does not exist.
2057 //!
2058 //! <b>Complexity</b>: Average case O(1), worst case O(this->size()).
2059 //!
2060 //! <b>Throws</b>: If hash_func or equal_func throw.
2061 //!
2062 //! <b>Note</b>: This function is used when constructing a value_type
2063 //! is expensive and the value_type can be compared with a cheaper
2064 //! key type. Usually this key is part of the value_type.
2065 template<class KeyType, class KeyHasher, class KeyValueEqual>
2066 const_iterator find
2067 (const KeyType &key, KeyHasher hash_func, KeyValueEqual equal_func) const
2068 {
2069 size_type bucket_n;
2070 std::size_t hash_value;
2071 siterator prev;
2072 siterator sit = this->priv_find(key, hash_func, equal_func, bucket_n, hash_value, prev);
2073 return const_iterator(sit, &this->get_bucket_value_traits());
2074 }
2075
2076 //! <b>Effects</b>: Returns a range containing all elements with values equivalent
2077 //! to value. Returns std::make_pair(this->end(), this->end()) if no such
2078 //! elements exist.
2079 //!
2080 //! <b>Complexity</b>: Average case O(this->count(value)). Worst case O(this->size()).
2081 //!
2082 //! <b>Throws</b>: If the internal hasher or the equality functor throws.
2083 std::pair<iterator,iterator> equal_range(const_reference value)
2084 { return this->equal_range(value, this->priv_hasher(), this->priv_equal()); }
2085
2086 //! <b>Requires</b>: "hash_func" must be a hash function that induces
2087 //! the same hash values as the stored hasher. The difference is that
2088 //! "hash_func" hashes the given key instead of the value_type.
2089 //!
2090 //! "equal_func" must be a equality function that induces
2091 //! the same equality as key_equal. The difference is that
2092 //! "equal_func" compares an arbitrary key with the contained values.
2093 //!
2094 //! <b>Effects</b>: Returns a range containing all elements with equivalent
2095 //! keys. Returns std::make_pair(this->end(), this->end()) if no such
2096 //! elements exist.
2097 //!
2098 //! <b>Complexity</b>: Average case O(this->count(key, hash_func, equal_func)).
2099 //! Worst case O(this->size()).
2100 //!
2101 //! <b>Throws</b>: If hash_func or the equal_func throw.
2102 //!
2103 //! <b>Note</b>: This function is used when constructing a value_type
2104 //! is expensive and the value_type can be compared with a cheaper
2105 //! key type. Usually this key is part of the value_type.
2106 template<class KeyType, class KeyHasher, class KeyValueEqual>
2107 std::pair<iterator,iterator> equal_range
2108 (const KeyType &key, KeyHasher hash_func, KeyValueEqual equal_func)
2109 {
2110 size_type bucket_n1, bucket_n2, cnt;
2111 std::pair<siterator, siterator> ret = this->priv_equal_range
2112 (key, hash_func, equal_func, bucket_n1, bucket_n2, cnt);
2113 return std::pair<iterator, iterator>
2114 (iterator(ret.first, &this->get_bucket_value_traits()), iterator(ret.second, &this->get_bucket_value_traits()));
2115 }
2116
2117 //! <b>Effects</b>: Returns a range containing all elements with values equivalent
2118 //! to value. Returns std::make_pair(this->end(), this->end()) if no such
2119 //! elements exist.
2120 //!
2121 //! <b>Complexity</b>: Average case O(this->count(value)). Worst case O(this->size()).
2122 //!
2123 //! <b>Throws</b>: If the internal hasher or the equality functor throws.
2124 std::pair<const_iterator, const_iterator>
2125 equal_range(const_reference value) const
2126 { return this->equal_range(value, this->priv_hasher(), this->priv_equal()); }
2127
2128 //! <b>Requires</b>: "hash_func" must be a hash function that induces
2129 //! the same hash values as the stored hasher. The difference is that
2130 //! "hash_func" hashes the given key instead of the value_type.
2131 //!
2132 //! "equal_func" must be a equality function that induces
2133 //! the same equality as key_equal. The difference is that
2134 //! "equal_func" compares an arbitrary key with the contained values.
2135 //!
2136 //! <b>Effects</b>: Returns a range containing all elements with equivalent
2137 //! keys. Returns std::make_pair(this->end(), this->end()) if no such
2138 //! elements exist.
2139 //!
2140 //! <b>Complexity</b>: Average case O(this->count(key, hash_func, equal_func)).
2141 //! Worst case O(this->size()).
2142 //!
2143 //! <b>Throws</b>: If the hasher or equal_func throw.
2144 //!
2145 //! <b>Note</b>: This function is used when constructing a value_type
2146 //! is expensive and the value_type can be compared with a cheaper
2147 //! key type. Usually this key is part of the value_type.
2148 template<class KeyType, class KeyHasher, class KeyValueEqual>
2149 std::pair<const_iterator,const_iterator> equal_range
2150 (const KeyType &key, KeyHasher hash_func, KeyValueEqual equal_func) const
2151 {
2152 size_type bucket_n1, bucket_n2, cnt;
2153 std::pair<siterator, siterator> ret =
2154 this->priv_equal_range(key, hash_func, equal_func, bucket_n1, bucket_n2, cnt);
2155 return std::pair<const_iterator, const_iterator>
2156 (const_iterator(ret.first, &this->get_bucket_value_traits()), const_iterator(ret.second, &this->get_bucket_value_traits()));
2157 }
2158
2159 //! <b>Requires</b>: value must be an lvalue and shall be in a unordered_set of
2160 //! appropriate type. Otherwise the behavior is undefined.
2161 //!
2162 //! <b>Effects</b>: Returns: a valid iterator belonging to the unordered_set
2163 //! that points to the value
2164 //!
2165 //! <b>Complexity</b>: Constant.
2166 //!
2167 //! <b>Throws</b>: If the internal hash function throws.
2168 iterator iterator_to(reference value)
2169 {
2170 return iterator(bucket_type::s_iterator_to(this->priv_value_to_node(value)), &this->get_bucket_value_traits());
2171 }
2172
2173 //! <b>Requires</b>: value must be an lvalue and shall be in a unordered_set of
2174 //! appropriate type. Otherwise the behavior is undefined.
2175 //!
2176 //! <b>Effects</b>: Returns: a valid const_iterator belonging to the
2177 //! unordered_set that points to the value
2178 //!
2179 //! <b>Complexity</b>: Constant.
2180 //!
2181 //! <b>Throws</b>: If the internal hash function throws.
2182 const_iterator iterator_to(const_reference value) const
2183 {
2184 siterator sit = bucket_type::s_iterator_to(const_cast<node &>(this->priv_value_to_node(value)));
2185 return const_iterator(sit, &this->get_bucket_value_traits());
2186 }
2187
2188 //! <b>Requires</b>: value must be an lvalue and shall be in a unordered_set of
2189 //! appropriate type. Otherwise the behavior is undefined.
2190 //!
2191 //! <b>Effects</b>: Returns: a valid local_iterator belonging to the unordered_set
2192 //! that points to the value
2193 //!
2194 //! <b>Complexity</b>: Constant.
2195 //!
2196 //! <b>Throws</b>: Nothing.
2197 //!
2198 //! <b>Note</b>: This static function is available only if the <i>value traits</i>
2199 //! is stateless.
2200 static local_iterator s_local_iterator_to(reference value)
2201 {
2202 BOOST_STATIC_ASSERT((!stateful_value_traits));
2203 siterator sit = bucket_type::s_iterator_to(((hashtable_impl*)0)->priv_value_to_node(value));
2204 return local_iterator(sit, const_real_value_traits_ptr());
2205 }
2206
2207 //! <b>Requires</b>: value must be an lvalue and shall be in a unordered_set of
2208 //! appropriate type. Otherwise the behavior is undefined.
2209 //!
2210 //! <b>Effects</b>: Returns: a valid const_local_iterator belonging to
2211 //! the unordered_set that points to the value
2212 //!
2213 //! <b>Complexity</b>: Constant.
2214 //!
2215 //! <b>Throws</b>: Nothing.
2216 //!
2217 //! <b>Note</b>: This static function is available only if the <i>value traits</i>
2218 //! is stateless.
2219 static const_local_iterator s_local_iterator_to(const_reference value)
2220 {
2221 BOOST_STATIC_ASSERT((!stateful_value_traits));
2222 siterator sit = bucket_type::s_iterator_to(((hashtable_impl*)0)->priv_value_to_node(const_cast<value_type&>(value)));
2223 return const_local_iterator(sit, const_real_value_traits_ptr());
2224 }
2225
2226 //! <b>Requires</b>: value must be an lvalue and shall be in a unordered_set of
2227 //! appropriate type. Otherwise the behavior is undefined.
2228 //!
2229 //! <b>Effects</b>: Returns: a valid local_iterator belonging to the unordered_set
2230 //! that points to the value
2231 //!
2232 //! <b>Complexity</b>: Constant.
2233 //!
2234 //! <b>Throws</b>: Nothing.
2235 local_iterator local_iterator_to(reference value)
2236 {
2237 siterator sit = bucket_type::s_iterator_to(this->priv_value_to_node(value));
2238 return local_iterator(sit, this->real_value_traits_ptr());
2239 }
2240
2241 //! <b>Requires</b>: value must be an lvalue and shall be in a unordered_set of
2242 //! appropriate type. Otherwise the behavior is undefined.
2243 //!
2244 //! <b>Effects</b>: Returns: a valid const_local_iterator belonging to
2245 //! the unordered_set that points to the value
2246 //!
2247 //! <b>Complexity</b>: Constant.
2248 //!
2249 //! <b>Throws</b>: Nothing.
2250 const_local_iterator local_iterator_to(const_reference value) const
2251 {
2252 siterator sit = bucket_type::s_iterator_to
2253 (const_cast<node &>(this->priv_value_to_node(value)));
2254 return const_local_iterator(sit, this->real_value_traits_ptr());
2255 }
2256
2257 //! <b>Effects</b>: Returns the number of buckets passed in the constructor
2258 //! or the last rehash function.
2259 //!
2260 //! <b>Complexity</b>: Constant.
2261 //!
2262 //! <b>Throws</b>: Nothing.
2263 size_type bucket_count() const
2264 { return this->priv_bucket_count(); }
2265
2266 //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
2267 //!
2268 //! <b>Effects</b>: Returns the number of elements in the nth bucket.
2269 //!
2270 //! <b>Complexity</b>: Constant.
2271 //!
2272 //! <b>Throws</b>: Nothing.
2273 size_type bucket_size(size_type n) const
2274 { return this->priv_bucket_pointer()[n].size(); }
2275
2276 //! <b>Effects</b>: Returns the index of the bucket in which elements
2277 //! with keys equivalent to k would be found, if any such element existed.
2278 //!
2279 //! <b>Complexity</b>: Constant.
2280 //!
2281 //! <b>Throws</b>: If the hash functor throws.
2282 //!
2283 //! <b>Note</b>: the return value is in the range [0, this->bucket_count()).
2284 size_type bucket(const key_type& k) const
2285 { return this->bucket(k, this->priv_hasher()); }
2286
2287 //! <b>Requires</b>: "hash_func" must be a hash function that induces
2288 //! the same hash values as the stored hasher. The difference is that
2289 //! "hash_func" hashes the given key instead of the value_type.
2290 //!
2291 //! <b>Effects</b>: Returns the index of the bucket in which elements
2292 //! with keys equivalent to k would be found, if any such element existed.
2293 //!
2294 //! <b>Complexity</b>: Constant.
2295 //!
2296 //! <b>Throws</b>: If hash_func throws.
2297 //!
2298 //! <b>Note</b>: the return value is in the range [0, this->bucket_count()).
2299 template<class KeyType, class KeyHasher>
2300 size_type bucket(const KeyType& k, const KeyHasher &hash_func) const
2301 { return this->priv_hash_to_bucket(hash_func(k)); }
2302
2303 //! <b>Effects</b>: Returns the bucket array pointer passed in the constructor
2304 //! or the last rehash function.
2305 //!
2306 //! <b>Complexity</b>: Constant.
2307 //!
2308 //! <b>Throws</b>: Nothing.
2309 bucket_ptr bucket_pointer() const
2310 { return this->priv_bucket_pointer(); }
2311
2312 //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
2313 //!
2314 //! <b>Effects</b>: Returns a local_iterator pointing to the beginning
2315 //! of the sequence stored in the bucket n.
2316 //!
2317 //! <b>Complexity</b>: Constant.
2318 //!
2319 //! <b>Throws</b>: Nothing.
2320 //!
2321 //! <b>Note</b>: [this->begin(n), this->end(n)) is a valid range
2322 //! containing all of the elements in the nth bucket.
2323 local_iterator begin(size_type n)
2324 { return local_iterator(this->priv_bucket_pointer()[n].begin(), this->real_value_traits_ptr()); }
2325
2326 //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
2327 //!
2328 //! <b>Effects</b>: Returns a const_local_iterator pointing to the beginning
2329 //! of the sequence stored in the bucket n.
2330 //!
2331 //! <b>Complexity</b>: Constant.
2332 //!
2333 //! <b>Throws</b>: Nothing.
2334 //!
2335 //! <b>Note</b>: [this->begin(n), this->end(n)) is a valid range
2336 //! containing all of the elements in the nth bucket.
2337 const_local_iterator begin(size_type n) const
2338 { return this->cbegin(n); }
2339
2340 //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
2341 //!
2342 //! <b>Effects</b>: Returns a const_local_iterator pointing to the beginning
2343 //! of the sequence stored in the bucket n.
2344 //!
2345 //! <b>Complexity</b>: Constant.
2346 //!
2347 //! <b>Throws</b>: Nothing.
2348 //!
2349 //! <b>Note</b>: [this->begin(n), this->end(n)) is a valid range
2350 //! containing all of the elements in the nth bucket.
2351 const_local_iterator cbegin(size_type n) const
2352 {
2353 siterator sit = const_cast<bucket_type&>(this->priv_bucket_pointer()[n]).begin();
2354 return const_local_iterator(sit, this->real_value_traits_ptr());
2355 }
2356
2357 //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
2358 //!
2359 //! <b>Effects</b>: Returns a local_iterator pointing to the end
2360 //! of the sequence stored in the bucket n.
2361 //!
2362 //! <b>Complexity</b>: Constant.
2363 //!
2364 //! <b>Throws</b>: Nothing.
2365 //!
2366 //! <b>Note</b>: [this->begin(n), this->end(n)) is a valid range
2367 //! containing all of the elements in the nth bucket.
2368 local_iterator end(size_type n)
2369 { return local_iterator(this->priv_bucket_pointer()[n].end(), this->real_value_traits_ptr()); }
2370
2371 //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
2372 //!
2373 //! <b>Effects</b>: Returns a const_local_iterator pointing to the end
2374 //! of the sequence stored in the bucket n.
2375 //!
2376 //! <b>Complexity</b>: Constant.
2377 //!
2378 //! <b>Throws</b>: Nothing.
2379 //!
2380 //! <b>Note</b>: [this->begin(n), this->end(n)) is a valid range
2381 //! containing all of the elements in the nth bucket.
2382 const_local_iterator end(size_type n) const
2383 { return this->cend(n); }
2384
2385 //! <b>Requires</b>: n is in the range [0, this->bucket_count()).
2386 //!
2387 //! <b>Effects</b>: Returns a const_local_iterator pointing to the end
2388 //! of the sequence stored in the bucket n.
2389 //!
2390 //! <b>Complexity</b>: Constant.
2391 //!
2392 //! <b>Throws</b>: Nothing.
2393 //!
2394 //! <b>Note</b>: [this->begin(n), this->end(n)) is a valid range
2395 //! containing all of the elements in the nth bucket.
2396 const_local_iterator cend(size_type n) const
2397 {
2398 return const_local_iterator ( const_cast<bucket_type&>(this->priv_bucket_pointer()[n]).end()
2399 , this->real_value_traits_ptr());
2400 }
2401
2402 //! <b>Requires</b>: new_bucket_traits can hold a pointer to a new bucket array
2403 //! or the same as the old bucket array with a different length. new_size is the length of the
2404 //! the array pointed by new_buckets. If new_bucket_traits.bucket_begin() == this->bucket_pointer()
2405 //! new_bucket_traits.bucket_count() can be bigger or smaller than this->bucket_count().
2406 //! 'new_bucket_traits' copy constructor should not throw.
2407 //!
2408 //! <b>Effects</b>: Updates the internal reference with the new bucket, erases
2409 //! the values from the old bucket and inserts then in the new one.
2410 //! Bucket traits hold by *this is assigned from new_bucket_traits.
2411 //! If the container is configured as incremental<>, the split bucket is set
2412 //! to the new bucket_count().
2413 //!
2414 //! If store_hash option is true, this method does not use the hash function.
2415 //!
2416 //! <b>Complexity</b>: Average case linear in this->size(), worst case quadratic.
2417 //!
2418 //! <b>Throws</b>: If the hasher functor throws. Basic guarantee.
2419 void rehash(const bucket_traits &new_bucket_traits)
2420 {
2421 const bucket_ptr new_buckets = new_bucket_traits.bucket_begin();
2422 size_type new_bucket_count = new_bucket_traits.bucket_count();
2423 const bucket_ptr old_buckets = this->priv_bucket_pointer();
2424 size_type old_bucket_count = this->priv_bucket_count();
2425
2426 //Check power of two bucket array if the option is activated
2427 BOOST_INTRUSIVE_INVARIANT_ASSERT
2428 (!power_2_buckets || (0 == (new_bucket_count & (new_bucket_count-1u))));
2429
2430 size_type n = this->priv_get_cache_bucket_num();
2431 const bool same_buffer = old_buckets == new_buckets;
2432 //If the new bucket length is a common factor
2433 //of the old one we can avoid hash calculations.
2434 const bool fast_shrink = (!incremental) && (old_bucket_count > new_bucket_count) &&
2435 (power_2_buckets ||(old_bucket_count % new_bucket_count) == 0);
2436 //If we are shrinking the same bucket array and it's
2437 //is a fast shrink, just rehash the last nodes
2438 size_type new_first_bucket_num = new_bucket_count;
2439 if(same_buffer && fast_shrink && (n < new_bucket_count)){
2440 n = new_bucket_count;
2441 new_first_bucket_num = this->priv_get_cache_bucket_num();
2442 }
2443
2444 //Anti-exception stuff: they destroy the elements if something goes wrong.
2445 //If the source and destination buckets are the same, the second rollback function
2446 //is harmless, because all elements have been already unlinked and destroyed
2447 typedef detail::init_disposer<node_algorithms> NodeDisposer;
2448 NodeDisposer node_disp;
2449 bucket_type & newbuck = new_buckets[0];
2450 bucket_type & oldbuck = old_buckets[0];
2451 detail::exception_array_disposer<bucket_type, NodeDisposer, size_type>
2452 rollback1(newbuck, node_disp, new_bucket_count);
2453 detail::exception_array_disposer<bucket_type, NodeDisposer, size_type>
2454 rollback2(oldbuck, node_disp, old_bucket_count);
2455
2456 //Put size in a safe value for rollback exception
2457 size_type size_backup = this->priv_size_traits().get_size();
2458 this->priv_size_traits().set_size(0);
2459 //Put cache to safe position
2460 this->priv_initialize_cache();
2461 this->priv_insertion_update_cache(size_type(0u));
2462
2463 //Iterate through nodes
2464 for(; n < old_bucket_count; ++n){
2465 bucket_type &old_bucket = old_buckets[n];
2466
2467 if(!fast_shrink){
2468 siterator before_i(old_bucket.before_begin());
2469 siterator end_sit(old_bucket.end());
2470 siterator i(old_bucket.begin());
2471 for(;i != end_sit; ++i){
2472 const value_type &v = this->priv_value_from_slist_node(i.pointed_node());
2473 const std::size_t hash_value = this->priv_stored_or_compute_hash(v, store_hash_t());
2474 const size_type new_n = detail::hash_to_bucket_split<power_2_buckets, incremental>(hash_value, new_bucket_count, new_bucket_count);
2475 if(cache_begin && new_n < new_first_bucket_num)
2476 new_first_bucket_num = new_n;
2477 siterator last = bucket_type::s_iterator_to
2478 (*group_functions_t::get_last_in_group
2479 (detail::dcast_bucket_ptr<node>(i.pointed_node()), optimize_multikey_t()));
2480 if(same_buffer && new_n == n){
2481 before_i = last;
2482 }
2483 else{
2484 bucket_type &new_b = new_buckets[new_n];
2485 new_b.splice_after(new_b.before_begin(), old_bucket, before_i, last);
2486 }
2487 i = before_i;
2488 }
2489 }
2490 else{
2491 const size_type new_n = detail::hash_to_bucket_split<power_2_buckets, incremental>(n, new_bucket_count, new_bucket_count);
2492 if(cache_begin && new_n < new_first_bucket_num)
2493 new_first_bucket_num = new_n;
2494 bucket_type &new_b = new_buckets[new_n];
2495 if(!old_bucket.empty()){
2496 new_b.splice_after( new_b.before_begin()
2497 , old_bucket
2498 , old_bucket.before_begin()
2499 , hashtable_impl::priv_get_last(old_bucket));
2500 }
2501 }
2502 }
2503
2504 this->priv_size_traits().set_size(size_backup);
2505 this->priv_split_traits().set_size(new_bucket_count);
2506 this->priv_real_bucket_traits() = new_bucket_traits;
2507 this->priv_initialize_cache();
2508 this->priv_insertion_update_cache(new_first_bucket_num);
2509 rollback1.release();
2510 rollback2.release();
2511 }
2512
2513 //! <b>Requires</b>:
2514 //!
2515 //! <b>Effects</b>:
2516 //!
2517 //! <b>Complexity</b>:
2518 //!
2519 //! <b>Throws</b>:
2520 //!
2521 //! <b>Note</b>: this method is only available if incremental<true> option is activated.
2522 bool incremental_rehash(bool grow = true)
2523 {
2524 //This function is only available for containers with incremental hashing
2525 BOOST_STATIC_ASSERT(( incremental && power_2_buckets ));
2526 const size_type split_idx = this->priv_split_traits().get_size();
2527 const size_type bucket_cnt = this->priv_bucket_count();
2528 const bucket_ptr buck_ptr = this->priv_bucket_pointer();
2529
2530 if(grow){
2531 //Test if the split variable can be changed
2532 if(split_idx >= bucket_cnt)
2533 return false;
2534
2535 const size_type bucket_to_rehash = split_idx - bucket_cnt/2;
2536 bucket_type &old_bucket = buck_ptr[bucket_to_rehash];
2537 siterator before_i(old_bucket.before_begin());
2538 const siterator end_sit(old_bucket.end());
2539 siterator i(old_bucket.begin());
2540 this->priv_split_traits().increment();
2541
2542 //Anti-exception stuff: if an exception is thrown while
2543 //moving elements from old_bucket to the target bucket, all moved
2544 //elements are moved back to the original one.
2545 detail::incremental_rehash_rollback<bucket_type, split_traits> rollback
2546 ( buck_ptr[split_idx], old_bucket, this->priv_split_traits());
2547 for(;i != end_sit; ++i){
2548 const value_type &v = this->priv_value_from_slist_node(i.pointed_node());
2549 const std::size_t hash_value = this->priv_stored_or_compute_hash(v, store_hash_t());
2550 const size_type new_n = this->priv_hash_to_bucket(hash_value);
2551 siterator last = bucket_type::s_iterator_to
2552 (*group_functions_t::get_last_in_group
2553 (detail::dcast_bucket_ptr<node>(i.pointed_node()), optimize_multikey_t()));
2554 if(new_n == bucket_to_rehash){
2555 before_i = last;
2556 }
2557 else{
2558 bucket_type &new_b = buck_ptr[new_n];
2559 new_b.splice_after(new_b.before_begin(), old_bucket, before_i, last);
2560 }
2561 i = before_i;
2562 }
2563 rollback.release();
2564 this->priv_erasure_update_cache();
2565 return true;
2566 }
2567 else{
2568 //Test if the split variable can be changed
2569 if(split_idx <= bucket_cnt/2)
2570 return false;
2571 const size_type target_bucket_num = split_idx - 1 - bucket_cnt/2;
2572 bucket_type &target_bucket = buck_ptr[target_bucket_num];
2573 bucket_type &source_bucket = buck_ptr[split_idx-1];
2574 target_bucket.splice_after(target_bucket.cbefore_begin(), source_bucket);
2575 this->priv_split_traits().decrement();
2576 this->priv_insertion_update_cache(target_bucket_num);
2577 return true;
2578 }
2579 }
2580
2581 //! <b>Effects</b>: If new_bucket_traits.bucket_count() is not
2582 //! this->bucket_count()/2 or this->bucket_count()*2, or
2583 //! this->split_bucket() != new_bucket_traits.bucket_count() returns false
2584 //! and does nothing.
2585 //!
2586 //! Otherwise, copy assigns new_bucket_traits to the internal bucket_traits
2587 //! and transfers all the objects from old buckets to the new ones.
2588 //!
2589 //! <b>Complexity</b>: Linear to size().
2590 //!
2591 //! <b>Throws</b>: Nothing
2592 //!
2593 //! <b>Note</b>: this method is only available if incremental<true> option is activated.
2594 bool incremental_rehash(const bucket_traits &new_bucket_traits)
2595 {
2596 //This function is only available for containers with incremental hashing
2597 BOOST_STATIC_ASSERT(( incremental && power_2_buckets ));
2598 size_type new_bucket_traits_size = new_bucket_traits.bucket_count();
2599 size_type cur_bucket_traits = this->priv_bucket_count();
2600 if(new_bucket_traits_size/2 != cur_bucket_traits && new_bucket_traits_size != cur_bucket_traits/2){
2601 return false;
2602 }
2603
2604 const size_type split_idx = this->split_count();
2605
2606 if(new_bucket_traits_size/2 == cur_bucket_traits){
2607 //Test if the split variable can be changed
2608 if(!(split_idx >= cur_bucket_traits))
2609 return false;
2610 }
2611 else{
2612 //Test if the split variable can be changed
2613 if(!(split_idx <= cur_bucket_traits/2))
2614 return false;
2615 }
2616
2617 const size_type ini_n = this->priv_get_cache_bucket_num();
2618 const bucket_ptr old_buckets = this->priv_bucket_pointer();
2619 this->priv_real_bucket_traits() = new_bucket_traits;
2620 if(new_bucket_traits.bucket_begin() != old_buckets){
2621 for(size_type n = ini_n; n < split_idx; ++n){
2622 bucket_type &new_bucket = new_bucket_traits.bucket_begin()[n];
2623 bucket_type &old_bucket = old_buckets[n];
2624 new_bucket.splice_after(new_bucket.cbefore_begin(), old_bucket);
2625 }
2626 //Put cache to safe position
2627 this->priv_initialize_cache();
2628 this->priv_insertion_update_cache(ini_n);
2629 }
2630 return true;
2631 }
2632
2633 //! <b>Requires</b>:
2634 //!
2635 //! <b>Effects</b>:
2636 //!
2637 //! <b>Complexity</b>:
2638 //!
2639 //! <b>Throws</b>:
2640 size_type split_count() const
2641 {
2642 //This function is only available if incremental hashing is activated
2643 BOOST_STATIC_ASSERT(( incremental && power_2_buckets ));
2644 return this->priv_split_traits().get_size();
2645 }
2646
2647 //! <b>Effects</b>: Returns the nearest new bucket count optimized for
2648 //! the container that is bigger or equal than n. This suggestion can be
2649 //! used to create bucket arrays with a size that will usually improve
2650 //! container's performance. If such value does not exist, the
2651 //! higher possible value is returned.
2652 //!
2653 //! <b>Complexity</b>: Amortized constant time.
2654 //!
2655 //! <b>Throws</b>: Nothing.
2656 static size_type suggested_upper_bucket_count(size_type n)
2657 {
2658 const std::size_t *primes = &detail::prime_list_holder<0>::prime_list[0];
2659 const std::size_t *primes_end = primes + detail::prime_list_holder<0>::prime_list_size;
2660 std::size_t const* bound = std::lower_bound(primes, primes_end, n);
2661 if(bound == primes_end)
2662 --bound;
2663 return size_type(*bound);
2664 }
2665
2666 //! <b>Effects</b>: Returns the nearest new bucket count optimized for
2667 //! the container that is smaller or equal than n. This suggestion can be
2668 //! used to create bucket arrays with a size that will usually improve
2669 //! container's performance. If such value does not exist, the
2670 //! lowest possible value is returned.
2671 //!
2672 //! <b>Complexity</b>: Amortized constant time.
2673 //!
2674 //! <b>Throws</b>: Nothing.
2675 static size_type suggested_lower_bucket_count(size_type n)
2676 {
2677 const std::size_t *primes = &detail::prime_list_holder<0>::prime_list[0];
2678 const std::size_t *primes_end = primes + detail::prime_list_holder<0>::prime_list_size;
2679 size_type const* bound = std::upper_bound(primes, primes_end, n);
2680 if(bound != primes)
2681 --bound;
2682 return size_type(*bound);
2683 }
2684
2685 /// @cond
2686 private:
2687
2688 void priv_clear_buckets(bucket_ptr buckets_ptr, size_type bucket_cnt)
2689 {
2690 for(; bucket_cnt--; ++buckets_ptr){
2691 if(safemode_or_autounlink){
2692 bucket_plus_vtraits_t::priv_clear_group_nodes(*buckets_ptr, optimize_multikey_t());
2693 buckets_ptr->clear_and_dispose(detail::init_disposer<node_algorithms>());
2694 }
2695 else{
2696 buckets_ptr->clear();
2697 }
2698 }
2699 this->priv_initialize_cache();
2700 }
2701
2702 void priv_initialize_buckets()
2703 { this->priv_clear_buckets(this->priv_bucket_pointer(), this->priv_bucket_count()); }
2704
2705 void priv_clear_buckets()
2706 {
2707 this->priv_clear_buckets
2708 ( this->priv_get_cache()
2709 , this->priv_bucket_count() - (this->priv_get_cache() - this->priv_bucket_pointer()));
2710 }
2711
2712 std::size_t priv_hash_to_bucket(std::size_t hash_value) const
2713 {
2714 return detail::hash_to_bucket_split<power_2_buckets, incremental>
2715 (hash_value, this->priv_real_bucket_traits().bucket_count(), this->priv_split_traits().get_size());
2716 }
2717
2718 template<class Disposer>
2719 void priv_erase_range_impl
2720 (size_type bucket_num, siterator before_first_it, siterator end_sit, Disposer disposer, size_type &num_erased)
2721 {
2722 const bucket_ptr buckets = this->priv_bucket_pointer();
2723 bucket_type &b = buckets[bucket_num];
2724
2725 if(before_first_it == b.before_begin() && end_sit == b.end()){
2726 this->priv_erase_range_impl(bucket_num, 1, disposer, num_erased);
2727 }
2728 else{
2729 num_erased = 0;
2730 siterator to_erase(before_first_it);
2731 ++to_erase;
2732 slist_node_ptr end_ptr = end_sit.pointed_node();
2733 while(to_erase != end_sit){
2734 group_functions_t::erase_from_group(end_ptr, detail::dcast_bucket_ptr<node>(to_erase.pointed_node()), optimize_multikey_t());
2735 to_erase = b.erase_after_and_dispose(before_first_it, make_node_disposer(disposer));
2736 ++num_erased;
2737 }
2738 this->priv_size_traits().set_size(this->priv_size_traits().get_size()-num_erased);
2739 }
2740 }
2741
2742 template<class Disposer>
2743 void priv_erase_range_impl
2744 (size_type first_bucket_num, size_type num_buckets, Disposer disposer, size_type &num_erased)
2745 {
2746 //Now fully clear the intermediate buckets
2747 const bucket_ptr buckets = this->priv_bucket_pointer();
2748 num_erased = 0;
2749 for(size_type i = first_bucket_num; i < (num_buckets + first_bucket_num); ++i){
2750 bucket_type &b = buckets[i];
2751 siterator b_begin(b.before_begin());
2752 siterator nxt(b_begin);
2753 ++nxt;
2754 siterator end_sit(b.end());
2755 while(nxt != end_sit){
2756 group_functions_t::init_group(detail::dcast_bucket_ptr<node>(nxt.pointed_node()), optimize_multikey_t());
2757 nxt = b.erase_after_and_dispose
2758 (b_begin, make_node_disposer(disposer));
2759 this->priv_size_traits().decrement();
2760 ++num_erased;
2761 }
2762 }
2763 }
2764
2765 template<class Disposer>
2766 void priv_erase_range( siterator before_first_it, size_type first_bucket
2767 , siterator last_it, size_type last_bucket
2768 , Disposer disposer)
2769 {
2770 size_type num_erased;
2771 if (first_bucket == last_bucket){
2772 this->priv_erase_range_impl(first_bucket, before_first_it, last_it, disposer, num_erased);
2773 }
2774 else {
2775 bucket_type *b = (&this->priv_bucket_pointer()[0]);
2776 this->priv_erase_range_impl(first_bucket, before_first_it, b[first_bucket].end(), disposer, num_erased);
2777 if(size_type n = (last_bucket - first_bucket - 1))
2778 this->priv_erase_range_impl(first_bucket + 1, n, disposer, num_erased);
2779 this->priv_erase_range_impl(last_bucket, b[last_bucket].before_begin(), last_it, disposer, num_erased);
2780 }
2781 }
2782
2783 std::size_t priv_get_bucket_num(siterator it)
2784 { return this->priv_get_bucket_num_hash_dispatch(it, store_hash_t()); }
2785
2786 std::size_t priv_get_bucket_num_hash_dispatch(siterator it, detail::true_) //store_hash
2787 {
2788 return this->priv_hash_to_bucket
2789 (this->priv_stored_hash(it.pointed_node(), store_hash_t()));
2790 }
2791
2792 std::size_t priv_get_bucket_num_hash_dispatch(siterator it, detail::false_) //NO store_hash
2793 { return this->priv_get_bucket_num_no_hash_store(it, optimize_multikey_t()); }
2794
2795 static siterator priv_get_previous(bucket_type &b, siterator i)
2796 { return bucket_plus_vtraits_t::priv_get_previous(b, i, optimize_multikey_t()); }
2797
2798 static siterator priv_get_last(bucket_type &b)
2799 { return bucket_plus_vtraits_t::priv_get_last(b, optimize_multikey_t()); }
2800
2801 template<class Disposer>
2802 void priv_erase(const_iterator i, Disposer disposer, detail::true_)
2803 {
2804 slist_node_ptr elem(i.slist_it().pointed_node());
2805 slist_node_ptr f_bucket_end, l_bucket_end;
2806 if(store_hash){
2807 f_bucket_end = l_bucket_end =
2808 (this->priv_bucket_pointer()
2809 [this->priv_hash_to_bucket
2810 (this->priv_stored_hash(elem, store_hash_t()))
2811 ]).before_begin().pointed_node();
2812 }
2813 else{
2814 f_bucket_end = this->priv_bucket_pointer()->cend().pointed_node();
2815 l_bucket_end = f_bucket_end + this->priv_bucket_count() - 1;
2816 }
2817 node_ptr nxt_in_group;
2818 siterator prev = bucket_type::s_iterator_to
2819 (*group_functions_t::get_previous_and_next_in_group
2820 ( elem, nxt_in_group, f_bucket_end, l_bucket_end)
2821 );
2822 bucket_type::s_erase_after_and_dispose(prev, make_node_disposer(disposer));
2823 if(nxt_in_group)
2824 group_algorithms::unlink_after(nxt_in_group);
2825 if(safemode_or_autounlink)
2826 group_algorithms::init(detail::dcast_bucket_ptr<node>(elem));
2827 }
2828
2829 template <class Disposer>
2830 void priv_erase(const_iterator i, Disposer disposer, detail::false_)
2831 {
2832 siterator to_erase(i.slist_it());
2833 bucket_type &b = this->priv_bucket_pointer()[this->priv_get_bucket_num(to_erase)];
2834 siterator prev(this->priv_get_previous(b, to_erase));
2835 b.erase_after_and_dispose(prev, make_node_disposer(disposer));
2836 }
2837
2838 template<class KeyType, class KeyHasher, class KeyValueEqual>
2839 siterator priv_find
2840 ( const KeyType &key, KeyHasher hash_func
2841 , KeyValueEqual equal_func, size_type &bucket_number, std::size_t &h, siterator &previt) const
2842 {
2843 h = hash_func(key);
2844 return this->priv_find_with_hash(key, equal_func, bucket_number, h, previt);
2845 }
2846
2847 template<class KeyType, class KeyValueEqual>
2848 siterator priv_find_with_hash
2849 ( const KeyType &key, KeyValueEqual equal_func, size_type &bucket_number, const std::size_t h, siterator &previt) const
2850 {
2851 bucket_number = this->priv_hash_to_bucket(h);
2852 bucket_type &b = this->priv_bucket_pointer()[bucket_number];
2853 previt = b.before_begin();
2854 if(constant_time_size && this->empty()){
2855 return this->priv_invalid_local_it();
2856 }
2857
2858 siterator it = previt;
2859 ++it;
2860
2861 while(it != b.end()){
2862 const value_type &v = this->priv_value_from_slist_node(it.pointed_node());
2863 if(compare_hash){
2864 std::size_t vh = this->priv_stored_or_compute_hash(v, store_hash_t());
2865 if(h == vh && equal_func(key, v)){
2866 return it;
2867 }
2868 }
2869 else if(equal_func(key, v)){
2870 return it;
2871 }
2872 if(optimize_multikey){
2873 previt = bucket_type::s_iterator_to
2874 (*group_functions_t::get_last_in_group
2875 (detail::dcast_bucket_ptr<node>(it.pointed_node()), optimize_multikey_t()));
2876 it = previt;
2877 }
2878 else{
2879 previt = it;
2880 }
2881 ++it;
2882 }
2883 previt = b.before_begin();
2884 return this->priv_invalid_local_it();
2885 }
2886
2887 iterator priv_insert_equal_with_hash(reference value, std::size_t hash_value)
2888 {
2889 size_type bucket_num;
2890 siterator prev;
2891 siterator it = this->priv_find_with_hash
2892 (value, this->priv_equal(), bucket_num, hash_value, prev);
2893 return this->priv_insert_equal_find(value, bucket_num, hash_value, it);
2894 }
2895
2896 iterator priv_insert_equal_find(reference value, size_type bucket_num, std::size_t hash_value, siterator it)
2897 {
2898 bucket_type &b = this->priv_bucket_pointer()[bucket_num];
2899 bool found_equal = it != this->priv_invalid_local_it();
2900 if(!found_equal){
2901 it = b.before_begin();
2902 }
2903 //Now store hash if needed
2904 node_ptr n = pointer_traits<node_ptr>::pointer_to(this->priv_value_to_node(value));
2905 node_functions_t::store_hash(n, hash_value, store_hash_t());
2906 //Checks for some modes
2907 if(safemode_or_autounlink)
2908 BOOST_INTRUSIVE_SAFE_HOOK_DEFAULT_ASSERT(node_algorithms::unique(n));
2909 //Shortcut for optimize_multikey cases
2910 if(optimize_multikey){
2911 node_ptr first_in_group = found_equal ?
2912 detail::dcast_bucket_ptr<node>(it.pointed_node()) : node_ptr();
2913 group_functions_t::insert_in_group(first_in_group, n, optimize_multikey_t());
2914 }
2915 //Update cache and increment size if needed
2916 this->priv_insertion_update_cache(bucket_num);
2917 this->priv_size_traits().increment();
2918 //Insert the element in the bucket after it
2919 return iterator(b.insert_after(it, *n), &this->get_bucket_value_traits());
2920 }
2921
2922 template<class KeyType, class KeyHasher, class KeyValueEqual>
2923 std::pair<siterator, siterator> priv_equal_range
2924 ( const KeyType &key
2925 , KeyHasher hash_func
2926 , KeyValueEqual equal_func
2927 , size_type &bucket_number_first
2928 , size_type &bucket_number_second
2929 , size_type &cnt) const
2930 {
2931 std::size_t h;
2932 cnt = 0;
2933 siterator prev;
2934 //Let's see if the element is present
2935 std::pair<siterator, siterator> to_return
2936 ( this->priv_find(key, hash_func, equal_func, bucket_number_first, h, prev)
2937 , this->priv_invalid_local_it());
2938 if(to_return.first == to_return.second){
2939 bucket_number_second = bucket_number_first;
2940 return to_return;
2941 }
2942 {
2943 //If it's present, find the first that it's not equal in
2944 //the same bucket
2945 bucket_type &b = this->priv_bucket_pointer()[bucket_number_first];
2946 siterator it = to_return.first;
2947 if(optimize_multikey){
2948 to_return.second = bucket_type::s_iterator_to
2949 (*node_traits::get_next(group_functions_t::get_last_in_group
2950 (detail::dcast_bucket_ptr<node>(it.pointed_node()), optimize_multikey_t())));
2951 cnt = std::distance(it, to_return.second);
2952 if(to_return.second != b.end()){
2953 bucket_number_second = bucket_number_first;
2954 return to_return;
2955 }
2956 }
2957 else{
2958 ++cnt;
2959 ++it;
2960 while(it != b.end()){
2961 const value_type &v = this->priv_value_from_slist_node(it.pointed_node());
2962 if(compare_hash){
2963 std::size_t hv = this->priv_stored_or_compute_hash(v, store_hash_t());
2964 if(hv != h || !equal_func(key, v)){
2965 to_return.second = it;
2966 bucket_number_second = bucket_number_first;
2967 return to_return;
2968 }
2969 }
2970 else if(!equal_func(key, v)){
2971 to_return.second = it;
2972 bucket_number_second = bucket_number_first;
2973 return to_return;
2974 }
2975 ++it;
2976 ++cnt;
2977 }
2978 }
2979 }
2980
2981 //If we reached the end, find the first, non-empty bucket
2982 for(bucket_number_second = bucket_number_first+1
2983 ; bucket_number_second != this->priv_bucket_count()
2984 ; ++bucket_number_second){
2985 bucket_type &b = this->priv_bucket_pointer()[bucket_number_second];
2986 if(!b.empty()){
2987 to_return.second = b.begin();
2988 return to_return;
2989 }
2990 }
2991
2992 //Otherwise, return the end node
2993 to_return.second = this->priv_invalid_local_it();
2994 return to_return;
2995 }
2996 /// @endcond
2997 };
2998
2999 /// @cond
3000 #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
3001 template < class T
3002 , bool UniqueKeys
3003 , class PackedOptions
3004 >
3005 #else
3006 template <class T, bool UniqueKeys, class ...Options>
3007 #endif
3008 struct make_real_bucket_traits
3009 {
3010 //Real value traits must be calculated from options
3011 typedef typename detail::get_value_traits
3012 <T, typename PackedOptions::proto_value_traits>::type value_traits;
3013 /*
3014 static const bool resizable_bucket_traits =
3015 detail::resizable_bool_is_true<bucket_traits_traits>::value;*/
3016 typedef typename detail::get_real_value_traits<value_traits>::type real_value_traits;
3017 typedef typename PackedOptions::bucket_traits specified_bucket_traits;
3018
3019 //Real bucket traits must be calculated from options and calculated value_traits
3020 typedef typename detail::get_slist_impl
3021 <typename detail::reduced_slist_node_traits
3022 <typename real_value_traits::node_traits>::type
3023 >::type slist_impl;
3024
3025 typedef typename
3026 detail::if_c< detail::is_same
3027 < specified_bucket_traits
3028 , default_bucket_traits
3029 >::value
3030 , detail::bucket_traits_impl<slist_impl>
3031 , specified_bucket_traits
3032 >::type type;
3033 };
3034 /// @endcond
3035
3036 //! Helper metafunction to define a \c hashtable that yields to the same type when the
3037 //! same options (either explicitly or implicitly) are used.
3038 #if defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED) || defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
3039 template<class T, class ...Options>
3040 #else
3041 template<class T, class O1 = void, class O2 = void
3042 , class O3 = void, class O4 = void
3043 , class O5 = void, class O6 = void
3044 , class O7 = void, class O8 = void
3045 , class O9 = void, class O10= void
3046 >
3047 #endif
3048 struct make_hashtable
3049 {
3050 /// @cond
3051 typedef typename pack_options
3052 < hashtable_defaults,
3053 #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
3054 O1, O2, O3, O4, O5, O6, O7, O8, O9, O10
3055 #else
3056 Options...
3057 #endif
3058 >::type packed_options;
3059
3060 typedef typename detail::get_value_traits
3061 <T, typename packed_options::proto_value_traits>::type value_traits;
3062
3063 typedef typename make_real_bucket_traits
3064 <T, false, packed_options>::type real_bucket_traits;
3065
3066 typedef hashtable_impl
3067 < value_traits
3068 , typename packed_options::hash
3069 , typename packed_options::equal
3070 , typename packed_options::size_type
3071 , real_bucket_traits
3072 , (std::size_t(false)*hash_bool_flags::unique_keys_pos)
3073 | (std::size_t(packed_options::constant_time_size)*hash_bool_flags::constant_time_size_pos)
3074 | (std::size_t(packed_options::power_2_buckets)*hash_bool_flags::power_2_buckets_pos)
3075 | (std::size_t(packed_options::cache_begin)*hash_bool_flags::cache_begin_pos)
3076 | (std::size_t(packed_options::compare_hash)*hash_bool_flags::compare_hash_pos)
3077 | (std::size_t(packed_options::incremental)*hash_bool_flags::incremental_pos)
3078 > implementation_defined;
3079
3080 /// @endcond
3081 typedef implementation_defined type;
3082 };
3083
3084 #if !defined(BOOST_INTRUSIVE_DOXYGEN_INVOKED)
3085
3086 #if defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
3087 template<class T, class ...Options>
3088 #else
3089 template<class T, class O1, class O2, class O3, class O4, class O5, class O6, class O7, class O8, class O9, class O10>
3090 #endif
3091 class hashtable
3092 : public make_hashtable<T,
3093 #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
3094 O1, O2, O3, O4, O5, O6, O7, O8, O9, O10
3095 #else
3096 Options...
3097 #endif
3098 >::type
3099 {
3100 typedef typename make_hashtable<T,
3101 #if !defined(BOOST_INTRUSIVE_VARIADIC_TEMPLATES)
3102 O1, O2, O3, O4, O5, O6, O7, O8, O9, O10
3103 #else
3104 Options...
3105 #endif
3106 >::type Base;
3107 BOOST_MOVABLE_BUT_NOT_COPYABLE(hashtable)
3108
3109 public:
3110 typedef typename Base::value_traits value_traits;
3111 typedef typename Base::real_value_traits real_value_traits;
3112 typedef typename Base::iterator iterator;
3113 typedef typename Base::const_iterator const_iterator;
3114 typedef typename Base::bucket_ptr bucket_ptr;
3115 typedef typename Base::size_type size_type;
3116 typedef typename Base::hasher hasher;
3117 typedef typename Base::bucket_traits bucket_traits;
3118 typedef typename Base::key_equal key_equal;
3119
3120 //Assert if passed value traits are compatible with the type
3121 BOOST_STATIC_ASSERT((detail::is_same<typename real_value_traits::value_type, T>::value));
3122
3123 explicit hashtable ( const bucket_traits &b_traits
3124 , const hasher & hash_func = hasher()
3125 , const key_equal &equal_func = key_equal()
3126 , const value_traits &v_traits = value_traits())
3127 : Base(b_traits, hash_func, equal_func, v_traits)
3128 {}
3129
3130 hashtable(BOOST_RV_REF(hashtable) x)
3131 : Base(::boost::move(static_cast<Base&>(x)))
3132 {}
3133
3134 hashtable& operator=(BOOST_RV_REF(hashtable) x)
3135 { return static_cast<hashtable&>(this->Base::operator=(::boost::move(static_cast<Base&>(x)))); }
3136 };
3137
3138 #endif
3139
3140 } //namespace intrusive
3141 } //namespace boost
3142
3143 #include <boost/intrusive/detail/config_end.hpp>
3144
3145 #endif //BOOST_INTRUSIVE_HASHTABLE_HPP