Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 149 additions & 2 deletions src/write_zipper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1568,6 +1568,32 @@ impl <'a, 'path, V: Clone + Send + Sync + Unpin, A: Allocator + 'a> WriteZipperC
pub fn set_val(&mut self, val: V) -> Option<V> {
self.set_val_at(&[], val)
}
/// Internal. `prune`, unless the focus doesn't exist
///
/// A pruning removal is the removal with `prune = false` followed by `prune_path`, which does
/// nothing at a focus that doesn't exist. This has to be decided before the removal: pruning
/// starts wherever the focus's node is empty, and a missing focus below a dangling path held as an
/// empty node (a write zipper's stump, an emptied graft) is in that empty node too.
#[inline]
fn prune_if_focus_exists(&self, prune: bool) -> bool {
prune && self.path_exists()
}

/// Internal. Whether the focus is a valueless link to an empty node, e.g. the node a `ZipperHead`
/// splits off at its root to give a read zipper a node of its own. That is a dangling tip, but the
/// node holding the link isn't empty, so [Self::prune_path_internal] needs this to recognise it.
fn focus_is_empty_child_link(&self) -> bool {
let key = self.key.node_key();
if key.is_empty() {
return false
}
let node = self.focus_stack.top().unwrap();
match node.node_get_child(key) {
Some((consumed, child)) => consumed == key.len() && child.as_tagged().node_is_empty() && !node.node_contains_val(key),
None => false,
}
}

