Skip to content
Open
Show file tree
Hide file tree
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
135 changes: 121 additions & 14 deletions src/dependent_zipper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,28 +241,133 @@ impl<'trie, PrimaryZ, SecondaryZ, V, C, F : Clone + for <'a> FnOnce(C, &'a [u8],
}
}

/// Where the walk of [DependentProductZipperG::get_val_at_walk] stands: a read zipper forked from
/// one of the zipper's own factors, or a factor zipper the walk enrolled itself
enum Probe<P, S, N> {
Primary(P),
Secondary(usize, S),
Enrolled(N),
}

impl<'trie, PrimaryZ, SecondaryZ, V, C, F : Clone + for <'a> FnOnce(C, &'a [u8], usize) -> (C, Option<SecondaryZ>)> DependentProductZipperG<'trie, PrimaryZ, SecondaryZ, V, C, F>
where
V: Clone + Send + Sync + 'trie,
PrimaryZ: ZipperMoving + ZipperPath + ZipperReadOnlyValues<'trie, V> + ZipperForking<V>,
SecondaryZ: ZipperMoving + ZipperReadOnlyValues<'trie, V> + ZipperForking<V>,
C: Clone,
{
/// Internal. `descend_to(path)` then [`get_val`](ZipperReadOnlyValues::get_val), without moving:
/// replays `descend_to` on zippers forked from the factors, calling `enroll` (on a copy of the
/// payload) wherever the walk stands on a leaf, as `enter_factors` does
///
/// The factor whose zipper is at the focus is `factor_idx(false)` (a factor entered at the focus
/// sits at its root). A location's value is the one of the factor it was reached in, not of a
/// factor entered there.
fn get_val_at_walk(&self, path: &[u8]) -> Option<&'trie V> {
//A value inside the focus's factor is the answer: a descent only enters the next factor at
// a leaf, and a leaf has nothing below it for `path` to continue into
let here = match self.factor_idx(false) {
Some(idx) => self.secondary[idx].get_val_at(path),
None => self.primary.get_val_at(path),
};
if here.is_some() {
return here
}
let at_leaf = |probe: &Probe<PrimaryZ::ReadZipperT<'_>, SecondaryZ::ReadZipperT<'_>, SecondaryZ>| match probe {
Probe::Primary(z) => z.child_count() == 0 && z.path_exists(),
Probe::Secondary(_, z) => z.child_count() == 0 && z.path_exists(),
Probe::Enrolled(z) => z.child_count() == 0 && z.path_exists(),
};
//A fork is `None` where the zipper's focus doesn't exist, so nothing below it does either
let mut probe = match self.factor_idx(false) {
Some(idx) => Probe::Secondary(idx, self.secondary[idx].fork_read_zipper()?),
None => Probe::Primary(self.primary.fork_read_zipper()?),
};
let mut payload = self.enroll_payload.clone();
let mut factor_count = self.secondary.len();
//The product path at the start of `path` and up to `pos`, for `enroll`, built on first use
let mut walked: Option<Vec<u8>> = None;
//Where in `path` the current factor's zipper stood when the walk took it up
let mut factor_start = 0;
let mut pos = 0;
//`enter_factors`: returns whether a factor was entered
let mut enter = |probe: &mut Probe<_, _, SecondaryZ>, pos: usize, factor_start: &mut usize| -> bool {
if !at_leaf(probe) {
return false
}
let Some(p) = payload.take() else { return false };
let walked = walked.get_or_insert_with(|| self.path().to_vec());
walked.truncate(self.path().len());
walked.extend_from_slice(&path[..pos]);
let (p, entered) = self.enroll.clone()(p, walked, factor_count);
payload = Some(p);
match entered {
Some(z) => {
*probe = Probe::Enrolled(z);
factor_count += 1;
*factor_start = pos;
true
},
None => false,
}
};
while pos < path.len() {
enter(&mut probe, pos, &mut factor_start);
let good = match &mut probe {
Probe::Primary(z) => z.descend_to_existing(&path[pos..]),
Probe::Secondary(_, z) => z.descend_to_existing(&path[pos..]),
Probe::Enrolled(z) => z.descend_to_existing(&path[pos..]),
};
if good == 0 {
//`descend_to` then enters at most one more factor, and takes the rest of the path
// into that factor's zipper whether it exists there or not
enter(&mut probe, pos, &mut factor_start);
break
}
pos += good;
}
match probe {
Probe::Primary(_) => self.primary.get_val_at(&path[factor_start..]),
Probe::Secondary(idx, _) => self.secondary[idx].get_val_at(&path[factor_start..]),
Probe::Enrolled(mut z) => {
if pos < path.len() {
z.descend_to(&path[pos..]);
}
z.get_val()
},
}
}
}

