lib/Statistics/SparseVector.pm

filter: Statementstmt 84.2% 123 / 146 Branchbran 80.4% 37 / 46 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 81.4% 22 / 27 Podpod 78.2% 18 / 23 totaltotal 81.7% 206 / 252 CC 29 SCAR 13.6
CC
29
Cov
83%
to_Int
12.0
Interval_Substitute
7.1
new_vec
6.1
CRAP 3.9
« lib/Statistics/Candidates.pm ↑ Summary
1 package Statistics::SparseVector;
2  
3 ##---------------------------------------------------------------------------##
4 ## Author:
5 ## Hugo WL ter Doest terdoest@cs.utwente.nl
6 ## Description: module for sparse bitvectors
7 ## method names equal that of Bit::Vector
8 ##
9 ##---------------------------------------------------------------------------##
10 ## Copyright (C) 1998, 1999 Hugo WL ter Doest terdoest@cs.utwente.nl
11 ##
12 ## This library is free software; you can redistribute it and/or modify
13 ## it under the terms of the GNU General Public License as published by
14 ## the Free Software Foundation; either version 2 of the License, or
15 ## (at your option) any later version.
16 ##
17 ## This library is distributed in the hope that it will be useful,
18 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ## GNU General Public License for more details.
21 ##
22 ## You should have received a copy of the GNU Library General Public
23 ## License along with this program; if not, write to the Free Software
24 ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 ##---------------------------------------------------------------------------##
26  
27  
28 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 4 ▶use vars qw($VERSION
Subroutine BEGIN: called CC=1 SCAR=0.0
30 @ISA
31 @EXPORT
32 4 @EXPORT_OK);
33 use overload
34 4 ▶'++' => \&increment,
Subroutine BEGIN: called CC=1 SCAR=0.0
35 '=' => \&Clone,
36 4 '""' => \&stringify;
37 4 require Exporter;
38 4 require AutoLoader;
39  
40 4 @ISA = qw(Exporter AutoLoader);
41 # Items to export into callers namespace by default. Note: do not export
42 # names by default without a very good reason. Use EXPORT_OK instead.
43 # Do not simply export all your public functions/methods/constants.
44 4 @EXPORT = qw(
45
46 );
47 4 $VERSION = '0.2';
48  
49  
50 # Preloaded methods go here.
51  
52 # necessary for overloading ""
53 # in turn required by Data::Dumper that wants to stringify variables
54 0 ▶sub stringify {
Subroutine stringify: not called CC=1 SCAR=6.9
Pod undocumented
55 0 my($self) = @_;
56  
57 0 return($self);
58 }
59  
60  
61 # create a new bitvector
62 # all bits are `off'
63 2305 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
64 2305 my($this, $n) = @_;
65  
66 # for calling $self->new($someth):
67 2305 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
023050
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
68 2305 my $self = {};
69 2305 bless $self, $class;
70 2305 $self->{N} = $n;
71 # $self->{VECTOR} = {};
72 2305 return($self);
73 }
74  
75  
76 # create a copy of a bitvector and return it
77 4 ▶sub Clone {
Subroutine Clone: called CC=2 SCAR=6.9
Pod documented
78 4 my($self) = @_;
79  
80 4 my($new);
81  
82 4 $new = Statistics::SparseVector->new($self->{N});
83 4 bless $new, ref($self);
84 4 for (keys %{$self->{VECTOR}}) {
85 17 $new->{VECTOR}{$_} = $self->{VECTOR}{$_};
86 }
87 4 return($new);
88 }
89  
90  
91 # is called right before Perl destroys a bitvector
92 # this happens automatically if a vector has zero references
93 1798 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
94 1798 my($self) = @_;
95 }
96  
97  
98 # creates a new vector from a bitstring
99 # checks for whitespace separators
100 2100 ▶sub new_vec {
Subroutine new_vec: called CC=6 SCAR=18.0
Pod undocumented
101 2100 my($this, $n, $vector, $vectype) = @_;
102  
103 2100 my($i,
104 @ints,
105 $int,
106 $self,
107 $sep);
108  
109 2100 $sep = '';
110 2100 ▶ if ($vector =~ /\s+/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vector =~ /\s+/) 2100 0 2100
111 2100 $sep = '\s+';
112 }
113 2100 @ints = split(/$sep/, $vector);
114 2100 ▶ if ($#ints+1 != $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#ints + 1 != $n) 0 2100 2100
115 0 die "inconsistent call to new_vec\n";
116 }
117 2100 $self = $this->new($n);
118 2100 $i = 0;
119 2100 ▶ while (@ints) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ints) 20300 2100 22400
120 20300 $int = shift(@ints);
121 20300 ▶ if ($int > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($int > 0) 11209 9091 20300
122 11209 ▶ $self->{VECTOR}{$i} = ($vectype eq "binary") ? 1 : $int;
Branch: 1 branch
BranchTrueFalseTotal
$vectype eq 'binary' ? : 11001 208 11209
123 }
124 20300 $i++;
125 }
126 2100 return($self);
127 }
128  
129  
130 # the value at position $col
131 1591915 ▶sub weight {
Subroutine weight: called CC=1 SCAR=0.0
Pod undocumented
132 1591915 my($self, $col) = @_;
133  
134 1591915 return($self->{VECTOR}{$col});
135 }
136  
137  
138 # we assume a bit is `on' if its value is defined
139 602990 ▶sub bit_test {
Subroutine bit_test: called CC=1 SCAR=0.0
Pod documented
140 602990 my($self, $i) = @_;
141  
142 602990 return(defined($self->{VECTOR}{$i}));
143 }
144  
145  
146 # turning off a bit is making its value undefined
147 0 ▶sub Bit_Off {
Subroutine Bit_Off: not called CC=1 SCAR=6.9
Pod documented
148 0 my($self, $i) = @_;
149  
150 0 undef $self->{VECTOR}{$i};
151 }
152  
153  
154 # turns on a bit
155 2 ▶sub Bit_On {
Subroutine Bit_On: called CC=1 SCAR=0.0
Pod documented
156 2 my($self, $i) = @_;
157  
158 2 $self->{VECTOR}{$i} = 1;
159 }
160  
161  
162 # increment integer at position $i by one
163 0 ▶sub Inc {
Subroutine Inc: not called CC=1 SCAR=6.9
Pod documented
164 0 my($self, $i) = @_;
165  
166 0 $self->{VECTOR}{$i}++;
167 }
168  
169  
170 # flips a bit, i.e. makes it undef if it is defined,
171 # and makes it defined if it is undefined
172 6087 ▶sub bit_flip {
Subroutine bit_flip: called CC=2 SCAR=6.9
Pod documented
173 6087 my($self, $i) = @_;
174  
175 6087 ▶ if (defined($self->{VECTOR}{$i})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'VECTOR'}{$i}) { } 2993 3094 6087
176 2993 undef $self->{VECTOR}{$i};
177 }
178 else {
179 3094 $self->{VECTOR}{$i} = 1;
180 }
181 }
182  
183  
184 # increases the integer value of the bitvector by one
185 77952 ▶sub increment {
Subroutine increment: called CC=4 SCAR=13.9
Pod documented
186 77952 my($self) = @_;
187  
188 77952 my($carry, $i);
189  
190 77952 $carry = 0;
191 77952 $i = 0;
192 do {
193 155869 ▶ if (defined($self->{VECTOR}{$i})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'VECTOR'}{$i}) { } 77934 77935 155869
194 77934 undef $self->{VECTOR}{$i};
195 77934 $carry = 1;
196 }
197 else {
198 77935 $self->{VECTOR}{$i} = 1;
199 77935 $carry = 0;
200 }
201 155869 $i++;
202 77952 ▶ } until (($carry == 0) || ($i == $self->{N}));
Branch: 1 branch
BranchTrueFalseTotal
($carry == 0 or $i == $self->{"N"}) or do { do { if (defined $self->{"VECTOR"}{$i}) { undef $self->{"VECTOR"}{$i}; $carry = 1; } else { $self->{"VECTOR"}{$i} = 1; $carry = 0; } ; $i++ } } 77952 77917 155869
Condition100%3/3
$carry == 0 or $i == $self->{"N"}
l!l&&r!l&&!r
779351777917
MC/DC100%2/2
$carry == 0 or $i == $self->{"N"}
$carry == 0 $i == $self->{"N"}
Truth table100%3/3
A or B
A= $carry == 0B= $i == $self->{"N"}
ABresult
1X1
011
000
203 }
204  
205  
206 # fills the set
207 16 ▶sub Fill {
Subroutine Fill: called CC=2 SCAR=6.9
Pod documented
208 16 my($self) = @_;
209  
210 16 my $i;
211  
212 16 ▶ for ($i = 0; $i < $self->{N}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->{"N"}) 160 16 176
213 160 $self->{VECTOR}{$i} = 1;
214 }
215 }
216  
217  
218 # clears the vector
219 16 ▶sub Empty {
Subroutine Empty: called CC=1 SCAR=0.0
Pod documented
220 16 my($self) = @_;
221
222 16 undef $self->{VECTOR};
223 }
224  
225  
226 # returns a bitstring
227 2402 ▶sub to_Bin {
Subroutine to_Bin: called CC=3 SCAR=11.0
Pod documented
228 2402 my($self, $sep) = @_;
229  
230 2402 my($s, $i);
231  
232 2402 $s = "";
233 2402 ▶ for ($i = 0; $i < $self->{N}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->{"N"}) 552190 2402 554592
234 552190 ▶ if (defined($self->{VECTOR}{$i})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'VECTOR'}{$i}) { } 15740 536450 552190
235 15740 $s = "1$sep" . $s;
236 }
237 else {
238 536450 $s = "0$sep" . $s;
239 }
240 }
241 2402 return($s);
242 }
243  
244  
245 # returns a bitstring
246 0 ▶sub to_Int {
Subroutine to_Int: not called CC=3 SCAR=24.8
Pod undocumented
247 0 my($self) = @_;
248  
249 0 my($s, $i);
250  
251 0 $s = "";
252 0 ▶ for ($i = 0; $i < $self->{N}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->{"N"}) 0 0 0
253 0 ▶ if (defined($self->{VECTOR}{$i})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'VECTOR'}{$i}) { } 0 0 0
254 0 $s .= "$self->{VECTOR}{$i} ";
255 }
256 else {
257 0 $s .= "0 ";
258 }
259 }
260 0 return($s);
261 }
262  
263  
264 # returns a comma-separated list of features that are on
265 2 ▶sub to_Enum {
Subroutine to_Enum: called CC=1 SCAR=0.0
Pod documented
266 2 my($self) = @_;
267  
268 2 return(join(',', keys(%{$self->{VECTOR}})));
269 }
270  
271  
272 # expects a comma-separated list of numbers
273 0 ▶sub new_Enum {
Subroutine new_Enum: not called CC=2 SCAR=17.9
Pod documented
274 0 my($this, $n, $s) = @_;
275  
276 0 my($self);
277  
278 0 $self = $this->new($n);
279 0 for (split(/,/,$s)) {
280 0 $self->{VECTOR}{$_} = 1
281 }
282 0 return($self);
283 }
284  
285  
286 # returns the length of the vector
287 264 ▶sub Size {
Subroutine Size: called CC=1 SCAR=0.0
Pod documented
288 264 my($self) = @_;
289  
290 264 return($self->{N});
291 }
292  
293  
294 # NOTA BENE: THIS ROUTINE HAS BUGS
295 # $len1 >= 0, $len2 >= 0, $vec2->Size() >= $off2 >= 0
296 # $vec1->Size() >= $off1 >= 0
297 # $vec1 may be undefined
298 # $len1 + $off1 < $vec->Size()
299 4 ▶sub Interval_Substitute {
Subroutine Interval_Substitute: called CC=7 SCAR=19.5
Pod undocumented
300 4 my($vec2, $vec1, $off2, $len2, $off1, $len1) = @_;
301  
302 4 my($i,
303 $oldvec2);
304  
305 # save $vec2 in $oldvec2
306 4 $oldvec2 = $vec2->Clone();
307 # determine the new length for $vec2
308 4 ▶ if ($off2 == $vec2->Size()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($off2 == $vec2->Size) { } 2 2 4
309 # we are appending bits from source $vec1
310 2 $vec2->{N} += $len1;
311 }
312 else {
313 # we are inserting bits from $vec1
314 2 $vec2->{N} += $len1 - $len2;
315 }
316 # target $vec2 changes only from $off2
317 # copy the new bits from the source $vec1
318 4 ▶ if (defined($vec1)) { # we have source bits
Branch: 1 branch
BranchTrueFalseTotal
if (defined $vec1) 4 0 4
319 4 ▶ for ($i = $off2; $i < $off2 + $len1; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $off2 + $len1) 14 4 18
320 14 ▶ if (defined($vec1->{VECTOR}{$i - $off2 + $off1})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $vec1->{'VECTOR'}{$i - $off2 + $off1}) { } 3 11 14
321 3 $vec2->{VECTOR}{$i} = $vec1->{VECTOR}{$i - $off2 + $off1};
322 }
323 else {
324 11 undef $vec2->{VECTOR}{$i};
325 }
326 }
327 }
328 # append the rest of $oldvec2
329 # index $i runs for $oldvec2, we correct for $vec2
330 4 ▶ for ($i = $off2 + $len2; $i < $oldvec2->{N}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $oldvec2->{"N"}) 1 4 5
331 1 ▶ if (defined($oldvec2->{VECTOR}{$i})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $oldvec2->{'VECTOR'}{$i}) { } 1 0 1
332 1 $vec2->{VECTOR}{$i-$len2+$len1} = $oldvec2->{VECTOR}{$i};
333 }
334 else {
335 0 undef $vec2->{VECTOR}{$i-$len2+$len1};
336 }
337 }
338 4 undef $oldvec2;
339 # print "$vec2->to_Bin()\n"
340 }
341  
342  
343 # removes a column from the vector
344 2000 ▶sub delete_column {
Subroutine delete_column: called CC=3 SCAR=11.2
Pod documented
345 2000 my($self, $col) = @_;
346  
347 2000 my($i);
348  
349 2000 ▶ for ($i = 0; $i < $self->{N} - 1; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $self->{"N"} - 1) 26800 2000 28800
350 26800 ▶ if ($i >= $col) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= $col) 0 26800 26800
351 0 $self->{VECTOR}{$i} = $self->{VECTOR}{$i + 1};
352 }
353 }
354 2000 undef $self->{VECTOR}{$self->{N}-1};
355 2000 $self->{N}--;
356 }
357  
358  
359 2800 ▶sub insert_column {
Subroutine insert_column: called CC=2 SCAR=6.9
Pod documented
360 2800 my($self, $pos, $val) = @_;
361  
362 2800 my($i);
363  
364 2800 $self->{N}++;
365 2800 ▶ for ($i = $self->{N}-1; $i > $pos; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > $pos) 34700 2800 37500
366 34700 $self->{VECTOR}{$i} = $self->{VECTOR}{$i - 1};
367 }
368 2800 $self->{VECTOR}{$pos} = $val;
369 }
370  
371  
372 # returns an array of indices of set bits
373 235316 ▶sub indices {
Subroutine indices: called CC=1 SCAR=0.0
Pod documented
374 235316 my($self) = @_;
375  
376 235316 return(grep(defined($self->{VECTOR}{$_}), keys(%{$self->{VECTOR}})));
377 # return(keys(%{$self->{VECTOR}}));
378 }
379  
380  
381 # the sum of the values
382 965721 ▶sub Norm {
Subroutine Norm: called CC=3 SCAR=11.0
Pod documented
383 965721 my($self) = @_;
384  
385 965721 my($n);
386  
387 965721 $n = 0;
388 965721 for (values(%{$self->{VECTOR}})) {
389 11518452 ▶ if (defined($_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_) 6580218 4938234 11518452
390 6580218 $n += $_;
391 }
392 }
393 965721 return($n);
394 }
395  
396  
397 # Autoload methods go after =cut, and are processed by the autosplit program.
398  
399 4 1;
400  
401 __END__
« lib/Statistics/Candidates.pm ↑ Summary