ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Array-Heap/Heap.xs
(Generate patch)

Comparing Array-Heap/Heap.xs (file contents):
Revision 1.3 by root, Sun Jul 26 04:50:02 2009 UTC vs.
Revision 1.4 by root, Sun Jul 26 05:25:18 2009 UTC

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
201static void 206static void
202push_heap (AV *av, f_cmp cmp, void *cmp_data, SV *elem) 207push_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
208static SV * 221static SV *
209pop_heap (AV *av, f_cmp cmp, void *cmp_data) 222pop_heap (AV *av, f_cmp cmp, void *cmp_data)
210{ 223{
276 289
277void 290void
278push_heap (SV *heap, ...) 291push_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
287void 296void
288push_heap_lex (SV *heap, ...) 297push_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
297void 302void
298push_heap_cmp (SV *cmp, SV *heap, ...) 303push_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
311SV * 314SV *
312pop_heap (SV *heap) 315pop_heap (SV *heap)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines