-
Notifications
You must be signed in to change notification settings - Fork 19
/
chash.h
1428 lines (1363 loc) · 47.4 KB
/
chash.h
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
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
#include <cstdint>
#include <algorithm>
#include <utility>
#include <memory>
#include <cstring>
#include <stdexcept>
#include <functional>
#include <cmath>
#include <type_traits>
namespace contiguous_hash_detail
{
class move_trivial_tag
{
};
class move_assign_tag
{
};
template<class T> struct is_trivial_expand : public std::is_trivial<T>
{
};
template<class K, class V> struct is_trivial_expand<std::pair<K, V>> : public std::conditional<std::is_trivial<K>::value && std::is_trivial<V>::value, std::true_type, std::false_type>::type
{
};
template<class iterator_t> struct get_tag
{
typedef typename std::conditional<is_trivial_expand<typename std::iterator_traits<iterator_t>::value_type>::value, move_trivial_tag, move_assign_tag>::type type;
};
template<class iterator_t, class tag_t, class ...args_t> void construct_one(iterator_t where, tag_t, args_t &&...args)
{
typedef typename std::iterator_traits<iterator_t>::value_type iterator_value_t;
::new(std::addressof(*where)) iterator_value_t(std::forward<args_t>(args)...);
}
template<class iterator_t> void destroy_one(iterator_t where, move_trivial_tag)
{
}
template<class iterator_t> void destroy_one(iterator_t where, move_assign_tag)
{
typedef typename std::iterator_traits<iterator_t>::value_type iterator_value_t;
where->~iterator_value_t();
}
template<class iterator_from_t, class iterator_to_t> void move_construct_and_destroy(iterator_from_t move_begin, iterator_from_t move_end, iterator_to_t to_begin, move_trivial_tag)
{
std::ptrdiff_t count = move_end - move_begin;
std::memmove(&*to_begin, &*move_begin, count * sizeof(*move_begin));
}
template<class iterator_from_t, class iterator_to_t> void move_construct_and_destroy(iterator_from_t move_begin, iterator_from_t move_end, iterator_to_t to_begin, move_assign_tag)
{
for(; move_begin != move_end; ++move_begin)
{
construct_one(to_begin++, move_assign_tag(), std::move(*move_begin));
destroy_one(move_begin, move_assign_tag());
}
}
}
template<class config_t>
class contiguous_hash
{
public:
typedef typename config_t::key_type key_type;
typedef typename config_t::mapped_type mapped_type;
typedef typename config_t::value_type value_type;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef typename config_t::hasher hasher;
typedef typename config_t::key_equal key_equal;
typedef typename config_t::allocator_type allocator_type;
typedef typename config_t::offset_type offset_type;
typedef typename config_t::hash_value_type hash_value_type;
typedef value_type &reference;
typedef value_type const &const_reference;
typedef value_type *pointer;
typedef value_type const *const_pointer;
static constexpr offset_type offset_empty = offset_type(-1);
protected:
struct hash_t
{
hash_value_type hash;
hash_t()
{
}
hash_t(hash_value_type value)
{
hash = value & ~(hash_value_type(1) << (sizeof(hash_value_type) * 8 - 1));
}
hash_t &operator = (hash_t const &other)
{
hash = other.hash;
return *this;
}
template<class any_type> any_type operator % (any_type const &value) const
{
return hash % value;
}
bool operator == (hash_t const &other) const
{
return hash == other.hash;
}
bool operator !() const
{
return hash == ~hash_value_type(0);
}
operator bool() const
{
return hash != ~hash_value_type(0);
}
void clear()
{
hash = ~hash_value_type(0);
}
};
struct index_t
{
hash_t hash;
offset_type next;
offset_type prev;
};
struct value_t
{
typename std::aligned_storage<sizeof(value_type), std::alignment_of<value_type>::value>::type value_pod;
value_type *value()
{
return reinterpret_cast<value_type *>(&value_pod);
}
value_type const *value() const
{
return reinterpret_cast<value_type const *>(&value_pod);
}
};
typedef typename allocator_type::template rebind<offset_type>::other bucket_allocator_t;
typedef typename allocator_type::template rebind<index_t>::other index_allocator_t;
typedef typename allocator_type::template rebind<value_t>::other value_allocator_t;
struct root_t : public hasher, public key_equal, public bucket_allocator_t, public index_allocator_t, public value_allocator_t
{
template<class any_hasher, class any_key_equal, class any_allocator_type> root_t(any_hasher &&hash, any_key_equal &&equal, any_allocator_type &&alloc)
: hasher(std::forward<any_hasher>(hash))
, key_equal(std::forward<any_key_equal>(equal))
, bucket_allocator_t(alloc)
, index_allocator_t(alloc)
, value_allocator_t(std::forward<any_allocator_type>(alloc))
{
static_assert(std::is_unsigned<offset_type>::value && std::is_integral<offset_type>::value, "offset_type must be unsighed integer");
static_assert(sizeof(offset_type) <= sizeof(contiguous_hash::size_type), "offset_type too big");
static_assert(std::is_integral<hash_value_type>::value, "hash_value_type must be integer");
bucket_count = 0;
capacity = 0;
size = 0;
free_count = 0;
free_list = offset_empty;
setting_load_factor = 1;
bucket = nullptr;
index = nullptr;
value = nullptr;
}
typename contiguous_hash::size_type bucket_count;
typename contiguous_hash::size_type capacity;
typename contiguous_hash::size_type size;
typename contiguous_hash::size_type free_count;
offset_type free_list;
float setting_load_factor;
offset_type *bucket;
index_t *index;
value_t *value;
};
template<class k_t, class v_t> struct get_key_select_t
{
key_type const &operator()(key_type const &value)
{
return value;
}
key_type const &operator()(value_type const &value)
{
return config_t::get_key(value);
}
template<class ...args_t> key_type const &operator()(key_type const &in, args_t &&...args)
{
return (*this)(in);
}
};
template<class k_t> struct get_key_select_t<k_t, k_t>
{
key_type const &operator()(key_type const &value)
{
return config_t::get_key(value);
}
template<class in_t, class ...args_t> key_type operator()(in_t const &in, args_t const &...args)
{
return key_type(in, args...);
}
};
typedef get_key_select_t<key_type, value_type> get_key_t;
public:
class iterator
{
public:
typedef std::forward_iterator_tag iterator_category;
typedef typename contiguous_hash::value_type value_type;
typedef typename contiguous_hash::difference_type difference_type;
typedef typename contiguous_hash::reference reference;
typedef typename contiguous_hash::pointer pointer;
public:
iterator(size_type _offset, contiguous_hash const *_self) : offset(_offset), self(_self)
{
}
iterator(iterator const &) = default;
iterator &operator++()
{
offset = self->advance_next_(offset);
return *this;
}
iterator operator++(int)
{
iterator save(*this);
++*this;
return save;
}
reference operator *() const
{
return *self->root_.value[offset].value();
}
pointer operator->() const
{
return self->root_.value[offset].value();
}
bool operator == (iterator const &other) const
{
return offset == other.offset && self == other.self;
}
bool operator != (iterator const &other) const
{
return offset != other.offset || self != other.self;
}
private:
friend class contiguous_hash;
size_type offset;
contiguous_hash const *self;
};
class const_iterator
{
public:
typedef std::forward_iterator_tag iterator_category;
typedef typename contiguous_hash::value_type value_type;
typedef typename contiguous_hash::difference_type difference_type;
typedef typename contiguous_hash::reference reference;
typedef typename contiguous_hash::const_reference const_reference;
typedef typename contiguous_hash::pointer pointer;
typedef typename contiguous_hash::const_pointer const_pointer;
public:
const_iterator(size_type _offset, contiguous_hash const *_self) : offset(_offset), self(_self)
{
}
const_iterator(const_iterator const &) = default;
const_iterator(iterator const &it) : offset(it.offset), self(it.self)
{
}
const_iterator &operator++()
{
offset = self->advance_next_(offset);
return *this;
}
const_iterator operator++(int)
{
const_iterator save(*this);
++*this;
return save;
}
const_reference operator *() const
{
return *self->root_.value[offset].value();
}
const_pointer operator->() const
{
return self->root_.value[offset].value();
}
bool operator == (const_iterator const &other) const
{
return offset == other.offset && self == other.self;
}
bool operator != (const_iterator const &other) const
{
return offset != other.offset || self != other.self;
}
private:
friend class contiguous_hash;
size_type offset;
contiguous_hash const *self;
};
class local_iterator
{
public:
typedef std::forward_iterator_tag iterator_category;
typedef typename contiguous_hash::value_type value_type;
typedef typename contiguous_hash::difference_type difference_type;
typedef typename contiguous_hash::reference reference;
typedef typename contiguous_hash::pointer pointer;
public:
local_iterator(size_type _offset, contiguous_hash const *_self) : offset(_offset), self(_self)
{
}
local_iterator(local_iterator const &) = default;
local_iterator &operator++()
{
offset = self->local_advance_next_(offset);
return *this;
}
local_iterator operator++(int)
{
local_iterator save(*this);
++*this;
return save;
}
reference operator *() const
{
return *self->root_.value[offset].value();
}
pointer operator->() const
{
return self->root_.value[offset].value();
}
bool operator == (local_iterator const &other) const
{
return offset == other.offset && self == other.self;
}
bool operator != (local_iterator const &other) const
{
return offset != other.offset || self != other.self;
}
private:
friend class contiguous_hash;
size_type offset;
contiguous_hash const *self;
};
class const_local_iterator
{
public:
typedef std::forward_iterator_tag iterator_category;
typedef typename contiguous_hash::value_type value_type;
typedef typename contiguous_hash::difference_type difference_type;
typedef typename contiguous_hash::reference reference;
typedef typename contiguous_hash::const_reference const_reference;
typedef typename contiguous_hash::pointer pointer;
typedef typename contiguous_hash::const_pointer const_pointer;
public:
const_local_iterator(size_type _offset, contiguous_hash const *_self) : offset(_offset), self(_self)
{
}
const_local_iterator(const_local_iterator const &) = default;
const_local_iterator(local_iterator const &it) : offset(it.offset), self(it.self)
{
}
const_local_iterator &operator++()
{
offset = self->local_advance_next_(offset);
return *this;
}
const_local_iterator operator++(int)
{
const_local_iterator save(*this);
++*this;
return save;
}
const_reference operator *() const
{
return *self->root_.value[offset].value();
}
const_pointer operator->() const
{
return self->root_.value[offset].value();
}
bool operator == (const_local_iterator const &other) const
{
return offset == other.offset && self == other.self;
}
bool operator != (const_local_iterator const &other) const
{
return offset != other.offset || self != other.self;
}
private:
friend class contiguous_hash;
size_type offset;
contiguous_hash const *self;
};
typedef typename std::conditional<config_t::unique_type::value, std::pair<iterator, bool>, iterator>::type insert_result_t;
typedef std::pair<iterator, bool> pair_ib_t;
protected:
typedef std::pair<size_type, bool> pair_posi_t;
template<class unique_type> typename std::enable_if<unique_type::value, insert_result_t>::type result_(pair_posi_t posi)
{
return std::make_pair(iterator(posi.first, this), posi.second);
}
template<class unique_type> typename std::enable_if<!unique_type::value, insert_result_t>::type result_(pair_posi_t posi)
{
return iterator(posi.first, this);
}
public:
//empty
contiguous_hash() : root_(hasher(), key_equal(), allocator_type())
{
}
//empty
explicit contiguous_hash(size_type bucket_count, hasher const &hash = hasher(), key_equal const &equal = key_equal(), allocator_type const &alloc = allocator_type()) : root_(hash, equal, alloc)
{
rehash(bucket_count);
}
//empty
explicit contiguous_hash(allocator_type const &alloc) : root_(hasher(), key_equal(), alloc)
{
}
//empty
contiguous_hash(size_type bucket_count, allocator_type const &alloc) : root_(hasher(), key_equal(), alloc)
{
rehash(bucket_count);
}
//empty
contiguous_hash(size_type bucket_count, hasher const &hash, allocator_type const &alloc) : root_(hash, key_equal(), alloc)
{
rehash(bucket_count);
}
//range
template <class iterator_t> contiguous_hash(iterator_t begin, iterator_t end, size_type bucket_count = 8, hasher const &hash = hasher(), key_equal const &equal = key_equal(), allocator_type const &alloc = allocator_type()) : root_(hash, equal, alloc)
{
rehash(bucket_count);
insert(begin, end);
}
//range
template <class iterator_t> contiguous_hash(iterator_t begin, iterator_t end, size_type bucket_count, allocator_type const &alloc) : root_(hasher(), key_equal(), alloc)
{
rehash(bucket_count);
insert(begin, end);
}
//range
template <class iterator_t> contiguous_hash(iterator_t begin, iterator_t end, size_type bucket_count, hasher const &hash, allocator_type const &alloc) : root_(hash, key_equal(), alloc)
{
rehash(bucket_count);
insert(begin, end);
}
//copy
contiguous_hash(contiguous_hash const &other) : root_(other.get_hasher(), other.get_key_equal(), other.get_value_allocator_())
{
copy_all_<false>(&other.root_);
}
//copy
contiguous_hash(contiguous_hash const &other, allocator_type const &alloc) : root_(other.get_hasher(), other.get_key_equal(), alloc)
{
copy_all_<false>(&other.root_);
}
//move
contiguous_hash(contiguous_hash &&other) : root_(hasher(), key_equal(), value_allocator_t())
{
swap(other);
}
//move
contiguous_hash(contiguous_hash &&other, allocator_type const &alloc) : root_(std::move(other.get_hasher()), std::move(other.get_key_equal()), alloc)
{
copy_all_<true>(&other.root_);
}
//initializer list
contiguous_hash(std::initializer_list<value_type> il, size_type bucket_count = 8, hasher const &hash = hasher(), key_equal const &equal = key_equal(), allocator_type const &alloc = allocator_type()) : contiguous_hash(il.begin(), il.end(), std::distance(il.begin(), il.end()), hash, equal, alloc)
{
}
//initializer list
contiguous_hash(std::initializer_list<value_type> il, size_type bucket_count, allocator_type const &alloc) : contiguous_hash(il.begin(), il.end(), std::distance(il.begin(), il.end()), alloc)
{
}
//initializer list
contiguous_hash(std::initializer_list<value_type> il, size_type bucket_count, hasher const &hash, allocator_type const &alloc) : contiguous_hash(il.begin(), il.end(), std::distance(il.begin(), il.end()), hash, alloc)
{
}
//destructor
~contiguous_hash()
{
dealloc_all_();
}
//copy
contiguous_hash &operator = (contiguous_hash const &other)
{
if(this == &other)
{
return *this;
}
dealloc_all_();
get_hasher() = other.get_hasher();
get_key_equal() = other.get_key_equal();
get_bucket_allocator_() = other.get_bucket_allocator_();
get_index_allocator_() = other.get_index_allocator_();
get_value_allocator_() = other.get_value_allocator_();
copy_all_<false>(&other.root_);
return *this;
}
//move
contiguous_hash &operator = (contiguous_hash &&other)
{
if(this == &other)
{
return *this;
}
swap(other);
return *this;
}
//initializer list
contiguous_hash &operator = (std::initializer_list<value_type> il)
{
clear();
rehash(std::distance(il.begin(), il.end()));
insert(il.begin(), il.end());
return *this;
}
allocator_type get_allocator() const
{
return *static_cast<value_allocator_t const *>(&root_);
}
hasher hash_function() const
{
return *static_cast<hasher const *>(&root_);
}
key_equal key_eq() const
{
return *static_cast<key_equal const *>(&root_);
}
void swap(contiguous_hash &other)
{
std::swap(root_, other.root_);
}
typedef std::pair<iterator, iterator> pair_ii_t;
typedef std::pair<const_iterator, const_iterator> pair_cici_t;
typedef std::pair<local_iterator, local_iterator> pair_lili_t;
typedef std::pair<const_local_iterator, const_local_iterator> pair_clicli_t;
//single element
insert_result_t insert(value_type const &value)
{
return result_<typename config_t::unique_type>(insert_value_(value));
}
//single element
template<class in_value_t> typename std::enable_if<std::is_convertible<in_value_t, value_type>::value, insert_result_t>::type insert(in_value_t &&value)
{
return result_<typename config_t::unique_type>(insert_value_(std::forward<in_value_t>(value)));
}
//with hint
iterator insert(const_iterator hint, value_type const &value)
{
return result_<typename config_t::unique_type>(insert_value_(value));
}
//with hint
template<class in_value_t> typename std::enable_if<std::is_convertible<in_value_t, value_type>::value, insert_result_t>::type insert(const_iterator hint, in_value_t &&value)
{
return result_<typename config_t::unique_type>(insert_value_(std::forward<in_value_t>(value)));
}
//range
template<class iterator_t> void insert(iterator_t begin, iterator_t end)
{
for(; begin != end; ++begin)
{
emplace_hint(cend(), *begin);
}
}
//initializer list
void insert(std::initializer_list<value_type> il)
{
insert(il.begin(), il.end());
}
//single element
template<class ...args_t> insert_result_t emplace(args_t &&...args)
{
return result_<typename config_t::unique_type>(insert_value_(std::forward<args_t>(args)...));
}
//with hint
template<class ...args_t> insert_result_t emplace_hint(const_iterator hint, args_t &&...args)
{
return result_<typename config_t::unique_type>(insert_value_(std::forward<args_t>(args)...));
}
template<class in_key_t> iterator find(in_key_t const &key)
{
if(root_.size == 0)
{
return end();
}
return iterator(find_value_(key), this);
}
template<class in_key_t> const_iterator find(in_key_t const &key) const
{
if(root_.size == 0)
{
return cend();
}
return const_iterator(find_value_(key), this);
}
template<class in_key_t, class = typename std::enable_if<std::is_convertible<in_key_t, key_type>::value && config_t::unique_type::value && !std::is_same<key_type, value_type>::value, void>::type> mapped_type &at(in_key_t const &key)
{
offset_type offset = root_.size;
if(root_.size != 0)
{
offset = find_value_(key);
}
if(offset == root_.size)
{
throw std::out_of_range("contiguous_hash out of range");
}
return root_.value[offset].value()->second;
}
template<class in_key_t, class = typename std::enable_if<std::is_convertible<in_key_t, key_type>::value && config_t::unique_type::value && !std::is_same<key_type, value_type>::value, void>::type> mapped_type const &at(in_key_t const &key) const
{
offset_type offset = root_.size;
if(root_.size != 0)
{
offset = find_value_(key);
}
if(offset == root_.size)
{
throw std::out_of_range("contiguous_hash out of range");
}
return root_.value[offset].value()->second;
}
template<class in_key_t, class = typename std::enable_if<std::is_convertible<in_key_t, key_type>::value && config_t::unique_type::value && !std::is_same<key_type, value_type>::value, void>::type> mapped_type &operator[](in_key_t &&key)
{
offset_type offset = root_.size;
if(root_.size != 0)
{
offset = find_value_(key);
}
if(offset == root_.size)
{
offset = insert_value_(key, mapped_type()).first;
}
return root_.value[offset].value()->second;
}
iterator erase(const_iterator it)
{
if(root_.size == 0)
{
return end();
}
remove_offset_(it.offset);
return iterator(advance_next_(it.offset), this);
}
local_iterator erase(const_local_iterator it)
{
if(root_.size == 0)
{
return local_iterator(offset_empty, this);
}
size_type next = local_advance_next_(offset_type(it.offset));
remove_offset_(it.offset);
return local_iterator(next, this);
}
size_type erase(key_type const &key)
{
if(root_.size == 0)
{
return 0;
}
return remove_value_(typename config_t::unique_type(), key);
}
iterator erase(const_iterator erase_begin, const_iterator erase_end)
{
if(erase_begin == cbegin() && erase_end == cend())
{
clear();
return end();
}
else
{
while(erase_begin != erase_end)
{
erase(erase_begin++);
}
return iterator(erase_begin.offset, this);
}
}
local_iterator erase(const_local_iterator erase_begin, const_local_iterator erase_end)
{
while(erase_begin != erase_end)
{
erase(erase_begin++);
}
return local_iterator(erase_begin.offset, this);
}
size_type count(key_type const &key) const
{
return find(key) == end() ? 0 : 1;
}
template<class in_key_t, class = typename std::enable_if<std::is_convertible<in_key_t, key_type>::value && config_t::unique_type::value, void>::type> pair_ii_t equal_range(in_key_t const &key)
{
auto where = find(key);
if(where == end())
{
return std::make_pair(end(), end());
}
else
{
return std::make_pair(where, iterator(advance_next_(where.offset), this));
}
}
template<class in_key_t, class = typename std::enable_if<std::is_convertible<in_key_t, key_type>::value && config_t::unique_type::value, void>::type> pair_cici_t equal_range(in_key_t const &key) const
{
auto where = find(key);
if(where == cend())
{
return std::make_pair(cend(), cend());
}
else
{
return std::make_pair(where, const_iterator(advance_next_(where.offset), this));
}
}
template<class in_key_t, class = typename std::enable_if<std::is_convertible<in_key_t, key_type>::value && !config_t::unique_type::value, void>::type> pair_lili_t equal_range(in_key_t const &key)
{
auto where = find(key);
if(where == end())
{
return std::make_pair(local_iterator(offset_empty, this), local_iterator(offset_empty, this));
}
else
{
return std::make_pair(local_iterator(where.offset, this), local_iterator(local_find_equal_(where.offset), this));
}
}
template<class in_key_t, class = typename std::enable_if<std::is_convertible<in_key_t, key_type>::value && !config_t::unique_type::value, void>::type> pair_clicli_t equal_range(in_key_t const &key) const
{
auto where = find(key);
if(where == end())
{
return std::make_pair(const_local_iterator(offset_empty, this), const_local_iterator(offset_empty, this));
}
else
{
return std::make_pair(const_local_iterator(where.offset, this), const_local_iterator(local_find_equal_(where.offset), this));
}
}
iterator begin()
{
return iterator(find_begin_(), this);
}
iterator end()
{
return iterator(root_.size, this);
}
const_iterator begin() const
{
return const_iterator(find_begin_(), this);
}
const_iterator end() const
{
return const_iterator(root_.size, this);
}
const_iterator cbegin() const
{
return const_iterator(find_begin_(), this);
}
const_iterator cend() const
{
return const_iterator(root_.size, this);
}
bool empty() const
{
return root_.size == root_.free_count;
}
void clear()
{
clear_all_();
}
size_type size() const
{
return root_.size - root_.free_count;
}
size_type max_size() const
{
return offset_empty - 1;
}
local_iterator begin(size_type n)
{
return local_iterator(root_.bucket[n], this);
}
local_iterator end(size_type n)
{
return local_iterator(offset_empty, this);
}
const_local_iterator begin(size_type n) const
{
return const_local_iterator(root_.bucket[n], this);
}
const_local_iterator end(size_type n) const
{
return const_local_iterator(offset_empty, this);
}
const_local_iterator cbegin(size_type n) const
{
return const_local_iterator(root_.bucket[n], this);
}
const_local_iterator cend(size_type n) const
{
return const_local_iterator(offset_empty, this);
}
size_type bucket_count() const
{
return root_.bucket_count;
}
size_type max_bucket_count() const
{
return max_size();
}
size_type bucket_size(size_type n) const
{
size_type step = 0;
for(size_type i = root_.bucket[n]; i != offset_empty; i = root_.index[i].next)
{
++step;
}
return step;
}
size_type bucket(key_type const &key) const
{
if(root_.size == 0)
{
return 0;
}
return hash_t(get_hasher()(key)) % root_.bucket_count;
}
void reserve(size_type count)
{
rehash(size_type(std::ceil(count / root_.setting_load_factor)));
if(count > root_.capacity && root_.capacity <= max_size())
{
realloc_(size_type(std::ceil(std::max<float>(float(count), root_.bucket_count * root_.setting_load_factor))));
}
}
void rehash(size_type count)
{
rehash_(typename config_t::unique_type(), std::max<size_type>({8, count, size_type(std::ceil(size() / root_.setting_load_factor))}));
}
void max_load_factor(float ml)
{
if(ml <= 0)
{
return;
}
root_.setting_load_factor = ml;
if(root_.size != 0)
{
rehash_(typename config_t::unique_type(), size_type(std::ceil(size() / root_.setting_load_factor)));
}
}
float max_load_factor() const
{
return root_.setting_load_factor;
}
float load_factor() const
{
if(root_.size == 0)
{
return 0;
}
return size() / float(root_.bucket_count);
}
protected:
root_t root_;
protected:
hasher &get_hasher()
{
return root_;
}
hasher const &get_hasher() const
{
return root_;
}
key_equal &get_key_equal()
{
return root_;
}
key_equal const &get_key_equal() const
{
return root_;
}
bucket_allocator_t &get_bucket_allocator_()
{
return root_;
}
bucket_allocator_t const &get_bucket_allocator_() const
{
return root_;
}
index_allocator_t &get_index_allocator_()
{
return root_;
}
index_allocator_t const &get_index_allocator_() const
{
return root_;
}
value_allocator_t &get_value_allocator_()
{
return root_;
}
value_allocator_t const &get_value_allocator_() const
{
return root_;
}
size_type advance_next_(size_type i) const
{
for(++i; i < root_.size; ++i)
{
if(root_.index[i].hash)
{
break;
}
}
return i;
}
size_type local_advance_next_(size_type i) const
{
return root_.index[i].next;
}
size_type local_find_equal_(size_type i) const
{
hash_t hash = root_.index[i].hash;
size_type next = root_.index[i].next;
while(next != offset_empty && root_.index[next].hash == hash && get_key_equal()(get_key_t()(*root_.value[i].value()), get_key_t()(*root_.value[next].value())))
{
next = root_.index[next].next;
}
return next;
}
size_type find_begin_() const
{
for(size_type i = 0; i < root_.size; ++i)
{
if(root_.index[i].hash)
{
return i;
}
}
return root_.size;
}
template<class iterator_t, class ...args_t> static void construct_one_(iterator_t where, args_t &&...args)
{
contiguous_hash_detail::construct_one(where, typename contiguous_hash_detail::get_tag<iterator_t>::type(), std::forward<args_t>(args)...);
}
template<class iterator_t> static void destroy_one_(iterator_t where)
{
contiguous_hash_detail::destroy_one(where, typename contiguous_hash_detail::get_tag<iterator_t>::type());
}
template<class iterator_from_t, class iterator_to_t> static void move_construct_and_destroy_(iterator_from_t move_begin, iterator_from_t move_end, iterator_to_t to_begin)
{
contiguous_hash_detail::move_construct_and_destroy(move_begin, move_end, to_begin, typename contiguous_hash_detail::get_tag<iterator_from_t>::type());
}
void dealloc_all_()
{
for(size_type i = 0; i < root_.size; ++i)
{
if(root_.index[i].hash)
{
destroy_one_(root_.value[i].value());
}
}
if(root_.bucket_count != 0)
{
get_bucket_allocator_().deallocate(root_.bucket, root_.bucket_count);