Arithmetic coding/As a generalized change of radix: Difference between revisions
Arithmetic coding/As a generalized change of radix (view source)
Revision as of 20:20, 4 February 2016
, 8 years agoJ: rewrite
No edit summary |
(J: rewrite) |
||
Line 187:
=={{header|J}}==
Implementation:
<lang J>NB. generate a frequency dictionary from a reference string
aekDict=:verb define
(o{d);o{f
)
NB. encode a string against a reference dict
aekEnc=:verb define
NB. use string to generate a dict if none provided
(aekDict y) aekEnc y
:
'u F'=.x NB. unpack dictionary
b=. x:#y NB. numeric base
f=. b*F NB. absolute frequencies
i=. u i.y NB. character indices
c=. +/\0,}:f NB. cumulative frequencies
L=. b #. (i{c)**/\1,}:i{f NB. lower bound
p=. */i{f NB. product of character frequencies
e=. x:<.10^.p NB. number of decimal positions to drop
e,~<.(L+p)%10^e
)
aekDec=:adverb define
:
'u F'=. x NB. unpack dictionary
f=. m*F NB. frequencies of characters
c=.+/\0,}:f NB. cumulative frequencies
C=.<:}.c,m NB. id lookup table
N=. (* 10&^)/y NB. remainder being decoded
r=. '' NB. result of decode
for_d. m^x:i.-m do. NB. positional values
id=. <.N%d NB. character id
i=.C I.id NB. character index
N=.<.(N -(i{c)*d)%i{f NB. corrected remainder
r=.r,u{~i NB. accumulated result
end.
)
NB. task demo utility:
aek=:verb define
dict=. aekDict y
echo 'Dictionary:'
echo ' ',.(0{::dict),.' ',.":,.1{::dict
echo 'Length:'
echo ' ',":#y
echo 'Encoded:'
echo ' ',":dict aekEnc y
echo 'Decoded:'
echo ' ',":dict (#y) aekDec aekEnc y
)</lang>
Line 209 ⟶ 247:
<lang J> aek 'DABDDB'
Dictionary:
A 1r6
B 1r3
D 1r2
Length:
6
Encoded:
251 2
Decoded:
DABDDB
aek 'DABDDBBDDBA'
Dictionary:
A 2r11
B 4r11
D 5r11
Length:
11
Encoded:
167351 6
Decoded:
DABDDBBDDBA
aek 'ABRACADABRA'
Dictionary:
A 5r11
B 2r11
C 1r11
D 1r11
R 2r11
Length:
11
Encoded:
7954170 4
Decoded:
ABRACADABRA
aek 'TOBEORNOTTOBEORTOBEORNOT'
Dictionary:
B 1r8
E 1r8
N 1r12
O 1r3
R 1r8
T 5r24
Length:
24
Encoded:
1150764267498783364 15
Decoded:
TOBEORNOTTOBEORTOBEORNOT</lang>
Note that for this task we use our plaintext to generate our dictionary for decoding.
=={{header|Perl}}==
|