This document outlines a phased approach to introduce reusable helper functions
and traits in src/lib.rs, with the goal of eliminating boilerplate and
clarifying the core B+‑tree operations (get, insert, remove, rebalance,
merge, etc.). By encapsulating common patterns (node lookup, child dispatch,
rebalance logic, merges, and split insertion) into small, well‑tested utilities,
we can shrink and simplify the implementation surface and reduce risks of
memory or logic errors.
Objective: Collapse the two-step computation of child index and child enum
(NodeRef) into a single helper.
Implementation steps:
- Implement:
fn find_child(&self, branch_id: NodeId, key: &K) -> Option<(usize, NodeRef<K, V>)>; fn find_child_mut(&mut self, branch_id: NodeId, key: &K) -> Option<(usize, NodeRef<K, V>)>;
- Write tests covering branch lookups and out-of-range indices.
- Replace manual
branch.find_child_index+branch.children.get(idx)code inget,insert,remove, and rebalance routines.
Objective: Provide ergonomic accessors on NodeRef<K,V> to reduce pattern matches.
Implementation steps:
- On
NodeRef<K, V>, add:fn id(&self) -> NodeId; fn is_leaf(&self) -> bool;
- Update code that matches on
NodeRef::Leaf/NodeRef::Branchto use the new helpers for dispatching to child nodes.
Objective: Factor out the repeated take-then-append merge pattern across four merge routines (left/right × leaf/branch).
Implementation steps:
- Add a generic helper:
fn move_node_contents<N, F>( arena: &mut Vec<Option<N>>, from: NodeId, to: NodeId, merge_fn: F ) -> Option<()> where F: FnOnce(&mut N, N);
- Refactor each of
merge_with_left_leaf,merge_with_right_leaf,merge_with_left_branch, andmerge_with_right_branchto usemove_node_contents.
Objective: Centralize branch-child insertion and split logic into a single method
on BranchNode<K,V>, eliminating repetitive arena bookkeeping and root-update code.
Implementation steps:
- On
BranchNode<K, V>, implement:fn insert_child( &mut self, idx: usize, sep_key: K, right: NodeRef<K, V>, capacity: usize ) -> Option<(BranchNode<K, V>, K)>;
- Refactor all calling sites in the tree map logic (
insert/split handlers) to use this new helper and simplify root creation.
- Remove now‑unused macros and old helper functions (e.g.
ENTER_TREE_LOOP). - Run unit tests and benchmarks to ensure no behavioral or performance regressions.
- Update
README.mdand other documentation to reflect the new APIs. - Submit a single cohesive PR with related tests and doc updates for review.
By following this plan, we will transform the current ~2,000 lines of tightly coupled tree
logic in src/lib.rs into a modular, maintainable codebase where complex operations
are expressed via small, composable utilities.