-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.rs
867 lines (794 loc) · 27.8 KB
/
mod.rs
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
use std::cmp;
use std::cmp::Ordering;
use std::fmt;
use std::mem;
/// A B+ tree node with height zero.
/// Leaf nodes store values.
#[derive(Debug)]
struct LeafNode {
keys: Vec<i32>,
vals: Vec<i32>,
}
/// A B+ tree node with height greater than zero.
/// Internal nodes store children.
#[derive(Debug)]
struct InternalNode {
keys: Vec<i32>,
children: Vec<Box<Node>>,
}
/// A B+ tree is defined by its root node and the
/// min and max bounds on node size.
#[derive(Debug)]
pub struct BTree {
root: Option<Box<Node>>,
max: usize,
min: usize,
}
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
enum Sibling {
Less,
Greater,
}
/// Outcomes of an insert operation.
enum InsertResult {
/// The pair (key, old) was replaced by (key, new).
/// Node splitting cannot occur at value replacement.
Replace(i32),
/// Pair (key, new) insertion caused the node to split.
Split(i32, Box<Node>),
/// Pair (key, new) insertion did not cause the node to split.
None,
}
/// Outcomes of a remove operation.
enum RemoveResult {
/// The pair (key, val) was removed. The node is too small.
Shrink(i32),
/// The pair (key, val) was removed. The node is not too small.
NoShrink(i32),
/// No pair was removed.
None,
}
impl BTree {
/// Makes a new empty BTree.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// use yaar::bplus::BTree;
///
/// let mut map = BTree::new(16);
///
/// // entries can now be inserted into the empty map
/// map.insert(1, 1);
/// ```
pub fn new(max: usize) -> BTree {
BTree {
root: None,
max: max,
min: max / 2,
}
}
/// Returns the value corresponding to the key.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// use yaar::bplus::BTree;
///
/// let mut map = BTree::new(16);
/// map.insert(1, 1);
/// assert_eq!(map.get(1), Some(1));
/// assert_eq!(map.get(2), None);
/// ```
pub fn get(&self, key: i32) -> Option<i32> {
match self.root {
None => None,
Some(ref node) => node.get(key),
}
}
/// Returns true if the map contains no elements.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// use yaar::bplus::BTree;
///
/// let mut a = BTree::new(16);
/// assert!(a.is_empty());
/// a.insert(1, 1);
/// assert!(!a.is_empty());
/// ```
pub fn is_empty(&self) -> bool {
self.root.is_none()
}
/// Inserts a key-value pair into the map.
///
/// If the map did not have this key present, `None` is returned.
///
/// If the map did have this key present, the value is updated, and the old
/// value is returned.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// use yaar::bplus::BTree;
///
/// let mut map = BTree::new(16);
/// assert_eq!(map.insert(37, 1), None);
/// assert_eq!(map.is_empty(), false);
///
/// map.insert(37, 2);
/// assert_eq!(map.insert(37, 3), Some(2));
/// ```
pub fn insert(&mut self, key: i32, value: i32) -> Option<i32> {
if self.root.is_none() {
self.root = LeafNode::new_leaf(key, value);
None
} else {
let max = self.max;
let result = self.mut_root().insert(key, value, max);
match result {
// grow the height of the tree if necessary
InsertResult::Split(key, child) => {
let children = vec![self.take_root(), child];
self.root = InternalNode::new_root(key, children);
None
}
InsertResult::Replace(value) => Some(value),
InsertResult::None => None,
}
}
}
/// Removes a key from the map, returning the value at the key if the key
/// was previously in the map.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// use yaar::bplus::BTree;
///
/// let mut map = BTree::new(16);
/// map.insert(1, 1);
/// assert_eq!(map.remove(1), Some(1));
/// assert_eq!(map.remove(1), None);
/// ```
pub fn remove(&mut self, key: i32) -> Option<i32> {
match self.root {
None => None,
Some(_) => {
let min = self.min;
let prev = self.mut_root().remove(key, min);
if self.ref_root().keys().is_empty() {
// shrink the height of the tree if necessary
self.root = self.take_root().shrink();
}
prev.result()
}
}
}
fn ref_root(&self) -> &Box<Node> {
self.root.as_ref().unwrap()
}
fn mut_root(&mut self) -> &mut Box<Node> {
self.root.as_mut().unwrap()
}
fn take_root(&mut self) -> Box<Node> {
self.root.take().unwrap()
}
}
trait Node: fmt::Debug {
/// Returns the value (if exists) corresponding to the key.
fn get(&self, key: i32) -> Option<i32>;
/// Insert a (key, value) pair into the tree.
fn insert(&mut self, key: i32, value: i32, max: usize) -> InsertResult;
/// Remove a (key, value) pair from the tree.
fn remove(&mut self, key: i32, min: usize) -> RemoveResult;
/// Split the node if the number of keys is greater than max.
fn split(&mut self, max: usize) -> InsertResult;
/// Move some elements from a sibling node onto this node.
/// Return value is the new partition between this node and sibling node.
/// Return value will replace an existing key in the parent.
fn shuffle(&mut self, sibling: &mut Box<Node>, pkey: i32, ord: Sibling, min: usize) -> i32;
/// Move all elements from this node to a sibling. This node will be deleted.
fn collapse(&mut self, sibling: &mut Box<Node>, pkey: i32, ord: Sibling);
/// Reduce the height of a tree. Used only by the root of the tree.
fn shrink(&mut self) -> Option<Box<Node>>;
/// Accessor function for the keys of a node
fn keys(&self) -> &[i32];
/// Returns true if the number of keys is less than min.
fn needs_merge(&self, min: usize) -> bool {
self.keys().len() < min
}
/// Assert this value is a leaf node or panic on internal node
fn as_leaf(&mut self) -> &mut LeafNode;
/// Assert this value is an internal node or panic on leaf node
fn as_internal(&mut self) -> &mut InternalNode;
}
impl Node for LeafNode {
/// Accessor function for the keys of a node
fn keys(&self) -> &[i32] {
&self.keys
}
/// Assert this value is a leaf node or panic on internal node
fn as_leaf(&mut self) -> &mut LeafNode {
self
}
/// Assert this value is an internal node or panic on leaf node
fn as_internal(&mut self) -> &mut InternalNode {
panic!("attempt to convert leaf node into internal node");
}
/// Reduce the height of a tree. Used only by the root of the tree.
/// Shrinking a leaf node generates an empty tree.
fn shrink(&mut self) -> Option<Box<Node>> {
debug_assert!(self.keys().is_empty());
None
}
/// Returns the value (if exists) corresponding to the key.
fn get(&self, key: i32) -> Option<i32> {
let position = self.keys.binary_search(&key);
match position {
Ok(index) => Some(self.vals[index]),
Err(_) => None,
}
}
/// Insert a (key, value) pair into the tree.
fn insert(&mut self, key: i32, value: i32, max: usize) -> InsertResult {
let position = self.keys.binary_search(&key);
let prev = match position {
Ok(index) => Some(self.vals[index]),
Err(_) => None,
};
match position {
Ok(index) => {
self.vals[index] = value;
}
Err(index) => {
self.keys.insert(index, value);
self.vals.insert(index, value);
}
};
match prev {
Some(value) => InsertResult::Replace(value),
None => self.split(max),
}
}
/// Remove a (key, value) pair from the tree.
fn remove(&mut self, key: i32, min: usize) -> RemoveResult {
let position = self.keys.binary_search(&key);
let prev = match position {
Ok(index) => {
self.keys.remove(index);
Some(self.vals.remove(index))
}
Err(_) => None,
};
match prev {
Some(val) => RemoveResult::generate(self, min, val),
None => RemoveResult::None,
}
}
/// Split the node if the number of keys is greater than max.
fn split(&mut self, max: usize) -> InsertResult {
if self.keys.len() > max {
let partition = self.keys.len() / 2;
let newkeys = self.keys.split_off(partition);
let newvals = self.vals.split_off(partition);
InsertResult::Split(newkeys[0],
Box::new(LeafNode {
keys: newkeys,
vals: newvals,
}))
} else {
InsertResult::None
}
}
/// Move some elements from a sibling node onto this node.
/// Return value is the new partition between this node and sibling node.
/// Return value will replace an existing key in the parent.
#[allow(unused_variables)]
fn shuffle(&mut self, sibling: &mut Box<Node>, pkey: i32, ord: Sibling, min: usize) -> i32 {
let sibling = sibling.as_leaf();
let sibling_len = sibling.keys.len();
let count = cmp::max(1, (sibling_len - min) / 2);
Node::vec_shuffle(&mut sibling.keys, &mut self.keys, ord, count);
Node::vec_shuffle(&mut sibling.vals, &mut self.vals, ord, count);
LeafNode::post_shuffle_get(&sibling.keys, ord)
}
/// Move all elements from this node to a sibling. This node will be deleted.
#[allow(unused_variables)]
fn collapse(&mut self, sibling: &mut Box<Node>, pkey: i32, ord: Sibling) {
let sibling = sibling.as_leaf();
Node::vec_collapse(&mut sibling.keys, &mut self.keys, ord);
Node::vec_collapse(&mut sibling.vals, &mut self.vals, ord);
}
}
impl Node for InternalNode {
/// Accessor function for the keys of a node
fn keys(&self) -> &[i32] {
&self.keys
}
/// Assert this value is a leaf node or panic on internal node
fn as_leaf(&mut self) -> &mut LeafNode {
panic!("attempt to convert internal node into leaf node");
}
/// Assert this value is an internal node or panic on leaf node
fn as_internal(&mut self) -> &mut InternalNode {
self
}
/// Reduce the height of a tree. Used only by the root of the tree.
/// Shrinking an internal node replaces the node with its only child.
fn shrink(&mut self) -> Option<Box<Node>> {
debug_assert!(self.keys().is_empty());
Some(self.children.remove(0))
}
/// Returns the value (if exists) corresponding to the key.
fn get(&self, key: i32) -> Option<i32> {
let position = self.keys.binary_search(&key);
let index = InternalNode::index(position);
self.children[index].get(key)
}
/// Insert a (key, value) pair into the tree.
fn insert(&mut self, key: i32, value: i32, max: usize) -> InsertResult {
let position = self.keys.binary_search(&key);
let index = InternalNode::index(position);
let recursive = self.children[index].insert(key, value, max);
match recursive {
InsertResult::Split(key, newchild) => {
let index = InternalNode::index(self.keys.binary_search(&key));
self.keys.insert(index, key);
self.children.insert(index + 1, newchild);
self.split(max)
}
_ => recursive,
}
}
/// Remove a (key, value) pair from the tree.
fn remove(&mut self, key: i32, min: usize) -> RemoveResult {
let position = self.keys.binary_search(&key);
let index = InternalNode::index(position);
let recursive = self.children[index].remove(key, min);
match recursive {
RemoveResult::Shrink(val) => {
self.merge_child(InternalNode::index(position), min);
RemoveResult::generate(self, min, val)
}
_ => recursive,
}
}
/// Split the node if the number of keys is greater than max.
fn split(&mut self, max: usize) -> InsertResult {
if self.keys.len() > max {
let partition = self.keys.len() / 2;
let mut newkeys = self.keys.split_off(partition);
let newchildren = self.children.split_off(partition + 1);
let newkey = newkeys.remove(0);
InsertResult::Split(newkey,
Box::new(InternalNode {
keys: newkeys,
children: newchildren,
}))
} else {
InsertResult::None
}
}
/// Move some elements from a sibling node onto this node.
/// Return value is the new partition between this node and sibling node.
/// Return value will replace an existing key in the parent.
fn shuffle(&mut self, sibling: &mut Box<Node>, pkey: i32, ord: Sibling, min: usize) -> i32 {
let sibling = sibling.as_internal();
let sibling_len = sibling.keys.len();
let count = cmp::max(1, (sibling_len - min) / 2);
InternalNode::pre_shuffle(&mut sibling.keys, pkey, ord);
Node::vec_shuffle(&mut sibling.keys, &mut self.keys, ord, count);
Node::vec_shuffle(&mut sibling.children, &mut self.children, ord, count);
InternalNode::post_shuffle_take(&mut sibling.keys, ord)
}
/// Move all elements from this node to a sibling. This node will be deleted.
fn collapse(&mut self, sibling: &mut Box<Node>, pkey: i32, ord: Sibling) {
let sibling = sibling.as_internal();
Node::vec_collapse_with_middle(&mut sibling.keys, &mut self.keys, pkey, ord);
Node::vec_collapse(&mut sibling.children, &mut self.children, ord);
}
}
impl LeafNode {
fn new_leaf(key: i32, value: i32) -> Option<Box<Node>> {
Some(Box::new(LeafNode {
keys: vec![key],
vals: vec![value],
}))
}
/// Copy the new partition key from a child node. The key
/// will be inserted into the parent of this node. The
/// insertion is performed by InternalNode::set_parent_key().
fn post_shuffle_get(sibling: &[i32], ord: Sibling) -> i32 {
match ord {
Sibling::Less => {
let len = sibling.len();
sibling[len - 1]
}
Sibling::Greater => sibling[0],
}
}
}
impl InternalNode {
fn new_root(key: i32, children: Vec<Box<Node>>) -> Option<Box<Node>> {
Some(Box::new(InternalNode {
keys: vec![key],
children: children,
}))
}
/// Using a binary search of the keys as input, return the index of
/// the child node as output. A key `k` divides the key space in half:
/// values less than `k` and values greater than or equal to `k`.
fn index(position: Result<usize, usize>) -> usize {
match position {
Ok(index) => index + 1,
Err(index) => index,
}
}
/// Given an index and the direction of a sibling return
/// the index of the sibling.
fn sibling(index: usize, ord: Sibling) -> usize {
match ord {
Sibling::Less => index - 1,
Sibling::Greater => index + 1,
}
}
/// Inspect the left and right siblings of a node
/// and return the larger sibling. Select the left
/// sibling in case of a tie.
fn merge_select_sibling(&self, index: usize) -> Sibling {
let leftsize = if index > 0 {
self.children[index - 1].keys().len()
} else {
0
};
let rightsize = if index < self.children.len() - 1 {
self.children[index + 1].keys().len()
} else {
0
};
if leftsize == 0 && rightsize == 0 {
panic!("left and right children are empty");
}
match rightsize.cmp(&leftsize) {
Ordering::Less | Ordering::Equal => Sibling::Less,
Ordering::Greater => Sibling::Greater,
}
}
/// The number of keys in a child node is less than
/// the minimum. Either shuffle elements from a sibling
/// node or collapse this node into a sibling.
fn merge_child(&mut self, index: usize, min: usize) {
let ord = self.merge_select_sibling(index);
let sib_index = InternalNode::sibling(index, ord);
let sib_len = self.children[sib_index].keys().len();
if sib_len > min {
self.merge_shuffle(index, ord, min);
} else {
self.merge_collapse(index, ord);
}
}
/// Move some elements from a sibling node onto this node.
fn merge_shuffle(&mut self, index: usize, ord: Sibling, min: usize) {
let pkey = self.get_parent_key(index, ord);
let ckey = {
let (mut child, mut sibling) = self.partition(index, ord);
child.shuffle(sibling, pkey, ord, min)
};
self.set_parent_key(ckey, index, ord);
}
/// Move all elements from this node to a sibling. This node will be deleted.
fn merge_collapse(&mut self, index: usize, ord: Sibling) {
let pkey = self.get_parent_key(index, ord);
{
let (mut child, mut sibling) = self.partition(index, ord);
child.collapse(sibling, pkey, ord);
}
self.drop_parent_key(index, ord);
self.children.remove(index);
}
/// Get the partition key associated with a child node to be merged.
/// The partition key is used when merging internal nodes and is
/// unused when merging leaf nodes.
fn get_parent_key(&self, index: usize, ord: Sibling) -> i32 {
match ord {
Sibling::Less => self.keys[index - 1],
Sibling::Greater => self.keys[index],
}
}
/// Replace a key in the parent node at completion of a shuffle operation.
/// This is the second half of a swap operation. The first half is
/// performed by InternalNode::pre_shuffle().
fn set_parent_key(&mut self, key: i32, index: usize, ord: Sibling) {
match ord {
Sibling::Less => {
self.keys[index - 1] = key;
}
Sibling::Greater => {
self.keys[index] = key;
}
}
}
/// Eliminate a key associated with a child node that is deleted.
/// The child node is deleted when it has too few elements and
/// its left and right siblings do not have extra elements to donate.
fn drop_parent_key(&mut self, index: usize, ord: Sibling) {
match ord {
Sibling::Less => {
self.keys.remove(index - 1);
}
Sibling::Greater => {
self.keys.remove(index);
}
}
}
/// Return a pair of references - the child node that is to
/// be merged and the sibling of the child node.
fn partition(&mut self, index: usize, ord: Sibling) -> (&mut Box<Node>, &mut Box<Node>) {
match ord {
Sibling::Less => {
let (left, right) = self.children.split_at_mut(index);
let index = left.len() - 1;
(&mut right[0], &mut left[index])
}
Sibling::Greater => {
let (left, right) = self.children.split_at_mut(index + 1);
let index = left.len() - 1;
(&mut left[index], &mut right[0])
}
}
}
/// Copy a key from the parent node into a child node prior to shuffle operation.
/// This is the first half of a swap operation. The second half is
/// performed by InternalNode::set_parent_key().
fn pre_shuffle(sibling: &mut Vec<i32>, key: i32, ord: Sibling) {
match ord {
Sibling::Less => {
sibling.push(key);
}
Sibling::Greater => {
sibling.insert(0, key);
}
}
}
/// Extract the new partition key from a child node. The key
/// will be inserted into the parent of this node. The
/// insertion is performed by InternalNode::set_parent_key().
fn post_shuffle_take(sibling: &mut Vec<i32>, ord: Sibling) -> i32 {
match ord {
Sibling::Less => sibling.pop().unwrap(),
Sibling::Greater => sibling.remove(0),
}
}
}
impl Node {
/// Move some elements from the source vector to the destination vector.
/// The Sibling direction determines whether elements are moved
/// from the head or tail of the source vector.
fn vec_shuffle<T>(src: &mut Vec<T>, dest: &mut Vec<T>, ord: Sibling, count: usize) {
match ord {
Sibling::Less => {
let position = src.len() - count;
let mut newdest = src.split_off(position);
newdest.append(dest);
*dest = newdest;
}
Sibling::Greater => {
let newsrc = src.split_off(count);
dest.append(src);
*src = newsrc;
}
}
}
/// Move all elements from the child vector to the sibling vector.
/// The Sibling direction determines whether elements are moved
/// onto the head or tail of the sibling vector.
fn vec_collapse<T>(sibling: &mut Vec<T>, child: &mut Vec<T>, ord: Sibling) {
match ord {
Sibling::Less => {
sibling.append(child);
}
Sibling::Greater => {
child.append(sibling);
mem::swap(child, sibling);
}
}
}
/// Move all elements from the child vector to the sibling vector.
/// A partition element is inserted between the old and new elements.
fn vec_collapse_with_middle<T>(sibling: &mut Vec<T>,
child: &mut Vec<T>,
middle: T,
ord: Sibling) {
match ord {
Sibling::Less => {
sibling.push(middle);
sibling.append(child);
}
Sibling::Greater => {
child.push(middle);
child.append(sibling);
mem::swap(child, sibling);
}
}
}
}
impl RemoveResult {
/// Extract the value stored by the RemoveResult
fn result(&self) -> Option<i32> {
match *self {
RemoveResult::Shrink(val) |
RemoveResult::NoShrink(val) => Some(val),
RemoveResult::None => None,
}
}
/// Generate a RemoveResult that stores a value
fn generate(node: &Node, min: usize, val: i32) -> RemoveResult {
if node.needs_merge(min) {
RemoveResult::Shrink(val)
} else {
RemoveResult::NoShrink(val)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use super::Node;
use super::InternalNode;
use super::LeafNode;
use super::Sibling;
#[test]
fn basic_construction() {
let mut tree = BTree::new(4);
assert_eq!(tree.get(0), None);
assert_eq!(tree.insert(0, 1), None);
assert_eq!(tree.insert(0, 2), Some(1));
assert_eq!(tree.get(0), Some(2));
}
#[test]
fn insert_sequence() {
let mut tree = BTree::new(4);
for i in 0..256 {
assert_eq!(tree.insert(i, i), None);
assert_eq!(tree.get(i), Some(i));
}
}
#[test]
fn vec_shuffle() {
let mut vec1 = vec![1, 2, 3];
let mut vec2 = vec![4, 5, 6];
Node::vec_shuffle(&mut vec1, &mut vec2, Sibling::Less, 2);
assert_eq!(vec1, vec![1]);
assert_eq!(vec2, vec![2, 3, 4, 5, 6]);
let mut vec3 = vec![1, 2, 3];
let mut vec4 = vec![4, 5, 6];
Node::vec_shuffle(&mut vec4, &mut vec3, Sibling::Greater, 2);
assert_eq!(vec3, vec![1, 2, 3, 4, 5]);
assert_eq!(vec4, vec![6]);
}
fn testnode() -> Box<Node> {
LeafNode::new_leaf(0, 0).unwrap()
}
#[test]
fn redistribute_children_left() {
let left = Box::new(InternalNode {
keys: vec![1, 2, 3, 4, 5, 6],
children: vec![testnode(), testnode(), testnode(), testnode(), testnode(), testnode(),
testnode()],
});
let right = Box::new(InternalNode {
keys: vec![70],
children: vec![testnode(), testnode()],
});
let mut parent = InternalNode {
keys: vec![50],
children: vec![left, right],
};
parent.merge_shuffle(1, Sibling::Less, 2);
assert_eq!(parent.keys(), [5]);
assert_eq!(parent.children[0].keys(), [1, 2, 3, 4]);
assert_eq!(parent.children[1].keys(), [6, 50, 70]);
}
#[test]
fn redistribute_children_right() {
let left = Box::new(InternalNode {
keys: vec![15],
children: vec![testnode(), testnode()],
});
let right = Box::new(InternalNode {
keys: vec![20, 30, 40, 50, 60, 70],
children: vec![testnode(), testnode(), testnode(), testnode(), testnode(), testnode(),
testnode()],
});
let mut parent = InternalNode {
keys: vec![18],
children: vec![left, right],
};
parent.merge_shuffle(0, Sibling::Greater, 2);
assert_eq!(parent.keys(), [30]);
assert_eq!(parent.children[0].keys(), [15, 18, 20]);
assert_eq!(parent.children[1].keys(), [40, 50, 60, 70]);
}
#[test]
fn collapse_children_left() {
let left = Box::new(InternalNode {
keys: vec![1, 2, 3],
children: vec![testnode(), testnode(), testnode(), testnode()],
});
let right = Box::new(InternalNode {
keys: vec![70],
children: vec![testnode(), testnode()],
});
let mut parent = InternalNode {
keys: vec![50],
children: vec![left, right],
};
parent.merge_collapse(1, Sibling::Less);
assert_eq!(parent.keys(), []);
assert_eq!(parent.children[0].keys(), [1, 2, 3, 50, 70]);
}
#[test]
fn collapse_children_right() {
let left = Box::new(InternalNode {
keys: vec![1],
children: vec![testnode(), testnode()],
});
let right = Box::new(InternalNode {
keys: vec![70, 80, 90],
children: vec![testnode(), testnode(), testnode(), testnode()],
});
let mut parent = InternalNode {
keys: vec![50],
children: vec![left, right],
};
parent.merge_collapse(0, Sibling::Greater);
assert_eq!(parent.keys(), []);
assert_eq!(parent.children[0].keys(), [1, 50, 70, 80, 90]);
}
#[test]
fn remove() {
let mut tree = BTree::new(4);
for i in 0..256 {
assert_eq!(tree.insert(i, i), None);
}
for i in 0..256 {
assert_eq!(tree.remove(i), Some(i));
assert_eq!(tree.remove(i), None);
}
}
#[test]
fn remove_reverse() {
let mut tree = BTree::new(4);
for i in 0..256 {
assert_eq!(tree.insert(i, i), None);
}
for i in (0..256).rev() {
assert_eq!(tree.remove(i), Some(i));
assert_eq!(tree.remove(i), None);
}
}
#[test]
fn remove_even() {
let mut tree = BTree::new(4);
for i in 0..256 {
assert_eq!(tree.insert(i, i), None);
}
for i in 0..128 {
assert_eq!(tree.remove(i * 2), Some(i * 2));
assert_eq!(tree.remove(i * 2), None);
assert_eq!(tree.get(i * 2 + 1), Some(i * 2 + 1));
}
}
}