-
Notifications
You must be signed in to change notification settings - Fork 2
/
Diaries.xs
237 lines (205 loc) · 6.3 KB
/
Diaries.xs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"
static int fib(n) {
if (n < 2)
return 1;
else
return fib(n-2) + fib(n-1);
}
MODULE = Acme::The::Secret::XS::Diaries PACKAGE = Acme::The::Secret::XS::Diaries
void
hello()
CODE:
printf("Hello, world!\n");
void
hello_to(someone)
const char * someone
CODE:
printf("Hello, %s!\n", someone);
int
is_even(input)
int input
CODE:
RETVAL = (input % 2 == 0);
OUTPUT:
RETVAL
void
hello_to_persons_arrayref(persons)
AV *persons;
PREINIT:
I32 i;
I32 len;
SV** element;
STRLEN str_len;
char * el_str;
I32 el_int;
PPCODE:
len = av_len(persons);
printf("Hello, %d persons:\n", len+1);
for (i = 0; i <= len; i++) {
element = av_fetch(persons, i, 0);
if (element != NULL) {
if (SvPOK(*element)) {
el_str = SvPV(*element, str_len);
printf(" %d: Hello, %s!\n", i, el_str);
} else if (SvIOK(*element)) {
el_int = SvIV(*element);
printf(" %d: Hello, %d!\n", i, el_int);
} else {
printf(" %d: (ignored)\n", i);
}
}
}
void
hello_to_persons_hashref(persons)
HV *persons;
PREINIT:
SV* value;
char* key;
I32 keylen;
STRLEN str_len;
char * el_str;
I32 el_int;
PPCODE:
hv_iterinit(persons);
printf("Hello, unknown amount of persons:\n");
while (value = hv_iternextsv(persons, &key, &keylen)) {
if (SvPOK(value)) {
el_str = SvPV(value, str_len);
printf(" Hello, %s %s!\n", el_str, key);
} else if (SvIOK(value)) {
el_int = SvIV(value);
printf(" Hello, %d %s!\n", el_int, key);
} else {
printf(" (ignored) %s\n", key);
}
}
void
hello_to_persons_hashref_by_keys(persons, keys)
HV *persons;
AV *keys;
PREINIT:
I32 i;
I32 len;
SV** key_el;
SV** value;
char* key;
STRLEN str_len;
char * el_str;
I32 el_int;
PPCODE:
len = av_len(keys);
printf("Hello, %d persons:\n", len+1);
for (i = 0; i <= len; i++) {
key_el = av_fetch(keys, i, 0);
if (key_el != NULL && SvPOK(*key_el)) {
key = SvPV(*key_el, str_len);
value = hv_fetch(persons, key, strlen(key), 0);
if (SvPOK(*value)) {
el_str = SvPV(*value, str_len);
printf(" Hello, %s %s!\n", el_str, key);
} else if (SvIOK(*value)) {
el_int = SvIV(*value);
printf(" Hello, %d %s!\n", el_int, key);
} else {
printf(" (ignored) %s\n", key);
}
}
}
void
get_5_ints()
PPCODE:
mXPUSHi(11);
mXPUSHi(33);
mXPUSHi(55);
mXPUSHi(77);
mXPUSHi(99);
void
get_2_strings()
INIT:
char * txt;
PPCODE:
txt = "AFFE";
mXPUSHp(txt, strlen(txt));
txt = "ZOMTEC";
mXPUSHp(txt, strlen(txt));
void
get_5_ints_faster()
PPCODE:
EXTEND(SP,5);
mPUSHi(11);
mPUSHi(33);
mPUSHi(55);
mPUSHi(77);
mPUSHi(99);
void
get_2_strings_faster()
INIT:
char * txt;
PPCODE:
EXTEND(SP,2);
txt = "AFFE";
mPUSHp(txt, strlen(txt));
txt = "ZOMTEC";
mPUSHp(txt, strlen(txt));
void
grep_even_integers(...)
INIT:
int i;
SV* el;
int value;
PPCODE:
for (i = 0; i < items; i++) {
el = ST(i);
if (SvIOK(el) || looks_like_number(el)) {
value = SvIV(el);
if (value % 2 == 0)
mXPUSHs(SvREFCNT_inc(el));
}
}
void
fib_overkill(n)
int n;
INIT:
int f1;
int f2;
PPCODE:
if (n < 2) {
mXPUSHi(1);
} else {
/* -------------------- f2 = fib(n-2) ----------------- */
ENTER;
SAVETMPS;
PUSHMARK(SP);
mXPUSHi(n-2);
PUTBACK;
call_pv("Acme::The::Secret::XS::Diaries::fib_overkill", 0);
SPAGAIN;
f2 = POPi;
PUTBACK;
FREETMPS;
LEAVE;
/* -------------------- f1 = fib(n-1) ----------------- */
ENTER;
SAVETMPS;
PUSHMARK(SP);
mXPUSHi(n-1);
PUTBACK;
call_pv("Acme::The::Secret::XS::Diaries::fib_overkill", 0);
SPAGAIN;
f1 = POPi;
PUTBACK;
FREETMPS;
LEAVE;
/* -------------------- f2 + f1 ----------------- */
mXPUSHi(f2+f1);
}
int
fib(n)
int n;
CODE:
RETVAL = fib(n);
OUTPUT:
RETVAL