September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
28
Task/LZW-compression/Zkl/lzw-compression-1.zkl
Normal file
28
Task/LZW-compression/Zkl/lzw-compression-1.zkl
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
fcn lzwCompress(uncompressed){ // text-->list of 12 bit ints
|
||||
dictionary:=(256).pump(Dictionary(),fcn(n){ return(n.toChar(),n) });
|
||||
w,compressed:="",List();
|
||||
foreach c in (uncompressed){
|
||||
wc:=w+c;
|
||||
if(dictionary.holds(wc)) w=wc;
|
||||
else{
|
||||
compressed.append(dictionary[w]); // 12 bits
|
||||
dictionary[wc]=dictionary.len();
|
||||
w=c;
|
||||
}
|
||||
}
|
||||
if(w) compressed.append(dictionary[w]);
|
||||
compressed
|
||||
}
|
||||
fcn lzwUncompress(compressed){ // compressed data-->text
|
||||
dictionary:=(256).pump(Dictionary(),fcn(n){ return(n,n.toChar()) });
|
||||
w,decommpressed:=dictionary[compressed[0]],Data(Void,w);
|
||||
foreach k in (compressed[1,*]){
|
||||
if(dictionary.holds(k)) entry:=dictionary[k];
|
||||
else if(k==dictionary.len()) entry:=w+w[0];
|
||||
else throw(Exception.ValueError("Invalid compressed data"));
|
||||
decommpressed.append(entry);
|
||||
dictionary.add(dictionary.len(),w+entry[0]);
|
||||
w=entry;
|
||||
}
|
||||
decommpressed.text
|
||||
}
|
||||
4
Task/LZW-compression/Zkl/lzw-compression-2.zkl
Normal file
4
Task/LZW-compression/Zkl/lzw-compression-2.zkl
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
compressed:=lzwCompress("TOBEORNOTTOBEORTOBEORNOT");
|
||||
compressed.toString(*).println();
|
||||
|
||||
lzwUncompress(compressed).println();
|
||||
Loading…
Add table
Add a link
Reference in a new issue