-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathintegers_binary_encoding_with_huffman_coding.pl
256 lines (187 loc) · 5.96 KB
/
integers_binary_encoding_with_huffman_coding.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/perl
# Author: Trizen
# Date: 10 June 2023
# https://github.com/trizen
# Encode and decode a random list of integers into a binary string, using a DEFLATE-like approach + Huffman coding.
use 5.036;
use List::Util qw(max shuffle);
use constant {MAX_INT => 0b11111111111111111111111111111111};
# [distance value, offset bits]
my @DISTANCE_SYMBOLS = (map { [$_, 0] } 0 .. 4);
until ($DISTANCE_SYMBOLS[-1][0] > MAX_INT) {
push @DISTANCE_SYMBOLS, [int($DISTANCE_SYMBOLS[-1][0] * (4 / 3)), $DISTANCE_SYMBOLS[-1][1] + 1];
push @DISTANCE_SYMBOLS, [int($DISTANCE_SYMBOLS[-1][0] * (3 / 2)), $DISTANCE_SYMBOLS[-1][1]];
}
sub read_bit ($fh, $bitstring) {
if (($$bitstring // '') eq '') {
$$bitstring = unpack('b*', getc($fh) // return undef);
}
chop($$bitstring);
}
sub read_bits ($fh, $bits_len) {
my $data = '';
read($fh, $data, $bits_len >> 3);
$data = unpack('B*', $data);
while (length($data) < $bits_len) {
$data .= unpack('B*', getc($fh) // return undef);
}
if (length($data) > $bits_len) {
$data = substr($data, 0, $bits_len);
}
return $data;
}
sub delta_encode ($integers) {
my @deltas;
my $prev = 0;
unshift(@$integers, scalar(@$integers));
while (@$integers) {
my $curr = shift(@$integers);
push @deltas, $curr - $prev;
$prev = $curr;
}
my $bitstring = '';
foreach my $d (@deltas) {
if ($d == 0) {
$bitstring .= '0';
}
else {
my $t = sprintf('%b', abs($d));
$bitstring .= '1' . (($d < 0) ? '0' : '1') . ('1' x (length($t) - 1)) . '0' . substr($t, 1);
}
}
pack('B*', $bitstring);
}
sub delta_decode ($fh) {
my @deltas;
my $buffer = '';
my $len = 0;
for (my $k = 0 ; $k <= $len ; ++$k) {
my $bit = read_bit($fh, \$buffer);
if ($bit eq '0') {
push @deltas, 0;
}
else {
my $bit = read_bit($fh, \$buffer);
my $n = 0;
++$n while (read_bit($fh, \$buffer) eq '1');
my $d = oct('0b1' . join('', map { read_bit($fh, \$buffer) } 1 .. $n));
push @deltas, ($bit eq '1' ? $d : -$d);
}
if ($k == 0) {
$len = pop(@deltas);
}
}
my @acc;
my $prev = $len;
foreach my $d (@deltas) {
$prev += $d;
push @acc, $prev;
}
return \@acc;
}
# produce encode and decode dictionary from a tree
sub walk ($node, $code, $h, $rev_h) {
my $c = $node->[0] // return ($h, $rev_h);
if (ref $c) { walk($c->[$_], $code . $_, $h, $rev_h) for ('0', '1') }
else { $h->{$c} = $code; $rev_h->{$code} = $c }
return ($h, $rev_h);
}
# make a tree, and return resulting dictionaries
sub mktree_from_freq ($freq) {
my @nodes = map { [$_, $freq->{$_}] } sort { $a <=> $b } keys %$freq;
do { # poor man's priority queue
@nodes = sort { $a->[1] <=> $b->[1] } @nodes;
my ($x, $y) = splice(@nodes, 0, 2);
if (defined($x)) {
if (defined($y)) {
push @nodes, [[$x, $y], $x->[1] + $y->[1]];
}
else {
push @nodes, [[$x], $x->[1]];
}
}
} while (@nodes > 1);
walk($nodes[0], '', {}, {});
}
sub huffman_encode ($bytes, $dict) {
join('', @{$dict}{@$bytes});
}
sub huffman_decode ($bits, $hash) {
local $" = '|';
$bits =~ s/(@{[sort { length($a) <=> length($b) } keys %{$hash}]})/$hash->{$1}/gr; # very fast
}
sub create_huffman_entry ($bytes, $out_fh) {
my %freq;
++$freq{$_} for @$bytes;
my ($h, $rev_h) = mktree_from_freq(\%freq);
my $enc = huffman_encode($bytes, $h);
my $max_symbol = max(keys %freq) // 0;
my @freqs;
foreach my $i (0 .. $max_symbol) {
push @freqs, $freq{$i} // 0;
}
print $out_fh delta_encode(\@freqs);
print $out_fh pack("N", length($enc));
print $out_fh pack("B*", $enc);
}
sub decode_huffman_entry ($fh) {
my @freqs = @{delta_decode($fh)};
my %freq;
foreach my $i (0 .. $#freqs) {
if ($freqs[$i]) {
$freq{$i} = $freqs[$i];
}
}
my (undef, $rev_dict) = mktree_from_freq(\%freq);
foreach my $k (keys %$rev_dict) {
$rev_dict->{$k} = chr($rev_dict->{$k});
}
my $enc_len = unpack('N', join('', map { getc($fh) } 1 .. 4));
if ($enc_len > 0) {
return huffman_decode(read_bits($fh, $enc_len), $rev_dict);
}
return '';
}
sub encode_integers_deflate_like ($integers) {
my @symbols;
my $offset_bits = '';
foreach my $dist (@$integers) {
foreach my $i (0 .. $#DISTANCE_SYMBOLS) {
if ($DISTANCE_SYMBOLS[$i][0] > $dist) {
push @symbols, $i - 1;
if ($DISTANCE_SYMBOLS[$i - 1][1] > 0) {
$offset_bits .= sprintf('%0*b', $DISTANCE_SYMBOLS[$i - 1][1], $dist - $DISTANCE_SYMBOLS[$i - 1][0]);
}
last;
}
}
}
my $str = '';
open(my $out_fh, '>:raw', \$str);
create_huffman_entry(\@symbols, $out_fh);
print $out_fh pack('B*', $offset_bits);
return $str;
}
sub decode_integers_deflate_like ($str) {
open(my $fh, '<:raw', \$str);
my @symbols = unpack('C*', decode_huffman_entry($fh));
my $bits_len = 0;
foreach my $i (@symbols) {
$bits_len += $DISTANCE_SYMBOLS[$i][1];
}
my $bits = read_bits($fh, $bits_len);
my @distances;
foreach my $i (@symbols) {
push @distances, $DISTANCE_SYMBOLS[$i][0] + oct('0b' . substr($bits, 0, $DISTANCE_SYMBOLS[$i][1], ''));
}
return \@distances;
}
my @integers = shuffle(map { int(rand($_)) } 1 .. 1000);
my $str = encode_integers_deflate_like([@integers]);
say "Encoded length: ", length($str);
say "Rawdata length: ", length(join(' ', @integers));
my $decoded = decode_integers_deflate_like($str);
join(' ', @integers) eq join(' ', @$decoded) or die "Decoding error";
__END__
Encoded length: 1196
Rawdata length: 3590