Skip to content

Commit 9206280

Browse files
authored
Merge ad71aac into 970d0a6
2 parents 970d0a6 + ad71aac commit 9206280

File tree

6 files changed

+918
-214
lines changed

6 files changed

+918
-214
lines changed

benchmarks/src/btreemap.rs

Lines changed: 501 additions & 24 deletions
Large diffs are not rendered by default.

src/btreemap.rs

Lines changed: 56 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ where
366366
let mut root = self.load_node(self.root_addr);
367367

368368
// 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()) {
370370
// The key exists. Overwrite it and return the previous value.
371371
let (_, previous_value) = root.swap_entry(idx, (key, value), self.memory());
372372
self.save_node(&mut root);
@@ -409,7 +409,7 @@ where
409409
assert!(!node.is_full());
410410

411411
// Look for the key in the node.
412-
match node.search(&key) {
412+
match node.search(&key, self.memory()) {
413413
Ok(idx) => {
414414
// The key is already in the node.
415415
// Overwrite it and return the previous value.
@@ -442,7 +442,7 @@ where
442442

443443
if child.is_full() {
444444
// 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()) {
446446
// The key exists. Overwrite it and return the previous value.
447447
let (_, previous_value) =
448448
child.swap_entry(idx, (key, value), self.memory());
@@ -455,7 +455,7 @@ where
455455

456456
// The children have now changed. Search again for
457457
// 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);
459459
child = self.load_node(node.child(idx));
460460
}
461461

@@ -469,7 +469,7 @@ where
469469
}
470470
}
471471

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
473473
/// splits this child into two, adding an additional child to `node`.
474474
///
475475
/// Example:
@@ -535,7 +535,7 @@ where
535535
{
536536
let node = self.load_node(node_addr);
537537
// Look for the key in the current node.
538-
match node.search(key) {
538+
match node.search(key, self.memory()) {
539539
Ok(idx) => Some(f(node, idx)), // Key found: apply `f`.
540540
Err(idx) => match node.node_type() {
541541
NodeType::Leaf => None, // At a leaf: key not present.
@@ -652,7 +652,7 @@ where
652652

653653
match node.node_type() {
654654
NodeType::Leaf => {
655-
match node.search(key) {
655+
match node.search(key, self.memory()) {
656656
Ok(idx) => {
657657
// Case 1: The node is a leaf node and the key exists in it.
658658
// This is the simplest case. The key is removed from the leaf.
@@ -679,7 +679,7 @@ where
679679
}
680680
}
681681
NodeType::Internal => {
682-
match node.search(key) {
682+
match node.search(key, self.memory()) {
683683
Ok(idx) => {
684684
// Case 2: The node is an internal node and the key exists in it.
685685

@@ -1310,10 +1310,40 @@ mod test {
13101310
}
13111311
}
13121312

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) => {{
13151329
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>();
13171347
}};
13181348
}
13191349

@@ -1322,37 +1352,19 @@ mod test {
13221352
($name:ident, $runner:ident) => {
13231353
#[test]
13241354
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);
13361358

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>);
13461361

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);
13561368
}
13571369
};
13581370
}
@@ -1440,7 +1452,7 @@ mod test {
14401452
assert!(right_child.is_full());
14411453
let median_index = right_child.entries_len() / 2;
14421454
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);
14441456

14451457
// Overwrite the value of the median key.
14461458
assert_eq!(btree.insert(median_key.clone(), value(123)), Some(value(0)));
@@ -3089,7 +3101,7 @@ mod test {
30893101
// [0, 1, 2, 3, 4, 5] [7, 8, 9, 10, 11]
30903102
let root = btree.load_node(btree.root_addr);
30913103
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]]);
30933105
assert_eq!(root.children_len(), 2);
30943106

30953107
// Remove the element in the root.
@@ -3101,7 +3113,7 @@ mod test {
31013113
// [0, 1, 2, 3, 4] [7, 8, 9, 10, 11]
31023114
let root = btree.load_node(btree.root_addr);
31033115
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]]);
31053117
assert_eq!(root.children_len(), 2);
31063118

31073119
// Remove the element in the root. This triggers the case where the root
@@ -3113,7 +3125,7 @@ mod test {
31133125
let root = btree.load_node(btree.root_addr);
31143126
assert_eq!(root.node_type(), NodeType::Leaf);
31153127
assert_eq!(
3116-
root.keys(),
3128+
root.keys(btree.memory()),
31173129
vec![
31183130
vec![0; 10_000],
31193131
vec![1; 10_000],

src/btreemap/iter.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ where
9494
Bound::Included(key) | Bound::Excluded(key) => {
9595
let mut node = self.map.load_node(self.map.root_addr);
9696
loop {
97-
match node.search(key) {
97+
match node.search(key, self.map.memory()) {
9898
Ok(idx) => {
9999
if let Bound::Included(_) = self.range.start_bound() {
100100
// We found the key exactly matching the left bound.
@@ -115,7 +115,7 @@ where
115115
};
116116

117117
if idx + 1 < node.entries_len()
118-
&& self.range.contains(node.key(idx + 1))
118+
&& self.range.contains(node.key(idx + 1, self.map.memory()))
119119
{
120120
self.forward_cursors.push(Cursor::Node {
121121
node,
@@ -152,7 +152,9 @@ where
152152
NodeType::Leaf => None,
153153
};
154154

155-
if idx < node.entries_len() && self.range.contains(node.key(idx)) {
155+
if idx < node.entries_len()
156+
&& self.range.contains(node.key(idx, self.map.memory()))
157+
{
156158
self.forward_cursors.push(Cursor::Node {
157159
node,
158160
next: Index::Entry(idx),
@@ -188,7 +190,7 @@ where
188190
Bound::Included(key) | Bound::Excluded(key) => {
189191
let mut node = self.map.load_node(self.map.root_addr);
190192
loop {
191-
match node.search(key) {
193+
match node.search(key, self.map.memory()) {
192194
Ok(idx) => {
193195
if let Bound::Included(_) = self.range.end_bound() {
194196
// We found the key exactly matching the right bound.
@@ -208,7 +210,9 @@ where
208210
NodeType::Leaf => None,
209211
};
210212

211-
if idx > 0 && self.range.contains(node.key(idx - 1)) {
213+
if idx > 0
214+
&& self.range.contains(node.key(idx - 1, self.map.memory()))
215+
{
212216
self.backward_cursors.push(Cursor::Node {
213217
node,
214218
next: Index::Entry(idx - 1),
@@ -243,7 +247,8 @@ where
243247
NodeType::Leaf => None,
244248
};
245249

246-
if idx > 0 && self.range.contains(node.key(idx - 1)) {
250+
if idx > 0 && self.range.contains(node.key(idx - 1, self.map.memory()))
251+
{
247252
self.backward_cursors.push(Cursor::Node {
248253
node,
249254
next: Index::Entry(idx - 1),
@@ -320,15 +325,15 @@ where
320325
next: Index::Entry(entry_idx),
321326
} => {
322327
// If the key does not belong to the range, iteration stops.
323-
if !self.range.contains(node.key(entry_idx)) {
328+
if !self.range.contains(node.key(entry_idx, self.map.memory())) {
324329
// Clear all cursors to avoid needless work in subsequent calls.
325330
self.forward_cursors = vec![];
326331
self.backward_cursors = vec![];
327332
return None;
328333
}
329334

330335
let res = map(&node, entry_idx);
331-
self.range.0 = Bound::Excluded(node.key(entry_idx).clone());
336+
self.range.0 = Bound::Excluded(node.key(entry_idx, self.map.memory()).clone());
332337

333338
let next = match node.node_type() {
334339
// If this is an internal node, add the next child to the cursors.
@@ -403,15 +408,15 @@ where
403408
next: Index::Entry(entry_idx),
404409
} => {
405410
// If the key does not belong to the range, iteration stops.
406-
if !self.range.contains(node.key(entry_idx)) {
411+
if !self.range.contains(node.key(entry_idx, self.map.memory())) {
407412
// Clear all cursors to avoid needless work in subsequent calls.
408413
self.forward_cursors = vec![];
409414
self.backward_cursors = vec![];
410415
return None;
411416
}
412417

413418
let res = map(&node, entry_idx);
414-
self.range.1 = Bound::Excluded(node.key(entry_idx).clone());
419+
self.range.1 = Bound::Excluded(node.key(entry_idx, self.map.memory()).clone());
415420

416421
if let Some(next) = match node.node_type() {
417422
// If this is an internal node, add the previous child to the cursors.
@@ -497,7 +502,7 @@ where
497502

498503
fn next(&mut self) -> Option<Self::Item> {
499504
self.0
500-
.next_map(|node, entry_idx| node.key(entry_idx).clone())
505+
.next_map(|node, entry_idx| node.key(entry_idx, self.0.map.memory()).clone())
501506
}
502507

503508
fn count(mut self) -> usize
@@ -516,7 +521,7 @@ where
516521
{
517522
fn next_back(&mut self) -> Option<Self::Item> {
518523
self.0
519-
.next_back_map(|node, entry_idx| node.key(entry_idx).clone())
524+
.next_back_map(|node, entry_idx| node.key(entry_idx, self.0.map.memory()).clone())
520525
}
521526
}
522527

0 commit comments

Comments
 (0)