Skip to content

Commit

Permalink
Fix BptreeMap range iter adjacent leaves (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
Erigara authored Jun 26, 2024
1 parent c0955ec commit 8e1bd22
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/bptree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ where

#[cfg(test)]
mod tests {
use std::ops::Bound;

use super::BptreeMap;
use crate::internals::bptree::node::{assert_released, L_CAPACITY};
// use rand::prelude::*;
Expand Down Expand Up @@ -363,6 +365,15 @@ mod tests {
assert!(r.range(1..=2).count() == 0);
}

#[test]
fn test_bptree2_map_rangeiter_3() {
let map = BptreeMap::from_iter([0, 1, 2, 3, 4, 5, 6, 8].map(|v| (v, ())));

let r = map.read();
assert!(r.range((Bound::Excluded(6), Bound::Included(7))).count() == 0);
assert!(r.range((Bound::Excluded(6), Bound::Excluded(8))).count() == 0);
}

/*
#[test]
fn test_bptree2_map_write_compact() {
Expand Down
24 changes: 24 additions & 0 deletions src/internals/bptree/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,14 @@ where
// eprintln!("Excluding Using, {}", fidx);
*idx = fidx + 1;
if *idx >= leaf.count() {
if let Some((rnode, _)) = right_iter.get_mut() {
// If the leaf iterators were in the same node before advancing left iterator
// means that left iterator would be ahead of right iter so no elements left
if rnode == node {
left_iter.clear();
right_iter.clear();
}
}
// Okay, this means we overflowed to the next leaf, so just
// advanced the leaf iter to the start of the next
left_iter.next();
Expand Down Expand Up @@ -521,6 +529,14 @@ where
// eprintln!("Using, {}", fidx);
let (nidx, oflow) = fidx.overflowing_sub(1);
if oflow {
if let Some((lnode, _)) = left_iter.get_mut() {
// If the leaf iterators were in the same node before advancing right iterator
// means that left iterator would be ahead of right iter so no elements left
if lnode == node {
left_iter.clear();
right_iter.clear();
}
}
right_iter.next();
} else {
*idx = nidx;
Expand All @@ -541,6 +557,14 @@ where
// eprintln!("Using, {}", fidx);
let (nidx, oflow) = fidx.overflowing_sub(1);
if oflow {
if let Some((lnode, _)) = left_iter.get_mut() {
// If the leaf iterators were in the same node before advancing right iterator
// means that left iterator would be ahead of right iter so no elements left
if lnode == node {
left_iter.clear();
right_iter.clear();
}
}
right_iter.next();
} else {
*idx = nidx;
Expand Down

0 comments on commit 8e1bd22

Please sign in to comment.