@@ -762,17 +762,13 @@ impl Property for ExtData {
762
762
let mut ops_count = 0 ;
763
763
let mut ops_count_sat_vec = Vec :: with_capacity ( n) ;
764
764
let mut ops_count_nsat_sum = 0 ;
765
- let mut op_count_sat = Some ( 0 ) ;
766
765
let mut timelocks = Vec :: with_capacity ( n) ;
767
766
let mut stack_elem_count_sat_vec = Vec :: with_capacity ( n) ;
768
- let mut stack_elem_count_sat = Some ( 0 ) ;
769
767
let mut stack_elem_count_dissat = Some ( 0 ) ;
770
768
let mut max_sat_size_vec = Vec :: with_capacity ( n) ;
771
- let mut max_sat_size = Some ( ( 0 , 0 ) ) ;
772
769
let mut max_dissat_size = Some ( ( 0 , 0 ) ) ;
773
770
// the max element count is same as max sat element count when satisfying one element + 1
774
771
let mut exec_stack_elem_count_sat_vec = Vec :: with_capacity ( n) ;
775
- let mut exec_stack_elem_count_sat = Some ( 0 ) ;
776
772
let mut exec_stack_elem_count_dissat = Some ( 0 ) ;
777
773
778
774
for i in 0 ..n {
@@ -810,42 +806,59 @@ impl Property for ExtData {
810
806
}
811
807
812
808
stack_elem_count_sat_vec. sort_by ( sat_minus_option_dissat) ;
813
- // Sum of the cost by iterating through the sorted vector *backward*.
814
- for ( i, & ( x, y) ) in stack_elem_count_sat_vec. iter ( ) . rev ( ) . enumerate ( ) {
815
- stack_elem_count_sat = if i <= k {
816
- x. and_then ( |x| stack_elem_count_sat. map ( |count| count + x) )
817
- } else {
818
- y. and_then ( |y| stack_elem_count_sat. map ( |count| count + y) )
819
- } ;
820
- }
809
+ let stack_elem_count_sat =
810
+ stack_elem_count_sat_vec
811
+ . iter ( )
812
+ . rev ( )
813
+ . enumerate ( )
814
+ . fold ( Some ( 0 ) , |acc, ( i, & ( x, y) ) | {
815
+ if i <= k {
816
+ opt_add ( acc, x)
817
+ } else {
818
+ opt_add ( acc, y)
819
+ }
820
+ } ) ;
821
821
822
822
exec_stack_elem_count_sat_vec. sort_by ( sat_minus_option_dissat) ;
823
- for ( i, & ( x, y) ) in exec_stack_elem_count_sat_vec. iter ( ) . rev ( ) . enumerate ( ) {
824
- exec_stack_elem_count_sat = if i <= k {
825
- opt_max ( exec_stack_elem_count_sat, x)
826
- } else {
827
- opt_max ( exec_stack_elem_count_sat, y)
828
- } ;
829
- }
823
+ let exec_stack_elem_count_sat = exec_stack_elem_count_sat_vec
824
+ . iter ( )
825
+ . rev ( )
826
+ . enumerate ( )
827
+ . fold ( Some ( 0 ) , |acc, ( i, & ( x, y) ) | {
828
+ if i <= k {
829
+ opt_max ( acc, x)
830
+ } else {
831
+ opt_max ( acc, y)
832
+ }
833
+ } ) ;
830
834
831
835
// FIXME: Maybe make the ExtData struct aware of Ctx and add a one_cost() method here ?
832
836
max_sat_size_vec. sort_by ( sat_minus_dissat_witness) ;
833
- for ( i, & ( x, y) ) in max_sat_size_vec. iter ( ) . enumerate ( ) {
834
- max_sat_size = if i <= k {
835
- x. and_then ( |x| max_sat_size. map ( |( w, s) | ( w + x. 0 , s + x. 1 ) ) )
836
- } else {
837
- y. and_then ( |y| max_sat_size. map ( |( w, s) | ( w + y. 0 , s + y. 1 ) ) )
838
- } ;
839
- }
837
+ let max_sat_size =
838
+ max_sat_size_vec
839
+ . iter ( )
840
+ . enumerate ( )
841
+ . fold ( Some ( ( 0 , 0 ) ) , |acc, ( i, & ( x, y) ) | {
842
+ if i <= k {
843
+ opt_tuple_add ( acc, x)
844
+ } else {
845
+ opt_tuple_add ( acc, y)
846
+ }
847
+ } ) ;
840
848
841
849
ops_count_sat_vec. sort_by ( sat_minus_dissat) ;
842
- for ( i, & ( x, y) ) in ops_count_sat_vec. iter ( ) . enumerate ( ) {
843
- op_count_sat = if i <= k {
844
- opt_add ( op_count_sat, x)
845
- } else {
846
- opt_add ( op_count_sat, Some ( y) )
847
- } ;
848
- }
850
+ let op_count_sat =
851
+ ops_count_sat_vec
852
+ . iter ( )
853
+ . enumerate ( )
854
+ . fold ( Some ( 0 ) , |acc, ( i, & ( x, y) ) | {
855
+ if i <= k {
856
+ opt_add ( acc, x)
857
+ } else {
858
+ opt_add ( acc, Some ( y) )
859
+ }
860
+ } ) ;
861
+
849
862
Ok ( ExtData {
850
863
pk_cost : pk_cost + n - 1 , //all pk cost + (n-1)*ADD
851
864
has_free_verify : true ,
@@ -1043,7 +1056,7 @@ fn sat_minus_dissat_witness<'r, 's>(
1043
1056
. cmp ( & b. 0 . map ( |x| b. 1 . map ( |y| x. 0 as isize - y. 0 as isize ) ) )
1044
1057
}
1045
1058
1046
- // Returns Some(max(x,y)) is both x and y are Some. Otherwise, return none
1059
+ /// Returns Some(max(x,y)) is both x and y are Some. Otherwise, returns `None`.
1047
1060
fn opt_max < T : Ord > ( a : Option < T > , b : Option < T > ) -> Option < T > {
1048
1061
if let ( Some ( x) , Some ( y) ) = ( a, b) {
1049
1062
Some ( cmp:: max ( x, y) )
@@ -1052,7 +1065,12 @@ fn opt_max<T: Ord>(a: Option<T>, b: Option<T>) -> Option<T> {
1052
1065
}
1053
1066
}
1054
1067
1055
- // Returns Some(x+y) is both x and y are Some. Otherwise, return none
1068
+ /// Returns Some(x+y) is both x and y are Some. Otherwise, returns `None`.
1056
1069
fn opt_add ( a : Option < usize > , b : Option < usize > ) -> Option < usize > {
1057
1070
a. and_then ( |x| b. map ( |y| x + y) )
1058
1071
}
1072
+
1073
+ /// Returns Some((x0+y0, x1+y1)) is both x and y are Some. Otherwise, returns `None`.
1074
+ fn opt_tuple_add ( a : Option < ( usize , usize ) > , b : Option < ( usize , usize ) > ) -> Option < ( usize , usize ) > {
1075
+ a. and_then ( |x| b. map ( |( w, s) | ( w + x. 0 , s + x. 1 ) ) )
1076
+ }
0 commit comments