-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathCPMA.hpp
More file actions
7104 lines (6437 loc) · 252 KB
/
CPMA.hpp
File metadata and controls
7104 lines (6437 loc) · 252 KB
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 CPMA_HPP
#define CPMA_HPP
#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <ranges>
#include <tuple>
#include <utility>
#ifdef __AVX2__
#include <immintrin.h>
#endif
#include <concepts>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <memory>
#include <optional>
#include <type_traits>
#include <vector>
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wsign-compare"
#pragma clang diagnostic ignored "-Wsign-compare"
#pragma clang diagnostic ignored "-Wextra-semi-stmt"
#pragma clang diagnostic ignored "-Wextra-semi"
#pragma clang diagnostic ignored "-Wcomma"
#pragma clang diagnostic ignored "-Wpadded"
#pragma clang diagnostic ignored "-Wunused-template"
#pragma clang diagnostic ignored "-Wpacked"
#pragma clang diagnostic ignored "-Wdeprecated-copy-with-dtor"
#pragma clang diagnostic ignored "-Wimplicit-int-float-conversion"
#pragma clang diagnostic ignored "-Wreserved-identifier"
#pragma clang diagnostic ignored "-Wglobal-constructors"
#pragma clang diagnostic ignored "-Wexit-time-destructors"
#pragma clang diagnostic ignored "-Wdouble-promotion"
#pragma clang diagnostic ignored "-Wnewline-eof"
#pragma clang diagnostic ignored "-Wsign-conversion"
#pragma clang diagnostic ignored "-Wshadow-uncaptured-local"
#pragma clang diagnostic ignored "-Wshadow-field-in-constructor"
#pragma clang diagnostic ignored "-Wshadow"
#pragma clang diagnostic ignored "-Wshorten-64-to-32"
#pragma clang diagnostic ignored "-Wnontrivial-memcall"
#if !defined(NO_TLX)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wfloat-equal"
#pragma clang diagnostic ignored "-Wpadded"
#include "tlx/container/btree_map.hpp"
#pragma clang diagnostic pop
#endif
#if VQSORT == 1
#include <hwy/contrib/sort/vqsort.h>
#endif
#include "ParallelTools/concurrent_hash_map.hpp"
#include "ParallelTools/flat_hash_map.hpp"
#include "ParallelTools/parallel.h"
#include "ParallelTools/reducer.h"
#include "ParallelTools/sort.hpp"
#include "StructOfArrays/multipointer.hpp"
#include "StructOfArrays/soa.hpp"
#include "parlay/internal/group_by.h"
#include "parlay/primitives.h"
#include "parlay/slice.h"
#pragma clang diagnostic pop
#include "internal/helpers.hpp"
#include "internal/leaf.hpp"
#include "internal/timers.hpp"
enum HeadForm { InPlace, Linear, Eytzinger, BNary };
// BNAry has B pointers, and B-1 elements in each block
template <typename T, typename U> struct overwrite_on_insert {
constexpr void operator()(T current_value, U new_value) const {
current_value = new_value;
}
};
class make_pcsr {};
template <typename l, HeadForm h, uint64_t b = 0, bool density = false,
bool rank = false, bool fixed_size_ = false,
uint64_t max_fixed_size_ = 4096, bool parallel_ = true,
bool maintain_offsets_ = false,
typename value_update_ = overwrite_on_insert<
typename l::element_ref_type, typename l::element_type>>
class PMA_traits {
public:
using leaf = l;
using key_type = typename leaf::key_type;
static constexpr bool compressed = leaf::compressed;
static constexpr HeadForm head_form = h;
static constexpr uint64_t B_size = b;
static constexpr bool store_density = density;
static constexpr bool support_rank = rank;
static constexpr bool fixed_size = fixed_size_;
static constexpr uint64_t max_fixed_size = max_fixed_size_;
static constexpr bool binary = leaf::binary;
using element_type = typename leaf::element_type;
using element_ref_type = typename leaf::element_ref_type;
using element_ptr_type = typename leaf::element_ptr_type;
using SOA_type = typename leaf::SOA_type;
using value_type = typename leaf::value_type;
using value_update = value_update_;
static_assert(
std::is_invocable_v<value_update, element_ref_type, element_type &>,
"the value update function must take in a reference to the "
"current value and the new value by reference");
#if defined(PMA_GROWING_FACTOR)
static constexpr double growing_factor = PMA_GROWING_FACTOR;
#else
static constexpr double growing_factor = 1.2; // 1.5;
#endif
#if PARALLEL == 1
static constexpr bool parallel = parallel_;
#elif DEBUG == 1
// make it run the parallel like paths when running in debug mode, even if it
// is run without cilk
static constexpr bool parallel = parallel_;
#else
static constexpr bool parallel = false;
#endif
static constexpr int leaf_blow_up_factor = (sizeof(key_type) == 3) ? 18 : 16;
static constexpr uint64_t min_leaf_size = 64;
static constexpr bool maintain_offsets = maintain_offsets_;
};
template <typename T = uint64_t>
using pma_settings = PMA_traits<uncompressed_leaf<T>, InPlace, 0, false, false>;
template <typename T = uint64_t>
using spmal_settings =
PMA_traits<uncompressed_leaf<T>, Linear, 0, false, false>;
template <typename T = uint64_t>
using spmae_settings =
PMA_traits<uncompressed_leaf<T>, Eytzinger, 0, false, false>;
template <typename T = uint64_t>
using spmab5_settings =
PMA_traits<uncompressed_leaf<T>, BNary, 5, false, false>;
template <typename T = uint64_t>
using spmab9_settings =
PMA_traits<uncompressed_leaf<T>, BNary, 9, false, false>;
template <typename T = uint64_t>
using spmab17_settings =
PMA_traits<uncompressed_leaf<T>, BNary, 17, false, false>;
template <typename T = uint64_t> using spmab_settings = spmab17_settings<T>;
template <typename T = uint64_t>
using cpma_settings =
PMA_traits<delta_compressed_leaf<T>, InPlace, 0, false, false>;
template <typename T = uint64_t>
using scpmal_settings =
PMA_traits<delta_compressed_leaf<T>, Linear, 0, false, false>;
template <typename T = uint64_t>
using scpmae_settings =
PMA_traits<delta_compressed_leaf<T>, Eytzinger, 0, false, false>;
template <typename T = uint64_t>
using scpmab5_settings =
PMA_traits<delta_compressed_leaf<T>, BNary, 5, false, false>;
template <typename T = uint64_t>
using scpmab9_settings =
PMA_traits<delta_compressed_leaf<T>, BNary, 9, false, false>;
template <typename T = uint64_t>
using scpmab17_settings =
PMA_traits<delta_compressed_leaf<T>, BNary, 17, false, false>;
template <typename T = uint64_t> using scpmab_settings = scpmab17_settings<T>;
class empty_type {};
namespace PMA_precalculate {
template <typename traits>
[[nodiscard]] static constexpr uint64_t
calculate_num_leaves_rounded_up(uint64_t total_leaves) {
static_assert(traits::head_form == Eytzinger || traits::head_form == BNary,
"you should only be rounding the head array size of you are "
"in either Eytzinger or BNary form");
// Eytzinger and Bnary sometimes need to know the rounded number of leaves
// linear order
// make next power of 2
if constexpr (traits::head_form == Eytzinger) {
if (nextPowerOf2(total_leaves) > total_leaves) {
return (nextPowerOf2(total_leaves) - 1);
}
return ((total_leaves * 2) - 1);
}
// BNary order
if constexpr (traits::head_form == BNary) {
uint64_t size = traits::B_size;
while (size <= total_leaves) {
size *= traits::B_size;
}
return size;
}
}
template <typename traits>
static constexpr uint64_t num_heads(uint64_t num_total_leaves) {
if constexpr (traits::head_form == InPlace) {
return 0;
}
// linear order
if constexpr (traits::head_form == Linear) {
return num_total_leaves;
}
// make next power of 2
if constexpr (traits::head_form == Eytzinger) {
if (nextPowerOf2(num_total_leaves) > num_total_leaves) {
uint64_t space =
((nextPowerOf2(num_total_leaves) - 1) + num_total_leaves + 1) / 2;
return space;
}
return ((num_total_leaves * 2) - 1);
}
// BNary order
if constexpr (traits::head_form == BNary) {
uint64_t size = traits::B_size;
while (size <= num_total_leaves) {
size *= traits::B_size;
}
uint64_t check_size =
((size / traits::B_size + num_total_leaves + traits::B_size) /
traits::B_size) *
traits::B_size;
return std::min(size, check_size);
}
}
template <typename traits>
[[nodiscard]] static constexpr uint64_t
head_array_size(uint64_t num_total_leaves) {
return traits::SOA_type::get_size_static(num_heads<traits>(num_total_leaves));
}
template <typename traits>
[[nodiscard]] static constexpr uint64_t data_array_size(uint64_t N) {
uint64_t allocated_size = N;
if constexpr (!traits::binary) {
// we will place the value of the key zero here
allocated_size += sizeof(typename traits::key_type);
}
if (allocated_size % 32 != 0) {
allocated_size += 32 - (allocated_size % 32);
}
return traits::SOA_type::get_size_static(allocated_size /
sizeof(typename traits::key_type)) +
32;
}
template <typename traits>
[[nodiscard]] static constexpr uint64_t
density_array_size(uint64_t num_total_leaves) {
if constexpr (!traits::store_density) {
return 0;
} else {
return num_total_leaves * sizeof(uint16_t);
}
}
template <typename traits>
[[nodiscard]] static constexpr uint64_t
rank_tree_array_size(uint64_t num_total_leaves) {
if constexpr (!traits::support_rank) {
return 0;
} else {
return nextPowerOf2(num_total_leaves) * sizeof(uint64_t);
}
}
template <typename traits>
[[nodiscard]] static constexpr uint64_t
underlying_array_size(uint64_t N, uint64_t num_total_leaves) {
uint64_t total_size = 0;
if constexpr (traits::head_form != InPlace) {
total_size += head_array_size<traits>(num_total_leaves);
if (total_size % 32 != 0) {
total_size += 32 - (total_size % 32);
}
}
total_size += data_array_size<traits>(N);
if constexpr (traits::store_density) {
if (total_size % 32 != 0) {
total_size += 32 - (total_size % 32);
}
total_size += density_array_size<traits>(num_total_leaves);
}
if constexpr (traits::support_rank) {
if (total_size % 32 != 0) {
total_size += 32 - (total_size % 32);
}
total_size += rank_tree_array_size<traits>(num_total_leaves);
}
if (total_size % 32 != 0) {
total_size += 32 - (total_size % 32);
}
return total_size;
}
template <typename traits>
[[nodiscard]] static constexpr uint64_t
get_data_array_offset(uint64_t num_total_leaves) {
if constexpr (traits::head_form == InPlace) {
return 0;
} else {
uint64_t offset = head_array_size<traits>(num_total_leaves);
if (offset % 32 != 0) {
offset += 32 - (offset % 32);
}
return offset;
}
}
template <typename traits>
[[nodiscard]] static constexpr uint64_t
get_density_array_offset(uint64_t N, uint64_t num_total_leaves) {
if constexpr (!traits::store_density) {
return 0;
} else {
uint64_t offset = get_data_array_offset<traits>(num_total_leaves) +
data_array_size<traits>(N);
if (offset % 32 != 0) {
offset += 32 - (offset % 32);
}
return offset;
}
}
template <typename traits>
[[nodiscard]] static constexpr uint64_t
get_rank_tree_array_offset(uint64_t N, uint64_t num_total_leaves) {
if constexpr (!traits::support_rank) {
return 0;
} else {
uint64_t offset = get_data_array_offset<traits>(num_total_leaves) +
data_array_size<traits>(N);
if (offset % 32 != 0) {
offset += 32 - (offset % 32);
}
if constexpr (traits::store_density) {
offset += density_array_size<traits>(num_total_leaves);
if (offset % 32 != 0) {
offset += 32 - (offset % 32);
}
}
return offset;
}
}
// This is technically only enough to grow to about 20TB, if you want it to be
// bigger you can't keep the index as a uint8_t, but it is probably plenty for
// now
struct meta_data_t {
uint64_t n;
uint64_t logn;
uint64_t loglogn;
uint64_t total_leaves;
uint64_t H;
uint64_t total_leaves_rounded_up;
uint64_t elts_per_leaf;
uint64_t num_heads;
uint64_t head_array_size;
uint64_t data_array_size;
uint64_t underlying_array_size;
uint64_t density_array_size;
uint64_t rank_tree_array_size;
uint64_t data_array_offset;
uint64_t density_array_offset;
uint64_t rank_tree_array_offset;
void print() const {
printf("N = %lu, logN = %lu, loglogN = %lu, total_leaves = %lu, H = %lu, "
"num_heads = %lu, head_array_size = %lu, data_array_size = %lu, "
"density_array_size = %lu, rank_tree_array_size = %lu, "
"underlying_array_size = %lu, data_array_offset = %lu, "
"density_array_offset = %lu, rank_tree_array_offset = %lu\n",
n, logn, loglogn, total_leaves, H, num_heads, head_array_size,
data_array_size, density_array_size, rank_tree_array_size,
underlying_array_size, data_array_offset, density_array_offset,
rank_tree_array_offset);
}
} __attribute__((aligned(128)));
template <typename traits>
static constexpr std::array<meta_data_t, 256> get_metadata_table() {
std::array<meta_data_t, 256> res = {{}};
uint64_t n = traits::min_leaf_size;
uint64_t lln = bsr_long(bsr_long(n));
uint64_t ln = traits::leaf_blow_up_factor * (1UL << lln);
if (n < ln) {
n = ln;
lln = bsr_long(bsr_long(n));
}
while (n % ln != 0) {
n += ln - (n % ln);
lln = bsr_long(bsr_long(n));
ln = traits::leaf_blow_up_factor * (1UL << lln);
}
uint64_t total_leaves = n / ln;
uint64_t total_leaves_rounded_up = 0;
if constexpr (traits::head_form == Eytzinger || traits::head_form == BNary) {
total_leaves_rounded_up =
PMA_precalculate::calculate_num_leaves_rounded_up<traits>(total_leaves);
}
uint64_t H = bsr_long(total_leaves);
uint64_t i = 0;
res[i] = {
.n = n,
.logn = ln,
.loglogn = lln,
.total_leaves = total_leaves,
.H = H,
.total_leaves_rounded_up = total_leaves_rounded_up,
.elts_per_leaf = ln / sizeof(typename traits::key_type),
.num_heads = num_heads<traits>(total_leaves),
.head_array_size = (traits::head_form == InPlace)
? 0
: head_array_size<traits>(total_leaves),
.data_array_size = data_array_size<traits>(n),
.underlying_array_size = underlying_array_size<traits>(n, total_leaves),
.density_array_size = (traits::store_density)
? density_array_size<traits>(total_leaves)
: 0,
.rank_tree_array_size = (traits::support_rank)
? rank_tree_array_size<traits>(total_leaves)
: 0,
.data_array_offset = get_data_array_offset<traits>(total_leaves),
.density_array_offset = get_density_array_offset<traits>(n, total_leaves),
.rank_tree_array_offset =
get_rank_tree_array_offset<traits>(n, total_leaves)};
i += 1;
for (; i < 256; i++) {
uint64_t min_new_size = n + ln;
uint64_t desired_new_size = std::numeric_limits<uint64_t>::max();
// max sure that it fits in the amount of bits
if (n < desired_new_size / traits::growing_factor) {
desired_new_size = n * traits::growing_factor;
}
if (desired_new_size < min_new_size) {
n = min_new_size;
} else {
n = desired_new_size;
}
lln = bsr_long(bsr_long(n));
ln = traits::leaf_blow_up_factor * (1UL << lln);
while (n % ln != 0) {
n += ln - (n % ln);
if (n == 0) {
lln = 0;
} else {
lln = bsr_long(bsr_long(n));
}
ln = traits::leaf_blow_up_factor * (1U << lln);
}
total_leaves = n / ln;
H = bsr_long(total_leaves);
if constexpr (traits::head_form == Eytzinger ||
traits::head_form == BNary) {
total_leaves_rounded_up =
PMA_precalculate::calculate_num_leaves_rounded_up<traits>(
total_leaves);
}
res[i] = {
.n = n,
.logn = ln,
.loglogn = lln,
.total_leaves = total_leaves,
.H = H,
.total_leaves_rounded_up = total_leaves_rounded_up,
.elts_per_leaf = ln / sizeof(typename traits::key_type),
.num_heads = num_heads<traits>(total_leaves),
.head_array_size = (traits::head_form == InPlace)
? 0
: head_array_size<traits>(total_leaves),
.data_array_size = data_array_size<traits>(n),
.underlying_array_size = underlying_array_size<traits>(n, total_leaves),
.density_array_size = (traits::store_density)
? density_array_size<traits>(total_leaves)
: 0,
.rank_tree_array_size = (traits::support_rank)
? rank_tree_array_size<traits>(total_leaves)
: 0,
.data_array_offset = get_data_array_offset<traits>(total_leaves),
.density_array_offset =
get_density_array_offset<traits>(n, total_leaves),
.rank_tree_array_offset =
get_rank_tree_array_offset<traits>(n, total_leaves)};
}
return res;
}
} // namespace PMA_precalculate
template <typename traits> class CPMA {
public:
using leaf = typename traits::leaf;
using key_type = typename traits::key_type;
static constexpr uint64_t B_size = traits::B_size;
static constexpr HeadForm head_form = traits::head_form;
static constexpr bool store_density = traits::store_density;
static constexpr bool support_rank = traits::support_rank;
static constexpr bool fixed_size = traits::fixed_size;
static constexpr uint64_t max_fixed_size = traits::max_fixed_size;
static constexpr bool parallel = traits::parallel;
static_assert(std::is_trivially_copyable_v<key_type>,
"T must be trivially copyable");
static_assert(B_size == 0 || head_form == BNary,
"B_size should only be used if we are using head_form = BNary");
static_assert(std::is_unsigned_v<key_type>,
"we assume that in sorted order the null sentinel, which is "
"zero, will be first");
static constexpr bool binary = traits::binary;
using element_type = typename traits::element_type;
using element_ref_type = typename traits::element_ref_type;
using element_ptr_type = typename traits::element_ptr_type;
using SOA_type = typename traits::SOA_type;
using value_type = typename traits::value_type;
using value_update = typename traits::value_update;
// types needs for graphs
// When we store a graph we assume that we are storing 64 bit element paris
// of src dest
using node_t = uint32_t;
using extra_data_t =
std::pair<std::unique_ptr<typename leaf::iterator, free_delete>,
std::unique_ptr<uint64_t, free_delete>>;
// bool in binary/
// the value if there is only 1 value
// tuple of the values if there are multiple
static constexpr auto get_value_type() {
if constexpr (binary) {
return (bool)true;
} else {
value_type v;
if constexpr (std::tuple_size_v<value_type> == 1) {
return std::tuple_element_t<0, value_type>();
} else {
return v;
}
}
}
using weight_t = decltype(get_value_type());
private:
static constexpr double growing_factor = traits::growing_factor;
static constexpr int leaf_blow_up_factor = traits::leaf_blow_up_factor;
static constexpr uint64_t min_leaf_size = traits::min_leaf_size;
static_assert(min_leaf_size >= 64, "min_leaf_size must be at least 64 bytes");
using meta_data_t = PMA_precalculate::meta_data_t;
static constexpr std::array<meta_data_t, 256> meta_data =
PMA_precalculate::get_metadata_table<traits>();
[[nodiscard]] static constexpr uint64_t
total_leaves(uint8_t meta_data_index) {
return meta_data[meta_data_index].total_leaves;
}
public:
[[nodiscard]] uint64_t total_leaves() const {
return meta_data[meta_data_index].total_leaves;
}
private:
[[nodiscard]] uint64_t num_heads() const {
return meta_data[meta_data_index].num_heads;
}
[[nodiscard]] static constexpr uint64_t N(uint8_t meta_data_index) {
return meta_data[meta_data_index].n;
}
public:
[[nodiscard]] uint64_t N() const { return N(meta_data_index); }
private:
[[nodiscard]] uint64_t head_array_size() const {
return meta_data[meta_data_index].head_array_size;
}
static constexpr uint64_t get_max_fixed_size() {
uint8_t meta_data_index = 0;
while (meta_data[meta_data_index + 1].underlying_array_size <=
max_fixed_size) {
meta_data_index++;
}
return meta_data_index;
}
[[nodiscard]] constexpr uint64_t underlying_array_size() const {
return meta_data[meta_data_index].underlying_array_size;
}
typename std::conditional<fixed_size,
std::array<uint8_t, meta_data[get_max_fixed_size()]
.underlying_array_size>,
void *>::type underlying_array;
key_type count_elements_ = 0;
uint8_t meta_data_index = 0;
bool has_0 = false;
// when we are running in pcsr like mode, this is to store the offsets to the
// different node regions
// we store the following
// - a pointer an an array of pointers to the start of each region
// - - there is an extra one at the end to point at the overall end
// - the number of nodes (how long the arrays are)
// - the degree of each node
[[no_unique_address]]
typename std::conditional<traits::maintain_offsets, pcsr_node_info<key_type>,
empty_type>::type offsets_array;
static constexpr std::array<std::array<float, sizeof(key_type) * 8>, 256>
get_upper_density_bound_table() {
uint64_t max_fixed_size_index = get_max_fixed_size();
std::array<std::array<float, sizeof(key_type) * 8>, 256> res;
for (uint64_t i = 0; i < 256; i++) {
auto m = meta_data[i];
for (uint64_t j = 0; j < sizeof(key_type) * 8; j++) {
float val = 1.0F / 2.0F;
if (m.H != 0) {
val = 1.0F / 2.0F + (((1.0F / 2.0F) * j) / m.H);
}
if (val >= static_cast<float>(m.logn - (3 * leaf::max_element_size)) /
m.logn) {
val = static_cast<float>(m.logn - (3 * leaf::max_element_size)) /
m.logn -
.001F;
}
if constexpr (fixed_size) {
if (j == 0 && (i + 1) > max_fixed_size_index) {
val = density_limit(meta_data[i].logn) - .001;
}
}
res[i][j] = val;
}
}
return res;
}
static constexpr std::array<std::array<float, sizeof(key_type) * 8>, 256>
get_lower_density_bound_table() {
std::array<std::array<float, sizeof(key_type) * 8>, 256> res;
for (uint64_t i = 0; i < 256; i++) {
for (uint64_t j = 0; j < sizeof(key_type) * 8; j++) {
auto m = meta_data[i];
float val = std::max(((float)sizeof(key_type)) / m.logn, 1.0F / 4.0F);
if (m.H != 0) {
val = std::max(((float)sizeof(key_type)) / m.logn,
1.0F / 4.0F - ((.125F * j) / m.H));
}
if (val <= 0) {
val = std::numeric_limits<float>::min();
}
res[i][j] = val;
}
}
return res;
}
static constexpr std::array<std::array<float, sizeof(key_type) * 8>, 256>
upper_density_bound_table = get_upper_density_bound_table();
static constexpr std::array<std::array<float, sizeof(key_type) * 8>, 256>
lower_density_bound_table = get_lower_density_bound_table();
[[nodiscard]] uint8_t *underlying_array_char() const {
if constexpr (fixed_size) {
return (uint8_t *)(underlying_array.data());
} else {
return static_cast<uint8_t *>(underlying_array);
}
}
key_type *head_array() const {
static_assert(head_form != InPlace);
return reinterpret_cast<key_type *>(underlying_array_char());
}
key_type *key_array() const {
auto ptr = reinterpret_cast<key_type *>(
underlying_array_char() + meta_data[meta_data_index].data_array_offset);
if constexpr (!binary) {
// +1 to offset past the value for zero
ptr += 1;
}
return ptr;
}
key_type *data_array() const {
return reinterpret_cast<key_type *>(
underlying_array_char() + meta_data[meta_data_index].data_array_offset);
}
// stored the density of each leaf to speed up merges and get_density_count
// only use a uint16_t to save on space
// this might not be enough to store out of place leaves after batch merge,
// so in the event of an overflow just store max which we then just go count
// the density as usual, which is cheap since it is stored out of place and
// the size is just written
auto density_array() const {
if constexpr (!store_density) {
return empty_type();
} else {
return reinterpret_cast<uint16_t *>(
underlying_array_char() +
meta_data[meta_data_index].density_array_offset);
}
}
// stored a flattened rank tree where each node stored the number of
// elements to the left of that node in the subtree of that node
auto rank_tree_array() const {
if constexpr (!support_rank) {
return empty_type();
} else {
return reinterpret_cast<uint64_t *>(
underlying_array_char() +
meta_data[meta_data_index].rank_tree_array_offset);
}
}
[[nodiscard]] uint64_t soa_num_spots() const {
if constexpr (!binary) {
// extra spot for the value of 0
return N() / sizeof(key_type) + 1;
} else {
return N() / sizeof(key_type);
}
}
template <size_t... Is> auto get_data_ref(size_t i) const {
if constexpr (!binary) {
// +1 to offset past the value for zero
i += 1;
}
if constexpr (sizeof...(Is) == 1) {
return std::get<0>(SOA_type::template get_static<Is...>(
data_array(), soa_num_spots(), i));
} else {
return SOA_type::template get_static<Is...>(data_array(), soa_num_spots(),
i);
}
}
template <size_t... Is> auto get_zero_el_ref() const {
static_assert(!binary);
return get_data_ref<Is...>(-1);
}
template <size_t... Is> auto get_data_ptr(size_t i) const {
if constexpr (!binary) {
// +1 to offset past the value for zero
i += 1;
}
return SOA_type::template get_static_ptr<Is...>(data_array(),
soa_num_spots(), i);
}
template <size_t... Is> auto get_zero_el_ptr() const {
static_assert(!binary);
return get_data_ptr<Is...>(-1);
}
template <size_t... Is> auto get_head_ref(size_t i) const {
if constexpr (sizeof...(Is) == 1) {
return std::get<0>(
SOA_type::template get_static<Is...>(head_array(), num_heads(), i));
} else {
return SOA_type::template get_static<Is...>(head_array(), num_heads(), i);
}
}
template <size_t... Is> auto get_head_ptr(size_t i) const {
return SOA_type::template get_static_ptr<Is...>(head_array(), num_heads(),
i);
}
#if VQSORT == 1
hwy::Sorter sorter;
#endif
uint64_t build_rank_array_recursive(uint64_t start, uint64_t end,
uint64_t rank_array_index,
uint64_t *correct_array) const;
[[nodiscard]] bool check_rank_array() const;
void update_rank(uint64_t leaf_changed, int64_t change_amount);
void update_rank(ParallelTools::Reducer_Vector<std::pair<uint64_t, uint64_t>>
&rank_additions);
void update_rank(std::vector<std::pair<uint64_t, uint64_t>> &rank_additions);
element_ref_type index_to_head(uint64_t index) const {
if constexpr (head_form == InPlace) {
return get_data_ref(index * elts_per_leaf());
}
// linear order
if constexpr (head_form == Linear) {
return get_head_ref(index);
}
//
// Eytzinger order
if constexpr (head_form == Eytzinger) {
return get_head_ref(e_index(index, total_leaves()));
}
// BNary order
if constexpr (head_form == BNary) {
uint64_t in = bnary_index<B_size>(index, total_leaves_rounded_up());
return get_head_ref(in);
}
}
key_type &index_to_head_key(uint64_t index) const {
return std::get<0>(index_to_head(index));
}
element_ptr_type index_to_data(uint64_t index) const {
if constexpr (head_form == InPlace) {
return get_data_ptr(index * elts_per_leaf()) + 1;
} else {
return get_data_ptr(index * elts_per_leaf());
}
}
leaf get_leaf(uint64_t leaf_number) const {
return leaf(index_to_head(leaf_number), index_to_data(leaf_number),
leaf_size_in_bytes());
}
// how big will the leaf be not counting the head
[[nodiscard]] uint64_t leaf_size_in_bytes() const {
if constexpr (head_form == InPlace) {
return logN() - sizeof(key_type);
} else {
return logN();
}
}
[[nodiscard]] std::pair<float, float> density_bound(uint64_t depth) const;
[[nodiscard]] uint64_t loglogN() const {
return meta_data[meta_data_index].loglogn;
}
[[nodiscard]] uint64_t logN() const {
return meta_data[meta_data_index].logn;
}
[[nodiscard]] uint64_t H() const { return meta_data[meta_data_index].H; }
[[nodiscard]] uint64_t total_leaves_rounded_up() const {
static_assert(head_form == Eytzinger || head_form == BNary,
"you should only be rounding the head array size of you are "
"in either Eytzinger or BNary form");
return meta_data[meta_data_index].total_leaves_rounded_up;
}
[[nodiscard]] uint64_t elts_per_leaf() const {
return meta_data[meta_data_index].elts_per_leaf;
}
[[nodiscard]] float lower_density_bound(uint64_t depth) const {
ASSERT(depth < 10000000,
"depth shouldn't be higher than log(n) it is %lu\n", depth);
assert(((double)sizeof(key_type)) / logN() <=
lower_density_bound_table[meta_data_index][depth]);
return lower_density_bound_table[meta_data_index][depth];
}
[[nodiscard]] float upper_density_bound(uint64_t depth) const {
ASSERT(upper_density_bound_table[meta_data_index][depth] <= density_limit(),
"density_bound_[%lu].second = %f > density_limit() = %f\n", depth,
upper_density_bound_table[meta_data_index][depth], density_limit());
// making sure we don't pass in a negative number
ASSERT(depth < 100000000UL, "depth = %lu\n", depth);
return upper_density_bound_table[meta_data_index][depth];
}
[[nodiscard]] static constexpr double density_limit(uint64_t logN) {
// we need enough space on both sides regardless of how elements are split
if constexpr (compressed) {
return static_cast<double>(logN - (3 * leaf::max_element_size)) / logN;
} else {
return static_cast<double>(logN - (1 * leaf::max_element_size)) / logN;
}
}
[[nodiscard]] double density_limit() const { return density_limit(logN()); }
void grow_list(uint64_t times);
void shrink_list(uint64_t times);
[[nodiscard]] uint64_t get_density_count(uint64_t index, uint64_t len) const;
[[nodiscard]] uint64_t get_density_count_no_overflow(uint64_t index,
uint64_t len) const;
bool
check_leaf_heads(uint64_t start_idx = 0,
uint64_t end_idx = std::numeric_limits<uint64_t>::max());
[[nodiscard]] uint64_t get_depth(uint64_t len) const {
return bsr_long(N() / len);
}
[[nodiscard]] uint64_t find_node(uint64_t index, uint64_t len) const {
return (index / len) * len;
}
[[nodiscard]] uint64_t find_containing_leaf_index(
key_type e, uint64_t start = 0,
uint64_t end = std::numeric_limits<uint64_t>::max()) const;
[[nodiscard]] uint64_t find_containing_leaf_number(
key_type e, uint64_t start = 0,
uint64_t end = std::numeric_limits<uint64_t>::max()) const {
return find_containing_leaf_index(e, start, end) / elts_per_leaf();
}
[[nodiscard]] uint64_t find_containing_leaf_index_debug(
key_type e, uint64_t start = 0,
uint64_t end = std::numeric_limits<uint64_t>::max()) const;
template <class F>
[[nodiscard]] std::pair<
ParallelTools::Reducer_Vector<std::tuple<uint64_t, uint64_t>>,
std::optional<uint64_t>>
get_ranges_to_redistibute(
const ParallelTools::Reducer_Vector<std::pair<uint64_t, uint64_t>>
&leaves_to_check,
uint64_t num_elts_merged, F bounds_check) const;
template <class F>
[[nodiscard]] std::pair<std::vector<std::tuple<uint64_t, uint64_t>>,
std::optional<uint64_t>>
get_ranges_to_redistibute(
const std::vector<std::pair<uint64_t, uint64_t>> &leaves_to_check,
uint64_t num_elts_merged, F bounds_check) const;
void redistribute_ranges(
const std::vector<std::tuple<uint64_t, uint64_t>> &ranges);
void redistribute_ranges(
const ParallelTools::Reducer_Vector<std::tuple<uint64_t, uint64_t>>
&ranges);
using leaf_bound_t = struct {
key_type start_elt;
key_type end_elt;
uint64_t start_leaf_index;
uint64_t end_leaf_index;
};
std::vector<leaf_bound_t> get_leaf_bounds(uint64_t split_points) const;
[[nodiscard]] uint64_t get_ranges_to_redistibute_lookup_sibling_count(
const std::vector<ParallelTools::concurrent_hash_map<uint64_t, uint64_t>>
&ranges_check,
uint64_t start, uint64_t length, uint64_t level,
uint64_t depth = 0) const;
[[nodiscard]] uint64_t get_ranges_to_redistibute_lookup_sibling_count(
const std::vector<ska::flat_hash_map<uint64_t, uint64_t>> &ranges_check,
uint64_t start, uint64_t length, uint64_t level) const;
[[nodiscard]] std::pair<std::vector<std::tuple<uint64_t, uint64_t>>,
std::optional<uint64_t>>
get_ranges_to_redistibute_debug(
const std::vector<std::pair<uint64_t, uint64_t>> &leaves_to_check,
uint64_t num_elts_merged) const;
[[nodiscard]] std::pair<std::vector<std::tuple<uint64_t, uint64_t>>,
std::optional<uint64_t>>
get_ranges_to_redistibute_debug(
const ParallelTools::Reducer_Vector<std::pair<uint64_t, uint64_t>>
&leaves_to_check,
uint64_t num_elts_merged) const {
return get_ranges_to_redistibute_debug(leaves_to_check.get(),
num_elts_merged);