-
Notifications
You must be signed in to change notification settings - Fork 3
/
sparse_patchmap.hpp
1410 lines (1392 loc) · 49 KB
/
sparse_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 SPARSE_PATCHMAP_H
#define SPARSE_PATCHMAP_H
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cstdint>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <stdexcept>
#include <boost/container/allocator.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <typeinfo>
#include <exception>
#include <memory>
#include "wmath_forward.hpp"
#include "wmath_bits.hpp"
#include "wmath_hash.hpp"
#include "wmath_math.hpp"
#include "hashed_array_tree.hpp"
#ifdef PATCHMAP_STAT
size_t recursion_depth;
size_t shift_count;
#endif
namespace{
template<class T>
double frac(const T& n){
return n*pow(0.5,std::numeric_limits<T>::digits);
}
}
namespace wmath{
using std::allocator_traits;
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;}
};
struct empty{
};
template<class key_type = int, // int is the default, why not
class mapped_type = int, // int is the default, why not
class hash = hash_functor<key_type>,
class equal = std::equal_to<key_type>,
class comp = typename conditional<is_injective<hash>::value,
dummy_comp<key_type>,
std::less<key_type>>::type,
class alloc = typename std::allocator<
typename conditional<
std::is_same<mapped_type,void>::value,
std::pair<key_type,empty>,
std::pair<key_type,mapped_type>
>::type>,
/*class alloc = typename boost::container::allocator<
typename conditional<
std::is_same<mapped_type,void>::value,
std::pair<key_type,empty>,
std::pair<key_type,mapped_type>
>::type,2>,*/
bool dynamic = true
>
class sparse_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::result_of<hash(key_type)>::type hash_type;
typedef typename conditional<is_same<mapped_type,void>::value,
key_type,
mapped_type>::type
_mapped_type;
private:
size_type num_data = 0;
size_type datasize = 0;
size_type masksize = 0;
allocator_type allocator;
boost::container::allocator<size_type,2> maskallocator;
hashed_array_tree<value_type,alloc> data;
hashed_array_tree< size_type,boost::container::allocator<size_type,2>>
mask;
comp comparator;
equal equator;
hash hasher;
using uphold_iterator_validity = true_type;
/* TODO
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 value.first;
}
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(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(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 distribute(hasher(k));
}
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>::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>::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);
}
}
bool inline index_key_is_less(const size_type& i,const key_type& k) const{
if (is_set(i)) return is_less(data[i].first,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)) return is_less(k,data[i].first);
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)) return is_less(data[i].first,data[j].first);
if (is_set(i)) return map(order(data[i].first))<j;
if (is_set(j)) return i<map(order(data[j].first));
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& key,
const size_type& mok,
const hash_type& ok
){
#ifdef PATCHMAP_STAT
shift_count = 0;
#endif
assert(mok<datasize);
assert(map(order(key))==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);
//data[i].first = key;
++num_data;
size_type i = j;
while(true){
if (i==0) break;
if (!is_set(i-1)) break;
if (is_less(data[i-1].first,key,order(data[i-1].first),ok)) break;
swap(data[i],data[i-1]);
--i;
}
#ifdef PATCHMAP_STAT
shift_count = j-i;
#endif
if (i!=j) return i;
while(true){
if (i+1>=datasize) break;
if (!is_set(i+1)) break;
if (is_less(key,data[i+1].first,ok,order(data[i+1].first))) break;
swap(data[i],data[i+1]);
++i;
}
#ifdef PATCHMAP_STAT
shift_count = i-j;
#endif
return i;
}
size_type inline reserve_node(
const key_type& key,
const size_type& hint){
const hash_type ok = order(key);
return reserve_node(key,hint,ok);
}
size_type inline reserve_node(const key_type& key){
const hash_type ok = order(key);
const size_type hint = map(ok);
assert(hint<datasize);
return reserve_node(key,hint,ok);
}
size_type inline find_node_binary(
const key_type& key,
const hash_type& ok,
const size_type& lo, // inclusive bounds
const size_type& hi // inclusive bounds
) const {
#ifdef PATCHMAP_STAT
++recursion_depth;
#endif
assert(lo<datasize);
assert(hi<datasize);
assert(lo<=hi);
const size_type mi = (hi+lo)/2;
if (is_set(mi)) if (equator(data[mi].first,key)) return mi;
if constexpr (is_injective<hash>::value) {
if (index_key_is_less(mi,key)){
if (mi<hi) return find_node_binary(key,ok,mi+1,hi);
else return ~size_type(0);
} else {
if (mi>lo) return find_node_binary(key,ok,lo,mi-1);
else return ~size_type(0);
}
} else {
if (index_key_is_less(mi,key)){
if (mi<hi) return find_node_binary(key,ok,mi+1,hi);
else return ~size_type(0);
}
if (key_index_is_less(key,mi)){
if (mi>lo) return find_node_binary(key,ok,lo,mi-1);
else return ~size_type(0);
}
}
}
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-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<hash_type>()-n;
const hash_type den = (ohi-olo)>>m;
const hash_type nom = (get<0>(lm)<<n)+(get<1>(lm)>>m);
return lo+nom/den;
}
size_type inline find_node_linear(
const key_type& k,
const size_type& lo,
const size_type& hi) const {
size_type i = lo;
while(true){
if (is_set(i)) if (equator(data[i].first,k)) return i;
if (i==hi) return ~size_type(0);
++i;
}
}
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;
while(true) {
if (!(lo<hi)) return ~size_type(0);
#ifdef PATCHMAP_STAT
++recursion_depth;
#endif
if (hi-lo<2) {
if (is_set_lo&&is_set_hi) return ~size_type(0);
if (is_set(lo)) if (equator(k,data[lo].first)) return lo;
if (is_set(hi)) if (equator(k,data[hi].first)) return hi;
return ~size_type(0);
}
if (hi-lo<8) {
if (is_set_hi && is_set_lo) {
mi = lo + ((hi-lo)>>1);
} else if (is_set_lo) {
mi = lo + ((hi-lo+2)>>2);
} else if (is_set_hi) {
mi = hi - ((hi-lo+2)>>2);
} else {
return ~size_type(0);
}
} else {
if (is_set_hi && is_set_lo) {
mi = interpol(ok,olo,ohi,lo,hi);
} else if (is_set_lo) {
const size_type st = map_diff(ok,olo);
mi = lo+st<hi?lo+st:hi;
} else if (is_set_hi) {
const size_type st = map_diff(ohi,ok);
mi = lo+st<hi?hi-st:lo;
} else {
return ~size_type(0);
}
mi = clip(mi,lo+1,hi-1);
}
if (!is_set(mi)) {
if (mi<mok) {
lo = mi;
is_set_lo=false;
continue;
}
if (mi>mok) {
hi = mi;
is_set_hi=false;
continue;
}
return ~size_type(0);
}
if (equator(k,data[mi].first)) return mi;
const hash_type omi = order(data[mi].first);
if (ok<omi) {
hi = mi;
ohi = omi;
is_set_hi = true;
continue;
}
if (ok>omi) {
lo = mi;
olo = omi;
is_set_lo = true;
continue;
}
if constexpr (is_injective<hash>::value) {
return ~size_type(0);
} else {
if (k<data[mi].first) {
hi = mi;
ohi = omi;
is_set_hi = true;
continue;
}
if (k>data[mi].first) {
lo = mi;
olo = omi;
is_set_lo = true;
continue;
}
}
return ~size_type(0);
}
return ~size_t(0);
}
size_type inline find_node(
const key_type & k,
const hash_type& ok,
const size_type& mok)
const {
#ifdef PATCHMAP_STAT
recursion_depth=0;
#endif
assert((mok<datasize)||(datasize==0));
if (datasize==0) return ~size_type(0);
if (!is_set(mok)) return ~size_type(0);
if (equator(data[mok].first,k)) return mok;
const hash_type omi = order(data[mok].first);
if (omi<ok) {
return find_node_interpol(k,ok,mok,
mok ,omi ,true ,
datasize-1,~size_type(0),false);
} else {
return find_node_interpol(k,ok,mok,
0 ,0 ,false,
mok , omi,true );
}
}
size_type const inline find_node(
const key_type& k,
const size_type& ok
) const { return find_node(k,ok,map(ok)); }
size_type const inline find_node(const key_type& k)
const { return find_node(k,order(k)); }
size_type const inline find_node_bruteforce(const key_type& k) const {
for (size_type i = 0; i!=datasize; ++i)
if (is_set(i)) if (equator(data[i].first,k)) return i;
return ~size_type(0);
}
void inline const restore_order() { // insertion sort
for (size_type i=0;i!=datasize;++i){
for(size_type j=i;j!=0;--j){
if (index_index_is_less(j-1,j)) break;
swap_set(j,j-1);
swap(data[j],data[j-1]);
}
}
}
template<typename map_type>
typename conditional<is_const<map_type>::value,
const _mapped_type&,
_mapped_type&>::type
static inline const_noconst_at(map_type& hashmap,const key_type& k) {
size_type i = hashmap.find_node(k);
if (i<hashmap.datasize){
assert(hashmap.is_set(i));
if constexpr (is_same<mapped_type,void>::value)
return hashmap.data[i].first;
else return
hashmap.data[i].second;
} else throw std::out_of_range(
std::string(typeid(hashmap).name())
+".const_noconst_at("+typeid(k).name()+" k)"
+"key not found, array index "
+to_string(i)+" out of bounds"
);
}
public:
void print() const {
cerr << datasize << " " << num_data << endl;
for (size_type i=0;i!=datasize;++i) {
cout << std::fixed << std::setprecision(16);
const size_type ok = order(data[i].first);
const size_type mok = map(ok);
if (is_set(i)) cout << setw(6) << i;
else cout << " " ;
cout << setw(20) << frac(uint32_t(ok))
<< setw(20) << frac(uint32_t(data[i].second));
if (is_set(i)) cout << setw( 8) << mok
<< setw( 8) << int(mok)-int(i);
else cout << setw( 8) << i
<< setw( 8) << 0;
cout << endl;
}
cout << endl;
}
size_type erase(
const key_type& k,
const hash_type& ok,
const size_type& hint){
size_type i = find_node(k,ok,hint);
if (i>=datasize) return 0;
//cout << "erasing " << wmath::frac(ok) << endl;
//cout << "found at " << i << endl;
const size_type j = i;
while(true){
if (i+1==datasize) break;
if (!is_set(i+1)) break;
if (map(order(data[i+1].first))>i) break;
swap(data[i],data[i+1]);
++i;
}
if (i==j){
while(true){
if (i==0) break;
if (!is_set(i-1)) break;
if (map(order(data[i-1].first))<i) break;
swap(data[i],data[i-1]);
--i;
}
}
unset(i);
//cout << "unset position " << i << endl;
//cout << k << " " << data[i].first << endl;
--num_data;
//check_ordering();
//cout << num_data << endl;
assert(num_data<datasize);
return 1;
}
size_type erase(
const key_type& k,
const size_type& ok
){
const hash_type hint = map(ok);
return erase(k,ok,hint);
}
size_type erase(const key_type& k){
const size_type ok = order(k);
return erase(k,ok);
}
void inline clear(){
for (size_type i=0;i!=masksize;++i) mask[i]=0;
num_data=0;
}
void const resize(const size_type& n){
//cout << "resizing patchmap " << num_data << endl;
if (n <num_data) return resize(num_data);
if (n==datasize) return;
const size_type new_datasize = n;
const size_type new_masksize =
(new_datasize+digits<size_type>()-1)/digits<size_type>();
if (n>datasize) {
data.resize(new_datasize);
mask.resize(new_masksize);
const size_type old_masksize = masksize;
masksize = new_masksize;
const size_type old_datasize = datasize;
datasize = new_datasize;
for (size_type i=old_masksize;i!=new_masksize;++i) mask[i]=0;
num_data = 0;
for (size_type n=old_datasize;n!=~size_type(0);--n) {
const size_type i = n/digits<size_type>();
const size_type j = n%digits<size_type>();
if (mask[i]&(size_type(1)<<(digits<size_type>()-j-1))) {
const value_type tmp = move(data[n]);
unset(n);
const size_type l = reserve_node(tmp.first);
data[l] = move(tmp);
set(l);
}
}
} else {
const size_type old_datasize = datasize;
datasize = new_datasize;
masksize = new_masksize;
num_data = 0;
for (size_type n=0;n!=old_datasize;++n) {
const size_type i = n/digits<size_type>();
const size_type j = n%digits<size_type>();
if (mask[i]&(size_type(1)<<(digits<size_type>()-j-1))) {
const value_type tmp = move(data[n]);
unset(n);
const size_type l = reserve_node(tmp.first);
data[l] = move(tmp);
set(l);
}
}
data.resize(datasize);
mask.resize(masksize);
}
}
size_type inline size() const { return num_data; }
size_type const test_size() const {
size_type test = 0;
for (size_type i=0;i!=datasize;++i) test += is_set(i);
return test;
}
void test_chunks() const {
for (size_type i=0;i!=masksize;++i){
cout << popcount(mask[i]) << endl;
}
}
bool check_ordering() const {
bool ordered = true;
for (size_type i=0,j=1;j<datasize;(++i,++j)){
if (!index_index_is_less(j,i)) continue;
cout << std::fixed << std::setprecision(16)
<< is_set(i) << " " << is_set(j) << " "
<< i << " " << j << " "
<< data[i].first << " " << data[j].first << " "
<< order(data[i].first) << " " << order(data[j].first) << endl;
//cout << index(i) << " " << index(j) << endl;
/*cout << double(index(i))
/pow(2.0,double(CHAR_BIT*sizeof(hash_type))) << " "
<< double(index(j))
/pow(2.0,double(CHAR_BIT*sizeof(hash_type))) << endl;*/
ordered = false;
}
return ordered;
}
bool check_ordering(const size_type& i) const {
if ( i>0 ) if (!index_index_is_less(i-1,i)) return false;
if (i+1<datasize) if (!index_index_is_less(i,i+1)) return false;
return true;
}
void inline ensure_size(){
if constexpr (!dynamic) return;
if (num_data*32<datasize*31) return;
//const size_type l2 = log2(datasize+1);
//if ( (128*31+l2*l2*32)*num_data < (128+l2*l2)*31*datasize ) return;
//if ( (128*15+l2*l2*16)*num_data < (128+l2*l2)*15*datasize ) return;
//if ( (128*7+l2*l2*8)*num_data < (128+l2*l2)*7*datasize ) return;
size_type nextsize;
if (datasize == 0) {
nextsize = digits<size_type>();
} else {
//nextsize = 50*datasize/31;
//nextsize = 48*datasize/31;
//nextsize = 47*datasize/31;
//nextsize = 47*datasize/37;
//nextsize = 53*datasize/41;
//nextsize = (113*datasize+44)/89;
nextsize = (107*datasize+89)/89;
nextsize = (nextsize+digits<size_type>()-1)/digits<size_type>();
nextsize = mask.next_size(nextsize);
nextsize*= digits<size_type>();
}
resize(nextsize);
}
_mapped_type& operator[](const key_type& k){
const size_type i = find_node(k);
if (i<datasize) {
if constexpr (is_same<mapped_type,void>::value) return data[i].first;
else return data[i].second;
}
//assert(find_node_bruteforce(k)==~size_type(0));
ensure_size();
//assert(check_ordering());
const size_type j = reserve_node(k);
if constexpr (is_same<void,mapped_type>::value) {
allocator_traits<alloc>::construct(
allocator,&data[j],k,wmath::empty());
} else {
allocator_traits<alloc>::construct(
allocator,&data[j],k,mapped_type());
}
//assert(check_ordering());
//assert(find_node_bruteforce(k)==j);
//assert(find_node(k)==j);
assert(check_ordering(j));
if constexpr (is_same<mapped_type,void>::value) return data[i].first;
else return data[j].second;
}
const _mapped_type& operator[](const key_type& k) const {
const size_type i = find_node(k);
assert(i<datasize);
if constexpr (is_same<void,mapped_type>::value) return data[i].first;
return data[i].second; // this is only valid if key exists!
}
_mapped_type& at(const key_type& k){
return const_noconst_at(*this,k);
}
const _mapped_type& at(const key_type& k) const {
return const_noconst_at(*this,k);
}
size_type const inline count(const key_type& k) const {
//assert(check_ordering());
//assert(find_node(k)==find_node_bruteforce(k));
return (find_node(k)<datasize);
}
double average_offset(){
double v = 0;
for (size_type i=0;i!=datasize;++i){
if (is_set(i)){
v+=double(map(data[i].first))-double(i);
cout << map(order(data[i].first)) << " " << i << " "
<< datasize << endl;
}
}
return v/size()/datasize;
}
void print_offsets(){
for (size_type i=0;i!=datasize;++i){
if (is_set(i)) cout << map(order(data[i].first)) << " " << i << endl;
//else cout << i << " " << i << endl;
}
}
void print_offsethist(){
sparse_patchmap<int,size_t> hist;
for (size_type i=0;i!=datasize;++i)
++hist[int(map(order(data[i].first)))-int(i)];
for (auto it=hist.begin();it!=hist.end();++it)
cout << it->first << " " << it->second << endl;
}
equal key_eq() const{ // get key equivalence predicate
return equal{};
}
comp key_comp() const{ // get key order predicate
return comp{};
}
alloc get_allocator() const{
return allocator;
}
hash hash_function() const{ // get hash function
return hash{};
}
template<bool is_const>
class const_noconst_iterator {
friend class sparse_patchmap;
public:
size_type hint;
key_type key;
typename conditional<is_const,
const sparse_patchmap*,
sparse_patchmap*
>::type map;
private:
void inline update_hint(){
if constexpr (!uphold_iterator_validity::value) return;
if (hint<map->datasize)
if (equal{}(map->data[hint].first,key)) return;
hint = map->find_node(key,hint);
if (hint>=map->datasize) hint = ~size_type(0);
}
void inline unsafe_increment(){ // assuming hint is valid
//cout << "unsafe_increment() " << hint << " " << key << endl;
if (++hint>=map->datasize){
//cout << "test1" << endl;
//cout << "becoming an end()" << endl;
hint=~size_type(0);
return;
}
while(true){
//cout << "test2" << endl;
const size_type k = hint/digits<size_type>();
const size_type l = hint%digits<size_type>();
const size_type m = (~size_type(0))>>l;
assert(k<map->masksize);
size_type p = (map->mask[k]&m)<<l;
if (k+1<map->masksize)
p|=shr(map->mask[k+1]&(~m),digits<size_type>()-l);
const size_type s = clz(p);
if (s==0) break;
hint+=s;
//cout << hint << " " << s << endl;
if (hint>=map->datasize){
//cout << "test3" << endl;
//cout << "becoming an end()" << endl;
hint=~size_type(0);
return;
}
}
//cout << "test4" << endl;
//cout << "new hint=" << hint << endl;
key = map->data[hint].first;
//cout << "new key=" << key << endl;
}
void inline unsafe_decrement(){ // assuming hint is valid
if (--hint>=map->datasize){
hint=~size_type(0);
return;
}
while(true){
const size_type k = hint/digits<size_type>();
const size_type l = hint%digits<size_type>();
const size_type m = (~size_type(0))<<(digits<size_type>()-l-1);
assert(k<map->masksize);
size_type p = (map->mask[k]&m)>>(digits<size_type>()-l-1);
if (k!=0) p|=shl(map->mask[k-1]&(~m),l+1);
const size_type s = ctz(p);
if (s==0) break;
hint-=s;
if (hint>=map->datasize){
hint=~size_type(0);
return;
}
}
key = map->data[hint].first;
}
template<bool is_const0,bool is_const1>
difference_type inline friend diff(
const_noconst_iterator<is_const0>& it0,
const_noconst_iterator<is_const1>& it1){
if (it1<it0) return -diff(it0,it1);
it0.update_hint();
it1.update_hint();
const size_type k0 = it0->hint/digits<size_type>();
const size_type l0 = it0->hint%digits<size_type>();
const size_type m0 = (~size_type(0))>>l0;
const size_type k1 = it1->hint/digits<size_type>();
const size_type l1 = it1->hint%digits<size_type>();
const size_type m1 = (~size_type(0))<<(digits<size_type>()-l1-1);
if (k0==k1) return popcount(m0&m1&it0.map->mask[k0])-1;
size_type d = popcount(m0&it0.map->mask[k0])
+popcount(m1&it1.map->mask[k1]);
for (size_type i = k0+1;i!=k1;++i)
d+=popcount(it0.map->mask[i]);
return d;
}
void inline add(const size_type& n){
update_hint();
size_type k = hint/digits<size_type>();