Skip to content
Merged
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
253 changes: 147 additions & 106 deletions src/overlay_zipper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,103 @@ impl<AV, BV, OutV, AZipper, BZipper, Mapping>
BZipper: ZipperMoving + ZipperPath + ZipperValues<BV>,
Mapping: for<'a> Fn(Option<&'a AV>, Option<&'a BV>) -> Option<&'a OutV>,
{
/// Internal. `descend_until` as far as the sources take it: each source stops at its own values
/// and branches, and the descent stops where the two part ways
fn descend_until_sources<Obs: PathObserver>(&mut self, obs: &mut Obs) -> bool {
//Descending happens in buffer-sized chunks, so neither source can outrun what we're able
// to capture. As long as both sources fill a whole chunk and agree on every byte of it,
// the chunk is committed and we go around again. Any other outcome ends the descent and
// is settled by the case analysis below.
const CHUNK: usize = 48;
let mut path_a = ArrayVec::<u8, CHUNK>::new();
let mut path_b = ArrayVec::<u8, CHUNK>::new();

//Total bytes committed to `obs` across all completed chunks
let mut committed = 0usize;
#[cfg(debug_assertions)]
let start_depth = self.a.path().len();

loop {
path_a.clear();
path_b.clear();
let desc_a = self.a.descend_until_max_bytes_observed(CHUNK, &mut path_a);
let desc_b = self.b.descend_until_max_bytes_observed(CHUNK, &mut path_b);

if !desc_a && !desc_b {
break;
}
if !desc_a && desc_b {
if self.a.child_count() == 0 {
self.a.descend_to(&path_b);
obs.descend_to(&path_b);
committed += path_b.len();
} else {
let to_ascend = path_b.len();
let ascended = self.b.ascend(to_ascend);
debug_assert_eq!(ascended, to_ascend);
}
break;
}
if desc_a && !desc_b {
if self.b.child_count() == 0 {
self.b.descend_to(&path_a);
obs.descend_to(&path_a);
committed += path_a.len();
} else {
let to_ascend = path_a.len();
let ascended = self.a.ascend(to_ascend);
debug_assert_eq!(ascended, to_ascend);
}
break;
}

//Both moved. Keep the portion they agree on and rewind the rest
let overlap = find_prefix_overlap(&path_a, &path_b);
if path_a.len() > overlap {
let to_ascend = path_a.len() - overlap;
let ascended = self.a.ascend(to_ascend);
debug_assert_eq!(ascended, to_ascend);
}
if path_b.len() > overlap {
let to_ascend = path_b.len() - overlap;
let ascended = self.b.ascend(to_ascend);
debug_assert_eq!(ascended, to_ascend);
}
//Both sources must now sit at the same position: the agreed-upon prefix
debug_assert_eq!(self.a.path(), self.b.path());
if overlap > 0 {
obs.descend_to(&path_a[..overlap]);
committed += overlap;
}

//Only a full chunk that both sources agreed on end-to-end can be continued. Anything
// shorter means at least one source stopped on its own, so the descent is complete.
if overlap < CHUNK || path_a.len() != CHUNK || path_b.len() != CHUNK {
break;
}
}

#[cfg(debug_assertions)]
debug_assert_eq!(start_depth + committed, self.a.path().len());
committed > 0
}

/// Internal. `ascend_until` (`stop_at_val`) or `ascend_until_branch`, over the overlay's own trie
///
/// Each source's own stops can't be used: a branch can be formed by the union (one child in each
/// source, a different byte), and a source value may be one the mapping filters out. So this
/// climbs a byte at a time and asks the overlay.
fn ascend_cond(&mut self, stop_at_val: bool) -> usize {
let mut ascended = 0;
while self.ascend_byte() {
ascended += 1;
if self.child_count() > 1 || (stop_at_val && self.is_val()) {
break
}
}
ascended
}

fn to_sibling(&mut self, next: bool) -> Option<u8> {
let last = self.focus_byte()?;
self.ascend(1);
Expand Down Expand Up @@ -250,82 +347,17 @@ impl<AV, BV, OutV, AZipper, BZipper, Mapping> ZipperMoving
}

fn descend_until_observed<Obs: PathObserver>(&mut self, obs: &mut Obs) -> bool {
//Descending happens in buffer-sized chunks, so neither source can outrun what we're able
// to capture. As long as both sources fill a whole chunk and agree on every byte of it,
// the chunk is committed and we go around again. Any other outcome ends the descent and
// is settled by the case analysis below.
const CHUNK: usize = 48;
let mut path_a = ArrayVec::<u8, CHUNK>::new();
let mut path_b = ArrayVec::<u8, CHUNK>::new();

//Total bytes committed to `obs` across all completed chunks
let mut committed = 0usize;
#[cfg(debug_assertions)]
let start_depth = self.a.path().len();

loop {
path_a.clear();
path_b.clear();
let desc_a = self.a.descend_until_max_bytes_observed(CHUNK, &mut path_a);
let desc_b = self.b.descend_until_max_bytes_observed(CHUNK, &mut path_b);

if !desc_a && !desc_b {
break;
}
if !desc_a && desc_b {
if self.a.child_count() == 0 {
self.a.descend_to(&path_b);
obs.descend_to(&path_b);
committed += path_b.len();
} else {
let to_ascend = path_b.len();
let ascended = self.b.ascend(to_ascend);
debug_assert_eq!(ascended, to_ascend);
}
break;
}
if desc_a && !desc_b {
if self.b.child_count() == 0 {
self.b.descend_to(&path_a);
obs.descend_to(&path_a);
committed += path_a.len();
} else {
let to_ascend = path_a.len();
let ascended = self.a.ascend(to_ascend);
debug_assert_eq!(ascended, to_ascend);
}
break;
}

//Both moved. Keep the portion they agree on and rewind the rest
let overlap = find_prefix_overlap(&path_a, &path_b);
if path_a.len() > overlap {
let to_ascend = path_a.len() - overlap;
let ascended = self.a.ascend(to_ascend);
debug_assert_eq!(ascended, to_ascend);
}
if path_b.len() > overlap {
let to_ascend = path_b.len() - overlap;
let ascended = self.b.ascend(to_ascend);
debug_assert_eq!(ascended, to_ascend);
}
//Both sources must now sit at the same position: the agreed-upon prefix
debug_assert_eq!(self.a.path(), self.b.path());
if overlap > 0 {
obs.descend_to(&path_a[..overlap]);
committed += overlap;
}

//Only a full chunk that both sources agreed on end-to-end can be continued. Anything
// shorter means at least one source stopped on its own, so the descent is complete.
if overlap < CHUNK || path_a.len() != CHUNK || path_b.len() != CHUNK {
break;
//The sources stop at their own values, but the overlay's values come from the mapping: a stop
// at a source value the mapping filtered out is not the overlay's stop, so carry on from there
// while the overlay has one child and no value
let mut moved = false;
while self.descend_until_sources(obs) {
moved = true;
if self.is_val() || self.child_count() != 1 {
break
}
}

#[cfg(debug_assertions)]
debug_assert_eq!(start_depth + committed, self.a.path().len());
committed > 0
moved
}

fn ascend(&mut self, steps: usize) -> usize {
Expand All @@ -341,41 +373,11 @@ impl<AV, BV, OutV, AZipper, BZipper, Mapping> ZipperMoving
}

fn ascend_until(&mut self) -> usize {
debug_assert_eq!(self.a.path(), self.b.path());
let start_depth = self.a.path().len();
let asc_a = self.a.ascend_until();
let path_a = self.a.path();
let depth_a = path_a.len();
let asc_b = self.b.ascend_until();
let path_b = self.b.path();
let depth_b = path_b.len();
if asc_a == 0 && asc_b == 0 {
return 0;
}
//Whichever source ascended further sets the shared position; the other descends to match
if depth_b > depth_a {
self.a.descend_to(&path_b[depth_a..]);
} else if depth_a > depth_b {
self.b.descend_to(&path_a[depth_b..]);
}
start_depth - self.a.path().len()
self.ascend_cond(true)
}

fn ascend_until_branch(&mut self) -> usize {
let start_depth = self.a.path().len();
let _asc_a = self.a.ascend_until_branch();
let path_a = self.a.path();
let depth_a = path_a.len();
let _asc_b = self.b.ascend_until_branch();
let path_b = self.b.path();
let depth_b = path_b.len();
//Whichever source ascended further sets the shared position; the other descends to match
if depth_b > depth_a {
self.a.descend_to(&path_b[depth_a..]);
} else if depth_a > depth_b {
self.b.descend_to(&path_a[depth_b..]);
}
start_depth - self.a.path().len()
self.ascend_cond(false)
}

fn to_next_sibling_byte(&mut self) -> Option<u8> {
Expand Down Expand Up @@ -422,6 +424,7 @@ mod tests {
ReadZipperUntracked,
zipper_iteration_tests,
zipper_moving_tests,
Zipper,
ZipperMoving,
ZipperPath,
ZipperValues,
Expand Down Expand Up @@ -653,4 +656,42 @@ mod tests {
assert_eq!(z.path(), &[1u8, 2, 3]);
assert_eq!(z.val(), Some(&3));
}

/// `ascend_until` / `ascend_until_branch` stop where the overlay branches or has a value, not
/// where a source does: a branch formed by the union is a stop, a value the mapping filtered out
/// is not. `descend_until` doesn't stop at a filtered-out value either. All three used to take
/// the sources' stops
#[test]
fn overlay_until_moves_follow_the_overlay() {
fn b_unless_a<'a>(a: Option<&'a u64>, b: Option<&'a u64>) -> Option<&'a u64> { if a.is_some() { None } else { b } }

//A branch formed by the union
let mut a = PathMap::<u64>::new();
a.set_val_at(&[1u8, 0, 0, 0], 0);
let mut b = PathMap::<u64>::new();
b.set_val_at(&[1u8, 1, 0, 0], 0);
let mut z = OverlayZipper::new(a.read_zipper(), b.read_zipper());
z.descend_to(&[1u8, 0]);
assert_eq!(z.ascend_until(), 1);
assert_eq!(z.path(), &[1u8]);
assert_eq!(z.child_count(), 2);
z.descend_to(&[0u8, 0]);
assert_eq!(z.ascend_until_branch(), 2);
assert_eq!(z.path(), &[1u8]);

//A value the mapping filters out
let mut a = PathMap::<u64>::new();
a.set_val_at(&[2u8], 0);
a.set_val_at(&[2u8, 0, 0, 0, 0], 0);
let b = PathMap::<u64>::new();
let mut z = OverlayZipper::with_mapping(a.read_zipper(), b.read_zipper(), b_unless_a);
z.descend_to(&[2u8, 0, 0]);
assert_eq!(z.ascend_until(), 3);
assert_eq!(z.path(), &[] as &[u8]);
assert!(z.descend_until());
assert_eq!(z.path(), &[2u8, 0, 0, 0, 0]);
z.reset();
assert!(z.descend_until_max_bytes(3));
assert_eq!(z.path(), &[2u8, 0, 0]);
}
}