impl<'trie, PrimaryZ, SecondaryZ, V, C, F : Clone + for <'a> FnOnce(C, &'a [u8], usize) -> (C, Option<SecondaryZ>)> ZipperValuesAt<V>
for DependentProductZipperG<'trie, PrimaryZ, SecondaryZ, V, C, F>
where
V: Clone + Send + Sync,
PrimaryZ: ZipperMoving + ZipperPath + ZipperValuesAt<V>,
SecondaryZ: ZipperMoving + ZipperPath + ZipperValuesAt<V>,
V: Clone + Send + Sync + 'trie,
PrimaryZ: ZipperMoving + ZipperPath + ZipperReadOnlyValues<'trie, V> + ZipperForking<V>,
SecondaryZ: ZipperMoving + ZipperReadOnlyValues<'trie, V> + ZipperForking<V>,
C: Clone,
{
/// The same as `descend_to(path)` then [`val`](ZipperValues::val), also across factor joints
///
/// A factor past the focus doesn't exist until a descent reaches it: the walk has the enroll
/// function make it, and drops it after. So the value must not live in that factor's zipper,
/// which is why this needs factor zippers whose values outlive them ([ZipperReadOnlyValues]).
fn val_at<K: AsRef<[u8]>>(&self, path: K) -> Option<&V> {
if let Some(idx) = self.factor_idx(true) {
self.secondary[idx].val_at(path)
} else {
self.primary.val_at(path)
let path = path.as_ref();
if path.is_empty() {
return self.val()
}
self.get_val_at_walk(path)
}
}

impl<'trie, PrimaryZ, SecondaryZ, V, C, F : Clone + for <'a> FnOnce(C, &'a [u8], usize) -> (C, Option<SecondaryZ>)> ZipperReadOnlyValues<'trie, V>
for DependentProductZipperG<'trie, PrimaryZ, SecondaryZ, V, C, F>
where
V: Clone + Send + Sync,
PrimaryZ: ZipperMoving + ZipperPath + ZipperReadOnlyValues<'trie, V>,
SecondaryZ: ZipperMoving + ZipperReadOnlyValues<'trie, V>,
V: Clone + Send + Sync + 'trie,
PrimaryZ: ZipperMoving + ZipperPath + ZipperReadOnlyValues<'trie, V> + ZipperForking<V>,
SecondaryZ: ZipperMoving + ZipperReadOnlyValues<'trie, V> + ZipperForking<V>,
C: Clone,
{
fn get_val(&self) -> Option<&'trie V> {
if let Some(idx) = self.factor_idx(true) {
Expand All @@ -271,12 +376,14 @@ impl<'trie, PrimaryZ, SecondaryZ, V, C, F : Clone + for <'a> FnOnce(C, &'a [u8],
self.primary.get_val()
}
}
/// The same as `descend_to(path)` then [`get_val`](ZipperReadOnlyValues::get_val), also across
/// factor joints
fn get_val_at<K: AsRef<[u8]>>(&self, path: K) -> Option<&'trie V> {
if let Some(idx) = self.factor_idx(true) {
self.secondary[idx].get_val_at(path)
} else {
self.primary.get_val_at(path)
let path = path.as_ref();
if path.is_empty() {
return self.get_val()
}
self.get_val_at_walk(path)
}
}

Expand Down
13 changes: 3 additions & 10 deletions src/poly_zipper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,10 @@ mod tests {

// ======================================================================================
// Cocktail of recursive zipper madness
//No `ZipperValuesAt`: `DependentProductZipperG::val_at` needs factor zippers whose values outlive
// them (`ZipperReadOnlyValues`), which `ReadZipperOwned`, and so `ExprFactor`, is not
#[derive(PolyZipperExplicit)]
#[poly_zipper_explicit(traits(Zipper, ZipperValues, ZipperValuesAt, ZipperMoving, ZipperPath, ZipperIteration))]
#[poly_zipper_explicit(traits(Zipper, ZipperValues, ZipperMoving, ZipperPath, ZipperIteration))]
pub enum ExprFactor<'trie, V: Clone + Send + Sync + Unpin + 'static = ()> {
Specific(ReadZipperOwned<V>),
Generic(PrefixZipper<'trie,
Expand All @@ -184,13 +186,4 @@ mod tests {
ExprFactor::Specific(btm.clone().into_read_zipper(path))
}
);

crate::zipper::zipper_moving_tests::zipper_val_at_tests!(recursive_zipper_madness,
|keys: &[&[u8]]| {
keys.iter().map(|k| (k, ())).collect::<PathMap<()>>()
},
|btm: &mut PathMap<()>, path: &[u8]| -> _ {
ExprFactor::Specific(btm.clone().into_read_zipper(path))
}
);
}
Loading