FastPermute.xs

filter: Statementstmt 96.7% 60 / 62 Branchbran 81.5% 31 / 38 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 91.0% 91 / 100 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Algorithm/FastPermute.pm ↑ Summary
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4 #include "multicall.h"
5  
6 #ifndef SvUOK
7 # define SvUOK(sv) SvIOK_UV(sv)
8 #endif
9  
10 #ifndef cxinc
11 # define cxinc() my_cxinc(aTHX)
12 static I32
13 my_cxinc(pTHX)
14 {
15 cxstack_max = cxstack_max * 3 / 2;
16 Renew(cxstack, cxstack_max + 1, struct context);
17 return cxstack_ix + 1;
18 }
19 #endif
20  
21 /* This was changed by patch 24531 -- one of Nick's optimizations */
22 #if PERL_VERSION < 10
23 # define AvARRAY_set(av, val) ((XPVAV*) SvANY(av))->xav_array = (char*) val
24 #else
25 # define AvARRAY_set(av, val) av->sv_u.svu_array = (SV**) val
26 #endif
27  
28 void
29 46340 permute_engine(AV* av, SV** array, I32 level, I32 len, SV*** tmparea, OP* multicall_cop)
30 {
31 46340 SV** copy = tmparea[level];
32 46340 int index = level;
33 46340 bool calling = (index + 1 == len);
34 SV* tmp;
35
36 46340 Copy(array, copy, len, SV*);
37
38 46340 ▶ if (calling)
Branch: 1 branch
BranchTrueFalseTotal
if (calling) 40388 5952 46340
39 40388 AvARRAY_set(av, copy);
40  
41 do {
42 409526 ▶ if (calling) {
Branch: 1 branch
BranchTrueFalseTotal
if (calling) { 363194 46332 409526
43 363194 MULTICALL;
44 }
45 else {
46 46332 permute_engine(av, copy, level + 1, len, tmparea, multicall_cop);
47 }
48 409518 ▶ if (index != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index != 0) { 363186 46332 409518
49 363186 tmp = copy[index];
50 363186 copy[index] = copy[index - 1];
51 363186 copy[index - 1] = tmp;
52 }
53 409518 ▶ } while (index-- > 0);
Branch: 1 branch
BranchTrueFalseTotal
} while (index-- > 0); 363186 46332 409518
54 46332 }
55  
56 struct afp_cache {
57 SV*** tmparea;
58 AV* array;
59 I32 len;
60 SV** array_array;
61 U32 array_flags;
62 SSize_t array_fill;
63 SV** copy; /* Non-magical SV list for magical array */
64 };
65  
66 static
67 8 void afp_destructor(void *cache)
68 {
69 8 struct afp_cache *c = cache;
70 I32 x;
71
72 /* PerlIO_stdoutf("DESTROY!\n"); */
73  
74 47 ▶ for (x = c->len - 1; x >= 0; x--) free(c->tmparea[x]);
Branch: 1 branch
BranchTrueFalseTotal
for (x = c->len - 1; x >= 0; x--) free(c->tmparea[x]); 39 8 47
75 8 free(c->tmparea);
76 8 ▶ if (c->copy) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->copy) { 1 7 8
77 6 ▶ for (x=0; x < c->len; x++) SvREFCNT_dec(c->copy[x]);
Branch: 1 branch
BranchTrueFalseTotal
for (x=0; x < c->len; x++) SvREFCNT_dec(c->copy[x]); 5 1 6
78 1 free(c->copy);
79 }
80
81 8 AvARRAY_set(c->array, c->array_array);
82 8 SvFLAGS(c->array) = c->array_flags;
83 8 AvFILLp(c->array) = c->array_fill;
84
85 8 free(c);
86 8 }
87  
88  
89 MODULE = Algorithm::FastPermute PACKAGE = Algorithm::FastPermute
90  
91 void
92 permute(callback_sv, array_sv)
93 SV* callback_sv;
94 SV* array_sv;
95 PROTOTYPE: &\@
96 PREINIT:
97 dMULTICALL;
98 10 I32 gimme = G_VOID; /* We call our callback in VOID context */
99 bool old_catch;
100 struct afp_cache *c;
101 I32 x;
102 PPCODE:
103 10 ▶ if (!SvROK(callback_sv) || SvTYPE(SvRV(callback_sv)) != SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(callback_sv) || SvTYPE(SvRV(callback_sv)) != SVt_PVCV) 10 0 10
if (!SvROK(callback_sv) || SvTYPE(SvRV(callback_sv)) != SVt_PVCV) 0 10 10
104 0 Perl_croak(aTHX_ "Callback is not a CODE reference");
105 10 ▶ if (!SvROK(array_sv) || SvTYPE(SvRV(array_sv)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(array_sv) || SvTYPE(SvRV(array_sv)) != SVt_PVAV) 10 0 10
if (!SvROK(array_sv) || SvTYPE(SvRV(array_sv)) != SVt_PVAV) 0 10 10
106 0 Perl_croak(aTHX_ "Array is not an ARRAY reference");
107
108 10 c = malloc(sizeof(struct afp_cache));
109 10 cv = (CV*)SvRV(callback_sv);
110 10 c->array = (AV*)SvRV(array_sv);
111 10 c->len = 1 + av_len(c->array);
112
113 10 ▶ if (SvREADONLY(c->array))
Branch: 1 branch
BranchTrueFalseTotal
if (SvREADONLY(c->array)) 1 9 10
114 1 Perl_croak(aTHX_ "Can't permute a read-only array");
115
116 9 ▶ if (c->len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->len == 0) { 1 8 9
117 /* Should we warn here? */
118 1 free(c);
119 1 return;
120 }
121
122 8 c->array_array = AvARRAY(c->array);
123 8 c->array_flags = SvFLAGS(c->array);
124 8 c->array_fill = AvFILLp(c->array);
125  
126 /* Magical array. Realise it temporarily. */
127 8 ▶ if (SvRMAGICAL(c->array)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvRMAGICAL(c->array)) { 1 7 8
128 1 c->copy = (SV**) malloc (c->len * sizeof *(c->copy));
129 6 ▶ for (x=0; x < c->len; x++) {
Branch: 1 branch
BranchTrueFalseTotal
for (x=0; x < c->len; x++) { 5 1 6
130 5 SV **svp = av_fetch(c->array, x, FALSE);
131 5 ▶ c->copy[x] = (svp) ? SvREFCNT_inc(*svp) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
c->copy[x] = (svp) ? SvREFCNT_inc(*svp) : &PL_sv_undef; 5 0 5
132 }
133 1 SvRMAGICAL_off(c->array);
134 1 AvARRAY_set(c->array, c->copy);
135 1 AvFILLp(c->array) = c->len - 1;
136 }
137 else
138 7 c->copy = 0;
139
140 8 SvREADONLY_on(c->array); /* Can't change the array during permute */
141
142 /* Allocate memory for the engine to scribble on */
143 8 c->tmparea = (SV***) malloc( (c->len+1) * sizeof *(c->tmparea));
144 55 ▶ for (x = c->len; x >= 0; x--)
Branch: 1 branch
BranchTrueFalseTotal
for (x = c->len; x >= 0; x--) 47 8 55
145 47 c->tmparea[x] = malloc(c->len * sizeof **(c->tmparea));
146
147 /* Set up the context for the callback */
148 8 ▶ PUSH_MULTICALL(cv);
Branch: 1 branch
BranchTrueFalseTotal
PUSH_MULTICALL(cv); 0 8 8
149 8 save_destructor(afp_destructor, c);
150
151 8 permute_engine(c->array, AvARRAY(c->array), 0, c->len,
152 c->tmparea, multicall_cop);
153
154 6 ▶ POP_MULTICALL;
Branch: 1 branch
BranchTrueFalseTotal
POP_MULTICALL; 6 0 6
« blib/lib/Algorithm/FastPermute.pm ↑ Summary