Mahjong.xs

filter: Statementstmt 0.0% 0 / 113 Branchbran 0.0% 0 / 96 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 209 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Game/Mahjong/Error.pm ↑ 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 0 static void mj_counts_from_av(pTHX_ AV *av, unsigned char *c)
35 {
36 int i;
37 0 c[0] = 0;
38 0 ▶ for (i = 1; i <= MJ_KINDS; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i <= MJ_KINDS; i++) { 0 0 0
39 0 SV **sv = av_fetch(av, i, 0);
40 0 ▶ IV v = (sv && SvOK(*sv)) ? SvIV(*sv) : 0;
Branch: 2 branches
BranchTrueFalseTotal
IV v = (sv && SvOK(*sv)) ? SvIV(*sv) : 0; 0 0 0
IV v = (sv && SvOK(*sv)) ? SvIV(*sv) : 0; 0 0 0
41 0 ▶ if (v < 0 || v > MJ_PER_KIND)
Branch: 2 branches
BranchTrueFalseTotal
if (v < 0 || v > MJ_PER_KIND) 0 0 0
if (v < 0 || v > MJ_PER_KIND) 0 0 0
42 0 croak("Game::Mahjong::Decompose: a count is 0 to %d, not %" IVdf " for kind %d",
43 MJ_PER_KIND, v, i);
44 0 c[i] = (unsigned char)v;
45 }
46 0 }
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 0 static SV *mj_split_to_sv(pTHX_ const struct mj_split *s)
56 {
57 0 HV *h = newHV();
58 0 AV *sets = newAV();
59 0 AV *singles = newAV();
60 0 HV *place = newHV();
61 int i;
62  
63 0 (void)hv_stores(h, "form", newSVpv(FORM_WORD[s->form], 0));
64 0 ▶ for (i = 0; i < s->nsets; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < s->nsets; i++) { 0 0 0
65 0 HV *set = newHV();
66 0 AV *tiles = newAV();
67 int j;
68 0 ▶ 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])); 0 0 0
69 0 (void)hv_stores(set, "kind", newSVpv(SET_WORD[s->sets[i].kind], 0));
70 0 (void)hv_stores(set, "tiles", newRV_noinc((SV *)tiles));
71 0 av_push(sets, newRV_noinc((SV *)set));
72 }
73 0 (void)hv_stores(h, "sets", newRV_noinc((SV *)sets));
74 0 ▶ (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)); 0 0 0
75 0 ▶ 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])); 0 0 0
76 0 (void)hv_stores(h, "singles", newRV_noinc((SV *)singles));
77 0 ▶ (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)); 0 0 0
78 0 (void)hv_stores(place, "index", newSViv(s->place_index));
79 0 ▶ (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)); 0 0 0
80 0 (void)hv_stores(h, "placement", newRV_noinc((SV *)place));
81 0 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 0 static int mj_kind_arg(pTHX_ SV *sv)
89 {
90 IV id;
91 0 ▶ if (!SvOK(sv) || !looks_like_number(sv))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(sv) || !looks_like_number(sv)) 0 0 0
if (!SvOK(sv) || !looks_like_number(sv)) 0 0 0
92 0 ▶ 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'", 0 0 0
93 MJ_PATTERNS, SvOK(sv) ? SvPV_nolen(sv) : "undef");
94 0 id = SvIV(sv);
95 0 ▶ if (id < 1 || id > MJ_PATTERNS)
Branch: 2 branches
BranchTrueFalseTotal
if (id < 1 || id > MJ_PATTERNS) 0 0 0
if (id < 1 || id > MJ_PATTERNS) 0 0 0
96 0 croak("Game::Mahjong::Tiles: there is no tile %" IVdf, id);
97 0 return (int)id;
98 }
99  
100 MODULE = Game::Mahjong PACKAGE = Game::Mahjong::Tiles
101  
102 PROTOTYPES: DISABLE
103  
104 BOOT:
105 0 MJ = mj_abi_table();
106  
107 UV
108 _abi_ptr()
109 CODE:
110 0 ▶ RETVAL = PTR2UV(MJ);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2UV(MJ); 0 0 0
111 OUTPUT:
112 RETVAL
113  
114 int
115 _abi_version()
116 CODE:
117 0 ▶ RETVAL = (int)MJ->version;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (int)MJ->version; 0 0 0
118 OUTPUT:
119 RETVAL
120  
121 int
122 _abi_kinds()
123 CODE:
124 0 ▶ RETVAL = (int)MJ->patterns;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (int)MJ->patterns; 0 0 0
125 OUTPUT:
126 RETVAL
127  
128 void
129 code_of(SV *kind)
130 PPCODE:
131 {
132 0 int id = mj_kind_arg(aTHX_ kind);
133 0 ▶ XPUSHs(sv_2mortal(newSVpvn(MJ->kind[id].code, 2)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvn(MJ->kind[id].code, 2))); 0 0 0
134 0 XSRETURN(1);
135 }
136  
137 void
138 id_of(SV *code)
139 PPCODE:
140 {
141 int id;
142 0 ▶ if (!SvOK(code))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(code)) 0 0 0
143 0 croak("Game::Mahjong::Tiles: a code is two letters, not undef");
144 0 id = MJ->id_of(SvPV_nolen(code));
145 0 ▶ if (!id)
Branch: 1 branch
BranchTrueFalseTotal
if (!id) 0 0 0
146 0 croak("Game::Mahjong::Tiles: there is no tile '%s'", SvPV_nolen(code));
147 0 XSRETURN_IV(id);
148 }
149  
150 void
151 suit_of(SV *kind)
152 PPCODE:
153 {
154 0 int id = mj_kind_arg(aTHX_ kind);
155 0 char letter = SUIT_LETTER[MJ->kind[id].suit];
156 0 ▶ if (!letter) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!letter) XSRETURN_UNDEF; 0 0 0
157 0 ▶ XPUSHs(sv_2mortal(newSVpvn(&letter, 1)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvn(&letter, 1))); 0 0 0
158 0 XSRETURN(1);
159 }
160  
161 void
162 rank_of(SV *kind)
163 PPCODE:
164 {
165 0 int id = mj_kind_arg(aTHX_ kind);
166 0 ▶ if (!(MJ->kind[id].flags & MJ_F_SUIT)) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!(MJ->kind[id].flags & MJ_F_SUIT)) XSRETURN_UNDEF; 0 0 0
167 0 XSRETURN_IV(MJ->kind[id].rank);
168 }
169  
170 void
171 wind_index(SV *kind)
172 PPCODE:
173 {
174 0 int id = mj_kind_arg(aTHX_ kind);
175 0 ▶ if (!(MJ->kind[id].flags & MJ_F_WIND)) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!(MJ->kind[id].flags & MJ_F_WIND)) XSRETURN_UNDEF; 0 0 0
176 0 XSRETURN_IV(MJ->kind[id].rank - 1);
177 }
178  
179 void
180 dragon_index(SV *kind)
181 PPCODE:
182 {
183 0 int id = mj_kind_arg(aTHX_ kind);
184 0 ▶ if (!(MJ->kind[id].flags & MJ_F_DRAGON)) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!(MJ->kind[id].flags & MJ_F_DRAGON)) XSRETURN_UNDEF; 0 0 0
185 0 XSRETURN_IV(MJ->kind[id].rank - 1);
186 }
187  
188 void
189 bonus_index(SV *kind)
190 PPCODE:
191 {
192 0 int id = mj_kind_arg(aTHX_ kind);
193 0 ▶ if (!(MJ->kind[id].flags & MJ_F_BONUS)) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (!(MJ->kind[id].flags & MJ_F_BONUS)) XSRETURN_UNDEF; 0 0 0
194 0 XSRETURN_IV(MJ->kind[id].rank - 1);
195 }
196  
197 void
198 flags_of(SV *kind)
199 PPCODE:
200 {
201 0 int id = mj_kind_arg(aTHX_ kind);
202 0 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 0 int id = mj_kind_arg(aTHX_ kind);
224 0 ▶ unsigned bit = ix ? (unsigned)ix : MJ_F_SUIT;
Branch: 1 branch
BranchTrueFalseTotal
unsigned bit = ix ? (unsigned)ix : MJ_F_SUIT; 0 0 0
225 0 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 0 mj_counts_from_av(aTHX_ counts, c);
244 0 ▶ if (melds < 0 || melds > 4)
Branch: 2 branches
BranchTrueFalseTotal
if (melds < 0 || melds > 4) 0 0 0
if (melds < 0 || melds > 4) 0 0 0
245 0 croak("Game::Mahjong::Decompose: melds is 0 to 4, not %d", melds);
246 0 ▶ if (winning < 0 || winning > MJ_KINDS)
Branch: 2 branches
BranchTrueFalseTotal
if (winning < 0 || winning > MJ_KINDS) 0 0 0
if (winning < 0 || winning > MJ_KINDS) 0 0 0
247 0 croak("Game::Mahjong::Decompose: a winning kind is 0 to %d, not %d", MJ_KINDS, winning);
248 0 n = mj_decompose(c, melds, winning, splits, MJ_MAX_SPLITS);
249 0 ▶ if (n < 0) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) XSRETURN_UNDEF; 0 0 0
250 0 list = newAV();
251 0 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 0 0 0
252 0 av_push(list, mj_split_to_sv(aTHX_ &splits[i]));
253 0 ▶ XPUSHs(sv_2mortal(newRV_noinc((SV *)list)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newRV_noinc((SV *)list))); 0 0 0
254 0 XSRETURN(1);
255 }
256  
257 int
258 _is_complete(AV *counts, int melds)
259 CODE:
260 {
261 unsigned char c[MJ_KINDS + 1];
262 0 mj_counts_from_av(aTHX_ counts, c);
263 0 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 0 mj_counts_from_av(aTHX_ counts, c);
277 0 n = mj_waits(c, melds, out);
278 0 list = newAV();
279 0 ▶ 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])); 0 0 0
280 0 ▶ XPUSHs(sv_2mortal(newRV_noinc((SV *)list)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newRV_noinc((SV *)list))); 0 0 0
281 0 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 0 mj_counts_from_av(aTHX_ counts, c);
294 0 ▶ if (melds < 0 || melds > 4)
Branch: 2 branches
BranchTrueFalseTotal
if (melds < 0 || melds > 4) 0 0 0
if (melds < 0 || melds > 4) 0 0 0
295 0 croak("Game::Mahjong::Shanten: melds is 0 to 4, not %d", melds);
296 0 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 0 mj_counts_from_av(aTHX_ counts, c);
309 0 ▶ if (melds < 0 || melds > 4)
Branch: 2 branches
BranchTrueFalseTotal
if (melds < 0 || melds > 4) 0 0 0
if (melds < 0 || melds > 4) 0 0 0
310 0 croak("Game::Mahjong::Shanten: melds is 0 to 4, not %d", melds);
311 0 mj_shanten_forms(c, melds, forms);
312 0 list = newAV();
313 0 ▶ 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])); 0 0 0
314 0 ▶ XPUSHs(sv_2mortal(newRV_noinc((SV *)list)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newRV_noinc((SV *)list))); 0 0 0
315 0 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 0 mj_counts_from_av(aTHX_ counts, c);
327 0 ▶ if (melds < 0 || melds > 4)
Branch: 2 branches
BranchTrueFalseTotal
if (melds < 0 || melds > 4) 0 0 0
if (melds < 0 || melds > 4) 0 0 0
328 0 croak("Game::Mahjong::Shanten: melds is 0 to 4, not %d", melds);
329 0 n = mj_ukeire(c, melds, out);
330 0 list = newAV();
331 0 ▶ 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])); 0 0 0
332 0 ▶ XPUSHs(sv_2mortal(newRV_noinc((SV *)list)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newRV_noinc((SV *)list))); 0 0 0
333 0 XSRETURN(1);
334 }
« blib/lib/Game/Mahjong/Error.pm ↑ Summary mahjong_decompose.c »