Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -86,7 +86,7 @@
for k = (make-vector-with-elt (aref octets i) 't)
for entry = (or (gethash k dictionary)
(if (equalp k dictionary-size)
(coerce (list w (aref w 0)) '(vector octet))
(vector-append1-new w (aref w 0))
(error "bad compresed entry at pos ~S" i)))
do (vector-append result entry)
(setf (gethash (make-vector-with-elt dictionary-size) dictionary)

View file

@ -0,0 +1,112 @@
class
APPLICATION
create
make
feature {NONE}
make
local
test: LINKED_LIST [INTEGER]
do
create test.make
test := compress ("TOBEORNOTTOBEORTOBEORNOT")
across
test as t
loop
io.put_string (t.item.out + " ")
end
io.new_line
io.put_string (decompress (test))
end
decompress (compressed: LINKED_LIST [INTEGER]): STRING
--Decompressed version of 'compressed'.
local
dictsize, i, k: INTEGER
dictionary: HASH_TABLE [STRING, INTEGER]
w, entry: STRING
char: CHARACTER_8
do
dictsize := 256
create dictionary.make (300)
create entry.make_empty
create Result.make_empty
from
i := 0
until
i > 256
loop
char := i.to_character_8
dictionary.put (char.out, i)
i := i + 1
end
w := compressed.first.to_character_8.out
compressed.go_i_th (1)
compressed.remove
Result := w
from
k := 1
until
k > compressed.count
loop
if attached dictionary.at (compressed [k]) as ata then
entry := ata
elseif compressed [k] = dictsize then
entry := w + w.at (1).out
else
io.put_string ("EXEPTION")
end
Result := Result + entry
dictsize := dictsize + 1
dictionary.put (w + entry.at (1).out, dictsize)
w := entry
k := k + 1
end
end
compress (uncompressed: STRING): LINKED_LIST [INTEGER]
-- Compressed version of 'uncompressed'.
local
dictsize: INTEGER
dictionary: HASH_TABLE [INTEGER, STRING]
i: INTEGER
w, wc: STRING
char: CHARACTER_8
do
dictsize := 256
create dictionary.make (256)
create w.make_empty
from
i := 0
until
i > 256
loop
char := i.to_character_8
dictionary.put (i, char.out)
i := i + 1
end
create Result.make
from
i := 1
until
i > uncompressed.count
loop
wc := w + uncompressed [i].out
if dictionary.has (wc) then
w := wc
else
Result.extend (dictionary.at (w))
dictSize := dictSize + 1
dictionary.put (dictSize, wc)
w := "" + uncompressed [i].out
end
i := i + 1
end
if w.count > 0 then
Result.extend (dictionary.at (w))
end
end
end

View file

@ -0,0 +1,48 @@
defmodule LZW do
def encode(str), do: encode(to_char_list(str), init, 256, [])
defp encode([h], d, _, out), do: Enum.reverse([Dict.get(d, [h]) | out])
defp encode([h|t], d, free, out) do
val = Dict.get(d, [h])
find_match(t, [h], val, d, free, out)
end
defp find_match([h|t], l, lastval, d, free, out) do
case Dict.fetch(d, [h|l]) do
{:ok, val} -> find_match(t, [h|l], val, d, free, out)
:error ->
d1 = Dict.put(d, [h|l], free)
encode([h|t], d1, free+1, [lastval | out])
end
end
defp find_match([], _, lastval, _, _, out), do: Enum.reverse([lastval | out])
defp init, do: init(255, Map.new)
defp init(0, d), do: d
defp init(n, d), do: init(n-1, Dict.put(d,[n],n))
def decode([h|t]) do
d = init1(Map.new)
val = Dict.get(d, h)
decode(t, val, 256, d, val)
end
defp decode([], _, _, _, l), do: Enum.reverse(l) |> to_string
defp decode([h|t], old, free, d, l) do
val = Dict.get(d, h)
add = [List.last(val) | old]
d1 = Dict.put(d, free, add)
decode(t, val, free+1, d1, val++l)
end
defp init1(d), do: init1(255, d)
defp init1(0, d), do: d
defp init1(n, d), do: init1(n-1, Dict.put(d, n, [n]))
end
str = "TOBEORNOTTOBEORTOBEORNOT"
IO.inspect enc = LZW.encode(str)
IO.inspect dec = LZW.decode(enc)
IO.inspect str == dec

View file

@ -0,0 +1,150 @@
*process source xref attributes or(!);
lzwt: Proc Options(main);
Dcl (LEFT,LENGTH,SUBSTR,TRANSLATE,TRIM,UNSPEC) Builtin;
Dcl SYSPRINT Print;
Dcl str Char(50) Var Init('TOBEORNOTTOBEORTOBEORNOT');
Dcl compressed Char(80) Var;
Dcl decompressed Char(80) Var;
Dcl 1 dict(0:300),
2 key Char(5) Var,
2 inx Bin Fixed(16) Unsigned;
Dcl dict_size Bin Fixed(31) Init(256);
Dcl hi Bin Fixed(16) Unsigned Init(65535);
Put Edit('str=',str)(Skip,a,a);
compressed = compress(str);
Put Edit(compressed)(Skip,a);
decompressed = decompress(compressed);
Put Edit('dec=',decompressed)(Skip,a,a);
If decompressed=str Then
Put Edit('decompression ok')(Skip,a);
Else
Put Edit('decompression not ok')(Skip,a);
compress: Proc(s) Returns(Char(80) Var);
Dcl s Char(*) Var;
Dcl res Char(80) Var;
Dcl i Bin Fixed(31);
Dcl c Char(1);
Dcl w Char(5) Var;
Dcl wc Char(5) Var;
dict.key='';
Dcl ii Bin Fixed(8) Unsigned;
Do i=0 To 255;
ii=i;
Unspec(c)=unspec(ii);
dict.key(i)=c;
dict.inx(i)=i;
End;
res='[';
w='';
Do i=1 To length(s);
c=substr(s,i,1);
wc=w!!c;
If dicti(wc)^=hi Then Do;
w=wc;
End;
Else Do;
res=res!!trim(dicti(w))!!', ';
Call dict_add(wc,dict_size);
w=c;
End;
End;
If w^='' Then
res=res!!trim(dicti(w))!!', ';
substr(res,length(res)-1,1)=']';
Return(res);
dicti: Proc(needle) Returns(Bin Fixed(31));
Dcl needle Char(*) Var;
Dcl i Bin Fixed(31);
Do i=1 To dict_size;
If dict.key(i)=needle Then
Return(i);
End;
Return(hi);
End;
dict_add: Proc(needle,dict_size);
Dcl needle Char(*) Var;
Dcl dict_size Bin Fixed(31);
dict.key(dict_size)=needle;
dict.inx(dict_size)=dict_size;
dict_size+=1;
End;
End;
decompress: Proc(s) Returns(Char(80) Var);
Dcl s Char(80) Var;
Dcl ss Char(80) Var;
Dcl words(50) Char(5) Var;
Dcl wn Bin Fixed(31);
Dcl ww Bin Fixed(31);
Dcl c Char(1);
Dcl entry Char(5) Var;
Dcl w Char(5) Var;
Dcl res Char(80) Var;
ss=translate(s,' ','[],');
Call mk_words(ss,words,wn);
dict.key='';
dict.inx=hi;
Dcl i Bin Fixed(31);
Dcl ii Bin Fixed(8) Unsigned;
Dcl dict(0:300) Char(5) Var;
Dcl dict_size Bin Fixed(31);
Do i=0 To 255;
ii=i;
Unspec(c)=unspec(ii);
dict(i)=c;
End;
dict_size=256;
ww=words(1);
w=dict(ww);
res=w;
Do i=2 To wn;
ww=words(i);
Select;
When(dict(ww)^='')
entry=dict(ww);
When(ww=dict_size)
entry=w!!substr(w,1,1);
Otherwise
Put Edit('Bad compressed k: ',ww)(Skip,a,a);
End;
res=res!!entry;
dict(dict_size)=w!!substr(entry,1,1);
dict_size+=1;
w=entry;
End;
Return(res);
End;
mk_words: Proc(st,arr,arrn);
Dcl st Char(*) Var;
Dcl sv Char(80) Var;
Dcl arr(*) Char(5) Var;
Dcl arrn Bin fixed(31);
Dcl elem Char(5) Var;
arrn=0;
sv=st!!' ';
elem='';
Do While(length(sv)>0);
If left(sv,1)=' ' Then Do;
If elem>'' Then Do;
arrn+=1;
arr(arrn)=elem;
elem='';
End;
End;
Else
elem=elem!!left(sv,1);
sv=substr(sv,2);
End;
End;
Return;
End;

View file

@ -1,4 +1,4 @@
sub compress(Str $uncompressed --> List) {
sub compress(Str $uncompressed --> Seq) {
my $dict-size = 256;
my %dictionary = (.chr => .chr for ^$dict-size);

View file

@ -1,5 +1,5 @@
/* REXX ---------------------------------------------------------------
* 20.07.2014 Wakter Pachl translated from Java
* 20.07.2014 Walter Pachl translated from Java
* 21.07.2014 WP allow for blanks in the string
*--------------------------------------------------------------------*/
Parse Arg str

View file

@ -0,0 +1,32 @@
/*REXX pgm compresses text using LZW (Lempel-Ziv-Welch), and reconstitutes it.*/
parse arg x /*get an optional argument from the CL.*/
if x=='' then x='"There is nothing permanent except change." Heraclitus [540-475 BC]'
say 'original text=' x
cypher=LZWc(x) /*compress text using the LZW algorithm*/
say 'reconstituted=' LZWd(cypher)
say ' LZW integers=' cypher /*also display the LZW integers used. */
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
LZWc: procedure; parse arg y,,w $ @.; #=256 /*the LZW compress algorithm.*/
do j=0 for 256; _=d2c(j); @._=j; end /*j*/
do k=1 for length(y); _=w || substr(y,k,1)
if @._=='' then do; $=$ @.w
@._=#; #=#+1
w=substr(y,k,1)
end
else w=_
end /*k*/
return strip($ @.w) /*remove any superfluous blanks*/
/*────────────────────────────────────────────────────────────────────────────*/
LZWd: procedure; parse arg w y,,@.; #=256 /*the LZW decompress algorithm.*/
do j=0 for 256; @.j=d2c(j); end /*j*/
$=@.w; w=$
do k=1 for words(y); _=word(y,k)
if @._\=='' | @.k==' ' then ?=@._
else if _=# then ?=w || left(w,1)
$=$ || ?
@.#=w || left(?,1); #=#+1
w=?
end /*k*/
return $