1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
|
// Copyright (C) 2003-2004 Jeremy B. Maitin-Shepard.
// Copyright (C) 2005-2009 Daniel James
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_UNORDERED_DETAIL_ALL_HPP_INCLUDED
#define BOOST_UNORDERED_DETAIL_ALL_HPP_INCLUDED
#include <cstddef>
#include <stdexcept>
#include <algorithm>
#include <boost/config/no_tr1/cmath.hpp>
#include <boost/iterator/iterator_categories.hpp>
#include <boost/throw_exception.hpp>
#include <boost/unordered/detail/buckets.hpp>
namespace boost { namespace unordered_detail {
////////////////////////////////////////////////////////////////////////////
// Helper methods
// strong exception safety, no side effects
template <class T>
inline bool hash_table<T>::equal(
key_type const& k, value_type const& v) const
{
return this->key_eq()(k, get_key(v));
}
// strong exception safety, no side effects
template <class T>
template <class Key, class Pred>
inline BOOST_DEDUCED_TYPENAME T::node_ptr
hash_table<T>::find_iterator(bucket_ptr bucket, Key const& k,
Pred const& eq) const
{
node_ptr it = bucket->next_;
while (BOOST_UNORDERED_BORLAND_BOOL(it) &&
!eq(k, get_key(node::get_value(it))))
{
it = node::next_group(it);
}
return it;
}
// strong exception safety, no side effects
template <class T>
inline BOOST_DEDUCED_TYPENAME T::node_ptr
hash_table<T>::find_iterator(
bucket_ptr bucket, key_type const& k) const
{
node_ptr it = bucket->next_;
while (BOOST_UNORDERED_BORLAND_BOOL(it) &&
!equal(k, node::get_value(it)))
{
it = node::next_group(it);
}
return it;
}
// strong exception safety, no side effects
// pre: this->buckets_
template <class T>
inline BOOST_DEDUCED_TYPENAME T::node_ptr
hash_table<T>::find_iterator(key_type const& k) const
{
return find_iterator(this->get_bucket(this->bucket_index(k)), k);
}
// strong exception safety, no side effects
template <class T>
inline BOOST_DEDUCED_TYPENAME T::node_ptr*
hash_table<T>::find_for_erase(
bucket_ptr bucket, key_type const& k) const
{
node_ptr* it = &bucket->next_;
while(BOOST_UNORDERED_BORLAND_BOOL(*it) &&
!equal(k, node::get_value(*it)))
{
it = &node::next_group(*it);
}
return it;
}
////////////////////////////////////////////////////////////////////////////
// Load methods
// no throw
template <class T>
std::size_t hash_table<T>::max_size() const
{
using namespace std;
// size < mlf_ * count
return double_to_size_t(ceil(
(double) this->mlf_ * this->max_bucket_count())) - 1;
}
// strong safety
template <class T>
inline std::size_t hash_table<T>::bucket_index(
key_type const& k) const
{
// hash_function can throw:
return this->hash_function()(k) % this->bucket_count_;
}
// no throw
template <class T>
inline std::size_t hash_table<T>::calculate_max_load()
{
using namespace std;
// From 6.3.1/13:
// Only resize when size >= mlf_ * count
return double_to_size_t(ceil((double) mlf_ * this->bucket_count_));
}
template <class T>
void hash_table<T>::max_load_factor(float z)
{
BOOST_ASSERT(z > 0);
mlf_ = (std::max)(z, minimum_max_load_factor);
this->max_load_ = this->calculate_max_load();
}
// no throw
template <class T>
inline std::size_t hash_table<T>::min_buckets_for_size(
std::size_t size) const
{
BOOST_ASSERT(this->mlf_ != 0);
using namespace std;
// From 6.3.1/13:
// size < mlf_ * count
// => count > size / mlf_
//
// Or from rehash post-condition:
// count > size / mlf_
return next_prime(double_to_size_t(floor(size / (double) mlf_)) + 1);
}
////////////////////////////////////////////////////////////////////////////
// recompute_begin_bucket
// init_buckets
template <class T>
inline void hash_table<T>::init_buckets()
{
if (this->size_) {
this->cached_begin_bucket_ = this->buckets_;
while (!this->cached_begin_bucket_->next_)
++this->cached_begin_bucket_;
} else {
this->cached_begin_bucket_ = this->get_bucket(this->bucket_count_);
}
this->max_load_ = calculate_max_load();
}
// After an erase cached_begin_bucket_ might be left pointing to
// an empty bucket, so this is called to update it
//
// no throw
template <class T>
inline void hash_table<T>::recompute_begin_bucket(bucket_ptr b)
{
BOOST_ASSERT(!(b < this->cached_begin_bucket_));
if(b == this->cached_begin_bucket_)
{
if (this->size_ != 0) {
while (!this->cached_begin_bucket_->next_)
++this->cached_begin_bucket_;
} else {
this->cached_begin_bucket_ =
this->get_bucket(this->bucket_count_);
}
}
}
// This is called when a range has been erased
//
// no throw
template <class T>
inline void hash_table<T>::recompute_begin_bucket(
bucket_ptr b1, bucket_ptr b2)
{
BOOST_ASSERT(!(b1 < this->cached_begin_bucket_) && !(b2 < b1));
BOOST_ASSERT(BOOST_UNORDERED_BORLAND_BOOL(b2->next_));
if(b1 == this->cached_begin_bucket_ && !b1->next_)
this->cached_begin_bucket_ = b2;
}
// no throw
template <class T>
inline float hash_table<T>::load_factor() const
{
BOOST_ASSERT(this->bucket_count_ != 0);
return static_cast<float>(this->size_)
/ static_cast<float>(this->bucket_count_);
}
////////////////////////////////////////////////////////////////////////////
// Constructors
template <class T>
hash_table<T>::hash_table(std::size_t num_buckets,
hasher const& hf, key_equal const& eq, node_allocator const& a)
: buckets(a, next_prime(num_buckets)),
base(hf, eq),
size_(),
mlf_(1.0f),
cached_begin_bucket_(),
max_load_(0)
{
}
// Copy Construct with allocator
template <class T>
hash_table<T>::hash_table(hash_table const& x,
node_allocator const& a)
: buckets(a, x.min_buckets_for_size(x.size_)),
base(x),
size_(x.size_),
mlf_(x.mlf_),
cached_begin_bucket_(),
max_load_(0)
{
if(x.size_) {
x.copy_buckets_to(*this);
this->init_buckets();
}
}
// Move Construct
template <class T>
hash_table<T>::hash_table(hash_table& x, move_tag)
: buckets(x.node_alloc(), x.bucket_count_),
base(x),
size_(0),
mlf_(1.0f),
cached_begin_bucket_(),
max_load_(0)
{
this->partial_swap(x);
}
template <class T>
hash_table<T>::hash_table(hash_table& x,
node_allocator const& a, move_tag)
: buckets(a, x.bucket_count_),
base(x),
size_(0),
mlf_(x.mlf_),
cached_begin_bucket_(),
max_load_(0)
{
if(a == x.node_alloc()) {
this->partial_swap(x);
}
else if(x.size_) {
x.copy_buckets_to(*this);
this->size_ = x.size_;
this->init_buckets();
}
}
template <class T>
hash_table<T>& hash_table<T>::operator=(
hash_table const& x)
{
hash_table tmp(x, this->node_alloc());
this->fast_swap(tmp);
return *this;
}
////////////////////////////////////////////////////////////////////////////
// Swap & Move
// Swap
//
// Strong exception safety
//
// Can throw if hash or predicate object's copy constructor throws
// or if allocators are unequal.
template <class T>
inline void hash_table<T>::partial_swap(hash_table& x)
{
this->buckets::swap(x); // No throw
std::swap(this->size_, x.size_);
std::swap(this->mlf_, x.mlf_);
std::swap(this->cached_begin_bucket_, x.cached_begin_bucket_);
std::swap(this->max_load_, x.max_load_);
}
template <class T>
inline void hash_table<T>::fast_swap(hash_table& x)
{
// These can throw, but they only affect the function objects
// that aren't in use so it is strongly exception safe, via.
// double buffering.
{
set_hash_functions<hasher, key_equal> op1(*this, x);
set_hash_functions<hasher, key_equal> op2(x, *this);
op1.commit();
op2.commit();
}
this->buckets::swap(x); // No throw
std::swap(this->size_, x.size_);
std::swap(this->mlf_, x.mlf_);
std::swap(this->cached_begin_bucket_, x.cached_begin_bucket_);
std::swap(this->max_load_, x.max_load_);
}
template <class T>
inline void hash_table<T>::slow_swap(hash_table& x)
{
if(this == &x) return;
{
// These can throw, but they only affect the function objects
// that aren't in use so it is strongly exception safe, via.
// double buffering.
set_hash_functions<hasher, key_equal> op1(*this, x);
set_hash_functions<hasher, key_equal> op2(x, *this);
// Create new buckets in separate hash_buckets objects
// which will clean up if anything throws an exception.
// (all can throw, but with no effect as these are new objects).
buckets b1(this->node_alloc(), x.min_buckets_for_size(x.size_));
if(x.size_) x.copy_buckets_to(b1);
buckets b2(x.node_alloc(), this->min_buckets_for_size(this->size_));
if(this->size_) copy_buckets_to(b2);
// Modifying the data, so no throw from now on.
b1.swap(*this);
b2.swap(x);
op1.commit();
op2.commit();
}
std::swap(this->size_, x.size_);
if(this->buckets_) this->init_buckets();
if(x.buckets_) x.init_buckets();
}
template <class T>
void hash_table<T>::swap(hash_table& x)
{
if(this->node_alloc() == x.node_alloc()) {
if(this != &x) this->fast_swap(x);
}
else {
this->slow_swap(x);
}
}
// Move
//
// Strong exception safety (might change unused function objects)
//
// Can throw if hash or predicate object's copy constructor throws
// or if allocators are unequal.
template <class T>
void hash_table<T>::move(hash_table& x)
{
// This can throw, but it only affects the function objects
// that aren't in use so it is strongly exception safe, via.
// double buffering.
set_hash_functions<hasher, key_equal> new_func_this(*this, x);
if(this->node_alloc() == x.node_alloc()) {
this->buckets::move(x); // no throw
this->size_ = x.size_;
this->cached_begin_bucket_ = x.cached_begin_bucket_;
this->max_load_ = x.max_load_;
x.size_ = 0;
}
else {
// Create new buckets in separate HASH_TABLE_DATA objects
// which will clean up if anything throws an exception.
// (all can throw, but with no effect as these are new objects).
buckets b(this->node_alloc(), x.min_buckets_for_size(x.size_));
if(x.size_) x.copy_buckets_to(b);
// Start updating the data here, no throw from now on.
this->size_ = x.size_;
b.swap(*this);
this->init_buckets();
}
// We've made it, the rest is no throw.
this->mlf_ = x.mlf_;
new_func_this.commit();
}
////////////////////////////////////////////////////////////////////////////
// Reserve & Rehash
// basic exception safety
template <class T>
inline void hash_table<T>::create_for_insert(std::size_t size)
{
this->bucket_count_ = (std::max)(this->bucket_count_,
this->min_buckets_for_size(size));
this->create_buckets();
this->init_buckets();
}
// basic exception safety
template <class T>
inline bool hash_table<T>::reserve_for_insert(std::size_t size)
{
if(size >= max_load_) {
std::size_t num_buckets
= this->min_buckets_for_size((std::max)(size,
this->size_ + (this->size_ >> 1)));
if(num_buckets != this->bucket_count_) {
rehash_impl(num_buckets);
return true;
}
}
return false;
}
// if hash function throws, basic exception safety
// strong otherwise.
template <class T>
inline void hash_table<T>::rehash(std::size_t min_buckets)
{
using namespace std;
if(!this->size_) {
if(this->buckets_) this->delete_buckets();
this->bucket_count_ = next_prime(min_buckets);
}
else {
// no throw:
min_buckets = next_prime((std::max)(min_buckets,
double_to_size_t(floor(this->size_ / (double) mlf_)) + 1));
if(min_buckets != this->bucket_count_) rehash_impl(min_buckets);
}
}
// if hash function throws, basic exception safety
// strong otherwise
template <class T>
void hash_table<T>
::rehash_impl(std::size_t num_buckets)
{
hasher const& hf = this->hash_function();
std::size_t size = this->size_;
bucket_ptr end = this->get_bucket(this->bucket_count_);
buckets dst(this->node_alloc(), num_buckets);
dst.create_buckets();
buckets src(this->node_alloc(), this->bucket_count_);
src.swap(*this);
this->size_ = 0;
for(bucket_ptr bucket = this->cached_begin_bucket_;
bucket != end; ++bucket)
{
node_ptr group = bucket->next_;
while(group) {
// Move the first group of equivalent nodes in bucket to dst.
// This next line throws iff the hash function throws.
bucket_ptr dst_bucket = dst.bucket_ptr_from_hash(
hf(get_key_from_ptr(group)));
node_ptr& next_group = node::next_group(group);
bucket->next_ = next_group;
next_group = dst_bucket->next_;
dst_bucket->next_ = group;
group = bucket->next_;
}
}
// Swap the new nodes back into the container and setup the local
// variables.
this->size_ = size;
dst.swap(*this); // no throw
this->init_buckets();
}
////////////////////////////////////////////////////////////////////////////
// copy_buckets_to
// copy_buckets_to
//
// basic excpetion safety. If an exception is thrown this will
// leave dst partially filled.
template <class T>
void hash_table<T>
::copy_buckets_to(buckets& dst) const
{
BOOST_ASSERT(this->buckets_ && !dst.buckets_);
hasher const& hf = this->hash_function();
bucket_ptr end = this->get_bucket(this->bucket_count_);
node_constructor a(dst);
dst.create_buckets();
// no throw:
for(bucket_ptr i = this->cached_begin_bucket_; i != end; ++i) {
// no throw:
for(node_ptr it = i->next_; it;) {
// hash function can throw.
bucket_ptr dst_bucket = dst.bucket_ptr_from_hash(
hf(get_key_from_ptr(it)));
// throws, strong
node_ptr group_end = node::next_group(it);
a.construct(node::get_value(it));
node_ptr n = a.release();
node::add_to_bucket(n, *dst_bucket);
for(it = it->next_; it != group_end; it = it->next_) {
a.construct(node::get_value(it));
node::add_after_node(a.release(), n);
}
}
}
}
////////////////////////////////////////////////////////////////////////////
// Misc. key methods
// strong exception safety
// count
//
// strong exception safety, no side effects
template <class T>
std::size_t hash_table<T>::count(key_type const& k) const
{
if(!this->size_) return 0;
node_ptr it = find_iterator(k); // throws, strong
return BOOST_UNORDERED_BORLAND_BOOL(it) ? node::group_count(it) : 0;
}
// find
//
// strong exception safety, no side effects
template <class T>
BOOST_DEDUCED_TYPENAME T::iterator_base
hash_table<T>::find(key_type const& k) const
{
if(!this->size_) return this->end();
bucket_ptr bucket = this->get_bucket(this->bucket_index(k));
node_ptr it = find_iterator(bucket, k);
if (BOOST_UNORDERED_BORLAND_BOOL(it))
return iterator_base(bucket, it);
else
return this->end();
}
template <class T>
template <class Key, class Hash, class Pred>
BOOST_DEDUCED_TYPENAME T::iterator_base hash_table<T>::find(Key const& k,
Hash const& h, Pred const& eq) const
{
if(!this->size_) return this->end();
bucket_ptr bucket = this->get_bucket(h(k) % this->bucket_count_);
node_ptr it = find_iterator(bucket, k, eq);
if (BOOST_UNORDERED_BORLAND_BOOL(it))
return iterator_base(bucket, it);
else
return this->end();
}
template <class T>
BOOST_DEDUCED_TYPENAME T::value_type&
hash_table<T>::at(key_type const& k) const
{
if(!this->size_)
boost::throw_exception(std::out_of_range("Unable to find key in unordered_map."));
bucket_ptr bucket = this->get_bucket(this->bucket_index(k));
node_ptr it = find_iterator(bucket, k);
if (!it)
boost::throw_exception(std::out_of_range("Unable to find key in unordered_map."));
return node::get_value(it);
}
// equal_range
//
// strong exception safety, no side effects
template <class T>
BOOST_DEDUCED_TYPENAME T::iterator_pair
hash_table<T>::equal_range(key_type const& k) const
{
if(!this->size_)
return iterator_pair(this->end(), this->end());
bucket_ptr bucket = this->get_bucket(this->bucket_index(k));
node_ptr it = find_iterator(bucket, k);
if (BOOST_UNORDERED_BORLAND_BOOL(it)) {
iterator_base first(iterator_base(bucket, it));
iterator_base second(first);
second.increment_bucket(node::next_group(second.node_));
return iterator_pair(first, second);
}
else {
return iterator_pair(this->end(), this->end());
}
}
////////////////////////////////////////////////////////////////////////////
// Erase methods
template <class T>
void hash_table<T>::clear()
{
if(!this->size_) return;
bucket_ptr end = this->get_bucket(this->bucket_count_);
for(bucket_ptr begin = this->buckets_; begin != end; ++begin) {
this->clear_bucket(begin);
}
this->size_ = 0;
this->cached_begin_bucket_ = end;
}
template <class T>
inline std::size_t hash_table<T>::erase_group(
node_ptr* it, bucket_ptr bucket)
{
node_ptr pos = *it;
node_ptr end = node::next_group(pos);
*it = end;
std::size_t count = this->delete_nodes(pos, end);
this->size_ -= count;
this->recompute_begin_bucket(bucket);
return count;
}
template <class T>
std::size_t hash_table<T>::erase_key(key_type const& k)
{
if(!this->size_) return 0;
// No side effects in initial section
bucket_ptr bucket = this->get_bucket(this->bucket_index(k));
node_ptr* it = this->find_for_erase(bucket, k);
// No throw.
return *it ? this->erase_group(it, bucket) : 0;
}
template <class T>
void hash_table<T>::erase(iterator_base r)
{
BOOST_ASSERT(r.node_);
--this->size_;
node::unlink_node(*r.bucket_, r.node_);
this->delete_node(r.node_);
// r has been invalidated but its bucket is still valid
this->recompute_begin_bucket(r.bucket_);
}
template <class T>
BOOST_DEDUCED_TYPENAME T::iterator_base
hash_table<T>::erase_return_iterator(iterator_base r)
{
BOOST_ASSERT(r.node_);
iterator_base next = r;
next.increment();
--this->size_;
node::unlink_node(*r.bucket_, r.node_);
this->delete_node(r.node_);
// r has been invalidated but its bucket is still valid
this->recompute_begin_bucket(r.bucket_, next.bucket_);
return next;
}
template <class T>
BOOST_DEDUCED_TYPENAME T::iterator_base
hash_table<T>::erase_range(
iterator_base r1, iterator_base r2)
{
if(r1 != r2)
{
BOOST_ASSERT(r1.node_);
if (r1.bucket_ == r2.bucket_) {
node::unlink_nodes(*r1.bucket_, r1.node_, r2.node_);
this->size_ -= this->delete_nodes(r1.node_, r2.node_);
// No need to call recompute_begin_bucket because
// the nodes are only deleted from one bucket, which
// still contains r2 after the erase.
BOOST_ASSERT(r1.bucket_->next_);
}
else {
bucket_ptr end_bucket = r2.node_ ?
r2.bucket_ : this->get_bucket(this->bucket_count_);
BOOST_ASSERT(r1.bucket_ < end_bucket);
node::unlink_nodes(*r1.bucket_, r1.node_, node_ptr());
this->size_ -= this->delete_nodes(r1.node_, node_ptr());
bucket_ptr i = r1.bucket_;
for(++i; i != end_bucket; ++i) {
this->size_ -= this->delete_nodes(i->next_, node_ptr());
i->next_ = node_ptr();
}
if(r2.node_) {
node_ptr first = r2.bucket_->next_;
node::unlink_nodes(*r2.bucket_, r2.node_);
this->size_ -= this->delete_nodes(first, r2.node_);
}
// r1 has been invalidated but its bucket is still
// valid.
this->recompute_begin_bucket(r1.bucket_, end_bucket);
}
}
return r2;
}
template <class T>
BOOST_DEDUCED_TYPENAME hash_table<T>::iterator_base
hash_table<T>::emplace_empty_impl_with_node(
node_constructor& a, std::size_t size)
{
key_type const& k = get_key(a.value());
std::size_t hash_value = this->hash_function()(k);
if(this->buckets_) this->reserve_for_insert(size);
else this->create_for_insert(size);
bucket_ptr bucket = this->bucket_ptr_from_hash(hash_value);
node_ptr n = a.release();
node::add_to_bucket(n, *bucket);
++this->size_;
this->cached_begin_bucket_ = bucket;
return iterator_base(bucket, n);
}
}}
#endif
|