line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Compress::Huffman; |
2
|
|
|
|
|
|
|
require Exporter; |
3
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
4
|
|
|
|
|
|
|
@EXPORT_OK = qw//; |
5
|
|
|
|
|
|
|
%EXPORT_TAGS = ( |
6
|
|
|
|
|
|
|
all => \@EXPORT_OK, |
7
|
|
|
|
|
|
|
); |
8
|
2
|
|
|
2
|
|
99679
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
59
|
|
9
|
2
|
|
|
2
|
|
10
|
use strict; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
34
|
|
10
|
2
|
|
|
2
|
|
8
|
use Carp; |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
131
|
|
11
|
2
|
|
|
2
|
|
13
|
use Scalar::Util 'looks_like_number'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
97
|
|
12
|
2
|
|
|
2
|
|
675
|
use POSIX qw/ceil/; |
|
2
|
|
|
|
|
9987
|
|
|
2
|
|
|
|
|
9
|
|
13
|
2
|
|
|
2
|
|
2878
|
use JSON::Create '0.22', 'create_json'; |
|
2
|
|
|
|
|
1635
|
|
|
2
|
|
|
|
|
239
|
|
14
|
2
|
|
|
2
|
|
777
|
use JSON::Parse '0.42', 'parse_json'; |
|
2
|
|
|
|
|
1632
|
|
|
2
|
|
|
|
|
220
|
|
15
|
|
|
|
|
|
|
our $VERSION = '0.07'; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# eps is the allowed floating point error for summing the values of |
18
|
|
|
|
|
|
|
# the symbol table to ensure they form a probability distribution. |
19
|
|
|
|
|
|
|
|
20
|
2
|
|
|
2
|
|
12
|
use constant 'eps' => 0.0001; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
3391
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# Private methods/functions |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# Add the prefix $i to everything underneath us. |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub addcodetosubtable |
27
|
|
|
|
|
|
|
{ |
28
|
21
|
|
|
21
|
0
|
50
|
my ($fakes, $h, $k, $size, $i) = @_; |
29
|
21
|
|
|
|
|
32
|
my $subhuff = $fakes->{$k}; |
30
|
21
|
|
|
|
|
42
|
for my $j (0..$size - 1) { |
31
|
47
|
|
|
|
|
68
|
my $subk = $subhuff->[$j]; |
32
|
47
|
100
|
|
|
|
85
|
if ($subk =~ /^fake/) { |
33
|
11
|
|
|
|
|
40
|
addcodetosubtable ($fakes, $h, $subk, $size, $i); |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
else { |
36
|
36
|
|
|
|
|
88
|
$h->{$subk} = $i . $h->{$subk}; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# Public methods below here |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub new |
44
|
|
|
|
|
|
|
{ |
45
|
3
|
|
|
3
|
1
|
625
|
return bless {}; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub symbols |
49
|
|
|
|
|
|
|
{ |
50
|
|
|
|
|
|
|
# Object and the table of symbols. |
51
|
5
|
|
|
5
|
1
|
2213
|
my ($o, $s, %options) = @_; |
52
|
5
|
50
|
|
|
|
16
|
if ($options{verbose}) { |
53
|
5
|
|
|
|
|
16
|
$o->{verbose} = 1; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
else { |
56
|
0
|
|
|
|
|
0
|
$o->{verbose} = undef; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
# Check $s is a hash reference. |
59
|
5
|
50
|
|
|
|
19
|
if (ref $s ne 'HASH') { |
60
|
0
|
|
|
|
|
0
|
croak "Use as \$o->symbols (\\\%symboltable, options...)"; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
# Copy the symbol table into our own thing. We need to put extra |
63
|
|
|
|
|
|
|
# symbols in to it. |
64
|
5
|
|
|
|
|
24
|
my %c = %$s; |
65
|
5
|
|
|
|
|
14
|
$o->{c} = \%c; |
66
|
|
|
|
|
|
|
# The number of symbols we encode with this Huffman code. |
67
|
5
|
|
|
|
|
11
|
my $nentries = scalar keys %$s; |
68
|
5
|
50
|
|
|
|
15
|
if (! $nentries) { |
69
|
0
|
|
|
|
|
0
|
croak "Symbol table has no entries"; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
# Check we have numbers. |
72
|
5
|
|
|
|
|
16
|
for my $k (keys %$s) { |
73
|
27
|
50
|
|
|
|
60
|
if (! looks_like_number ($s->{$k})) { |
74
|
0
|
|
|
|
|
0
|
croak "Non-numerical value '$s->{$k}' for key '$k'"; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
} |
77
|
5
|
50
|
|
|
|
11
|
if ($o->{verbose}) { |
78
|
5
|
|
|
|
|
68
|
print "Checked for numerical keys.\n"; |
79
|
|
|
|
|
|
|
} |
80
|
5
|
|
|
|
|
13
|
my $size = $options{size}; |
81
|
5
|
100
|
|
|
|
13
|
if (! defined $size) { |
82
|
3
|
|
|
|
|
6
|
$size = 2; |
83
|
|
|
|
|
|
|
} |
84
|
5
|
50
|
33
|
|
|
25
|
if ($size < 2 || int ($size) != $size) { |
85
|
0
|
|
|
|
|
0
|
croak "Bad size $size for Huffman table, must be integer >= 2"; |
86
|
|
|
|
|
|
|
} |
87
|
5
|
50
|
33
|
|
|
13
|
if ($size > 10 && ! $options{alphabet}) { |
88
|
0
|
|
|
|
|
0
|
croak "Use \$o->symbols (\%t, alphabet => ['a', 'b',...]) for table sizes bigger than 10"; |
89
|
|
|
|
|
|
|
} |
90
|
5
|
50
|
|
|
|
15
|
if ($o->{verbose}) { |
91
|
5
|
|
|
|
|
22
|
print "Set size of Huffman code alphabet to $size.\n"; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
# If this is supposed to be a probability distribution, check |
94
|
5
|
|
|
|
|
11
|
my $notprob = $options{notprob}; |
95
|
5
|
100
|
|
|
|
9
|
if ($notprob) { |
96
|
2
|
|
|
|
|
4
|
my $total = 0.0; |
97
|
2
|
|
|
|
|
6
|
for my $k (keys %$s) { |
98
|
6
|
|
|
|
|
9
|
my $value = $s->{$k}; |
99
|
6
|
50
|
|
|
|
14
|
if ($value < 0.0) { |
100
|
0
|
|
|
|
|
0
|
croak "Negative weight $value for symbol $k"; |
101
|
|
|
|
|
|
|
} |
102
|
6
|
|
|
|
|
12
|
$total += $value; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
else { |
106
|
3
|
|
|
|
|
7
|
my $total = 0.0; |
107
|
3
|
|
|
|
|
7
|
for my $k (keys %$s) { |
108
|
21
|
|
|
|
|
24
|
my $value = $s->{$k}; |
109
|
21
|
50
|
33
|
|
|
57
|
if ($value < 0.0 || $value > 1.0) { |
110
|
0
|
|
|
|
|
0
|
croak "Value $value for symbol $k is not a probability; use \$o->symbols (\\\%s, notprob => 1) if not a probability distribution"; |
111
|
|
|
|
|
|
|
} |
112
|
21
|
|
|
|
|
28
|
$total += $s->{$k}; |
113
|
|
|
|
|
|
|
} |
114
|
3
|
50
|
|
|
|
11
|
if (abs ($total - 1.0) > eps) { |
115
|
0
|
|
|
|
|
0
|
croak "Input values don't sum to 1.0; use \$o->symbols (\\\%s, notprob => 1) if not a probability distribution"; |
116
|
|
|
|
|
|
|
} |
117
|
3
|
50
|
|
|
|
21
|
if ($o->{verbose}) { |
118
|
3
|
|
|
|
|
23
|
print "Is a valid probability distribution (total = $total).\n"; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
# The number of tables. We need $t - 1 pointers to tables, which |
122
|
|
|
|
|
|
|
# each require one table entry, so $t is the smallest number which |
123
|
|
|
|
|
|
|
# satisfies |
124
|
|
|
|
|
|
|
# |
125
|
|
|
|
|
|
|
# $t * $size >= $nentries + $t - 1 |
126
|
|
|
|
|
|
|
|
127
|
5
|
|
|
|
|
46
|
my $t = ceil (($nentries -1) / ($size - 1)); |
128
|
5
|
50
|
|
|
|
49
|
if ($o->{verbose}) { |
129
|
5
|
|
|
|
|
38
|
print "This symbol table requires $t Huffman tables of size $size.\n"; |
130
|
|
|
|
|
|
|
} |
131
|
5
|
|
|
|
|
13
|
my $ndummies = 0; |
132
|
5
|
100
|
|
|
|
12
|
if ($size > 2) { |
133
|
|
|
|
|
|
|
# The number of dummy entries we need is |
134
|
2
|
|
|
|
|
5
|
my $ndummies = $t * ($size - 1) - $nentries + 1; |
135
|
2
|
50
|
|
|
|
6
|
if ($o->{verbose}) { |
136
|
2
|
|
|
|
|
6
|
print "The Huffman tables need $ndummies dummy entries.\n"; |
137
|
|
|
|
|
|
|
} |
138
|
2
|
50
|
|
|
|
6
|
if ($ndummies > 0) { |
139
|
|
|
|
|
|
|
# Insert $ndummies dummy entries with probability zero into |
140
|
|
|
|
|
|
|
# our copy of the symbol table. |
141
|
0
|
|
|
|
|
0
|
for (0..$ndummies - 1) { |
142
|
0
|
|
|
|
|
0
|
my $dummy = "dummy$_"; |
143
|
0
|
0
|
|
|
|
0
|
if ($c{$dummy}) { |
144
|
|
|
|
|
|
|
# This is a bug not a user error. |
145
|
0
|
|
|
|
|
0
|
die "The symbol table already has an entry '$dummy'"; |
146
|
|
|
|
|
|
|
} |
147
|
0
|
|
|
|
|
0
|
$c{$dummy} = 0.0; |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
# The end-product, the Huffman encoding of the symbol table. |
152
|
5
|
|
|
|
|
22
|
my %h; |
153
|
5
|
|
|
|
|
8
|
my $nfake = 0; |
154
|
5
|
|
|
|
|
7
|
my %fakes; |
155
|
5
|
|
|
|
|
12
|
while ($nfake < $t) { |
156
|
15
|
50
|
|
|
|
25
|
if ($o->{verbose}) { |
157
|
15
|
|
|
|
|
56
|
print "Making key list for sub-table $nfake / $t.\n"; |
158
|
|
|
|
|
|
|
} |
159
|
15
|
|
|
|
|
23
|
my $total = 0; |
160
|
15
|
|
|
|
|
22
|
my @keys; |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
# Find the $size keys with the minimum value and go through, |
163
|
|
|
|
|
|
|
# picking them out. |
164
|
|
|
|
|
|
|
|
165
|
15
|
|
|
|
|
35
|
for my $i (0..$size - 1) { |
166
|
|
|
|
|
|
|
# This method is from |
167
|
|
|
|
|
|
|
# https://stackoverflow.com/questions/1185822/how-do-i-create-or-test-for-nan-or-infinity-in-perl/1185828#1185828 |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
# inf doesn't work on some versions of Perl, see |
170
|
|
|
|
|
|
|
# http://www.cpantesters.org/cpan/report/314e30b0-6bfb-1014-8e6c-c1e3e4f7669d |
171
|
37
|
|
|
|
|
50
|
my $min = 9**9**9; |
172
|
37
|
|
|
|
|
44
|
my $minkey; |
173
|
37
|
|
|
|
|
120
|
for my $k (sort keys %c) { |
174
|
132
|
100
|
|
|
|
256
|
if ($c{$k} < $min) { |
175
|
43
|
|
|
|
|
56
|
$min = $c{$k}; |
176
|
43
|
|
|
|
|
61
|
$minkey = $k; |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
} |
179
|
37
|
|
|
|
|
53
|
$total += $min; |
180
|
37
|
50
|
|
|
|
62
|
if ($o->{verbose}) { |
181
|
37
|
|
|
|
|
182
|
print "Choosing $minkey with $min for symbol $i\n"; |
182
|
|
|
|
|
|
|
} |
183
|
37
|
|
|
|
|
70
|
delete $c{$minkey}; |
184
|
37
|
|
|
|
|
53
|
push @keys, $minkey; |
185
|
37
|
|
|
|
|
94
|
$h{$minkey} = $i; |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
# The total weight of this table. |
188
|
|
|
|
|
|
|
# The next table |
189
|
15
|
|
|
|
|
23
|
my @huff; |
190
|
15
|
|
|
|
|
25
|
for my $i (0..$size - 1) { |
191
|
37
|
|
|
|
|
50
|
my $k = $keys[$i]; |
192
|
37
|
50
|
|
|
|
64
|
if (! defined $k) { |
193
|
0
|
|
|
|
|
0
|
last; |
194
|
|
|
|
|
|
|
} |
195
|
37
|
|
|
|
|
47
|
push @huff, $k; |
196
|
37
|
100
|
|
|
|
83
|
if ($k =~ /^fake/) { |
197
|
10
|
|
|
|
|
26
|
addcodetosubtable (\%fakes, \%h, $k, $size, $i); |
198
|
|
|
|
|
|
|
} |
199
|
|
|
|
|
|
|
} |
200
|
15
|
|
|
|
|
29
|
my $fakekey = 'fake'.$nfake; |
201
|
15
|
|
|
|
|
26
|
$c{$fakekey} = $total; |
202
|
15
|
|
|
|
|
26
|
$fakes{$fakekey} = \@huff; |
203
|
15
|
|
|
|
|
39
|
$nfake++; |
204
|
|
|
|
|
|
|
} |
205
|
5
|
50
|
|
|
|
12
|
if ($o->{verbose}) { |
206
|
5
|
|
|
|
|
17
|
print "Deleting dummy keys.\n"; |
207
|
|
|
|
|
|
|
} |
208
|
5
|
|
|
|
|
18
|
for my $k (keys %h) { |
209
|
37
|
100
|
|
|
|
105
|
if ($k =~ /fake|dummy/) { |
210
|
10
|
|
|
|
|
25
|
delete $h{$k}; |
211
|
|
|
|
|
|
|
} |
212
|
|
|
|
|
|
|
} |
213
|
5
|
|
|
|
|
51
|
$o->{h} = \%h; |
214
|
5
|
|
|
|
|
11
|
$o->{s} = $s; |
215
|
|
|
|
|
|
|
# Blank this out for the case that the user inserts a new symbol |
216
|
|
|
|
|
|
|
# table, etc. |
217
|
5
|
|
|
|
|
10
|
$o->{value_re} = undef; |
218
|
5
|
|
|
|
|
28
|
$o->{r} = undef; |
219
|
|
|
|
|
|
|
} |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
sub xl |
222
|
|
|
|
|
|
|
{ |
223
|
1
|
|
|
1
|
1
|
229
|
my ($o) = @_; |
224
|
1
|
|
|
|
|
3
|
my $h = $o->{h}; |
225
|
1
|
|
|
|
|
2
|
my $s = $o->{s}; |
226
|
1
|
50
|
33
|
|
|
6
|
croak "Bad object" unless $h && $s; |
227
|
1
|
|
|
|
|
2
|
my $len = 0.0; |
228
|
1
|
|
|
|
|
2
|
my $total = 0.0; |
229
|
1
|
|
|
|
|
3
|
for my $k (keys %$h) { |
230
|
3
|
|
|
|
|
7
|
$len += length ($h->{$k}) * $s->{$k}; |
231
|
3
|
|
|
|
|
5
|
$total += $s->{$k}; |
232
|
3
|
50
|
|
|
|
12
|
if ($o->{verbose}) { |
233
|
3
|
|
|
|
|
13
|
print "$k $h->{$k} $s->{$k} $len\n"; |
234
|
|
|
|
|
|
|
} |
235
|
|
|
|
|
|
|
} |
236
|
1
|
|
|
|
|
4
|
return $len / $total; |
237
|
|
|
|
|
|
|
} |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
sub table |
240
|
|
|
|
|
|
|
{ |
241
|
0
|
|
|
0
|
1
|
0
|
my ($o) = @_; |
242
|
0
|
|
|
|
|
0
|
return $o->{h}; |
243
|
|
|
|
|
|
|
} |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
sub encode_array |
246
|
|
|
|
|
|
|
{ |
247
|
0
|
|
|
0
|
1
|
0
|
my ($o, $msg) = @_; |
248
|
0
|
|
|
|
|
0
|
my @output; |
249
|
0
|
|
|
|
|
0
|
for my $k (@$msg) { |
250
|
0
|
|
|
|
|
0
|
my $h = $o->{h}{$k}; |
251
|
0
|
0
|
|
|
|
0
|
if (! defined $h) { |
252
|
0
|
|
|
|
|
0
|
carp "Symbol '$k' is not in the symbol table"; |
253
|
0
|
|
|
|
|
0
|
next; |
254
|
|
|
|
|
|
|
} |
255
|
0
|
|
|
|
|
0
|
push @output, $h; |
256
|
|
|
|
|
|
|
} |
257
|
0
|
|
|
|
|
0
|
return \@output; |
258
|
|
|
|
|
|
|
} |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
sub encode |
261
|
|
|
|
|
|
|
{ |
262
|
0
|
|
|
0
|
1
|
0
|
my ($o, $msg) = @_; |
263
|
0
|
|
|
|
|
0
|
my $output = $o->encode_array ($msg); |
264
|
0
|
|
|
|
|
0
|
return join '', @$output; |
265
|
|
|
|
|
|
|
} |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
sub decode |
268
|
|
|
|
|
|
|
{ |
269
|
0
|
|
|
0
|
1
|
0
|
my ($o, $msg) = @_; |
270
|
0
|
0
|
|
|
|
0
|
if (! $o->{value_re}) { |
271
|
0
|
|
|
|
|
0
|
my @values = sort {length ($b) <=> length ($a)} values %{$o->{h}}; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
272
|
0
|
|
|
|
|
0
|
my $value_re = '(' . join ('|', @values) . ')'; |
273
|
0
|
|
|
|
|
0
|
$o->{value_re} = $value_re; |
274
|
0
|
0
|
|
|
|
0
|
if ($o->{verbose}) { |
275
|
0
|
|
|
|
|
0
|
print "Value regex is ", $o->{value_re}, "\n"; |
276
|
|
|
|
|
|
|
} |
277
|
|
|
|
|
|
|
} |
278
|
0
|
0
|
|
|
|
0
|
if (! $o->{r}) { |
279
|
0
|
|
|
|
|
0
|
$o->{r} = {reverse %{$o->{h}}}; |
|
0
|
|
|
|
|
0
|
|
280
|
|
|
|
|
|
|
} |
281
|
0
|
|
|
|
|
0
|
my @output; |
282
|
0
|
|
|
|
|
0
|
while ($msg =~ s/^$o->{value_re}//) { |
283
|
0
|
|
|
|
|
0
|
push @output, $o->{r}{$1}; |
284
|
|
|
|
|
|
|
} |
285
|
0
|
0
|
|
|
|
0
|
if (length ($msg) > 0) { |
286
|
0
|
|
|
|
|
0
|
carp "Input starting from $msg was not Huffman encoded using this table"; |
287
|
|
|
|
|
|
|
} |
288
|
0
|
|
|
|
|
0
|
return \@output; |
289
|
|
|
|
|
|
|
} |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
sub save |
292
|
|
|
|
|
|
|
{ |
293
|
1
|
|
|
1
|
1
|
6
|
my ($o) = @_; |
294
|
1
|
|
|
|
|
12
|
return create_json ($o); |
295
|
|
|
|
|
|
|
} |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
sub load |
298
|
|
|
|
|
|
|
{ |
299
|
1
|
|
|
1
|
1
|
5
|
my ($o, $data) = @_; |
300
|
1
|
|
|
|
|
10
|
my $input = parse_json ($data); |
301
|
1
|
|
|
|
|
4
|
for my $k (keys %$input) { |
302
|
6
|
|
|
|
|
10
|
$o->{$k} = $input->{$k}; |
303
|
|
|
|
|
|
|
} |
304
|
|
|
|
|
|
|
} |
305
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
1; |