-
Notifications
You must be signed in to change notification settings - Fork 20
Implement iter_arc method for List
#71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
PoulavBhowmick03
wants to merge
4
commits into
sigp:main
Choose a base branch
from
PoulavBhowmick03:list/iter_arc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c7b3b27
Implement iter_arc method for List
PoulavBhowmick03 dc9fac4
conflicts and clippy
PoulavBhowmick03 f4c9ead
added helper struct for handling pending updates
PoulavBhowmick03 a8ae440
Added new type ArcMap implementing UpdateMap for proper Arc implement…
PoulavBhowmick03 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| use tree_hash::{TreeHash, TreeHashType}; | ||
| use triomphe::Arc; | ||
|
|
||
| use crate::{ | ||
| Error, Leaf, Tree, UpdateMap, Value, | ||
| utils::{Length, opt_packing_depth}, | ||
| }; | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct ArcIter<'a, T: Value> { | ||
| /// Stack of tree nodes corresponding to the current position. | ||
| stack: Vec<&'a Tree<T>>, | ||
| /// The list index corresponding to the current position (next element to be yielded). | ||
| index: usize, | ||
| /// The `depth` of the root tree. | ||
| full_depth: usize, | ||
| /// Cached packing depth to avoid re-calculating `opt_packing_depth`. | ||
| packing_depth: usize, | ||
| /// Number of items that will be yielded by the iterator. | ||
| length: Length, | ||
| } | ||
|
|
||
| impl<'a, T: Value> ArcIter<'a, T> { | ||
| pub fn from_index( | ||
| index: usize, | ||
| root: &'a Tree<T>, | ||
| depth: usize, | ||
| length: Length, | ||
| ) -> Result<Self, Error> { | ||
| if <T as TreeHash>::tree_hash_type() == TreeHashType::Basic { | ||
| return Err(Error::PackedLeavesNoArc); | ||
| } | ||
| let mut stack = Vec::with_capacity(depth); | ||
| stack.push(root); | ||
|
|
||
| Ok(ArcIter { | ||
| stack, | ||
| index, | ||
| full_depth: depth, | ||
| packing_depth: opt_packing_depth::<T>().unwrap_or(0), | ||
| length, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| impl<'a, T: Value> ArcIter<'a, T> { | ||
| pub fn new(root: &'a Tree<T>, depth: usize, length: Length) -> Self { | ||
| let mut stack = Vec::with_capacity(depth); | ||
| stack.push(root); | ||
|
|
||
| ArcIter { | ||
| stack, | ||
| index: 0, | ||
| full_depth: depth, | ||
| packing_depth: opt_packing_depth::<T>().unwrap_or(0), | ||
| length, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<'a, T: Value> Iterator for ArcIter<'a, T> { | ||
| type Item = &'a Arc<T>; | ||
|
|
||
| fn next(&mut self) -> Option<Self::Item> { | ||
| if self.index >= self.length.as_usize() { | ||
| return None; | ||
| } | ||
|
|
||
| match self.stack.last() { | ||
| None | Some(Tree::Zero(_)) => None, | ||
| Some(Tree::Leaf(Leaf { value, .. })) => { | ||
| let result = Some(value); | ||
|
|
||
| self.index += 1; | ||
|
|
||
| // Backtrack to the parent node of the next subtree | ||
| for _ in 0..=self.index.trailing_zeros() { | ||
| self.stack.pop(); | ||
| } | ||
|
|
||
| result | ||
| } | ||
| Some(Tree::PackedLeaf(_)) => { | ||
michaelsproul marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Return None case of PackedLeaf | ||
| None | ||
| } | ||
| Some(Tree::Node { left, right, .. }) => { | ||
| let depth = self.full_depth - self.stack.len(); | ||
|
|
||
| // Go left | ||
| if (self.index >> (depth + self.packing_depth)) & 1 == 0 { | ||
| self.stack.push(left); | ||
| self.next() | ||
| } | ||
| // Go right | ||
| else { | ||
| self.stack.push(right); | ||
| self.next() | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn size_hint(&self) -> (usize, Option<usize>) { | ||
| let remaining = self.length.as_usize().saturating_sub(self.index); | ||
| (remaining, Some(remaining)) | ||
| } | ||
| } | ||
|
|
||
| impl<T: Value> ExactSizeIterator for ArcIter<'_, T> {} | ||
| #[derive(Debug)] | ||
| pub struct ArcInterfaceIter<'a, T: Value, U: UpdateMap<T>> { | ||
| tree_iter: ArcIter<'a, T>, | ||
| updates: &'a U, | ||
| index: usize, | ||
| length: usize, | ||
| } | ||
|
|
||
| impl<'a, T: Value, U: UpdateMap<T>> ArcInterfaceIter<'a, T, U> { | ||
| pub fn new(root: &'a Tree<T>, depth: usize, length: Length, updates: &'a U) -> Self { | ||
| ArcInterfaceIter { | ||
| tree_iter: ArcIter::new(root, depth, length), | ||
| updates, | ||
| index: 0, | ||
| length: length.as_usize(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<'a, T: Value, U: UpdateMap<T>> Iterator for ArcInterfaceIter<'a, T, U> { | ||
| type Item = Arc<T>; | ||
|
|
||
| fn next(&mut self) -> Option<Self::Item> { | ||
| if self.index >= self.length { | ||
| return None; | ||
| } | ||
| let idx = self.index; | ||
| self.index += 1; | ||
|
|
||
| let backing = self.tree_iter.next(); | ||
| if let Some(new_val) = self.updates.get(idx) { | ||
| Some( | ||
| self.updates | ||
| .get_arc(idx) | ||
| .unwrap_or_else(|| Arc::new(new_val.clone())), | ||
| ) | ||
| } else { | ||
| backing.cloned() | ||
| } | ||
| } | ||
|
|
||
| fn size_hint(&self) -> (usize, Option<usize>) { | ||
| let rem = self.length.saturating_sub(self.index); | ||
| (rem, Some(rem)) | ||
| } | ||
| } | ||
| impl<T: Value, U: UpdateMap<T>> ExactSizeIterator for ArcInterfaceIter<'_, T, U> {} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this might be wrong in the case where there are pending updates. The proptest should have caught this
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking to change it to use
ArcInterfaceIter, which handles the pending updates. But in that case, I will have to change theImmList<>and include<U: UpdateMap<T>>to it. what would you suggest is the way to go about this?