fix parent indices

This commit is contained in:
janis 2025-09-18 20:41:39 +02:00
parent 216485e448
commit cc6b783be9
Signed by: janis
SSH key fingerprint: SHA256:bB1qbbqmDXZNT0KKD5c2Dfjg53JGhj7B3CFcLIzSqq8

View file

@ -744,13 +744,21 @@ impl<K, V> NodeRef<marker::Owned, K, V> {
// 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<Q, K>, K, V>
where
Q: Iterator<Item = K>,
@ -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));
}
}