lib/List/Flatten/XS.xs

filter: Statementstmt 98.3% 58 / 59 Branchbran 92.8% 39 / 42 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 96.0% 97 / 101 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/List/Flatten/XS.pm »
1 #ifdef __cplusplus
2 extern "C" {
3 #endif
4  
5 #define PERL_NO_GET_CONTEXT /* we want efficiency */
6 #include <EXTERN.h>
7 #include <perl.h>
8 #include <XSUB.h>
9  
10 #ifdef __cplusplus
11 } /* extern "C" */
12 #endif
13  
14 #define NEED_newSVpvn_flags
15 #include "ppport.h"
16  
17 #define IS_ARRAYREF(sv) SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV && !SvOBJECT(SvRV(sv))
18 #define AV_FETCH_MUST(ary, idx) *av_fetch(ary, idx, FALSE)
19  
20 #define AV_PUSH_INC(dest, val) \
21 av_push(dest, SvREFCNT_inc_NN(val)) \
22  
23 #define AV_UNSHIFT_ARRAYREF(dest, src) \
24 ({ \
25 AV *ary = (AV *)SvRV(src); \
26 IV l = av_len(ary) + 1; \
27 av_unshift(dest, l); \
28 SV *val; \
29 for (IV i = 0; i < l; i++) { \
30 val = AV_FETCH_MUST(ary, i); \
31 av_store(dest, i, SvREFCNT_inc_NN(val)); \
32 } \
33 })
34  
35 static SV *
36 32 _fast_flatten(pTHX_ SV *ref)
37 {
38 32 AV *args = (AV *)SvRV(ref);
39 32 AV *dest = (AV *)sv_2mortal((SV *)newAV());
40  
41 32 IV len = av_len(args) + 1;
42 10116 ▶ for (IV i = 0; i < len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (IV i = 0; i < len; i++) 10084 32 10116
43 10084 AV_PUSH_INC(dest, AV_FETCH_MUST(args, i));
44  
45 32 AV *result = (AV *)sv_2mortal((SV *)newAV());
46  
47 // This is to detect circular reference
48 32 HV *memo = (HV *)sv_2mortal((SV *)newHV());
49  
50 20632 ▶ while (av_len(dest) + 1) {
Branch: 1 branch
BranchTrueFalseTotal
while (av_len(dest) + 1) { 20605 27 20632
51 20605 SV *tmp = av_shift(dest);
52 20605 ▶ if (hv_exists_ent(memo, tmp, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists_ent(memo, tmp, 0)) { 5 20600 20605
53 5 Perl_croak(aTHX_ "tried to flatten recursive list(circular references)");
54 }
55 20600 ▶ if (IS_ARRAYREF(tmp)) {
Branch: 3 branches
BranchTrueFalseTotal
if (IS_ARRAYREF(tmp)) { 275 20325 20600
if (IS_ARRAYREF(tmp)) { 230 45 275
if (IS_ARRAYREF(tmp)) { 218 12 230
56 // store the pointer of array reference
57 218 hv_store_ent(memo, tmp, &PL_sv_undef, 0);
58 10760 ▶ AV_UNSHIFT_ARRAYREF(dest, tmp);
Branch: 1 branch
BranchTrueFalseTotal
AV_UNSHIFT_ARRAYREF(dest, tmp); 10542 218 10760
59 } else {
60 20382 AV_PUSH_INC(result, tmp);
61 }
62 }
63  
64 27 return sv_2mortal(newRV_inc((SV *)result));
65 }
66  
67 static SV *
68 73 _flatten_per_level(pTHX_ SV *ref, IV level)
69 {
70 73 AV *stack = (AV *)sv_2mortal((SV *)newAV());
71 73 AV *result = (AV *)sv_2mortal((SV *)newAV());
72  
73 // This is to detect circular reference
74 73 HV *memo = (HV *)sv_2mortal((SV *)newHV());
75  
76 73 IV i = 0;
77 SV *tmp;
78 73 AV *ary = (AV *)SvRV(ref);
79 347 while (1) {
80 21487 ▶ while (i < av_len(ary) + 1) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < av_len(ary) + 1) { 21072 415 21487
81 21072 tmp = AV_FETCH_MUST(ary, i++);
82 21072 ▶ if ((av_len(stack) + 1) / 2 >= level) {
Branch: 1 branch
BranchTrueFalseTotal
if ((av_len(stack) + 1) / 2 >= level) { 10218 10854 21072
83 10218 AV_PUSH_INC(result, tmp);
84 10218 continue;
85 }
86  
87 10854 ▶ if (IS_ARRAYREF(tmp)) {
Branch: 3 branches
BranchTrueFalseTotal
if (IS_ARRAYREF(tmp)) { 434 10420 10854
if (IS_ARRAYREF(tmp)) { 378 56 434
if (IS_ARRAYREF(tmp)) { 377 1 378
88 377 ▶ if (hv_exists_ent(memo, tmp, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists_ent(memo, tmp, 0)) { 5 372 377
89 5 SvREFCNT_inc(stack);
90 5 Perl_croak(aTHX_ "tried to flatten recursive list(circular references)");
91 }
92 // store the pointer of array reference
93 372 hv_store_ent(memo, tmp, &PL_sv_undef, 0);
94  
95 // push value to the stack
96 372 av_push(stack, (SV *)ary);
97 372 av_push(stack, sv_2mortal(newSViv(i)));
98 372 ary = (AV *)SvRV(tmp);
99 372 i = 0;
100 } else {
101 10477 AV_PUSH_INC(result, tmp);
102 }
103 }
104  
105 415 ▶ if (av_len(stack) + 1 == 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(stack) + 1 == 0) break; 68 347 415
106
107 347 SV *idx = av_pop(stack);
108 347 i = SvIV(idx);
109 347 SV *poped = av_pop(stack);
110 347 ary = (AV *)poped; // Already done SvRV(SV *)
111 }
112  
113 68 return sv_2mortal(newRV_inc((SV *)result));
114 }
115  
116 MODULE = List::Flatten::XS PACKAGE = List::Flatten::XS
117 PROTOTYPES: DISABLE
118  
119 void *
120 flatten(ref, svlevel = sv_2mortal(newSViv(-1)))
121 SV *ref;
122 SV *svlevel;
123 PPCODE:
124 {
125 105 ▶ if (!SvROK(ref) || SvTYPE(SvRV(ref)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(ref) || SvTYPE(SvRV(ref)) != SVt_PVAV) 105 0 105
if (!SvROK(ref) || SvTYPE(SvRV(ref)) != SVt_PVAV) 0 105 105
126 0 Perl_croak(aTHX_ "Please pass an array reference to the first argument");
127
128 105 IV level = SvIV(svlevel);
129 32 SV *result = (level < 0) ? _fast_flatten(aTHX_ ref)
130 105 ▶ : _flatten_per_level(aTHX_ ref, level);
Branch: 1 branch
BranchTrueFalseTotal
: _flatten_per_level(aTHX_ ref, level); 32 73 105
131  
132 95 ▶ if (GIMME_V == G_ARRAY) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_ARRAY) { 48 47 95
133 48 AV *av_result = (AV *)SvRV(result);
134 48 IV len = av_len(av_result) + 1;
135 48 ▶ EXTEND(SP, len);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, len); 48 0 48
EXTEND(SP, len); 1 47 48
136 40466 ▶ for (IV i = 0; i < len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (IV i = 0; i < len; i++) 40418 48 40466
137 40418 ST(i) = AV_FETCH_MUST(av_result, i);
138 48 XSRETURN(len);
139 }
140  
141 47 ST(0) = result;
142 47 XSRETURN(1);
143 }
↑ Summary blib/lib/List/Flatten/XS.pm »