Skip to content

Commit

Permalink
semantic: Remove recursion in sorted
Browse files Browse the repository at this point in the history
Done as part of the effort to remove all the recursion crate wide.

Use the `TreeLike` trait to iterate over policy nodes and remove the
recursive call in `semantic::Policy::sorted`.
  • Loading branch information
tcharding committed Oct 13, 2023
1 parent 2ed5aef commit 81055a4
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions src/policy/semantic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,17 +649,29 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
/// in general this appears to require Gröbner basis techniques that are not
/// implemented.
pub fn sorted(self) -> Policy<Pk> {
match self {
Policy::Threshold(k, subs) => {
let mut new_subs: Vec<_> = subs
.into_iter()
.map(|p| Arc::new(p.as_ref().clone().sorted()))
.collect();
new_subs.sort();
Policy::Threshold(k, new_subs)
use Policy::*;

let mut sorted = vec![];
for data in Arc::new(self).post_order_iter() {
let child_n = |n| Arc::clone(&sorted[data.child_indices[n]]);

let new_policy = match data.node.as_ref() {
Threshold(k, ref subs) => {
let mut subs = (0..subs.len()).map(child_n).collect::<Vec<_>>();
subs.sort();
Some(Threshold(*k, subs))
}
_ => None,
};
match new_policy {
Some(new_policy) => sorted.push(Arc::new(new_policy)),
None => sorted.push(Arc::clone(&data.node)),
}
x => x,
}
// Unwrap is ok because we know we processed at least one node.
let root_node = sorted.pop().unwrap();
// Unwrap is ok because we know `root_node` is the only strong reference.
Arc::try_unwrap(root_node).unwrap()
}
}

Expand Down

0 comments on commit 81055a4

Please sign in to comment.