PDL/SV.pm

filter: Statementstmt 97.8% 277 / 283 Branchbran 82.2% 51 / 62 Conditioncond 75.0% 9 / 12 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 98.3% 61 / 62 Podpod 82.3% 14 / 17 totaltotal 93.4% 415 / 444 CC 43 SCAR 9.8
CC
43
Cov
94%
_compare
8.0
__ANON__
7.0
_array_set
3.0
CRAP 2.7
« PDL/Factor.pm ↑ Summary PDL/Role/Stringifiable.pm »
1 package PDL::SV;
2 18 $PDL::SV::VERSION = '0.006005';
3 # ABSTRACT: PDL subclass for keeping scalar data (like strings)
4  
5 18 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 18 ▶use PDL::Lite (); # PDL::Lite is the minimal to get PDL work
Subroutine BEGIN: called CC=1 SCAR=0.0
9 18 ▶use PDL::Core qw(pdl);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 18 ▶use PDL::Primitive qw(which);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 18 ▶use Ref::Util qw(is_plain_arrayref);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 18 ▶use Safe::Isa;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 18 ▶use Type::Params;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 18 ▶use Types::Standard qw(slurpy ArrayRef ConsumerOf Int);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 18 ▶use List::AllUtils ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 18 ▶use parent 'PDL';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 18 ▶use Class::Method::Modifiers;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 18 ▶use Role::Tiny::With;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 18 with qw(PDL::Role::Stringifiable);
23  
24 18 ▶use Devel::OverloadInfo qw(overload_op_info);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 18 my $overload_info;
27 my $super_dotassign;
28  
29 18 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
30 18 my $overload_info = overload_op_info('PDL', '.=');
31 18 $super_dotassign = $overload_info->{code};
32 }
33  
34 use overload
35 18 ▶ '==' => \&_eq,
Subroutine BEGIN: called CC=1 SCAR=0.0
36 'eq' => \&_eq,
37 '!=' => \&_ne,
38 'ne' => \&_ne,
39 '<' => \&_lt,
40 'lt' => \&_lt,
41 '<=' => \&_le,
42 'le' => \&_le,
43 '>' => \&_gt,
44 'gt' => \&_gt,
45 '>=' => \&_ge,
46 'ge' => \&_ge,
47  
48 4 ▶ '.=' => sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
49 4 my ( $self, $other, $swap ) = @_;
50  
51 4 ▶ if ( $other->$_DOES('PDL::SV') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($other->$PDL::SV::_DOES('PDL::SV')) { } 3 1 4
elsif ($other->$PDL::SV::_DOES('PDL')) { } 0 1 1
52 3 my $internal = $self->_internal;
53 3 ▶ if ($other->dim(0) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($other->dim(0) == 1) { } 2 1 3
54 2 for my $i ( 0 .. $self->dim(0) - 1 ) {
55 4 my $idx = PDL::Core::at( $self, $i );
56 4 $internal->[$idx] = $other->at(0);
57 }
58 } else {
59 1 for my $i ( 0 .. $self->dim(0) - 1 ) {
60 3 my $idx = PDL::Core::at( $self, $i );
61 3 $internal->[$idx] = $other->at($i);
62 }
63 }
64 3 return $self;
65 }
66 elsif ($other->$_DOES('PDL')) {
67 0 return $super_dotassign->( $self, $other, $swap );
68 }
69 else { # non-piddle
70 1 my $internal = $self->_internal;
71 1 for my $i ( 0 .. $self->dim(0) - 1 ) {
72 2 my $idx = PDL::Core::at( $self, $i );
73 2 $internal->[$idx] = $other;
74 }
75 }
76 },
77 18 fallback => 1;
78  
79 # after stringifiable role is added, the string method will exist
80 18 ▶eval q{
Subroutine BEGIN: called CC=1 SCAR=0.0
81 use overload ( '""' => \&PDL::SV::string );
82 };
83  
84 3470 ▶sub _internal {
Subroutine _internal: called CC=2 SCAR=6.9
85 3470 my ( $self, $val ) = @_;
86 3470 ▶ if ( defined $val ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) 1095 2375 3470
87 1095 $self->{_internal} = $val;
88 }
89 3470 return $self->{_internal};
90 }
91  
92 771 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod undocumented
93 771 my ( $class, @args ) = @_;
94 771 my $data = shift @args; # first arg
95  
96 186921 ▶ state $rmap = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
97 186921 my ( $x ) = @_;
98 is_plain_arrayref($x)
99 186921 ▶ ? [ map { __SUB__->( $_ ) } @$x ]
Branch: 1 branch
BranchTrueFalseTotal
is_plain_arrayref($x) ? : 805 186116 186921
100 : 0;
101 771 };
102  
103 771 my $faked_data = $rmap->($data);
104  
105 771 my $self = $class->initialize();
106 771 my $pdl = $self->{PDL};
107 771 $pdl .= PDL::Core::indx($faked_data);
108 771 $pdl .= PDL->sequence( PDL::Core::indx(), $self->dims );
109  
110 771 ▶ if ($self->ndims == 1) { # for speed
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ndims == 1) { } 754 17 771
111 754 $self->_internal($data);
112 } else {
113 17 my $internal = $self->_internal;
114 17 for my $idx ( 0 .. $self->nelem - 1 ) {
115 102 my @where = reverse $self->one2nd($idx);
116 102 $internal->[$idx] = $self->_array_get( $data, \@where );
117 }
118 }
119  
120 771 $self;
121 }
122  
123 773 ▶sub initialize {
Subroutine initialize: called CC=2 SCAR=7.1
Pod undocumented
124 773 my ($class) = @_;
125 773 ▶ return bless( { PDL => PDL::Core::null, _internal => [] }, ref $class || $class );
Condition66%2/3
ref $class || $class
l!l&&r!l&&!r
27710
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table66%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
126 }
127  
128 # code modified from <https://metacpan.org/pod/Hash::Path>
129 102 ▶sub _array_get {
Subroutine _array_get: called CC=2 SCAR=6.9
130 102 my ( $self, $array, $indices ) = @_;
131  
132 102 my $return_value = $array->[ $indices->[0] ];
133 102 for ( 1 .. $#$indices ) {
134 102 $return_value = $return_value->[ $indices->[$_] ];
135 }
136 102 return $return_value;
137 }
138  
139 2 ▶sub _array_set {
Subroutine _array_set: called CC=3 SCAR=11.0
140 2 my ( $self, $array, $indices, $val ) = @_;
141 2 ▶ return unless scalar @$indices;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$indices 0 2 2
142  
143 2 my $subarray = $array;
144 2 for ( 0 .. $#$indices - 1 ) {
145 2 $subarray = $subarray->[ $indices->[$_] ];
146 }
147 2 $subarray->[ $indices->[-1] ] = $val;
148 }
149  
150  
151 653 ▶around qw(slice dice) => sub : lvalue {
Subroutine __ANON__: called CC=1 SCAR=0.0
152 653 my $orig = shift;
153 653 my $self = shift;
154 653 my $class = ref($self);
155  
156 653 my $pdl = $self->{PDL}->$orig(@_);
157 653 return bless( { PDL => $pdl, _internal => $self->_internal }, $class );
158 18 };
159  
160  
161 2 ▶sub glue {
Subroutine glue: called CC=3 SCAR=11.0
Pod documented
162 2 my $self = shift;
163  
164 2 state $check =
165 Type::Params::compile( Int,
166 slurpy ArrayRef [ ConsumerOf ['PDL::SV'] ] );
167 2 my ( $dim, $others ) = $check->(@_);
168  
169 2 my $class = ref($self);
170  
171 2 ▶ if ($dim != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dim != 0) 0 2 2
172 0 die('PDL::SV::glue does not yet support $dim != 0');
173 }
174  
175 2 my $data = [ map { @{$_->unpdl} } ($self, @$others) ];
176 2 my $new = $class->new($data);
177 2 ▶ if (List::AllUtils::any { $_->badflag } ($self, @$others)) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&List::AllUtils::any(sub { $_->badflag; } , $self, @$others)) 1 1 2
178 1 my $isbad = pdl([ map { @{$_->isbad->unpdl} } ($self, @$others) ]);
179 1 $new->{PDL} = $new->{PDL}->setbadif($isbad);
180 }
181 2 return $new;
182 }
183  
184  
185 2 ▶sub uniq {
Subroutine uniq: called CC=1 SCAR=0.0
Pod documented
186 2 my ($self) = @_;
187 2 my $class = ref($self);
188  
189 9 my @uniq = List::AllUtils::uniq( grep { defined $_ }
190 2 @{ $self->_effective_internal } );
191 2 return $class->new( \@uniq );
192 }
193  
194 ## Please see file perltidy.ERR
195 1 ▶sub uniqind {
Subroutine uniqind: called CC=1 SCAR=0.0
Pod documented
196 1 my ($self) = @_;
197  
198 1 my $effective_internal = $self->_effective_internal;
199 3 ▶ my @uniqind = List::AllUtils::uniq_by { $effective_internal->[$_] }
Subroutine __ANON__: called CC=1 SCAR=0.0
200 1 grep { defined $effective_internal->[$_] }
201 ( 0 .. $#$effective_internal );
202 1 return pdl( \@uniqind );
203 }
204  
205  
206 1 ▶sub sever {
Subroutine sever: called CC=2 SCAR=7.0
Pod documented
207 1 my ($self) = @_;
208  
209 1 $self->_internal( $self->_effective_internal );
210 1 my $p = PDL->sequence( $self->type, $self->dims );
211 1 ▶ $p = $p->setbadif( $self->isbad ) if $self->badflag;
Branch: 1 branch
BranchTrueFalseTotal
if $self->badflag 1 0 1
212 1 $self->{PDL} = $p;
213 1 return $self;
214 }
215  
216  
217 1 ▶sub set {
Subroutine set: called CC=1 SCAR=0.0
Pod documented
218 1 my ($self, @position) = @_;
219  
220 1 my $value = pop @position;
221 1 my $idx = $self->{PDL}->at(@position);
222 1 $self->_internal->[$idx] = $value;
223 1 return $self;
224 }
225  
226  
227 937 ▶sub at {
Subroutine at: called CC=2 SCAR=6.9
Pod documented
228 937 my $self = shift;
229  
230 937 my $idx = $self->{PDL}->at(@_);
231 937 ▶ return 'BAD' if $idx eq 'BAD';
Branch: 1 branch
BranchTrueFalseTotal
if $idx eq "BAD" 1 936 937
232 936 return $self->_internal->[$idx];
233 }
234  
235  
236 327 ▶sub unpdl {
Subroutine unpdl: called CC=3 SCAR=11.0
Pod documented
237 327 my $self = shift;
238  
239 327 ▶ if ($self->ndims == 1) { # shortcut for 1D for performance
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ndims == 1) 324 3 327
240 324 return [ $self->list ];
241 }
242  
243 27 ▶ state $rmap = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
244 27 my ( $x, $f ) = @_;
245 is_plain_arrayref($x)
246 27 ▶ ? [ map { __SUB__->( $_, $f ) } @$x ]
Branch: 1 branch
BranchTrueFalseTotal
is_plain_arrayref($x) ? : 9 18 27
247 : $f->($x);
248 3 };
249  
250 3 my $internal = $self->_internal;
251 my $f =
252 $self->badflag
253 12 ▶ ? sub { $_ eq 'BAD' ? 'BAD' : $internal->[$_] }
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'BAD' ? : 4 8 12
254 3 ▶ : sub { $internal->[$_] };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
$self->badflag ? : 2 1 3
255 3 return $rmap->( $self->{PDL}->unpdl, $f );
256 }
257  
258  
259 328 ▶sub list {
Subroutine list: called CC=2 SCAR=6.9
Pod documented
260 328 my ($self) = @_;
261  
262 328 my $internal = $self->_internal;
263 328 my @list = do {
264 18 ▶ no warnings 'numeric';
Subroutine BEGIN: called CC=1 SCAR=0.0
265 328 map { $internal->[$_] } $self->{PDL}->list;
266 };
267 328 ▶ if ($self->badflag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->badflag) 304 24 328
268 304 my @bad_indices = which($self->isbad)->list;
269 304 @list[@bad_indices] = (('BAD') x @bad_indices);
270 }
271 328 return @list;
272 }
273  
274  
275 340 ▶sub copy {
Subroutine copy: called CC=2 SCAR=6.9
Pod documented
276 340 my ($self) = @_;
277  
278 340 my $new = PDL::SV->new( [] );
279 340 $new->{PDL} = PDL->sequence( $self->type, $self->dims );
280 340 $new->_internal( $self->_effective_internal );
281 340 ▶ if ( $self->badflag ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->badflag) 13 327 340
282 13 $new->{PDL} = $new->{PDL}->setbadif( $self->isbad );
283 }
284 340 return $new;
285 }
286  
287  
288 0 ▶sub inplace {
Subroutine inplace: not called CC=1 SCAR=6.9
Pod documented
289 0 my $self = shift;
290 0 $self->{PDL}->inplace(@_);
291 0 return $self;
292 }
293  
294 72 ▶sub _call_on_pdl {
Subroutine _call_on_pdl: called CC=1 SCAR=0.0
295 72 my ($method) = @_;
296  
297 913 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
298 913 my $self = shift;
299 913 return $self->{PDL}->$method(@_);
300 72 };
301 }
302  
303  
304 291 ▶sub where {
Subroutine where: called CC=1 SCAR=0.0
Pod documented
305 291 my ( $self, $mask ) = @_;
306 291 return $self->slice( which($mask) );
307 }
308  
309  
310 18 for my $method (qw(isbad isgood ngood nbad)) {
311 18 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
312 72 *{$method} = _call_on_pdl($method);
313 }
314  
315  
316 322 ▶sub setbadif {
Subroutine setbadif: called CC=1 SCAR=0.0
Pod documented
317 322 my $self = shift;
318  
319 322 my $new = $self->copy;
320 322 $new->{PDL} = $new->{PDL}->setbadif(@_);
321 322 return $new;
322 }
323  
324  
325 1 ▶sub setbadtoval {
Subroutine setbadtoval: called CC=3 SCAR=11.0
Pod documented
326 1 my $self = shift;
327 1 my ($val) = @_;
328  
329 1 my $class = ref($self);
330  
331 1 my $data = $self->unpdl;
332 1 ▶ if ( $self->badflag ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->badflag) 1 0 1
333 1 my $isbad = $self->isbad;
334 1 for my $idx ( which($isbad)->list ) {
335 2 my @where = reverse $self->one2nd($idx);
336 2 $self->_array_set( $data, \@where, $val );
337 }
338 }
339 1 return $class->new($data);
340 }
341  
342  
343 2 ▶sub match_regexp {
Subroutine match_regexp: called CC=3 SCAR=11.0
Pod documented
344 2 my ( $self, $regexp ) = @_;
345  
346 2 ▶ my @matches = map { $_ =~ $regexp ? 1 : 0 } @{ $self->_internal };
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ /$regexp/u ? : 5 4 9
347 2 my $p = pdl( \@matches )->reshape( $self->dims );
348 2 ▶ if ( $self->badflag ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->badflag) 2 0 2
349 2 $p = $p->setbadif( $self->isbad );
350 }
351 2 return $p;
352 }
353  
354 429 ▶sub _effective_internal {
Subroutine _effective_internal: called CC=2 SCAR=6.9
355 429 my ($self) = @_;
356  
357 429 my $internal = $self->_internal;
358 429 my @indices = $self->{PDL}->list;
359  
360 18 ▶ no warnings 'numeric';
Subroutine BEGIN: called CC=1 SCAR=0.0
361 429 my @rslt = @$internal[@indices];
362 429 ▶ if ($self->badflag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->badflag) 72 357 429
363 72 my @isbad = which($self->isbad)->list;
364 72 @rslt[@isbad] = ((undef) x @isbad);
365 }
366 429 return \@rslt;
367 }
368  
369 46 ▶sub _compare {
Subroutine _compare: called CC=8 SCAR=20.9
370 46 my ($self, $other) = @_;
371  
372 46 ▶ unless ($other->$_DOES('PDL::SV') or !ref($other)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($other->$PDL::SV::_DOES("PDL::SV") or not ref $other) 0 46 46
Condition66%2/3
$other->$PDL::SV::_DOES("PDL::SV") or not ref $other
l!l&&r!l&&!r
3970
MC/DC0%0/2
$other->$PDL::SV::_DOES("PDL::SV") or not ref $other
$other->$PDL::SV::_DOES("PDL::SV") not ref $other
Truth table66%2/3
A or not(B)
A= $other->$PDL::SV::_DOES("PDL::SV")B= not ref $other
ABresult
1X1
011
000
373 0 die "Cannot compare PDL::SV to anything other than a PDL::SV or a plain string";
374 }
375  
376 46 my $rslt;
377 46 ▶ if (ref($other)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $other) { } 39 7 46
378  
379 # check dimensions
380 {
381 # this would die if they are not same
382 18 ▶ no warnings qw(void);
Subroutine BEGIN: called CC=1 SCAR=0.0
383 39 $self->{PDL}->shape == $other->{PDL}->shape;
384 }
385  
386 149 ▶ my @cmp_rslt = List::AllUtils::pairwise {
Subroutine __ANON__: called CC=3 SCAR=11.0
387 149 ▶ (defined $a and defined $b) ? ($a cmp $b) : 0
Branch: 1 branch
BranchTrueFalseTotal
defined $a && defined $b ? : 113 36 149
Condition100%3/3
defined $a and defined $b
!ll&&!rl&&r
351113
MC/DC100%2/2
defined $a and defined $b
defined $a defined $b
Truth table100%3/3
A and B
A= defined $aB= defined $b
ABresult
0X0
100
111
388 39 } @{$self->_effective_internal}, @{$other->_effective_internal};
389  
390 39 $rslt = PDL::Core::pdl( \@cmp_rslt )->reshape( $self->dims );
391 39 ▶ if ( $self->badflag or $other->badflag ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->badflag or $other->badflag) 25 14 39
Condition66%2/3
$self->badflag or $other->badflag
l!l&&r!l&&!r
25014
MC/DC50%1/2
$self->badflag or $other->badflag
$self->badflag $other->badflag
Truth table66%2/3
A or B
A= $self->badflagB= $other->badflag
ABresult
1X1
011
000
392 25 $rslt = $rslt->setbadif( $self->isbad | $other->isbad );
393 }
394 } else { # $other is a plain string
395 my @cmp_rslt = map {
396 39 ▶ (defined $_) ? ($_ cmp $other) : 0
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 27 12 39
397 7 } @{$self->_effective_internal};
398  
399 7 $rslt = PDL::Core::pdl( \@cmp_rslt )->reshape( $self->dims );
400 7 ▶ if ( $self->badflag ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->badflag) 6 1 7
401 6 $rslt = $rslt->setbadif( $self->isbad );
402 }
403 }
404  
405 46 return $rslt;
406 }
407  
408 108 ▶sub _gen_compare {
Subroutine _gen_compare: called CC=1 SCAR=0.0
409 108 my ($f) = @_;
410  
411 46 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
412 46 my ( $self, $other, $swap ) = @_;
413 46 my $cmp_rslt = $self->_compare($other);
414 46 return $f->($swap, $cmp_rslt);
415 }
416 108 }
417  
418 5 ▶*_eq = _gen_compare( sub { $_[1] == 0 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
419 21 ▶*_ne = _gen_compare( sub { $_[1] != 0 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
420 6 ▶*_lt = _gen_compare( sub { $_[0] ? $_[1] > 0 : $_[1] < 0 } );
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 0 6 6
421 6 ▶*_le = _gen_compare( sub { $_[0] ? $_[1] >= 0 : $_[1] <= 0 } );
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 0 6 6
422 4 ▶*_gt = _gen_compare( sub { $_[0] ? $_[1] < 0 : $_[1] > 0 } );
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 0 4 4
423 4 ▶*_ge = _gen_compare( sub { $_[0] ? $_[1] <= 0 : $_[1] >= 0 } );
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 0 4 4
424  
425 1 ▶sub element_stringify_max_width {
Subroutine element_stringify_max_width: called CC=1 SCAR=0.0
Pod undocumented
426 1 my ( $self, $element ) = @_;
427 1 my @where = @{ $self->uniq->SUPER::unpdl };
428 1 my @which = @{ $self->_internal }[@where];
429 1 my @lengths = map { length $_ } @which;
430 1 List::AllUtils::max(@lengths);
431 }
432  
433 18 1;
434  
435 __END__
« PDL/Factor.pm ↑ Summary PDL/Role/Stringifiable.pm »