Basic/Vector.pm

filter: Statementstmt 94.4% 152 / 161 Branchbran 73.6% 53 / 72 Conditioncond 64.7% 11 / 17 MC/DCmcdc 45.4% 5 / 11 Subroutinesub 95.8% 23 / 24 Podpod 100% 9 / 9 totaltotal 86.0% 253 / 294 CC 45 SCAR 16.0
CC
45
Cov
86%
set_vector
8.8
ginsert
8.6
insert
8.4
CRAP 5.0
« Basic/Variance.pm ↑ Summary Basic/Mode.pm »
1 package Statistics::Basic::Vector;
2  
3 33 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 33 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 33 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 33 ▶use Scalar::Util qw(blessed weaken looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 33 our $tag_number = 0;
9  
10 33 ▶use Statistics::Basic;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 use overload
13 0 ▶ '0+' => sub { croak "attempt to use vector as scalar numerical value" },
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: not called CC=1 SCAR=6.9
14 42 ▶ '""' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
15 42 my $this = $_[0];
16 42 local $" = ", ";
17 42 ▶ my @r = map { defined $_ ? $Statistics::Basic::fmt->format_number($_, $Statistics::Basic::IPRES) : "_" } $this->query;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 203 4 207
18 42 ▶ $Statistics::Basic::DEBUG ? "vector-$this->{tag}:[@r]" : "[@r]";
Branch: 1 branch
BranchTrueFalseTotal
$Statistics::Basic::DEBUG ? : 4 38 42
19 },
20 328 ▶ 'bool' => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
21 33 fallback => 1; # tries to do what it would have done if this wasn't present.
22  
23 # new {{{
24 294 ▶sub new {
Subroutine new: called CC=5 SCAR=16.3
Pod documented
25 294 my $class = shift;
26 294 my $vector = $_[0];
27  
28 294 ▶ if( blessed($vector) and $vector->isa(__PACKAGE__) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($vector) and $vector->isa(__PACKAGE__)) 182 112 294
Condition66%2/3
builtin::blessed($vector) and $vector->isa(__PACKAGE__)
!ll&&!rl&&r
1120182
MC/DC50%1/2
builtin::blessed($vector) and $vector->isa(__PACKAGE__)
builtin::blessed($vector) $vector->isa(__PACKAGE__)
Truth table66%2/3
A and B
A= builtin::blessed($vector)B= $vector->isa(__PACKAGE__)
ABresult
0X0
100
111
29 182 ▶ warn "vector->new called with blessed argument, returning $vector instead of making another\n" if $Statistics::Basic::DEBUG >= 3;
Branch: 1 branch
BranchTrueFalseTotal
if $Statistics::Basic::DEBUG >= 3 0 182 182
30 182 return $vector;
31 }
32  
33 112 my $this = bless {tag=>(++$tag_number), s=>0, c=>{}, v=>[]}, $class;
34 112 $this->set_vector( @_ );
35  
36 112 ▶ warn "created new vector $this\n" if $Statistics::Basic::DEBUG >= 3;
Branch: 1 branch
BranchTrueFalseTotal
if $Statistics::Basic::DEBUG >= 3 0 112 112
37  
38 112 return $this;
39 }
40 # }}}
41 # copy {{{
42 3 ▶sub copy {
Subroutine copy: called CC=2 SCAR=7.0
Pod documented
43 3 my $this = shift;
44 3 my $that = __PACKAGE__->new( [@{$this->{v}}] );
45  
46 3 ▶ warn "copied vector($this -> $that)\n" if $Statistics::Basic::DEBUG >= 3;
Branch: 1 branch
BranchTrueFalseTotal
if $Statistics::Basic::DEBUG >= 3 0 3 3
47  
48 3 return $that;
49 }
50 # }}}
51  
52 # _set_computer {{{
53 243 ▶sub _set_computer {
Subroutine _set_computer: called CC=3 SCAR=11.0
54 243 my $this = shift;
55  
56 243 ▶ while( my ($k,$v) = splice @_, 0, 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 243 243 486
57 243 ▶ warn "$this set_computer($k => " . overload::StrVal($v) . ")\n" if $Statistics::Basic::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Statistics::Basic::DEBUG 2 241 243
58 243 weaken($this->{c}{$k} = $v);
59 243 $v->_recalc_needed;
60 }
61  
62 243 return;
63 }
64 # }}}
65 # _set_linked_computer {{{
66 62 ▶sub _set_linked_computer {
Subroutine _set_linked_computer: called CC=1 SCAR=0.0
67 62 my $this = shift;
68 62 my $key = shift;
69 62 my $var = shift;
70  
71 0 my $new_key = join("_", ($key, sort {$a<=>$b} map {$_->{tag}} @_));
72  
73 62 $this->_set_computer( $new_key => $var );
74  
75 62 return;
76 }
77 # }}}
78 # _get_computer {{{
79 244 ▶sub _get_computer {
Subroutine _get_computer: called CC=3 SCAR=11.0
80 244 my $this = shift;
81 244 my $k = shift;
82  
83 244 ▶ warn "$this get_computer($k): " . overload::StrVal($this->{c}{$k}||"<undef>") . "\n" if $Statistics::Basic::DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Statistics::Basic::DEBUG 2 242 244
Condition50%1/2
$this->{'c'}{$k} || '<undef>'
l!l
02
MC/DC0%0/1
$this->{'c'}{$k} || '<undef>'
$this->{'c'}{$k}
Truth table50%1/2
A
A= $this->{'c'}{$k}
Aresult
11
00
84  
85 244 return $this->{c}{$k};
86 }
87 # }}}
88 # _get_linked_computer {{{
89 32 ▶sub _get_linked_computer {
Subroutine _get_linked_computer: called CC=1 SCAR=0.0
90 32 my $this = shift;
91 32 my $key = shift;
92  
93 0 my $new_key = join("_", ($key, sort {$a<=>$b} map {$_->{tag}} @_));
94  
95 32 return $this->_get_computer( $new_key );
96 }
97 # }}}
98 # _inform_computers_of_change {{{
99 175 ▶sub _inform_computers_of_change {
Subroutine _inform_computers_of_change: called CC=4 SCAR=13.9
100 175 my $this = shift;
101  
102 175 for my $k (keys %{ $this->{c} }) {
103 271 my $v = $this->{c}{$k};
104  
105 271 ▶ if( defined($v) and blessed($v) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v and builtin::blessed($v)) { } 268 3 271
Condition66%2/3
defined $v and builtin::blessed($v)
!ll&&!rl&&r
30268
MC/DC50%1/2
defined $v and builtin::blessed($v)
defined $v builtin::blessed($v)
Truth table66%2/3
A and B
A= defined $vB= builtin::blessed($v)
ABresult
0X0
100
111
106 268 $v->_recalc_needed;
107  
108 } else {
109 3 delete $this->{c}{$k};
110 }
111 }
112  
113 175 return;
114 }
115 # }}}
116  
117 # _fix_size {{{
118 42 ▶sub _fix_size {
Subroutine _fix_size: called CC=5 SCAR=16.1
119 42 my $this = shift;
120  
121 42 my $fixed = 0;
122  
123 42 my $d = @{$this->{v}} - $this->{s};
124 42 ▶ if( $d > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d > 0) 25 17 42
125 25 splice @{$this->{v}}, 0, $d;
126 25 $fixed = 1;
127 }
128  
129 42 ▶ unless( $Statistics::Basic::NOFILL ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Statistics::Basic::NOFILL) 34 8 42
130 34 ▶ if( $d < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d < 0) 8 26 34
131 8 unshift @{$this->{v}}, # unshift so the 0s leave first
132 8 map {0} $d .. -1; # add $d of them
133  
134 8 $fixed = 1;
135 }
136 }
137  
138 0 ▶ warn "[fix_size $this] [@{ $this->{v} }]\n" if $Statistics::Basic::DEBUG >= 2;
Branch: 1 branch
BranchTrueFalseTotal
if $Statistics::Basic::DEBUG >= 2 0 42 42
139  
140 42 return $fixed;
141 }
142 # }}}
143  
144 # query {{{
145 328 ▶sub query {
Subroutine query: called CC=2 SCAR=6.9
Pod documented
146 328 my $this = shift;
147  
148 328 ▶ return (wantarray ? @{$this->{v}} : $this->{v});
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 262 66 328
149 }
150 # }}}
151 # query_filled {{{
152 185 ▶sub query_filled {
Subroutine query_filled: called CC=3 SCAR=11.0
Pod documented
153 185 my $this = shift;
154  
155 185 ▶ warn "[query_filled $this $this->{s}]\n" if $Statistics::Basic::DEBUG >= 1;
Branch: 1 branch
BranchTrueFalseTotal
if $Statistics::Basic::DEBUG >= 1 0 185 185
156  
157 185 ▶ return if @{$this->{v}} < $this->{s};
Branch: 1 branch
BranchTrueFalseTotal
if @{$this->{"v"};} < $this->{"s"} 4 181 185
158 181 return 1;
159 }
160 # }}}
161  
162 # insert {{{
163 29 ▶sub insert {
Subroutine insert: called CC=8 SCAR=21.2
Pod documented
164 29 my $this = shift;
165  
166 29 ▶ croak "you must define a vector size before using insert()" unless defined $this->{s};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $this->{"s"} 0 29 29
167  
168 29 for my $e (@_) {
169 31 ▶ if( ref($e) and not blessed($e) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $e and not builtin::blessed($e)) { } 3 28 31
Condition100%3/3
ref $e and not builtin::blessed($e)
!ll&&!rl&&r
2623
MC/DC100%2/2
ref $e and not builtin::blessed($e)
ref $e not builtin::blessed($e)
Truth table100%3/3
A and not(B)
A= ref $eB= not builtin::blessed($e)
ABresult
0X0
100
111
170 3 ▶ if( ref($e) eq "ARRAY" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $e eq 'ARRAY') { } 3 0 3
171 3 push @{ $this->{v} }, @$e;
172 3 ▶ warn "[insert $this] @$e\n" if $Statistics::Basic::DEBUG >= 1;
Branch: 1 branch
BranchTrueFalseTotal
if $Statistics::Basic::DEBUG >= 1 0 3 3
173  
174 } else {
175 0 croak "insert() elements do not make sense";
176 }
177  
178 } else {
179 28 push @{ $this->{v} }, $e;
180 28 ▶ warn "[insert $this] $e\n" if $Statistics::Basic::DEBUG >= 1;
Branch: 1 branch
BranchTrueFalseTotal
if $Statistics::Basic::DEBUG >= 1 0 28 28
181 }
182 }
183  
184 29 $this->_fix_size;
185 29 $this->_inform_computers_of_change;
186  
187 29 return $this;
188 }
189 # }}}
190 # ginsert {{{
191 39 ▶sub ginsert {
Subroutine ginsert: called CC=8 SCAR=21.5
Pod documented
192 39 my $this = shift;
193  
194 39 for my $e (@_) {
195 39 ▶ if( ref($e) and not blessed($e)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $e and not builtin::blessed($e)) { } 2 37 39
Condition66%2/3
ref $e and not builtin::blessed($e)
!ll&&!rl&&r
3702
MC/DC50%1/2
ref $e and not builtin::blessed($e)
ref $e not builtin::blessed($e)
Truth table66%2/3
A and not(B)
A= ref $eB= not builtin::blessed($e)
ABresult
0X0
100
111
196 2 ▶ if( ref($e) eq "ARRAY" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $e eq 'ARRAY') { } 2 0 2
197 2 push @{ $this->{v} }, @$e;
198 2 ▶ warn "[ginsert $this] @$e\n" if $Statistics::Basic::DEBUG >= 1;
Branch: 1 branch
BranchTrueFalseTotal
if $Statistics::Basic::DEBUG >= 1 0 2 2
199  
200 } else {
201 0 croak "insert() elements do not make sense";
202 }
203  
204 } else {
205 37 push @{ $this->{v} }, $e;
206 37 ▶ warn "[ginsert $this] $e\n" if $Statistics::Basic::DEBUG >= 1;
Branch: 1 branch
BranchTrueFalseTotal
if $Statistics::Basic::DEBUG >= 1 0 37 37
207 }
208 }
209  
210 39 ▶ $this->{s} = @{$this->{v}} if @{$this->{v}} > $this->{s};
Branch: 1 branch
BranchTrueFalseTotal
if @{$this->{"v"};} > $this->{"s"} 39 0 39
211 39 $this->_inform_computers_of_change;
212  
213 39 return $this;
214 }
215 33 *append = \&ginsert;
216 # }}}
217  
218 # query_size {{{
219 322 ▶sub query_size {
Subroutine query_size: called CC=1 SCAR=0.0
Pod documented
220 322 my $this = shift;
221  
222 322 return scalar @{$this->{v}};
223 }
224  
225 # maybe deprecate this later
226 33 ▶*size = \&query_size unless $ENV{TEST_AUTHOR};
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"TEST_AUTHOR"} 33 0 33
227  
228 # }}}
229 # set_size {{{
230 13 ▶sub set_size {
Subroutine set_size: called CC=3 SCAR=11.1
Pod documented
231 13 my $this = shift;
232 13 my $size = shift;
233  
234 13 ▶ croak "invalid vector size ($size)" if $size < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $size < 0 0 13 13
235  
236 13 ▶ if( $this->{s} != $size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"s"} != $size) 13 0 13
237 13 $this->{s} = $size;
238 13 $this->_fix_size;
239 13 $this->_inform_computers_of_change;
240 }
241  
242 13 return $this;
243 }
244 # }}}
245 # set_vector {{{
246 160 ▶sub set_vector {
Subroutine set_vector: called CC=8 SCAR=21.8
Pod documented
247 160 my $this = shift;
248 160 my $vector = $_[0];
249  
250 160 ▶ if( ref($vector) eq "ARRAY" ) {
Branch: 5 branches
BranchTrueFalseTotal
if (ref $vector eq 'ARRAY') { } 76 84 160
elsif (UNIVERSAL::isa($vector, 'Statistics::Basic::ComputedVector')) { } 0 84 84
elsif (UNIVERSAL::isa($vector, 'Statistics::Basic::Vector')) { } 3 81 84
elsif (@_) { } 37 44 81
elsif (defined $vector) { } 0 44 44
251 76 @{$this->{v}} = @$vector;
252 76 $this->{s} = int @$vector;
253 76 $this->_inform_computers_of_change;
254  
255 } elsif( UNIVERSAL::isa($vector, "Statistics::Basic::ComputedVector") ) {
256 0 $this->set_vector($vector->{input_vector});
257  
258 } elsif( UNIVERSAL::isa($vector, "Statistics::Basic::Vector") ) {
259 3 $this->{s} = $vector->{s};
260 3 @{$this->{v}} = @{$vector->{v}}; # copy the vector
261  
262 # I don't think this is the behavior that we really want, since they
263 # stay separate objects, they shouldn't be linked like this.
264 # $this->{s} = $vector->{s};
265 # $this->{v} = $vector->{v}; # this links the vectors together
266 # $this->{c} = $vector->{c}; # so we should link their computers too
267  
268 } elsif( @_ ) {
269 37 @{$this->{v}} = @_;
270 37 $this->{s} = int @_;
271  
272 } elsif( defined $vector ) {
273 0 croak "argument to set_vector() too strange";
274 }
275  
276 0 ▶ warn "[set_vector $this] [@{ $this->{v} }]\n" if $Statistics::Basic::DEBUG >= 2 and ref($this->{v});
Branch: 1 branch
BranchTrueFalseTotal
if $Statistics::Basic::DEBUG >= 2 and ref $this->{"v"} 0 160 160
Condition33%1/3
$Statistics::Basic::DEBUG >= 2 and ref $this->{"v"}
!ll&&!rl&&r
16000
MC/DC0%0/2
$Statistics::Basic::DEBUG >= 2 and ref $this->{"v"}
$Statistics::Basic::DEBUG >= 2 ref $this->{"v"}
Truth table33%1/3
A and B
A= $Statistics::Basic::DEBUG >= 2B= ref $this->{"v"}
ABresult
0X0
100
111
277  
278 160 return $this;
279 }
280 # }}}
281  
282 33 1;
« Basic/Variance.pm ↑ Summary Basic/Mode.pm »