Insert a separator key and new child into this branch node. Returns None if no split needed, or Some((new_branch_data, promoted_key)) if split occurred. The caller should handle arena allocation for the split data.
(
&mut self,
child_index: usize,
separator_key: K,
new_child: NodeRef<K, V>,
)
| 520 | /// Returns None if no split needed, or Some((new_branch_data, promoted_key)) if split occurred. |
| 521 | /// The caller should handle arena allocation for the split data. |
| 522 | pub fn insert_child_and_split_if_needed( |
| 523 | &mut self, |
| 524 | child_index: usize, |
| 525 | separator_key: K, |
| 526 | new_child: NodeRef<K, V>, |
| 527 | ) -> Option<(BranchNode<K, V>, K)> { |
| 528 | // Check if split is needed BEFORE inserting |
| 529 | if self.is_full() { |
| 530 | // Branch is at capacity, need to handle split |
| 531 | // For branches, we MUST insert first because split promotes a key |
| 532 | // With capacity=4: 4 keys → split needs 5 keys (2 left + 1 promoted + 2 right) |
| 533 | self.keys.insert(child_index, separator_key); |
| 534 | self.children.insert(child_index + 1, new_child); |
| 535 | |
| 536 | // Now split the overfull branch |
| 537 | let (new_right, promoted_key) = self.split_data(); |
| 538 | Some((new_right, promoted_key)) |
| 539 | } else { |
| 540 | // Room to insert without splitting |
| 541 | self.keys.insert(child_index, separator_key); |
| 542 | self.children.insert(child_index + 1, new_child); |
| 543 | None |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | /// Split this branch node, returning the new right node and promoted key. |
| 548 | pub fn split_data(&mut self) -> (BranchNode<K, V>, K) { |
no test coverage detected