Skip to content

Commit 7f7bdba

Browse files
committed
Step 4: Move new_root method to insert_operations
- Move new_root helper method from lib.rs to insert_operations.rs - Make method public so it can be accessed from lib.rs - All tests still pass (35/35) - Continuing careful incremental extraction
1 parent effb56b commit 7f7bdba

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

rust/src/insert_operations.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,23 @@ impl<K: Ord + Clone, V: Clone> BPlusTreeMap<K, V> {
1818
pub fn allocate_branch(&mut self, branch: BranchNode<K, V>) -> NodeId {
1919
self.branch_arena.allocate(branch)
2020
}
21+
22+
/// Create a new root node when the current root splits.
23+
/// New roots are the only BranchNodes allowed to remain underfull.
24+
pub fn new_root(&mut self, new_node: NodeRef<K, V>, separator_key: K) -> BranchNode<K, V> {
25+
let mut new_root = BranchNode::new(self.capacity);
26+
new_root.keys.push(separator_key);
27+
28+
// Move the current root to be the left child
29+
// Use a dummy NodeRef with NULL_NODE to avoid arena allocation
30+
let dummy = NodeRef::Leaf(crate::types::NULL_NODE, PhantomData);
31+
let old_root = std::mem::replace(&mut self.root, dummy);
32+
33+
new_root.children.push(old_root);
34+
new_root.children.push(new_node);
35+
36+
new_root
37+
}
2138
}
2239

2340
#[cfg(test)]

rust/src/lib.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -262,21 +262,7 @@ impl<K: Ord + Clone, V: Clone> BPlusTreeMap<K, V> {
262262
// HELPERS FOR INSERT OPERATIONS
263263
// ============================================================================
264264

265-
/// New roots are the only BranchNodes allowed to remain underfull
266-
fn new_root(&mut self, new_node: NodeRef<K, V>, separator_key: K) -> BranchNode<K, V> {
267-
let mut new_root = BranchNode::new(self.capacity);
268-
new_root.keys.push(separator_key);
269-
270-
// Move the current root to be the left child
271-
// Use a dummy NodeRef with NULL_NODE to avoid arena allocation
272-
let dummy = NodeRef::Leaf(NULL_NODE, PhantomData);
273-
let old_root = std::mem::replace(&mut self.root, dummy);
274-
275-
new_root.children.push(old_root);
276-
new_root.children.push(new_node);
277-
278-
new_root
279-
}
265+
// new_root method moved to insert_operations.rs module
280266

281267
/// Recursively insert a key with proper arena access.
282268
fn insert_recursive(&mut self, node: &NodeRef<K, V>, key: K, value: V) -> InsertResult<K, V> {

0 commit comments

Comments
 (0)