Repository navigation
Expand file tree
/
Copy pathXS.xs
More file actions
453 lines (405 loc) · 11.8 KB
/
Copy pathXS.xs
File metadata and controls
453 lines (405 loc) · 11.8 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#define PERL_NO_GET_CONTEXT
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"
#ifndef NOT_REACHED
# define NOT_REACHED assert(0)
#endif
#define iParent(i) (((i)-1) / 2)
#define iLeftChild(i) ((2*(i)) + 1)
#define iRightChild(i) ((2*(i)) + 2)
#define HAVE_PERL_SV_NUMCMP \
(PERL_REVISION > 5 || (PERL_REVISION == 5 && (PERL_VERSION > 43 || (PERL_VERSION == 43 && PERL_SUBVERSION >= 8))))
#define OUT_OF_ORDER(a,child,parent,is_min) \
( ( (is_min & 2) \
? my_sv_string_gt(aTHX_ (a)[(child)], (a)[(parent)]) \
: my_sv_num_gt(aTHX_ (a)[(child)], (a)[(parent)])) \
? !(is_min & 1) : (is_min & 1) )
#define FORCE_SCALAR(fakeop) \
STMT_START { \
SAVEOP(); \
Copy(PL_op, &fakeop, 1, OP); \
fakeop.op_flags = OPf_WANT_SCALAR; \
PL_op = &fakeop; \
} STMT_END
#ifdef Perl_do_ncmp
#define my_Perl_do_ncmp Perl_do_ncmp
#else
/* compare left and right SVs. Returns:
* -1: <
* 0: ==
* 1: >
* 2: left or right was a NaN
*/
I32
my_Perl_do_ncmp(pTHX_ SV* const left, SV * const right)
{
PERL_ARGS_ASSERT_DO_NCMP;
/* Fortunately it seems NaN isn't IOK */
if (SvIV_please_nomg(right) && SvIV_please_nomg(left)) {
if (!SvIsUV(left)) {
const IV leftiv = SvIVX(left);
if (!SvIsUV(right)) {
/* ## IV <=> IV ## */
const IV rightiv = SvIVX(right);
return (leftiv > rightiv) - (leftiv < rightiv);
}
/* ## IV <=> UV ## */
if (leftiv < 0)
/* As (b) is a UV, it's >=0, so it must be < */
return -1;
{
const UV rightuv = SvUVX(right);
return ((UV)leftiv > rightuv) - ((UV)leftiv < rightuv);
}
}
if (SvIsUV(right)) {
/* ## UV <=> UV ## */
const UV leftuv = SvUVX(left);
const UV rightuv = SvUVX(right);
return (leftuv > rightuv) - (leftuv < rightuv);
}
/* ## UV <=> IV ## */
{
const IV rightiv = SvIVX(right);
if (rightiv < 0)
/* As (a) is a UV, it's >=0, so it cannot be < */
return 1;
{
const UV leftuv = SvUVX(left);
return (leftuv > (UV)rightiv) - (leftuv < (UV)rightiv);
}
}
NOT_REACHED; /* NOTREACHED */
}
{
NV const lnv = SvNV_nomg(left);
NV const rnv = SvNV_nomg(right);
#if defined(NAN_COMPARE_BROKEN) && defined(Perl_isnan)
if (Perl_isnan(lnv) || Perl_isnan(rnv)) {
return 2;
}
return (lnv > rnv) - (lnv < rnv);
#else
if (lnv < rnv)
return -1;
if (lnv > rnv)
return 1;
if (lnv == rnv)
return 0;
return 2;
#endif
}
}
#endif
static bool
my_has_real_overload_method(pTHX_ SV *sv, const char *name, STRLEN len)
{
GV *gv;
CV *cv;
GV *cvgv;
HV *stash;
const HEK *gvhek;
const HEK *stashek;
if (!SvAMAGIC(sv) || !SvROK(sv)) {
return FALSE;
}
stash = SvSTASH(SvRV(sv));
if (!stash) {
return FALSE;
}
gv = gv_fetchmeth_pvn(stash, name, len, -1, 0);
if (!gv) {
return FALSE;
}
cv = GvCV(gv);
if (!cv) {
return FALSE;
}
cvgv = CvGV(cv);
if (!cvgv) {
return TRUE;
}
gvhek = GvNAME_HEK(cvgv);
stashek = HvNAME_HEK(GvSTASH(cvgv));
if (!gvhek || !stashek) {
return TRUE;
}
return !(stashek
&& memEQs(HEK_KEY(gvhek), HEK_LEN(gvhek), "nil")
&& memEQs(HEK_KEY(stashek), HEK_LEN(stashek), "overload"));
}
static SV *
my_sv_2num(pTHX_ SV *sv)
{
if (!SvROK(sv)) {
return sv;
}
if (SvAMAGIC(sv)) {
SV *tmpsv = AMG_CALLunary(sv, numer_amg);
if (tmpsv && (!SvROK(tmpsv) || SvRV(tmpsv) != SvRV(sv))) {
return my_sv_2num(aTHX_ tmpsv);
}
}
return sv_2mortal(newSVuv(PTR2UV(SvRV(sv))));
}
static bool
my_sv_string_gt(pTHX_ SV *left, SV *right)
{
SV *tmpsv = NULL;
if (SvAMAGIC(left) || SvAMAGIC(right)) {
if (my_has_real_overload_method(aTHX_ left, "(gt", 3)
|| my_has_real_overload_method(aTHX_ right, "(gt", 3)) {
tmpsv = amagic_call(left, right, sgt_amg, 0);
if (tmpsv) {
return SvTRUE(tmpsv);
}
}
if (my_has_real_overload_method(aTHX_ left, "(cmp", 4)
|| my_has_real_overload_method(aTHX_ right, "(cmp", 4)) {
tmpsv = amagic_call(left, right, scmp_amg, 0);
if (tmpsv) {
return SvIV(tmpsv) > 0;
}
}
}
return Perl_sv_cmp(aTHX_ left, right) > 0;
}
static bool
my_sv_num_gt(pTHX_ SV *left, SV *right)
{
#if HAVE_PERL_SV_NUMCMP
return sv_numcmp(left, right) > 0;
#else
if (SvAMAGIC(left) || SvAMAGIC(right)) {
SV *tmpsv = NULL;
if (my_has_real_overload_method(aTHX_ left, "(>", 2)
|| my_has_real_overload_method(aTHX_ right, "(>", 2)) {
tmpsv = amagic_call(left, right, gt_amg, 0);
if (tmpsv) {
return SvTRUE(tmpsv);
}
}
if (my_has_real_overload_method(aTHX_ left, "(<=>", 4)
|| my_has_real_overload_method(aTHX_ right, "(<=>", 4)) {
tmpsv = amagic_call(left, right, ncmp_amg, 0);
if (tmpsv) {
return SvIV(tmpsv) > 0;
}
}
}
left = my_sv_2num(aTHX_ left);
right = my_sv_2num(aTHX_ right);
return my_Perl_do_ncmp(aTHX_ left, right) > 0;
#endif
}
I32 sift_up(pTHX_ SV **a, ssize_t start, ssize_t end, I32 is_min) {
/*start represents the limit of how far up the heap to sift.
end is the node to sift up. */
ssize_t child = end;
I32 swapped = 0;
SvGETMAGIC(a[child]);
while (child > start) {
ssize_t parent = iParent(child);
SvGETMAGIC(a[parent]);
if ( OUT_OF_ORDER(a,child,parent,is_min) ) {
SV *swap_tmp= a[parent];
a[parent]= a[child];
a[child]= swap_tmp;
child = parent; /* repeat to continue sifting up the parent now */
swapped++;
}
else {
return swapped;
}
}
return swapped;
}
/*Repair the heap whose root element is at index 'start', assuming the heaps rooted at its children are valid*/
I32 sift_down(pTHX_ SV **a, ssize_t start, ssize_t end, I32 is_min) {
ssize_t root = start;
I32 swapped = 0;
while (iLeftChild(root) <= end) { /* While the root has at least one child */
ssize_t child = iLeftChild(root); /* Left child of root */
ssize_t swap = root; /* Keeps track of child to swap with */
/* if the root is smaller than the left child
* then the swap is with the left child */
if ( OUT_OF_ORDER(a,child,swap,is_min) ) {
swap = child;
}
/* if there is a right child and the right child is larger than the root or the left child
* then the swap is with the right child */
if (child+1 <= end) {
if ( OUT_OF_ORDER(a,child+1,swap,is_min) ) {
swap = child + 1;
}
}
/* check if we need to swap or if this tree is in heap-order */
if (swap == root) {
/* The root is larger than both children, and as we assume the heaps rooted at the children are valid
* then we know we can stop. */
return swapped;
} else {
/* swap the root with the largest child */
SV *tmp= a[root];
a[root]= a[swap];
a[swap]= tmp;
/* continue sifting down the child by setting the root to the chosen child
* effectively we sink down the tree towards the leafs */
root = swap;
swapped++;
}
}
return swapped;
}
/* this is O(N log N) */
void heapify_with_sift_up(pTHX_ SV **a, ssize_t count, I32 is_min) {
ssize_t end = 1; /* end is assigned the index of the first (left) child of the root */
while (end < count) {
/*sift up the node at index end to the proper place such that all nodes above
the end index are in heap order */
(void)sift_up(aTHX_ a, 0, end, is_min);
end++;
}
/* after sifting up the last node all nodes are in heap order */
}
/* this is O(N) */
void heapify_with_sift_down(pTHX_ SV **a, ssize_t count, I32 is_min) {
/*start is assigned the index in 'a' of the last parent node
the last element in a 0-based array is at index count-1; find the parent of that element */
ssize_t start = iParent(count-1);
while (start >= 0) {
/* sift down the node at index 'start' to the proper place such that all nodes below
the start index are in heap order */
(void)sift_down(aTHX_ a, start, count - 1, is_min);
/* go to the next parent node */
start--;
}
/* after sifting down the root all nodes/elements are in heap order */
}
MODULE = Algorithm::Heapify::XS PACKAGE = Algorithm::Heapify::XS
void
max_heapify(av)
AV *av
PROTOTYPE: \@
ALIAS:
max_heapify = 0
min_heapify = 1
maxstr_heapify = 2
minstr_heapify = 3
PREINIT:
OP fakeop;
I32 count;
PPCODE:
FORCE_SCALAR(fakeop);
count = av_top_index(av)+1;
if ( count ) {
heapify_with_sift_down(aTHX_ AvARRAY(av),count,ix);
ST(0)= AvARRAY(av)[0];
XSRETURN(1);
}
else {
XSRETURN(0);
}
void
max_heap_shift(av)
AV *av
PROTOTYPE: \@
ALIAS:
max_heap_shift = 0
min_heap_shift = 1
maxstr_heap_shift = 2
minstr_heap_shift = 3
PREINIT:
OP fakeop;
I32 top;
I32 count;
PPCODE:
FORCE_SCALAR(fakeop);
top= av_top_index(av);
count= top+1;
if (count) {
SV *tmp= AvARRAY(av)[0];
AvARRAY(av)[0]= AvARRAY(av)[top];
AvARRAY(av)[top]= tmp;
ST(0)= av_pop(av);
if (count > 2)
sift_down(aTHX_ AvARRAY(av),0,top-1,ix);
XSRETURN(1);
}
else {
XSRETURN(0);
}
void
max_heap_push(av,sv)
AV *av
SV *sv
PROTOTYPE: \@$
ALIAS:
max_heap_push = 0
min_heap_push = 1
maxstr_heap_push = 2
minstr_heap_push = 3
PREINIT:
OP fakeop;
I32 top;
I32 count;
PPCODE:
FORCE_SCALAR(fakeop);
av_push(av,newSVsv(sv));
top= av_top_index(av);
count= top+1;
sift_up(aTHX_ AvARRAY(av),0,top,ix);
ST(0)= AvARRAY(av)[0];
XSRETURN(1);
void
max_heap_adjust_top(av)
AV *av
PROTOTYPE: \@
ALIAS:
max_heap_adjust_top = 0
min_heap_adjust_top = 1
maxstr_heap_adjust_top = 2
minstr_heap_adjust_top = 3
PREINIT:
OP fakeop;
I32 top;
I32 count;
PPCODE:
FORCE_SCALAR(fakeop);
top= av_top_index(av);
count= top+1;
if ( count ) {
(void)sift_down(aTHX_ AvARRAY(av),0,top,ix);
ST(0)= AvARRAY(av)[0];
XSRETURN(1);
} else {
XSRETURN(0);
}
void
max_heap_adjust_item(av,idx=0)
AV *av
I32 idx;
PROTOTYPE: \@;$
ALIAS:
max_heap_adjust_item = 0
min_heap_adjust_item = 1
maxstr_heap_adjust_item = 2
minstr_heap_adjust_item = 3
PREINIT:
OP fakeop;
I32 top;
I32 count;
PPCODE:
FORCE_SCALAR(fakeop);
top= av_top_index(av);
count= top+1;
if ( idx < count ) {
if (!idx || !sift_up(aTHX_ AvARRAY(av),0,idx,ix))
(void)sift_down(aTHX_ AvARRAY(av),idx,top,ix);
ST(0)= AvARRAY(av)[0];
XSRETURN(1);
} else {
XSRETURN(0);
}