diff --git a/src/tree.rs b/src/tree.rs index a648d90..6165e1d 100644 --- a/src/tree.rs +++ b/src/tree.rs @@ -744,13 +744,21 @@ impl NodeRef { // insert new key and child node. // SAFETY: we just grew the allocations. unsafe { - assert!(new_len == parent.idx + 1); slice_insert(parent.node.key_area_mut(..new_len), parent.idx, key); slice_insert(parent.node.edge_area_mut(..new_len), parent.idx, self.node); } *parent.node.len_mut() = new_len as u16; + // adjust parent indices of siblings to the right. + for i in (parent.idx + 1)..new_len { + unsafe { + let sibling = parent.node.edge_area_mut(i).assume_init_read(); + + (&raw mut (*sibling.as_ptr()).parent_idx).write(MaybeUninit::new(i as u16)); + } + } + unsafe { self.borrow_mut().dormant().awaken() } } } @@ -1487,6 +1495,19 @@ where } } + pub fn as_subtree<'a>(&'a self) -> subtree::Subtree<'a, K, V, marker::Immut<'a>> { + match self.root.as_mut() { + Some(node) => { + let dormant = node.borrow_mut().dormant(); + Subtree::new_root(unsafe { dormant.awaken().reborrow() }) + } + None => { + let (_, dormant) = borrow::DormantMutRef::new(self); + Subtree::new_empty(dormant) + } + } + } + pub fn entry<'a, Q>(&'a mut self, key_seq: Q) -> entry::Entry<'a, OnceAndIter, K, V> where Q: Iterator, @@ -1771,6 +1792,7 @@ mod tests { tree.entry("asdf".chars()).or_insert(1); tree.entry("asd".chars()).or_insert(2); tree.entry("asdg".chars()).or_insert(3); + tree.entry("asda".chars()).or_insert(4); tree } @@ -1779,4 +1801,11 @@ mod tests { fn drop_tree() { let tree = build_tree(); } + + #[test] + fn entry() { + let tree = build_tree(); + + assert_eq!(tree.as_subtree_mut().get("asdf".chars()), Some(&1)); + } }