-
Notifications
You must be signed in to change notification settings - Fork 2
/
SkewHeap.xs
522 lines (412 loc) · 9.53 KB
/
SkewHeap.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
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#define PERL_NO_GET_CONTEXT
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"
/*
* Allocate memory with Newx if it's
* available - if it's an older perl
* that doesn't have Newx then we
* resort to using New.
*/
#ifndef Newx
#define Newx(v, n, t) New(0, v, n, t)
#endif
/*
* perl object ref to skewheap_t*
*/
#ifndef SKEW
#define SKEW(obj) ((skewheap_t*) SvIV(SvRV(obj)))
#endif
/*
* Thanks again, MLEHMANN:
* http://grokbase.com/t/perl/perl5-porters/097tr5nw6b/perl-67894-multicall-push-requires-perl-core
*/
#ifndef cxinc
#define cxinc() Perl_cxinc(aTHX)
#endif
typedef struct SkewNode {
struct SkewNode *left;
struct SkewNode *right;
SV *value;
} skewnode_t;
typedef struct SkewHeap {
skewnode_t *root;
IV size;
SV *cmp;
} skewheap_t;
static
skewnode_t* new_node(pTHX_ SV *value) {
skewnode_t *node;
Newx(node, 1, skewnode_t);
node->left = NULL;
node->right = NULL;
node->value = newSVsv(value);
return node;
}
static
skewnode_t* clone_node(pTHX_ skewnode_t *node) {
if (node == NULL) {
return NULL;
}
skewnode_t *new_node;
Newx(new_node, 1, skewnode_t);
new_node->value = newSVsv(node->value);
new_node->left = clone_node(aTHX_ node->left);
new_node->right = clone_node(aTHX_ node->right);
return new_node;
}
static
void free_node(pTHX_ skewnode_t *node) {
if (node->left != NULL) free_node(aTHX_ node->left);
if (node->right != NULL) free_node(aTHX_ node->right);
SvREFCNT_dec(node->value);
Safefree(node);
}
static
SV* new(pTHX_ const char *class, SV *cmp) {
skewheap_t *heap;
SV *obj;
SV *ref;
Newx(heap, 1, skewheap_t);
heap->root = NULL;
heap->size = 0;
heap->cmp = cmp;
SvREFCNT_inc(heap->cmp);
obj = newSViv((IV) heap);
ref = newRV_noinc(obj);
sv_bless(ref, gv_stashpv(class, GV_ADD));
SvREADONLY_on(obj);
return ref;
}
static
void DESTROY(pTHX_ SV *ref) {
skewheap_t *heap = SKEW(ref);
if (heap->root != NULL) free_node(aTHX_ heap->root);
SvREFCNT_dec(heap->cmp);
Safefree(heap);
}
static
size_t walk_tree(skewnode_t *node, skewnode_t *nodes[], size_t idx) {
size_t inc = 0;
nodes[ idx ] = node;
++inc;
if (node->left != NULL) {
inc += walk_tree(node->left, nodes, idx + inc);
}
if (node->right != NULL) {
inc += walk_tree(node->right, nodes, idx + inc);
}
return inc;
}
static
SV* to_array(pTHX_ SV *ref) {
skewheap_t *heap = SKEW(ref);
skewnode_t *nodes[ heap->size ];
AV *array = newAV();
size_t i;
walk_tree(heap->root, nodes, 0);
for (i = 0; i < heap->size; ++i) {
av_push(array, newSVsv( nodes[i]->value ));
}
return newRV_noinc( (SV*) array );
}
static
void sort_nodes(pTHX_ skewnode_t *nodes[], int length, SV *cmp) {
skewnode_t *tmp, *x;
int p, j;
int start = 0;
int end = length - 1;
int top = 1;
int stack[end - start + 1];
stack[0] = start;
stack[1] = end;
// set up multicall
dSP;
GV *agv, *bgv, *gv;
HV *stash;
// code value from sv code ref
CV *cv = sv_2cv(cmp, &stash, &gv, 0);
if (cv == Nullcv) {
croak("Not a subroutine reference");
}
agv = gv_fetchpv("main::a", GV_ADD, SVt_PV);
bgv = gv_fetchpv("main::b", GV_ADD, SVt_PV);
SAVESPTR(GvSV(agv));
SAVESPTR(GvSV(bgv));
dMULTICALL;
I8 gimme = G_SCALAR;
PUSH_MULTICALL(cv);
// multicall ready
while (top >= 0) {
end = stack[top--];
start = stack[top--];
x = nodes[end];
p = start - 1;
for (j = start; j <= end - 1; ++j) {
GvSV(agv) = nodes[j]->value;
GvSV(bgv) = x->value;
MULTICALL;
int test = SvIV(*PL_stack_sp);
if (test < 1) {
p++;
tmp = nodes[p];
nodes[p] = nodes[j];
nodes[j] = tmp;
}
}
tmp = nodes[++p];
nodes[p] = nodes[end];
nodes[end] = tmp;
if (p - 1 > start) {
stack[++top] = start;
stack[++top] = p - 1;
}
if (p + 1 < end) {
stack[++top] = p + 1;
stack[++top] = end;
}
}
POP_MULTICALL;
}
static
void _merge(pTHX_ SV *heap_ref, SV *heap_ref_a, SV *heap_ref_b) {
skewheap_t *heap = SKEW(heap_ref);
skewheap_t *heap_a = SKEW(heap_ref_a);
skewheap_t *heap_b = SKEW(heap_ref_b);
skewnode_t *a = heap_a->root;
skewnode_t *b = heap_b->root;
size_t size = heap_a->size + heap_b->size;
skewnode_t *todo[size];
skewnode_t *nodes[size];
skewnode_t *node, *prev, *tmp_node;
int tidx = 0;
int nidx = 0;
int i;
// Set the new heap's size
heap->size = size;
// Cut the right subtree from each path
if (a != NULL) todo[tidx++] = a;
if (b != NULL) todo[tidx++] = b;
while (tidx > 0) {
node = todo[--tidx];
tmp_node = new_node(aTHX_ node->value);
tmp_node->left = clone_node(aTHX_ node->left);
if (node->right != NULL) {
todo[tidx] = node->right;
++tidx;
}
nodes[nidx] = tmp_node;
++nidx;
}
if (nidx > 0) {
// Sort the subtrees
if (nidx > 1) {
sort_nodes(aTHX_ nodes, nidx, heap->cmp);
}
// Recombine subtrees
for (i = nidx; i > 1; --i) {
node = nodes[i - 1]; // last node
prev = nodes[i - 2]; // second to last node
// Set penultimate node's right child to its left (and only) subtree
if (prev->left != NULL) {
prev->right = prev->left;
}
// Set its left child to the ultimate node
prev->left = node;
}
heap->root = nodes[0];
}
return;
}
static
void _merge_destructive(pTHX_ skewheap_t *heap, skewnode_t *a, skewnode_t *b) {
skewnode_t* todo[heap->size];
skewnode_t* nodes[heap->size];
skewnode_t* node;
skewnode_t* prev;
int tidx = 0;
int nidx = 0;
int i;
if (a == NULL) {
heap->root = b;
return;
}
else if (b == NULL) {
heap->root = a;
return;
}
// Cut the right subtree from each path
todo[tidx++] = a;
todo[tidx++] = b;
while (tidx > 0) {
node = todo[--tidx];
if (node->right != NULL) {
todo[tidx++] = node->right;
node->right = NULL;
}
nodes[nidx++] = node;
}
if (nidx == 0) {
heap->root = NULL;
}
else {
// Sort the subtrees
if (nidx > 1) {
sort_nodes(aTHX_ nodes, nidx, heap->cmp);
}
// Recombine subtrees
for (i = nidx; i > 1; --i) {
node = nodes[i - 1]; // last node
prev = nodes[i - 2]; // second to last node
// Set penultimate node's right child to its left (and only) subtree
if (prev->left != NULL) {
prev->right = prev->left;
}
// Set its left child to the ultimate node
prev->left = node;
}
heap->root = nodes[0];
}
}
static
IV put_one(pTHX_ SV *ref, SV *value) {
skewheap_t *heap = SKEW(ref);
skewnode_t *node;
node = new_node(aTHX_ value);
++heap->size;
if (heap->root == NULL) {
heap->root = node;
} else {
_merge_destructive(aTHX_ heap, heap->root, node);
}
return heap->size;
}
static
SV* take(pTHX_ SV *ref) {
skewheap_t *heap = SKEW(ref);
skewnode_t *root = heap->root;
SV *item;
if (root != NULL) {
item = newSVsv(root->value);
--heap->size;
_merge_destructive(aTHX_ heap, root->left, root->right);
root->left = NULL;
root->right = NULL;
free_node(aTHX_ root);
}
else {
item = &PL_sv_undef;
}
return item;
}
static
SV* top(pTHX_ SV *ref) {
skewheap_t *heap = SKEW(ref);
return heap->root == NULL
? &PL_sv_undef
: newSVsv(heap->root->value);
}
static
IV size(pTHX_ SV *ref) {
skewheap_t *heap = SKEW(ref);
return heap->size;
}
static
SV* merge(pTHX_ SV *heap_a, SV *heap_b) {
SV *new_heap = new(aTHX_ "SkewHeap", SKEW(heap_a)->cmp);
_merge(aTHX_ new_heap, heap_a, heap_b);
return new_heap;
}
static
void _explain(pTHX_ SV *out, skewnode_t *node, int depth) {
int i;
for (i = 0; i < depth; ++i) sv_catpvn(out, "--", 2);
sv_catpvf(out, "NODE<%p>\n", (void*)node);
++depth;
for (i = 0; i < depth; ++i) sv_catpvn(out, "--", 2);
sv_catpvf(out, "VALUE<%p>: ", (void*)node->value);
sv_catsv(out, sv_mortalcopy(node->value));
sv_catpvn(out, "\n", 1);
if (node->left != NULL) {
for (i = 0; i < depth; ++i) sv_catpvn(out, "--", 2);
sv_catpvn(out, "LEFT:\n", 6);
_explain(aTHX_ out, node->left, depth + 1);
}
if (node->right != NULL) {
for (i = 0; i < depth; ++i) sv_catpvn(out, "--", 2);
sv_catpvn(out, "RIGHT:\n", 7);
_explain(aTHX_ out, node->right, depth + 1);
}
}
static
SV* explain(pTHX_ SV *ref) {
skewheap_t *heap = SKEW(ref);
SV *out = newSVpvn("", 0);
sv_catpvn(out, "SKEWHEAP:\n", 10);
if (heap->root != NULL) {
_explain(aTHX_ out, heap->root, 2);
}
return out;
}
MODULE = SkewHeap PACKAGE = SkewHeap
PROTOTYPES: ENABLE
VERSIONCHECK: ENABLE
SV* new(const char *class, SV *cmp)
PROTOTYPE: $&
CODE:
RETVAL = new(aTHX_ class, cmp);
OUTPUT:
RETVAL
SV* skewheap(SV *cmp)
PROTOTYPE: &
CODE:
RETVAL = new(aTHX_ "SkewHeap", cmp);
OUTPUT:
RETVAL
void DESTROY(SV *heap)
CODE:
DESTROY(aTHX_ heap);
IV put_one(SV *heap, SV *value)
CODE:
RETVAL = put_one(aTHX_ heap, value);
OUTPUT:
RETVAL
IV put(SV *heap, ...)
CODE:
size_t i;
for (i = 1; i < items; ++i) {
RETVAL = put_one(aTHX_ heap, ST(i));
}
OUTPUT:
RETVAL
SV* take(SV *heap)
CODE:
RETVAL = take(aTHX_ heap);
OUTPUT:
RETVAL
IV size(SV *heap)
CODE:
RETVAL = size(aTHX_ heap);
OUTPUT:
RETVAL
SV* merge(SV *heap_a, SV *heap_b)
CODE:
RETVAL = merge(aTHX_ heap_a, heap_b);
OUTPUT:
RETVAL
SV* top(SV *heap)
CODE:
RETVAL = top(aTHX_ heap);
OUTPUT:
RETVAL
SV* to_array(SV *heap)
CODE:
RETVAL = to_array(aTHX_ heap);
OUTPUT:
RETVAL
SV* explain(SV *heap)
CODE:
RETVAL = explain(aTHX_ heap);
OUTPUT:
RETVAL