Progressive.pm

filter: Statementstmt 63.4% 132 / 208 Branchbran 38.6% 44 / 114 Conditioncond 16.0% 4 / 25 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 72.7% 8 / 11 Podpod 83.3% 5 / 6 totaltotal 51.0% 193 / 378 CC 71 SCAR 40.5
CC
71
Cov
52%
stats
182.0
dict_dump
56.0
__ANON__
56.0
CRAP 57.3
↑ Summary Progressive/Dict.pm »
1 package Compress::LZW::Progressive;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Compress::LZW::Progressive::Dict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '0.102';
9  
10 2 my @empty_dict;
11 2 @empty_dict[0..255] = map { chr } 0..255;
12  
13 2 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
14 2 my ($class, %args) = @_;
15  
16 2 ▶ $args{bits} ||= 16;
Condition50%1/2
$args{"bits"} ||= 16
l!l
20
MC/DC0%0/1
$args{"bits"} ||= 16
$args{"bits"}
Truth table50%1/2
A
A= $args{"bits"}
Aresult
11
00
17  
18 2 my $code_counter = (2 ** $args{bits}) - 1;
19 2 $args{code_end_segment} = $code_counter--;
20 2 $args{code_add_start} = $code_counter--;
21 2 $args{code_add_end} = $code_counter--;
22 2 $args{code_delete_start} = $code_counter--;
23 2 $args{code_delete_end} = $code_counter--;
24 2 $args{code_delete_count} = $code_counter--;
25 2 $args{code_max} = $code_counter--;
26  
27 2 $args{compress_resets} = 0;
28 2 $args{decompress_resets} = 0;
29  
30 2 $args{compress_deleted_least_used_codes} = 0;
31  
32 2 my $self = bless \%args, $class;
33 2 $self->reset();
34  
35 2 return $self;
36 }
37  
38 6 ▶sub reset {
Subroutine reset: called CC=6 SCAR=19.2
Pod documented
39 6 my ($self, $which) = @_;
40 6 ▶ $which ||= '';
Condition50%1/2
$which ||= ""
l!l
06
MC/DC0%0/1
$which ||= ""
$which
Truth table50%1/2
A
A= $which
Aresult
11
00
41  
42 6 ▶ if (! $which || $which eq 'compress') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $which or $which eq "compress") 6 0 6
Condition33%1/3
not $which or $which eq "compress"
l!l&&r!l&&!r
600
MC/DC0%0/2
not $which or $which eq "compress"
not $which $which eq "compress"
Truth table33%1/3
not(A) or B
A= not $whichB= $which eq "compress"
ABresult
1X1
011
000
43 6 $self->{cdict} = Compress::LZW::Progressive::Dict->new();
44 6 $self->{compress_resets}++;
45 }
46  
47 6 ▶ if (! $which || $which eq 'decompress') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $which or $which eq "decompress") 6 0 6
Condition33%1/3
not $which or $which eq "decompress"
l!l&&r!l&&!r
600
MC/DC0%0/2
not $which or $which eq "decompress"
not $which $which eq "decompress"
Truth table33%1/3
not(A) or B
A= not $whichB= $which eq "decompress"
ABresult
1X1
011
000
48 6 $self->{ddict_reuse_codes} = [];
49 6 $self->{ddict} = [ @empty_dict ];
50 6 $self->{ddict_usage} = Compress::LZW::Progressive::Dict->new();
51 6 $self->{dnext} = 256;
52 6 $self->{decompress_resets}++;
53 }
54  
55 6 $self->{code_frequency} = {};
56 6 $self->{stats} = {};
57 }
58  
59 86 ▶sub compress {
Subroutine compress: called CC=17 SCAR=32.2
Pod documented
60 86 my ($self, $str) = @_;
61  
62 86 my $dict = $self->{cdict};
63 86 my $debug = $self->{debug};
64  
65 86 my @out = ();
66  
67 86 my @char = split //, $str;
68 86 ▶ while (int @char > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (int @char > 0) 8555 0 8555
69 8555 ▶ print "Matching '".join('', @char[0..($#char > 20 ? 20 : $#char)])."'\n" if $debug;
Branch: 2 branches
BranchTrueFalseTotal
if $debug 0 8555 8555
$#char > 20 ? : 0 0 0
70  
71 # Find the code that matches the most of the upcoming chars
72 8555 my $code = $dict->code_matching_array(\@char);
73 8555 ▶ die "Caouldn't find code to match '".join('', @char)."'" if ! defined $code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 0 8555 8555
74  
75 8555 my $phrase = $dict->phrase($code);
76 8555 ▶ die "Found code that has no phrase ($code)" if ! length $phrase;
Branch: 1 branch
BranchTrueFalseTotal
unless length $phrase 0 8555 8555
77  
78 8555 $dict->increment_code_usage_count($code);
79 8555 ▶ print " + $code for '$phrase'" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 8555 8555
80 8555 push @out, $code;
81  
82 # Remove the phrase found from the start of the @char
83 8555 splice @char, 0, length($phrase);
84 8555 ▶ if (! defined $char[0]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $char[0]) 86 8469 8555
85 86 ▶ print "\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 86 86
86 86 last;
87 }
88  
89 # If I'm running out of code space...
90 8469 ▶ if ($dict->codes_used + 1 == $self->{code_max}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dict->codes_used + 1 == $self->{"code_max"}) 3 8466 8469
91 # First, try getting some old, unused codes, and asking the client to delete that many (1/4th of custom codes)
92 3 my $delete_max_old_codes = int(($dict->codes_used - 256) * .25);
93 3 ▶ if (my @delete = $dict->least_used_codes($delete_max_old_codes)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@delete) = $dict->least_used_codes($delete_max_old_codes)) { } 3 0 3
94 3 ▶ print "Asking reusal of ".int(@delete)." codes ".join(', ', @delete)."\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 3 3
95 3 ▶ die "Couldn't delete codes" unless $dict->delete_codes(@delete);
Branch: 1 branch
BranchTrueFalseTotal
unless $dict->delete_codes(@delete) 0 3 3
96  
97 # Push to out codestream
98 3 push @out, $self->{code_delete_count};
99 3 push @out, int @delete;
100  
101 3 $self->{compress_deleted_least_used_codes}++;
102  
103 # ...and continue with next code creation
104 }
105 # Otherwise (probably won't get here), do a full dict reset and skip to next char (can't create new)
106 else {
107 0 ▶ print " + reset code '".$self->{code_max}."'\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
108 0 push @out, $self->{code_max};
109  
110 0 $self->{compress_resets}++;
111 0 $dict = Compress::LZW::Progressive::Dict->new();
112 0 ▶ print "\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
113 0 next;
114 }
115 }
116  
117 8469 my $new_phrase = $phrase . $char[0];
118 8469 my $new_code = $dict->add($new_phrase);
119 8469 ▶ print ", creating $new_code => '$new_phrase'\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 8469 8469
120 8469 $dict->increment_code_usage_count($new_code);
121 }
122 86 ▶ print "End of \@char; putting end segment code\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 86 86
123 86 push @out, $self->{code_end_segment};
124  
125 86 $self->{stats}{last_compress_in_bytes} = length $str;
126 86 $self->{stats}{last_compress_out_bytes} = int(@out) * 2;
127 86 $self->{stats}{compress_in_bytes} += $self->{stats}{last_compress_in_bytes};
128 86 $self->{stats}{compress_out_bytes} += $self->{stats}{last_compress_out_bytes};
129
130 86 $self->{cdict} = $dict;
131 86 return pack 'S*', @out;
132 }
133  
134 45 ▶sub decompress {
Subroutine decompress: called CC=25 SCAR=37.0
Pod documented
135 45 my ($self, $str) = @_;
136
137 45 my $dict = $self->{ddict};
138 45 my $dict_usage = $self->{ddict_usage};
139 45 my $reuse = $self->{ddict_reuse_codes};
140 45 my $debug = $self->{debug};
141 45 my $next = $self->{dnext};
142  
143 45 my @code = unpack 'S*', $str;
144
145 45 my $last_code;
146 45 my $return = '';
147 45 ▶ while (defined (my $code = shift @code)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $code = shift @code)) 8644 45 8689
148 8644 ▶ if ($code >= $self->{code_max}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code >= $self->{"code_max"}) 89 8555 8644
149 89 ▶ print "Code $code\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 89 89
150 # Resetting dictionary to scratch
151 89 ▶ if ($code == $self->{code_max}) {
Branch: 4 branches
BranchTrueFalseTotal
if ($code == $self->{'code_max'}) { } 0 89 89
elsif ($code == $self->{'code_end_segment'}) { } 86 3 89
elsif ($code == $self->{'code_delete_start'}) { } 0 3 3
elsif ($code == $self->{'code_delete_count'}) { } 3 0 3
152 0 ▶ print "Resetting decompress as have reached the max code '$self->{code_max}'\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
153  
154 0 $self->{decompress_resets}++;
155 0 $next = 256;
156 0 $dict = [ @empty_dict ];
157 0 $last_code = undef;
158 }
159 # End of segment; don't allow last code to affect new codes
160 elsif ($code == $self->{code_end_segment}) {
161 86 ▶ print "Reached seg code '$self->{code_end_segment}'\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 86 86
162 86 $last_code = undef;
163 }
164 # Process a list of codes to delete
165 elsif ($code == $self->{code_delete_start}) {
166 0 ▶ while (defined (my $delete_code = shift @code)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $delete_code = shift @code)) 0 0 0
167 0 ▶ last if $delete_code == $self->{code_delete_end};
Branch: 1 branch
BranchTrueFalseTotal
if $delete_code == $self->{"code_delete_end"} 0 0 0
168 0 $dict_usage->{codes_used}[$delete_code] = undef;
169 0 $dict->[$delete_code] = undef;
170 0 push @$reuse, $delete_code;
171 }
172 }
173 # Received a request to delete a number of unused codes; find that many least used codes and delete them
174 elsif ($code == $self->{code_delete_count}) {
175 3 my $delete_count = shift @code;
176  
177 3 my @delete = $dict_usage->least_used_codes($delete_count);
178 3 ▶ if (int(@delete) != $delete_count) {
Branch: 1 branch
BranchTrueFalseTotal
if (int @delete != $delete_count) 0 3 3
179 0 die "Tried to find $delete_count unused codes, but found ".int(@delete)." instead; (".join(', ', @delete).")\n";
180 }
181 3 ▶ print "Reusing ".int(@delete)." (asked $delete_count) codes ".join(', ', @delete)."\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 3 3
182 3 foreach my $delete_code (@delete) {
183 2874 ▶ if (! $dict->[$delete_code]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dict->[$delete_code]) 0 2874 2874
184 0 die "Attempting to delete non-defined code $delete_code";
185 }
186 2874 $dict_usage->{codes_used}[$delete_code] = undef;
187 2874 $dict->[$delete_code] = undef;
188 2874 push @$reuse, $delete_code;
189 }
190 }
191 89 next;
192 }
193  
194 8555 my $next_code;
195 8555 ▶ if (defined $dict->[$code]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dict->[$code]) { } 8552 3 8555
196 8552 $return .= $dict->[$code];
197  
198 8552 ▶ print " + '".$dict->[$code]."' from $code" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 8552 8552
199 8552 ▶ if (defined $last_code) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $last_code) 8466 86 8552
200 8466 ▶ $next_code = @$reuse ? shift @$reuse : $next++;
Branch: 1 branch
BranchTrueFalseTotal
@$reuse ? : 2595 5871 8466
201 8466 $dict->[$next_code] = $dict->[$last_code] . substr($dict->[$code], 0, 1);
202 8466 ▶ print " and adding '".$dict->[$next_code]."' to dict on code $next_code" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 8466 8466
203 }
204 8552 ▶ print "\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 8552 8552
205 }
206 # This is the edge case where repeating phrase won't be defined (see wikipedia.org on LZW)
207 else {
208 3 ▶ $next_code = @$reuse ? shift @$reuse : $next++;
Branch: 1 branch
BranchTrueFalseTotal
@$reuse ? : 0 3 3
209 3 my $dp = $dict->[$last_code];
210 3 $return .= $dict->[$code] = $dp . substr($dp, 0, 1);
211 3 ▶ print " + '".$dict->[$code]."' from $code\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 3 3
212 }
213 8555 ▶ $dict_usage->increment_code_usage_count($next_code) if defined $next_code;
Branch: 1 branch
BranchTrueFalseTotal
if defined $next_code 8469 86 8555
214 8555 $dict_usage->increment_code_usage_count($code);
215 8555 $last_code = $code;
216 }
217  
218 45 $self->{stats}{last_decompress_in_bytes} = length $str;
219 45 $self->{stats}{last_decompress_out_bytes} = length $return;
220 45 $self->{stats}{decompress_in_bytes} += $self->{stats}{last_decompress_in_bytes};
221 45 $self->{stats}{decompress_out_bytes} += $self->{stats}{last_decompress_out_bytes};
222  
223 45 $self->{dnext} = $next;
224 45 $self->{ddict} = $dict;
225 45 return $return;
226 }
227  
228 0 ▶sub stats {
Subroutine stats: not called CC=13 SCAR=52.0
Pod documented
229 0 my ($self, $type, $phrases) = @_;
230  
231 0 my $devel_size;
232 0 eval {
233 0 require Devel::Size;
234 0 $devel_size = 1;
235 };
236 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
237 0 print STDERR "Devel::Size not installed so stats() will exclude data size\n";
238 }
239  
240 0 my @return;
241  
242 0 push @return, sprintf "Bits %d", $self->{bits};
243 0 ▶ if (! $type || $type eq 'compress') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $type or $type eq "compress") 0 0 0
Condition0%0/3
not $type or $type eq "compress"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $type or $type eq "compress"
not $type $type eq "compress"
Truth table0%0/3
not(A) or B
A= not $typeB= $type eq "compress"
ABresult
1X1
011
000
244 push @return, sprintf "Compress efficiency: %3.1f%% (%3.1f%% last) with %d/%d codes used",
245 100 * (1 - ($self->{stats}{compress_out_bytes} / $self->{stats}{compress_in_bytes})),
246 100 * (1 - ($self->{stats}{last_compress_out_bytes} / $self->{stats}{last_compress_in_bytes})),
247 $self->{cdict}->codes_used,
248 $self->{code_max},
249 0 ;
250 push @return, sprintf "cdict: %.2f Kb",
251 0 ▶ Devel::Size::total_size($self->{cdict}) / 1024
Branch: 1 branch
BranchTrueFalseTotal
if $devel_size 0 0 0
252 if $devel_size;
253  
254 0 ▶ if ($phrases) {
Branch: 1 branch
BranchTrueFalseTotal
if ($phrases) 0 0 0
255 # Collect stats on phrase lengths
256 0 my $smallest = 100;
257 0 my $largest = 0;
258 0 my $total = 0;
259 0 my $avg_count = 0;
260 0 foreach my $code (256..$#{ $self->{cdict}{array} }) {
261 0 my $phrase = $self->{cdict}->phrase($code);
262 0 ▶ next unless defined $phrase;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $phrase 0 0 0
263 0 my $length = length $phrase;
264 0 ▶ $smallest = $length if $length < $smallest;
Branch: 1 branch
BranchTrueFalseTotal
if $length < $smallest 0 0 0
265 0 $total += $length;
266 0 $avg_count++;
267 0 ▶ $largest = $length if $length > $largest;
Branch: 1 branch
BranchTrueFalseTotal
if $length > $largest 0 0 0
268 }
269 0 my $average = $total / $avg_count;
270  
271 0 push @return, sprintf "phrase lengths, sm: %d, avg: %d, lg: %d, total: %d",
272 $smallest, $average, $largest, $total;
273 }
274 }
275 0 ▶ if (! $type || $type eq 'decompress') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $type or $type eq "decompress") 0 0 0
Condition0%0/3
not $type or $type eq "decompress"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $type or $type eq "decompress"
not $type $type eq "decompress"
Truth table0%0/3
not(A) or B
A= not $typeB= $type eq "decompress"
ABresult
1X1
011
000
276 push @return, sprintf "ddict: %.2f Kb [%d/%d codes used]",
277 (Devel::Size::total_size($self->{ddict}) +
278 Devel::Size::total_size($self->{ddict_reuse_codes}) +
279 Devel::Size::total_size($self->{ddict_usage})) / 1024,
280 0 $self->{dnext} - int @{ $self->{ddict_reuse_codes} },
281 $self->{code_max}
282 0 ▶ if $devel_size;
Branch: 1 branch
BranchTrueFalseTotal
if $devel_size 0 0 0
283 }
284  
285 0 return join("; ", @return);
286 }
287  
288 0 ▶sub dict_dump {
Subroutine dict_dump: not called CC=7 SCAR=40.3
Pod undocumented
289 0 my $self = shift;
290 0 my $return = " Index | Compress | Decompress \n";
291  
292 0 my $comp = $self->{cdict}{array}; #[ sort { $self->{cdict}{$a} <=> $self->{cdict}{$b} } keys %{ $self->{cdict} } ];
293 0 my $decomp = $self->{ddict};
294 0 ▶ my $count = $#{ $comp } > $#{ $decomp } ? $#{ $comp } : $#{ $decomp };
Branch: 1 branch
BranchTrueFalseTotal
$#{$comp;} > $#{$decomp;} ? : 0 0 0
295
296 0 my @char_map = qw/nul soh stx etx eot enq ack bel bs ht lf vt ff cr so si dle dc1 dc2 dc3 dc4 nak syn etb can em sub esc fs gs rs us/;
297 0 $char_map[127] = 'del';
298 0 ▶ my $show_invis = sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
299 0 my $return = '';
300 0 foreach my $char (split(//, shift)) {
301 0 my $num = ord $char;
302 0 ▶ if (($num < 32 || $num == 127 || $num > 128) && $num != 10) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num < 32 || $num == 127 || $num > 128 and $num != 10) { } 0 0 0
Condition0%0/9
$num < 32 || $num == 127 || $num > 128 and $num != 10
!ll&&!rl&&r
000
$num < 32 or $num == 127 or $num > 128
l!l&&r!l&&!r
000
$num < 32 or $num == 127
l!l&&r!l&&!r
000
MC/DC0%0/4
$num < 32 || $num == 127 || $num > 128 and $num != 10
$num < 32 $num == 127 $num > 128 $num != 10
Truth table0%0/7
A or B or C and D
A= $num < 32B= $num == 127C= $num > 128D= $num != 10
ABCDresult
000X0
1XX00
01X00
00100
1XX11
01X11
00111
303 0 ▶ if (defined $char_map[$num]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $char_map[$num]) { } 0 0 0
304 0 $return .= "^$char_map[$num]"."[$num]";
305 } else {
306 0 $return .= "[$num]";
307 }
308 } else {
309 0 $return .= $char;
310 }
311 }
312 0 return $return;
313 0 };
314  
315 0 for my $i (0..$count) {
316 0 ▶ my $c = defined $comp->[$i] ? $comp->[$i] : '[undef]';
Branch: 1 branch
BranchTrueFalseTotal
defined $comp->[$i] ? : 0 0 0
317 0 ▶ my $d = defined $decomp->[$i] ? $decomp->[$i] : '[undef]';
Branch: 1 branch
BranchTrueFalseTotal
defined $decomp->[$i] ? : 0 0 0
318 0 ▶ next if $c eq $d;
Branch: 1 branch
BranchTrueFalseTotal
if $c eq $d 0 0 0
319
320 0 ▶ printf " %-6d | %6s %s %6s\n", $i, $show_invis->($c), ($c eq $d ? '=' : '!'), $show_invis->($d);
Branch: 1 branch
BranchTrueFalseTotal
$c eq $d ? : 0 0 0
321 }
322 }
323  
324 2 1;
325  
326  
327 __END__
↑ Summary Progressive/Dict.pm »