@@ -809,13 +809,8 @@ impl Property for ExtData {
809
809
) ;
810
810
}
811
811
812
- // We sort by [satisfaction cost - dissatisfaction cost] to make a worst-case (the most
813
- // costy satisfaction are satisfied, the most costy dissatisfactions are dissatisfied)
814
- // sum of the cost by iterating through the sorted vector *backward*.
815
- stack_elem_count_sat_vec. sort_by ( |a, b| {
816
- a. 0 . map ( |x| a. 1 . map ( |y| x as isize - y as isize ) )
817
- . cmp ( & b. 0 . map ( |x| b. 1 . map ( |y| x as isize - y as isize ) ) )
818
- } ) ;
812
+ stack_elem_count_sat_vec. sort_by ( sat_minus_option_dissat) ;
813
+ // Sum of the cost by iterating through the sorted vector *backward*.
819
814
for ( i, & ( x, y) ) in stack_elem_count_sat_vec. iter ( ) . rev ( ) . enumerate ( ) {
820
815
stack_elem_count_sat = if i <= k {
821
816
x. and_then ( |x| stack_elem_count_sat. map ( |count| count + x) )
@@ -824,11 +819,7 @@ impl Property for ExtData {
824
819
} ;
825
820
}
826
821
827
- // Same logic as above
828
- exec_stack_elem_count_sat_vec. sort_by ( |a, b| {
829
- a. 0 . map ( |x| a. 1 . map ( |y| x as isize - y as isize ) )
830
- . cmp ( & b. 0 . map ( |x| b. 1 . map ( |y| x as isize - y as isize ) ) )
831
- } ) ;
822
+ exec_stack_elem_count_sat_vec. sort_by ( sat_minus_option_dissat) ;
832
823
for ( i, & ( x, y) ) in exec_stack_elem_count_sat_vec. iter ( ) . rev ( ) . enumerate ( ) {
833
824
exec_stack_elem_count_sat = if i <= k {
834
825
opt_max ( exec_stack_elem_count_sat, x)
@@ -837,14 +828,8 @@ impl Property for ExtData {
837
828
} ;
838
829
}
839
830
840
- // Same for the size cost. A bit more intricated as we need to account for both the witness
841
- // and scriptSig cost, so we end up with a tuple of Options of tuples. We use the witness
842
- // cost (first element of the mentioned tuple) here.
843
831
// FIXME: Maybe make the ExtData struct aware of Ctx and add a one_cost() method here ?
844
- max_sat_size_vec. sort_by ( |a, b| {
845
- a. 0 . map ( |x| a. 1 . map ( |y| x. 0 as isize - y. 0 as isize ) )
846
- . cmp ( & b. 0 . map ( |x| b. 1 . map ( |y| x. 0 as isize - y. 0 as isize ) ) )
847
- } ) ;
832
+ max_sat_size_vec. sort_by ( sat_minus_dissat_witness) ;
848
833
for ( i, & ( x, y) ) in max_sat_size_vec. iter ( ) . enumerate ( ) {
849
834
max_sat_size = if i <= k {
850
835
x. and_then ( |x| max_sat_size. map ( |( w, s) | ( w + x. 0 , s + x. 1 ) ) )
@@ -853,10 +838,7 @@ impl Property for ExtData {
853
838
} ;
854
839
}
855
840
856
- ops_count_sat_vec. sort_by ( |a, b| {
857
- a. 0 . map ( |x| x as isize - a. 1 as isize )
858
- . cmp ( & b. 0 . map ( |x| x as isize - b. 1 as isize ) )
859
- } ) ;
841
+ ops_count_sat_vec. sort_by ( sat_minus_dissat) ;
860
842
for ( i, & ( x, y) ) in ops_count_sat_vec. iter ( ) . enumerate ( ) {
861
843
op_count_sat = if i <= k {
862
844
opt_add ( op_count_sat, x)
@@ -1021,6 +1003,46 @@ impl Property for ExtData {
1021
1003
}
1022
1004
}
1023
1005
1006
+ // Function to pass to sort_by. Sort by (satisfaction cost - dissatisfaction cost).
1007
+ //
1008
+ // We sort by (satisfaction cost - dissatisfaction cost) to make a worst-case (the most
1009
+ // costy satisfactions are satisfied, the most costy dissatisfactions are dissatisfied).
1010
+ //
1011
+ // Args are of form: (<count_sat>, <count_dissat>)
1012
+ fn sat_minus_dissat < ' r , ' s > (
1013
+ a : & ' r ( Option < usize > , usize ) ,
1014
+ b : & ' s ( Option < usize > , usize ) ,
1015
+ ) -> std:: cmp:: Ordering {
1016
+ a. 0 . map ( |x| x as isize - a. 1 as isize )
1017
+ . cmp ( & b. 0 . map ( |x| x as isize - b. 1 as isize ) )
1018
+ }
1019
+
1020
+ // Function to pass to sort_by. Sort by (satisfaction cost - dissatisfaction cost).
1021
+ //
1022
+ // We sort by (satisfaction cost - dissatisfaction cost) to make a worst-case (the most
1023
+ // costy satisfactions are satisfied, the most costy dissatisfactions are dissatisfied).
1024
+ //
1025
+ // Args are of form: (<count_sat>, <count_dissat>)
1026
+ fn sat_minus_option_dissat < ' r , ' s > (
1027
+ a : & ' r ( Option < usize > , Option < usize > ) ,
1028
+ b : & ' s ( Option < usize > , Option < usize > ) ,
1029
+ ) -> std:: cmp:: Ordering {
1030
+ a. 0 . map ( |x| a. 1 . map ( |y| x as isize - y as isize ) )
1031
+ . cmp ( & b. 0 . map ( |x| b. 1 . map ( |y| x as isize - y as isize ) ) )
1032
+ }
1033
+
1034
+ // Function to pass to sort_by. Sort by (satisfaction cost - dissatisfaction cost) of cost of witness.
1035
+ //
1036
+ // Args are of form: (<max_sat_size>, <count_dissat_size>)
1037
+ // max_[dis]sat_size of form: (<cost_of_witness>, <cost_of_sciptsig>)
1038
+ fn sat_minus_dissat_witness < ' r , ' s > (
1039
+ a : & ' r ( Option < ( usize , usize ) > , Option < ( usize , usize ) > ) ,
1040
+ b : & ' s ( Option < ( usize , usize ) > , Option < ( usize , usize ) > ) ,
1041
+ ) -> std:: cmp:: Ordering {
1042
+ a. 0 . map ( |x| a. 1 . map ( |y| x. 0 as isize - y. 0 as isize ) )
1043
+ . cmp ( & b. 0 . map ( |x| b. 1 . map ( |y| x. 0 as isize - y. 0 as isize ) ) )
1044
+ }
1045
+
1024
1046
// Returns Some(max(x,y)) is both x and y are Some. Otherwise, return none
1025
1047
fn opt_max < T : Ord > ( a : Option < T > , b : Option < T > ) -> Option < T > {
1026
1048
if let ( Some ( x) , Some ( y) ) = ( a, b) {
0 commit comments