-
Notifications
You must be signed in to change notification settings - Fork 152
Eliminate a bunch of recursion #567
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
Merged
sanket1729
merged 5 commits into
rust-bitcoin:master
from
apoelstra:2023-07--dag-overhaul
Jul 16, 2023
+560
−287
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3c0ff73
iter: add module, copied in large from rust-simplicity
apoelstra 2930938
miniscript: eliminate recursion in for_each_key
apoelstra 8c32a19
miniscript: eliminate recursion from TranslatePk
apoelstra d858ffc
miniscript: remove recursion from script_size
apoelstra 67e91b9
miniscript: eliminate recursion in substitute_raw_pkh
apoelstra 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Written in 2023 by Andrew Poelstra <[email protected]> | ||
// SPDX-License-Identifier: CC0-1.0 | ||
|
||
//! Abstract Tree Iteration | ||
//! | ||
//! This module provides functionality to treat Miniscript objects abstractly | ||
//! as trees, iterating over them in various orders. The iterators in this | ||
//! module can be used to avoid explicitly recursive algorithms. | ||
//! | ||
|
||
mod tree; | ||
|
||
pub use tree::{ | ||
PostOrderIter, PostOrderIterItem, PreOrderIter, PreOrderIterItem, Tree, TreeLike, | ||
VerbosePreOrderIter, | ||
}; | ||
|
||
use crate::sync::Arc; | ||
use crate::{Miniscript, MiniscriptKey, ScriptContext, Terminal}; | ||
|
||
impl<'a, Pk: MiniscriptKey, Ctx: ScriptContext> TreeLike for &'a Miniscript<Pk, Ctx> { | ||
fn as_node(&self) -> Tree<Self> { | ||
match self.node { | ||
Terminal::PkK(..) | ||
| Terminal::PkH(..) | ||
| Terminal::RawPkH(..) | ||
| Terminal::After(..) | ||
| Terminal::Older(..) | ||
| Terminal::Sha256(..) | ||
| Terminal::Hash256(..) | ||
| Terminal::Ripemd160(..) | ||
| Terminal::Hash160(..) | ||
| Terminal::True | ||
| Terminal::False | ||
| Terminal::Multi(..) | ||
| Terminal::MultiA(..) => Tree::Nullary, | ||
Terminal::Alt(ref sub) | ||
| Terminal::Swap(ref sub) | ||
| Terminal::Check(ref sub) | ||
| Terminal::DupIf(ref sub) | ||
| Terminal::Verify(ref sub) | ||
| Terminal::NonZero(ref sub) | ||
| Terminal::ZeroNotEqual(ref sub) => Tree::Unary(sub), | ||
Terminal::AndV(ref left, ref right) | ||
| Terminal::AndB(ref left, ref right) | ||
| Terminal::OrB(ref left, ref right) | ||
| Terminal::OrD(ref left, ref right) | ||
| Terminal::OrC(ref left, ref right) | ||
| Terminal::OrI(ref left, ref right) => Tree::Binary(left, right), | ||
Terminal::AndOr(ref a, ref b, ref c) => Tree::Nary(Arc::from([a.as_ref(), b, c])), | ||
Terminal::Thresh(_, ref subs) => Tree::Nary(subs.iter().map(Arc::as_ref).collect()), | ||
} | ||
} | ||
} | ||
|
||
impl<Pk: MiniscriptKey, Ctx: ScriptContext> TreeLike for Arc<Miniscript<Pk, Ctx>> { | ||
fn as_node(&self) -> Tree<Self> { | ||
match self.node { | ||
Terminal::PkK(..) | ||
| Terminal::PkH(..) | ||
| Terminal::RawPkH(..) | ||
| Terminal::After(..) | ||
| Terminal::Older(..) | ||
| Terminal::Sha256(..) | ||
| Terminal::Hash256(..) | ||
| Terminal::Ripemd160(..) | ||
| Terminal::Hash160(..) | ||
| Terminal::True | ||
| Terminal::False | ||
| Terminal::Multi(..) | ||
| Terminal::MultiA(..) => Tree::Nullary, | ||
Terminal::Alt(ref sub) | ||
| Terminal::Swap(ref sub) | ||
| Terminal::Check(ref sub) | ||
| Terminal::DupIf(ref sub) | ||
| Terminal::Verify(ref sub) | ||
| Terminal::NonZero(ref sub) | ||
| Terminal::ZeroNotEqual(ref sub) => Tree::Unary(Arc::clone(sub)), | ||
Terminal::AndV(ref left, ref right) | ||
| Terminal::AndB(ref left, ref right) | ||
| Terminal::OrB(ref left, ref right) | ||
| Terminal::OrD(ref left, ref right) | ||
| Terminal::OrC(ref left, ref right) | ||
| Terminal::OrI(ref left, ref right) => { | ||
Tree::Binary(Arc::clone(left), Arc::clone(right)) | ||
} | ||
Terminal::AndOr(ref a, ref b, ref c) => { | ||
Tree::Nary(Arc::from([Arc::clone(a), Arc::clone(b), Arc::clone(c)])) | ||
} | ||
Terminal::Thresh(_, ref subs) => Tree::Nary(subs.iter().map(Arc::clone).collect()), | ||
} | ||
} | ||
} |
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.
Hey @apoelstra, I saw this line and wondered if you added it as a habit or if I have been annoying you by removing them? Or was this PR just before we discussed all that? Cheers.
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 it predates your PR to remove these. I would've just mildlessly copied the line from other files.
I'm certainly not annoyed :).
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.
Sweet man, cheers.