blib/lib/PDL/PP/PdlParObj.pm

filter: Statementstmt 97.7% 129 / 132 Branchbran 78.3% 47 / 60 Conditioncond 63.6% 14 / 22 MC/DCmcdc 28.5% 4 / 14 Subroutinesub 100% 21 / 21 Podpod 0.0% 0 / 17 totaltotal 80.8% 215 / 266 CC 43 SCAR 19.3
CC
43
Cov
89%
new
15.2
adjusted_type
8.8
perldimcheck
6.1
CRAP 6.9
« lib/PDL/Bad.pd ↑ Summary blib/lib/PDL/PP/CType.pm »
1 package # hide from PAUSE/MetaCPAN
2 PDL::PP::PdlParObj;
3  
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use PDL::Types ':All';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 our %INVALID_PAR = map +($_=>1), qw(
10 I
11 );
12  
13 6 my $typeregex = join '|', map $_->ppforcetype, types;
14 6 my $complex_regex = join '|', qw(real complex !real !complex);
15 6 our $sqbr_re = qr/\[([^]]*)\]/x;
16 6 our $pars_re = qr/^
17 \s*(?:($complex_regex|$typeregex)\b([+]*)|)\s* # $1,2: first option then plus
18 (?:$sqbr_re)?\s* # $3: The initial [option] part
19 (\w+) # $4: The name
20 \((.*)\) # $5: The indices
21 \s*\Z # that's all
22 /xs;
23 6 my %flag2info = (
24 io => [[qw(FlagW)]],
25 o => [[qw(FlagOut FlagCreat FlagW)]],
26 oca => [[qw(FlagOut FlagCreat FlagW FlagCreateAlways)]],
27 t => [[qw(FlagTemp FlagCreat FlagW)]],
28 phys => [[qw(FlagPhys)]],
29 real => [[qw(FlagTypeOverride FlagReal)]],
30 complex => [[qw(FlagTypeOverride FlagComplex)]],
31 '!real' => [[qw(FlagTypeOverride FlagNotReal)]],
32 '!complex' => [[qw(FlagTypeOverride FlagNotComplex)]],
33 (map +($_->ppforcetype => [[qw(FlagTypeOverride FlagTyped)], 'Type']), types),
34 );
35 6 my %flag2c = qw(
36 FlagReal PDL_PARAM_ISREAL
37 FlagComplex PDL_PARAM_ISCOMPLEX
38 FlagNotReal PDL_PARAM_ISNOTREAL
39 FlagNotComplex PDL_PARAM_ISNOTCOMPLEX
40 FlagTyped PDL_PARAM_ISTYPED
41 FlagTplus PDL_PARAM_ISTPLUS
42 FlagCreat PDL_PARAM_ISCREAT
43 FlagCreateAlways PDL_PARAM_ISCREATEALWAYS
44 FlagOut PDL_PARAM_ISOUT
45 FlagTemp PDL_PARAM_ISTEMP
46 FlagW PDL_PARAM_ISWRITE
47 FlagPhys PDL_PARAM_ISPHYS
48 FlagIgnore PDL_PARAM_ISIGNORE
49 );
50 6 my $calc_re = qr{
51 (\w+)\s*=\s* # paren group 1 (dim name) - from perlre/PARNO
52 CALC
53 ( # paren group 2 (parens)
54 \(
55 ( # paren group 3 (contents of parens)
56 (?:
57 (?> [^()]+ ) # Non-parens without backtracking
58 |
59 (?2) # Recurse to start of paren group 2
60 )*
61 )
62 \)
63 )
64 }xo;
65 179 ▶sub new {
Subroutine new: called CC=15 SCAR=27.2
Pod undocumented
66 179 my ($type,$string,$opname) = @_;
67 179 ▶ $string =~ $pars_re or croak "pp_def($opname): Invalid pdl def $string (regex $pars_re)\n";
Branch: 1 branch
BranchTrueFalseTotal
$string =~ /$PDL::PP::PdlParObj::pars_re/sx or croak("pp_def($opname): Invalid pdl def $string (regex $PDL::PP::PdlParObj::pars_re)\n") 179 0 179
68 179 my $this = bless {Number => "PDL_UNDEF_NUMBER", OpName=>$opname},$type;
69 179 ▶ my($opt1,$opt_plus,$sqbr_opt,$name,$inds) = map $_ // '', $1,$2,$3,$4,$5;
Condition100%2/2
$_ // ""
l!l
477418
MC/DC100%1/1
$_ // ""
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
70 179 $this->{OrigInds} = $inds;
71 179 ▶ print "PDL: '$opt1$opt_plus', '$sqbr_opt', '$name', '$inds'\n"
Branch: 1 branch
BranchTrueFalseTotal
if $main::PP_VERBOSE 0 179 179
72 if $::PP_VERBOSE;
73 179 ▶ croak "pp_def($opname): Invalid Pars name: $name" if $INVALID_PAR{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $PDL::PP::PdlParObj::INVALID_PAR{$name} 1 178 179
74 # Set my internal variables
75 178 $this->{Name} = $name;
76 178 ▶ $this->{Flags} = [(split ',',$sqbr_opt),($opt1?$opt1:())];
Branch: 1 branch
BranchTrueFalseTotal
$opt1 ? : 21 157 178
77 178 for(@{$this->{Flags}}) {
78 croak("pp_def($opname): Invalid flag $_ given for $string\n")
79 105 ▶ unless my ($set, $store) = @{ $flag2info{$_} || [] };
Branch: 1 branch
BranchTrueFalseTotal
unless () = @{$flag2info{$_} or [];} 0 105 105
Condition50%1/2
$flag2info{$_} || []
l!l
1050
MC/DC0%0/1
$flag2info{$_} || []
$flag2info{$_}
Truth table50%1/2
A
A= $flag2info{$_}
Aresult
11
00
80 105 ▶ $this->{$store} = $_ if $store;
Branch: 1 branch
BranchTrueFalseTotal
if $store 21 84 105
81 105 $this->{$_} = 1 for @$set;
82 }
83 178 ▶ $this->{FlagTplus} = 1 if $this->{FlagTyped} && $opt_plus;
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"FlagTyped"} and $opt_plus 2 176 178
Condition100%3/3
$this->{"FlagTyped"} and $opt_plus
!ll&&!rl&&r
157192
MC/DC100%2/2
$this->{"FlagTyped"} and $opt_plus
$this->{"FlagTyped"} $opt_plus
Truth table100%3/3
A and B
A= $this->{"FlagTyped"}B= $opt_plus
ABresult
0X0
100
111
84 178 ▶ $this->{Type} &&= PDL::Type->new($this->{Type});
Condition66%2/3
$this->{"Type"} &&= "PDL::Type"->new($this->{"Type"})
!ll&&!rl&&r
157021
MC/DC50%1/2
$this->{"Type"} &&= "PDL::Type"->new($this->{"Type"})
$this->{"Type"} "PDL::Type"->new($this->{"Type"})
Truth table66%2/3
A &&= B
A= $this->{"Type"}B= "PDL::Type"->new($this->{"Type"})
ABresult
0X0
100
111
85 178 $this->{Ind2Calc} = \my %ind2calc;
86 178 ▶ $ind2calc{$1} = $3 while $inds =~ s#$calc_re#$1#;
Branch: 1 branch
BranchTrueFalseTotal
$inds =~ s/$calc_re/$1/x and do { $ind2calc{$1} = $3 } 1 178 179
87 $this->{RawInds} = [map{
88 178 s/\s//g; # Remove spaces
89 139 $_;
90 } split ',', $inds];
91 178 return $this;
92 }
93  
94 70 ▶sub cflags {
Subroutine cflags: called CC=1 SCAR=0.0
Pod undocumented
95 70 my ($this) = @_;
96 70 map $flag2c{$_}, grep $this->{$_}, sort keys %flag2c;
97 }
98  
99 726 ▶sub name {$_[0]{Name}}
Subroutine name: called CC=1 SCAR=0.0
Pod undocumented
100  
101 208 ▶sub add_inds {
Subroutine add_inds: called CC=1 SCAR=0.0
Pod undocumented
102 208 my($this,$dimsobj) = @_;
103 208 $this->{IndObjs} = [my @objs = map $dimsobj->get_indobj_make($_, $this->{Ind2Calc}{$_}, $this->{OrigInds}, $this->{Name}), @{$this->{RawInds}}];
104 208 my %indcount;
105 208 $this->{IndCounts} = [ map 0+($indcount{$_->name}++), @objs ];
106 208 $this->{IndTotCounts} = [ map $indcount{$_->name}, @objs ];
107 }
108  
109  
110 # do the dimension checking for perl level broadcasting
111 # assumes that IndObjs have been created
112 30 ▶sub perldimcheck {
Subroutine perldimcheck: called CC=6 SCAR=18.1
Pod undocumented
113 30 my ($this,$pdl) = @_;
114 croak ("can't create ".$this->name) if $pdl->isnull &&
115 30 ▶ !$this->{FlagCreat};
Branch: 1 branch
BranchTrueFalseTotal
if $pdl->isnull and not $this->{"FlagCreat"} 0 30 30
Condition66%2/3
$pdl->isnull and not $this->{"FlagCreat"}
!ll&&!rl&&r
2280
MC/DC0%0/2
$pdl->isnull and not $this->{"FlagCreat"}
$pdl->isnull not $this->{"FlagCreat"}
Truth table66%2/3
A and not(B)
A= $pdl->isnullB= not $this->{"FlagCreat"}
ABresult
0X0
100
111
116 30 ▶ return 1 if $pdl->isnull;
Branch: 1 branch
BranchTrueFalseTotal
if $pdl->isnull 8 22 30
117 22 my $rdims = @{$this->{RawInds}};
118 22 ▶ croak ("not enough dimensions for ".$this->name)
Branch: 1 branch
BranchTrueFalseTotal
if ($pdl->broadcastids)[0] < $rdims 0 22 22
119 if ($pdl->broadcastids)[0] < $rdims;
120 22 my @dims = $pdl->dims;
121 22 my ($i,$ind) = (0,undef);
122 22 for $ind (@{$this->{IndObjs}}) {
123 18 $ind->add_value($dims[$i++]);
124 }
125 20 return 0; # not creating
126 }
127  
128 27 ▶sub finalcheck {
Subroutine finalcheck: called CC=4 SCAR=13.9
Pod undocumented
129 27 my ($this,$pdl) = @_;
130 27 ▶ return [] if $pdl->isnull;
Branch: 1 branch
BranchTrueFalseTotal
if $pdl->isnull 8 19 27
131 19 my @corr = ();
132 19 my @dims = $pdl->dims;
133 19 my $i = 0;
134 19 for my $ind (@{$this->{IndObjs}}) {
135 15 ▶ push @corr,[$i-1,$ind->{Value},$dims[$i-1]] if $dims[$i++] != $ind->{Value};
Branch: 1 branch
BranchTrueFalseTotal
if $dims[$i++] != $ind->{"Value"} 1 14 15
136 }
137 19 return \@corr;
138 }
139  
140 # get index sizes for a parameter that has to be created
141 8 ▶sub getcreatedims {
Subroutine getcreatedims: called CC=3 SCAR=11.8
Pod undocumented
142 8 my $this = shift;
143 return map
144 { croak "pp_def($this->{OpName}): can't create: index size ".$_->name." not initialised"
145 1 ▶ if !defined($_->{Value}) || $_->{Value} < 1;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_->{"Value"} or $_->{"Value"} < 1 0 1 1
Condition33%1/3
not defined $_->{"Value"} or $_->{"Value"} < 1
l!l&&r!l&&!r
001
MC/DC0%0/2
not defined $_->{"Value"} or $_->{"Value"} < 1
not defined $_->{"Value"} $_->{"Value"} < 1
Truth table33%1/3
not(A) or B
A= not defined $_->{"Value"}B= $_->{"Value"} < 1
ABresult
1X1
011
000
146 8 $_->{Value} } @{$this->{IndObjs}};
147 }
148  
149 254 ▶sub adjusted_type {
Subroutine adjusted_type: called CC=8 SCAR=21.7
Pod undocumented
150 254 my ($this, $generic) = @_;
151 254 ▶ confess "adjusted_type given undefined generic type\n" if !defined $generic;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $generic 0 254 254
152 254 ▶ return $generic->realversion if $this->{FlagReal} || $this->{FlagNotComplex};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"FlagReal"} or $this->{"FlagNotComplex"} 0 254 254
Condition33%1/3
$this->{"FlagReal"} or $this->{"FlagNotComplex"}
l!l&&r!l&&!r
00254
MC/DC0%0/2
$this->{"FlagReal"} or $this->{"FlagNotComplex"}
$this->{"FlagReal"} $this->{"FlagNotComplex"}
Truth table33%1/3
A or B
A= $this->{"FlagReal"}B= $this->{"FlagNotComplex"}
ABresult
1X1
011
000
153 254 ▶ return $generic->complexversion if $this->{FlagComplex};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"FlagComplex"} 0 254 254
154 254 ▶ return $generic unless $this->{FlagTyped};
Branch: 1 branch
BranchTrueFalseTotal
unless $this->{"FlagTyped"} 150 104 254
155 return $this->{Type}->numval > $generic->numval
156 ? $this->{Type} : $generic
157 104 ▶ if $this->{FlagTplus};
Branch: 2 branches
BranchTrueFalseTotal
if $this->{"FlagTplus"} 18 86 104
$this->{'Type'}->numval > $generic->numval ? : 9 9 18
158 86 $this->{Type};
159 }
160  
161 77 ▶sub get_nname{ my($this) = @_;
Subroutine get_nname: called CC=1 SCAR=0.0
Pod undocumented
162 77 "(\$PRIV(pdls)[$this->{Number}])";
163 }
164  
165 101 ▶sub get_substname {
Subroutine get_substname: called CC=2 SCAR=7.2
Pod undocumented
166 101 my($this,$ind) = @_;
167 101 ▶ $this->{IndObjs}[$ind]->name.($this->{IndTotCounts}[$ind] > 1 ? $this->{IndCounts}[$ind] : '');
Branch: 1 branch
BranchTrueFalseTotal
$this->{'IndTotCounts'}[$ind] > 1 ? : 0 101 101
168 }
169  
170 115 ▶sub get_incname {
Subroutine get_incname: called CC=2 SCAR=6.9
Pod undocumented
171 115 my($this,$ind,$for_local) = @_;
172 115 ▶ return "inc_sizes[PDL_INC_ID(__privtrans->vtable,$this->{Number},$ind)]" if !$for_local;
Branch: 1 branch
BranchTrueFalseTotal
unless $for_local 43 72 115
173 72 "__inc_$this->{Name}_".$this->get_substname($ind);
174 }
175  
176 72 ▶sub get_incregisters {
Subroutine get_incregisters: called CC=2 SCAR=6.9
Pod undocumented
177 72 my($this) = @_;
178 72 ▶ return '' if scalar(@{$this->{IndObjs}}) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$this->{"IndObjs"};} == 0 36 36 72
179 join '', map {
180 43 my $x = $_;
181 43 my ($name, $for_local) = map $this->get_incname($x, $_), 0, 1;
182 43 "register PDL_Indx $for_local = __privtrans->$name; (void)$for_local;";
183 36 } 0..$#{$this->{IndObjs}};
184 }
185  
186 # Print an access part.
187 104 ▶sub do_access {
Subroutine do_access: called CC=6 SCAR=18.0
Pod undocumented
188 104 my($this,$inds,$context) = @_;
189 104 my $pdl = $this->{Name};
190 my %subst = map {
191 104 ▶ if (!/^\s*(\w+)\s*=>\s*(\S*)\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/^\s*(\w+)\s*=>\s*(\S*)\s*$/) 2 7 9
192 2 my $msg = "Invalid subst '$_' in \$$pdl($inds):";
193 2 ▶ $msg .= " no '=>' seen" if !/=>/;
Branch: 1 branch
BranchTrueFalseTotal
unless /=>/ 1 1 2
194 2 ▶ $msg .= " invalid dim name '$1'" if /^\s*([^\w]*?)\s*=>/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*([^\w]*?)\s*=>/ 0 2 2
195 2 ▶ $msg .= " (no spaces in => value)" if /=>\s*\S\s*\S/;
Branch: 1 branch
BranchTrueFalseTotal
if /=>\s*\S\s*\S/ 1 1 2
196 2 croak "pp_def($this->{OpName}): $msg\n";
197 }
198 7 ($1,$2)
199 } PDL::PP::Rule::Substitute::split_cpp($inds);
200 my $text = "(${pdl}_datap)[" .
201 102 join('+','0', map $this->do_indterm($pdl,$_,\%subst,$context), 0..$#{$this->{IndObjs}})
202 . "]";
203 # If not all substitutions made, the user probably made a spelling error
204 102 ▶ croak "pp_def($this->{OpName}): Substitutions left for \$$pdl($inds): ".(join ',',sort keys %subst)."\n"
Branch: 1 branch
BranchTrueFalseTotal
if keys %subst != 0 0 102 102
205 if keys(%subst) != 0;
206 102 $text;
207 }
208  
209 26 ▶sub do_pdlaccess {
Subroutine do_pdlaccess: called CC=1 SCAR=0.0
Pod undocumented
210 26 my($this) = @_;
211 26 '$PRIV(pdls)['.$this->{Number}.']';
212 }
213  
214 416 ▶sub do_pointeraccess {
Subroutine do_pointeraccess: called CC=1 SCAR=0.0
Pod undocumented
215 416 my($this) = @_;
216 416 return $this->{Name}."_datap";
217 }
218  
219 29 ▶sub do_indterm { my($this,$pdl,$ind,$subst,$context) = @_;
Subroutine do_indterm: called CC=3 SCAR=12.6
Pod undocumented
220 29 my $substname = $this->get_substname($ind);
221 # See if substitutions
222 29 ▶ my $index = delete($subst->{$substname}) //
Condition66%2/3
delete $subst->{$substname} // (grep(($_ eq $substname), map($_->[1], reverse(@$context))))[0]
l!l&&r!l&&!r
7220
MC/DC0%0/2
delete $subst->{$substname} // (grep(($_ eq $substname), map($_->[1], reverse(@$context))))[0]
delete $subst->{$substname} (grep(($_ eq $substname), map($_->[1], reverse(@$context))))[0]
Truth table66%2/3
A // B
A= delete $subst->{$substname}B= (grep(($_ eq $substname), map($_->[1], reverse(@$context))))[0]
ABresult
1X1
011
000
223 # No => get the one from the nearest context.
224 (grep $_ eq $substname, map $_->[1], reverse @$context)[0];
225 29 ▶ if (!defined $index) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $index) 0 29 29
226 croak "pp_def($this->{OpName}): no value given for ndarray '$pdl' index '$substname'
227 0 You supplied (@{[sort keys %$subst]})
228 0 On stack: ".(join ' ',map {"($_->[0],$_->[1])"} @$context)."\n"
229 }
230 29 "(".($this->get_incname($ind,1))."*($index))";
231 }
232  
233 77 ▶sub get_xsdatapdecl {
Subroutine get_xsdatapdecl: called CC=2 SCAR=6.9
Pod undocumented
234 77 my ($this,$ctype,$nulldatacheck,$ppsym,$badflag) = @_;
235 77 my $pdl = $this->get_nname;
236 77 my $name = $this->{Name};
237 77 ▶ my $macro = "PDL_DECLARE_PARAMETER".($badflag ? "_BADVAL" : "");
Branch: 1 branch
BranchTrueFalseTotal
$badflag ? : 5 72 77
238 77 "$macro($ctype, $name, $pdl, $nulldatacheck, $ppsym)";
239 }
240  
241 6 1;
« lib/PDL/Bad.pd ↑ Summary blib/lib/PDL/PP/CType.pm »