Skip to content

Commit 67d6ff7

Browse files
committed
iter: introduce Ternary variant
It's lazy and takes some extra allocations to use Nary for the andor combinator; introduce a dedicated Ternary variant for this. This also means that Nary will be exclusively used for thresholds, which should give us some more freedom to mess with it.
1 parent b729fdd commit 67d6ff7

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/iter/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl<'a, Pk: MiniscriptKey, Ctx: ScriptContext> TreeLike for &'a Miniscript<Pk,
3636
| OrD(ref left, ref right)
3737
| OrC(ref left, ref right)
3838
| OrI(ref left, ref right) => Tree::Binary(left, right),
39-
AndOr(ref a, ref b, ref c) => Tree::Nary(Arc::from([a.as_ref(), b, c])),
39+
AndOr(ref a, ref b, ref c) => Tree::Ternary(a, b, c),
4040
Thresh(ref thresh) => Tree::Nary(thresh.iter().map(Arc::as_ref).collect()),
4141
}
4242
}
@@ -62,7 +62,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> TreeLike for Arc<Miniscript<Pk, Ctx>
6262
| OrC(ref left, ref right)
6363
| OrI(ref left, ref right) => Tree::Binary(Arc::clone(left), Arc::clone(right)),
6464
AndOr(ref a, ref b, ref c) => {
65-
Tree::Nary(Arc::from([Arc::clone(a), Arc::clone(b), Arc::clone(c)]))
65+
Tree::Ternary(Arc::clone(a), Arc::clone(b), Arc::clone(c))
6666
}
6767
Thresh(ref thresh) => Tree::Nary(thresh.iter().map(Arc::clone).collect()),
6868
}

src/iter/tree.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ pub enum Tree<T> {
2020
Unary(T),
2121
/// Combinator with two children.
2222
Binary(T, T),
23+
/// Combinator with two children.
24+
Ternary(T, T, T),
2325
/// Combinator with more than two children.
2426
Nary(Arc<[T]>),
2527
}
@@ -45,6 +47,7 @@ pub trait TreeLike: Clone + Sized {
4547
Tree::Nullary => 0,
4648
Tree::Unary(..) => 1,
4749
Tree::Binary(..) => 2,
50+
Tree::Ternary(..) => 3,
4851
Tree::Nary(children) => children.len(),
4952
}
5053
}
@@ -58,6 +61,10 @@ pub trait TreeLike: Clone + Sized {
5861
(0, Tree::Binary(sub, _)) => Some(sub),
5962
(1, Tree::Binary(_, sub)) => Some(sub),
6063
(_, Tree::Binary(..)) => None,
64+
(0, Tree::Ternary(sub, _, _)) => Some(sub),
65+
(1, Tree::Ternary(_, sub, _)) => Some(sub),
66+
(2, Tree::Ternary(_, _, sub)) => Some(sub),
67+
(_, Tree::Ternary(..)) => None,
6168
(n, Tree::Nary(children)) => children.get(n).cloned(),
6269
}
6370
}
@@ -210,6 +217,11 @@ impl<T: TreeLike> Iterator for PreOrderIter<T> {
210217
self.stack.push(right);
211218
self.stack.push(left);
212219
}
220+
Tree::Ternary(a, b, c) => {
221+
self.stack.push(c);
222+
self.stack.push(b);
223+
self.stack.push(a);
224+
}
213225
Tree::Nary(children) => {
214226
self.stack.extend(children.iter().rev().cloned());
215227
}

0 commit comments

Comments
 (0)