@@ -366,7 +366,7 @@ where
366
366
let mut root = self . load_node ( self . root_addr ) ;
367
367
368
368
// Check if the key already exists in the root.
369
- if let Ok ( idx) = root. search ( & key) {
369
+ if let Ok ( idx) = root. search ( & key, self . memory ( ) ) {
370
370
// The key exists. Overwrite it and return the previous value.
371
371
let ( _, previous_value) = root. swap_entry ( idx, ( key, value) , self . memory ( ) ) ;
372
372
self . save_node ( & mut root) ;
@@ -409,7 +409,7 @@ where
409
409
assert ! ( !node. is_full( ) ) ;
410
410
411
411
// Look for the key in the node.
412
- match node. search ( & key) {
412
+ match node. search ( & key, self . memory ( ) ) {
413
413
Ok ( idx) => {
414
414
// The key is already in the node.
415
415
// Overwrite it and return the previous value.
@@ -442,7 +442,7 @@ where
442
442
443
443
if child. is_full ( ) {
444
444
// Check if the key already exists in the child.
445
- if let Ok ( idx) = child. search ( & key) {
445
+ if let Ok ( idx) = child. search ( & key, self . memory ( ) ) {
446
446
// The key exists. Overwrite it and return the previous value.
447
447
let ( _, previous_value) =
448
448
child. swap_entry ( idx, ( key, value) , self . memory ( ) ) ;
@@ -455,7 +455,7 @@ where
455
455
456
456
// The children have now changed. Search again for
457
457
// the child where we need to store the entry in.
458
- let idx = node. search ( & key) . unwrap_or_else ( |idx| idx) ;
458
+ let idx = node. search ( & key, self . memory ( ) ) . unwrap_or_else ( |idx| idx) ;
459
459
child = self . load_node ( node. child ( idx) ) ;
460
460
}
461
461
@@ -469,7 +469,7 @@ where
469
469
}
470
470
}
471
471
472
- /// Takes as input a nonfull internal `node` and index to its full child, then
472
+ /// Takes as input a non-full internal `node` and index to its full child, then
473
473
/// splits this child into two, adding an additional child to `node`.
474
474
///
475
475
/// Example:
@@ -535,7 +535,7 @@ where
535
535
{
536
536
let node = self . load_node ( node_addr) ;
537
537
// Look for the key in the current node.
538
- match node. search ( key) {
538
+ match node. search ( key, self . memory ( ) ) {
539
539
Ok ( idx) => Some ( f ( node, idx) ) , // Key found: apply `f`.
540
540
Err ( idx) => match node. node_type ( ) {
541
541
NodeType :: Leaf => None , // At a leaf: key not present.
@@ -652,7 +652,7 @@ where
652
652
653
653
match node. node_type ( ) {
654
654
NodeType :: Leaf => {
655
- match node. search ( key) {
655
+ match node. search ( key, self . memory ( ) ) {
656
656
Ok ( idx) => {
657
657
// Case 1: The node is a leaf node and the key exists in it.
658
658
// This is the simplest case. The key is removed from the leaf.
@@ -679,7 +679,7 @@ where
679
679
}
680
680
}
681
681
NodeType :: Internal => {
682
- match node. search ( key) {
682
+ match node. search ( key, self . memory ( ) ) {
683
683
Ok ( idx) => {
684
684
// Case 2: The node is an internal node and the key exists in it.
685
685
@@ -1310,10 +1310,40 @@ mod test {
1310
1310
}
1311
1311
}
1312
1312
1313
- macro_rules! verify_and_run {
1314
- ( $runner: ident, $K: ty, $V: ty) => { {
1313
+ /// Asserts that the associated `BOUND` for `$ty` is _not_ `Unbounded`.
1314
+ macro_rules! assert_bounded {
1315
+ ( $ty: ty) => {
1316
+ assert_ne!( <$ty>:: BOUND , StorableBound :: Unbounded , "Must be Bounded" ) ;
1317
+ } ;
1318
+ }
1319
+
1320
+ /// Asserts that the associated `BOUND` for `$ty` _is_ `Unbounded`.
1321
+ macro_rules! assert_unbounded {
1322
+ ( $ty: ty) => {
1323
+ assert_eq!( <$ty>:: BOUND , StorableBound :: Unbounded , "Must be Unbounded" ) ;
1324
+ } ;
1325
+ }
1326
+
1327
+ macro_rules! run_with_key {
1328
+ ( $runner: ident, $K: ty) => { {
1315
1329
verify_monotonic:: <$K>( ) ;
1316
- $runner:: <$K, $V>( ) ;
1330
+
1331
+ // Empty value.
1332
+ $runner:: <$K, ( ) >( ) ;
1333
+
1334
+ // Bounded values.
1335
+ assert_bounded!( u32 ) ;
1336
+ $runner:: <$K, u32 >( ) ;
1337
+
1338
+ assert_bounded!( Blob <20 >) ;
1339
+ $runner:: <$K, Blob <20 >>( ) ;
1340
+
1341
+ // Unbounded values.
1342
+ assert_unbounded!( MonotonicVec32 ) ;
1343
+ $runner:: <$K, MonotonicVec32 >( ) ;
1344
+
1345
+ assert_unbounded!( MonotonicString32 ) ;
1346
+ $runner:: <$K, MonotonicString32 >( ) ;
1317
1347
} } ;
1318
1348
}
1319
1349
@@ -1322,37 +1352,19 @@ mod test {
1322
1352
( $name: ident, $runner: ident) => {
1323
1353
#[ test]
1324
1354
fn $name( ) {
1325
- use StorableBound :: Unbounded ;
1326
-
1327
- // Set, empty value, bounded.
1328
- {
1329
- type Value = ( ) ;
1330
- assert_ne!( <Value >:: BOUND , Unbounded , "Must be Bounded" ) ;
1331
- verify_and_run!( $runner, u32 , Value ) ;
1332
- verify_and_run!( $runner, Blob <10 >, Value ) ;
1333
- verify_and_run!( $runner, MonotonicVec32 , Value ) ;
1334
- verify_and_run!( $runner, MonotonicString32 , Value ) ;
1335
- }
1355
+ // Bounded keys.
1356
+ assert_bounded!( u32 ) ;
1357
+ run_with_key!( $runner, u32 ) ;
1336
1358
1337
- // Map, bounded value.
1338
- {
1339
- type Value = u32 ;
1340
- assert_ne!( Value :: BOUND , Unbounded , "Must be Bounded" ) ;
1341
- verify_and_run!( $runner, u32 , Value ) ;
1342
- verify_and_run!( $runner, Blob <10 >, Value ) ;
1343
- verify_and_run!( $runner, MonotonicVec32 , Value ) ;
1344
- verify_and_run!( $runner, MonotonicString32 , Value ) ;
1345
- }
1359
+ assert_bounded!( Blob <10 >) ;
1360
+ run_with_key!( $runner, Blob <10 >) ;
1346
1361
1347
- // Map, unbounded value.
1348
- {
1349
- type Value = MonotonicVec32 ;
1350
- assert_eq!( Value :: BOUND , Unbounded , "Must be Unbounded" ) ;
1351
- verify_and_run!( $runner, u32 , Value ) ;
1352
- verify_and_run!( $runner, Blob <10 >, Value ) ;
1353
- verify_and_run!( $runner, MonotonicVec32 , Value ) ;
1354
- verify_and_run!( $runner, MonotonicString32 , Value ) ;
1355
- }
1362
+ // Unbounded keys.
1363
+ assert_unbounded!( MonotonicVec32 ) ;
1364
+ run_with_key!( $runner, MonotonicVec32 ) ;
1365
+
1366
+ assert_unbounded!( MonotonicString32 ) ;
1367
+ run_with_key!( $runner, MonotonicString32 ) ;
1356
1368
}
1357
1369
} ;
1358
1370
}
@@ -1440,7 +1452,7 @@ mod test {
1440
1452
assert ! ( right_child. is_full( ) ) ;
1441
1453
let median_index = right_child. entries_len ( ) / 2 ;
1442
1454
let median_key = key ( 12 ) ;
1443
- assert_eq ! ( right_child. key( median_index) , & median_key) ;
1455
+ assert_eq ! ( right_child. key( median_index, btree . memory ( ) ) , & median_key) ;
1444
1456
1445
1457
// Overwrite the value of the median key.
1446
1458
assert_eq ! ( btree. insert( median_key. clone( ) , value( 123 ) ) , Some ( value( 0 ) ) ) ;
@@ -3089,7 +3101,7 @@ mod test {
3089
3101
// [0, 1, 2, 3, 4, 5] [7, 8, 9, 10, 11]
3090
3102
let root = btree. load_node ( btree. root_addr ) ;
3091
3103
assert_eq ! ( root. node_type( ) , NodeType :: Internal ) ;
3092
- assert_eq ! ( root. keys( ) , vec![ vec![ 6 ; 10_000 ] ] ) ;
3104
+ assert_eq ! ( root. keys( btree . memory ( ) ) , vec![ vec![ 6 ; 10_000 ] ] ) ;
3093
3105
assert_eq ! ( root. children_len( ) , 2 ) ;
3094
3106
3095
3107
// Remove the element in the root.
@@ -3101,7 +3113,7 @@ mod test {
3101
3113
// [0, 1, 2, 3, 4] [7, 8, 9, 10, 11]
3102
3114
let root = btree. load_node ( btree. root_addr ) ;
3103
3115
assert_eq ! ( root. node_type( ) , NodeType :: Internal ) ;
3104
- assert_eq ! ( root. keys( ) , vec![ vec![ 5 ; 10_000 ] ] ) ;
3116
+ assert_eq ! ( root. keys( btree . memory ( ) ) , vec![ vec![ 5 ; 10_000 ] ] ) ;
3105
3117
assert_eq ! ( root. children_len( ) , 2 ) ;
3106
3118
3107
3119
// Remove the element in the root. This triggers the case where the root
@@ -3113,7 +3125,7 @@ mod test {
3113
3125
let root = btree. load_node ( btree. root_addr ) ;
3114
3126
assert_eq ! ( root. node_type( ) , NodeType :: Leaf ) ;
3115
3127
assert_eq ! (
3116
- root. keys( ) ,
3128
+ root. keys( btree . memory ( ) ) ,
3117
3129
vec![
3118
3130
vec![ 0 ; 10_000 ] ,
3119
3131
vec![ 1 ; 10_000 ] ,
0 commit comments