fix parent indices
This commit is contained in:
parent
216485e448
commit
cc6b783be9
31
src/tree.rs
31
src/tree.rs
|
|
@ -744,13 +744,21 @@ impl<K, V> NodeRef<marker::Owned, K, V> {
|
||||||
// insert new key and child node.
|
// insert new key and child node.
|
||||||
// SAFETY: we just grew the allocations.
|
// SAFETY: we just grew the allocations.
|
||||||
unsafe {
|
unsafe {
|
||||||
assert!(new_len == parent.idx + 1);
|
|
||||||
slice_insert(parent.node.key_area_mut(..new_len), parent.idx, key);
|
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);
|
slice_insert(parent.node.edge_area_mut(..new_len), parent.idx, self.node);
|
||||||
}
|
}
|
||||||
|
|
||||||
*parent.node.len_mut() = new_len as u16;
|
*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() }
|
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>
|
pub fn entry<'a, Q>(&'a mut self, key_seq: Q) -> entry::Entry<'a, OnceAndIter<Q, K>, K, V>
|
||||||
where
|
where
|
||||||
Q: Iterator<Item = K>,
|
Q: Iterator<Item = K>,
|
||||||
|
|
@ -1771,6 +1792,7 @@ mod tests {
|
||||||
tree.entry("asdf".chars()).or_insert(1);
|
tree.entry("asdf".chars()).or_insert(1);
|
||||||
tree.entry("asd".chars()).or_insert(2);
|
tree.entry("asd".chars()).or_insert(2);
|
||||||
tree.entry("asdg".chars()).or_insert(3);
|
tree.entry("asdg".chars()).or_insert(3);
|
||||||
|
tree.entry("asda".chars()).or_insert(4);
|
||||||
|
|
||||||
tree
|
tree
|
||||||
}
|
}
|
||||||
|
|
@ -1779,4 +1801,11 @@ mod tests {
|
||||||
fn drop_tree() {
|
fn drop_tree() {
|
||||||
let tree = build_tree();
|
let tree = build_tree();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn entry() {
|
||||||
|
let tree = build_tree();
|
||||||
|
|
||||||
|
assert_eq!(tree.as_subtree_mut().get("asdf".chars()), Some(&1));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue