HeapPQ.c

filter: Statementstmt 65.2% 1220 / 1869 Branchbran 43.5% 486 / 1116 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 57.1% 1706 / 2985 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/Heap/PQ.pm »
1 /*
2 * heap.c - Ultra-fast binary heap (priority queue)
3 *
4 * Three API levels for different speed/convenience tradeoffs:
5 *
6 * 1. RAW ARRAY API (fastest - matches Array::Heap speed)
7 * push_heap_min(\@array, $val)
8 * pop_heap_min(\@array)
9 * make_heap_min(\@array) # O(n) Floyd's heapify
10 *
11 * 2. NUMERIC HEAP (very fast - stores NV directly, no SV overhead)
12 * my $h = heap::new_nv('min');
13 * $h->push(3.14); # No SV allocation
14 * $h->pop; # Returns NV directly
15 *
16 * 3. OO HEAP (convenient - stores any Perl values)
17 * my $h = heap::new('min');
18 * $h->push($anything);
19 *
20 * Optimizations:
21 * - Custom ops bypass method dispatch
22 * - Inlined comparison for min/max heaps
23 * - Floyd's O(n) heapify for bulk operations
24 * - Zero-copy returns where possible
25 */
26  
27 #include "EXTERN.h"
28 #include "perl.h"
29 #include "XSUB.h"
30 #include "heap_compat.h"
31  
32 /* ============================================
33 Heap type enum
34 ============================================ */
35 typedef enum {
36 HEAP_MIN = 0,
37 HEAP_MAX = 1
38 } HeapType;
39  
40 /* ============================================
41 Standard Heap structure (stores SV*)
42 ============================================ */
43 typedef struct Heap_s Heap;
44 typedef void (*heap_sift_fn)(pTHX_ Heap*, IV);
45  
46 struct Heap_s {
47 SV **data;
48 NV *priorities;
49 IV size;
50 IV capacity;
51 HeapType type;
52 SV *comparator;
53 GV *gv_a;
54 GV *gv_b;
55 char **key_path;
56 STRLEN *key_lens;
57 IV key_depth;
58 heap_sift_fn sift_up;
59 heap_sift_fn sift_down;
60 };
61  
62 /* ============================================
63 Numeric Heap structure (stores NV directly)
64 ============================================ */
65 typedef struct {
66 NV *data;
67 IV size;
68 IV capacity;
69 HeapType type;
70 } NumericHeap;
71  
72 /* ============================================
73 Custom op declarations
74 ============================================ */
75 static XOP heap_func_push_xop;
76 static XOP heap_func_pop_xop;
77 static XOP heap_func_peek_xop;
78 static XOP heap_func_size_xop;
79  
80 /* Raw array ops */
81 static XOP push_heap_min_xop;
82 static XOP pop_heap_min_xop;
83 static XOP push_heap_max_xop;
84 static XOP pop_heap_max_xop;
85 static XOP make_heap_min_xop;
86 static XOP make_heap_max_xop;
87  
88 /* Numeric heap ops */
89 static XOP nv_push_xop;
90 static XOP nv_pop_xop;
91 static XOP nv_peek_xop;
92 static XOP nv_size_xop;
93 static XOP nv_peek_n_xop;
94  
95 /* peek_n ops */
96 static XOP heap_func_peek_n_xop;
97  
98 /* is_empty/clear/type ops */
99 static XOP heap_func_is_empty_xop;
100 static XOP heap_func_clear_xop;
101 static XOP heap_func_type_xop;
102 static XOP nv_is_empty_xop;
103 static XOP nv_clear_xop;
104  
105 /* search/delete ops */
106 static XOP heap_func_search_xop;
107 static XOP heap_func_delete_xop;
108 static XOP nv_search_xop;
109 static XOP nv_delete_xop;
110  
111 /* ============================================
112 Magic vtables
113 ============================================ */
114 static int heap_free(pTHX_ SV *sv, MAGIC *mg);
115 static int numeric_heap_free(pTHX_ SV *sv, MAGIC *mg);
116  
117 static MGVTBL heap_vtbl = {
118 NULL, NULL, NULL, NULL,
119 heap_free,
120 NULL, NULL, NULL
121 };
122  
123 static MGVTBL numeric_heap_vtbl = {
124 NULL, NULL, NULL, NULL,
125 numeric_heap_free,
126 NULL, NULL, NULL
127 };
128  
129 /* ============================================
130 Unified heap lookup (single magic walk)
131 ============================================ */
132 typedef enum { MAGIC_NONE, MAGIC_HEAP, MAGIC_NUMERIC } HeapMagicType;
133  
134 typedef struct {
135 HeapMagicType type;
136 union {
137 Heap *heap;
138 NumericHeap *nheap;
139 } ptr;
140 } HeapLookup;
141  
142 18675 PERL_STATIC_INLINE HeapLookup find_heap(pTHX_ SV *obj) {
143 HeapLookup result;
144 MAGIC *mg;
145 18675 result.type = MAGIC_NONE;
146 18675 result.ptr.heap = NULL;
147 18675 ▶ if (!SvROK(obj)) return result;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(obj)) return result; 0 18675 18675
148 18675 mg = mg_find(SvRV(obj), PERL_MAGIC_ext);
149 18675 ▶ while (mg) {
Branch: 1 branch
BranchTrueFalseTotal
while (mg) { 18675 0 18675
150 18675 ▶ if (mg->mg_virtual == &heap_vtbl) {
Branch: 1 branch
BranchTrueFalseTotal
if (mg->mg_virtual == &heap_vtbl) { 18669 6 18675
151 18669 result.type = MAGIC_HEAP;
152 18669 result.ptr.heap = (Heap*)mg->mg_ptr;
153 18669 return result;
154 }
155 6 ▶ if (mg->mg_virtual == &numeric_heap_vtbl) {
Branch: 1 branch
BranchTrueFalseTotal
if (mg->mg_virtual == &numeric_heap_vtbl) { 6 0 6
156 6 result.type = MAGIC_NUMERIC;
157 6 result.ptr.nheap = (NumericHeap*)mg->mg_ptr;
158 6 return result;
159 }
160 0 mg = mg->mg_moremagic;
161 }
162 0 return result;
163 }
164  
165 /* ============================================
166 Get structures from blessed SV
167 ============================================ */
168 /* Fast path: OO methods always receive a valid blessed ref.
169 Our magic is always the first PERL_MAGIC_ext on the SV. */
170 #define HEAP_FAST(sv) ((Heap*)mg_find(SvRV(sv), PERL_MAGIC_ext)->mg_ptr)
171 #define NV_HEAP_FAST(sv) ((NumericHeap*)mg_find(SvRV(sv), PERL_MAGIC_ext)->mg_ptr)
172  
173 #define GET_HEAP_MAGIC(obj) mg_find(SvRV(obj), PERL_MAGIC_ext)
174  
175 5 PERL_STATIC_INLINE Heap* get_heap(pTHX_ SV *obj) {
176 MAGIC *mg;
177 5 ▶ if (!SvROK(obj)) croak("Not a reference");
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(obj)) croak("Not a reference"); 0 5 5
178 5 mg = mg_find(SvRV(obj), PERL_MAGIC_ext);
179 5 ▶ while (mg) {
Branch: 1 branch
BranchTrueFalseTotal
while (mg) { 5 0 5
180 5 ▶ if (mg->mg_virtual == &heap_vtbl) {
Branch: 1 branch
BranchTrueFalseTotal
if (mg->mg_virtual == &heap_vtbl) { 5 0 5
181 5 return (Heap*)mg->mg_ptr;
182 }
183 0 mg = mg->mg_moremagic;
184 }
185 0 croak("Not a heap object");
186 return NULL;
187 }
188  
189 PERL_STATIC_INLINE NumericHeap* get_numeric_heap(pTHX_ SV *obj) {
190 MAGIC *mg;
191 if (!SvROK(obj)) croak("Not a reference");
192 mg = mg_find(SvRV(obj), PERL_MAGIC_ext);
193 while (mg) {
194 if (mg->mg_virtual == &numeric_heap_vtbl) {
195 return (NumericHeap*)mg->mg_ptr;
196 }
197 mg = mg->mg_moremagic;
198 }
199 croak("Not a numeric heap object");
200 return NULL;
201 }
202  
203 /* ============================================
204 PART 1: RAW ARRAY API (fastest)
205 Operates directly on Perl arrays
206 ============================================ */
207  
208 /* Sift up for raw array - min heap */
209 4409 static void raw_sift_up_min(pTHX_ AV *av, IV idx) {
210 4409 SV **arr = AvARRAY(av);
211 4409 SV *val = arr[idx];
212 4409 NV val_nv = SvNV(val);
213  
214 4414 ▶ while (idx > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (idx > 0) { 3809 605 4414
215 3809 IV parent = (idx - 1) >> 1;
216 3809 NV parent_nv = SvNV(arr[parent]);
217 3809 ▶ if (val_nv < parent_nv) {
Branch: 1 branch
BranchTrueFalseTotal
if (val_nv < parent_nv) { 5 3804 3809
218 5 arr[idx] = arr[parent];
219 5 idx = parent;
220 } else {
221 3804 break;
222 }
223 }
224 4409 arr[idx] = val;
225 4409 }
226  
227 /* Sift up for raw array - max heap */
228 409 static void raw_sift_up_max(pTHX_ AV *av, IV idx) {
229 409 SV **arr = AvARRAY(av);
230 409 SV *val = arr[idx];
231 409 NV val_nv = SvNV(val);
232  
233 415 ▶ while (idx > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (idx > 0) { 10 405 415
234 10 IV parent = (idx - 1) >> 1;
235 10 NV parent_nv = SvNV(arr[parent]);
236 10 ▶ if (val_nv > parent_nv) {
Branch: 1 branch
BranchTrueFalseTotal
if (val_nv > parent_nv) { 6 4 10
237 6 arr[idx] = arr[parent];
238 6 idx = parent;
239 } else {
240 4 break;
241 }
242 }
243 409 arr[idx] = val;
244 409 }
245  
246 /* Sift down for raw array - min heap */
247 6530 static void raw_sift_down_min(pTHX_ AV *av, IV idx, IV size) {
248 6530 SV **arr = AvARRAY(av);
249 6530 SV *val = arr[idx];
250 6530 NV val_nv = SvNV(val);
251 6530 IV half = size >> 1;
252  
253 24834 ▶ while (idx < half) {
Branch: 1 branch
BranchTrueFalseTotal
while (idx < half) { 18858 5976 24834
254 18858 IV left = (idx << 1) + 1;
255 18858 IV right = left + 1;
256 18858 IV best = left;
257 18858 NV best_nv = SvNV(arr[left]);
258  
259 18858 ▶ if (right < size) {
Branch: 1 branch
BranchTrueFalseTotal
if (right < size) { 18436 422 18858
260 18436 NV right_nv = SvNV(arr[right]);
261 18436 ▶ if (right_nv < best_nv) {
Branch: 1 branch
BranchTrueFalseTotal
if (right_nv < best_nv) { 8313 10123 18436
262 8313 best = right;
263 8313 best_nv = right_nv;
264 }
265 }
266  
267 18858 ▶ if (best_nv < val_nv) {
Branch: 1 branch
BranchTrueFalseTotal
if (best_nv < val_nv) { 18304 554 18858
268 18304 arr[idx] = arr[best];
269 18304 idx = best;
270 } else {
271 554 break;
272 }
273 }
274 6530 arr[idx] = val;
275 6530 }
276  
277 /* Sift down for raw array - max heap */
278 1215 static void raw_sift_down_max(pTHX_ AV *av, IV idx, IV size) {
279 1215 SV **arr = AvARRAY(av);
280 1215 SV *val = arr[idx];
281 1215 NV val_nv = SvNV(val);
282 1215 IV half = size >> 1;
283  
284 2427 ▶ while (idx < half) {
Branch: 1 branch
BranchTrueFalseTotal
while (idx < half) { 1617 810 2427
285 1617 IV left = (idx << 1) + 1;
286 1617 IV right = left + 1;
287 1617 IV best = left;
288 1617 NV best_nv = SvNV(arr[left]);
289  
290 1617 ▶ if (right < size) {
Branch: 1 branch
BranchTrueFalseTotal
if (right < size) { 1613 4 1617
291 1613 NV right_nv = SvNV(arr[right]);
292 1613 ▶ if (right_nv > best_nv) {
Branch: 1 branch
BranchTrueFalseTotal
if (right_nv > best_nv) { 804 809 1613
293 804 best = right;
294 804 best_nv = right_nv;
295 }
296 }
297  
298 1617 ▶ if (best_nv > val_nv) {
Branch: 1 branch
BranchTrueFalseTotal
if (best_nv > val_nv) { 1212 405 1617
299 1212 arr[idx] = arr[best];
300 1212 idx = best;
301 } else {
302 405 break;
303 }
304 }
305 1215 arr[idx] = val;
306 1215 }
307  
308 /* push_heap_min(\@array, $value) */
309 0 XS_EXTERNAL(XS_push_heap_min) {
310 0 dXSARGS;
311 AV *av;
312 SV *val;
313 IV size;
314  
315 0 ▶ if (items != 2) croak("Usage: push_heap_min(\\@array, $value)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 2) croak("Usage: push_heap_min(\\@array, $value)"); 0 0 0
316 0 ▶ if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVAV) { 0 0 0
if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVAV) { 0 0 0
317 0 croak("First argument must be an array reference");
318 }
319  
320 0 av = (AV*)SvRV(ST(0));
321 0 val = newSVsv(ST(1));
322  
323 0 av_push(av, val);
324 0 size = av_len(av) + 1;
325 0 raw_sift_up_min(aTHX_ av, size - 1);
326  
327 0 XSRETURN_EMPTY;
328 }
329  
330 /* push_heap_max(\@array, $value) */
331 0 XS_EXTERNAL(XS_push_heap_max) {
332 0 dXSARGS;
333 AV *av;
334 SV *val;
335 IV size;
336  
337 0 ▶ if (items != 2) croak("Usage: push_heap_max(\\@array, $value)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 2) croak("Usage: push_heap_max(\\@array, $value)"); 0 0 0
338 0 ▶ if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVAV) { 0 0 0
if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVAV) { 0 0 0
339 0 croak("First argument must be an array reference");
340 }
341  
342 0 av = (AV*)SvRV(ST(0));
343 0 val = newSVsv(ST(1));
344  
345 0 av_push(av, val);
346 0 size = av_len(av) + 1;
347 0 raw_sift_up_max(aTHX_ av, size - 1);
348  
349 0 XSRETURN_EMPTY;
350 }
351  
352 /* pop_heap_min(\@array) */
353 0 XS_EXTERNAL(XS_pop_heap_min) {
354 0 dXSARGS;
355 AV *av;
356 IV size;
357 SV *result;
358 SV **arr;
359  
360 0 ▶ if (items != 1) croak("Usage: pop_heap_min(\\@array)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: pop_heap_min(\\@array)"); 0 0 0
361 0 ▶ if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVAV) { 0 0 0
if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVAV) { 0 0 0
362 0 croak("First argument must be an array reference");
363 }
364  
365 0 av = (AV*)SvRV(ST(0));
366 0 size = av_len(av) + 1;
367  
368 0 ▶ if (size == 0) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (size == 0) XSRETURN_UNDEF; 0 0 0
369  
370 0 arr = AvARRAY(av);
371 0 result = arr[0];
372  
373 0 ▶ if (size > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (size > 1) { 0 0 0
374 0 arr[0] = arr[size - 1];
375 0 AvFILLp(av) = size - 2;
376 0 raw_sift_down_min(aTHX_ av, 0, size - 1);
377 } else {
378 0 AvFILLp(av) = -1;
379 }
380  
381 0 ST(0) = sv_2mortal(result);
382 0 XSRETURN(1);
383 }
384  
385 /* pop_heap_max(\@array) */
386 0 XS_EXTERNAL(XS_pop_heap_max) {
387 0 dXSARGS;
388 AV *av;
389 IV size;
390 SV *result;
391 SV **arr;
392  
393 0 ▶ if (items != 1) croak("Usage: pop_heap_max(\\@array)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: pop_heap_max(\\@array)"); 0 0 0
394 0 ▶ if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVAV) { 0 0 0
if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVAV) { 0 0 0
395 0 croak("First argument must be an array reference");
396 }
397  
398 0 av = (AV*)SvRV(ST(0));
399 0 size = av_len(av) + 1;
400  
401 0 ▶ if (size == 0) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (size == 0) XSRETURN_UNDEF; 0 0 0
402  
403 0 arr = AvARRAY(av);
404 0 result = arr[0];
405  
406 0 ▶ if (size > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (size > 1) { 0 0 0
407 0 arr[0] = arr[size - 1];
408 0 AvFILLp(av) = size - 2;
409 0 raw_sift_down_max(aTHX_ av, 0, size - 1);
410 } else {
411 0 AvFILLp(av) = -1;
412 }
413  
414 0 ST(0) = sv_2mortal(result);
415 0 XSRETURN(1);
416 }
417  
418 /* make_heap_min(\@array) - Floyd's O(n) heapify */
419 0 XS_EXTERNAL(XS_make_heap_min) {
420 0 dXSARGS;
421 AV *av;
422 IV size, i;
423  
424 0 ▶ if (items != 1) croak("Usage: make_heap_min(\\@array)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: make_heap_min(\\@array)"); 0 0 0
425 0 ▶ if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVAV) { 0 0 0
if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVAV) { 0 0 0
426 0 croak("First argument must be an array reference");
427 }
428  
429 0 av = (AV*)SvRV(ST(0));
430 0 size = av_len(av) + 1;
431  
432 /* Floyd's algorithm: sift down from middle to root */
433 0 ▶ for (i = (size >> 1) - 1; i >= 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = (size >> 1) - 1; i >= 0; i--) { 0 0 0
434 0 raw_sift_down_min(aTHX_ av, i, size);
435 }
436  
437 0 XSRETURN_EMPTY;
438 }
439  
440 /* make_heap_max(\@array) - Floyd's O(n) heapify */
441 0 XS_EXTERNAL(XS_make_heap_max) {
442 0 dXSARGS;
443 AV *av;
444 IV size, i;
445  
446 0 ▶ if (items != 1) croak("Usage: make_heap_max(\\@array)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: make_heap_max(\\@array)"); 0 0 0
447 0 ▶ if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVAV) { 0 0 0
if (!SvROK(ST(0)) || SvTYPE(SvRV(ST(0))) != SVt_PVAV) { 0 0 0
448 0 croak("First argument must be an array reference");
449 }
450  
451 0 av = (AV*)SvRV(ST(0));
452 0 size = av_len(av) + 1;
453  
454 0 ▶ for (i = (size >> 1) - 1; i >= 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = (size >> 1) - 1; i >= 0; i--) { 0 0 0
455 0 raw_sift_down_max(aTHX_ av, i, size);
456 }
457  
458 0 XSRETURN_EMPTY;
459 }
460  
461 /* ============================================
462 Raw array custom ops
463 ============================================ */
464  
465 /* pp_push_heap_min: BINOP(\@array, $value) */
466 4409 static OP* pp_push_heap_min(pTHX) {
467 4409 dSP;
468 4409 SV *val_sv = TOPs;
469 4409 SV *aref = TOPm1s;
470 AV *av;
471 SV *val;
472 IV size;
473  
474 4409 ▶ if (!SvROK(aref) || SvTYPE(SvRV(aref)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(aref) || SvTYPE(SvRV(aref)) != SVt_PVAV) 4409 0 4409
if (!SvROK(aref) || SvTYPE(SvRV(aref)) != SVt_PVAV) 0 4409 4409
475 0 croak("First argument must be an array reference");
476  
477 4409 av = (AV*)SvRV(aref);
478 4409 val = newSVsv(val_sv);
479 4409 av_push(av, val);
480 4409 size = av_len(av) + 1;
481 4409 raw_sift_up_min(aTHX_ av, size - 1);
482  
483 4409 SP--;
484 4409 SETs(&PL_sv_undef);
485 4409 RETURN;
486 }
487  
488 /* pp_push_heap_max: BINOP(\@array, $value) */
489 409 static OP* pp_push_heap_max(pTHX) {
490 409 dSP;
491 409 SV *val_sv = TOPs;
492 409 SV *aref = TOPm1s;
493 AV *av;
494 SV *val;
495 IV size;
496  
497 409 ▶ if (!SvROK(aref) || SvTYPE(SvRV(aref)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(aref) || SvTYPE(SvRV(aref)) != SVt_PVAV) 409 0 409
if (!SvROK(aref) || SvTYPE(SvRV(aref)) != SVt_PVAV) 0 409 409
498 0 croak("First argument must be an array reference");
499  
500 409 av = (AV*)SvRV(aref);
501 409 val = newSVsv(val_sv);
502 409 av_push(av, val);
503 409 size = av_len(av) + 1;
504 409 raw_sift_up_max(aTHX_ av, size - 1);
505  
506 409 SP--;
507 409 SETs(&PL_sv_undef);
508 409 RETURN;
509 }
510  
511 /* pp_pop_heap_min: UNOP(\@array) */
512 5422 static OP* pp_pop_heap_min(pTHX) {
513 5422 dSP;
514 5422 SV *aref = TOPs;
515 AV *av;
516 IV size;
517 SV *result;
518 SV **arr;
519  
520 5422 ▶ if (!SvROK(aref) || SvTYPE(SvRV(aref)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(aref) || SvTYPE(SvRV(aref)) != SVt_PVAV) 5422 0 5422
if (!SvROK(aref) || SvTYPE(SvRV(aref)) != SVt_PVAV) 0 5422 5422
521 0 croak("First argument must be an array reference");
522  
523 5422 av = (AV*)SvRV(aref);
524 5422 size = av_len(av) + 1;
525  
526 5422 ▶ if (size == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (size == 0) { 1 5421 5422
527 1 SETs(&PL_sv_undef);
528 1 RETURN;
529 }
530  
531 5421 arr = AvARRAY(av);
532 5421 result = arr[0];
533  
534 5421 ▶ if (size > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (size > 1) { 4816 605 5421
535 4816 arr[0] = arr[size - 1];
536 4816 AvFILLp(av) = size - 2;
537 4816 raw_sift_down_min(aTHX_ av, 0, size - 1);
538 } else {
539 605 AvFILLp(av) = -1;
540 }
541  
542 5421 SETs(sv_2mortal(result));
543 5421 RETURN;
544 }
545  
546 /* pp_pop_heap_max: UNOP(\@array) */
547 411 static OP* pp_pop_heap_max(pTHX) {
548 411 dSP;
549 411 SV *aref = TOPs;
550 AV *av;
551 IV size;
552 SV *result;
553 SV **arr;
554  
555 411 ▶ if (!SvROK(aref) || SvTYPE(SvRV(aref)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(aref) || SvTYPE(SvRV(aref)) != SVt_PVAV) 411 0 411
if (!SvROK(aref) || SvTYPE(SvRV(aref)) != SVt_PVAV) 0 411 411
556 0 croak("First argument must be an array reference");
557  
558 411 av = (AV*)SvRV(aref);
559 411 size = av_len(av) + 1;
560  
561 411 ▶ if (size == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (size == 0) { 1 410 411
562 1 SETs(&PL_sv_undef);
563 1 RETURN;
564 }
565  
566 410 arr = AvARRAY(av);
567 410 result = arr[0];
568  
569 410 ▶ if (size > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (size > 1) { 8 402 410
570 8 arr[0] = arr[size - 1];
571 8 AvFILLp(av) = size - 2;
572 8 raw_sift_down_max(aTHX_ av, 0, size - 1);
573 } else {
574 402 AvFILLp(av) = -1;
575 }
576  
577 410 SETs(sv_2mortal(result));
578 410 RETURN;
579 }
580  
581 /* pp_make_heap_min: UNOP(\@array) */
582 409 static OP* pp_make_heap_min(pTHX) {
583 409 dSP;
584 409 SV *aref = TOPs;
585 AV *av;
586 IV size, i;
587  
588 409 ▶ if (!SvROK(aref) || SvTYPE(SvRV(aref)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(aref) || SvTYPE(SvRV(aref)) != SVt_PVAV) 409 0 409
if (!SvROK(aref) || SvTYPE(SvRV(aref)) != SVt_PVAV) 0 409 409
589 0 croak("First argument must be an array reference");
590  
591 409 av = (AV*)SvRV(aref);
592 409 size = av_len(av) + 1;
593  
594 2123 ▶ for (i = (size >> 1) - 1; i >= 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = (size >> 1) - 1; i >= 0; i--) { 1714 409 2123
595 1714 raw_sift_down_min(aTHX_ av, i, size);
596 }
597  
598 409 SETs(&PL_sv_undef);
599 409 RETURN;
600 }
601  
602 /* pp_make_heap_max: UNOP(\@array) */
603 405 static OP* pp_make_heap_max(pTHX) {
604 405 dSP;
605 405 SV *aref = TOPs;
606 AV *av;
607 IV size, i;
608  
609 405 ▶ if (!SvROK(aref) || SvTYPE(SvRV(aref)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(aref) || SvTYPE(SvRV(aref)) != SVt_PVAV) 405 0 405
if (!SvROK(aref) || SvTYPE(SvRV(aref)) != SVt_PVAV) 0 405 405
610 0 croak("First argument must be an array reference");
611  
612 405 av = (AV*)SvRV(aref);
613 405 size = av_len(av) + 1;
614  
615 1612 ▶ for (i = (size >> 1) - 1; i >= 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = (size >> 1) - 1; i >= 0; i--) { 1207 405 1612
616 1207 raw_sift_down_max(aTHX_ av, i, size);
617 }
618  
619 405 SETs(&PL_sv_undef);
620 405 RETURN;
621 }
622  
623 /* ============================================
624 PART 2: NUMERIC HEAP (stores NV directly)
625 ============================================ */
626  
627 4340 PERL_STATIC_INLINE void nv_ensure_capacity(NumericHeap *h, IV needed) {
628 4340 ▶ if (needed > h->capacity) {
Branch: 1 branch
BranchTrueFalseTotal
if (needed > h->capacity) { 7 4333 4340
629 7 ▶ IV new_cap = h->capacity ? h->capacity * 2 : 16;
Branch: 1 branch
BranchTrueFalseTotal
IV new_cap = h->capacity ? h->capacity * 2 : 16; 7 0 7
630 12 ▶ while (new_cap < needed) new_cap *= 2;
Branch: 1 branch
BranchTrueFalseTotal
while (new_cap < needed) new_cap *= 2; 5 7 12
631 7 ▶ Renew(h->data, new_cap, NV);
Branch: 1 branch
BranchTrueFalseTotal
Renew(h->data, new_cap, NV); 0 7 7
632 7 h->capacity = new_cap;
633 }
634 4340 }
635  
636 /* Sift up for NV min-heap */
637 4134 static void nv_sift_up_min(NumericHeap *h, IV idx) {
638 4134 NV *data = h->data;
639 4134 NV val = data[idx];
640  
641 5364 ▶ while (idx > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (idx > 0) { 4104 1260 5364
642 4104 IV parent = (idx - 1) >> 1;
643 4104 ▶ if (val < data[parent]) {
Branch: 1 branch
BranchTrueFalseTotal
if (val < data[parent]) { 1230 2874 4104
644 1230 data[idx] = data[parent];
645 1230 idx = parent;
646 } else {
647 2874 break;
648 }
649 }
650 4134 data[idx] = val;
651 4134 }
652  
653 /* Sift up for NV max-heap */
654 1019 static void nv_sift_up_max(NumericHeap *h, IV idx) {
655 1019 NV *data = h->data;
656 1019 NV val = data[idx];
657  
658 2228 ▶ while (idx > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (idx > 0) { 1219 1009 2228
659 1219 IV parent = (idx - 1) >> 1;
660 1219 ▶ if (val > data[parent]) {
Branch: 1 branch
BranchTrueFalseTotal
if (val > data[parent]) { 1209 10 1219
661 1209 data[idx] = data[parent];
662 1209 idx = parent;
663 } else {
664 10 break;
665 }
666 }
667 1019 data[idx] = val;
668 1019 }
669  
670 /* Sift down for NV min-heap */
671 1594 static void nv_sift_down_min(NumericHeap *h, IV idx) {
672 1594 NV *data = h->data;
673 1594 IV size = h->size;
674 1594 NV val = data[idx];
675 1594 IV half = size >> 1;
676  
677 9871 ▶ while (idx < half) {
Branch: 1 branch
BranchTrueFalseTotal
while (idx < half) { 8619 1252 9871
678 8619 IV left = (idx << 1) + 1;
679 8619 IV right = left + 1;
680 8619 IV best = left;
681 8619 NV best_nv = data[left];
682  
683 8619 ▶ if (right < size && data[right] < best_nv) {
Branch: 2 branches
BranchTrueFalseTotal
if (right < size && data[right] < best_nv) { 8564 55 8619
if (right < size && data[right] < best_nv) { 4130 4434 8564
684 4130 best = right;
685 4130 best_nv = data[right];
686 }
687  
688 8619 ▶ if (best_nv < val) {
Branch: 1 branch
BranchTrueFalseTotal
if (best_nv < val) { 8277 342 8619
689 8277 data[idx] = data[best];
690 8277 idx = best;
691 } else {
692 342 break;
693 }
694 }
695 1594 data[idx] = val;
696 1594 }
697  
698 /* Sift down for NV max-heap */
699 809 static void nv_sift_down_max(NumericHeap *h, IV idx) {
700 809 NV *data = h->data;
701 809 IV size = h->size;
702 809 NV val = data[idx];
703 809 IV half = size >> 1;
704  
705 1216 ▶ while (idx < half) {
Branch: 1 branch
BranchTrueFalseTotal
while (idx < half) { 809 407 1216
706 809 IV left = (idx << 1) + 1;
707 809 IV right = left + 1;
708 809 IV best = left;
709 809 NV best_nv = data[left];
710  
711 809 ▶ if (right < size && data[right] > best_nv) {
Branch: 2 branches
BranchTrueFalseTotal
if (right < size && data[right] > best_nv) { 407 402 809
if (right < size && data[right] > best_nv) { 3 404 407
712 3 best = right;
713 3 best_nv = data[right];
714 }
715  
716 809 ▶ if (best_nv > val) {
Branch: 1 branch
BranchTrueFalseTotal
if (best_nv > val) { 407 402 809
717 407 data[idx] = data[best];
718 407 idx = best;
719 } else {
720 402 break;
721 }
722 }
723 809 data[idx] = val;
724 809 }
725  
726 239 static int numeric_heap_free(pTHX_ SV *sv, MAGIC *mg) {
727 239 NumericHeap *h = (NumericHeap*)mg->mg_ptr;
728 PERL_UNUSED_ARG(sv);
729 239 ▶ if (h->data) Safefree(h->data);
Branch: 1 branch
BranchTrueFalseTotal
if (h->data) Safefree(h->data); 239 0 239
730 239 Safefree(h);
731 239 return 0;
732 }
733  
734 /* heap::new_nv($type) - create numeric heap */
735 239 XS_EXTERNAL(XS_heap_new_nv) {
736 239 dXSARGS;
737 NumericHeap *h;
738 SV *obj_sv, *rv;
739 HV *stash;
740 239 HeapType type = HEAP_MIN;
741 239 int arg_offset = 0;
742  
743 239 ▶ if (items >= 1 && SvPOK(ST(0))) {
Branch: 2 branches
BranchTrueFalseTotal
if (items >= 1 && SvPOK(ST(0))) { 239 0 239
if (items >= 1 && SvPOK(ST(0))) { 239 0 239
744 STRLEN len;
745 239 const char *str = SvPV(ST(0), len);
746 239 ▶ if (len == 8 && strEQ(str, "Heap::PQ")) {
Branch: 2 branches
BranchTrueFalseTotal
if (len == 8 && strEQ(str, "Heap::PQ")) { 0 239 239
if (len == 8 && strEQ(str, "Heap::PQ")) { 0 0 0
747 0 arg_offset = 1;
748 239 ▶ } else if (len == 3 && (strEQ(str, "min") || strEQ(str, "max"))) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (len == 3 && (strEQ(str, "min") || strEQ(str, "max"))) { 239 0 239
} else if (len == 3 && (strEQ(str, "min") || strEQ(str, "max"))) { 205 34 239
} else if (len == 3 && (strEQ(str, "min") || strEQ(str, "max"))) { 205 0 205
749 239 arg_offset = 0;
750 } else {
751 0 arg_offset = 1;
752 }
753 }
754  
755 239 ▶ if (items > arg_offset) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > arg_offset) { 239 0 239
756 STRLEN len;
757 239 const char *type_str = SvPV(ST(arg_offset), len);
758 239 ▶ if (len == 3 && strEQ(type_str, "max")) {
Branch: 2 branches
BranchTrueFalseTotal
if (len == 3 && strEQ(type_str, "max")) { 239 0 239
if (len == 3 && strEQ(type_str, "max")) { 205 34 239
759 205 type = HEAP_MAX;
760 }
761 }
762  
763 239 Newxz(h, 1, NumericHeap);
764 239 h->type = type;
765 239 h->size = 0;
766 239 h->capacity = 16;
767 239 Newx(h->data, 16, NV);
768  
769 239 obj_sv = newSV(0);
770 239 sv_magicext(obj_sv, NULL, PERL_MAGIC_ext, &numeric_heap_vtbl, (char*)h, 0);
771  
772 239 rv = newRV_noinc(obj_sv);
773 239 stash = gv_stashpvn("Heap::PQ::nv", 12, GV_ADD);
774 239 sv_bless(rv, stash);
775  
776 239 ST(0) = sv_2mortal(rv);
777 239 XSRETURN(1);
778 }
779  
780 /* $nv_heap->push($value) */
781 4136 XS_EXTERNAL(XS_nv_push) {
782 4136 dXSARGS;
783 NumericHeap *h;
784 NV val;
785  
786 4136 ▶ if (items != 2) croak("Usage: $heap->push($value)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 2) croak("Usage: $heap->push($value)"); 0 4136 4136
787  
788 4136 h = NV_HEAP_FAST(ST(0));
789 4136 val = SvNV(ST(1));
790  
791 4136 nv_ensure_capacity(h, h->size + 1);
792 4136 h->data[h->size] = val;
793 4136 h->size++;
794  
795 4136 ▶ if (h->type == HEAP_MIN) {
Branch: 1 branch
BranchTrueFalseTotal
if (h->type == HEAP_MIN) { 4117 19 4136
796 4117 nv_sift_up_min(h, h->size - 1);
797 } else {
798 19 nv_sift_up_max(h, h->size - 1);
799 }
800  
801 4136 ST(0) = ST(0);
802 4136 XSRETURN(1);
803 }
804  
805 /* $nv_heap->push_all(@values) - with Floyd's heapify */
806 204 XS_EXTERNAL(XS_nv_push_all) {
807 204 dXSARGS;
808 NumericHeap *h;
809 int i;
810 IV start_size;
811  
812 204 ▶ if (items < 1) croak("Usage: $heap->push_all(@values)");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) croak("Usage: $heap->push_all(@values)"); 0 204 204
813  
814 204 h = NV_HEAP_FAST(ST(0));
815 204 start_size = h->size;
816  
817 204 nv_ensure_capacity(h, h->size + items - 1);
818  
819 /* Add all values first */
820 2221 ▶ for (i = 1; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) { 2017 204 2221
821 2017 h->data[h->size++] = SvNV(ST(i));
822 }
823  
824 /* Floyd's heapify on the new portion if significant */
825 204 ▶ if (items - 1 > 10) {
Branch: 1 branch
BranchTrueFalseTotal
if (items - 1 > 10) { 1 203 204
826 /* Full Floyd's heapify */
827 IV j;
828 501 ▶ for (j = (h->size >> 1) - 1; j >= 0; j--) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = (h->size >> 1) - 1; j >= 0; j--) { 500 1 501
829 500 ▶ if (h->type == HEAP_MIN) {
Branch: 1 branch
BranchTrueFalseTotal
if (h->type == HEAP_MIN) { 500 0 500
830 500 nv_sift_down_min(h, j);
831 } else {
832 0 nv_sift_down_max(h, j);
833 }
834 }
835 } else {
836 /* Just sift up each new element */
837 1220 ▶ for (i = start_size; i < h->size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = start_size; i < h->size; i++) { 1017 203 1220
838 1017 ▶ if (h->type == HEAP_MIN) {
Branch: 1 branch
BranchTrueFalseTotal
if (h->type == HEAP_MIN) { 17 1000 1017
839 17 nv_sift_up_min(h, i);
840 } else {
841 1000 nv_sift_up_max(h, i);
842 }
843 }
844 }
845  
846 204 ST(0) = ST(0);
847 204 XSRETURN(1);
848 }
849  
850 /* $nv_heap->pop() */
851 3095 XS_EXTERNAL(XS_nv_pop) {
852 3095 dXSARGS;
853 NumericHeap *h;
854 NV result;
855  
856 3095 ▶ if (items != 1) croak("Usage: $heap->pop()");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: $heap->pop()"); 0 3095 3095
857  
858 3095 h = NV_HEAP_FAST(ST(0));
859  
860 3095 ▶ if (h->size == 0) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (h->size == 0) XSRETURN_UNDEF; 2 3093 3095
861  
862 3093 result = h->data[0];
863 3093 h->size--;
864  
865 3093 ▶ if (h->size > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (h->size > 0) { 1873 1220 3093
866 1873 h->data[0] = h->data[h->size];
867 1873 ▶ if (h->type == HEAP_MIN) {
Branch: 1 branch
BranchTrueFalseTotal
if (h->type == HEAP_MIN) { 1068 805 1873
868 1068 nv_sift_down_min(h, 0);
869 } else {
870 805 nv_sift_down_max(h, 0);
871 }
872 }
873  
874 3093 ST(0) = sv_2mortal(newSVnv(result));
875 3093 XSRETURN(1);
876 }
877  
878 /* $nv_heap->peek() */
879 1011 XS_EXTERNAL(XS_nv_peek) {
880 1011 dXSARGS;
881 NumericHeap *h;
882  
883 1011 ▶ if (items != 1) croak("Usage: $heap->peek()");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: $heap->peek()"); 0 1011 1011
884  
885 1011 h = NV_HEAP_FAST(ST(0));
886  
887 1011 ▶ if (h->size == 0) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (h->size == 0) XSRETURN_UNDEF; 2 1009 1011
888  
889 1009 ST(0) = sv_2mortal(newSVnv(h->data[0]));
890 1009 XSRETURN(1);
891 }
892  
893 /* $nv_heap->size() */
894 1015 XS_EXTERNAL(XS_nv_size) {
895 1015 dXSARGS;
896 NumericHeap *h;
897  
898 1015 ▶ if (items != 1) croak("Usage: $heap->size()");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: $heap->size()"); 0 1015 1015
899  
900 1015 h = NV_HEAP_FAST(ST(0));
901 1015 XSRETURN_IV(h->size);
902 }
903  
904 /* $nv_heap->is_empty() */
905 2232 XS_EXTERNAL(XS_nv_is_empty) {
906 2232 dXSARGS;
907 NumericHeap *h;
908  
909 2232 ▶ if (items != 1) croak("Usage: $heap->is_empty()");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: $heap->is_empty()"); 0 2232 2232
910  
911 2232 h = NV_HEAP_FAST(ST(0));
912  
913 2232 ▶ if (h->size == 0) XSRETURN_YES;
Branch: 1 branch
BranchTrueFalseTotal
if (h->size == 0) XSRETURN_YES; 210 2022 2232
914 2022 XSRETURN_NO;
915 }
916  
917 /* $nv_heap->clear() */
918 201 XS_EXTERNAL(XS_nv_clear) {
919 201 dXSARGS;
920 NumericHeap *h;
921  
922 201 ▶ if (items != 1) croak("Usage: $heap->clear()");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: $heap->clear()"); 0 201 201
923  
924 201 h = NV_HEAP_FAST(ST(0));
925 201 h->size = 0;
926  
927 201 XSRETURN_EMPTY;
928 }
929  
930 /* $nv_heap->peek_n($n) - return top N elements in sorted order without removing */
931 6 XS_EXTERNAL(XS_nv_peek_n) {
932 6 dXSARGS;
933 NumericHeap *h;
934 IV n, i, count;
935 NV *saved;
936  
937 6 ▶ if (items != 2) croak("Usage: $heap->peek_n($n)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 2) croak("Usage: $heap->peek_n($n)"); 0 6 6
938  
939 6 h = NV_HEAP_FAST(ST(0));
940 6 n = SvIV(ST(1));
941  
942 6 ▶ if (n <= 0 || h->size == 0) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (n <= 0 || h->size == 0) XSRETURN_EMPTY; 6 0 6
if (n <= 0 || h->size == 0) XSRETURN_EMPTY; 1 5 6
943 5 ▶ if (n > h->size) n = h->size;
Branch: 1 branch
BranchTrueFalseTotal
if (n > h->size) n = h->size; 1 4 5
944  
945 /* Save the original state */
946 5 ▶ Newx(saved, h->size, NV);
Branch: 1 branch
BranchTrueFalseTotal
Newx(saved, h->size, NV); 0 5 5
947 5 ▶ Copy(h->data, saved, h->size, NV);
Branch: 1 branch
BranchTrueFalseTotal
Copy(h->data, saved, h->size, NV); 0 5 5
948 5 IV saved_size = h->size;
949  
950 /* Pop n elements */
951 5 ▶ EXTEND(SP, n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n); 5 0 5
EXTEND(SP, n); 0 5 5
952 24 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 19 5 24
953 19 NV val = h->data[0];
954 19 h->size--;
955 19 ▶ if (h->size > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (h->size > 0) { 18 1 19
956 18 h->data[0] = h->data[h->size];
957 18 ▶ if (h->type == HEAP_MIN)
Branch: 1 branch
BranchTrueFalseTotal
if (h->type == HEAP_MIN) 16 2 18
958 16 nv_sift_down_min(h, 0);
959 else
960 2 nv_sift_down_max(h, 0);
961 }
962 19 ST(i) = sv_2mortal(newSVnv(val));
963 }
964 5 count = n;
965  
966 /* Restore original state */
967 5 ▶ Copy(saved, h->data, saved_size, NV);
Branch: 1 branch
BranchTrueFalseTotal
Copy(saved, h->data, saved_size, NV); 0 5 5
968 5 h->size = saved_size;
969 5 Safefree(saved);
970  
971 5 XSRETURN(count);
972 }
973  
974 /* $nv_heap->search(sub { ... }) - find NV elements matching condition */
975 0 XS_EXTERNAL(XS_nv_search) {
976 0 dXSARGS;
977 NumericHeap *h;
978 0 IV i, found = 0;
979 SV *callback;
980 NV *results;
981  
982 0 ▶ if (items != 2) croak("Usage: $heap->search(sub { ... })");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 2) croak("Usage: $heap->search(sub { ... })"); 0 0 0
983  
984 0 h = NV_HEAP_FAST(ST(0));
985 0 callback = ST(1);
986  
987 0 ▶ if (!SvROK(callback) || SvTYPE(SvRV(callback)) != SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(callback) || SvTYPE(SvRV(callback)) != SVt_PVCV) 0 0 0
if (!SvROK(callback) || SvTYPE(SvRV(callback)) != SVt_PVCV) 0 0 0
988 0 croak("search requires a code reference");
989  
990 0 ▶ if (h->size == 0) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (h->size == 0) XSRETURN_EMPTY; 0 0 0
991  
992 0 ▶ Newx(results, h->size, NV);
Branch: 1 branch
BranchTrueFalseTotal
Newx(results, h->size, NV); 0 0 0
993  
994 0 ▶ for (i = 0; i < h->size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < h->size; i++) { 0 0 0
995 0 dSP;
996 0 SV *elem = sv_2mortal(newSVnv(h->data[i]));
997 IV result;
998 int count;
999  
1000 0 ENTER; SAVETMPS;
1001 0 SAVE_DEFSV;
1002 0 DEFSV_set(elem);
1003  
1004 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
1005 0 ▶ XPUSHs(elem);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(elem); 0 0 0
1006 0 PUTBACK;
1007  
1008 0 count = call_sv(callback, G_SCALAR);
1009 0 SPAGAIN;
1010 0 ▶ result = count > 0 ? SvTRUE(TOPs) : 0;
Branch: 1 branch
BranchTrueFalseTotal
result = count > 0 ? SvTRUE(TOPs) : 0; 0 0 0
1011 0 ▶ if (count > 0) POPs;
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) POPs; 0 0 0
1012 0 PUTBACK;
1013 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
1014  
1015 0 ▶ if (result) {
Branch: 1 branch
BranchTrueFalseTotal
if (result) { 0 0 0
1016 0 results[found++] = h->data[i];
1017 }
1018 }
1019  
1020 0 ▶ EXTEND(SP, found);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, found); 0 0 0
EXTEND(SP, found); 0 0 0
1021 0 ▶ for (i = 0; i < found; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < found; i++) { 0 0 0
1022 0 ST(i) = sv_2mortal(newSVnv(results[i]));
1023 }
1024 0 Safefree(results);
1025 0 XSRETURN(found);
1026 }
1027  
1028 /* $nv_heap->delete(sub { ... }) - remove matching NV elements, returns count */
1029 0 XS_EXTERNAL(XS_nv_delete) {
1030 0 dXSARGS;
1031 NumericHeap *h;
1032 0 IV i, write_pos = 0, deleted = 0;
1033 SV *callback;
1034  
1035 0 ▶ if (items != 2) croak("Usage: $heap->delete(sub { ... })");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 2) croak("Usage: $heap->delete(sub { ... })"); 0 0 0
1036  
1037 0 h = NV_HEAP_FAST(ST(0));
1038 0 callback = ST(1);
1039  
1040 0 ▶ if (!SvROK(callback) || SvTYPE(SvRV(callback)) != SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(callback) || SvTYPE(SvRV(callback)) != SVt_PVCV) 0 0 0
if (!SvROK(callback) || SvTYPE(SvRV(callback)) != SVt_PVCV) 0 0 0
1041 0 croak("delete requires a code reference");
1042  
1043 0 ▶ for (i = 0; i < h->size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < h->size; i++) { 0 0 0
1044 0 dSP;
1045 0 SV *elem = sv_2mortal(newSVnv(h->data[i]));
1046 IV result;
1047 int count;
1048  
1049 0 ENTER; SAVETMPS;
1050 0 SAVE_DEFSV;
1051 0 DEFSV_set(elem);
1052  
1053 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
1054 0 ▶ XPUSHs(elem);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(elem); 0 0 0
1055 0 PUTBACK;
1056  
1057 0 count = call_sv(callback, G_SCALAR);
1058 0 SPAGAIN;
1059 0 ▶ result = count > 0 ? SvTRUE(TOPs) : 0;
Branch: 1 branch
BranchTrueFalseTotal
result = count > 0 ? SvTRUE(TOPs) : 0; 0 0 0
1060 0 ▶ if (count > 0) POPs;
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) POPs; 0 0 0
1061 0 PUTBACK;
1062 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
1063  
1064 0 ▶ if (result) {
Branch: 1 branch
BranchTrueFalseTotal
if (result) { 0 0 0
1065 0 deleted++;
1066 } else {
1067 0 ▶ if (write_pos != i) {
Branch: 1 branch
BranchTrueFalseTotal
if (write_pos != i) { 0 0 0
1068 0 h->data[write_pos] = h->data[i];
1069 }
1070 0 write_pos++;
1071 }
1072 }
1073  
1074 0 h->size = write_pos;
1075  
1076 /* Rebuild heap with Floyd's heapify */
1077 0 ▶ if (deleted > 0 && h->size > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (deleted > 0 && h->size > 1) { 0 0 0
if (deleted > 0 && h->size > 1) { 0 0 0
1078 IV j;
1079 0 ▶ for (j = (h->size >> 1) - 1; j >= 0; j--) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = (h->size >> 1) - 1; j >= 0; j--) { 0 0 0
1080 0 ▶ if (h->type == HEAP_MIN)
Branch: 1 branch
BranchTrueFalseTotal
if (h->type == HEAP_MIN) 0 0 0
1081 0 nv_sift_down_min(h, j);
1082 else
1083 0 nv_sift_down_max(h, j);
1084 }
1085 }
1086  
1087 0 XSRETURN_IV(deleted);
1088 }
1089  
1090 /* ============================================
1091 PART 3: STANDARD HEAP (original OO API)
1092 ============================================ */
1093  
1094 4591 PERL_STATIC_INLINE void heap_ensure_capacity(Heap *h, IV needed) {
1095 4591 ▶ if (needed > h->capacity) {
Branch: 1 branch
BranchTrueFalseTotal
if (needed > h->capacity) { 6 4585 4591
1096 6 ▶ IV new_cap = h->capacity ? h->capacity * 2 : 16;
Branch: 1 branch
BranchTrueFalseTotal
IV new_cap = h->capacity ? h->capacity * 2 : 16; 6 0 6
1097 6 ▶ while (new_cap < needed) new_cap *= 2;
Branch: 1 branch
BranchTrueFalseTotal
while (new_cap < needed) new_cap *= 2; 0 6 6
1098 6 ▶ Renew(h->data, new_cap, SV*);
Branch: 1 branch
BranchTrueFalseTotal
Renew(h->data, new_cap, SV*); 0 6 6
1099 6 ▶ Renew(h->priorities, new_cap, NV);
Branch: 1 branch
BranchTrueFalseTotal
Renew(h->priorities, new_cap, NV); 0 6 6
1100 6 h->capacity = new_cap;
1101 }
1102 4591 }
1103  
1104 /* Sift functions for standard heap */
1105 3509 static void heap_sift_up_min(pTHX_ Heap *h, IV idx) {
1106 3509 SV **data = h->data;
1107 3509 NV *prio = h->priorities;
1108 3509 SV *val = data[idx];
1109 3509 NV val_nv = prio[idx];
1110 PERL_UNUSED_CONTEXT;
1111  
1112 3699 ▶ while (idx > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (idx > 0) { 572 3127 3699
1113 572 IV parent = (idx - 1) >> 1;
1114 572 ▶ if (val_nv < prio[parent]) {
Branch: 1 branch
BranchTrueFalseTotal
if (val_nv < prio[parent]) { 190 382 572
1115 190 data[idx] = data[parent];
1116 190 prio[idx] = prio[parent];
1117 190 idx = parent;
1118 } else {
1119 382 break;
1120 }
1121 }
1122 3509 data[idx] = val;
1123 3509 prio[idx] = val_nv;
1124 3509 }
1125  
1126 40 static void heap_sift_up_max(pTHX_ Heap *h, IV idx) {
1127 40 SV **data = h->data;
1128 40 NV *prio = h->priorities;
1129 40 SV *val = data[idx];
1130 40 NV val_nv = prio[idx];
1131 PERL_UNUSED_CONTEXT;
1132  
1133 55 ▶ while (idx > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (idx > 0) { 38 17 55
1134 38 IV parent = (idx - 1) >> 1;
1135 38 ▶ if (val_nv > prio[parent]) {
Branch: 1 branch
BranchTrueFalseTotal
if (val_nv > prio[parent]) { 15 23 38
1136 15 data[idx] = data[parent];
1137 15 prio[idx] = prio[parent];
1138 15 idx = parent;
1139 } else {
1140 23 break;
1141 }
1142 }
1143 40 data[idx] = val;
1144 40 prio[idx] = val_nv;
1145 40 }
1146  
1147 332 static void heap_sift_down_min(pTHX_ Heap *h, IV idx) {
1148 332 SV **data = h->data;
1149 332 NV *prio = h->priorities;
1150 332 IV size = h->size;
1151 332 SV *val = data[idx];
1152 332 NV val_nv = prio[idx];
1153 332 IV half = size >> 1;
1154 PERL_UNUSED_CONTEXT;
1155  
1156 1258 ▶ while (idx < half) {
Branch: 1 branch
BranchTrueFalseTotal
while (idx < half) { 989 269 1258
1157 989 IV left = (idx << 1) + 1;
1158 989 IV right = left + 1;
1159 989 IV best = left;
1160 989 NV best_nv = prio[left];
1161  
1162 989 ▶ if (right < size) {
Branch: 1 branch
BranchTrueFalseTotal
if (right < size) { 916 73 989
1163 916 NV right_nv = prio[right];
1164 916 ▶ if (right_nv < best_nv) {
Branch: 1 branch
BranchTrueFalseTotal
if (right_nv < best_nv) { 414 502 916
1165 414 best = right;
1166 414 best_nv = right_nv;
1167 }
1168 }
1169  
1170 989 ▶ if (best_nv < val_nv) {
Branch: 1 branch
BranchTrueFalseTotal
if (best_nv < val_nv) { 926 63 989
1171 926 data[idx] = data[best];
1172 926 prio[idx] = prio[best];
1173 926 idx = best;
1174 } else {
1175 63 break;
1176 }
1177 }
1178 332 data[idx] = val;
1179 332 prio[idx] = val_nv;
1180 332 }
1181  
1182 23 static void heap_sift_down_max(pTHX_ Heap *h, IV idx) {
1183 23 SV **data = h->data;
1184 23 NV *prio = h->priorities;
1185 23 IV size = h->size;
1186 23 SV *val = data[idx];
1187 23 NV val_nv = prio[idx];
1188 23 IV half = size >> 1;
1189 PERL_UNUSED_CONTEXT;
1190  
1191 39 ▶ while (idx < half) {
Branch: 1 branch
BranchTrueFalseTotal
while (idx < half) { 21 18 39
1192 21 IV left = (idx << 1) + 1;
1193 21 IV right = left + 1;
1194 21 IV best = left;
1195 21 NV best_nv = prio[left];
1196  
1197 21 ▶ if (right < size) {
Branch: 1 branch
BranchTrueFalseTotal
if (right < size) { 13 8 21
1198 13 NV right_nv = prio[right];
1199 13 ▶ if (right_nv > best_nv) {
Branch: 1 branch
BranchTrueFalseTotal
if (right_nv > best_nv) { 8 5 13
1200 8 best = right;
1201 8 best_nv = right_nv;
1202 }
1203 }
1204  
1205 21 ▶ if (best_nv > val_nv) {
Branch: 1 branch
BranchTrueFalseTotal
if (best_nv > val_nv) { 16 5 21
1206 16 data[idx] = data[best];
1207 16 prio[idx] = prio[best];
1208 16 idx = best;
1209 } else {
1210 5 break;
1211 }
1212 }
1213 23 data[idx] = val;
1214 23 prio[idx] = val_nv;
1215 23 }
1216  
1217 /* Key-path sift operations — pure C hash traversal, no Perl callbacks */
1218  
1219 127 static NV heap_extract_key_nv(pTHX_ SV *sv, char **path, STRLEN *lens, IV depth) {
1220 IV i;
1221 127 SV *cur = sv;
1222 263 ▶ for (i = 0; i < depth; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < depth; i++) { 136 127 263
1223 HV *hv;
1224 SV **val;
1225 136 ▶ if (!SvROK(cur)) croak("Key path: expected hash reference at depth %d", (int)i);
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(cur)) croak("Key path: expected hash reference at depth %d", (int)i); 0 136 136
1226 136 hv = (HV*)SvRV(cur);
1227 136 ▶ if (SvTYPE((SV*)hv) != SVt_PVHV) croak("Key path: not a hash at depth %d", (int)i);
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE((SV*)hv) != SVt_PVHV) croak("Key path: not a hash at depth %d", (int)i); 0 136 136
1228 136 val = hv_fetch(hv, path[i], lens[i], 0);
1229 136 ▶ if (!val || !*val) croak("Key path: key '%s' not found at depth %d", path[i], (int)i);
Branch: 2 branches
BranchTrueFalseTotal
if (!val || !*val) croak("Key path: key '%s' not found at depth %d", path[i], (int)i); 136 0 136
if (!val || !*val) croak("Key path: key '%s' not found at depth %d", path[i], (int)i); 0 136 136
1230 136 cur = *val;
1231 }
1232 127 return SvNV(cur);
1233 }
1234  
1235 0 static void heap_sift_up_keypath_min(pTHX_ Heap *h, IV idx) {
1236 0 SV **data = h->data;
1237 0 char **path = h->key_path;
1238 0 STRLEN *lens = h->key_lens;
1239 0 IV depth = h->key_depth;
1240 0 SV *val = data[idx];
1241 0 NV val_nv = heap_extract_key_nv(aTHX_ val, path, lens, depth);
1242  
1243 0 ▶ while (idx > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (idx > 0) { 0 0 0
1244 0 IV parent = (idx - 1) >> 1;
1245 0 NV parent_nv = heap_extract_key_nv(aTHX_ data[parent], path, lens, depth);
1246 0 ▶ if (val_nv < parent_nv) {
Branch: 1 branch
BranchTrueFalseTotal
if (val_nv < parent_nv) { 0 0 0
1247 0 data[idx] = data[parent];
1248 0 idx = parent;
1249 } else {
1250 0 break;
1251 }
1252 }
1253 0 data[idx] = val;
1254 0 }
1255  
1256 0 static void heap_sift_up_keypath_max(pTHX_ Heap *h, IV idx) {
1257 0 SV **data = h->data;
1258 0 char **path = h->key_path;
1259 0 STRLEN *lens = h->key_lens;
1260 0 IV depth = h->key_depth;
1261 0 SV *val = data[idx];
1262 0 NV val_nv = heap_extract_key_nv(aTHX_ val, path, lens, depth);
1263  
1264 0 ▶ while (idx > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (idx > 0) { 0 0 0
1265 0 IV parent = (idx - 1) >> 1;
1266 0 NV parent_nv = heap_extract_key_nv(aTHX_ data[parent], path, lens, depth);
1267 0 ▶ if (val_nv > parent_nv) {
Branch: 1 branch
BranchTrueFalseTotal
if (val_nv > parent_nv) { 0 0 0
1268 0 data[idx] = data[parent];
1269 0 idx = parent;
1270 } else {
1271 0 break;
1272 }
1273 }
1274 0 data[idx] = val;
1275 0 }
1276  
1277 0 static void heap_sift_down_keypath_min(pTHX_ Heap *h, IV idx) {
1278 0 SV **data = h->data;
1279 0 IV size = h->size;
1280 0 char **path = h->key_path;
1281 0 STRLEN *lens = h->key_lens;
1282 0 IV depth = h->key_depth;
1283 0 SV *val = data[idx];
1284 0 NV val_nv = heap_extract_key_nv(aTHX_ val, path, lens, depth);
1285 0 IV half = size >> 1;
1286  
1287 0 ▶ while (idx < half) {
Branch: 1 branch
BranchTrueFalseTotal
while (idx < half) { 0 0 0
1288 0 IV left = (idx << 1) + 1;
1289 0 IV right = left + 1;
1290 0 IV best = left;
1291 0 NV best_nv = heap_extract_key_nv(aTHX_ data[left], path, lens, depth);
1292  
1293 0 ▶ if (right < size) {
Branch: 1 branch
BranchTrueFalseTotal
if (right < size) { 0 0 0
1294 0 NV right_nv = heap_extract_key_nv(aTHX_ data[right], path, lens, depth);
1295 0 ▶ if (right_nv < best_nv) {
Branch: 1 branch
BranchTrueFalseTotal
if (right_nv < best_nv) { 0 0 0
1296 0 best = right;
1297 0 best_nv = right_nv;
1298 }
1299 }
1300  
1301 0 ▶ if (best_nv < val_nv) {
Branch: 1 branch
BranchTrueFalseTotal
if (best_nv < val_nv) { 0 0 0
1302 0 data[idx] = data[best];
1303 0 idx = best;
1304 } else {
1305 0 break;
1306 }
1307 }
1308 0 data[idx] = val;
1309 0 }
1310  
1311 0 static void heap_sift_down_keypath_max(pTHX_ Heap *h, IV idx) {
1312 0 SV **data = h->data;
1313 0 IV size = h->size;
1314 0 char **path = h->key_path;
1315 0 STRLEN *lens = h->key_lens;
1316 0 IV depth = h->key_depth;
1317 0 SV *val = data[idx];
1318 0 NV val_nv = heap_extract_key_nv(aTHX_ val, path, lens, depth);
1319 0 IV half = size >> 1;
1320  
1321 0 ▶ while (idx < half) {
Branch: 1 branch
BranchTrueFalseTotal
while (idx < half) { 0 0 0
1322 0 IV left = (idx << 1) + 1;
1323 0 IV right = left + 1;
1324 0 IV best = left;
1325 0 NV best_nv = heap_extract_key_nv(aTHX_ data[left], path, lens, depth);
1326  
1327 0 ▶ if (right < size) {
Branch: 1 branch
BranchTrueFalseTotal
if (right < size) { 0 0 0
1328 0 NV right_nv = heap_extract_key_nv(aTHX_ data[right], path, lens, depth);
1329 0 ▶ if (right_nv > best_nv) {
Branch: 1 branch
BranchTrueFalseTotal
if (right_nv > best_nv) { 0 0 0
1330 0 best = right;
1331 0 best_nv = right_nv;
1332 }
1333 }
1334  
1335 0 ▶ if (best_nv > val_nv) {
Branch: 1 branch
BranchTrueFalseTotal
if (best_nv > val_nv) { 0 0 0
1336 0 data[idx] = data[best];
1337 0 idx = best;
1338 } else {
1339 0 break;
1340 }
1341 }
1342 0 data[idx] = val;
1343 0 }
1344  
1345 /* Custom comparator sift operations using MULTICALL for minimal per-call overhead */
1346  
1347 /* Inline compare helper - must be called within a MULTICALL block.
1348 Uses SvIVX (no type check) since <=> and cmp always return an integer. */
1349 #define HEAP_CMP_CUSTOM(h, sv_a, sv_b, result_var) \
1350 STMT_START { \
1351 GvSV((h)->gv_a) = (sv_a); \
1352 GvSV((h)->gv_b) = (sv_b); \
1353 MULTICALL; \
1354 (result_var) = SvIVX(*PL_stack_sp); \
1355 } STMT_END
1356  
1357 1063 static void heap_sift_up_custom(pTHX_ Heap *h, IV idx) {
1358 1063 dSP;
1359 IV cmp_result;
1360  
1361 1063 ▶ if (idx <= 0) return;
Branch: 1 branch
BranchTrueFalseTotal
if (idx <= 0) return; 119 944 1063
1362  
1363 #if HEAP_USE_MULTICALL
1364 {
1365 dMULTICALL;
1366 944 I32 gimme = G_SCALAR;
1367 944 CV *cv = (CV*)SvRV(h->comparator);
1368  
1369 assert(cv != NULL);
1370 944 ▶ PUSH_MULTICALL(cv);
Branch: 1 branch
BranchTrueFalseTotal
PUSH_MULTICALL(cv); 0 944 944
1371  
1372 964 ▶ while (idx > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (idx > 0) { 947 17 964
1373 947 IV parent = (idx - 1) >> 1;
1374 947 HEAP_CMP_CUSTOM(h, h->data[idx], h->data[parent], cmp_result);
1375 947 ▶ if (h->type == HEAP_MIN ? cmp_result < 0 : cmp_result > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (h->type == HEAP_MIN ? cmp_result < 0 : cmp_result > 0) { 943 4 947
if (h->type == HEAP_MIN ? cmp_result < 0 : cmp_result > 0) { 20 927 947
1376 20 SV *tmp = h->data[idx];
1377 20 h->data[idx] = h->data[parent];
1378 20 h->data[parent] = tmp;
1379 20 idx = parent;
1380 } else {
1381 927 break;
1382 }
1383 }
1384  
1385 944 ▶ POP_MULTICALL;
Branch: 1 branch
BranchTrueFalseTotal
POP_MULTICALL; 944 0 944
1386 }
1387 #else
1388 while (idx > 0) {
1389 IV parent = (idx - 1) >> 1;
1390 HEAP_CMP_CALL(h, h->data[idx], h->data[parent], cmp_result);
1391 if (h->type == HEAP_MIN ? cmp_result < 0 : cmp_result > 0) {
1392 SV *tmp = h->data[idx];
1393 h->data[idx] = h->data[parent];
1394 h->data[parent] = tmp;
1395 idx = parent;
1396 } else {
1397 break;
1398 }
1399 }
1400 #endif
1401 }
1402  
1403 940 static void heap_sift_down_custom(pTHX_ Heap *h, IV idx) {
1404 940 dSP;
1405 IV cmp_result;
1406  
1407 940 ▶ if (h->size <= 1) return;
Branch: 1 branch
BranchTrueFalseTotal
if (h->size <= 1) return; 118 822 940
1408  
1409 #if HEAP_USE_MULTICALL
1410 {
1411 dMULTICALL;
1412 822 I32 gimme = G_SCALAR;
1413 822 CV *cv = (CV*)SvRV(h->comparator);
1414  
1415 assert(cv != NULL);
1416 822 ▶ PUSH_MULTICALL(cv);
Branch: 1 branch
BranchTrueFalseTotal
PUSH_MULTICALL(cv); 0 822 822
1417  
1418 1208 while (1) {
1419 2030 IV left = (idx << 1) + 1;
1420 2030 IV right = left + 1;
1421 2030 IV best = idx;
1422  
1423 2030 ▶ if (left < h->size) {
Branch: 1 branch
BranchTrueFalseTotal
if (left < h->size) { 1423 607 2030
1424 1423 HEAP_CMP_CUSTOM(h, h->data[left], h->data[best], cmp_result);
1425 1423 ▶ if (h->type == HEAP_MIN ? cmp_result < 0 : cmp_result > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (h->type == HEAP_MIN ? cmp_result < 0 : cmp_result > 0) { 1421 2 1423
if (h->type == HEAP_MIN ? cmp_result < 0 : cmp_result > 0) { 1207 216 1423
1426 1207 best = left;
1427 }
1428 }
1429 2030 ▶ if (right < h->size) {
Branch: 1 branch
BranchTrueFalseTotal
if (right < h->size) { 1205 825 2030
1430 1205 HEAP_CMP_CUSTOM(h, h->data[right], h->data[best], cmp_result);
1431 1205 ▶ if (h->type == HEAP_MIN ? cmp_result < 0 : cmp_result > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (h->type == HEAP_MIN ? cmp_result < 0 : cmp_result > 0) { 1205 0 1205
if (h->type == HEAP_MIN ? cmp_result < 0 : cmp_result > 0) { 303 902 1205
1432 303 best = right;
1433 }
1434 }
1435  
1436 2030 ▶ if (best != idx) {
Branch: 1 branch
BranchTrueFalseTotal
if (best != idx) { 1208 822 2030
1437 1208 SV *tmp = h->data[idx];
1438 1208 h->data[idx] = h->data[best];
1439 1208 h->data[best] = tmp;
1440 1208 idx = best;
1441 } else {
1442 822 break;
1443 }
1444 }
1445  
1446 822 ▶ POP_MULTICALL;
Branch: 1 branch
BranchTrueFalseTotal
POP_MULTICALL; 822 0 822
1447 }
1448 #else
1449 while (1) {
1450 IV left = (idx << 1) + 1;
1451 IV right = left + 1;
1452 IV best = idx;
1453  
1454 if (left < h->size) {
1455 HEAP_CMP_CALL(h, h->data[left], h->data[best], cmp_result);
1456 if (h->type == HEAP_MIN ? cmp_result < 0 : cmp_result > 0)
1457 best = left;
1458 }
1459 if (right < h->size) {
1460 HEAP_CMP_CALL(h, h->data[right], h->data[best], cmp_result);
1461 if (h->type == HEAP_MIN ? cmp_result < 0 : cmp_result > 0)
1462 best = right;
1463 }
1464  
1465 if (best != idx) {
1466 SV *tmp = h->data[idx];
1467 h->data[idx] = h->data[best];
1468 h->data[best] = tmp;
1469 idx = best;
1470 } else {
1471 break;
1472 }
1473 }
1474 #endif
1475 }
1476  
1477 209 static int heap_free(pTHX_ SV *sv, MAGIC *mg) {
1478 209 Heap *h = (Heap*)mg->mg_ptr;
1479 PERL_UNUSED_ARG(sv);
1480  
1481 209 ▶ if (!PL_dirty) {
Branch: 1 branch
BranchTrueFalseTotal
if (!PL_dirty) { 209 0 209
1482 IV i;
1483 392 ▶ for (i = 0; i < h->size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < h->size; i++) { 183 209 392
1484 183 ▶ if (h->data[i]) SvREFCNT_dec(h->data[i]);
Branch: 1 branch
BranchTrueFalseTotal
if (h->data[i]) SvREFCNT_dec(h->data[i]); 183 0 183
1485 }
1486 209 ▶ if (h->comparator) SvREFCNT_dec(h->comparator);
Branch: 1 branch
BranchTrueFalseTotal
if (h->comparator) SvREFCNT_dec(h->comparator); 119 90 209
1487 }
1488 209 ▶ if (h->data) Safefree(h->data);
Branch: 1 branch
BranchTrueFalseTotal
if (h->data) Safefree(h->data); 209 0 209
1489 209 ▶ if (h->priorities) Safefree(h->priorities);
Branch: 1 branch
BranchTrueFalseTotal
if (h->priorities) Safefree(h->priorities); 209 0 209
1490 209 ▶ if (h->key_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (h->key_path) { 10 199 209
1491 IV i;
1492 23 ▶ for (i = 0; i < h->key_depth; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < h->key_depth; i++) { 13 10 23
1493 13 Safefree(h->key_path[i]);
1494 }
1495 10 Safefree(h->key_path);
1496 10 Safefree(h->key_lens);
1497 }
1498 209 Safefree(h);
1499 209 return 0;
1500 }
1501  
1502 /* Function-style custom ops */
1503  
1504 /* Dedicated NV heap custom ops - no find_heap, no SV allocation */
1505 0 static OP* pp_nv_push(pTHX) {
1506 0 dSP;
1507 0 NV val = SvNV(TOPs);
1508 0 SV *heap_sv = TOPm1s;
1509 0 NumericHeap *nh = NV_HEAP_FAST(heap_sv);
1510  
1511 0 nv_ensure_capacity(nh, nh->size + 1);
1512 0 nh->data[nh->size] = val;
1513 0 nh->size++;
1514 0 ▶ if (nh->type == HEAP_MIN) {
Branch: 1 branch
BranchTrueFalseTotal
if (nh->type == HEAP_MIN) { 0 0 0
1515 0 nv_sift_up_min(nh, nh->size - 1);
1516 } else {
1517 0 nv_sift_up_max(nh, nh->size - 1);
1518 }
1519 0 SP--;
1520 0 SETs(heap_sv);
1521 0 RETURN;
1522 }
1523  
1524 2 static OP* pp_nv_pop(pTHX) {
1525 2 dSP;
1526 2 dTARGET;
1527 2 NumericHeap *nh = NV_HEAP_FAST(TOPs);
1528  
1529 2 ▶ if (nh->size == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nh->size == 0) { 0 2 2
1530 0 SETs(&PL_sv_undef);
1531 0 RETURN;
1532 }
1533 2 NV result = nh->data[0];
1534 2 nh->size--;
1535 2 ▶ if (nh->size > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nh->size > 0) { 2 0 2
1536 2 nh->data[0] = nh->data[nh->size];
1537 2 ▶ if (nh->type == HEAP_MIN) {
Branch: 1 branch
BranchTrueFalseTotal
if (nh->type == HEAP_MIN) { 2 0 2
1538 2 nv_sift_down_min(nh, 0);
1539 } else {
1540 0 nv_sift_down_max(nh, 0);
1541 }
1542 }
1543 2 ▶ SETn(result);
Branch: 1 branch
BranchTrueFalseTotal
SETn(result); 0 2 2
1544 2 RETURN;
1545 }
1546  
1547 0 static OP* pp_nv_peek(pTHX) {
1548 0 dSP;
1549 0 dTARGET;
1550 0 NumericHeap *nh = NV_HEAP_FAST(TOPs);
1551  
1552 0 ▶ if (nh->size == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nh->size == 0) { 0 0 0
1553 0 SETs(&PL_sv_undef);
1554 0 RETURN;
1555 }
1556 0 ▶ SETn(nh->data[0]);
Branch: 1 branch
BranchTrueFalseTotal
SETn(nh->data[0]); 0 0 0
1557 0 RETURN;
1558 }
1559  
1560 1 static OP* pp_nv_size(pTHX) {
1561 1 dSP;
1562 1 dTARGET;
1563 1 NumericHeap *nh = NV_HEAP_FAST(TOPs);
1564 1 ▶ SETi(nh->size);
Branch: 1 branch
BranchTrueFalseTotal
SETi(nh->size); 0 1 1
1565 1 RETURN;
1566 }
1567  
1568 0 static OP* pp_nv_is_empty(pTHX) {
1569 0 dSP;
1570 0 NumericHeap *nh = NV_HEAP_FAST(TOPs);
1571 0 ▶ if (nh->size == 0) { SETs(&PL_sv_yes); }
Branch: 1 branch
BranchTrueFalseTotal
if (nh->size == 0) { SETs(&PL_sv_yes); } 0 0 0
1572 0 else { SETs(&PL_sv_no); }
1573 0 RETURN;
1574 }
1575  
1576 0 static OP* pp_nv_clear(pTHX) {
1577 0 dSP;
1578 0 NumericHeap *nh = NV_HEAP_FAST(TOPs);
1579 0 nh->size = 0;
1580 0 SETs(&PL_sv_undef);
1581 0 RETURN;
1582 }
1583  
1584 3 static OP* pp_nv_peek_n(pTHX) {
1585 3 dSP;
1586 3 IV n = SvIV(TOPs);
1587 3 NumericHeap *nh = NV_HEAP_FAST(TOPm1s);
1588 IV i;
1589  
1590 3 SP -= 2;
1591  
1592 3 ▶ if (n <= 0 || nh->size == 0) RETURN;
Branch: 2 branches
BranchTrueFalseTotal
if (n <= 0 || nh->size == 0) RETURN; 3 0 3
if (n <= 0 || nh->size == 0) RETURN; 0 3 3
1593 3 ▶ if (n > nh->size) n = nh->size;
Branch: 1 branch
BranchTrueFalseTotal
if (n > nh->size) n = nh->size; 0 3 3
1594  
1595 {
1596 NV *saved;
1597 3 IV saved_size = nh->size;
1598 3 ▶ Newx(saved, saved_size, NV);
Branch: 1 branch
BranchTrueFalseTotal
Newx(saved, saved_size, NV); 0 3 3
1599 3 ▶ Copy(nh->data, saved, saved_size, NV);
Branch: 1 branch
BranchTrueFalseTotal
Copy(nh->data, saved, saved_size, NV); 0 3 3
1600  
1601 3 ▶ EXTEND(SP, n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n); 3 0 3
EXTEND(SP, n); 0 3 3
1602 11 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 8 3 11
1603 8 NV val = nh->data[0];
1604 8 nh->size--;
1605 8 ▶ if (nh->size > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nh->size > 0) { 8 0 8
1606 8 nh->data[0] = nh->data[nh->size];
1607 8 ▶ if (nh->type == HEAP_MIN) nv_sift_down_min(nh, 0);
Branch: 1 branch
BranchTrueFalseTotal
if (nh->type == HEAP_MIN) nv_sift_down_min(nh, 0); 6 2 8
1608 2 else nv_sift_down_max(nh, 0);
1609 }
1610 8 PUSHs(sv_2mortal(newSVnv(val)));
1611 }
1612  
1613 3 ▶ Copy(saved, nh->data, saved_size, NV);
Branch: 1 branch
BranchTrueFalseTotal
Copy(saved, nh->data, saved_size, NV); 0 3 3
1614 3 nh->size = saved_size;
1615 3 Safefree(saved);
1616 }
1617 3 RETURN;
1618 }
1619  
1620 4 static OP* pp_heap_func_peek_n(pTHX) {
1621 4 dSP;
1622 4 IV n = SvIV(TOPs);
1623 4 SV *heap_sv = TOPm1s;
1624 4 HeapLookup lookup = find_heap(aTHX_ heap_sv);
1625 IV i;
1626  
1627 4 SP -= 2;
1628  
1629 4 ▶ if (lookup.type == MAGIC_NUMERIC) {
Branch: 1 branch
BranchTrueFalseTotal
if (lookup.type == MAGIC_NUMERIC) { 1 3 4
1630 1 NumericHeap *nh = lookup.ptr.nheap;
1631 1 ▶ if (n <= 0 || nh->size == 0) RETURN;
Branch: 2 branches
BranchTrueFalseTotal
if (n <= 0 || nh->size == 0) RETURN; 1 0 1
if (n <= 0 || nh->size == 0) RETURN; 0 1 1
1632 1 ▶ if (n > nh->size) n = nh->size;
Branch: 1 branch
BranchTrueFalseTotal
if (n > nh->size) n = nh->size; 0 1 1
1633  
1634 {
1635 NV *saved;
1636 1 IV saved_size = nh->size;
1637 1 ▶ Newx(saved, saved_size, NV);
Branch: 1 branch
BranchTrueFalseTotal
Newx(saved, saved_size, NV); 0 1 1
1638 1 ▶ Copy(nh->data, saved, saved_size, NV);
Branch: 1 branch
BranchTrueFalseTotal
Copy(nh->data, saved, saved_size, NV); 0 1 1
1639  
1640 1 ▶ EXTEND(SP, n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n); 1 0 1
EXTEND(SP, n); 0 1 1
1641 3 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 2 1 3
1642 2 NV val = nh->data[0];
1643 2 nh->size--;
1644 2 ▶ if (nh->size > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nh->size > 0) { 2 0 2
1645 2 nh->data[0] = nh->data[nh->size];
1646 2 ▶ if (nh->type == HEAP_MIN) nv_sift_down_min(nh, 0);
Branch: 1 branch
BranchTrueFalseTotal
if (nh->type == HEAP_MIN) nv_sift_down_min(nh, 0); 2 0 2
1647 0 else nv_sift_down_max(nh, 0);
1648 }
1649 2 PUSHs(sv_2mortal(newSVnv(val)));
1650 }
1651  
1652 1 ▶ Copy(saved, nh->data, saved_size, NV);
Branch: 1 branch
BranchTrueFalseTotal
Copy(saved, nh->data, saved_size, NV); 0 1 1
1653 1 nh->size = saved_size;
1654 1 Safefree(saved);
1655 }
1656 1 RETURN;
1657 }
1658  
1659 3 ▶ if (lookup.type == MAGIC_HEAP) {
Branch: 1 branch
BranchTrueFalseTotal
if (lookup.type == MAGIC_HEAP) { 3 0 3
1660 3 Heap *h = lookup.ptr.heap;
1661 3 ▶ if (n <= 0 || h->size == 0) RETURN;
Branch: 2 branches
BranchTrueFalseTotal
if (n <= 0 || h->size == 0) RETURN; 3 0 3
if (n <= 0 || h->size == 0) RETURN; 1 2 3
1662 2 ▶ if (n > h->size) n = h->size;
Branch: 1 branch
BranchTrueFalseTotal
if (n > h->size) n = h->size; 0 2 2
1663  
1664 {
1665 SV **saved_data;
1666 NV *saved_pri;
1667 2 IV saved_size = h->size;
1668 2 ▶ Newx(saved_data, saved_size, SV*);
Branch: 1 branch
BranchTrueFalseTotal
Newx(saved_data, saved_size, SV*); 0 2 2
1669 2 ▶ Newx(saved_pri, saved_size, NV);
Branch: 1 branch
BranchTrueFalseTotal
Newx(saved_pri, saved_size, NV); 0 2 2
1670 2 ▶ Copy(h->data, saved_data, saved_size, SV*);
Branch: 1 branch
BranchTrueFalseTotal
Copy(h->data, saved_data, saved_size, SV*); 0 2 2
1671 2 ▶ Copy(h->priorities, saved_pri, saved_size, NV);
Branch: 1 branch
BranchTrueFalseTotal
Copy(h->priorities, saved_pri, saved_size, NV); 0 2 2
1672  
1673 2 ▶ EXTEND(SP, n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n); 2 0 2
EXTEND(SP, n); 0 2 2
1674 7 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 5 2 7
1675 5 PUSHs(h->data[0]);
1676 5 h->size--;
1677 5 ▶ if (h->size > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (h->size > 0) { 5 0 5
1678 5 h->data[0] = h->data[h->size];
1679 5 h->priorities[0] = h->priorities[h->size];
1680 5 h->sift_down(aTHX_ h, 0);
1681 }
1682 }
1683  
1684 2 ▶ Copy(saved_data, h->data, saved_size, SV*);
Branch: 1 branch
BranchTrueFalseTotal
Copy(saved_data, h->data, saved_size, SV*); 0 2 2
1685 2 ▶ Copy(saved_pri, h->priorities, saved_size, NV);
Branch: 1 branch
BranchTrueFalseTotal
Copy(saved_pri, h->priorities, saved_size, NV); 0 2 2
1686 2 h->size = saved_size;
1687 2 Safefree(saved_data);
1688 2 Safefree(saved_pri);
1689 }
1690 2 RETURN;
1691 }
1692  
1693 0 croak("Not a heap object");
1694 RETURN;
1695 }
1696  
1697 /* Dedicated NV search custom op */
1698 0 static OP* pp_nv_search(pTHX) {
1699 0 dSP;
1700 0 SV *callback = TOPs;
1701 0 SV *heap_sv = TOPm1s;
1702 0 NumericHeap *nh = NV_HEAP_FAST(heap_sv);
1703 0 IV i, found = 0;
1704 NV *results;
1705  
1706 0 SP -= 2;
1707 0 PUTBACK;
1708  
1709 0 ▶ if (!SvROK(callback) || SvTYPE(SvRV(callback)) != SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(callback) || SvTYPE(SvRV(callback)) != SVt_PVCV) 0 0 0
if (!SvROK(callback) || SvTYPE(SvRV(callback)) != SVt_PVCV) 0 0 0
1710 0 croak("search requires a code reference");
1711  
1712 0 ▶ if (nh->size == 0) RETURN;
Branch: 1 branch
BranchTrueFalseTotal
if (nh->size == 0) RETURN; 0 0 0
1713  
1714 0 ▶ Newx(results, nh->size, NV);
Branch: 1 branch
BranchTrueFalseTotal
Newx(results, nh->size, NV); 0 0 0
1715  
1716 0 ▶ for (i = 0; i < nh->size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nh->size; i++) { 0 0 0
1717 0 dSP;
1718 0 SV *elem = sv_2mortal(newSVnv(nh->data[i]));
1719 IV result;
1720 int count;
1721  
1722 0 ENTER; SAVETMPS;
1723 0 SAVE_DEFSV;
1724 0 DEFSV_set(elem);
1725  
1726 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
1727 0 ▶ XPUSHs(elem);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(elem); 0 0 0
1728 0 PUTBACK;
1729  
1730 0 count = call_sv(callback, G_SCALAR);
1731 0 SPAGAIN;
1732 0 ▶ result = count > 0 ? SvTRUE(TOPs) : 0;
Branch: 1 branch
BranchTrueFalseTotal
result = count > 0 ? SvTRUE(TOPs) : 0; 0 0 0
1733 0 ▶ if (count > 0) POPs;
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) POPs; 0 0 0
1734 0 PUTBACK;
1735 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
1736  
1737 0 ▶ if (result) {
Branch: 1 branch
BranchTrueFalseTotal
if (result) { 0 0 0
1738 0 results[found++] = nh->data[i];
1739 }
1740 }
1741  
1742 0 SPAGAIN;
1743 0 ▶ EXTEND(SP, found);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, found); 0 0 0
EXTEND(SP, found); 0 0 0
1744 0 ▶ for (i = 0; i < found; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < found; i++) { 0 0 0
1745 0 PUSHs(sv_2mortal(newSVnv(results[i])));
1746 }
1747 0 Safefree(results);
1748 0 RETURN;
1749 }
1750  
1751 /* Dedicated NV delete custom op */
1752 0 static OP* pp_nv_delete(pTHX) {
1753 0 dSP;
1754 0 SV *callback = TOPs;
1755 0 SV *heap_sv = TOPm1s;
1756 0 NumericHeap *nh = NV_HEAP_FAST(heap_sv);
1757 0 IV i, write_pos = 0, deleted = 0;
1758 0 dTARGET;
1759  
1760 0 SP -= 2;
1761 0 PUTBACK;
1762  
1763 0 ▶ if (!SvROK(callback) || SvTYPE(SvRV(callback)) != SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(callback) || SvTYPE(SvRV(callback)) != SVt_PVCV) 0 0 0
if (!SvROK(callback) || SvTYPE(SvRV(callback)) != SVt_PVCV) 0 0 0
1764 0 croak("delete requires a code reference");
1765  
1766 0 ▶ for (i = 0; i < nh->size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nh->size; i++) { 0 0 0
1767 0 dSP;
1768 0 SV *elem = sv_2mortal(newSVnv(nh->data[i]));
1769 IV result;
1770 int count;
1771  
1772 0 ENTER; SAVETMPS;
1773 0 SAVE_DEFSV;
1774 0 DEFSV_set(elem);
1775  
1776 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
1777 0 ▶ XPUSHs(elem);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(elem); 0 0 0
1778 0 PUTBACK;
1779  
1780 0 count = call_sv(callback, G_SCALAR);
1781 0 SPAGAIN;
1782 0 ▶ result = count > 0 ? SvTRUE(TOPs) : 0;
Branch: 1 branch
BranchTrueFalseTotal
result = count > 0 ? SvTRUE(TOPs) : 0; 0 0 0
1783 0 ▶ if (count > 0) POPs;
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) POPs; 0 0 0
1784 0 PUTBACK;
1785 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
1786  
1787 0 ▶ if (result) {
Branch: 1 branch
BranchTrueFalseTotal
if (result) { 0 0 0
1788 0 deleted++;
1789 } else {
1790 0 ▶ if (write_pos != i) {
Branch: 1 branch
BranchTrueFalseTotal
if (write_pos != i) { 0 0 0
1791 0 nh->data[write_pos] = nh->data[i];
1792 }
1793 0 write_pos++;
1794 }
1795 }
1796  
1797 0 nh->size = write_pos;
1798  
1799 0 ▶ if (deleted > 0 && nh->size > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (deleted > 0 && nh->size > 1) { 0 0 0
if (deleted > 0 && nh->size > 1) { 0 0 0
1800 IV j;
1801 0 ▶ for (j = (nh->size >> 1) - 1; j >= 0; j--) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = (nh->size >> 1) - 1; j >= 0; j--) { 0 0 0
1802 0 ▶ if (nh->type == HEAP_MIN)
Branch: 1 branch
BranchTrueFalseTotal
if (nh->type == HEAP_MIN) 0 0 0
1803 0 nv_sift_down_min(nh, j);
1804 else
1805 0 nv_sift_down_max(nh, j);
1806 }
1807 }
1808  
1809 /* Push (not set) the result. The op's contract is "consume 2,
1810 * produce 1" (net stack effect -1). After SP -= 2, SP sits at
1811 * the slot below the original two args; SETi here would only
1812 * overwrite that slot without advancing SP, leaving net -2. The
1813 * immediate consumer (padsv_store) usually still reads the right
1814 * value, but under PERL_RC_STACK + DEBUGGING an outer scope's
1815 * rpp_popfree_to_NN sees the stack one slot below where it
1816 * expects and asserts (`sp <= PL_stack_sp`). XPUSHi advances SP
1817 * so the net effect is correctly -1. */
1818 0 SPAGAIN;
1819 0 ▶ XPUSHi(deleted);
Branch: 2 branches
BranchTrueFalseTotal
XPUSHi(deleted); 0 0 0
XPUSHi(deleted); 0 0 0
1820 0 RETURN;
1821 }
1822  
1823 /* Generic search custom op - dispatches to both heap types */
1824 0 static OP* pp_heap_func_search(pTHX) {
1825 0 dSP;
1826 0 SV *callback = TOPs;
1827 0 SV *heap_sv = TOPm1s;
1828 0 HeapLookup lookup = find_heap(aTHX_ heap_sv);
1829 0 IV i, found = 0;
1830  
1831 0 SP -= 2;
1832 0 PUTBACK;
1833  
1834 0 ▶ if (!SvROK(callback) || SvTYPE(SvRV(callback)) != SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(callback) || SvTYPE(SvRV(callback)) != SVt_PVCV) 0 0 0
if (!SvROK(callback) || SvTYPE(SvRV(callback)) != SVt_PVCV) 0 0 0
1835 0 croak("search requires a code reference");
1836  
1837 0 ▶ if (lookup.type == MAGIC_NUMERIC) {
Branch: 1 branch
BranchTrueFalseTotal
if (lookup.type == MAGIC_NUMERIC) { 0 0 0
1838 0 NumericHeap *nh = lookup.ptr.nheap;
1839 NV *results;
1840  
1841 0 ▶ if (nh->size == 0) RETURN;
Branch: 1 branch
BranchTrueFalseTotal
if (nh->size == 0) RETURN; 0 0 0
1842  
1843 0 ▶ Newx(results, nh->size, NV);
Branch: 1 branch
BranchTrueFalseTotal
Newx(results, nh->size, NV); 0 0 0
1844  
1845 0 ▶ for (i = 0; i < nh->size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nh->size; i++) { 0 0 0
1846 0 dSP;
1847 0 SV *elem = sv_2mortal(newSVnv(nh->data[i]));
1848 IV result;
1849 int count;
1850  
1851 0 ENTER; SAVETMPS;
1852 0 SAVE_DEFSV;
1853 0 DEFSV_set(elem);
1854  
1855 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
1856 0 ▶ XPUSHs(elem);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(elem); 0 0 0
1857 0 PUTBACK;
1858  
1859 0 count = call_sv(callback, G_SCALAR);
1860 0 SPAGAIN;
1861 0 ▶ result = count > 0 ? SvTRUE(TOPs) : 0;
Branch: 1 branch
BranchTrueFalseTotal
result = count > 0 ? SvTRUE(TOPs) : 0; 0 0 0
1862 0 ▶ if (count > 0) POPs;
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) POPs; 0 0 0
1863 0 PUTBACK;
1864 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
1865  
1866 0 ▶ if (result) {
Branch: 1 branch
BranchTrueFalseTotal
if (result) { 0 0 0
1867 0 results[found++] = nh->data[i];
1868 }
1869 }
1870  
1871 0 SPAGAIN;
1872 0 ▶ EXTEND(SP, found);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, found); 0 0 0
EXTEND(SP, found); 0 0 0
1873 0 ▶ for (i = 0; i < found; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < found; i++) { 0 0 0
1874 0 PUSHs(sv_2mortal(newSVnv(results[i])));
1875 }
1876 0 Safefree(results);
1877 0 RETURN;
1878 }
1879  
1880 0 ▶ if (lookup.type == MAGIC_HEAP) {
Branch: 1 branch
BranchTrueFalseTotal
if (lookup.type == MAGIC_HEAP) { 0 0 0
1881 0 Heap *h = lookup.ptr.heap;
1882 SV **results;
1883  
1884 0 ▶ if (h->size == 0) RETURN;
Branch: 1 branch
BranchTrueFalseTotal
if (h->size == 0) RETURN; 0 0 0
1885  
1886 0 ▶ Newx(results, h->size, SV*);
Branch: 1 branch
BranchTrueFalseTotal
Newx(results, h->size, SV*); 0 0 0
1887  
1888 0 ▶ for (i = 0; i < h->size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < h->size; i++) { 0 0 0
1889 0 dSP;
1890 IV result;
1891 int count;
1892  
1893 0 ENTER; SAVETMPS;
1894 0 SAVE_DEFSV;
1895 0 DEFSV_set(h->data[i]);
1896  
1897 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
1898 0 ▶ XPUSHs(h->data[i]);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(h->data[i]); 0 0 0
1899 0 PUTBACK;
1900  
1901 0 count = call_sv(callback, G_SCALAR);
1902 0 SPAGAIN;
1903 0 ▶ result = count > 0 ? SvTRUE(TOPs) : 0;
Branch: 1 branch
BranchTrueFalseTotal
result = count > 0 ? SvTRUE(TOPs) : 0; 0 0 0
1904 0 ▶ if (count > 0) POPs;
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) POPs; 0 0 0
1905 0 PUTBACK;
1906 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
1907  
1908 0 ▶ if (result) {
Branch: 1 branch
BranchTrueFalseTotal
if (result) { 0 0 0
1909 0 results[found++] = h->data[i];
1910 }
1911 }
1912  
1913 0 SPAGAIN;
1914 0 ▶ EXTEND(SP, found);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, found); 0 0 0
EXTEND(SP, found); 0 0 0
1915 0 ▶ for (i = 0; i < found; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < found; i++) { 0 0 0
1916 0 PUSHs(results[i]);
1917 }
1918 0 Safefree(results);
1919 0 RETURN;
1920 }
1921  
1922 0 croak("Not a heap object");
1923 RETURN;
1924 }
1925  
1926 /* Generic delete custom op - dispatches to both heap types */
1927 0 static OP* pp_heap_func_delete(pTHX) {
1928 0 dSP;
1929 0 SV *callback = TOPs;
1930 0 SV *heap_sv = TOPm1s;
1931 0 HeapLookup lookup = find_heap(aTHX_ heap_sv);
1932 0 IV i, write_pos = 0, deleted = 0;
1933 0 dTARGET;
1934  
1935 0 SP -= 2;
1936 0 PUTBACK;
1937  
1938 0 ▶ if (!SvROK(callback) || SvTYPE(SvRV(callback)) != SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(callback) || SvTYPE(SvRV(callback)) != SVt_PVCV) 0 0 0
if (!SvROK(callback) || SvTYPE(SvRV(callback)) != SVt_PVCV) 0 0 0
1939 0 croak("delete requires a code reference");
1940  
1941 0 ▶ if (lookup.type == MAGIC_NUMERIC) {
Branch: 1 branch
BranchTrueFalseTotal
if (lookup.type == MAGIC_NUMERIC) { 0 0 0
1942 0 NumericHeap *nh = lookup.ptr.nheap;
1943  
1944 0 ▶ for (i = 0; i < nh->size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nh->size; i++) { 0 0 0
1945 0 dSP;
1946 0 SV *elem = sv_2mortal(newSVnv(nh->data[i]));
1947 IV result;
1948 int count;
1949  
1950 0 ENTER; SAVETMPS;
1951 0 SAVE_DEFSV;
1952 0 DEFSV_set(elem);
1953  
1954 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
1955 0 ▶ XPUSHs(elem);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(elem); 0 0 0
1956 0 PUTBACK;
1957  
1958 0 count = call_sv(callback, G_SCALAR);
1959 0 SPAGAIN;
1960 0 ▶ result = count > 0 ? SvTRUE(TOPs) : 0;
Branch: 1 branch
BranchTrueFalseTotal
result = count > 0 ? SvTRUE(TOPs) : 0; 0 0 0
1961 0 ▶ if (count > 0) POPs;
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) POPs; 0 0 0
1962 0 PUTBACK;
1963 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
1964  
1965 0 ▶ if (result) {
Branch: 1 branch
BranchTrueFalseTotal
if (result) { 0 0 0
1966 0 deleted++;
1967 } else {
1968 0 ▶ if (write_pos != i) {
Branch: 1 branch
BranchTrueFalseTotal
if (write_pos != i) { 0 0 0
1969 0 nh->data[write_pos] = nh->data[i];
1970 }
1971 0 write_pos++;
1972 }
1973 }
1974  
1975 0 nh->size = write_pos;
1976  
1977 0 ▶ if (deleted > 0 && nh->size > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (deleted > 0 && nh->size > 1) { 0 0 0
if (deleted > 0 && nh->size > 1) { 0 0 0
1978 IV j;
1979 0 ▶ for (j = (nh->size >> 1) - 1; j >= 0; j--) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = (nh->size >> 1) - 1; j >= 0; j--) { 0 0 0
1980 0 ▶ if (nh->type == HEAP_MIN)
Branch: 1 branch
BranchTrueFalseTotal
if (nh->type == HEAP_MIN) 0 0 0
1981 0 nv_sift_down_min(nh, j);
1982 else
1983 0 nv_sift_down_max(nh, j);
1984 }
1985 }
1986  
1987 0 SPAGAIN;
1988 0 ▶ PUSHi(deleted);
Branch: 1 branch
BranchTrueFalseTotal
PUSHi(deleted); 0 0 0
1989 0 RETURN;
1990 }
1991  
1992 0 ▶ if (lookup.type == MAGIC_HEAP) {
Branch: 1 branch
BranchTrueFalseTotal
if (lookup.type == MAGIC_HEAP) { 0 0 0
1993 0 Heap *h = lookup.ptr.heap;
1994  
1995 0 ▶ for (i = 0; i < h->size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < h->size; i++) { 0 0 0
1996 0 dSP;
1997 IV result;
1998 int count;
1999  
2000 0 ENTER; SAVETMPS;
2001 0 SAVE_DEFSV;
2002 0 DEFSV_set(h->data[i]);
2003  
2004 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
2005 0 ▶ XPUSHs(h->data[i]);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(h->data[i]); 0 0 0
2006 0 PUTBACK;
2007  
2008 0 count = call_sv(callback, G_SCALAR);
2009 0 SPAGAIN;
2010 0 ▶ result = count > 0 ? SvTRUE(TOPs) : 0;
Branch: 1 branch
BranchTrueFalseTotal
result = count > 0 ? SvTRUE(TOPs) : 0; 0 0 0
2011 0 ▶ if (count > 0) POPs;
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) POPs; 0 0 0
2012 0 PUTBACK;
2013 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
2014  
2015 0 ▶ if (result) {
Branch: 1 branch
BranchTrueFalseTotal
if (result) { 0 0 0
2016 0 SvREFCNT_dec(h->data[i]);
2017 0 deleted++;
2018 } else {
2019 0 ▶ if (write_pos != i) {
Branch: 1 branch
BranchTrueFalseTotal
if (write_pos != i) { 0 0 0
2020 0 h->data[write_pos] = h->data[i];
2021 0 h->priorities[write_pos] = h->priorities[i];
2022 }
2023 0 write_pos++;
2024 }
2025 }
2026  
2027 0 h->size = write_pos;
2028  
2029 0 ▶ if (deleted > 0 && h->size > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (deleted > 0 && h->size > 1) { 0 0 0
if (deleted > 0 && h->size > 1) { 0 0 0
2030 IV j;
2031 0 ▶ for (j = (h->size >> 1) - 1; j >= 0; j--) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = (h->size >> 1) - 1; j >= 0; j--) { 0 0 0
2032 0 h->sift_down(aTHX_ h, j);
2033 }
2034 }
2035  
2036 0 SPAGAIN;
2037 0 ▶ PUSHi(deleted);
Branch: 1 branch
BranchTrueFalseTotal
PUSHi(deleted); 0 0 0
2038 0 RETURN;
2039 }
2040  
2041 0 croak("Not a heap object");
2042 RETURN;
2043 }
2044  
2045 1032 static OP* pp_heap_func_push(pTHX) {
2046 1032 dSP;
2047 1032 SV *val_sv = TOPs;
2048 1032 SV *heap_sv = TOPm1s;
2049 1032 HeapLookup lookup = find_heap(aTHX_ heap_sv);
2050  
2051 1032 ▶ if (lookup.type == MAGIC_NUMERIC) {
Branch: 1 branch
BranchTrueFalseTotal
if (lookup.type == MAGIC_NUMERIC) { 0 1032 1032
2052 0 NumericHeap *nh = lookup.ptr.nheap;
2053 0 NV val = SvNV(val_sv);
2054 0 nv_ensure_capacity(nh, nh->size + 1);
2055 0 nh->data[nh->size] = val;
2056 0 nh->size++;
2057 0 ▶ if (nh->type == HEAP_MIN) {
Branch: 1 branch
BranchTrueFalseTotal
if (nh->type == HEAP_MIN) { 0 0 0
2058 0 nv_sift_up_min(nh, nh->size - 1);
2059 } else {
2060 0 nv_sift_up_max(nh, nh->size - 1);
2061 }
2062 0 SP--;
2063 0 SETs(heap_sv);
2064 0 RETURN;
2065 }
2066  
2067 1032 ▶ if (lookup.type == MAGIC_HEAP) {
Branch: 1 branch
BranchTrueFalseTotal
if (lookup.type == MAGIC_HEAP) { 1032 0 1032
2068 1032 Heap *h = lookup.ptr.heap;
2069 SV *value;
2070  
2071 1032 heap_ensure_capacity(h, h->size + 1);
2072 1032 ▶ if (h->comparator) {
Branch: 1 branch
BranchTrueFalseTotal
if (h->comparator) { 3 1029 1032
2073 3 value = newSVsv(val_sv);
2074 3 h->data[h->size] = value;
2075 1029 ▶ } else if (h->key_path) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (h->key_path) { 2 1027 1029
2076 2 value = newSVsv(val_sv);
2077 2 h->data[h->size] = value;
2078 2 h->priorities[h->size] = heap_extract_key_nv(aTHX_ value, h->key_path, h->key_lens, h->key_depth);
2079 } else {
2080 1027 NV prio = SvNV(val_sv);
2081 1027 value = newSVsv(val_sv);
2082 1027 h->data[h->size] = value;
2083 1027 h->priorities[h->size] = prio;
2084 }
2085 1032 h->size++;
2086 1032 h->sift_up(aTHX_ h, h->size - 1);
2087  
2088 1032 SP--;
2089 1032 SETs(heap_sv);
2090 1032 RETURN;
2091 }
2092  
2093 0 croak("Not a heap object");
2094 RETURN;
2095 }
2096  
2097 1011 static OP* pp_heap_func_pop(pTHX) {
2098 1011 dSP;
2099 1011 SV *heap_sv = TOPs;
2100 1011 HeapLookup lookup = find_heap(aTHX_ heap_sv);
2101  
2102 1011 ▶ if (lookup.type == MAGIC_NUMERIC) {
Branch: 1 branch
BranchTrueFalseTotal
if (lookup.type == MAGIC_NUMERIC) { 0 1011 1011
2103 0 NumericHeap *nh = lookup.ptr.nheap;
2104 0 ▶ if (nh->size == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nh->size == 0) { 0 0 0
2105 0 SETs(&PL_sv_undef);
2106 0 RETURN;
2107 }
2108 0 NV result = nh->data[0];
2109 0 nh->size--;
2110 0 ▶ if (nh->size > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nh->size > 0) { 0 0 0
2111 0 nh->data[0] = nh->data[nh->size];
2112 0 ▶ if (nh->type == HEAP_MIN) {
Branch: 1 branch
BranchTrueFalseTotal
if (nh->type == HEAP_MIN) { 0 0 0
2113 0 nv_sift_down_min(nh, 0);
2114 } else {
2115 0 nv_sift_down_max(nh, 0);
2116 }
2117 }
2118 0 SETs(sv_2mortal(newSVnv(result)));
2119 0 RETURN;
2120 }
2121  
2122 1011 ▶ if (lookup.type == MAGIC_HEAP) {
Branch: 1 branch
BranchTrueFalseTotal
if (lookup.type == MAGIC_HEAP) { 1011 0 1011
2123 1011 Heap *h = lookup.ptr.heap;
2124 1011 ▶ if (h->size == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (h->size == 0) { 1 1010 1011
2125 1 SETs(&PL_sv_undef);
2126 1 RETURN;
2127 }
2128 1010 SV *result = sv_2mortal(h->data[0]);
2129 1010 h->size--;
2130 1010 ▶ if (h->size > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (h->size > 0) { 8 1002 1010
2131 8 h->data[0] = h->data[h->size];
2132 8 h->priorities[0] = h->priorities[h->size];
2133 8 h->sift_down(aTHX_ h, 0);
2134 }
2135 1010 SETs(result);
2136 1010 RETURN;
2137 }
2138  
2139 0 croak("Not a heap object");
2140 RETURN;
2141 }
2142  
2143 1005 static OP* pp_heap_func_peek(pTHX) {
2144 1005 dSP;
2145 1005 SV *heap_sv = TOPs;
2146 1005 HeapLookup lookup = find_heap(aTHX_ heap_sv);
2147  
2148 1005 ▶ if (lookup.type == MAGIC_NUMERIC) {
Branch: 1 branch
BranchTrueFalseTotal
if (lookup.type == MAGIC_NUMERIC) { 0 1005 1005
2149 0 NumericHeap *nh = lookup.ptr.nheap;
2150 0 ▶ SETs(nh->size > 0 ? sv_2mortal(newSVnv(nh->data[0])) : &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
SETs(nh->size > 0 ? sv_2mortal(newSVnv(nh->data[0])) : &PL_sv_undef); 0 0 0
2151 0 RETURN;
2152 }
2153  
2154 1005 ▶ if (lookup.type == MAGIC_HEAP) {
Branch: 1 branch
BranchTrueFalseTotal
if (lookup.type == MAGIC_HEAP) { 1005 0 1005
2155 1005 Heap *h = lookup.ptr.heap;
2156 1005 ▶ SETs(h->size > 0 ? h->data[0] : &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
SETs(h->size > 0 ? h->data[0] : &PL_sv_undef); 1004 1 1005
2157 1005 RETURN;
2158 }
2159  
2160 0 croak("Not a heap object");
2161 RETURN;
2162 }
2163  
2164 1009 static OP* pp_heap_func_size(pTHX) {
2165 1009 dSP;
2166 1009 SV *heap_sv = TOPs;
2167 1009 HeapLookup lookup = find_heap(aTHX_ heap_sv);
2168  
2169 1009 ▶ if (lookup.type == MAGIC_NUMERIC) {
Branch: 1 branch
BranchTrueFalseTotal
if (lookup.type == MAGIC_NUMERIC) { 0 1009 1009
2170 0 SETs(sv_2mortal(newSViv(lookup.ptr.nheap->size)));
2171 0 RETURN;
2172 }
2173  
2174 1009 ▶ if (lookup.type == MAGIC_HEAP) {
Branch: 1 branch
BranchTrueFalseTotal
if (lookup.type == MAGIC_HEAP) { 1009 0 1009
2175 1009 SETs(sv_2mortal(newSViv(lookup.ptr.heap->size)));
2176 1009 RETURN;
2177 }
2178  
2179 0 croak("Not a heap object");
2180 RETURN;
2181 }
2182  
2183 7 static OP* pp_heap_func_is_empty(pTHX) {
2184 7 dSP;
2185 7 SV *heap_sv = TOPs;
2186 7 HeapLookup lookup = find_heap(aTHX_ heap_sv);
2187  
2188 7 ▶ if (lookup.type == MAGIC_NUMERIC) {
Branch: 1 branch
BranchTrueFalseTotal
if (lookup.type == MAGIC_NUMERIC) { 3 4 7
2189 3 ▶ if (lookup.ptr.nheap->size == 0) { SETs(&PL_sv_yes); }
Branch: 1 branch
BranchTrueFalseTotal
if (lookup.ptr.nheap->size == 0) { SETs(&PL_sv_yes); } 2 1 3
2190 1 else { SETs(&PL_sv_no); }
2191 3 RETURN;
2192 }
2193  
2194 4 ▶ if (lookup.type == MAGIC_HEAP) {
Branch: 1 branch
BranchTrueFalseTotal
if (lookup.type == MAGIC_HEAP) { 4 0 4
2195 4 ▶ if (lookup.ptr.heap->size == 0) { SETs(&PL_sv_yes); }
Branch: 1 branch
BranchTrueFalseTotal
if (lookup.ptr.heap->size == 0) { SETs(&PL_sv_yes); } 3 1 4
2196 1 else { SETs(&PL_sv_no); }
2197 4 RETURN;
2198 }
2199  
2200 0 croak("Not a heap object");
2201 RETURN;
2202 }
2203  
2204 2 static OP* pp_heap_func_clear(pTHX) {
2205 2 dSP;
2206 2 SV *heap_sv = TOPs;
2207 2 HeapLookup lookup = find_heap(aTHX_ heap_sv);
2208  
2209 2 ▶ if (lookup.type == MAGIC_NUMERIC) {
Branch: 1 branch
BranchTrueFalseTotal
if (lookup.type == MAGIC_NUMERIC) { 1 1 2
2210 1 lookup.ptr.nheap->size = 0;
2211 1 SETs(&PL_sv_undef);
2212 1 RETURN;
2213 }
2214  
2215 1 ▶ if (lookup.type == MAGIC_HEAP) {
Branch: 1 branch
BranchTrueFalseTotal
if (lookup.type == MAGIC_HEAP) { 1 0 1
2216 1 Heap *h = lookup.ptr.heap;
2217 IV i;
2218 6 ▶ for (i = 0; i < h->size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < h->size; i++) { 5 1 6
2219 5 ▶ if (h->data[i]) SvREFCNT_dec(h->data[i]);
Branch: 1 branch
BranchTrueFalseTotal
if (h->data[i]) SvREFCNT_dec(h->data[i]); 5 0 5
2220 }
2221 1 h->size = 0;
2222 1 SETs(&PL_sv_undef);
2223 1 RETURN;
2224 }
2225  
2226 0 croak("Not a heap object");
2227 RETURN;
2228 }
2229  
2230 3 static OP* pp_heap_func_type(pTHX) {
2231 3 dSP;
2232 3 SV *heap_sv = TOPs;
2233 3 HeapLookup lookup = find_heap(aTHX_ heap_sv);
2234  
2235 3 ▶ if (lookup.type == MAGIC_NUMERIC) {
Branch: 1 branch
BranchTrueFalseTotal
if (lookup.type == MAGIC_NUMERIC) { 1 2 3
2236 1 ▶ if (lookup.ptr.nheap->type == HEAP_MIN)
Branch: 1 branch
BranchTrueFalseTotal
if (lookup.ptr.nheap->type == HEAP_MIN) 0 1 1
2237 0 SETs(sv_2mortal(newSVpvn("min", 3)));
2238 else
2239 1 SETs(sv_2mortal(newSVpvn("max", 3)));
2240 1 RETURN;
2241 }
2242  
2243 2 ▶ if (lookup.type == MAGIC_HEAP) {
Branch: 1 branch
BranchTrueFalseTotal
if (lookup.type == MAGIC_HEAP) { 2 0 2
2244 2 ▶ if (lookup.ptr.heap->type == HEAP_MIN)
Branch: 1 branch
BranchTrueFalseTotal
if (lookup.ptr.heap->type == HEAP_MIN) 1 1 2
2245 1 SETs(sv_2mortal(newSVpvn("min", 3)));
2246 else
2247 1 SETs(sv_2mortal(newSVpvn("max", 3)));
2248 2 RETURN;
2249 }
2250  
2251 0 croak("Not a heap object");
2252 RETURN;
2253 }
2254  
2255 /* Call checkers for function-style calls (Heap::PQ::push($h,$v) and imported heap_push($h,$v))
2256 *
2257 * pad_alloc was promoted to public Perl API in 5.15.1 (per ppport.h). On older
2258 * Perls the symbol isn't exported by libperl, so the bare reference would be
2259 * unresolved at dlopen time even though the call_checker code is dead unless
2260 * cv_set_call_checker fires (5.14+). To keep the module loadable on those
2261 * Perls, the optimisation is gated on >= 5.16 and the checkers degrade to a
2262 * no-op rewrite (returning the original entersub) below that. */
2263 typedef OP* (*heap_ppfunc)(pTHX);
2264  
2265 #if PERL_VERSION_GE(5,16,0)
2266 71 static OP* heap_call_checker_1arg(pTHX_ OP *entersubop, GV *namegv, SV *ckobj) {
2267 71 heap_ppfunc ppfunc = (heap_ppfunc)SvIVX(ckobj);
2268 OP *pushop, *cvop, *heapop;
2269 OP *newop;
2270  
2271 PERL_UNUSED_ARG(namegv);
2272  
2273 71 pushop = cUNOPx(entersubop)->op_first;
2274 71 ▶ if (!OpHAS_SIBLING(pushop)) pushop = cUNOPx(pushop)->op_first;
Branch: 1 branch
BranchTrueFalseTotal
if (!OpHAS_SIBLING(pushop)) pushop = cUNOPx(pushop)->op_first; 71 0 71
2275  
2276 71 ▶ heapop = OpSIBLING(pushop);
Branch: 1 branch
BranchTrueFalseTotal
heapop = OpSIBLING(pushop); 71 0 71
2277 71 ▶ if (!heapop) return entersubop;
Branch: 1 branch
BranchTrueFalseTotal
if (!heapop) return entersubop; 0 71 71
2278  
2279 71 ▶ cvop = OpSIBLING(heapop);
Branch: 1 branch
BranchTrueFalseTotal
cvop = OpSIBLING(heapop); 71 0 71
2280 71 ▶ if (!cvop) return entersubop;
Branch: 1 branch
BranchTrueFalseTotal
if (!cvop) return entersubop; 0 71 71
2281 71 ▶ if (OpSIBLING(heapop) != cvop) return entersubop;
Branch: 2 branches
BranchTrueFalseTotal
if (OpSIBLING(heapop) != cvop) return entersubop; 71 0 71
if (OpSIBLING(heapop) != cvop) return entersubop; 0 71 71
2282  
2283 71 OpMORESIB_set(pushop, cvop);
2284 71 OpLASTSIB_set(heapop, NULL);
2285  
2286 71 newop = newUNOP(OP_NULL, 0, heapop);
2287 71 newop->op_type = OP_CUSTOM;
2288 71 newop->op_ppaddr = ppfunc;
2289 71 newop->op_targ = pad_alloc(OP_CUSTOM, SVs_PADTMP);
2290  
2291 71 op_free(entersubop);
2292 71 return newop;
2293 }
2294  
2295 49 static OP* heap_call_checker_2arg(pTHX_ OP *entersubop, GV *namegv, SV *ckobj) {
2296 49 heap_ppfunc ppfunc = (heap_ppfunc)SvIVX(ckobj);
2297 OP *pushop, *cvop, *heapop, *valop;
2298 OP *newop;
2299  
2300 PERL_UNUSED_ARG(namegv);
2301  
2302 49 pushop = cUNOPx(entersubop)->op_first;
2303 49 ▶ if (!OpHAS_SIBLING(pushop)) pushop = cUNOPx(pushop)->op_first;
Branch: 1 branch
BranchTrueFalseTotal
if (!OpHAS_SIBLING(pushop)) pushop = cUNOPx(pushop)->op_first; 49 0 49
2304  
2305 49 ▶ heapop = OpSIBLING(pushop);
Branch: 1 branch
BranchTrueFalseTotal
heapop = OpSIBLING(pushop); 49 0 49
2306 49 ▶ if (!heapop) return entersubop;
Branch: 1 branch
BranchTrueFalseTotal
if (!heapop) return entersubop; 0 49 49
2307  
2308 49 ▶ valop = OpSIBLING(heapop);
Branch: 1 branch
BranchTrueFalseTotal
valop = OpSIBLING(heapop); 49 0 49
2309 49 ▶ if (!valop) return entersubop;
Branch: 1 branch
BranchTrueFalseTotal
if (!valop) return entersubop; 0 49 49
2310  
2311 49 ▶ cvop = OpSIBLING(valop);
Branch: 1 branch
BranchTrueFalseTotal
cvop = OpSIBLING(valop); 49 0 49
2312 49 ▶ if (!cvop) return entersubop;
Branch: 1 branch
BranchTrueFalseTotal
if (!cvop) return entersubop; 0 49 49
2313 49 ▶ if (OpSIBLING(valop) != cvop) return entersubop;
Branch: 2 branches
BranchTrueFalseTotal
if (OpSIBLING(valop) != cvop) return entersubop; 49 0 49
if (OpSIBLING(valop) != cvop) return entersubop; 0 49 49
2314  
2315 49 OpMORESIB_set(pushop, cvop);
2316 49 OpLASTSIB_set(valop, NULL);
2317 49 OpLASTSIB_set(heapop, NULL);
2318  
2319 49 newop = newBINOP(OP_NULL, 0, heapop, valop);
2320 49 newop->op_type = OP_CUSTOM;
2321 49 newop->op_ppaddr = ppfunc;
2322 49 newop->op_targ = pad_alloc(OP_CUSTOM, SVs_PADTMP);
2323  
2324 49 op_free(entersubop);
2325 49 return newop;
2326 }
2327 #else /* pre-5.16: no pad_alloc — leave the optree alone */
2328 static OP* heap_call_checker_1arg(pTHX_ OP *entersubop, GV *namegv, SV *ckobj) {
2329 PERL_UNUSED_ARG(namegv);
2330 PERL_UNUSED_ARG(ckobj);
2331 return entersubop;
2332 }
2333 static OP* heap_call_checker_2arg(pTHX_ OP *entersubop, GV *namegv, SV *ckobj) {
2334 PERL_UNUSED_ARG(namegv);
2335 PERL_UNUSED_ARG(ckobj);
2336 return entersubop;
2337 }
2338 #endif /* PERL_VERSION_GE(5,16,0) */
2339  
2340 /* XS Functions */
2341 209 XS_EXTERNAL(XS_heap_new) {
2342 209 dXSARGS;
2343 Heap *h;
2344 SV *obj_sv, *rv;
2345 HV *stash;
2346 209 HeapType type = HEAP_MIN;
2347 209 SV *comparator = NULL;
2348 209 char *key_path_str = NULL;
2349 209 int arg_offset = 0;
2350  
2351 209 ▶ if (items >= 1 && SvPOK(ST(0))) {
Branch: 2 branches
BranchTrueFalseTotal
if (items >= 1 && SvPOK(ST(0))) { 193 16 209
if (items >= 1 && SvPOK(ST(0))) { 193 0 193
2352 STRLEN len;
2353 193 const char *str = SvPV(ST(0), len);
2354 193 ▶ if (len == 8 && strEQ(str, "Heap::PQ")) {
Branch: 2 branches
BranchTrueFalseTotal
if (len == 8 && strEQ(str, "Heap::PQ")) { 0 193 193
if (len == 8 && strEQ(str, "Heap::PQ")) { 0 0 0
2355 0 arg_offset = 1;
2356 193 ▶ } else if (len == 3 && (strEQ(str, "min") || strEQ(str, "max"))) {
Branch: 3 branches
BranchTrueFalseTotal
} else if (len == 3 && (strEQ(str, "min") || strEQ(str, "max"))) { 193 0 193
} else if (len == 3 && (strEQ(str, "min") || strEQ(str, "max"))) { 11 182 193
} else if (len == 3 && (strEQ(str, "min") || strEQ(str, "max"))) { 11 0 11
2357 193 arg_offset = 0;
2358 } else {
2359 0 arg_offset = 1;
2360 }
2361 }
2362  
2363 209 ▶ if (items > arg_offset) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > arg_offset) { 193 16 209
2364 STRLEN len;
2365 193 const char *type_str = SvPV(ST(arg_offset), len);
2366 193 ▶ if (len == 3 && strEQ(type_str, "max")) {
Branch: 2 branches
BranchTrueFalseTotal
if (len == 3 && strEQ(type_str, "max")) { 193 0 193
if (len == 3 && strEQ(type_str, "max")) { 11 182 193
2367 11 type = HEAP_MAX;
2368 182 ▶ } else if (len == 3 && strEQ(type_str, "min")) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 3 && strEQ(type_str, "min")) { 182 0 182
} else if (len == 3 && strEQ(type_str, "min")) { 182 0 182
2369 182 type = HEAP_MIN;
2370 } else {
2371 0 croak("heap type must be 'min' or 'max'");
2372 }
2373 }
2374  
2375 209 ▶ if (items > arg_offset + 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > arg_offset + 1) { 129 80 209
2376 129 SV *cmp_arg = ST(arg_offset + 1);
2377 129 ▶ if (SvROK(cmp_arg) && SvTYPE(SvRV(cmp_arg)) == SVt_PVCV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(cmp_arg) && SvTYPE(SvRV(cmp_arg)) == SVt_PVCV) { 119 10 129
if (SvROK(cmp_arg) && SvTYPE(SvRV(cmp_arg)) == SVt_PVCV) { 119 0 119
2378 119 comparator = newSVsv(cmp_arg);
2379 10 ▶ } else if (SvPOK(cmp_arg)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (SvPOK(cmp_arg)) { 10 0 10
2380 /* String key path — parse "key.subkey.subsubkey" into segments */
2381 10 const char *raw = SvPV_nolen(cmp_arg);
2382 10 key_path_str = savepv(raw);
2383 0 ▶ } else if (SvOK(cmp_arg)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (SvOK(cmp_arg)) { 0 0 0
2384 0 croak("Comparator must be a code reference or a key path string");
2385 }
2386 }
2387  
2388 209 Newxz(h, 1, Heap);
2389 209 h->type = type;
2390 209 h->size = 0;
2391 209 h->capacity = 16;
2392 209 Newx(h->data, 16, SV*);
2393 209 Newx(h->priorities, 16, NV);
2394 209 h->comparator = comparator;
2395 209 h->gv_a = NULL;
2396 209 h->gv_b = NULL;
2397 209 h->key_path = NULL;
2398 209 h->key_lens = NULL;
2399 209 h->key_depth = 0;
2400  
2401 209 ▶ if (key_path_str) {
Branch: 1 branch
BranchTrueFalseTotal
if (key_path_str) { 10 199 209
2402 /* Parse "key.subkey.deep" into segments */
2403 char *p, *seg;
2404 10 IV depth = 0, cap = 4;
2405 10 ▶ Newx(h->key_path, cap, char*);
Branch: 1 branch
BranchTrueFalseTotal
Newx(h->key_path, cap, char*); 0 10 10
2406 10 ▶ Newx(h->key_lens, cap, STRLEN);
Branch: 1 branch
BranchTrueFalseTotal
Newx(h->key_lens, cap, STRLEN); 0 10 10
2407  
2408 10 p = key_path_str;
2409 23 ▶ while (*p) {
Branch: 1 branch
BranchTrueFalseTotal
while (*p) { 13 10 23
2410 13 seg = p;
2411 55 ▶ while (*p && *p != '.') p++;
Branch: 2 branches
BranchTrueFalseTotal
while (*p && *p != '.') p++; 45 10 55
while (*p && *p != '.') p++; 42 3 45
2412 13 ▶ if (depth >= cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (depth >= cap) { 0 13 13
2413 0 cap *= 2;
2414 0 ▶ Renew(h->key_path, cap, char*);
Branch: 1 branch
BranchTrueFalseTotal
Renew(h->key_path, cap, char*); 0 0 0
2415 0 ▶ Renew(h->key_lens, cap, STRLEN);
Branch: 1 branch
BranchTrueFalseTotal
Renew(h->key_lens, cap, STRLEN); 0 0 0
2416 }
2417 13 h->key_lens[depth] = p - seg;
2418 13 h->key_path[depth] = savepvn(seg, p - seg);
2419 13 depth++;
2420 13 ▶ if (*p == '.') p++;
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '.') p++; 3 10 13
2421 }
2422 10 Safefree(key_path_str);
2423 10 h->key_depth = depth;
2424  
2425 10 ▶ if (type == HEAP_MIN) {
Branch: 1 branch
BranchTrueFalseTotal
if (type == HEAP_MIN) { 9 1 10
2426 9 h->sift_up = heap_sift_up_min;
2427 9 h->sift_down = heap_sift_down_min;
2428 } else {
2429 1 h->sift_up = heap_sift_up_max;
2430 1 h->sift_down = heap_sift_down_max;
2431 }
2432 199 ▶ } else if (comparator) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (comparator) { 119 80 199
2433 119 ▶ const char *pkg = CopSTASHPV(PL_curcop);
Branch: 7 branches
BranchTrueFalseTotal
const char *pkg = CopSTASHPV(PL_curcop); 119 0 119
const char *pkg = CopSTASHPV(PL_curcop); 119 0 119
const char *pkg = CopSTASHPV(PL_curcop); 119 0 119
const char *pkg = CopSTASHPV(PL_curcop); 0 119 119
const char *pkg = CopSTASHPV(PL_curcop); 0 0 0
const char *pkg = CopSTASHPV(PL_curcop); 119 0 119
const char *pkg = CopSTASHPV(PL_curcop); 0 119 119
2434 119 ▶ if (!pkg) pkg = "main";
Branch: 1 branch
BranchTrueFalseTotal
if (!pkg) pkg = "main"; 0 119 119
2435 119 h->gv_a = gv_fetchpv(Perl_form(aTHX_ "%s::a", pkg), GV_ADD, SVt_PV);
2436 119 h->gv_b = gv_fetchpv(Perl_form(aTHX_ "%s::b", pkg), GV_ADD, SVt_PV);
2437 119 GvMULTI_on(h->gv_a);
2438 119 GvMULTI_on(h->gv_b);
2439 119 h->sift_up = heap_sift_up_custom;
2440 119 h->sift_down = heap_sift_down_custom;
2441 80 ▶ } else if (type == HEAP_MIN) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (type == HEAP_MIN) { 72 8 80
2442 72 h->sift_up = heap_sift_up_min;
2443 72 h->sift_down = heap_sift_down_min;
2444 } else {
2445 8 h->sift_up = heap_sift_up_max;
2446 8 h->sift_down = heap_sift_down_max;
2447 }
2448  
2449 209 obj_sv = newSV(0);
2450 209 sv_magicext(obj_sv, NULL, PERL_MAGIC_ext, &heap_vtbl, (char*)h, 0);
2451  
2452 209 rv = newRV_noinc(obj_sv);
2453 209 stash = gv_stashpvn("Heap::PQ", 8, GV_ADD);
2454 209 sv_bless(rv, stash);
2455  
2456 209 ST(0) = sv_2mortal(rv);
2457 209 XSRETURN(1);
2458 }
2459  
2460 3554 XS_EXTERNAL(XS_heap_push) {
2461 3554 dXSARGS;
2462 HeapLookup hl;
2463  
2464 3554 ▶ if (items != 2) croak("Usage: $heap->push($value)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 2) croak("Usage: $heap->push($value)"); 0 3554 3554
2465  
2466 3554 hl = find_heap(aTHX_ ST(0));
2467  
2468 3554 ▶ if (hl.type == MAGIC_NUMERIC) {
Branch: 1 branch
BranchTrueFalseTotal
if (hl.type == MAGIC_NUMERIC) { 0 3554 3554
2469 0 NumericHeap *nh = hl.ptr.nheap;
2470 0 NV val = SvNV(ST(1));
2471 0 nv_ensure_capacity(nh, nh->size + 1);
2472 0 nh->data[nh->size] = val;
2473 0 nh->size++;
2474 0 ▶ if (nh->type == HEAP_MIN)
Branch: 1 branch
BranchTrueFalseTotal
if (nh->type == HEAP_MIN) 0 0 0
2475 0 nv_sift_up_min(nh, nh->size - 1);
2476 else
2477 0 nv_sift_up_max(nh, nh->size - 1);
2478 0 ST(0) = ST(0);
2479 0 XSRETURN(1);
2480 }
2481  
2482 {
2483 3554 Heap *h = hl.ptr.heap;
2484 3554 SV *val_sv = ST(1);
2485 SV *value;
2486  
2487 3554 heap_ensure_capacity(h, h->size + 1);
2488 3554 ▶ if (h->comparator) {
Branch: 1 branch
BranchTrueFalseTotal
if (h->comparator) { 1057 2497 3554
2489 1057 value = newSVsv(val_sv);
2490 1057 h->data[h->size] = value;
2491 2497 ▶ } else if (h->key_path) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (h->key_path) { 122 2375 2497
2492 122 value = newSVsv(val_sv);
2493 122 h->data[h->size] = value;
2494 122 h->priorities[h->size] = heap_extract_key_nv(aTHX_ value, h->key_path, h->key_lens, h->key_depth);
2495 } else {
2496 2375 NV prio = SvNV(val_sv);
2497 2375 value = newSVsv(val_sv);
2498 2375 h->data[h->size] = value;
2499 2375 h->priorities[h->size] = prio;
2500 }
2501 3554 h->size++;
2502 3554 h->sift_up(aTHX_ h, h->size - 1);
2503  
2504 3554 ST(0) = ST(0);
2505 3554 XSRETURN(1);
2506 }
2507 }
2508  
2509 5 XS_EXTERNAL(XS_heap_push_all) {
2510 5 dXSARGS;
2511 Heap *h;
2512 int i;
2513 IV start_size;
2514  
2515 5 ▶ if (items < 1) croak("Usage: $heap->push_all(@values)");
Branch: 1 branch
BranchTrueFalseTotal
if (items < 1) croak("Usage: $heap->push_all(@values)"); 0 5 5
2516  
2517 5 h = get_heap(aTHX_ ST(0));
2518 5 start_size = h->size;
2519  
2520 5 heap_ensure_capacity(h, h->size + items - 1);
2521  
2522 /* Add all values first */
2523 31 ▶ for (i = 1; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) { 26 5 31
2524 26 SV *val = newSVsv(ST(i));
2525 26 h->data[h->size] = val;
2526 26 ▶ if (h->key_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (h->key_path) { 3 23 26
2527 3 h->priorities[h->size] = heap_extract_key_nv(aTHX_ val, h->key_path, h->key_lens, h->key_depth);
2528 23 ▶ } else if (!h->comparator) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!h->comparator) { 20 3 23
2529 20 h->priorities[h->size] = SvNV(ST(i));
2530 }
2531 26 h->size++;
2532 }
2533  
2534 /* Floyd's heapify if adding many elements */
2535 5 ▶ if (items - 1 > 10) {
Branch: 1 branch
BranchTrueFalseTotal
if (items - 1 > 10) { 0 5 5
2536 IV j;
2537 0 ▶ for (j = (h->size >> 1) - 1; j >= 0; j--) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = (h->size >> 1) - 1; j >= 0; j--) { 0 0 0
2538 0 h->sift_down(aTHX_ h, j);
2539 }
2540 } else {
2541 31 ▶ for (i = start_size; i < h->size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = start_size; i < h->size; i++) { 26 5 31
2542 26 h->sift_up(aTHX_ h, i);
2543 }
2544 }
2545  
2546 5 ST(0) = ST(0);
2547 5 XSRETURN(1);
2548 }
2549  
2550 3502 XS_EXTERNAL(XS_heap_pop) {
2551 3502 dXSARGS;
2552 HeapLookup hl;
2553  
2554 3502 ▶ if (items != 1) croak("Usage: $heap->pop()");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: $heap->pop()"); 0 3502 3502
2555  
2556 3502 hl = find_heap(aTHX_ ST(0));
2557  
2558 3502 ▶ if (hl.type == MAGIC_NUMERIC) {
Branch: 1 branch
BranchTrueFalseTotal
if (hl.type == MAGIC_NUMERIC) { 0 3502 3502
2559 0 NumericHeap *nh = hl.ptr.nheap;
2560 NV result;
2561 0 ▶ if (nh->size == 0) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (nh->size == 0) XSRETURN_UNDEF; 0 0 0
2562 0 result = nh->data[0];
2563 0 nh->size--;
2564 0 ▶ if (nh->size > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nh->size > 0) { 0 0 0
2565 0 nh->data[0] = nh->data[nh->size];
2566 0 ▶ if (nh->type == HEAP_MIN)
Branch: 1 branch
BranchTrueFalseTotal
if (nh->type == HEAP_MIN) 0 0 0
2567 0 nv_sift_down_min(nh, 0);
2568 else
2569 0 nv_sift_down_max(nh, 0);
2570 }
2571 0 ST(0) = sv_2mortal(newSVnv(result));
2572 0 XSRETURN(1);
2573 }
2574  
2575 {
2576 3502 Heap *h = hl.ptr.heap;
2577 SV *result;
2578  
2579 3502 ▶ if (h->size == 0) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (h->size == 0) XSRETURN_UNDEF; 102 3400 3502
2580  
2581 3400 result = sv_2mortal(h->data[0]);
2582  
2583 3400 h->size--;
2584 3400 ▶ if (h->size > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (h->size > 0) { 1255 2145 3400
2585 1255 h->data[0] = h->data[h->size];
2586 1255 h->priorities[0] = h->priorities[h->size];
2587 1255 h->sift_down(aTHX_ h, 0);
2588 }
2589  
2590 3400 ST(0) = result;
2591 3400 XSRETURN(1);
2592 }
2593 }
2594  
2595 2016 XS_EXTERNAL(XS_heap_peek) {
2596 2016 dXSARGS;
2597 HeapLookup hl;
2598  
2599 2016 ▶ if (items != 1) croak("Usage: $heap->peek()");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: $heap->peek()"); 0 2016 2016
2600  
2601 2016 hl = find_heap(aTHX_ ST(0));
2602  
2603 2016 ▶ if (hl.type == MAGIC_NUMERIC) {
Branch: 1 branch
BranchTrueFalseTotal
if (hl.type == MAGIC_NUMERIC) { 0 2016 2016
2604 0 NumericHeap *nh = hl.ptr.nheap;
2605 0 ▶ if (nh->size == 0) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (nh->size == 0) XSRETURN_UNDEF; 0 0 0
2606 0 ST(0) = sv_2mortal(newSVnv(nh->data[0]));
2607 0 XSRETURN(1);
2608 }
2609  
2610 {
2611 2016 Heap *h = hl.ptr.heap;
2612 2016 ▶ if (h->size == 0) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (h->size == 0) XSRETURN_UNDEF; 1 2015 2016
2613 2015 ST(0) = h->data[0];
2614 2015 XSRETURN(1);
2615 }
2616 }
2617  
2618 2041 XS_EXTERNAL(XS_heap_size) {
2619 2041 dXSARGS;
2620 HeapLookup hl;
2621  
2622 2041 ▶ if (items != 1) croak("Usage: $heap->size()");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: $heap->size()"); 0 2041 2041
2623  
2624 2041 hl = find_heap(aTHX_ ST(0));
2625  
2626 2041 ▶ if (hl.type == MAGIC_NUMERIC)
Branch: 1 branch
BranchTrueFalseTotal
if (hl.type == MAGIC_NUMERIC) 0 2041 2041
2627 0 XSRETURN_IV(hl.ptr.nheap->size);
2628  
2629 2041 XSRETURN_IV(hl.ptr.heap->size);
2630 }
2631  
2632 3271 XS_EXTERNAL(XS_heap_is_empty) {
2633 3271 dXSARGS;
2634 HeapLookup lookup;
2635  
2636 3271 ▶ if (items != 1) croak("Usage: $heap->is_empty()");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: $heap->is_empty()"); 0 3271 3271
2637  
2638 3271 lookup = find_heap(aTHX_ ST(0));
2639  
2640 3271 ▶ if (lookup.type == MAGIC_NUMERIC) {
Branch: 1 branch
BranchTrueFalseTotal
if (lookup.type == MAGIC_NUMERIC) { 0 3271 3271
2641 0 ▶ if (lookup.ptr.nheap->size == 0) XSRETURN_YES;
Branch: 1 branch
BranchTrueFalseTotal
if (lookup.ptr.nheap->size == 0) XSRETURN_YES; 0 0 0
2642 0 XSRETURN_NO;
2643 }
2644 3271 ▶ if (lookup.type == MAGIC_HEAP) {
Branch: 1 branch
BranchTrueFalseTotal
if (lookup.type == MAGIC_HEAP) { 3271 0 3271
2645 3271 ▶ if (lookup.ptr.heap->size == 0) XSRETURN_YES;
Branch: 1 branch
BranchTrueFalseTotal
if (lookup.ptr.heap->size == 0) XSRETURN_YES; 2118 1153 3271
2646 1153 XSRETURN_NO;
2647 }
2648 0 croak("Not a heap object");
2649 }
2650  
2651 201 XS_EXTERNAL(XS_heap_clear) {
2652 201 dXSARGS;
2653 HeapLookup lookup;
2654  
2655 201 ▶ if (items != 1) croak("Usage: $heap->clear()");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: $heap->clear()"); 0 201 201
2656  
2657 201 lookup = find_heap(aTHX_ ST(0));
2658  
2659 201 ▶ if (lookup.type == MAGIC_NUMERIC) {
Branch: 1 branch
BranchTrueFalseTotal
if (lookup.type == MAGIC_NUMERIC) { 0 201 201
2660 0 lookup.ptr.nheap->size = 0;
2661 0 XSRETURN_EMPTY;
2662 }
2663 201 ▶ if (lookup.type == MAGIC_HEAP) {
Branch: 1 branch
BranchTrueFalseTotal
if (lookup.type == MAGIC_HEAP) { 201 0 201
2664 201 Heap *h = lookup.ptr.heap;
2665 IV i;
2666 214 ▶ for (i = 0; i < h->size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < h->size; i++) { 13 201 214
2667 13 ▶ if (h->data[i]) SvREFCNT_dec(h->data[i]);
Branch: 1 branch
BranchTrueFalseTotal
if (h->data[i]) SvREFCNT_dec(h->data[i]); 13 0 13
2668 }
2669 201 h->size = 0;
2670 201 XSRETURN_EMPTY;
2671 }
2672 0 croak("Not a heap object");
2673 }
2674  
2675 2 XS_EXTERNAL(XS_heap_type) {
2676 2 dXSARGS;
2677 HeapLookup lookup;
2678 HeapType type;
2679  
2680 2 ▶ if (items != 1) croak("Usage: $heap->type()");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 1) croak("Usage: $heap->type()"); 0 2 2
2681  
2682 2 lookup = find_heap(aTHX_ ST(0));
2683  
2684 2 ▶ if (lookup.type == MAGIC_NUMERIC) {
Branch: 1 branch
BranchTrueFalseTotal
if (lookup.type == MAGIC_NUMERIC) { 0 2 2
2685 0 type = lookup.ptr.nheap->type;
2686 2 ▶ } else if (lookup.type == MAGIC_HEAP) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (lookup.type == MAGIC_HEAP) { 2 0 2
2687 2 type = lookup.ptr.heap->type;
2688 } else {
2689 0 croak("Not a heap object");
2690 }
2691  
2692 2 ▶ if (type == HEAP_MIN) {
Branch: 1 branch
BranchTrueFalseTotal
if (type == HEAP_MIN) { 1 1 2
2693 1 ST(0) = sv_2mortal(newSVpvn("min", 3));
2694 } else {
2695 1 ST(0) = sv_2mortal(newSVpvn("max", 3));
2696 }
2697 2 XSRETURN(1);
2698 }
2699  
2700 /* $heap->peek_n($n) - return top N elements in sorted order without removing */
2701 13 XS_EXTERNAL(XS_heap_peek_n) {
2702 13 dXSARGS;
2703 HeapLookup hl;
2704 IV n, i, count;
2705  
2706 13 ▶ if (items != 2) croak("Usage: $heap->peek_n($n)");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 2) croak("Usage: $heap->peek_n($n)"); 0 13 13
2707  
2708 13 hl = find_heap(aTHX_ ST(0));
2709 13 n = SvIV(ST(1));
2710  
2711 13 ▶ if (hl.type == MAGIC_NUMERIC) {
Branch: 1 branch
BranchTrueFalseTotal
if (hl.type == MAGIC_NUMERIC) { 0 13 13
2712 0 NumericHeap *nh = hl.ptr.nheap;
2713 NV *saved;
2714 IV saved_size;
2715  
2716 0 ▶ if (n <= 0 || nh->size == 0) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (n <= 0 || nh->size == 0) XSRETURN_EMPTY; 0 0 0
if (n <= 0 || nh->size == 0) XSRETURN_EMPTY; 0 0 0
2717 0 ▶ if (n > nh->size) n = nh->size;
Branch: 1 branch
BranchTrueFalseTotal
if (n > nh->size) n = nh->size; 0 0 0
2718  
2719 0 ▶ Newx(saved, nh->size, NV);
Branch: 1 branch
BranchTrueFalseTotal
Newx(saved, nh->size, NV); 0 0 0
2720 0 ▶ Copy(nh->data, saved, nh->size, NV);
Branch: 1 branch
BranchTrueFalseTotal
Copy(nh->data, saved, nh->size, NV); 0 0 0
2721 0 saved_size = nh->size;
2722  
2723 0 ▶ EXTEND(SP, n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n); 0 0 0
EXTEND(SP, n); 0 0 0
2724 0 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 0 0 0
2725 0 NV val = nh->data[0];
2726 0 nh->size--;
2727 0 ▶ if (nh->size > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nh->size > 0) { 0 0 0
2728 0 nh->data[0] = nh->data[nh->size];
2729 0 ▶ if (nh->type == HEAP_MIN)
Branch: 1 branch
BranchTrueFalseTotal
if (nh->type == HEAP_MIN) 0 0 0
2730 0 nv_sift_down_min(nh, 0);
2731 else
2732 0 nv_sift_down_max(nh, 0);
2733 }
2734 0 ST(i) = sv_2mortal(newSVnv(val));
2735 }
2736 0 count = n;
2737  
2738 0 ▶ Copy(saved, nh->data, saved_size, NV);
Branch: 1 branch
BranchTrueFalseTotal
Copy(saved, nh->data, saved_size, NV); 0 0 0
2739 0 nh->size = saved_size;
2740 0 Safefree(saved);
2741 0 XSRETURN(count);
2742 }
2743  
2744 {
2745 13 Heap *h = hl.ptr.heap;
2746 SV **saved_data;
2747 NV *saved_pri;
2748 IV saved_size;
2749  
2750 13 ▶ if (n <= 0 || h->size == 0) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (n <= 0 || h->size == 0) XSRETURN_EMPTY; 11 2 13
if (n <= 0 || h->size == 0) XSRETURN_EMPTY; 1 10 11
2751 10 ▶ if (n > h->size) n = h->size;
Branch: 1 branch
BranchTrueFalseTotal
if (n > h->size) n = h->size; 1 9 10
2752  
2753 10 ▶ Newx(saved_data, h->size, SV*);
Branch: 1 branch
BranchTrueFalseTotal
Newx(saved_data, h->size, SV*); 0 10 10
2754 10 ▶ Newx(saved_pri, h->size, NV);
Branch: 1 branch
BranchTrueFalseTotal
Newx(saved_pri, h->size, NV); 0 10 10
2755 10 ▶ Copy(h->data, saved_data, h->size, SV*);
Branch: 1 branch
BranchTrueFalseTotal
Copy(h->data, saved_data, h->size, SV*); 0 10 10
2756 10 ▶ Copy(h->priorities, saved_pri, h->size, NV);
Branch: 1 branch
BranchTrueFalseTotal
Copy(h->priorities, saved_pri, h->size, NV); 0 10 10
2757 10 saved_size = h->size;
2758  
2759 10 ▶ EXTEND(SP, n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n); 10 0 10
EXTEND(SP, n); 0 10 10
2760 38 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 28 10 38
2761 28 ST(i) = h->data[0];
2762 28 h->size--;
2763 28 ▶ if (h->size > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (h->size > 0) { 26 2 28
2764 26 h->data[0] = h->data[h->size];
2765 26 h->priorities[0] = h->priorities[h->size];
2766 26 h->sift_down(aTHX_ h, 0);
2767 }
2768 }
2769 10 count = n;
2770  
2771 10 ▶ Copy(saved_data, h->data, saved_size, SV*);
Branch: 1 branch
BranchTrueFalseTotal
Copy(saved_data, h->data, saved_size, SV*); 0 10 10
2772 10 ▶ Copy(saved_pri, h->priorities, saved_size, NV);
Branch: 1 branch
BranchTrueFalseTotal
Copy(saved_pri, h->priorities, saved_size, NV); 0 10 10
2773 10 h->size = saved_size;
2774 10 Safefree(saved_data);
2775 10 Safefree(saved_pri);
2776 10 XSRETURN(count);
2777 }
2778 }
2779  
2780 /* $heap->search(sub { ... }) - find elements matching condition */
2781 1 XS_EXTERNAL(XS_heap_search) {
2782 1 dXSARGS;
2783 HeapLookup hl;
2784 1 IV i, found = 0;
2785 SV *callback;
2786  
2787 1 ▶ if (items != 2) croak("Usage: $heap->search(sub { ... })");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 2) croak("Usage: $heap->search(sub { ... })"); 0 1 1
2788  
2789 1 hl = find_heap(aTHX_ ST(0));
2790 1 callback = ST(1);
2791  
2792 1 ▶ if (!SvROK(callback) || SvTYPE(SvRV(callback)) != SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(callback) || SvTYPE(SvRV(callback)) != SVt_PVCV) 1 0 1
if (!SvROK(callback) || SvTYPE(SvRV(callback)) != SVt_PVCV) 0 1 1
2793 0 croak("search requires a code reference");
2794  
2795 1 ▶ if (hl.type == MAGIC_NUMERIC) {
Branch: 1 branch
BranchTrueFalseTotal
if (hl.type == MAGIC_NUMERIC) { 0 1 1
2796 0 NumericHeap *nh = hl.ptr.nheap;
2797 NV *results;
2798  
2799 0 ▶ if (nh->size == 0) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (nh->size == 0) XSRETURN_EMPTY; 0 0 0
2800  
2801 0 ▶ Newx(results, nh->size, NV);
Branch: 1 branch
BranchTrueFalseTotal
Newx(results, nh->size, NV); 0 0 0
2802  
2803 0 ▶ for (i = 0; i < nh->size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nh->size; i++) { 0 0 0
2804 0 dSP;
2805 0 SV *elem = sv_2mortal(newSVnv(nh->data[i]));
2806 IV result;
2807 int count;
2808  
2809 0 ENTER; SAVETMPS;
2810 0 SAVE_DEFSV;
2811 0 DEFSV_set(elem);
2812  
2813 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
2814 0 ▶ XPUSHs(elem);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(elem); 0 0 0
2815 0 PUTBACK;
2816  
2817 0 count = call_sv(callback, G_SCALAR);
2818 0 SPAGAIN;
2819 0 ▶ result = count > 0 ? SvTRUE(TOPs) : 0;
Branch: 1 branch
BranchTrueFalseTotal
result = count > 0 ? SvTRUE(TOPs) : 0; 0 0 0
2820 0 ▶ if (count > 0) POPs;
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) POPs; 0 0 0
2821 0 PUTBACK;
2822 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
2823  
2824 0 ▶ if (result) {
Branch: 1 branch
BranchTrueFalseTotal
if (result) { 0 0 0
2825 0 results[found++] = nh->data[i];
2826 }
2827 }
2828  
2829 0 ▶ EXTEND(SP, found);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, found); 0 0 0
EXTEND(SP, found); 0 0 0
2830 0 ▶ for (i = 0; i < found; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < found; i++) { 0 0 0
2831 0 ST(i) = sv_2mortal(newSVnv(results[i]));
2832 }
2833 0 Safefree(results);
2834 0 XSRETURN(found);
2835 }
2836  
2837 {
2838 1 Heap *h = hl.ptr.heap;
2839 SV **results;
2840  
2841 1 ▶ if (h->size == 0) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (h->size == 0) XSRETURN_EMPTY; 0 1 1
2842  
2843 1 ▶ Newx(results, h->size, SV*);
Branch: 1 branch
BranchTrueFalseTotal
Newx(results, h->size, SV*); 0 1 1
2844  
2845 5 ▶ for (i = 0; i < h->size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < h->size; i++) { 4 1 5
2846 4 dSP;
2847 IV result;
2848 int count;
2849  
2850 4 ENTER; SAVETMPS;
2851 4 SAVE_DEFSV;
2852 4 DEFSV_set(h->data[i]);
2853  
2854 4 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 4 4
2855 4 ▶ XPUSHs(h->data[i]);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(h->data[i]); 0 4 4
2856 4 PUTBACK;
2857  
2858 4 count = call_sv(callback, G_SCALAR);
2859 4 SPAGAIN;
2860 4 ▶ result = count > 0 ? SvTRUE(TOPs) : 0;
Branch: 1 branch
BranchTrueFalseTotal
result = count > 0 ? SvTRUE(TOPs) : 0; 4 0 4
2861 4 ▶ if (count > 0) POPs;
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) POPs; 4 0 4
2862 4 PUTBACK;
2863 4 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 4 0 4
2864  
2865 4 ▶ if (result) {
Branch: 1 branch
BranchTrueFalseTotal
if (result) { 1 3 4
2866 1 results[found++] = h->data[i];
2867 }
2868 }
2869  
2870 1 ▶ EXTEND(SP, found);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, found); 1 0 1
EXTEND(SP, found); 0 1 1
2871 2 ▶ for (i = 0; i < found; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < found; i++) { 1 1 2
2872 1 ST(i) = results[i];
2873 }
2874 1 Safefree(results);
2875 1 XSRETURN(found);
2876 }
2877 }
2878  
2879 /* $heap->delete(sub { ... }) - remove matching elements, returns count */
2880 1 XS_EXTERNAL(XS_heap_delete) {
2881 1 dXSARGS;
2882 HeapLookup hl;
2883 1 IV i, write_pos = 0, deleted = 0;
2884 SV *callback;
2885  
2886 1 ▶ if (items != 2) croak("Usage: $heap->delete(sub { ... })");
Branch: 1 branch
BranchTrueFalseTotal
if (items != 2) croak("Usage: $heap->delete(sub { ... })"); 0 1 1
2887  
2888 1 hl = find_heap(aTHX_ ST(0));
2889 1 callback = ST(1);
2890  
2891 1 ▶ if (!SvROK(callback) || SvTYPE(SvRV(callback)) != SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(callback) || SvTYPE(SvRV(callback)) != SVt_PVCV) 1 0 1
if (!SvROK(callback) || SvTYPE(SvRV(callback)) != SVt_PVCV) 0 1 1
2892 0 croak("delete requires a code reference");
2893  
2894 1 ▶ if (hl.type == MAGIC_NUMERIC) {
Branch: 1 branch
BranchTrueFalseTotal
if (hl.type == MAGIC_NUMERIC) { 0 1 1
2895 0 NumericHeap *nh = hl.ptr.nheap;
2896  
2897 0 ▶ for (i = 0; i < nh->size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nh->size; i++) { 0 0 0
2898 0 dSP;
2899 0 SV *elem = sv_2mortal(newSVnv(nh->data[i]));
2900 IV result;
2901 int count;
2902  
2903 0 ENTER; SAVETMPS;
2904 0 SAVE_DEFSV;
2905 0 DEFSV_set(elem);
2906  
2907 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
2908 0 ▶ XPUSHs(elem);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(elem); 0 0 0
2909 0 PUTBACK;
2910  
2911 0 count = call_sv(callback, G_SCALAR);
2912 0 SPAGAIN;
2913 0 ▶ result = count > 0 ? SvTRUE(TOPs) : 0;
Branch: 1 branch
BranchTrueFalseTotal
result = count > 0 ? SvTRUE(TOPs) : 0; 0 0 0
2914 0 ▶ if (count > 0) POPs;
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) POPs; 0 0 0
2915 0 PUTBACK;
2916 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
2917  
2918 0 ▶ if (result) {
Branch: 1 branch
BranchTrueFalseTotal
if (result) { 0 0 0
2919 0 deleted++;
2920 } else {
2921 0 ▶ if (write_pos != i) {
Branch: 1 branch
BranchTrueFalseTotal
if (write_pos != i) { 0 0 0
2922 0 nh->data[write_pos] = nh->data[i];
2923 }
2924 0 write_pos++;
2925 }
2926 }
2927  
2928 0 nh->size = write_pos;
2929  
2930 0 ▶ if (deleted > 0 && nh->size > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (deleted > 0 && nh->size > 1) { 0 0 0
if (deleted > 0 && nh->size > 1) { 0 0 0
2931 IV j;
2932 0 ▶ for (j = (nh->size >> 1) - 1; j >= 0; j--) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = (nh->size >> 1) - 1; j >= 0; j--) { 0 0 0
2933 0 ▶ if (nh->type == HEAP_MIN)
Branch: 1 branch
BranchTrueFalseTotal
if (nh->type == HEAP_MIN) 0 0 0
2934 0 nv_sift_down_min(nh, j);
2935 else
2936 0 nv_sift_down_max(nh, j);
2937 }
2938 }
2939  
2940 0 XSRETURN_IV(deleted);
2941 }
2942  
2943 {
2944 1 Heap *h = hl.ptr.heap;
2945  
2946 5 ▶ for (i = 0; i < h->size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < h->size; i++) { 4 1 5
2947 4 dSP;
2948 IV result;
2949 int count;
2950  
2951 4 ENTER; SAVETMPS;
2952 4 SAVE_DEFSV;
2953 4 DEFSV_set(h->data[i]);
2954  
2955 4 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 4 4
2956 4 ▶ XPUSHs(h->data[i]);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(h->data[i]); 0 4 4
2957 4 PUTBACK;
2958  
2959 4 count = call_sv(callback, G_SCALAR);
2960 4 SPAGAIN;
2961 4 ▶ result = count > 0 ? SvTRUE(TOPs) : 0;
Branch: 1 branch
BranchTrueFalseTotal
result = count > 0 ? SvTRUE(TOPs) : 0; 4 0 4
2962 4 ▶ if (count > 0) POPs;
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) POPs; 4 0 4
2963 4 PUTBACK;
2964 4 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 4 0 4
2965  
2966 4 ▶ if (result) {
Branch: 1 branch
BranchTrueFalseTotal
if (result) { 1 3 4
2967 1 SvREFCNT_dec(h->data[i]);
2968 1 deleted++;
2969 } else {
2970 3 ▶ if (write_pos != i) {
Branch: 1 branch
BranchTrueFalseTotal
if (write_pos != i) { 0 3 3
2971 0 h->data[write_pos] = h->data[i];
2972 0 h->priorities[write_pos] = h->priorities[i];
2973 }
2974 3 write_pos++;
2975 }
2976 }
2977  
2978 1 h->size = write_pos;
2979  
2980 1 ▶ if (deleted > 0 && h->size > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (deleted > 0 && h->size > 1) { 1 0 1
if (deleted > 0 && h->size > 1) { 1 0 1
2981 IV j;
2982 2 ▶ for (j = (h->size >> 1) - 1; j >= 0; j--) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = (h->size >> 1) - 1; j >= 0; j--) { 1 1 2
2983 1 h->sift_down(aTHX_ h, j);
2984 }
2985 }
2986  
2987 1 XSRETURN_IV(deleted);
2988 }
2989 }
2990  
2991 /* Import functions */
2992 28 static void install_heap_func_1arg(pTHX_ const char *pkg, const char *name,
2993 XSUBADDR_t xsub, heap_ppfunc ppfunc) {
2994 char full_name[256];
2995 CV *cv;
2996 SV *ckobj;
2997  
2998 28 snprintf(full_name, sizeof(full_name), "%s::%s", pkg, name);
2999 28 cv = newXS(full_name, xsub, __FILE__);
3000 28 ckobj = newSViv(PTR2IV(ppfunc));
3001 28 cv_set_call_checker(cv, heap_call_checker_1arg, ckobj);
3002 28 }
3003  
3004 18 static void install_heap_func_2arg(pTHX_ const char *pkg, const char *name,
3005 XSUBADDR_t xsub, heap_ppfunc ppfunc) {
3006 char full_name[256];
3007 CV *cv;
3008 SV *ckobj;
3009  
3010 18 snprintf(full_name, sizeof(full_name), "%s::%s", pkg, name);
3011 18 cv = newXS(full_name, xsub, __FILE__);
3012 18 ckobj = newSViv(PTR2IV(ppfunc));
3013 18 cv_set_call_checker(cv, heap_call_checker_2arg, ckobj);
3014 18 }
3015  
3016 20 XS_EXTERNAL(XS_heap_import) {
3017 20 dXSARGS;
3018 const char *pkg;
3019 int i;
3020 20 bool want_import = FALSE;
3021 20 bool want_raw = FALSE;
3022  
3023 20 ▶ pkg = CopSTASHPV(PL_curcop);
Branch: 7 branches
BranchTrueFalseTotal
pkg = CopSTASHPV(PL_curcop); 20 0 20
pkg = CopSTASHPV(PL_curcop); 20 0 20
pkg = CopSTASHPV(PL_curcop); 20 0 20
pkg = CopSTASHPV(PL_curcop); 0 20 20
pkg = CopSTASHPV(PL_curcop); 0 0 0
pkg = CopSTASHPV(PL_curcop); 20 0 20
pkg = CopSTASHPV(PL_curcop); 0 20 20
3024  
3025 25 ▶ for (i = 1; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) { 5 20 25
3026 STRLEN len;
3027 5 const char *arg = SvPV(ST(i), len);
3028 5 ▶ if (len == 6 && strEQ(arg, "import")) {
Branch: 2 branches
BranchTrueFalseTotal
if (len == 6 && strEQ(arg, "import")) { 4 1 5
if (len == 6 && strEQ(arg, "import")) { 4 0 4
3029 4 want_import = TRUE;
3030 1 ▶ } else if (len == 3 && strEQ(arg, "raw")) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (len == 3 && strEQ(arg, "raw")) { 1 0 1
} else if (len == 3 && strEQ(arg, "raw")) { 1 0 1
3031 1 want_raw = TRUE;
3032 }
3033 }
3034  
3035 20 ▶ if (want_import) {
Branch: 1 branch
BranchTrueFalseTotal
if (want_import) { 4 16 20
3036 4 install_heap_func_2arg(aTHX_ pkg, "heap_push", XS_heap_push, pp_heap_func_push);
3037 4 install_heap_func_1arg(aTHX_ pkg, "heap_pop", XS_heap_pop, pp_heap_func_pop);
3038 4 install_heap_func_1arg(aTHX_ pkg, "heap_peek", XS_heap_peek, pp_heap_func_peek);
3039 4 install_heap_func_1arg(aTHX_ pkg, "heap_size", XS_heap_size, pp_heap_func_size);
3040 4 install_heap_func_2arg(aTHX_ pkg, "heap_peek_n", XS_heap_peek_n, pp_heap_func_peek_n);
3041 4 install_heap_func_2arg(aTHX_ pkg, "heap_search", XS_heap_search, pp_heap_func_search);
3042 4 install_heap_func_2arg(aTHX_ pkg, "heap_delete", XS_heap_delete, pp_heap_func_delete);
3043 4 install_heap_func_1arg(aTHX_ pkg, "heap_is_empty", XS_heap_is_empty, pp_heap_func_is_empty);
3044 4 install_heap_func_1arg(aTHX_ pkg, "heap_clear", XS_heap_clear, pp_heap_func_clear);
3045 4 install_heap_func_1arg(aTHX_ pkg, "heap_type", XS_heap_type, pp_heap_func_type);
3046 }
3047  
3048 20 ▶ if (want_raw) {
Branch: 1 branch
BranchTrueFalseTotal
if (want_raw) { 1 19 20
3049 /* Install raw array functions with custom ops */
3050 1 install_heap_func_2arg(aTHX_ pkg, "push_heap_min", XS_push_heap_min, pp_push_heap_min);
3051 1 install_heap_func_1arg(aTHX_ pkg, "pop_heap_min", XS_pop_heap_min, pp_pop_heap_min);
3052 1 install_heap_func_2arg(aTHX_ pkg, "push_heap_max", XS_push_heap_max, pp_push_heap_max);
3053 1 install_heap_func_1arg(aTHX_ pkg, "pop_heap_max", XS_pop_heap_max, pp_pop_heap_max);
3054 1 install_heap_func_1arg(aTHX_ pkg, "make_heap_min", XS_make_heap_min, pp_make_heap_min);
3055 1 install_heap_func_1arg(aTHX_ pkg, "make_heap_max", XS_make_heap_max, pp_make_heap_max);
3056 }
3057  
3058 /* Mark $a and $b as multi-use in the caller's package to suppress
3059 "used only once" warnings when they appear in comparator subs */
3060 {
3061 20 GV *gv_a = gv_fetchpv(Perl_form(aTHX_ "%s::a", pkg), GV_ADD, SVt_PV);
3062 20 GV *gv_b = gv_fetchpv(Perl_form(aTHX_ "%s::b", pkg), GV_ADD, SVt_PV);
3063 20 GvMULTI_on(gv_a);
3064 20 GvMULTI_on(gv_b);
3065 }
3066  
3067 20 XSRETURN_EMPTY;
3068 }
3069  
3070 /* ============================================
3071 Boot function
3072 ============================================ */
3073  
3074 17 XS_EXTERNAL(boot_Heap__PQ) {
3075 17 dXSBOOTARGSXSAPIVERCHK;
3076 PERL_UNUSED_VAR(items);
3077  
3078 /* Register custom ops */
3079 17 XopENTRY_set(&heap_func_push_xop, xop_name, "heap_func_push");
3080 17 XopENTRY_set(&heap_func_push_xop, xop_desc, "heap function push");
3081 17 Perl_custom_op_register(aTHX_ pp_heap_func_push, &heap_func_push_xop);
3082  
3083 17 XopENTRY_set(&heap_func_pop_xop, xop_name, "heap_func_pop");
3084 17 XopENTRY_set(&heap_func_pop_xop, xop_desc, "heap function pop");
3085 17 Perl_custom_op_register(aTHX_ pp_heap_func_pop, &heap_func_pop_xop);
3086  
3087 17 XopENTRY_set(&heap_func_peek_xop, xop_name, "heap_func_peek");
3088 17 XopENTRY_set(&heap_func_peek_xop, xop_desc, "heap function peek");
3089 17 Perl_custom_op_register(aTHX_ pp_heap_func_peek, &heap_func_peek_xop);
3090  
3091 17 XopENTRY_set(&heap_func_size_xop, xop_name, "heap_func_size");
3092 17 XopENTRY_set(&heap_func_size_xop, xop_desc, "heap function size");
3093 17 Perl_custom_op_register(aTHX_ pp_heap_func_size, &heap_func_size_xop);
3094  
3095 /* Dedicated NV heap custom ops */
3096 17 XopENTRY_set(&nv_push_xop, xop_name, "nv_push");
3097 17 XopENTRY_set(&nv_push_xop, xop_desc, "nv heap push");
3098 17 Perl_custom_op_register(aTHX_ pp_nv_push, &nv_push_xop);
3099  
3100 /* Raw array custom ops */
3101 17 XopENTRY_set(&push_heap_min_xop, xop_name, "push_heap_min");
3102 17 XopENTRY_set(&push_heap_min_xop, xop_desc, "raw array push min");
3103 17 Perl_custom_op_register(aTHX_ pp_push_heap_min, &push_heap_min_xop);
3104  
3105 17 XopENTRY_set(&push_heap_max_xop, xop_name, "push_heap_max");
3106 17 XopENTRY_set(&push_heap_max_xop, xop_desc, "raw array push max");
3107 17 Perl_custom_op_register(aTHX_ pp_push_heap_max, &push_heap_max_xop);
3108  
3109 17 XopENTRY_set(&pop_heap_min_xop, xop_name, "pop_heap_min");
3110 17 XopENTRY_set(&pop_heap_min_xop, xop_desc, "raw array pop min");
3111 17 Perl_custom_op_register(aTHX_ pp_pop_heap_min, &pop_heap_min_xop);
3112  
3113 17 XopENTRY_set(&pop_heap_max_xop, xop_name, "pop_heap_max");
3114 17 XopENTRY_set(&pop_heap_max_xop, xop_desc, "raw array pop max");
3115 17 Perl_custom_op_register(aTHX_ pp_pop_heap_max, &pop_heap_max_xop);
3116  
3117 17 XopENTRY_set(&make_heap_min_xop, xop_name, "make_heap_min");
3118 17 XopENTRY_set(&make_heap_min_xop, xop_desc, "raw array make min");
3119 17 Perl_custom_op_register(aTHX_ pp_make_heap_min, &make_heap_min_xop);
3120  
3121 17 XopENTRY_set(&make_heap_max_xop, xop_name, "make_heap_max");
3122 17 XopENTRY_set(&make_heap_max_xop, xop_desc, "raw array make max");
3123 17 Perl_custom_op_register(aTHX_ pp_make_heap_max, &make_heap_max_xop);
3124  
3125 17 XopENTRY_set(&nv_pop_xop, xop_name, "nv_pop");
3126 17 XopENTRY_set(&nv_pop_xop, xop_desc, "nv heap pop");
3127 17 Perl_custom_op_register(aTHX_ pp_nv_pop, &nv_pop_xop);
3128  
3129 17 XopENTRY_set(&nv_peek_xop, xop_name, "nv_peek");
3130 17 XopENTRY_set(&nv_peek_xop, xop_desc, "nv heap peek");
3131 17 Perl_custom_op_register(aTHX_ pp_nv_peek, &nv_peek_xop);
3132  
3133 17 XopENTRY_set(&nv_size_xop, xop_name, "nv_size");
3134 17 XopENTRY_set(&nv_size_xop, xop_desc, "nv heap size");
3135 17 Perl_custom_op_register(aTHX_ pp_nv_size, &nv_size_xop);
3136  
3137 17 XopENTRY_set(&nv_peek_n_xop, xop_name, "nv_peek_n");
3138 17 XopENTRY_set(&nv_peek_n_xop, xop_desc, "nv heap peek_n");
3139 17 Perl_custom_op_register(aTHX_ pp_nv_peek_n, &nv_peek_n_xop);
3140  
3141 17 XopENTRY_set(&heap_func_peek_n_xop, xop_name, "heap_func_peek_n");
3142 17 XopENTRY_set(&heap_func_peek_n_xop, xop_desc, "heap function peek_n");
3143 17 Perl_custom_op_register(aTHX_ pp_heap_func_peek_n, &heap_func_peek_n_xop);
3144  
3145 17 XopENTRY_set(&heap_func_search_xop, xop_name, "heap_func_search");
3146 17 XopENTRY_set(&heap_func_search_xop, xop_desc, "heap function search");
3147 17 Perl_custom_op_register(aTHX_ pp_heap_func_search, &heap_func_search_xop);
3148  
3149 17 XopENTRY_set(&heap_func_delete_xop, xop_name, "heap_func_delete");
3150 17 XopENTRY_set(&heap_func_delete_xop, xop_desc, "heap function delete");
3151 17 Perl_custom_op_register(aTHX_ pp_heap_func_delete, &heap_func_delete_xop);
3152  
3153 17 XopENTRY_set(&nv_search_xop, xop_name, "nv_search");
3154 17 XopENTRY_set(&nv_search_xop, xop_desc, "nv heap search");
3155 17 Perl_custom_op_register(aTHX_ pp_nv_search, &nv_search_xop);
3156  
3157 17 XopENTRY_set(&nv_delete_xop, xop_name, "nv_delete");
3158 17 XopENTRY_set(&nv_delete_xop, xop_desc, "nv heap delete");
3159 17 Perl_custom_op_register(aTHX_ pp_nv_delete, &nv_delete_xop);
3160  
3161 17 XopENTRY_set(&heap_func_is_empty_xop, xop_name, "heap_func_is_empty");
3162 17 XopENTRY_set(&heap_func_is_empty_xop, xop_desc, "heap function is_empty");
3163 17 Perl_custom_op_register(aTHX_ pp_heap_func_is_empty, &heap_func_is_empty_xop);
3164  
3165 17 XopENTRY_set(&heap_func_clear_xop, xop_name, "heap_func_clear");
3166 17 XopENTRY_set(&heap_func_clear_xop, xop_desc, "heap function clear");
3167 17 Perl_custom_op_register(aTHX_ pp_heap_func_clear, &heap_func_clear_xop);
3168  
3169 17 XopENTRY_set(&heap_func_type_xop, xop_name, "heap_func_type");
3170 17 XopENTRY_set(&heap_func_type_xop, xop_desc, "heap function type");
3171 17 Perl_custom_op_register(aTHX_ pp_heap_func_type, &heap_func_type_xop);
3172  
3173 17 XopENTRY_set(&nv_is_empty_xop, xop_name, "nv_is_empty");
3174 17 XopENTRY_set(&nv_is_empty_xop, xop_desc, "nv heap is_empty");
3175 17 Perl_custom_op_register(aTHX_ pp_nv_is_empty, &nv_is_empty_xop);
3176  
3177 17 XopENTRY_set(&nv_clear_xop, xop_name, "nv_clear");
3178 17 XopENTRY_set(&nv_clear_xop, xop_desc, "nv heap clear");
3179 17 Perl_custom_op_register(aTHX_ pp_nv_clear, &nv_clear_xop);
3180  
3181 /* Register XS subs with call checkers */
3182 {
3183 CV *cv;
3184 SV *ckobj;
3185  
3186 /* Standard heap */
3187 17 newXS("Heap::PQ::new", XS_heap_new, __FILE__);
3188  
3189 17 cv = newXS("Heap::PQ::push", XS_heap_push, __FILE__);
3190 17 ckobj = newSViv(PTR2IV(pp_heap_func_push));
3191 17 cv_set_call_checker(cv, heap_call_checker_2arg, ckobj);
3192  
3193 17 newXS("Heap::PQ::push_all", XS_heap_push_all, __FILE__);
3194  
3195 17 cv = newXS("Heap::PQ::pop", XS_heap_pop, __FILE__);
3196 17 ckobj = newSViv(PTR2IV(pp_heap_func_pop));
3197 17 cv_set_call_checker(cv, heap_call_checker_1arg, ckobj);
3198  
3199 17 cv = newXS("Heap::PQ::peek", XS_heap_peek, __FILE__);
3200 17 ckobj = newSViv(PTR2IV(pp_heap_func_peek));
3201 17 cv_set_call_checker(cv, heap_call_checker_1arg, ckobj);
3202  
3203 17 cv = newXS("Heap::PQ::size", XS_heap_size, __FILE__);
3204 17 ckobj = newSViv(PTR2IV(pp_heap_func_size));
3205 17 cv_set_call_checker(cv, heap_call_checker_1arg, ckobj);
3206  
3207 17 cv = newXS("Heap::PQ::is_empty", XS_heap_is_empty, __FILE__);
3208 17 ckobj = newSViv(PTR2IV(pp_heap_func_is_empty));
3209 17 cv_set_call_checker(cv, heap_call_checker_1arg, ckobj);
3210  
3211 17 cv = newXS("Heap::PQ::clear", XS_heap_clear, __FILE__);
3212 17 ckobj = newSViv(PTR2IV(pp_heap_func_clear));
3213 17 cv_set_call_checker(cv, heap_call_checker_1arg, ckobj);
3214  
3215 17 cv = newXS("Heap::PQ::type", XS_heap_type, __FILE__);
3216 17 ckobj = newSViv(PTR2IV(pp_heap_func_type));
3217 17 cv_set_call_checker(cv, heap_call_checker_1arg, ckobj);
3218  
3219 17 cv = newXS("Heap::PQ::peek_n", XS_heap_peek_n, __FILE__);
3220 17 ckobj = newSViv(PTR2IV(pp_heap_func_peek_n));
3221 17 cv_set_call_checker(cv, heap_call_checker_2arg, ckobj);
3222  
3223 17 cv = newXS("Heap::PQ::search", XS_heap_search, __FILE__);
3224 17 ckobj = newSViv(PTR2IV(pp_heap_func_search));
3225 17 cv_set_call_checker(cv, heap_call_checker_2arg, ckobj);
3226  
3227 17 cv = newXS("Heap::PQ::delete", XS_heap_delete, __FILE__);
3228 17 ckobj = newSViv(PTR2IV(pp_heap_func_delete));
3229 17 cv_set_call_checker(cv, heap_call_checker_2arg, ckobj);
3230  
3231 17 newXS("Heap::PQ::import", XS_heap_import, __FILE__);
3232  
3233 /* Numeric heap */
3234 17 newXS("Heap::PQ::new_nv", XS_heap_new_nv, __FILE__);
3235  
3236 17 cv = newXS("Heap::PQ::nv::push", XS_nv_push, __FILE__);
3237 17 ckobj = newSViv(PTR2IV(pp_nv_push));
3238 17 cv_set_call_checker(cv, heap_call_checker_2arg, ckobj);
3239  
3240 17 newXS("Heap::PQ::nv::push_all", XS_nv_push_all, __FILE__);
3241  
3242 17 cv = newXS("Heap::PQ::nv::pop", XS_nv_pop, __FILE__);
3243 17 ckobj = newSViv(PTR2IV(pp_nv_pop));
3244 17 cv_set_call_checker(cv, heap_call_checker_1arg, ckobj);
3245  
3246 17 cv = newXS("Heap::PQ::nv::peek", XS_nv_peek, __FILE__);
3247 17 ckobj = newSViv(PTR2IV(pp_nv_peek));
3248 17 cv_set_call_checker(cv, heap_call_checker_1arg, ckobj);
3249  
3250 17 cv = newXS("Heap::PQ::nv::size", XS_nv_size, __FILE__);
3251 17 ckobj = newSViv(PTR2IV(pp_nv_size));
3252 17 cv_set_call_checker(cv, heap_call_checker_1arg, ckobj);
3253  
3254 17 cv = newXS("Heap::PQ::nv::is_empty", XS_nv_is_empty, __FILE__);
3255 17 ckobj = newSViv(PTR2IV(pp_nv_is_empty));
3256 17 cv_set_call_checker(cv, heap_call_checker_1arg, ckobj);
3257  
3258 17 cv = newXS("Heap::PQ::nv::clear", XS_nv_clear, __FILE__);
3259 17 ckobj = newSViv(PTR2IV(pp_nv_clear));
3260 17 cv_set_call_checker(cv, heap_call_checker_1arg, ckobj);
3261  
3262 17 cv = newXS("Heap::PQ::nv::peek_n", XS_nv_peek_n, __FILE__);
3263 17 ckobj = newSViv(PTR2IV(pp_nv_peek_n));
3264 17 cv_set_call_checker(cv, heap_call_checker_2arg, ckobj);
3265  
3266 17 cv = newXS("Heap::PQ::nv::search", XS_nv_search, __FILE__);
3267 17 ckobj = newSViv(PTR2IV(pp_nv_search));
3268 17 cv_set_call_checker(cv, heap_call_checker_2arg, ckobj);
3269  
3270 17 cv = newXS("Heap::PQ::nv::delete", XS_nv_delete, __FILE__);
3271 17 ckobj = newSViv(PTR2IV(pp_nv_delete));
3272 17 cv_set_call_checker(cv, heap_call_checker_2arg, ckobj);
3273  
3274 /* Raw array functions */
3275 17 cv = newXS("Heap::PQ::push_heap_min", XS_push_heap_min, __FILE__);
3276 17 ckobj = newSViv(PTR2IV(pp_push_heap_min));
3277 17 cv_set_call_checker(cv, heap_call_checker_2arg, ckobj);
3278  
3279 17 cv = newXS("Heap::PQ::pop_heap_min", XS_pop_heap_min, __FILE__);
3280 17 ckobj = newSViv(PTR2IV(pp_pop_heap_min));
3281 17 cv_set_call_checker(cv, heap_call_checker_1arg, ckobj);
3282  
3283 17 cv = newXS("Heap::PQ::push_heap_max", XS_push_heap_max, __FILE__);
3284 17 ckobj = newSViv(PTR2IV(pp_push_heap_max));
3285 17 cv_set_call_checker(cv, heap_call_checker_2arg, ckobj);
3286  
3287 17 cv = newXS("Heap::PQ::pop_heap_max", XS_pop_heap_max, __FILE__);
3288 17 ckobj = newSViv(PTR2IV(pp_pop_heap_max));
3289 17 cv_set_call_checker(cv, heap_call_checker_1arg, ckobj);
3290  
3291 17 cv = newXS("Heap::PQ::make_heap_min", XS_make_heap_min, __FILE__);
3292 17 ckobj = newSViv(PTR2IV(pp_make_heap_min));
3293 17 cv_set_call_checker(cv, heap_call_checker_1arg, ckobj);
3294  
3295 17 cv = newXS("Heap::PQ::make_heap_max", XS_make_heap_max, __FILE__);
3296 17 ckobj = newSViv(PTR2IV(pp_make_heap_max));
3297 17 cv_set_call_checker(cv, heap_call_checker_1arg, ckobj);
3298 }
3299  
3300 #if PERL_VERSION_GE(5,22,0)
3301 17 Perl_xs_boot_epilog(aTHX_ ax);
3302 #else
3303 XSRETURN_YES;
3304 #endif
3305 17 }
↑ Summary blib/lib/Heap/PQ.pm »