Mahjong.xs

filter: Statementstmt 98.2% 111 / 113 Branchbran 80.2% 77 / 96 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 89.9% 188 / 209 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« mahjong_tiles.c ↑ Summary mahjong_decompose.c »
1 /* Mahjong.xs - the door to the C tile table.
2 *
3 * At the TOP of the distribution and not under lib/, for Game::Go's reason: an
4 * XSMULTI build's export list and Strawberry's import library rule never agree
5 * on a name.
6 *
7 * The XSUBs land in Game::Mahjong::Tiles. Every one that takes a kind checks
8 * it and croaks with the distribution's own sentence on a number that names no
9 * tile, because an off-table id is a programmer error and the house rule is
10 * that programmer error dies where a refused move is returned.
11 *
12 * PHASE 01 SCOPE. This door reaches the table and nothing else. The decomposer
13 * (phase 03) and the shanten (phase 07) append their own sections rather than
14 * changing this one.
15 */
16  
17 #define PERL_NO_GET_CONTEXT
18  
19 #include "EXTERN.h"
20 #include "perl.h"
21 #include "XSUB.h"
22  
23 #include "mahjong_abi.h"
24 #include "mahjong_decompose.h"
25 #include "mahjong_shanten.h"
26  
27 static const struct mj_abi *MJ = NULL;
28  
29 /* ---- the decomposer's helpers ------------------------------------------------ */
30  
31 /* A counts arrayref crosses as thirty-five bytes. Index 0 is ignored, a
32 * missing or undef element is 0, and a count outside 0 to 4 croaks: a hand
33 * that holds five of a kind is a programmer error upstream. */
34 158013 static void mj_counts_from_av(pTHX_ AV *av, unsigned char *c)
35 {
36 int i;
37 158013 c[0] = 0;
38 5530405 ▶ for (i = 1; i <= MJ_KINDS; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i <= MJ_KINDS; i++) { 5372394 158011 5530405
39 5372394 SV **sv = av_fetch(av, i, 0);
40 5372394 ▶ IV v = (sv && SvOK(*sv)) ? SvIV(*sv) : 0;
Branch: 2 branches
BranchTrueFalseTotal
IV v = (sv && SvOK(*sv)) ? SvIV(*sv) : 0; 5372292 102 5372394
IV v = (sv && SvOK(*sv)) ? SvIV(*sv) : 0; 5372292 0 5372292
41 5372394 ▶ if (v < 0 || v > MJ_PER_KIND)
Branch: 2 branches
BranchTrueFalseTotal
if (v < 0 || v > MJ_PER_KIND) 5372394 0 5372394
if (v < 0 || v > MJ_PER_KIND) 2 5372392 5372394
42 2 croak("Game::Mahjong::Decompose: a count is 0 to %d, not %" IVdf " for kind %d",
43 MJ_PER_KIND, v, i);
44 5372392 c[i] = (unsigned char)v;
45 }
46 158011 }
47  
48 static const char *SET_WORD[] = { "", "chow", "pung", "knit" };
49 static const char *FORM_WORD[] = { "", "standard", "seven_pairs", "thirteen_orphans",
50 "honours_knitted", "knitted_straight" };
51 static const char *IN_WORD[] = { "", "set", "pair", "single" };
52 static const char *WAIT_WORD[] = { "", "edge", "closed", "two_sided", "pair", "pung",
53 "single", "knit" };
54  
55 2199 static SV *mj_split_to_sv(pTHX_ const struct mj_split *s)
56 {
57 2199 HV *h = newHV();
58 2199 AV *sets = newAV();
59 2199 AV *singles = newAV();
60 2199 HV *place = newHV();
61 int i;
62  
63 2199 (void)hv_stores(h, "form", newSVpv(FORM_WORD[s->form], 0));
64 8757 ▶ for (i = 0; i < s->nsets; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < s->nsets; i++) { 6558 2199 8757
65 6558 HV *set = newHV();
66 6558 AV *tiles = newAV();
67 int j;
68 26232 ▶ for (j = 0; j < 3; j++) av_push(tiles, newSViv(s->sets[i].tiles[j]));
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < 3; j++) av_push(tiles, newSViv(s->sets[i].tiles[j])); 19674 6558 26232
69 6558 (void)hv_stores(set, "kind", newSVpv(SET_WORD[s->sets[i].kind], 0));
70 6558 (void)hv_stores(set, "tiles", newRV_noinc((SV *)tiles));
71 6558 av_push(sets, newRV_noinc((SV *)set));
72 }
73 2199 (void)hv_stores(h, "sets", newRV_noinc((SV *)sets));
74 2199 ▶ (void)hv_stores(h, "pair", s->pair ? newSViv(s->pair) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(h, "pair", s->pair ? newSViv(s->pair) : newSV(0)); 2144 55 2199
75 3282 ▶ for (i = 0; i < s->nsingles; i++) av_push(singles, newSViv(s->singles[i]));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < s->nsingles; i++) av_push(singles, newSViv(s->singles[i])); 1083 2199 3282
76 2199 (void)hv_stores(h, "singles", newRV_noinc((SV *)singles));
77 2199 ▶ (void)hv_stores(place, "in", s->place_in ? newSVpv(IN_WORD[s->place_in], 0) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(place, "in", s->place_in ? newSVpv(IN_WORD[s->place_in], 0) : newSV(0)); 1888 311 2199
78 2199 (void)hv_stores(place, "index", newSViv(s->place_index));
79 2199 ▶ (void)hv_stores(place, "wait", s->wait ? newSVpv(WAIT_WORD[s->wait], 0) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(place, "wait", s->wait ? newSVpv(WAIT_WORD[s->wait], 0) : newSV(0)); 1888 311 2199
80 2199 (void)hv_stores(h, "placement", newRV_noinc((SV *)place));
81 2199 return newRV_noinc((SV *)h);
82 }
83  
84 /* The suit letters as the Perl side spells them: the code's first letter for
85 * a suit tile, undef for everything else. */
86 static const char SUIT_LETTER[] = { 0, 'm', 'p', 's', 0, 0, 0, 0 };
87  
88 6806201 static int mj_kind_arg(pTHX_ SV *sv)
89 {
90 IV id;
91 6806201 ▶ if (!SvOK(sv) || !looks_like_number(sv))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(sv) || !looks_like_number(sv)) 6806200 1 6806201
if (!SvOK(sv) || !looks_like_number(sv)) 1 6806199 6806200
92 3 ▶ croak("Game::Mahjong::Tiles: a kind is a number from 1 to %d, not '%s'",
Branch: 1 branch
BranchTrueFalseTotal
croak("Game::Mahjong::Tiles: a kind is a number from 1 to %d, not '%s'", 1 1 2
93 MJ_PATTERNS, SvOK(sv) ? SvPV_nolen(sv) : "undef");
94 6806199 id = SvIV(sv);
95 6806199 ▶ if (id < 1 || id > MJ_PATTERNS)
Branch: 2 branches
BranchTrueFalseTotal
if (id < 1 || id > MJ_PATTERNS) 6806197 2 6806199
if (id < 1 || id > MJ_PATTERNS) 5 6806192 6806197
96 7 croak("Game::Mahjong::Tiles: there is no tile %" IVdf, id);
97 6806192 return (int)id;
98 }
99  
100 MODULE = Game::Mahjong PACKAGE = Game::Mahjong::Tiles
101  
102 PROTOTYPES: DISABLE
103  
104 BOOT:
105 34 MJ = mj_abi_table();
106  
107 UV
108 _abi_ptr()
109 CODE:
110 1 ▶ RETVAL = PTR2UV(MJ);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2UV(MJ); 0 1 1
111 OUTPUT:
112 RETVAL
113  
114 int
115 _abi_version()
116 CODE:
117 1 ▶ RETVAL = (int)MJ->version;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (int)MJ->version; 0 1 1
118 OUTPUT:
119 RETVAL
120  
121 int
122 _abi_kinds()
123 CODE:
124 1 ▶ RETVAL = (int)MJ->patterns;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (int)MJ->patterns; 0 1 1
125 OUTPUT:
126 RETVAL
127  
128 void
129 code_of(SV *kind)
130 PPCODE:
131 {
132 2566094 int id = mj_kind_arg(aTHX_ kind);
133 2566086 ▶ XPUSHs(sv_2mortal(newSVpvn(MJ->kind[id].code, 2)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvn(MJ->kind[id].code, 2))); 0 2566086 2566086
134 2566086 XSRETURN(1);
135 }
136  
137 void
138 id_of(SV *code)
139 PPCODE:
140 {
141 int id;
142 9191 ▶ if (!SvOK(code))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(code)) 1 9190 9191
143 1 croak("Game::Mahjong::Tiles: a code is two letters, not undef");
144 18380 id = MJ->id_of(SvPV_nolen(code));
145 9190 ▶ if (!id)
Branch: 1 branch
BranchTrueFalseTotal
if (!id) 2 9188 9190
146 2 croak("Game::Mahjong::Tiles: there is no tile '%s'", SvPV_nolen(code));
147 9188 XSRETURN_IV(id);
148 }
149  
150 void
151 suit_of(SV *kind)
152 PPCODE:
153 {
154 224140 int id = mj_kind_arg(aTHX_ kind);
155 224140 char letter = SUIT_LETTER[MJ->kind[id].suit];
156 224140 ▶ if (!letter) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!letter) XSRETURN_UNDEF; 28783 195357 224140
157 195357 ▶ XPUSHs(sv_2mortal(newSVpvn(&letter, 1)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvn(&letter, 1))); 0 195357 195357
158 195357 XSRETURN(1);
159 }
160  
161 void
162 rank_of(SV *kind)
163 PPCODE:
164 {
165 462979 int id = mj_kind_arg(aTHX_ kind);
166 462979 ▶ if (!(MJ->kind[id].flags & MJ_F_SUIT)) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!(MJ->kind[id].flags & MJ_F_SUIT)) XSRETURN_UNDEF; 25183 437796 462979
167 437796 XSRETURN_IV(MJ->kind[id].rank);
168 }
169  
170 void
171 wind_index(SV *kind)
172 PPCODE:
173 {
174 2898 int id = mj_kind_arg(aTHX_ kind);
175 2898 ▶ if (!(MJ->kind[id].flags & MJ_F_WIND)) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!(MJ->kind[id].flags & MJ_F_WIND)) XSRETURN_UNDEF; 1 2897 2898
176 2897 XSRETURN_IV(MJ->kind[id].rank - 1);
177 }
178  
179 void
180 dragon_index(SV *kind)
181 PPCODE:
182 {
183 8 int id = mj_kind_arg(aTHX_ kind);
184 8 ▶ if (!(MJ->kind[id].flags & MJ_F_DRAGON)) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!(MJ->kind[id].flags & MJ_F_DRAGON)) XSRETURN_UNDEF; 1 7 8
185 7 XSRETURN_IV(MJ->kind[id].rank - 1);
186 }
187  
188 void
189 bonus_index(SV *kind)
190 PPCODE:
191 {
192 21 int id = mj_kind_arg(aTHX_ kind);
193 21 ▶ if (!(MJ->kind[id].flags & MJ_F_BONUS)) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!(MJ->kind[id].flags & MJ_F_BONUS)) XSRETURN_UNDEF; 1 20 21
194 20 XSRETURN_IV(MJ->kind[id].rank - 1);
195 }
196  
197 void
198 flags_of(SV *kind)
199 PPCODE:
200 {
201 2 int id = mj_kind_arg(aTHX_ kind);
202 2 XSRETURN_IV(MJ->kind[id].flags);
203 }
204  
205 # One XSUB, eleven names: the flag bit rides in ix. A C macro that stamped
206 # out eleven XSUBs would not survive xsubpp, which reads the file as XS and
207 # not as C.
208 void
209 is_suit(SV *kind)
210 ALIAS:
211 is_honour = MJ_F_HONOUR
212 is_wind = MJ_F_WIND
213 is_dragon = MJ_F_DRAGON
214 is_terminal = MJ_F_TERMINAL
215 is_simple = MJ_F_SIMPLE
216 is_bonus = MJ_F_BONUS
217 is_flower = MJ_F_FLOWER
218 is_season = MJ_F_SEASON
219 is_green = MJ_F_GREEN
220 is_reversible = MJ_F_REVERSIBLE
221 PPCODE:
222 {
223 3550059 int id = mj_kind_arg(aTHX_ kind);
224 3550058 ▶ unsigned bit = ix ? (unsigned)ix : MJ_F_SUIT;
Branch: 1 branch
BranchTrueFalseTotal
unsigned bit = ix ? (unsigned)ix : MJ_F_SUIT; 3549800 258 3550058
225 3550058 XSRETURN_IV((MJ->kind[id].flags & bit) ? 1 : 0);
226 }
227  
228 MODULE = Game::Mahjong PACKAGE = Game::Mahjong::Decompose
229  
230 # The decomposer's door. A counts arrayref (index 0 unused, 1 to 34 the
231 # kinds) crosses as thirty-five bytes; the splits come back as Perl
232 # structures the scorer and the bot read, with the forms, kinds and waits
233 # spelled out as words here so nothing in Perl knows a C enum.
234  
235 void
236 _decompose(AV *counts, int melds, int winning)
237 PPCODE:
238 {
239 unsigned char c[MJ_KINDS + 1];
240 struct mj_split splits[MJ_MAX_SPLITS];
241 int n, i;
242 AV *list;
243 25084 mj_counts_from_av(aTHX_ counts, c);
244 25083 ▶ if (melds < 0 || melds > 4)
Branch: 2 branches
BranchTrueFalseTotal
if (melds < 0 || melds > 4) 25083 0 25083
if (melds < 0 || melds > 4) 1 25082 25083
245 1 croak("Game::Mahjong::Decompose: melds is 0 to 4, not %d", melds);
246 25082 ▶ if (winning < 0 || winning > MJ_KINDS)
Branch: 2 branches
BranchTrueFalseTotal
if (winning < 0 || winning > MJ_KINDS) 25082 0 25082
if (winning < 0 || winning > MJ_KINDS) 1 25081 25082
247 1 croak("Game::Mahjong::Decompose: a winning kind is 0 to %d, not %d", MJ_KINDS, winning);
248 25081 n = mj_decompose(c, melds, winning, splits, MJ_MAX_SPLITS);
249 25081 ▶ if (n < 0) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) XSRETURN_UNDEF; 0 25081 25081
250 25081 list = newAV();
251 27280 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 2199 25081 27280
252 2199 av_push(list, mj_split_to_sv(aTHX_ &splits[i]));
253 25081 ▶ XPUSHs(sv_2mortal(newRV_noinc((SV *)list)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newRV_noinc((SV *)list))); 0 25081 25081
254 25081 XSRETURN(1);
255 }
256  
257 int
258 _is_complete(AV *counts, int melds)
259 CODE:
260 {
261 unsigned char c[MJ_KINDS + 1];
262 18 mj_counts_from_av(aTHX_ counts, c);
263 18 RETVAL = mj_is_complete(c, melds);
264 }
265 OUTPUT:
266 RETVAL
267  
268 void
269 _waits(AV *counts, int melds)
270 PPCODE:
271 {
272 unsigned char c[MJ_KINDS + 1];
273 unsigned char out[MJ_KINDS];
274 int n, i;
275 AV *list;
276 38620 mj_counts_from_av(aTHX_ counts, c);
277 38620 n = mj_waits(c, melds, out);
278 38620 list = newAV();
279 48810 ▶ for (i = 0; i < n; i++) av_push(list, newSViv(out[i]));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) av_push(list, newSViv(out[i])); 10190 38620 48810
280 38620 ▶ XPUSHs(sv_2mortal(newRV_noinc((SV *)list)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newRV_noinc((SV *)list))); 0 38620 38620
281 38620 XSRETURN(1);
282 }
283  
284 MODULE = Game::Mahjong PACKAGE = Game::Mahjong::Shanten
285  
286 # The shanten's door: the same thirty-five bytes in, numbers out.
287  
288 int
289 _shanten(AV *counts, int melds)
290 CODE:
291 {
292 unsigned char c[MJ_KINDS + 1];
293 71706 mj_counts_from_av(aTHX_ counts, c);
294 71705 ▶ if (melds < 0 || melds > 4)
Branch: 2 branches
BranchTrueFalseTotal
if (melds < 0 || melds > 4) 71705 0 71705
if (melds < 0 || melds > 4) 1 71704 71705
295 1 croak("Game::Mahjong::Shanten: melds is 0 to 4, not %d", melds);
296 71704 RETVAL = mj_shanten(c, melds);
297 }
298 OUTPUT:
299 RETVAL
300  
301 void
302 _forms(AV *counts, int melds)
303 PPCODE:
304 {
305 unsigned char c[MJ_KINDS + 1];
306 int forms[MJ_FORM_S_COUNT], i;
307 AV *list;
308 2 mj_counts_from_av(aTHX_ counts, c);
309 2 ▶ if (melds < 0 || melds > 4)
Branch: 2 branches
BranchTrueFalseTotal
if (melds < 0 || melds > 4) 2 0 2
if (melds < 0 || melds > 4) 0 2 2
310 0 croak("Game::Mahjong::Shanten: melds is 0 to 4, not %d", melds);
311 2 mj_shanten_forms(c, melds, forms);
312 2 list = newAV();
313 10 ▶ for (i = 0; i < MJ_FORM_S_COUNT; i++) av_push(list, newSViv(forms[i]));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < MJ_FORM_S_COUNT; i++) av_push(list, newSViv(forms[i])); 8 2 10
314 2 ▶ XPUSHs(sv_2mortal(newRV_noinc((SV *)list)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newRV_noinc((SV *)list))); 0 2 2
315 2 XSRETURN(1);
316 }
317  
318 void
319 _ukeire(AV *counts, int melds)
320 PPCODE:
321 {
322 unsigned char c[MJ_KINDS + 1];
323 unsigned char out[MJ_KINDS];
324 int n, i;
325 AV *list;
326 22583 mj_counts_from_av(aTHX_ counts, c);
327 22583 ▶ if (melds < 0 || melds > 4)
Branch: 2 branches
BranchTrueFalseTotal
if (melds < 0 || melds > 4) 22583 0 22583
if (melds < 0 || melds > 4) 0 22583 22583
328 0 croak("Game::Mahjong::Shanten: melds is 0 to 4, not %d", melds);
329 22583 n = mj_ukeire(c, melds, out);
330 22583 list = newAV();
331 192293 ▶ for (i = 0; i < n; i++) av_push(list, newSViv(out[i]));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) av_push(list, newSViv(out[i])); 169710 22583 192293
332 22583 ▶ XPUSHs(sv_2mortal(newRV_noinc((SV *)list)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newRV_noinc((SV *)list))); 0 22583 22583
333 22583 XSRETURN(1);
334 }
« mahjong_tiles.c ↑ Summary mahjong_decompose.c »