-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBTree.hpp
1221 lines (1013 loc) · 35.8 KB
/
BTree.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
// THIS FILE IS AUTOMATICALLY GENERATED, DO NOT EDIT
#include "utility.hpp"
#include <functional>
#include <cstddef>
#include "exception.hpp"
#include <map>
#include <fstream>
#define ONLINE_JUDGE
namespace sjtu {
//
// Created by Alex Chi on 2019-05-30.
//
#ifndef BPLUSTREE_LRU_HPP
#define BPLUSTREE_LRU_HPP
#include <cstring>
template<unsigned Cap, typename Idx = unsigned>
class LRU {
struct Node {
Idx idx;
Node *prev, *next;
Node(Idx idx) : idx(idx), prev(nullptr), next(nullptr) {}
} *head, *tail;
void push(Node* ptr) {
if (size == 0) { head = tail = ptr; }
else {
ptr->next = head;
head->prev = ptr;
head = ptr;
}
++size;
}
void remove(Node* ptr) {
if (ptr->prev) ptr->prev->next = ptr->next; else {
head = ptr->next;
}
if (ptr->next) ptr->next->prev = ptr->prev; else {
tail = ptr->prev;
}
ptr->next = ptr->prev = nullptr;
--size;
}
public:
unsigned size;
Node** nodes;
LRU() : head(nullptr), tail(nullptr), size(0) {
nodes = new Node*[Cap];
memset(nodes, 0, sizeof(Node*) * Cap);
}
~LRU() {
delete[] nodes;
}
void put(Idx idx) {
assert(idx < Cap);
assert(nodes[idx] == nullptr);
Node *ptr = new Node(idx);
push(ptr);
nodes[idx] = ptr;
}
void get(Idx idx) {
assert(idx < Cap);
assert(nodes[idx]);
remove(nodes[idx]);
push(nodes[idx]);
}
Idx expire() {
return tail->idx;
}
void remove(Idx idx) {
assert(idx < Cap);
assert(nodes[idx]);
remove(nodes[idx]);
delete nodes[idx];
nodes[idx] = nullptr;
}
void debug() {
unsigned last_idx;
for (auto ptr = head; ptr != nullptr; ptr = ptr->next) {
if (ptr->idx == last_idx) {
std::clog << "Cycle detected!" << std::endl;
return;
}
std::clog << ptr->idx << " ";
last_idx = ptr->idx;
}
std::clog << std::endl;
}
};
#endif //BPLUSTREE_LRU_HPP
//
// Created by Alex Chi on 2019-05-29.
//
#ifndef BPLUSTREE_PERSISTENCE_HPP
#define BPLUSTREE_PERSISTENCE_HPP
#include <fstream>
#include <iostream>
#include <cstring>
#ifndef ONLINE_JUDGE
#include "LRU.hpp"
#endif
class Serializable {
public:
virtual unsigned storage_size() const = 0;
virtual void serialize(std::ostream &out) const = 0;
virtual void deserialize(std::istream &in) = 0;
static constexpr bool is_serializable() { return true; }
};
template<typename Block, typename Index, typename Leaf, unsigned MAX_PAGES = 1048576, unsigned MAX_IN_MEMORY = 65536>
struct Persistence {
const char *path;
std::fstream f;
static const unsigned VERSION = 6;
struct PersistenceIndex {
unsigned root_idx;
unsigned magic_key;
unsigned version;
unsigned size;
size_t tail_pos;
size_t page_offset[MAX_PAGES];
size_t page_size[MAX_PAGES];
unsigned char is_leaf[MAX_PAGES];
static unsigned constexpr MAGIC_KEY() {
return sizeof(Index) * 233
+ sizeof(Leaf) * 23333
+ MAX_PAGES * 23;
}
PersistenceIndex() : root_idx(0), magic_key(MAGIC_KEY()),
version(VERSION),
tail_pos(sizeof(PersistenceIndex)),
size(0) {
memset(page_offset, 0, sizeof(page_offset));
memset(is_leaf, 0, sizeof(is_leaf));
}
} *persistence_index;
Block **pages;
bool *dirty;
unsigned lst_empty_slot;
struct Stat {
long long create;
long long destroy;
long long access_cache_hit;
long long access_cache_miss;
long long dirty_write;
long long swap_out;
void stat() {
printf(" access hit/total %lld/%lld %.5f%%\n",
access_cache_hit,
access_cache_miss + access_cache_hit,
double(access_cache_hit) / (access_cache_miss + access_cache_hit) * 100);
printf(" create/destroy %lld %lld\n", create, destroy);
printf(" swap_in/out/dirty %lld %lld %lld %.5f%%\n",
access_cache_miss, swap_out, dirty_write,
double(dirty_write) / (swap_out) * 100);
}
Stat() : create(0), destroy(0),
access_cache_hit(1), access_cache_miss(0), dirty_write(0), swap_out(0) {}
} stat;
using BLRU = LRU<MAX_PAGES>;
BLRU lru;
Persistence(const char *path = nullptr) : path(path), lst_empty_slot(16) {
assert(Index::is_serializable());
assert(Leaf::is_serializable());
persistence_index = new PersistenceIndex;
pages = new Block *[MAX_PAGES];
dirty = new bool[MAX_PAGES];
memset(pages, 0, sizeof(Block *) * MAX_PAGES);
memset(dirty, 0, sizeof(bool) * MAX_PAGES);
if (path) {
f.open(path, std::ios::in | std::ios::out | std::ios::ate | std::ios::binary);
if (f)
restore();
else
f.open(path, std::ios::in | std::ios::out | std::ios::trunc | std::ios::binary);
}
}
~Persistence() {
save();
f.close();
delete[] dirty;
delete[] pages;
delete persistence_index;
}
void restore() {
if (!path) return;
f.seekg(0, f.beg);
if (!f.read(reinterpret_cast<char *>(persistence_index), sizeof(PersistenceIndex))) {
std::clog << "[Warning] failed to restore from " << path << " " << f.gcount() << std::endl;
f.clear();
}
assert(persistence_index->version == VERSION);
assert(persistence_index->magic_key == PersistenceIndex::MAGIC_KEY());
}
void offload_page(unsigned page_id) {
if (!path) return;
Block *page = pages[page_id];
if (dirty[page_id]) {
auto offset = persistence_index->page_offset[page_id];
f.seekp(offset, f.beg);
page->serialize(f);
++stat.dirty_write;
}
delete pages[page_id];
pages[page_id] = nullptr;
lru.remove(page_id);
}
bool is_loaded(unsigned page_id) { return pages[page_id] != nullptr; }
Block *load_page(unsigned page_id) {
if (pages[page_id]) {
++stat.access_cache_hit;
lru.get(page_id);
return pages[page_id];
}
if (!path) return nullptr;
++stat.access_cache_miss;
Block *page;
if (!persistence_index->page_offset[page_id]) return nullptr;
if (persistence_index->is_leaf[page_id] == 1)
page = new Leaf;
else if (persistence_index->is_leaf[page_id] == 0)
page = new Index;
else
assert(false);
f.seekg(persistence_index->page_offset[page_id], f.beg);
page->deserialize(f);
pages[page_id] = page;
page->storage = this;
page->idx = page_id;
lru.put(page_id);
return page;
}
void save() {
if (!path) return;
f.seekp(0, f.beg);
f.write(reinterpret_cast<char *>(persistence_index), sizeof(PersistenceIndex));
for (unsigned i = 0; i < MAX_PAGES; i++) {
if (pages[i]) offload_page(i);
}
}
const Block *read(unsigned page_id) {
return load_page(page_id);
}
Block *get(unsigned page_id) {
dirty[page_id] = true;
return load_page(page_id);
}
size_t align_to_4k(size_t offset) {
return (offset + 0xfff) & (~0xfff);
}
unsigned append_page(size_t &offset, size_t size) {
offset = align_to_4k(persistence_index->tail_pos);
persistence_index->tail_pos = offset + size;
return lst_empty_slot++;
}
unsigned request_page(size_t &offset, size_t size) {
for(;;lst_empty_slot++) {
if (persistence_index->page_offset[lst_empty_slot] == 0) return append_page(offset, size);
if (persistence_index->is_leaf[lst_empty_slot] == 2 && persistence_index->page_size[lst_empty_slot] >= size) {
offset = persistence_index->page_offset[lst_empty_slot];
return lst_empty_slot++;
}
}
}
void create_page(Block *block) {
size_t offset;
unsigned page_id = request_page(offset, block->storage_size());
block->idx = page_id;
pages[page_id] = block;
persistence_index->is_leaf[page_id] = block->is_leaf() ? 1 : 0;
persistence_index->page_offset[page_id] = offset;
persistence_index->page_size[page_id] = block->storage_size();
lru.put(page_id);
dirty[page_id] = true;
}
void swap_out_pages() {
while (lru.size > MAX_IN_MEMORY) {
unsigned idx = lru.expire();
offload_page(idx);
++stat.swap_out;
}
}
void record(Block *block) {
create_page(block);
block->storage = this;
++stat.create;
}
void deregister(Block *block) {
lru.remove(block->idx);
pages[block->idx] = nullptr;
dirty[block->idx] = false;
lst_empty_slot = std::min(lst_empty_slot, block->idx);
persistence_index->is_leaf[block->idx] = 2;
block->idx = 0;
++stat.destroy;
}
};
#endif //BPLUSTREE_PERSISTENCE_HPP
//
// Created by Alex Chi on 2019-05-24.
//
#ifndef BPLUSTREE_CONTAINER_HPP
#define BPLUSTREE_CONTAINER_HPP
#include <cassert>
#include <cstring>
#include <iostream>
#ifndef ONLINE_JUDGE
#include "Persistence.hpp"
#endif
#ifdef ONLINE_JUDGE
#define ALLOCATOR_DISABLE_MEMORY_ALIGN
#endif
template<typename U>
struct Allocator {
U *allocate(unsigned size) {
#ifdef ALLOCATOR_DISABLE_MEMORY_ALIGN
return (U *) ::operator new(sizeof(U) * size);
#else
return (U *) ::operator new(sizeof(U) * size, (std::align_val_t) (4 * 1024));
#endif
}
void deallocate(U *x) { ::operator delete(x); }
void construct(U *x, const U &d) { new(x) U(d); }
void destruct(U *x) { x->~U(); }
};
template<typename T, unsigned Cap>
class Vector : Serializable {
Allocator<T> a;
public:
T *x;
unsigned size;
static constexpr unsigned capacity() { return Cap; }
Vector() : size(0) {
x = a.allocate(capacity());
}
virtual ~Vector() {
for (int i = 0; i < size; i++) a.destruct(&x[i]);
a.deallocate(x);
}
Vector(const Vector &) = delete;
T &operator[](unsigned i) {
assert(i < size);
return x[i];
}
const T &operator[](unsigned i) const {
assert(i < size);
return x[i];
}
void append(const T &d) {
assert(size < Cap);
a.construct(&x[size++], d);
}
T pop() {
assert(size > 0);
T d = x[size - 1];
a.destruct(&x[--size]);
return d;
}
void insert(unsigned pos, const T &d) {
assert(pos >= 0 && pos <= size);
assert(size < capacity());
memmove(x + pos + 1, x + pos, (size - pos) * sizeof(T));
a.construct(&x[pos], d);
++size;
}
void remove_range(unsigned pos, unsigned length = 1) {
assert(size >= length);
assert(pos < size);
for (int i = pos; i < pos + length; i++) a.destruct(&x[i]);
memmove(x + pos, x + pos + length, (size - length - pos) * sizeof(T));
size -= length;
}
T remove(unsigned pos) {
assert(size > 0);
assert(pos < size);
T element = x[pos];
a.destruct(&x[pos]);
memmove(x + pos, x + pos + 1, (size - 1 - pos) * sizeof(T));
size -= 1;
return element;
}
void move_from(Vector &that, unsigned offset, unsigned length) {
assert(size == 0);
move_insert_from(that, offset, length, 0);
}
void move_insert_from(Vector &that, unsigned offset, unsigned length, unsigned at) {
assert(at <= size);
assert(offset + length <= that.size);
assert(size + length <= capacity());
memmove(x + at + length, x + at, (size - at) * sizeof(T));
memcpy(x + at, that.x + offset, length * sizeof(T));
memmove(that.x + offset, that.x + offset + length, (that.size - length - offset) * sizeof(T));
that.size -= length;
size += length;
}
unsigned storage_size() const { return Storage_Size(); };
/*
* Storage Mapping
* | 8 size | Cap() T x |
*/
void serialize(std::ostream &out) const {
out.write(reinterpret_cast<const char *>(&size), sizeof(size));
out.write(reinterpret_cast<const char *>(x), sizeof(T) * capacity());
};
void deserialize(std::istream &in) {
in.read(reinterpret_cast<char *>(&size), sizeof(size));
in.read(reinterpret_cast<char *>(x), sizeof(T) * capacity());
// WARNING: only applicable to primitive types because no data were constructed!!!
};
static constexpr unsigned Storage_Size() {
return sizeof(T) * capacity() + sizeof(unsigned);
}
};
template<typename T, unsigned Cap>
class Set : public Vector<T, Cap> {
public:
unsigned bin_lower_bound(const T &d) const {
// https://academy.realm.io/posts/how-we-beat-cpp-stl-binary-search/
unsigned low = 0, size = this->size;
while (size > 0) {
unsigned half = size / 2;
unsigned other_half = size - half;
unsigned probe = low + half;
unsigned other_low = low + other_half;
size = half;
low = this->x[probe] < d ? other_low : low;
}
return low;
}
unsigned bin_upper_bound(const T &d) const {
// https://academy.realm.io/posts/how-we-beat-cpp-stl-binary-search/
unsigned low = 0, size = this->size;
while (size > 0) {
unsigned half = size / 2;
unsigned other_half = size - half;
unsigned probe = low + half;
unsigned other_low = low + other_half;
size = half;
low = this->x[probe] < d || this->x[probe] == d ? other_low : low;
}
return low;
}
unsigned linear_upper_bound(const T &d) const {
for (unsigned i = 0; i < this->size; i++) {
if (this->x[i] > d) return i;
}
return this->size;
}
unsigned linear_lower_bound(const T &d) const {
for (unsigned i = 0; i < this->size; i++) {
if (this->x[i] >= d) return i;
}
return this->size;
}
unsigned upper_bound(const T &d) const { return bin_upper_bound(d); }
unsigned lower_bound(const T &d) const { return bin_lower_bound(d); }
unsigned insert(const T &d) {
unsigned pos = upper_bound(d);
Vector<T, Cap>::insert(pos, d);
return pos;
}
};
#endif //BPLUSTREE_CONTAINER_HPP
//
// Created by Alex Chi on 2019-06-08.
//
#ifndef BPLUSTREE_ITERATOR_HPP
#define BPLUSTREE_ITERATOR_HPP
template<typename BTree, typename Block, typename Leaf, typename K, typename V>
class Iterator {
mutable BTree *tree;
using BlockIdx = typename BTree::BlockIdx;
BlockIdx leaf_idx;
int pos;
public:
Iterator(BTree *tree, BlockIdx leaf_idx, int pos) : tree(tree), leaf_idx(leaf_idx), pos(pos) {}
Iterator(const Iterator &that) : tree(that.tree), leaf_idx(that.leaf_idx), pos(that.pos) {}
Iterator &operator++() {
++pos;
if (pos == leaf()->keys.size && leaf()->next) {
pos = 0;
leaf_idx = leaf()->next;
}
expire();
return *this;
}
void expire() const {
tree->storage->swap_out_pages();
}
const Leaf *leaf() const { return reinterpret_cast<const Leaf *>(tree->storage->read(leaf_idx)); }
Leaf *leaf_mut() { return Block::into_leaf(tree->storage->get(leaf_idx)); }
Iterator &operator--() {
--pos;
if (pos < 0 && leaf()->prev) {
leaf_idx = leaf()->prev;
pos = leaf()->keys.size - 1;
}
expire();
return *this;
}
Iterator operator++(int) {
auto _ = Iterator(*this);
++(*this);
return _;
}
Iterator operator--(int) {
auto _ = Iterator(*this);
--(*this);
return _;
}
V operator*() {
return getValue();
}
friend bool operator==(const Iterator &a, const Iterator &b) {
return a.tree == b.tree && a.leaf_idx == b.leaf_idx && a.pos == b.pos;
}
friend bool operator!=(const Iterator &a, const Iterator &b) {
return !(a == b);
}
V getValue() {
expire();
return leaf_mut()->data[pos];
}
void modify(const V &v) {
expire();
leaf_mut()->data[pos] = v;
}
};
#endif //BPLUSTREE_ITERATOR_HPP
//
// Created by Alex Chi on 2019-05-23.
//ac
#ifndef BPLUSTREE_BTREE_HPP
#define BPLUSTREE_BTREE_HPP
#include <cassert>
#include <iostream>
#include <fstream>
#include "utility.hpp"
#ifndef ONLINE_JUDGE
#include "Container.hpp"
#include "Persistence.hpp"
#include "Iterator.hpp"
#endif
template<typename K>
constexpr unsigned Default_Ord() {
return std::max((int) ((4 * 1024 - sizeof(unsigned) * 3) / (sizeof(K) + sizeof(unsigned))), 4);
}
template<typename K, unsigned Ord>
constexpr unsigned Default_Max_Page_In_Memory() {
// maximum is about 6GB in memory
return 2 * 1024 * 1024 / Ord / sizeof(K) * 1024;
}
// if unit test is enabled
#ifdef REQUIRE
constexpr unsigned Default_Max_Pages() {
return 1048576;
}
#else
constexpr unsigned Default_Max_Pages() {
return 16777216;
}
#endif
template<typename K, typename V,
unsigned Ord = Default_Ord<K>(),
unsigned Max_Page_In_Memory = Default_Max_Page_In_Memory<K, Ord>(),
unsigned Max_Page = Default_Max_Pages()>
class BTree {
public:
static constexpr unsigned MaxPageInMemory() { return Max_Page_In_Memory; }
static constexpr unsigned MaxPage() { return Max_Page; }
using BlockIdx = unsigned;
static constexpr unsigned Order() { return Ord; }
static constexpr unsigned HalfOrder() { return Order() >> 1; }
/*
* data Block k v = Index { idx :: Int,
* keys :: [k],
* children :: [Block] } |
* Leaf { idx :: Int,
* prev :: Block,
* next :: Block,
* keys :: [k],
* data :: [v] }
*/
class Leaf;
class Index;
class Block;
using LeafPos = pair<BlockIdx, unsigned>;
using BPersistence = Persistence<Block, Index, Leaf, Max_Page, Max_Page_In_Memory>;
struct Block : public Serializable {
BlockIdx idx;
Set<K, Order()> keys;
BPersistence *storage;
Block() : storage(nullptr) {}
virtual ~Block() {}
virtual bool is_leaf() const = 0;
virtual Block *split(K &split_key) = 0;
virtual bool insert(const K &k, const V &v) = 0;
virtual bool remove(const K &k) = 0;
virtual const V *query(const K &k) const = 0;
virtual LeafPos find(const K &k) const = 0;
bool should_split() const { return keys.size == Order(); }
bool should_merge() const { return keys.size * 2 < Order(); }
bool may_borrow() const { return keys.size * 2 > Order(); }
virtual K borrow_from_left(Block *left, const K &split_key) = 0;
virtual K borrow_from_right(Block *right, const K &split_key) = 0;
virtual void merge_with_left(Block *left, const K &split_key) = 0;
virtual void merge_with_right(Block *right, const K &split_key) = 0;
inline static Leaf *into_leaf(Block *b) {
assert(b->is_leaf());
Leaf *l = reinterpret_cast<Leaf *>(b);
assert(l);
return l;
}
inline static Index *into_index(Block *b) {
assert(!b->is_leaf());
Index *i = reinterpret_cast<Index *>(b);
assert(i);
return i;
}
};
struct Index : public Block {
Index() : Block() {}
Vector<BlockIdx, Order() + 1> children;
constexpr bool is_leaf() const override { return false; }
Index *split(K &k) override {
Index *that = new Index;
this->storage->record(that);
that->keys.move_from(this->keys, HalfOrder(), HalfOrder());
that->children.move_from(this->children, HalfOrder(), HalfOrder() + 1);
k = this->keys.pop();
return that;
}
void insert_block(const K &k, BlockIdx v) {
unsigned pos = this->keys.insert(k);
this->children.insert(pos + 1, v);
}
const V *query(const K &k) const override {
// {left: key < index_key} {right: key >= index_key}
unsigned pos = this->keys.upper_bound(k);
const Block *block = this->storage->read(children[pos]);
return block->query(k);
}
LeafPos find(const K &k) const override {
unsigned pos = this->keys.upper_bound(k);
const Block *block = this->storage->read(children[pos]);
return block->find(k);
}
bool insert(const K &k, const V &v) override {
// {left: key < index_key} {right: key >= index_key}
unsigned pos = this->keys.upper_bound(k);
Block *block = this->storage->get(children[pos]);
bool result = block->insert(k, v);
if (!result) return false;
if (block->should_split()) {
K k;
Block *that = block->split(k);
insert_block(k, that->idx);
}
return true;
};
bool remove(const K &k) override {
unsigned pos = this->keys.upper_bound(k);
Block *block = this->storage->get(children[pos]);
bool result = block->remove(k);
if (!result) return false;
if (block->should_merge()) {
if (pos != 0) {
K split_key = this->keys[pos - 1];
Block *left = this->storage->get(children[pos - 1]);
if (left->may_borrow()) {
this->keys[pos - 1] = block->borrow_from_left(left, split_key);
return true;
}
}
if (pos != this->children.size - 1) {
K split_key = this->keys[pos];
Block *right = this->storage->get(children[pos + 1]);
if (right->may_borrow()) {
this->keys[pos] = block->borrow_from_right(right, split_key);
return true;
}
}
if (pos != 0) {
K split_key = this->keys[pos - 1];
Block *left = this->storage->get(children.remove(pos - 1));
block->merge_with_left(left, split_key);
delete left;
this->keys.remove(pos - 1);
return true;
}
if (pos != this->children.size - 1) {
K split_key = this->keys[pos];
Block *right = this->storage->get(children.remove(pos + 1));
block->merge_with_right(right, split_key);
delete right;
this->keys.remove(pos);
return true;
}
assert(false);
}
return true;
}
static constexpr unsigned Storage_Size() {
return Set<K, Order()>::Storage_Size() + Vector<BlockIdx, Order() + 1>::Storage_Size();
}
K borrow_from_left(Block *_left, const K &split_key) override {
Index *left = this->into_index(_left);
// TODO: we should verify that split_key is always the minimum
this->keys.insert(split_key);
// TODO: wish I were writing in Rust... therefore there'll be no copy overhead
K new_split_key = left->keys.pop();
this->children.move_insert_from(left->children, left->children.size - 1, 1, 0);
return new_split_key;
};
K borrow_from_right(Block *_right, const K &split_key) override {
Index *right = this->into_index(_right);
this->keys.insert(split_key);
K new_split_key = right->keys.remove(0);
this->children.move_insert_from(right->children, 0, 1, this->children.size);
return new_split_key;
};
void merge_with_left(Block *_left, const K &split_key) override {
Index *left = this->into_index(_left);
this->keys.insert(split_key);
this->keys.move_insert_from(left->keys, 0, left->keys.size, 0);
this->children.move_insert_from(left->children, 0, left->children.size, 0);
this->storage->deregister(left);
};
void merge_with_right(Block *_right, const K &split_key) override {
Index *right = this->into_index(_right);
this->keys.insert(split_key);
this->keys.move_insert_from(right->keys, 0, right->keys.size, this->keys.size);
this->children.move_insert_from(right->children, 0, right->children.size, this->children.size);
this->storage->deregister(right);
};
unsigned storage_size() const override { return Storage_Size(); }
/*
* Storage Mapping
* | 8 size | Order() K keys |
* | 8 size | Order()+1 BlockIdx children |
*/
void serialize(std::ostream &out) const override {
this->keys.serialize(out);
this->children.serialize(out);
};
void deserialize(std::istream &in) override {
this->keys.deserialize(in);
this->children.deserialize(in);
};
};
struct Leaf : public Block {
BlockIdx prev, next;
Vector<V, Order()> data;
Leaf() : Block(), prev(0), next(0) {}
constexpr bool is_leaf() const override { return true; }
const V *query(const K &k) const override {
LeafPos pos = this->find(k);
if (pos.first == 0) return nullptr;
return &this->data[pos.second];
}
LeafPos find(const K &k) const override {
unsigned pos = this->keys.lower_bound(k);
if (pos >= this->keys.size || this->keys[pos] != k)
return LeafPos(0, 0);
else
return LeafPos(this->idx, pos);
}
bool insert(const K &k, const V &v) override {
unsigned pos = this->keys.lower_bound(k);
if (pos < this->keys.size && this->keys[pos] == k) return false;
this->keys.insert(k);
this->data.insert(pos, v);
return true;
}
bool remove(const K &k) override {
unsigned pos = this->keys.lower_bound(k);
if (pos >= this->keys.size || this->keys[pos] != k)
return false;
this->keys.remove(pos);
this->data.remove(pos);
return true;
}
/* split :: Leaf -> (k, Leaf, Leaf)
* split (Leaf a) = [k, prev, next]
* this = prev, return = next
*/
Leaf *split(K &k) override {
assert(this->should_split());
Leaf *that = new Leaf;
this->storage->record(that);
if (this->next) {
Leaf* rr = this->into_leaf(this->storage->get(this->next));
rr->prev = that->idx;
that->next = rr->idx;
}
this->next = that->idx;
that->prev = this->idx;
that->keys.move_from(this->keys, HalfOrder(), HalfOrder());
that->data.move_from(this->data, HalfOrder(), HalfOrder());
k = that->keys[0];
return that;
}
K borrow_from_left(Block *_left, const K &) override {
Leaf *left = this->into_leaf(_left);
assert(this->prev == left->idx);
assert(left->next == this->idx);
this->keys.move_insert_from(left->keys, left->keys.size - 1, 1, 0);
this->data.move_insert_from(left->data, left->data.size - 1, 1, 0);
return this->keys[0];
};
K borrow_from_right(Block *_right, const K &) override {
Leaf *right = this->into_leaf(_right);
assert(this->next == right->idx);
assert(right->prev == this->idx);
this->keys.move_insert_from(right->keys, 0, 1, this->keys.size);
this->data.move_insert_from(right->data, 0, 1, this->data.size);
return right->keys[0];
};
void merge_with_left(Block *_left, const K &) override {
Leaf *left = this->into_leaf(_left);
assert(this->prev == left->idx);
assert(left->next == this->idx);
this->keys.move_insert_from(left->keys, 0, left->keys.size, 0);
this->data.move_insert_from(left->data, 0, left->data.size, 0);
this->prev = left->prev;
if (this->prev) {
Leaf* ll = this->into_leaf(this->storage->get(this->prev));
ll->next = this->idx;
}
this->storage->deregister(left);
};