@@ -430,19 +430,28 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
430
430
let n = subs. len ( ) - unsatisfied_count - trivial_count; // remove all true/false
431
431
let m = k. checked_sub ( trivial_count) . unwrap_or ( 0 ) ; // satisfy all trivial
432
432
433
- // m == n denotes `and` and m == 1 denotes `or`
434
- let is_and = m == n;
435
- let is_or = m == 1 ;
433
+ // Call the current threshold "parent" to differentiate it from its "child" threshold below.
434
+ let parent_is_and = m == n; // (n, n)-thresh is an AND
435
+ let parent_is_or = m == 1 ; // (1, n)-thresh is an OR
436
436
437
437
for sub in subs {
438
438
match sub. as_ref ( ) {
439
439
Policy :: Trivial | Policy :: Unsatisfiable => { }
440
440
Policy :: Threshold ( ref k, ref subs) => {
441
- match ( is_and, is_or) {
442
- ( true , true ) => ret_subs. push ( Arc :: clone ( & sub) ) , // m = n = 1, thresh(1,X) type thing.
443
- ( true , false ) if * k == subs. len ( ) => ret_subs. extend ( subs. to_vec ( ) ) , // and case
444
- ( false , true ) if * k == 1 => ret_subs. extend ( subs. to_vec ( ) ) , // or case
445
- _ => ret_subs. push ( Arc :: clone ( & sub) ) ,
441
+ let child_is_and = * k == subs. len ( ) ;
442
+ let child_is_or = * k == 1 ;
443
+
444
+ if parent_is_and && parent_is_or {
445
+ // m = n = 1, child must be the non-trivial, non-unsatisfiable node.
446
+ ret_subs. push ( Arc :: clone ( & sub) ) ;
447
+ } else if parent_is_and && child_is_and {
448
+ // If both parent and child are ANDs we can flatten them.
449
+ subs. iter ( ) . for_each ( |sub| ret_subs. push ( Arc :: clone ( sub) ) ) ;
450
+ } else if parent_is_or && child_is_or {
451
+ // If both parent and child are ORs we can flatten them.
452
+ subs. iter ( ) . for_each ( |sub| ret_subs. push ( Arc :: clone ( sub) ) ) ;
453
+ } else {
454
+ ret_subs. push ( Arc :: clone ( & sub) ) ;
446
455
}
447
456
}
448
457
_ => ret_subs. push ( Arc :: clone ( & sub) ) ,
@@ -456,9 +465,9 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
456
465
} else if ret_subs. len ( ) == 1 {
457
466
let policy = ret_subs. pop ( ) . unwrap ( ) ;
458
467
( * policy) . clone ( ) // I'm lost now, can we try_unwrap still?
459
- } else if is_and {
468
+ } else if parent_is_and {
460
469
Policy :: Threshold ( ret_subs. len ( ) , ret_subs)
461
- } else if is_or {
470
+ } else if parent_is_or {
462
471
Policy :: Threshold ( 1 , ret_subs)
463
472
} else {
464
473
Policy :: Threshold ( m, ret_subs)
0 commit comments