#[inline(always)]
fn node_prune_limit(&self, prune: bool) -> usize {
if prune {
Expand All @@ -1584,6 +1610,7 @@ impl <'a, 'path, V: Clone + Send + Sync + Unpin, A: Allocator + 'a> WriteZipperC
let root_val_ref = self.root_val.as_mut().unwrap();
return core::mem::take(unsafe{&mut **root_val_ref})
}
let prune = self.prune_if_focus_exists(prune);
let prune_limit = self.node_prune_limit(prune);
let mut focus_node = self.focus_stack.top_mut().unwrap();
let result = focus_node.node_remove_val(self.key.node_key(), prune_limit);
Expand Down Expand Up @@ -2436,6 +2463,7 @@ impl <'a, 'path, V: Clone + Send + Sync + Unpin, A: Allocator + 'a> WriteZipperC
}
/// See [WriteZipper::remove_branches]
pub fn remove_branches(&mut self, prune: bool) -> bool {
let prune = self.prune_if_focus_exists(prune);
let node_key = self.key.node_key();
if node_key.len() > 0 {
let prune_limit = self.node_prune_limit(prune);
Expand Down Expand Up @@ -2466,6 +2494,7 @@ impl <'a, 'path, V: Clone + Send + Sync + Unpin, A: Allocator + 'a> WriteZipperC
return removed_branches || removed_val;
}

let prune = self.prune_if_focus_exists(prune);
let prune_limit = self.node_prune_limit(prune);
let removed = {
// Hold one mutable node guard and defer pruning until both removals are complete.
Expand Down Expand Up @@ -2499,6 +2528,7 @@ impl <'a, 'path, V: Clone + Send + Sync + Unpin, A: Allocator + 'a> WriteZipperC
}
/// See [WriteZipper::remove_unmasked_branches]
pub fn remove_unmasked_branches(&mut self, mask: ByteMask, prune: bool) {
let prune = self.prune_if_focus_exists(prune);
let node_key = self.key.node_key();
let prune_limit = self.node_prune_limit(prune);
let mut focus_node = self.focus_stack.top_mut().unwrap();
Expand Down Expand Up @@ -2730,8 +2760,9 @@ impl <'a, 'path, V: Clone + Send + Sync + Unpin, A: Allocator + 'a> WriteZipperC
/// to [`ZipperMoving::ascend_until`]
pub(crate) fn prune_path_internal(&mut self, should_ascend: bool) -> usize {

//We need to make sure we're at the end of a dangling path
if !self.focus_stack.top().unwrap().node_is_empty() {
//We need to make sure we're at the end of a dangling path: the focus's node is empty, or the
// focus links to an empty node
if !self.focus_stack.top().unwrap().node_is_empty() && !self.focus_is_empty_child_link() {
return 0
}

Expand Down Expand Up @@ -6490,6 +6521,122 @@ mod tests {
assert!(map.path_exists_at(&[0, 1]));
}

/// A pruning remove at a focus that doesn't exist removes nothing, also below a dangling path held
/// as an empty node: one an empty `graft_masked_branches` leaves, or a `ZipperHead` writer's stump.
/// The prune used to start there, because the focus's node (the empty one) is empty
#[test]
fn prune_at_missing_focus_below_empty_node_keeps_parent() {
let exists = |m: &PathMap<u64>, p: &[u8]| m.read_zipper_at_path(p).path_exists();
//Below an emptied graft
let mut m0 = PathMap::<u64>::new();
m0.set_val_at(&[], 0);
m0.set_val_at(&[0u8, 0, 0, 0, 0], 0);
let mut m1 = PathMap::<u64>::new();
m1.set_val_at(&[1u8, 1, 0, 0], 0);
let rz = m1.read_zipper_at_path(&[1u8]);
let mut wz = m0.write_zipper();
wz.descend_to(&[0u8]);
wz.graft_masked_branches(&rz, ByteMask::from_iter([3u8, 2, 0]), true);
assert!(wz.path_exists());
wz.descend_to_byte(0);
wz.remove_unmasked_branches(ByteMask::from_iter([0u8]), true);
wz.ascend_byte();
assert!(wz.path_exists());

//Below a stump, as below an ordinary dangling path
let mut a = PathMap::<u64>::new();
a.create_path(&[0u8]);
a.remove(&[0u8, 0]);
assert!(exists(&a, &[0]));
let mut b = PathMap::<u64>::new();
{ let zh = b.zipper_head(); let _w = zh.write_zipper_at_exclusive_path(&[0u8]).unwrap(); }
assert!(exists(&b, &[0]));
b.remove(&[0u8, 0]);
assert!(exists(&b, &[0]));

//Every pruning remove, inside a session, below a stump a nested writer left
let mut m = PathMap::<u64>::new();
m.set_val_at(&[9u8], 1);
{
let zh = m.zipper_head();
{ let _w = zh.write_zipper_at_exclusive_path(&[0u8, 1]).unwrap(); }
let mut w = zh.write_zipper_at_exclusive_path(&[0u8]).unwrap();
w.descend_to(&[1u8, 5]);
assert!(!w.path_exists());
assert_eq!(w.remove_val(true), None);
assert!(!w.remove_branches(true));
w.remove_unmasked_branches(ByteMask::EMPTY, true);
assert!(!w.remove_subtrie(true));
}
assert!(exists(&m, &[0, 1]));

//At a focus that exists, the prune still happens
let mut m = PathMap::<u64>::new();
{ let zh = m.zipper_head(); let _w = zh.write_zipper_at_exclusive_path(&[0u8]).unwrap(); }
m.set_val_at(&[0u8, 1], 3);
assert_eq!(m.remove(&[0u8, 1]), Some(3));
assert!(!exists(&m, &[0]));
let mut m = PathMap::<u64>::new();
{ let zh = m.zipper_head(); let _w = zh.write_zipper_at_exclusive_path(&[0u8]).unwrap(); }
let mut wz = m.write_zipper();
wz.descend_to(&[0u8]);
wz.remove_branches(true);
drop(wz);
assert!(!exists(&m, &[0]));
}

/// A dangling tip held as a link to an empty node is pruned like any other: the node a
/// `ZipperHead` splits off at its root for a read zipper is left behind at a leaf, and `remove`
/// used to leave the location dangling
#[test]
fn prune_dangling_tip_linking_to_empty_node() {
let exists = |m: &PathMap<u64>, p: &[u8]| m.read_zipper_at_path(p).path_exists();
let leaf_head_read = || {
let mut m = PathMap::<u64>::new();
m.set_val_at(&[1u8], 0);
m.set_val_at(&[2u8, 3], 7);
let zh = m.into_zipper_head(&[1u8]);
drop(zh.read_zipper_at_path(&[]).unwrap());
zh.into_map()
};
let mut m = leaf_head_read();
assert_eq!(m.remove(&[1u8]), Some(0));
assert!(!exists(&m, &[1]));
assert_eq!(m.val_at(&[2u8, 3]), Some(&7));

//The other ways to prune the emptied tip
let mut m = leaf_head_read();
m.remove_val_at(&[1u8], false);
assert!(exists(&m, &[1]));
{ let mut wz = m.write_zipper(); wz.descend_to(&[1u8]); assert_eq!(wz.prune_path(), 1); }
assert!(!exists(&m, &[1]));
let mut m = leaf_head_read();
m.remove_val_at(&[1u8], false);
m.prune_path(&[1u8]);
assert!(!exists(&m, &[1]));
for which in 0..2 {
let mut m = leaf_head_read();
m.remove_val_at(&[1u8], false);
let mut wz = m.write_zipper();
wz.descend_to(&[1u8]);
if which == 0 { wz.remove_branches(true); } else { wz.remove_subtrie(true); }
drop(wz);
assert!(!exists(&m, &[1]), "which {which}");
}

//A head from a write zipper
let mut m = PathMap::<u64>::new();
m.set_val_at(&[1u8, 2], 0);
{
let mut wz = m.write_zipper_at_path(&[1u8]);
let zh = wz.zipper_head();
drop(zh.read_zipper_at_path(&[2u8]).unwrap());
}
m.remove(&[1u8, 2]);
assert!(!exists(&m, &[1, 2]));
assert!(!exists(&m, &[1]));
}

#[test]
fn write_zipper_shared_queries_preserve_focus_borrows() {
let mut map = PathMap::from_iter([(b"a".as_slice(), 10u64), (b"ab", 20), (b"ac", 30)]);
Expand Down