-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy patharithmetic_coding_mpz.pl
163 lines (120 loc) · 4.06 KB
/
arithmetic_coding_mpz.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
#!/usr/bin/perl
# Author: Trizen
# Date: 11 February 2016
# Edit: 31 July 2023
# https://github.com/trizen
# Arithmetic coding, implemented using big integers.
# See also:
# https://en.wikipedia.org/wiki/Arithmetic_coding#Arithmetic_coding_as_a_generalized_change_of_radix
use 5.036;
use Math::GMPz;
use List::Util qw(sum);
sub cumulative_freq ($freq) {
my %cf;
my $total = 0;
foreach my $c (sort { $a <=> $b } keys %$freq) {
$cf{$c} = $total;
$total += $freq->{$c};
}
return %cf;
}
sub ac_encode ($bytes_arr) {
my @chars = @$bytes_arr;
# The frequency characters
my %freq;
++$freq{$_} for @chars;
# Create the cumulative frequency table
my %cf = cumulative_freq(\%freq);
# Limit and base
my $base = Math::GMPz->new(scalar @chars);
# Lower bound
my $L = Math::GMPz->new(0);
# Product of all frequencies
my $pf = Math::GMPz->new(1);
# Each term is multiplied by the product of the
# frequencies of all previously occurring symbols
foreach my $c (@chars) {
Math::GMPz::Rmpz_mul($L, $L, $base);
Math::GMPz::Rmpz_addmul_ui($L, $pf, $cf{$c});
Math::GMPz::Rmpz_mul_ui($pf, $pf, $freq{$c});
}
# Upper bound
my $U = $L + $pf;
# Compute the power for left shift
my $pow = Math::GMPz::Rmpz_sizeinbase($pf, 2) - 1;
# Set $enc to (U-1) divided by 2^pow
my $enc = ($U - 1) >> $pow;
# Remove any divisibility by 2
if ($enc > 0 and Math::GMPz::Rmpz_even_p($enc)) {
$pow += Math::GMPz::Rmpz_remove($enc, $enc, Math::GMPz->new(2));
}
my $bin = Math::GMPz::Rmpz_get_str($enc, 2);
return ($bin, $pow, \%freq);
}
sub ac_decode ($bits, $pow2, $freq) {
# Decode the bits into an integer
my $enc = Math::GMPz->new($bits, 2);
$enc <<= $pow2;
my $base = sum(values %$freq) // 0;
if ($base == 0) {
return [];
}
elsif ($base == 1) {
return [keys %$freq];
}
# Create the cumulative frequency table
my %cf = cumulative_freq($freq);
# Create the dictionary
my %dict;
while (my ($k, $v) = each %cf) {
$dict{$v} = $k;
}
# Fill the gaps in the dictionary
my $lchar;
foreach my $i (0 .. $base - 1) {
if (exists $dict{$i}) {
$lchar = $dict{$i};
}
elsif (defined $lchar) {
$dict{$i} = $lchar;
}
}
my $div = Math::GMPz::Rmpz_init();
my @dec;
# Decode the input number
for (my $pow = Math::GMPz->new($base)**($base - 1) ; Math::GMPz::Rmpz_sgn($pow) > 0 ; Math::GMPz::Rmpz_tdiv_q_ui($pow, $pow, $base)) {
Math::GMPz::Rmpz_tdiv_q($div, $enc, $pow);
my $c = $dict{$div};
my $fv = $freq->{$c};
my $cv = $cf{$c};
Math::GMPz::Rmpz_submul_ui($enc, $pow, $cv);
Math::GMPz::Rmpz_tdiv_q_ui($enc, $enc, $fv);
push @dec, $c;
}
return \@dec;
}
#
## Run some tests
#
foreach my $str (
'',
'a',
'this is a message for you to encode and to decode correctly!',
join('', 'a' .. 'z', 0 .. 9, 'A' .. 'Z', 0 .. 9),
qw(DABDDB DABDDBBDDBA ABBDDD ABRACADABRA CoMpReSSeD Sidef Trizen google TOBEORNOTTOBEORTOBEORNOT),
'In a positional numeral system the radix, or base, is numerically equal to a number of different symbols '
. 'used to express the number. For example, in the decimal system the number of symbols is 10, namely 0, 1, 2, '
. '3, 4, 5, 6, 7, 8, and 9. The radix is used to express any finite integer in a presumed multiplier in polynomial '
. 'form. For example, the number 457 is actually 4×102 + 5×101 + 7×100, where base 10 is presumed but not shown explicitly.'
) {
my @bytes = unpack('C*', $str);
my ($enc, $len, $freq) = ac_encode(\@bytes);
my $dec_bytes = ac_decode($enc, $len, $freq);
my $dec = pack('C*', @$dec_bytes);
say "Encoded: $enc";
say "Decoded: $dec";
if ($str ne $dec) {
die "\tHowever that is incorrect!";
}
say "-" x 80;
}