-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpatchmap.hpp
2262 lines (2203 loc) · 75 KB
/
patchmap.hpp
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
#ifndef ORDERED_PATCH_MAP_H
#define ORDERED_PATCH_MAP_H
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cstdint>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <stdexcept>
#include <typeinfo>
#include <exception>
#include <memory>
namespace whash{
bool constexpr VERBOSE_PATCHMAP = false;
using std::allocator_traits;
using std::array;
using std::cerr;
using std::conditional;
using std::cout;
using std::decay;
using std::declval;
using std::enable_if;
using std::endl;
using std::false_type;
using std::get;
using std::index_sequence;
using std::index_sequence_for;
using std::initializer_list;
using std::integral_constant;
using std::invoke_result;
using std::is_const;
using std::is_fundamental;
using std::is_integral;
using std::is_pointer;
using std::is_same;
using std::is_signed;
using std::is_trivially_copyable;
using std::make_unique;
using std::make_unsigned;
using std::numeric_limits;
using std::pair;
using std::remove_const;
using std::setw;
using std::string;
using std::swap;
using std::to_string;
using std::true_type;
using std::tuple;
using std::tuple_size;
using std::unique_ptr;
using std::vector;
using std::void_t;
template<class T>
double frac(const T& n){
return n*pow(0.5,numeric_limits<T>::digits);
}
template<typename T>
struct dummy_comp{ // dummy comparator for when we don't need a comparator
constexpr bool operator()(const T&,const T&) const {return false;}
};
template <typename T>
constexpr size_t digits(const T& n=0){
return numeric_limits<T>::digits;
}
template<typename T>
typename std::enable_if<std::is_unsigned<T>::value,tuple<T,T>>::type
constexpr long_mul(const T& a, const T& b);
// calculate a * b = r0r1
template<typename T>
typename std::enable_if<std::is_unsigned<T>::value,tuple<T,T>>::type
constexpr long_mul(const T& a, const T& b){
const T N = digits<T>()/2;
const T t0 = (a>>N)*(b>>N);
const T t1 = ((a<<N)>>N)*(b>>N);
const T t2 = (a>>N)*((b<<N)>>N);
const T t3 = ((a<<N)>>N)*((b<<N)>>N);
const T t4 = t3+(t1<<N);
const T r1 = t4+(t2<<N);
const T r0 = (r1<t4)+(t4<t3)+(t1>>N)+(t2>>N)+t0;
return {r0,r1};
}
#ifdef __SIZEOF_INT128__
template<>
tuple<uint64_t,uint64_t>
constexpr long_mul(const uint64_t& a, const uint64_t& b){
unsigned __int128 r = ((unsigned __int128)(a))*((unsigned __int128)(b));
return {r>>64,r};
}
#endif
template<>
tuple<uint8_t,uint8_t> constexpr long_mul(const uint8_t& a,const uint8_t& b){
const int_fast16_t r = int_fast16_t(a)*int_fast16_t(b);
return {uint8_t(r>>8),uint8_t(r)};
}
template<>
tuple<uint16_t,uint16_t> constexpr long_mul(
const uint16_t& a,
const uint16_t& b){
const int_fast32_t r = int_fast32_t(a)*int_fast32_t(b);
return {uint16_t(r>>16),uint16_t(r)};
}
template<>
tuple<uint32_t,uint32_t> constexpr long_mul(
const uint32_t& a,
const uint32_t& b){
const int_fast64_t r = int_fast64_t(a)*int_fast64_t(b);
return {uint32_t(r>>32),uint32_t(r)};
}
template <typename T>
constexpr size_t popcount(const T n){
size_t c=0;
while(n) (n&=(n-1),++c);
return c;
}
constexpr size_t popcount(const uint32_t n){
return __builtin_popcountl(n);
}
constexpr size_t popcount(const uint64_t n){
return __builtin_popcountll(n);
}
template<typename T0,typename T1,typename T2>
constexpr T0 clip(const T0& n,const T1& l,const T2& h){
return n<l?l:n>h?h:n;
}
template <typename T>
typename std::enable_if<std::is_unsigned<T>::value,T>::type
constexpr clz(const T x,const T lower=0,const T upper=digits<T>()){
return (upper-lower==T(1))?digits<T>()-upper:
(x&(T(0)-T(1)<<((upper+lower)/2))?
clz(x,(upper+lower)/2,upper):
clz(x,lower,(upper+lower)/2));
}
template <typename T>
typename std::enable_if<std::is_unsigned<T>::value,T>::type
constexpr ctz(const T x,const T lower=0,const T upper=digits<T>()){
return
(upper-lower==T(1))?lower:(x&(T(0)-T(1)<<((upper+lower)/2))?
ctz(x,(upper+lower)/2,upper):
ctz(x,lower,(upper+lower)/2));
// TODO
}
template <typename T>
typename std::enable_if<std::is_unsigned<T>::value,T>::type
constexpr log2(const T x,const T lower=0,const T upper=digits<T>()){
return (upper-lower==T(1))?lower:(x&(T(0)-T(1)<<((upper+lower)/2))?
log2(x,(upper+lower)/2,upper):
log2(x,lower,(upper+lower)/2));
}
#if __GNUC__ > 3 || __clang__
uint32_t constexpr clz(const uint32_t x){
return x==0?32:__builtin_clz(x);
}
uint32_t constexpr ctz(const uint32_t x){
return x==0?32:__builtin_ctz(x);
}
uint64_t constexpr clz(const uint64_t x){
return x==0?64:__builtin_clzll(x);
}
uint64_t constexpr ctz(const uint64_t x){
return x==0?64:__builtin_ctzll(x);
}
uint32_t constexpr log2(const uint32_t x){
return x==0?0:31-__builtin_clz(x);
}
uint64_t constexpr log2(const uint64_t x){
return x==0?0:63-__builtin_clzll(x);
}
#endif
template <typename T,typename S>
typename std::enable_if<std::is_unsigned<T>::value,T>::type
constexpr shl(const T n, const S i){
if ((i<digits<T>())&&(i>=0)) return n<<i;
return 0;
}
template <typename T,typename S>
typename std::enable_if<std::is_unsigned<T>::value,T>::type
constexpr shr(const T n, const S i){
if ((i<digits<T>())&&(i>=0)) return n>>i;
return 0;
}
template <typename T,typename S>
typename std::enable_if<std::is_unsigned<T>::value,T>::type
constexpr ror(const T n, const S i){
const T m = (std::numeric_limits<T>::digits-1);
const T c = i&m;
return (n>>c)|(n<<((-c)&m));
}
template <typename T,typename S>
typename std::enable_if<std::is_unsigned<T>::value,T>::type
constexpr rol(const T n, const S i){
const T m = (std::numeric_limits<T>::digits-1);
const T c = i&m;
return (n<<c)|(n>>((-c)&m));
}
template<typename test, template<typename...> class ref>
struct is_specialization : std::false_type {};
template<template<typename...> class ref, typename... args>
struct is_specialization<ref<args...>, ref>: std::true_type {};
template<typename T>
typename std::enable_if<std::is_unsigned<T>::value,T>::type
constexpr modular_inverse(const T& a) {
T x = a&1u;
for (size_t i(1);i!=digits<T>();++i) x*=T(2u)-a*x;
return x;
}
template <template <class...> class trait, class always_void, class... args>
struct detector : std::false_type {};
template <template <class...> class trait,class... args>
struct detector<trait,void_t<trait<args...>>,args...> : std::true_type {};
template<class hash,class hash_type>
using unhash_method_t = decltype(declval<hash>().unhash(hash_type{}));
template<class hash,class hash_type>
using unhash_defined =
typename detector<unhash_method_t,void,hash,hash_type>::type;
template <class hash,class hash_type, typename = int>
struct is_injective : unhash_defined<hash,hash_type>{};
//template <typename T>
//struct is_injective <T, decltype((void) T::is_injective, 0)>
//: std::true_type {}; // TODO why does this not override previous def
template<typename T,class enable = void>
class hash;
template<>
class hash<uint8_t,void>{
private:
const uint8_t a = 97u;
const uint8_t i = modular_inverse(a);
const uint8_t b = 111u;
public:
typedef typename true_type::type is_injective;
typedef typename true_type::type unhash_defined;
constexpr size_t digits() const {
return whash::digits<uint8_t>();
}
constexpr uint8_t operator()(const uint8_t v) const {
return (v+b)*a;
}
constexpr uint8_t unhash(const uint8_t v) const {
return v*i-b;
}
};
template<>
class hash<uint16_t,void>{
private:
const uint16_t a = 43581u;
const uint16_t i = modular_inverse(a);
const uint16_t b = 36690u;
public:
typedef typename true_type::type is_injective;
typedef typename true_type::type unhash_defined;
constexpr size_t digits() const {
return whash::digits<uint16_t>();
}
constexpr uint16_t operator()(const uint16_t v) const {
return (v+b)*a;
}
constexpr uint16_t unhash(const uint16_t v) const {
return v*i-b;
}
};
template<>
class hash<uint32_t,void>{
private:
const uint32_t p = 0x55555555ul;
const uint32_t a = 3370923577ul;
public:
typedef typename true_type::type is_injective;
typedef typename true_type::type unhash_defined;
constexpr size_t digits() const {
return whash::digits<uint32_t>();
}
constexpr uint32_t operator()(uint32_t v) const {
v^= v>>16;
v*= p;
v^= v>>16;
v*= a;
return v;
}
constexpr uint32_t unhash(uint32_t v) const {
v*= modular_inverse(a);
v^= v>>16;
v*= modular_inverse(p);
v^= v>>16;
return v;
}
};
template<>
class hash<uint64_t,void>{
private:
const uint64_t p = 0x5555555555555555ull;
const uint64_t a = 15864664792644967873ull;
public:
typedef typename true_type::type is_injective;
typedef typename true_type::type unhash_defined;
constexpr size_t digits() const {
return whash::digits<uint64_t>();
}
uint64_t constexpr operator()(uint64_t v) const {
v^= v>>32;
v*= p;
v^= v>>32;
v*= a;
return v;
}
uint64_t constexpr unhash(uint64_t v) const {
v*= modular_inverse(a);
v^= v>>32;
v*= modular_inverse(p);
v^= v>>32;
return v;
}
};
template<typename P>
class hash<P,
typename enable_if<is_pointer<P>::value>::type
>
{
public:
typedef typename true_type::type is_injective;
typedef typename true_type::type unhash_defined;
uintptr_t constexpr operator()(const P p) const {
return hash<uintptr_t,void>{}(reinterpret_cast<uintptr_t>(p));
}
P constexpr unhash(const uintptr_t p) const {
return reinterpret_cast<P>(hash<uintptr_t,void>{}.unhash(p));
}
};
template<typename S>
class hash<S,
typename enable_if<is_integral<S>::value&&is_signed<S>::value>::type>
{
private:
using U = typename make_unsigned<S>::type;
const hash<typename remove_const<U>::type> hasher{};
public:
typedef typename true_type::type is_injective;
typedef typename true_type::type unhash_defined;
constexpr size_t digits() const {
return hasher.digits();
}
constexpr U operator()(const S& v) const {
return hasher(U(v));
}
constexpr S unhash(const U& v) const {
return S(hasher.unhash(v));
}
};
template<typename T>
class hash<T,typename enable_if<
(is_fundamental<T>::value)&&(!is_integral<T>::value)
>::type>
{
public:
typedef typename false_type::type is_injective;
typedef typename false_type::type unhash_defined;
constexpr size_t digits() const {
return whash::digits<size_t>();
}
const inline size_t operator()(const T& v) const {
size_t h = 0;
for (size_t i=0;i!=sizeof(T);++i) {
h^=*(reinterpret_cast<const char*>(&v)+i);
rol(h,whash::digits<char>());
if (((i%sizeof(size_t))==(sizeof(size_t)-1))||(i==(sizeof(T)-1))) {
h = hash<size_t>{}(h);
}
//cerr << "# " << h << endl;
}
return h;
}
};
template<>
class hash<string>
{
public:
typedef typename false_type::type is_injective;
typedef typename false_type::type unhash_defined;
constexpr size_t digits() {
return whash::digits<size_t>();
}
const inline size_t operator()(const string& s) const {
return std::hash<string>{}(s);
}
};
template<typename T>
class hash<
T,
typename enable_if<
is_specialization<
typename remove_const<T>::type,
tuple
>::value
>::type
>
{
private:
template<size_t i = 0>
const size_t impl(const T& v,const size_t& h=0u)
const
{
if constexpr (i==tuple_size<T>::value) {
return h;
} else {
const auto e = get<i>(v);
size_t t =
hash<size_t>{}(hash<typename remove_const<decltype(e)>::type>{}(e));
return impl<i+1>(v,h^(size_t(2u*i+1u)*t));
}
}
public:
typedef typename false_type::type is_injective;
typedef typename false_type::type unhash_defined;
constexpr size_t digits() const {
return whash::digits<size_t>();
}
const inline size_t operator()(const T& v) const {
return impl(v);
}
};
template<typename T>
class hash<
T,
typename enable_if<
is_specialization<typename remove_const<T>::type,vector>::value
>::type
>
{
public:
typedef typename false_type::type is_injective;
typedef typename false_type::type unhash_defined;
constexpr size_t digits() const {
return whash::digits<size_t>();
}
const inline size_t operator()(const T& v) const {
size_t h = 0;
for (size_t i=0;i!=v.size();++i) {
auto e = v[i];
h^= size_t(2u*i+1u)*hash<decltype(e)>{}(e);
}
return h;
}
};
template<
class key_type,
class mapped_type,
class hash = hash<key_type>,
class equal = std::equal_to<key_type>,
class comp = typename conditional<
is_injective<hash,typename invoke_result<hash,key_type&>::type>::value,
dummy_comp<key_type>,
std::less<key_type>>::type,
class alloc = typename std::allocator
<
tuple
<
typename conditional<
unhash_defined<
hash,
typename invoke_result<hash,key_type&>::type
>::value,
typename invoke_result<hash,key_type&>::type,
key_type
>::type,
typename conditional
<
std::is_same<mapped_type,void>::value,
std::true_type,
mapped_type
>::type
>
>
>
class patchmap{
public:
typedef alloc allocator_type;
typedef typename alloc::value_type value_type;
typedef typename alloc::pointer value_pointer;
typedef typename alloc::reference reference;
typedef typename alloc::const_reference const_reference;
typedef typename alloc::difference_type difference_type;
typedef typename alloc::size_type size_type;
typedef typename std::invoke_result<hash,key_type&>::type hash_type;
typedef typename conditional<is_same<mapped_type,void>::value,
std::true_type,
mapped_type>::type
_mapped_type;
private:
template
<
size_type resize_nom ,size_type resize_denom,
size_type nextsize_nom,size_type nextsize_denom
>
struct patchmap_sizing_policy{
const size_type& num_data;
const size_type& datasize;
patchmap_sizing_policy(
const size_type& num_data,const size_type& datasize
) : num_data(num_data), datasize(datasize) {}
constexpr size_type nextsize() const {
size_t nextsize = (nextsize_nom*datasize+nextsize_denom)
/nextsize_denom;
nextsize = (nextsize+digits<size_type>()-1)/digits<size_type>();
nextsize*= digits<size_type>();
return nextsize;
}
constexpr bool is_sufficient() const {
return ((num_data*resize_nom)<(datasize*resize_denom));
}
};
using desperate_patchmap_sizing_policy = // desperately save memory
patchmap_sizing_policy<32,31,107,89>;
using aggressive_patchmap_sizing_policy = // agressive resizing for speed
patchmap_sizing_policy<5,4,12,7>;
using default_patchmap_sizing_policy = // best trade-off
patchmap_sizing_policy<7,6,53,32>;
using sizing_policy = default_patchmap_sizing_policy;
size_type num_data;
size_type datasize;
size_type masksize;
value_type * data;
size_type * mask;
allocator_type allocator;
std::allocator<size_type> maskallocator;
comp comparator;
equal equator;
hash hasher;
using uphold_iterator_validity = true_type;
// size_type const inline masksize() const {
// return (datasize+digits<size_type>()-1)/digits<size_type>();
//}
template<typename T>
const key_type& key_of(T&& value) const {
if constexpr (is_same<void,mapped_type>::value) return value;
else return get<0>(value);
}
template<typename T>
const _mapped_type& mapped_of(T&& value) const {
if constexpr (is_same<void,mapped_type>::value) return std::true_type();
else return get<1>(value);
}
size_type inline map(
const hash_type& h,
const hash_type& n
) const {
return get<0>(long_mul(h,n));
}
size_type inline map(const hash_type& h) const {
return map(h,datasize);
}
size_type inline map_diff(
const hash_type& h0,
const hash_type& h1,
const hash_type& n
) const {
const auto lm = long_mul(hash_type(h0-h1),n);
return get<0>(lm);
}
size_type inline map_diff(
const hash_type& h0,
const hash_type& h1
) const {
return map_diff(h0,h1,datasize);
}
size_type inline map_diff_round(
const hash_type& h0,
const hash_type& h1,
const hash_type& n
) const {
const auto lm = long_mul(hash_type(h0-h1),n);
return get<0>(lm)+(get<1>(lm)>((~hash_type(0))>>1));
}
size_type inline map_diff_round(
const hash_type& h0,
const hash_type& h1
) const {
return map_diff_round(h0,h1,datasize);
}
hash_type inline order(const key_type& k) const {
return hasher(k);
}
template<class other_type>
bool inline is_equal(
const pair<key_type,hash_type> a,
const other_type b) const {
if constexpr (unhash_defined<hash,hash_type>::value) {
return get<1>(a) == b;
} else {
return equator(get<0>(a),b);
}
}
bool inline is_less(
const key_type& a,
const key_type& b,
const hash_type& oa,
const hash_type& ob
) const {
if constexpr (is_injective<hash,hash_type>::value){
assert(equator(a,b)==(oa==ob));
if (oa<ob) return true;
else return false;
} else {
if (oa<ob) return true;
if (oa>ob) return false;
return comparator(a,b);
}
}
bool inline is_less(
const key_type& a,
const key_type& b,
const hash_type& oa
) const {
return is_less(a,b,oa,order(b));
}
bool inline is_less(const key_type& a,const key_type& b) const {
return is_less(a,b,order(a),order(b));
}
bool inline is_more(
const key_type& a,
const key_type& b,
const hash_type& oa,
const hash_type& ob
) const {
if constexpr (is_injective<hash,hash_type>::value){
assert(equator(a,b)==(oa==ob));
if (oa>ob) return true;
else return false;
} else {
if (oa>ob) return true;
if (oa<ob) return false;
return !((comparator(a,b))||(equator(a,b)));
}
}
bool inline is_more(
const key_type& a,
const key_type& b,
const hash_type& oa
) const {
return is_more(a,b,oa,order(b));
}
bool inline is_more(
const key_type& a,
const key_type& b
) const {
return is_more(a,b,order(a),order(b));
}
bool inline is_set(const size_type& n) const {
const size_type i = n/digits<size_type>();
const size_type j = n%digits<size_type>();
assert(i<masksize);
return (mask[i]&(size_type(1)<<(digits<size_type>()-j-1)));
}
bool inline is_set_any(
const size_type& lo,
const size_type& hi) const {
const size_type k0 = lo/digits<size_type>();
const size_type l0 = lo%digits<size_type>();
const size_type m0 = (~size_type(0))>>l0;
const size_type k1 = hi/digits<size_type>();
const size_type l1 = hi%digits<size_type>();
const size_type m1 = (~size_type(0))<<(digits<size_type>()-l1-1);
if (k0==k1) return ((m0&m1&mask[k0])!=0);
if (((m0&mask[k0])!=0)||((m1&mask[k1])!=0)) return true;
for (size_type i = k0+1;i!=k1;++i)
if (mask[i]!=0) return true;
return false;
}
void inline set(const size_type& n) {
const size_type i = n/digits<size_type>();
const size_type j = n%digits<size_type>();
mask[i]|=size_type(1)<<(digits<size_type>()-j-1);
}
void inline unset(const size_type& n) {
const size_type i = n/digits<size_type>();
const size_type j = n%digits<size_type>();
mask[i]&=((~size_type(0))^(size_type(1)<<(digits<size_type>()-j-1)));
}
void inline swap_set(const size_type& i,const size_type& j){
if (is_set(i)==is_set(j)) return;
if (is_set(i)){
set(j);
unset(i);
}else{
set(i);
unset(j);
}
}
/*hash_type inline index(const size_type& i) const {
assert(i<datasize);
if (is_set(i)) return order(get<0>(data[i]));
else return hash_type(i)*inversed;
}*/
bool inline index_key_is_less(const size_type& i,const key_type& k) const{
if (is_set(i)) {
if constexpr (unhash_defined<hash,hash_type>::value) {
return is_less(unhash(get<0>(data[i])),k,get<0>(data[i]),order(k));
} else {
return is_less(get<0>(data[i]),k);
}
}
return i<map(order(k));
}
bool inline key_index_is_less(const key_type& k,const size_type& i) const{
if (is_set(i)) {
if constexpr (unhash_defined<hash,hash_type>::value) {
return is_less(k,hasher.unhash(get<0>(data[i])),
order(k),get<0>(data[i]));
} else {
return is_less(k,get<0>(data[i]));
}
}
return map(order(k))<i;
}
bool inline index_index_is_less(const size_type& i,const size_type& j)
const {
assert(i<datasize);
assert(j<datasize);
if (is_set(i)&&is_set(j)) {
if constexpr (unhash_defined<hash,hash_type>::value) {
return get<0>(data[i]) < get<0>(data[j]);
} else {
return is_less(get<0>(data[i]),get<0>(data[j]));
}
}
if (is_set(i)) {
if constexpr (unhash_defined<hash,hash_type>::value) {
return map(get<0>(data[i]))<j;
} else {
return map(order(get<0>(data[i])))<j;
}
}
if (is_set(j)) {
if constexpr (unhash_defined<hash,hash_type>::value) {
return i<map(get<0>(data[j]));
} else {
return i<map(order(get<0>(data[j])));
}
}
return i<j;
}
bool inline index_index_is_more(const size_type& i,const size_type& j)
const {
return index_index_is_less(j,i);
}
size_type inline find_first() const {
size_type i=0;
if (i>=datasize) return ~size_type(0);
while(true){
const size_type k = i/digits<size_type>();
const size_type l = i%digits<size_type>();
const size_type m = (~size_type(0))>>l;
assert(k<masksize);
size_type p = (mask[k]&m)<<l;
if (k+1<masksize)
p|=shr(mask[k+1]&(~m),digits<size_type>()-l);
const size_type s = clz(p);
if (s==0) return i;
i+=s;
if (i>=datasize) return ~size_type(0);
}
}
// search for free bucket in decreasing order
size_type inline search_free_dec(size_type i) const {
while(true){
const size_type k = i/digits<size_type>();
const size_type l = i%digits<size_type>();
const size_type m = (~size_type(0))<<(digits<size_type>()-l-1);
assert(k<masksize);
size_type p = ((~(mask[k]&m))>>(digits<size_type>()-l-1));
if (k!=0) p|=shl(~(mask[k-1]&(~m)),l+1);
const size_type s = ctz(p);
if (s==0){
assert(!is_set(i));
return i;
}
i-=s;
if (i>=datasize) return ~size_type(0);
}
}
// search for free bucket in increasing order
size_type inline search_free_inc(size_type i) const {
while(true){
const size_type k = i/digits<size_type>();
const size_type l = i%digits<size_type>();
const size_type m = (~size_type(0))>>l;
assert(k<masksize);
size_type p = (~(mask[k]&m))<<l;
if (k+1<masksize) p|=shr(~(mask[k+1]&(~m)),digits<size_type>()-l);
const size_type s = clz(p);
if (s==0){
assert(!is_set(i));
return i;
}
i+=s;
if (i>=datasize) return ~size_type(0);
}
}
// search for free bucket bidirectional
size_type inline search_free_bidir_v0(size_type i) const {
const size_type k = search_free_inc(i);
const size_type l = search_free_dec(i);
assert((k<datasize)||(l<datasize));
if (k>=datasize) i=l;
else if (l>=datasize) i=k;
else if (k-i<i-l) i=k;
else i=l;
return i;
}
// search for free bucket truly bidirectional
// this is optimal vor very high load factors > 0.98
size_type inline search_free_bidir(const size_type& n) const {
size_type i = n, j = n, si=~size_type(0),sj=~size_type(0);
while(true){
if ((i!=~size_type(0))&&si){
const size_type k = i/digits<size_type>();
const size_type l = i%digits<size_type>();
const size_type m = (~size_type(0))>>l;
size_type p = (~(mask[k]&m))<<l;
if (k+1<masksize) p|=shr(~(mask[k+1]&(~m)),digits<size_type>()-l);
si= clz(p);
}
if (si==0){
if (j==~size_type(0)) return i;
if (i-n+digits<size_type>()<=n-j) return i;
} else {
i+=si;
if (i>=datasize) i=~size_type(0);
if ((i&j)==~size_type(0)) return ~size_type(0);
}
if ((j!=~size_type(0))&&sj){
const size_type k = j/digits<size_type>();
const size_type l = j%digits<size_type>();
const size_type m = (~size_type(0))<<(digits<size_type>()-l-1);
size_type p = ((~(mask[k]&m))>>(digits<size_type>()-l-1));
if (k!=0) p|=shl(~(mask[k-1]&(~m)),l+1);
sj= ctz(p);
}
if (sj==0) {
if (i==~size_type(0)) return j;
if (n-j+digits<size_type>()<=i-n) return j;
} else {
j-=sj;
if (j>=datasize) j=~size_type(0);
if ((i&j)==~size_type(0)) return ~size_type(0);
}
if ((si==0)&&(sj==0)){
if (i-n<=n-j) return i;
else return j;
}
}
return ~size_type(0);
}
size_type const inline reserve_node(
const key_type& k,
const hash_type& ok,
const size_type& mok
){
if (!is_set(mok)) {
set(mok);
++num_data;
return mok;
}
const size_type j = search_free_bidir_v0(mok);
assert(j<datasize);
assert(!is_set(j));
set(j);
++num_data;
size_type i = j;
while(true){
if (i==0) break;
if (!is_set(i-1)) break;
if constexpr (unhash_defined<hash,hash_type>::value) {
if (get<0>(data[i-1])<ok) break;
} else {
if (is_less(get<0>(data[i-1]),k,order(get<0>(data[i-1])),ok)) break;
}
swap(data[i],data[i-1]);
--i;
}
if (i!=j) return i;
while(true){
if (i+1>=datasize) break;
if (!is_set(i+1)) break;
if constexpr (unhash_defined<hash,hash_type>::value) {
if (get<0>(data[i+1])>ok) break;
} else {
if (is_less(k,get<0>(data[i+1]),ok,order(get<0>(data[i+1])))) break;
}
swap(data[i],data[i+1]);
++i;
}
return i;
}
size_type inline reserve_node(
const key_type& k,
const hash_type& ok) {
const hash_type mok = map(ok);
return reserve_node(k,ok,mok);
}
size_type inline reserve_node(const key_type& k){
const hash_type ok = order( k);
const size_type mok = map(ok);
assert(mok<datasize);
return reserve_node(k,ok,mok);
}
size_type inline interpol(
const hash_type& ok,
const hash_type& olo,
const hash_type& ohi,
const size_type& lo,
const size_type& hi
) const {
auto lm = long_mul(size_type(ok)-size_type(olo),hi-lo);
// this is theoretically better but not worth the time
//const hash_type tmp = get<1>(lm)+(ohi-olo)/2;
//if (tmp<get<1>(lm)) ++get<0>(lm);
//get<1>(lm) = tmp;
const size_type n = clz(get<0>(lm));
const size_type m = digits<size_type>()-n;
const hash_type den = (size_type(ohi)-size_type(olo))>>m;
const hash_type nom = (get<0>(lm)<<n)+(get<1>(lm)>>m);
return lo+nom/den;
}
size_type inline find_node_interpol(
const key_type& k,
const hash_type& ok,
const size_type& mok,
size_type lo,
hash_type olo,
bool is_set_lo,
size_type hi,
size_type ohi,
bool is_set_hi
) const {
assert(lo<=hi||datasize==0);
size_type mi;
//for (size_t i=0;;++i) {
while(true) {
if (hi-lo<8) {
if (hi-lo<4) {
if (hi-lo<2) {
if (hi-lo<1) {
if (is_set(lo))
if (is_equal({k,ok},get<0>(data[lo]))) return lo;
break;
} else {
if (is_set_lo&&is_set_hi) return ~size_type(0);
if (is_set(lo))