@@ -775,17 +775,13 @@ impl Property for ExtData {
775
775
let mut ops_count = 0 ;
776
776
let mut ops_count_sat_vec = Vec :: with_capacity ( n) ;
777
777
let mut ops_count_nsat_sum = 0 ;
778
- let mut op_count_sat = Some ( 0 ) ;
779
778
let mut timelocks = Vec :: with_capacity ( n) ;
780
779
let mut stack_elem_count_sat_vec = Vec :: with_capacity ( n) ;
781
- let mut stack_elem_count_sat = Some ( 0 ) ;
782
780
let mut stack_elem_count_dissat = Some ( 0 ) ;
783
781
let mut max_sat_size_vec = Vec :: with_capacity ( n) ;
784
- let mut max_sat_size = Some ( ( 0 , 0 ) ) ;
785
782
let mut max_dissat_size = Some ( ( 0 , 0 ) ) ;
786
783
// the max element count is same as max sat element count when satisfying one element + 1
787
784
let mut exec_stack_elem_count_sat_vec = Vec :: with_capacity ( n) ;
788
- let mut exec_stack_elem_count_sat = Some ( 0 ) ;
789
785
let mut exec_stack_elem_count_dissat = Some ( 0 ) ;
790
786
791
787
for i in 0 ..n {
@@ -823,42 +819,59 @@ impl Property for ExtData {
823
819
}
824
820
825
821
stack_elem_count_sat_vec. sort_by ( sat_minus_option_dissat) ;
826
- // Sum of the cost by iterating through the sorted vector *backward*.
827
- for ( i, & ( x, y) ) in stack_elem_count_sat_vec. iter ( ) . rev ( ) . enumerate ( ) {
828
- stack_elem_count_sat = if i <= k {
829
- x. and_then ( |x| stack_elem_count_sat. map ( |count| count + x) )
830
- } else {
831
- y. and_then ( |y| stack_elem_count_sat. map ( |count| count + y) )
832
- } ;
833
- }
822
+ let stack_elem_count_sat =
823
+ 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_add ( acc, x)
830
+ } else {
831
+ opt_add ( acc, y)
832
+ }
833
+ } ) ;
834
834
835
835
exec_stack_elem_count_sat_vec. sort_by ( sat_minus_option_dissat) ;
836
- for ( i, & ( x, y) ) in exec_stack_elem_count_sat_vec. iter ( ) . rev ( ) . enumerate ( ) {
837
- exec_stack_elem_count_sat = if i <= k {
838
- opt_max ( exec_stack_elem_count_sat, x)
839
- } else {
840
- opt_max ( exec_stack_elem_count_sat, y)
841
- } ;
842
- }
836
+ let exec_stack_elem_count_sat = exec_stack_elem_count_sat_vec
837
+ . iter ( )
838
+ . rev ( )
839
+ . enumerate ( )
840
+ . fold ( Some ( 0 ) , |acc, ( i, & ( x, y) ) | {
841
+ if i <= k {
842
+ opt_max ( acc, x)
843
+ } else {
844
+ opt_max ( acc, y)
845
+ }
846
+ } ) ;
843
847
844
848
// FIXME: Maybe make the ExtData struct aware of Ctx and add a one_cost() method here ?
845
849
max_sat_size_vec. sort_by ( sat_minus_dissat_witness) ;
846
- for ( i, & ( x, y) ) in max_sat_size_vec. iter ( ) . enumerate ( ) {
847
- max_sat_size = if i <= k {
848
- x. and_then ( |x| max_sat_size. map ( |( w, s) | ( w + x. 0 , s + x. 1 ) ) )
849
- } else {
850
- y. and_then ( |y| max_sat_size. map ( |( w, s) | ( w + y. 0 , s + y. 1 ) ) )
851
- } ;
852
- }
850
+ let max_sat_size =
851
+ max_sat_size_vec
852
+ . iter ( )
853
+ . enumerate ( )
854
+ . fold ( Some ( ( 0 , 0 ) ) , |acc, ( i, & ( x, y) ) | {
855
+ if i <= k {
856
+ opt_tuple_add ( acc, x)
857
+ } else {
858
+ opt_tuple_add ( acc, y)
859
+ }
860
+ } ) ;
853
861
854
862
ops_count_sat_vec. sort_by ( sat_minus_dissat) ;
855
- for ( i, & ( x, y) ) in ops_count_sat_vec. iter ( ) . enumerate ( ) {
856
- op_count_sat = if i <= k {
857
- opt_add ( op_count_sat, x)
858
- } else {
859
- opt_add ( op_count_sat, Some ( y) )
860
- } ;
861
- }
863
+ let op_count_sat =
864
+ ops_count_sat_vec
865
+ . iter ( )
866
+ . enumerate ( )
867
+ . fold ( Some ( 0 ) , |acc, ( i, & ( x, y) ) | {
868
+ if i <= k {
869
+ opt_add ( acc, x)
870
+ } else {
871
+ opt_add ( acc, Some ( y) )
872
+ }
873
+ } ) ;
874
+
862
875
Ok ( ExtData {
863
876
pk_cost : pk_cost + n - 1 , //all pk cost + (n-1)*ADD
864
877
has_free_verify : true ,
@@ -1056,7 +1069,7 @@ fn sat_minus_dissat_witness<'r, 's>(
1056
1069
. cmp ( & b. 0 . map ( |x| b. 1 . map ( |y| x. 0 as isize - y. 0 as isize ) ) )
1057
1070
}
1058
1071
1059
- // Returns Some(max(x,y)) is both x and y are Some. Otherwise, return none
1072
+ /// Returns Some(max(x,y)) is both x and y are Some. Otherwise, returns `None`.
1060
1073
fn opt_max < T : Ord > ( a : Option < T > , b : Option < T > ) -> Option < T > {
1061
1074
if let ( Some ( x) , Some ( y) ) = ( a, b) {
1062
1075
Some ( cmp:: max ( x, y) )
@@ -1065,7 +1078,12 @@ fn opt_max<T: Ord>(a: Option<T>, b: Option<T>) -> Option<T> {
1065
1078
}
1066
1079
}
1067
1080
1068
- // Returns Some(x+y) is both x and y are Some. Otherwise, return none
1081
+ /// Returns Some(x+y) is both x and y are Some. Otherwise, returns `None`.
1069
1082
fn opt_add ( a : Option < usize > , b : Option < usize > ) -> Option < usize > {
1070
1083
a. and_then ( |x| b. map ( |y| x + y) )
1071
1084
}
1085
+
1086
+ /// Returns Some((x0+y0, x1+y1)) is both x and y are Some. Otherwise, returns `None`.
1087
+ fn opt_tuple_add ( a : Option < ( usize , usize ) > , b : Option < ( usize , usize ) > ) -> Option < ( usize , usize ) > {
1088
+ a. and_then ( |x| b. map ( |( w, s) | ( w + x. 0 , s + x. 1 ) ) )
1089
+ }
0 commit comments