Skip to content

Commit 2c3d41d

Browse files
committed
Refactor to use parent/child notation
In `normalized` we process the threshold policy as well as its child threshold policy and we shadow `subs` - this is all very confusing. Add parent/child terminology and a couple extra local variables in an attempt to make the code more clear. Refactor only, no logic changes.
1 parent d3d4564 commit 2c3d41d

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

src/policy/semantic.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -422,18 +422,27 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
422422
let n = subs.len() - unsatisfied_count - trivial_count; // remove all true/false
423423
let m = k.checked_sub(trivial_count).unwrap_or(0); // satisfy all trivial
424424

425-
let is_and = m == n;
426-
let is_or = m == 1;
425+
let parent_is_and = m == n; // (n, n)-thresh is an AND
426+
let parent_is_or = m == 1; // (1, n)-thresh is an OR
427427

428428
for sub in subs {
429429
match sub.as_ref() {
430430
Policy::Trivial | Policy::Unsatisfiable => {}
431431
Policy::Threshold(ref k, ref subs) => {
432-
match (is_and, is_or) {
433-
(true, true) => ret_subs.push(Arc::clone(&sub)), // m = n = 1, thresh(1,X) type thing.
434-
(true, false) if *k == subs.len() => ret_subs.extend(subs.to_vec()), // and case
435-
(false, true) if *k == 1 => ret_subs.extend(subs.to_vec()), // or case
436-
_ => ret_subs.push(Arc::clone(&sub)),
432+
let child_is_and = *k == subs.len();
433+
let child_is_or = *k == 1;
434+
435+
if parent_is_and && parent_is_or {
436+
// m = n = 1, child must be the non-trivial, non-unsatisfiable node.
437+
ret_subs.push(Arc::clone(&sub));
438+
} else if parent_is_and && child_is_and {
439+
// If both parent and child are ANDs we can flatten them.
440+
subs.iter().for_each(|sub| ret_subs.push(Arc::clone(sub)));
441+
} else if parent_is_or && child_is_or {
442+
// If both parent and child are ORs we can flatten them.
443+
subs.iter().for_each(|sub| ret_subs.push(Arc::clone(sub)));
444+
} else {
445+
ret_subs.push(Arc::clone(&sub));
437446
}
438447
}
439448
_ => ret_subs.push(Arc::clone(&sub)),
@@ -447,9 +456,9 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
447456
} else if ret_subs.len() == 1 {
448457
let policy = ret_subs.pop().unwrap();
449458
(*policy).clone() // More than one strong reference, can't use `try_unwrap()`.
450-
} else if is_and {
459+
} else if parent_is_and {
451460
Policy::Threshold(ret_subs.len(), ret_subs)
452-
} else if is_or {
461+
} else if parent_is_or {
453462
Policy::Threshold(1, ret_subs)
454463
} else {
455464
Policy::Threshold(m, ret_subs)

0 commit comments

Comments
 (0)