Progressive/Dict.pm

filter: Statementstmt 83.7% 113 / 135 Branchbran 70.8% 34 / 48 Conditioncond 74.0% 20 / 27 MC/DCmcdc 44.4% 8 / 18 Subroutinesub 85.1% 23 / 27 Podpod 0.0% 0 / 15 totaltotal 73.3% 198 / 270 CC 37 SCAR 16.4
CC
37
Cov
80%
dump
12.0
print
12.0
delete
9.2
CRAP 5.2
« Progressive.pm ↑ Summary
1 package Compress::LZW::Progressive::Dict;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 our $VERSION = '0.1';
8  
9 13 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
10 13 my ($class) = @_;
11  
12 13 my %self = (
13 tree => Compress::LZW::Progressive::Dict::Tree->new(),
14 # hash => {},
15 array => [],
16 next_code => 0,
17 reuse_codes => [],
18 codes_used => [],
19 code_counter => 0,
20 );
21  
22 13 my $self = bless \%self, $class;
23  
24 13 $self->add($_) foreach map { chr } 0..255;
25  
26 13 return $self;
27 }
28  
29 ## Adding and deleting from the dict
30  
31 11800 ▶sub add {
Subroutine add: called CC=5 SCAR=16.3
Pod undocumented
32 11800 my ($self, $phrase, $code) = @_;
33  
34 11800 ▶ return 0 unless defined $phrase;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $phrase 0 11800 11800
35 11800 ▶ return 1 if $self->code($phrase); #defined $self->{hash}{$phrase};
Branch: 1 branch
BranchTrueFalseTotal
if $self->code($phrase) 0 11800 11800
36  
37 11800 ▶ if (! defined $code) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $code) 11800 0 11800
38 11800 ▶ $code = int @{ $self->{reuse_codes} } ? shift @{ $self->{reuse_codes} } : $self->{next_code}++;
Branch: 1 branch
BranchTrueFalseTotal
int @{$self->{'reuse_codes'};} ? : 2596 9204 11800
39 }
40  
41 11800 my @chars = split //, $phrase;
42 11800 $self->{tree}->add(\@chars, $code);
43  
44 # $self->{hash}{$phrase} = $code;
45 11800 $self->{array}[$code] = $phrase;
46  
47 11800 return $code;
48 }
49  
50 2876 ▶sub delete {
Subroutine delete: called CC=4 SCAR=14.4
Pod undocumented
51 2876 my ($self, $phrase, $code) = @_;
52  
53 2876 ▶ return 0 unless defined $phrase && defined $code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $phrase and defined $code 0 2876 2876
Condition33%1/3
defined $phrase and defined $code
!ll&&!rl&&r
002876
MC/DC0%0/2
defined $phrase and defined $code
defined $phrase defined $code
Truth table33%1/3
A and B
A= defined $phraseB= defined $code
ABresult
0X0
100
111
54 # return 0 unless defined $self->{hash}{$phrase};
55 2876 ▶ return 0 unless defined $self->{array}[$code];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"array"}[$code] 0 2876 2876
56  
57 2876 my @chars = split //, $phrase;
58 2876 $self->{tree}->delete(\@chars);
59  
60 # delete $self->{hash}{$phrase};
61 2876 $self->{array}[$code] = undef;
62
63 2876 $self->{codes_used}[$code] = undef;
64  
65 2876 push @{ $self->{reuse_codes} }, $code;
66  
67 2876 return 1;
68 }
69  
70 0 ▶sub delete_phrase {
Subroutine delete_phrase: not called CC=1 SCAR=6.9
Pod undocumented
71 0 my ($self, $phrase) = @_;
72  
73 0 my $code = $self->{hash}{$phrase};
74 0 return $self->delete($phrase, $code);
75 }
76  
77 2 ▶sub delete_code {
Subroutine delete_code: called CC=1 SCAR=0.0
Pod undocumented
78 2 my ($self, $code) = @_;
79  
80 2 my $phrase = $self->{array}[$code];
81 2 return $self->delete($phrase, $code);
82 }
83  
84 3 ▶sub delete_codes {
Subroutine delete_codes: called CC=3 SCAR=11.0
Pod undocumented
85 3 my ($self, @codes) = @_;
86 3 ▶ while (my $code = shift @codes) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = shift @codes) 2874 3 2877
87 2874 my $phrase = $self->{array}[$code];
88 2874 ▶ return 0 unless $self->delete($phrase, $code);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->delete($phrase, $code) 0 2874 2874
89 }
90 3 return 1;
91 }
92  
93 ## Accessors
94  
95 11800 ▶sub code_matching_str {
Subroutine code_matching_str: called CC=1 SCAR=0.0
Pod undocumented
96 11800 my ($self, $str) = @_;
97 11800 return $self->code_matching_array([ split //, $str ]);
98 }
99 20359 ▶sub code_matching_array {
Subroutine code_matching_array: called CC=1 SCAR=0.0
Pod undocumented
100 20359 my ($self, $arr) = @_;
101 20359 return $self->{tree}->search(0, $arr);
102 }
103  
104 34048 ▶sub increment_code_usage_count {
Subroutine increment_code_usage_count: called CC=1 SCAR=0.0
Pod undocumented
105 34048 my ($self, $code) = @_;
106 34048 $self->{codes_used}[$code] = $self->{code_counter}++;
107 34048 return undef;
108 }
109  
110 0 ▶sub next_code {
Subroutine next_code: not called CC=1 SCAR=6.9
Pod undocumented
111 0 return $_[0]->{next_code};
112 }
113  
114 8472 ▶sub codes_used {
Subroutine codes_used: called CC=1 SCAR=0.0
Pod undocumented
115 8472 my $self = shift;
116 8472 return $self->{next_code} - int @{ $self->{reuse_codes} };
117 }
118  
119 # Given a count, return that many codes which haven't been used lately
120  
121 6 ▶sub least_used_codes {
Subroutine least_used_codes: called CC=2 SCAR=7.0
Pod undocumented
122 6 my ($self, $count) = @_;
123  
124 6 my $codes_used = $self->{codes_used};
125 my @delete =
126 160780 sort { $codes_used->[$a] <=> $codes_used->[$b] }
127 22992 grep { defined $codes_used->[$_] }
128 6 256..$#{ $codes_used };
129  
130 6 ▶ $count = int(@delete) if int(@delete) < $count;
Branch: 1 branch
BranchTrueFalseTotal
if int @delete < $count 0 6 6
131  
132 # print join ', ', map { "$_ => $codes_used->[$_]" } @delete[0..($count-1)];
133 # print "\n";
134  
135 6 return @delete[0..($count - 1)];
136 }
137  
138 17026 ▶sub phrase {
Subroutine phrase: called CC=1 SCAR=0.0
Pod undocumented
139 17026 my ($self, $code) = @_;
140  
141 17026 return $self->{array}[$code];
142 }
143  
144 11800 ▶sub code {
Subroutine code: called CC=3 SCAR=11.8
Pod undocumented
145 11800 my ($self, $phrase) = @_;
146  
147 # return $self->{hash}{$phrase};
148 11800 my $code = $self->code_matching_str($phrase);
149 11800 ▶ if ($code && $self->phrase($code) eq $phrase) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code and $self->phrase($code) eq $phrase) { } 0 11800 11800
Condition66%2/3
$code and $self->phrase($code) eq $phrase
!ll&&!rl&&r
332984710
MC/DC0%0/2
$code and $self->phrase($code) eq $phrase
$code $self->phrase($code) eq $phrase
Truth table66%2/3
A and B
A= $codeB= $self->phrase($code) eq $phrase
ABresult
0X0
100
111
150 0 return $code;
151 }
152 else {
153 11800 return undef;
154 }
155 }
156  
157 0 ▶sub dump {
Subroutine dump: not called CC=3 SCAR=24.8
Pod undocumented
158 0 my ($self) = shift;
159  
160 # print "Phrase Hash\n";
161 # foreach my $phrase (keys %{ $self->{hash} }) {
162 # printf "%6d : %20s\n", $self->{hash}{$phrase}, $phrase;
163 # }
164  
165 0 print "Code Array\n";
166 0 foreach my $code (0..$#{ $self->{array} }) {
167 0 ▶ next unless defined $self->{array}[$code];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"array"}[$code] 0 0 0
168 0 printf "%6d : %20s (%8d)\n", $code, $self->{array}[$code], $self->{codes_used}[$code];
169 }
170  
171 0 print "Next Code: ".$self->{next_code}."\n";
172 0 print "Reuse Codes:\n" . join(", ", @{ $self->{reuse_codes} }) . "\n";
173
174 # return;
175 0 print "Tree\n";
176 0 $self->{tree}->print(0);
177 }
178  
179 package Compress::LZW::Progressive::Dict::Tree;
180  
181 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
182 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
183 3 ▶no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
184 3 ▶use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
185  
186 3 our $VERSION = '0.11';
187  
188 11824 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
189 11824 my ($class) = @_;
190 11824 ▶ $class = ref $class if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 11811 13 11824
191  
192 11824 my @self = (
193 {},
194 undef,
195 );
196  
197 11824 return bless \@self, $class;
198 }
199  
200 # Given an array of characters and a code, create children for each character and finally
201 # set the value of the final node
202  
203 44447 ▶sub add {
Subroutine add: called CC=4 SCAR=13.9
204 44447 my ($self, $chars, $code) = @_;
205  
206 44447 my $char = shift @$chars;
207 44447 ▶ if (defined $char) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $char) { } 32647 11800 44447
208 32647 ▶ $char = 'null' if ord($char) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if ord $char == 0 27 32620 32647
209 32647 ▶ $self->[0]{$char} ||= $self->new();
Condition66%2/3
$self->[0]{$char} ||= $self->new
l!l&&r!l&&!r
20836118110
MC/DC0%0/2
$self->[0]{$char} ||= $self->new
$self->[0]{$char} $self->new
Truth table66%2/3
A ||= B
A= $self->[0]{$char}B= $self->new
ABresult
1X1
011
000
210 32647 $self->[0]{$char}->add($chars, $code);
211 }
212 else {
213 11800 $self->[1] = $code;
214 }
215 }
216  
217 # Given an array and an index on that array, recursively delete all nodes from that point on and
218 # backwards while such nodes have no value
219  
220 9255 ▶sub delete {
Subroutine delete: called CC=9 SCAR=22.2
221 9255 my ($self, $chars) = @_;
222  
223 9255 my $char = shift @$chars;
224 9255 ▶ $char = 'null' if defined $char && ord($char) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $char and ord $char == 0 0 9255 9255
Condition66%2/3
defined $char and ord $char == 0
!ll&&!rl&&r
287663790
MC/DC0%0/2
defined $char and ord $char == 0
defined $char ord $char == 0
Truth table66%2/3
A and B
A= defined $charB= ord $char == 0
ABresult
0X0
100
111
225  
226 # Descend to the last char
227 9255 ▶ if (defined $char && (my $child = $self->[0]{$char})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $char and my $child = $self->[0]{$char}) { } 6379 2876 9255
elsif (not defined $char) { } 2876 0 2876
Condition66%2/3
defined $char and my $child = $self->[0]{$char}
!ll&&!rl&&r
287606379
MC/DC50%1/2
defined $char and my $child = $self->[0]{$char}
defined $char my $child = $self->[0]{$char}
Truth table66%2/3
A and B
A= defined $charB= my $child = $self->[0]{$char}
ABresult
0X0
100
111
228 6379 ▶ if ($child->delete($chars)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child->delete($chars)) 2886 3493 6379
229 2886 delete $self->[0]{$char};
230 }
231 }
232 elsif (! defined $char) {
233 2876 $self->[1] = undef;
234 }
235  
236 # Now, delete backwards unless I have children or a value
237 9255 ▶ return (%{ $self->[0] } || defined $self->[1]) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
%{$self->[0];} || defined $self->[1] ? : 6369 2886 9255
Condition100%3/3
%{$self->[0];} or defined $self->[1]
l!l&&r!l&&!r
636812886
MC/DC100%2/2
%{$self->[0];} or defined $self->[1]
%{$self->[0];} defined $self->[1]
Truth table100%3/3
A or B
A= %{$self->[0];}B= defined $self->[1]
ABresult
1X1
011
000
238 }
239  
240 # Given an array and an index on that array, recursively search for a defined node that matches
241 # as many as possible of the characters
242  
243 62400 ▶sub search {
Subroutine search: called CC=8 SCAR=20.8
244 62400 my ($self, $index, $arr) = @_;
245  
246 62400 my $found_desc;
247  
248 62400 my $char = $arr->[$index];
249 62400 ▶ $char = 'null' if defined $char && ord($char) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $char and ord $char == 0 41 62359 62400
Condition100%3/3
defined $char and ord $char == 0
!ll&&!rl&&r
876227241
MC/DC100%2/2
defined $char and ord $char == 0
defined $char ord $char == 0
Truth table100%3/3
A and B
A= defined $charB= ord $char == 0
ABresult
0X0
100
111
250  
251 62400 ▶ if (defined $char && (my $child = $self->[0]{$char})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $char and my $child = $self->[0]{$char}) 42041 20359 62400
Condition100%3/3
defined $char and my $child = $self->[0]{$char}
!ll&&!rl&&r
872027242041
MC/DC100%2/2
defined $char and my $child = $self->[0]{$char}
defined $char my $child = $self->[0]{$char}
Truth table100%3/3
A and B
A= defined $charB= my $child = $self->[0]{$char}
ABresult
0X0
100
111
252 42041 $found_desc = $child->search($index + 1, $arr);
253 42041 ▶ return $found_desc if defined $found_desc;
Branch: 1 branch
BranchTrueFalseTotal
if defined $found_desc 42032 9 42041
254 }
255  
256 20368 ▶ if (! defined $found_desc && defined $self->[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $found_desc and defined $self->[1]) 17031 3337 20368
Condition66%2/3
not defined $found_desc and defined $self->[1]
!ll&&!rl&&r
0333717031
MC/DC50%1/2
not defined $found_desc and defined $self->[1]
not defined $found_desc defined $self->[1]
Truth table66%2/3
not(A) and B
A= not defined $found_descB= defined $self->[1]
ABresult
0X0
100
111
257 17031 return $self->[1];
258 }
259
260 3337 return undef;
261 }
262  
263 0 ▶sub print {
Subroutine print: not called CC=3 SCAR=24.8
264 0 my ($self, $level) = @_;
265
266 0 ▶ print ' ' . (' 'x$level) . ' => ' . $self->[1] . "\n" if defined $self->[1];
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->[1] 0 0 0
267 0 foreach my $char (sort keys %{ $self->[0] }) {
268 0 print ' ' . (' 'x$level) . $char . "\n";
269 0 $self->[0]{$char}->print($level + 1);
270 }
271 }
272  
273 3 1;
« Progressive.pm ↑ Summary