… | |
… | |
8 | #endif |
8 | #endif |
9 | #ifndef gv_fetchpvs |
9 | #ifndef gv_fetchpvs |
10 | # define gv_fetchpvs gv_fetchpv |
10 | # define gv_fetchpvs gv_fetchpv |
11 | #endif |
11 | #endif |
12 | |
12 | |
|
|
13 | /* pre-5.8 compatibility */ |
|
|
14 | #ifndef PERL_MAGIC_tied |
|
|
15 | # define PERL_MAGIC_tied 'P' |
|
|
16 | #endif |
|
|
17 | |
13 | #include "multicall.h" |
18 | #include "multicall.h" |
14 | |
19 | |
15 | /* workaround for buggy multicall API */ |
20 | /* workaround for buggy multicall API */ |
16 | #ifndef cxinc |
21 | #ifndef cxinc |
17 | # define cxinc() Perl_cxinc (aTHX) |
22 | # define cxinc() Perl_cxinc (aTHX) |
… | |
… | |
197 | for (i = 0; i <= len; ++i) |
202 | for (i = 0; i <= len; ++i) |
198 | upheap (av, cmp, cmp_data, i); |
203 | upheap (av, cmp, cmp_data, i); |
199 | } |
204 | } |
200 | |
205 | |
201 | static void |
206 | static void |
202 | push_heap (AV *av, f_cmp cmp, void *cmp_data, SV *elem) |
207 | push_heap (AV *av, f_cmp cmp, void *cmp_data, SV **elems, int nelems) |
203 | { |
208 | { |
204 | av_push (av, newSVsv (elem)); |
209 | int i; |
|
|
210 | |
|
|
211 | av_extend (av, AvFILLp (av) + nelems); |
|
|
212 | |
|
|
213 | /* we do it in two steps, as the perl cmp function might copy the stack */ |
|
|
214 | for (i = 0; i < nelems; ++i) |
|
|
215 | AvARRAY (av)[++AvFILLp (av)] = newSVsv (elems [i]); |
|
|
216 | |
|
|
217 | for (i = 0; i < nelems; ++i) |
205 | upheap (av, cmp, cmp_data, AvFILLp (av)); |
218 | upheap (av, cmp, cmp_data, AvFILLp (av) - i); |
206 | } |
219 | } |
207 | |
220 | |
208 | static SV * |
221 | static SV * |
209 | pop_heap (AV *av, f_cmp cmp, void *cmp_data) |
222 | pop_heap (AV *av, f_cmp cmp, void *cmp_data) |
210 | { |
223 | { |
… | |
… | |
276 | |
289 | |
277 | void |
290 | void |
278 | push_heap (SV *heap, ...) |
291 | push_heap (SV *heap, ...) |
279 | PROTOTYPE: \@@ |
292 | PROTOTYPE: \@@ |
280 | CODE: |
293 | CODE: |
281 | { |
|
|
282 | int i; |
|
|
283 | for (i = 1; i < items; i++) |
|
|
284 | push_heap (array (heap), cmp_nv, 0, ST(i)); |
294 | push_heap (array (heap), cmp_nv, 0, &(ST(1)), items - 1); |
285 | } |
|
|
286 | |
295 | |
287 | void |
296 | void |
288 | push_heap_lex (SV *heap, ...) |
297 | push_heap_lex (SV *heap, ...) |
289 | PROTOTYPE: \@@ |
298 | PROTOTYPE: \@@ |
290 | CODE: |
299 | CODE: |
291 | { |
|
|
292 | int i; |
|
|
293 | for (i = 1; i < items; i++) |
|
|
294 | push_heap (array (heap), cmp_sv, 0, ST(i)); |
300 | push_heap (array (heap), cmp_sv, 0, &(ST(1)), items - 1); |
295 | } |
|
|
296 | |
301 | |
297 | void |
302 | void |
298 | push_heap_cmp (SV *cmp, SV *heap, ...) |
303 | push_heap_cmp (SV *cmp, SV *heap, ...) |
299 | PROTOTYPE: &\@@ |
304 | PROTOTYPE: &\@@ |
300 | CODE: |
305 | CODE: |
301 | { |
306 | { |
302 | int i; |
307 | SV **st_2 = &(ST(2)); /* multicall.h uses PUSHSTACK */ |
303 | dCMP; |
308 | dCMP; |
304 | |
|
|
305 | CMP_PUSH (cmp); |
309 | CMP_PUSH (cmp); |
306 | for (i = 2; i < items; i++) |
|
|
307 | push_heap (array (heap), cmp_custom, cmp_data, ST(i)); |
310 | push_heap (array (heap), cmp_custom, cmp_data, st_2, items - 2); |
308 | CMP_POP; |
311 | CMP_POP; |
309 | } |
312 | } |
310 | |
313 | |
311 | SV * |
314 | SV * |
312 | pop_heap (SV *heap) |
315 | pop_heap (SV *heap) |