forked from trizen/perl-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lzss_encoding_hash_table_fast.pl
141 lines (100 loc) · 2.95 KB
/
lzss_encoding_hash_table_fast.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
#!/usr/bin/perl
# Author: Trizen
# Date: 02 May 2024
# https://github.com/trizen
# Implementation of LZSS encoding, using an hash table.
# A non-optimal, but very fast approach.
use 5.036;
sub lzss_encode($str) {
my $la = 0;
my @symbols = unpack('C*', $str);
my $end = $#symbols;
my $min_len = 4; # minimum match length
my $max_len = 255; # maximum match length
my $max_dist = (1 << 16) - 1; # maximum match distance
my (@literals, @distances, @lengths, %table);
while ($la <= $end) {
my $best_n = 1;
my $best_p = $la;
my $lookahead = substr($str, $la, $min_len);
if (exists($table{$lookahead}) and $la - $table{$lookahead} <= $max_dist) {
my $p = $table{$lookahead};
my $n = $min_len;
while ($n <= $max_len and $la + $n <= $end and $symbols[$la + $n - 1] == $symbols[$p + $n - 1]) {
++$n;
}
$best_p = $p;
$best_n = $n;
}
$table{$lookahead} = $la;
if ($best_n > $min_len) {
push @lengths, $best_n - 1;
push @distances, $la - $best_p;
push @literals, undef;
$la += $best_n - 1;
}
else {
push @lengths, (0) x $best_n;
push @distances, (0) x $best_n;
push @literals, @symbols[$best_p .. $best_p + $best_n - 1];
$la += $best_n;
}
}
return (\@literals, \@distances, \@lengths);
}
sub lzss_decode ($literals, $distances, $lengths) {
my @data;
my $data_len = 0;
foreach my $i (0 .. $#$lengths) {
if ($lengths->[$i] == 0) {
push @data, $literals->[$i];
$data_len += 1;
next;
}
my $length = $lengths->[$i];
my $dist = $distances->[$i];
foreach my $j (1 .. $length) {
push @data, $data[$data_len + $j - $dist - 1];
}
$data_len += $length;
}
pack('C*', @data);
}
my $string = "abbaabbaabaabaaaa";
my ($literals, $distances, $lengths) = lzss_encode($string);
my $decoded = lzss_decode($literals, $distances, $lengths);
$string eq $decoded or die "error: <<$string>> != <<$decoded>>";
foreach my $i (0 .. $#$literals) {
if ($lengths->[$i] == 0) {
say $literals->[$i];
}
else {
say "[$distances->[$i], $lengths->[$i]]";
}
}
foreach my $file (__FILE__, $^X) { # several tests
my $string = do {
open my $fh, '<:raw', $file or die "error for <<$file>>: $!";
local $/;
<$fh>;
};
my ($literals, $distances, $lengths) = lzss_encode($string);
my $decoded = lzss_decode($literals, $distances, $lengths);
say "Ratio: ", scalar(@$literals) / scalar(grep { defined($_) } @$literals);
$string eq $decoded or die "error: <<$string>> != <<$decoded>>";
}
__END__
97
98
98
97
[4, 6]
97
97
98
97
97
97
97
Ratio: 1.36301369863014
Ratio: 1.46043165467626