| 1 |
|
| #include "EXTERN.h" |
| 2 |
|
| #include "perl.h" |
| 3 |
|
| #include "XSUB.h" |
| 4 |
|
| |
| 5 |
|
| #include "ppport.h" |
| 6 |
|
| |
| 7 |
|
| |
| 8 |
|
| MODULE = String::MMM PACKAGE = String::MMM |
| 9 |
|
| |
| 10 |
|
| void |
| 11 |
|
| match_strings(hidden,target,colors) |
| 12 |
|
| char *hidden; |
| 13 |
|
| char *target; |
| 14 |
|
| int colors; |
| 15 |
|
| INIT: |
| 16 |
|
| int i; |
| 17 |
5 |
| int blacks = 0; |
| 18 |
5 |
| int whites = 0; |
| 19 |
5 |
| int colors_in_string_h[colors], colors_in_string_t[colors]; |
| 20 |
|
| PPCODE: |
| 21 |
42 |
▶ | for ( i = 0; i < colors; i++ ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( i = 0; i < colors; i++ ) { |
37 |
5 |
42 |
|
| 22 |
37 |
| colors_in_string_h[i] = colors_in_string_t[i] = 0; |
| 23 |
|
| } |
| 24 |
37 |
▶ | for ( i = 0; i < strlen( hidden ); i++ ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( i = 0; i < strlen( hidden ); i++ ) { |
32 |
5 |
37 |
|
| 25 |
32 |
▶ | if ( hidden[i] == target[i] ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( hidden[i] == target[i] ) { |
8 |
24 |
32 |
|
| 26 |
8 |
| blacks++; |
| 27 |
|
| } else { |
| 28 |
24 |
| colors_in_string_h[hidden[i] - 'A']++; |
| 29 |
24 |
| colors_in_string_t[target[i] - 'A']++; |
| 30 |
|
| } |
| 31 |
|
| } |
| 32 |
42 |
▶ | for ( i = 0; i < colors; i ++ ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( i = 0; i < colors; i ++ ) { |
37 |
5 |
42 |
|
| 33 |
37 |
▶ | if ( colors_in_string_h[i] && colors_in_string_t[i] ) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ( colors_in_string_h[i] && colors_in_string_t[i] ) { |
21 |
16 |
37 |
| if ( colors_in_string_h[i] && colors_in_string_t[i] ) { |
8 |
13 |
21 |
|
| 34 |
8 |
| whites += ( colors_in_string_h[i] < colors_in_string_t[i])? |
| 35 |
8 |
| colors_in_string_h[i]: colors_in_string_t[i]; |
| 36 |
|
| } |
| 37 |
|
| } |
| 38 |
5 |
▶ | XPUSHs(sv_2mortal(newSViv(blacks))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSViv(blacks))); |
0 |
5 |
5 |
|
| 39 |
5 |
▶ | XPUSHs(sv_2mortal(newSViv(whites))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSViv(whites))); |
0 |
5 |
5 |
|
| 40 |
|
| |
| 41 |
|
| void |
| 42 |
|
| s_match_strings(hidden,target,colors) |
| 43 |
|
| char *hidden; |
| 44 |
|
| char *target; |
| 45 |
|
| int colors; |
| 46 |
|
| INIT: |
| 47 |
|
| int i; |
| 48 |
5 |
| int blacks = 0; |
| 49 |
5 |
| int whites = 0; |
| 50 |
5 |
| int colors_in_string_h[colors], colors_in_string_t[colors]; |
| 51 |
|
| PPCODE: |
| 52 |
42 |
▶ | for ( i = 0; i < colors; i++ ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( i = 0; i < colors; i++ ) { |
37 |
5 |
42 |
|
| 53 |
37 |
| colors_in_string_h[i] = colors_in_string_t[i] = 0; |
| 54 |
|
| } |
| 55 |
37 |
▶ | for ( i = 0; i < strlen( hidden ); i++ ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( i = 0; i < strlen( hidden ); i++ ) { |
32 |
5 |
37 |
|
| 56 |
32 |
▶ | if ( hidden[i] == target[i] ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( hidden[i] == target[i] ) { |
8 |
24 |
32 |
|
| 57 |
8 |
| blacks++; |
| 58 |
|
| } else { |
| 59 |
24 |
| colors_in_string_h[hidden[i] - 'A']++; |
| 60 |
24 |
| colors_in_string_t[target[i] - 'A']++; |
| 61 |
|
| } |
| 62 |
|
| } |
| 63 |
42 |
▶ | for ( i = 0; i < colors; i ++ ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( i = 0; i < colors; i ++ ) { |
37 |
5 |
42 |
|
| 64 |
37 |
▶ | if ( colors_in_string_h[i] && colors_in_string_t[i] ) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ( colors_in_string_h[i] && colors_in_string_t[i] ) { |
21 |
16 |
37 |
| if ( colors_in_string_h[i] && colors_in_string_t[i] ) { |
8 |
13 |
21 |
|
| 65 |
8 |
| whites += ( colors_in_string_h[i] < colors_in_string_t[i])? |
| 66 |
8 |
| colors_in_string_h[i]: colors_in_string_t[i]; |
| 67 |
|
| } |
| 68 |
|
| } |
| 69 |
|
| char str[7]; |
| 70 |
5 |
| sprintf(str, "%db%dw", blacks, whites ); |
| 71 |
5 |
▶ | XPUSHs(sv_2mortal(newSVpv(str,0))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVpv(str,0))); |
0 |
5 |
5 |
|
| 72 |
|
| |
| 73 |
|
| void |
| 74 |
|
| match_strings_a(hidden,target) |
| 75 |
|
| char *hidden; |
| 76 |
|
| char *target; |
| 77 |
|
| INIT: |
| 78 |
|
| int i; |
| 79 |
5 |
| int blacks = 0; |
| 80 |
5 |
| int whites = 0; |
| 81 |
5 |
| int colors = 26; |
| 82 |
5 |
| int colors_in_string_h[colors], colors_in_string_t[colors]; |
| 83 |
|
| PPCODE: |
| 84 |
135 |
▶ | for ( i = 0; i < colors; i++ ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( i = 0; i < colors; i++ ) { |
130 |
5 |
135 |
|
| 85 |
130 |
| colors_in_string_h[i] = colors_in_string_t[i] = 0; |
| 86 |
|
| } |
| 87 |
37 |
▶ | for ( i = 0; i < strlen( hidden ); i++ ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( i = 0; i < strlen( hidden ); i++ ) { |
32 |
5 |
37 |
|
| 88 |
32 |
▶ | if ( hidden[i] == target[i] ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( hidden[i] == target[i] ) { |
8 |
24 |
32 |
|
| 89 |
8 |
| blacks++; |
| 90 |
|
| } else { |
| 91 |
24 |
| colors_in_string_h[hidden[i] - 'a']++; |
| 92 |
24 |
| colors_in_string_t[target[i] - 'a']++; |
| 93 |
|
| } |
| 94 |
|
| } |
| 95 |
135 |
▶ | for ( i = 0; i < colors; i ++ ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( i = 0; i < colors; i ++ ) { |
130 |
5 |
135 |
|
| 96 |
130 |
▶ | if ( colors_in_string_h[i] && colors_in_string_t[i] ) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ( colors_in_string_h[i] && colors_in_string_t[i] ) { |
21 |
109 |
130 |
| if ( colors_in_string_h[i] && colors_in_string_t[i] ) { |
8 |
13 |
21 |
|
| 97 |
8 |
| whites += ( colors_in_string_h[i] < colors_in_string_t[i])? |
| 98 |
8 |
| colors_in_string_h[i]: colors_in_string_t[i]; |
| 99 |
|
| } |
| 100 |
|
| } |
| 101 |
5 |
▶ | XPUSHs(sv_2mortal(newSViv(blacks))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSViv(blacks))); |
0 |
5 |
5 |
|
| 102 |
5 |
▶ | XPUSHs(sv_2mortal(newSViv(whites))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSViv(whites))); |
0 |
5 |
5 |
|
| 103 |
|
| |
| 104 |
|
| void |
| 105 |
|
| match_arrays(hidden_ref,target_ref, colors) |
| 106 |
|
| SV* hidden_ref; |
| 107 |
|
| SV* target_ref; |
| 108 |
|
| unsigned int colors; |
| 109 |
|
| INIT: |
| 110 |
|
| int i; |
| 111 |
5 |
| int blacks = 0; |
| 112 |
5 |
| int whites = 0; |
| 113 |
5 |
| int colors_in_string_h[colors], colors_in_string_t[colors]; |
| 114 |
|
| PPCODE: |
| 115 |
|
| AV* hidden; |
| 116 |
|
| AV* target; |
| 117 |
5 |
| hidden = (AV*) SvRV(hidden_ref); |
| 118 |
5 |
| target = (AV*) SvRV(target_ref); |
| 119 |
95 |
▶ | for ( i = 0; i < colors; i++ ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( i = 0; i < colors; i++ ) { |
90 |
5 |
95 |
|
| 120 |
90 |
| colors_in_string_h[i] = colors_in_string_t[i] = 0; |
| 121 |
|
| } |
| 122 |
37 |
▶ | for ( i = 0; i <= av_len( hidden ); i++ ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( i = 0; i <= av_len( hidden ); i++ ) { |
32 |
5 |
37 |
|
| 123 |
32 |
| int hidden_val = SvIV(*av_fetch(hidden,i,0)); |
| 124 |
32 |
| int target_val = SvIV(*av_fetch(target,i,0)); |
| 125 |
32 |
| printf( "%d h %d t %d\n", i, hidden_val, target_val ); |
| 126 |
32 |
▶ | if ( hidden_val == target_val ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( hidden_val == target_val ) { |
8 |
24 |
32 |
|
| 127 |
8 |
| blacks++; |
| 128 |
|
| } else { |
| 129 |
24 |
| colors_in_string_h[ hidden_val ]++; |
| 130 |
24 |
| colors_in_string_t[ target_val ]++; |
| 131 |
|
| } |
| 132 |
|
| } |
| 133 |
5 |
| printf("\n"); |
| 134 |
95 |
▶ | for ( i = 0; i < colors; i ++ ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for ( i = 0; i < colors; i ++ ) { |
90 |
5 |
95 |
|
| 135 |
90 |
▶ | if ( colors_in_string_h[i] && colors_in_string_t[i] ) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ( colors_in_string_h[i] && colors_in_string_t[i] ) { |
21 |
69 |
90 |
| if ( colors_in_string_h[i] && colors_in_string_t[i] ) { |
8 |
13 |
21 |
|
| 136 |
8 |
| whites += ( colors_in_string_h[i] < colors_in_string_t[i])? |
| 137 |
8 |
| colors_in_string_h[i]: colors_in_string_t[i]; |
| 138 |
|
| } |
| 139 |
|
| } |
| 140 |
5 |
▶ | XPUSHs(sv_2mortal(newSViv(blacks))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSViv(blacks))); |
0 |
5 |
5 |
|
| 141 |
5 |
▶ | XPUSHs(sv_2mortal(newSViv(whites))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSViv(whites))); |
0 |
5 |
5 |
|