LZW compression: Difference between revisions
Applesoft BASIC
m (→{{header|Julia}}: update from Julia 0.6 to 1.1.1) |
(Applesoft BASIC) |
||
(44 intermediate revisions by 25 users not shown) | |||
Line 8:
=={{header|11l}}==
{{trans|Python}}
<
V dict_size = 256
V dictionary = Dict((0 .< dict_size).map(i -> (String(Char(code' i)), i)))
Line 51:
V compressed = compress(‘TOBEORNOTTOBEORTOBEORNOT’)
print(compressed)
print(decompress(&compressed))</
=={{header|Ada}}==
Line 57:
lzw.ads:
<
MAX_CODE : constant := 4095;
Line 67:
function Decompress (Data : in Compressed_Data) return String;
end LZW;</
lzw.adb:
<
with Ada.Strings.Unbounded;
Line 185:
end Decompress;
end LZW;</
test.adb:
<
with Ada.Text_IO;
Line 208:
Text_IO.Put_Line (Cleartext);
end;
end Test;</
=={{header|Applesoft BASIC}}==
{{trans|BBC BASIC}}
<syntaxhighlight lang="BASIC"> 0 PLAIN$ = "TOBEORNOTTOBEORTOBEORNOT"
10 GOSUB 200"ENCODE PLAIN$
20 FOR I = 1 TO LEN (ENCODE$) STEP 2
30 PRINT S$ ASC ( MID$ (ENCODE$,I)) + 256 * ASC ( MID$ (ENCODE$,I + 1));
40 LET S$ = " "
50 NEXT
60 PRINT
70 GOSUB 300"DECODE ENCODE$
80 PRINT PLAIN$;
90 END
100 FOR C = 0 TO 1E9 STEP 0
110 IF I > S THEN RETURN
120 FOR D = 1 TO L - 1
130 IF W$ < > DICT$(D) THEN NEXT D
140 IF D > = L THEN RETURN
150 LET I = I + 1
160 LET W$ = W$ + MID$ (PLAIN$,I,1)
170 LET C = D
180 NEXT C
190 RETURN
REM ENCODE PLAIN$ RETURN ENCODE$
200 IF NOT DI THEN DIM DICT$(4095)
210 FOR I = 0 TO 255:DICT$(I) = CHR$ (I): NEXT
220 LET DI = 1 : L = I : S = LEN (PLAIN$):ENCODE$ = ""
230 LET W$ = LEFT$ (PLAIN$,1)
240 FOR I = 1 TO 1E9 STEP 0
250 GOSUB 100
260 LET DICT$(L) = W$:L = L + 1:W$ = RIGHT$ (W$,1)
270 LET C% = C / 256:ENCODE$ = ENCODE$ + CHR$ (C - C% * 256) + CHR$ (C%)
280 IF I < = S THEN NEXT I
290 RETURN
REM DECODE ENCODE$ RETURN PLAIN$
300 IF NOT DI THEN DIM DICT$(4095)
310 FOR I = 0 TO 255:DICT$(I) = CHR$ (I): NEXT
320 LET DI = 1 : L = I
330 FOR I = L TO 4095:DICT$(I) = "": NEXT
340 LET C = ASC (ENCODE$) + 256 * ASC ( MID$ (ENCODE$,2))
350 LET W$ = DICT$(C)
360 LET PLAIN$ = W$
370 LET S = LEN (ENCODE$)
380 IF S < 4 THEN RETURN
400 FOR I = 3 TO S STEP 2
410 LET C = ASC ( MID$ (ENCODE$,I)) + 256 * ASC ( MID$ (ENCODE$,I + 1))
420 IF C < L THEN T$ = DICT$(C)
430 IF C > = L THEN T$ = W$ + LEFT$ (W$,1)
440 LET PLAIN$ = PLAIN$ + T$
450 LET DICT$(L) = W$ + LEFT$ (T$,1)
460 LET L = L + 1
470 LET W$ = T$
480 NEXT
490 RETURN</syntaxhighlight>
{{out}}
<pre>84 79 66 69 79 82 78 79 84 256 258 260 265 259 261 263
TOBEORNOTTOBEORTOBEORNOT</pre>
=={{header|AWK}}==
copy the following to standard input
<syntaxhighlight>
==comp
TOBEORNOTTOBEORTOBEORNOT
To be or not to be that is the question!
"There is nothing permanent except change." --- Heraclitus [540 -- 475 BCE]
==decomp
84,79,66,69,79,82,78,79,84,256,258,260,265,259,261,263
84,111,32,98,101,32,111,114,32,110,111,116,32,116,257,259,268,104,97,267,105,115,272,260,113,117,101,115,116,105,111,110,33
34,84,104,101,114,101,32,105,115,32,110,111,116,104,105,110,103,32,112,259,109,97,110,101,110,116,32,101,120,99,101,112,281,99,104,277,103,101,46,34,32,296,45,298,296,32,72,259,97,99,108,105,116,117,264,32,91,53,52,48,32,299,52,55,53,32,66,67,69,93
</syntaxhighlight>
<syntaxhighlight lang="AWK">
# ported from Python
BEGIN {
is_comp = 0
is_decomp = 0
}
{
if ($0 == "") next
if ($1 == "==comp") {
is_comp = 1
is_decomp = 0
print "\ncompressing..."
next
}
if ($1 == "==decomp") {
is_comp = 0
is_decomp = 1
print "\ndecompressing..."
next
}
if (is_comp) print compress($0)
if (is_decomp) print decompress($0)
}
function compress(str, dict_size, i, dictionary, w, result, len, uncompressed, c, wc ) {
dict_size = 256
for (i = 0; i <= dict_size; i++) dictionary[chr(i)] = i
w = ""
result = ""
len = split(str, uncompressed, "")
for (i = 1; i <= len; i++) {
c = uncompressed[i]
wc = w c
if (wc in dictionary) w = wc
else {
result = result "," dictionary[w]
dictionary[wc] = dict_size++
w = c
}
}
if (length(w)) result = result "," dictionary[w]
return substr(result,2)
return "[" substr(result,2) "]"
}
function decompress(str) {
dict_size = 256
for (i = 0; i <= dict_size; i++) dictionary[i] = chr(i)
result = ""
len = split(str, compressed, ",")
w = chr(compressed[1])
result = result w
for (i = 2; i <= len; i++) {
k = compressed[i]
if (k in dictionary) entry = dictionary[k]
else if (k == dict_size) entry = w substr(w,1,1)
else { entry = "*" }
result = result entry
dictionary[dict_size++] = w substr(entry,1,1)
w = entry
}
return result
}
function chr(c) {
return sprintf("%c", c + 0)
}</syntaxhighlight>
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">compress: function [str][
dict: #[]
loop 0..255 'i -> dict\[to :char i]: i
w: ""
result: new []
loop str 'c [
wc: w ++ c
if? key? dict wc -> w: wc
else [
'result ++ dict\[w]
dict\[wc]: size dict
w: to :string c
]
]
if 0 < size w -> 'result ++ dict\[w]
return result
]
decompress: function [compressed][
dict: #[]
arr: new compressed
loop 0..255 'i -> dict\[i]: to :string to :char i
w: dict\[first arr]
remove 'arr .index 0
result: w
loop arr 'k [
entry: ""
if? key? dict k -> entry: dict\[k]
else [
if? k = size dict -> entry: w ++ first w
else -> panic ~"Error with compressed: |k|"
]
'result ++ entry
dict\[size dict]: w ++ first entry
w: entry
]
return result
]
compressed: compress "TOBEORNOTTOBEORTOBEORNOT"
print "Compressed:"
print compressed
print ""
decompressed: decompress compressed
print "Decompressed:"
print decompressed</syntaxhighlight>
{{out}}
<pre>Compressed:
84 79 66 69 79 82 78 79 84 256 258 260 265 259 261 263
Decompressed:
TOBEORNOTOBEORNOTOOBEORTOOBEORNOT</pre>
=={{header|BaCon}}==
<
PRINT "LZWData: ", lzw_data$
Line 286 ⟶ 498:
RETURN result$
END FUNCTION</
{{out}}
<pre>LZWData: TOBEORNOTTOBEORTOBEORNOT
Line 296 ⟶ 508:
{{works with|BBC BASIC for Windows}}
Uses fixed bit-width (16 bits) and initial dictionary size = 256.
<
encodeLZW$ = FNencodeLZW(plaintext$)
FOR i% = 1 TO LEN(encodeLZW$) STEP 2
Line 343 ⟶ 555:
w$ = t$
NEXT
= o$</
{{out}}
<pre>
Line 358 ⟶ 570:
'''WARNING: This code appears to have come from a GIF codec that has been modified to meet the requirements of this page, provided that the decoder works with the encoder to produce correct output. For writing GIF files the write_bits subroutine is wrong for Little Endian systems (it may be wrong for Big Endian as well.) The encoder also increases the number of bits in the variable length GIF-LZW after the N-2 code, whereas this must be done after N-1 to produce a working GIF file (just looking at the encoder, it's easy to see how this mistake could be made.)'''
<
#include <stdlib.h>
#include <string.h>
Line 615 ⟶ 827:
return 0;
}</
=={{header|C sharp}}==
{{trans|Java}}
<syntaxhighlight lang="c sharp">using System;
using System.Collections.Generic;
using System.Text;
namespace LZW
{
public class Program
{
public static void Main(string[] args)
{
List<int> compressed = Compress("TOBEORNOTTOBEORTOBEORNOT");
Console.WriteLine(string.Join(", ", compressed));
string decompressed = Decompress(compressed);
Console.WriteLine(decompressed);
}
public static List<int> Compress(string uncompressed)
{
// build the dictionary
Dictionary<string, int> dictionary = new Dictionary<string, int>();
for (int i = 0; i < 256; i++)
dictionary.Add(((char)i).ToString(), i);
string w = string.Empty;
List<int> compressed = new List<int>();
foreach (char c in uncompressed)
{
string wc = w + c;
if (dictionary.ContainsKey(wc))
{
w = wc;
}
else
{
// write w to output
compressed.Add(dictionary[w]);
// wc is a new sequence; add it to the dictionary
dictionary.Add(wc, dictionary.Count);
w = c.ToString();
}
}
// write remaining output if necessary
if (!string.IsNullOrEmpty(w))
compressed.Add(dictionary[w]);
return compressed;
}
public static string Decompress(List<int> compressed)
{
// build the dictionary
Dictionary<int, string> dictionary = new Dictionary<int, string>();
for (int i = 0; i < 256; i++)
dictionary.Add(i, ((char)i).ToString());
string w = dictionary[compressed[0]];
compressed.RemoveAt(0);
StringBuilder decompressed = new StringBuilder(w);
foreach (int k in compressed)
{
string entry = null;
if (dictionary.ContainsKey(k))
entry = dictionary[k];
else if (k == dictionary.Count)
entry = w + w[0];
decompressed.Append(entry);
// new sequence; add it to the dictionary
dictionary.Add(dictionary.Count, w + entry[0]);
w = entry;
}
return decompressed.ToString();
}
}
}</syntaxhighlight>
{{out}}
<pre>84, 79, 66, 69, 79, 82, 78, 79, 84, 256, 258, 260, 265, 259, 261, 263
TOBEORNOTTOBEORTOBEORNOT</pre>
=={{header|C++}}==
{{trans|D}}
<syntaxhighlight lang="cpp">#include <string>
#include <map>
// Compress a string to a list of output symbols.
// The result will be written to the output iterator
// starting at "result"; the final iterator is returned.
template <typename Iterator>
Iterator compress(const std::string &uncompressed, Iterator result) {
// Build the dictionary.
int dictSize = 256;
std::map<std::string,int> dictionary;
for (int i = 0; i < 256; i++)
dictionary[std::string(1, i)] = i;
std::string w;
for (std::string::const_iterator it = uncompressed.begin();
it != uncompressed.end(); ++it) {
char c = *it;
std::string wc = w + c;
if (dictionary.count(wc))
w = wc;
else {
*result++ = dictionary[w];
// Add wc to the dictionary.
dictionary[wc] = dictSize++;
w = std::string(1, c);
}
}
// Output the code for w.
if (!w.empty())
*result++ = dictionary[w];
return result;
}
// Decompress a list of output ks to a string.
// "begin" and "end" must form a valid range of ints
template <typename Iterator>
std::string decompress(Iterator begin, Iterator end) {
// Build the dictionary.
int dictSize = 256;
std::map<int,std::string> dictionary;
for (int i = 0; i < 256; i++)
dictionary[i] = std::string(1, i);
std::string w(1, *begin++);
std::string result = w;
std::string entry;
for ( ; begin != end; begin++) {
int k = *begin;
if (dictionary.count(k))
entry = dictionary[k];
else if (k == dictSize)
entry = w + w[0];
else
throw "Bad compressed k";
result += entry;
// Add w+entry[0] to the dictionary.
dictionary[dictSize++] = w + entry[0];
w = entry;
}
return result;
}
#include <iostream>
#include <iterator>
#include <vector>
int main() {
std::vector<int> compressed;
compress("TOBEORNOTTOBEORTOBEORNOT", std::back_inserter(compressed));
copy(compressed.begin(), compressed.end(), std::ostream_iterator<int>(std::cout, ", "));
std::cout << std::endl;
std::string decompressed = decompress(compressed.begin(), compressed.end());
std::cout << decompressed << std::endl;
return 0;
}</syntaxhighlight>
=={{header|Clojure}}==
<syntaxhighlight lang="lisp">(defn make-dict []
(let [vals (range 0 256)]
(zipmap (map (comp #'list #'char) vals) vals)))
(defn compress [#^String text]
(loop [t (seq text)
r '()
w '()
dict (make-dict)
s 256]
(let [c (first t)]
(if c
(let [wc (cons c w)]
(if (get dict wc)
(recur (rest t) r wc dict s)
(recur (rest t) (cons (get dict w) r) (list c) (assoc dict wc s) (inc s))))
(reverse (if w (cons (get dict w) r) r))))))
(compress "TOBEORNOTTOBEORTOBEORNOT")</syntaxhighlight>
{{out}}
<syntaxhighlight lang="lisp">(84 79 66 69 79 82 78 79 84 256 258 260 265 259 261 263)</syntaxhighlight>
=={{header|CoffeeScript}}==
This only does the encoding step for now.
<
lzw = (s) ->
dct = {} # map substrings to codes between 256 and 4096
Line 657 ⟶ 1,064:
console.log lzw "TOBEORNOTTOBEORTOBEORNOT"
</syntaxhighlight>
{{out}}
<pre>
Line 689 ⟶ 1,096:
The exact encoding used is dependent upon the user's locale (<code>LC_CTYPE</code> on Unix).
<
vector-append))
(defun vector-append (old new &optional (start2 0) end2)
Line 798 ⟶ 1,205:
(assert (equal #2=(lzw-decompress-to-string (lzw-compress string)) string) ()
"Can't compress ~S properly, got ~S instead" string #2#)
t)</
And the format used:
<
T
CL-USER> (lzw-compress "TOBEORNOTTOBEORTOBEORNOT")
#(84 79 66 69 79 82 78 79 84 256 258 260 265 259 261 263)
CL-USER> (lzw-decompress-to-string *)
"TOBEORNOTTOBEORTOBEORNOT"</
=={{header|D}}==
===Simpler Version===
<
auto compress(in string original) pure nothrow {
Line 1,044 ⟶ 1,257:
auto comp = "TOBEORNOTTOBEORTOBEORNOT".compress;
writeln(comp, "\n", comp.decompress);
}</
{{out}}
<pre>[84, 79, 66, 69, 79, 82, 78, 79, 84, 256, 258, 260, 265, 259, 261, 263]
Line 1,051 ⟶ 1,264:
===More Refined Version===
This longer version is a little more efficient and it uses stronger static typing.
<
import std.array: empty;
Line 1,145 ⟶ 1,358:
compressed.writeln;
LZW.decompress(compressed).assumeUTF.writeln;
}</
{{out}}
<pre>[84, 79, 66, 69, 79, 82, 78, 79, 84, 256, 258, 260, 265, 259, 261, 263]
Line 1,153 ⟶ 1,366:
{{trans|C}}
This code retains part of the style of the original C code.
<
CLR = 256, // Clear table marker.
EOD = 257, // End-of-data marker.
Line 1,374 ⟶ 1,587:
"Decoded OK.".writeln;
}</
{{out}}
<pre>Input size: 206403
Line 1,382 ⟶ 1,595:
=={{header|Dylan}}==
<
Synopsis: LZW implementation for Rosetta code
Line 1,424 ⟶ 1,637:
end;
format-out("%=\n", compress("TOBEORNOTTOBEORTOBEORNOT"))</
=={{header|Eiffel}}==
<syntaxhighlight lang="eiffel">
class
APPLICATION
Line 1,539 ⟶ 1,753:
end
</syntaxhighlight>
{{out}}
<pre>
Line 1,548 ⟶ 1,762:
=={{header|Elixir}}==
{{trans|Erlang}}
<
@encode_map Enum.into(0..255, Map.new, &{[&1],&1})
@decode_map Enum.into(0..255, Map.new, &{&1,[&1]})
Line 1,586 ⟶ 1,800:
IO.inspect enc = LZW.encode(str)
IO.inspect dec = LZW.decode(enc)
IO.inspect str == dec</
{{out}}
Line 1,593 ⟶ 1,807:
"TOBEORNOTTOBEORTOBEORNOT"
true
</pre>
=={{header|EMal}}==
<syntaxhighlight lang="emal">
type LzwCompression
fun compress ← List by text uncompressed
List output ← int[]
text working ← Text.EMPTY
Map symbolTable ← text%int[].with(256, <int i|text%int(chr(i) => i))
for each text c in uncompressed
text augmented ← working + c
if symbolTable.has(augmented)
working ← augmented
else
symbolTable.insert(augmented, symbolTable.length)
int i ← symbolTable[working]
output.append(i)
working ← c
end
end
if not working.isEmpty()
int i ← symbolTable[working]
output.append(i)
end
return output
end
fun decompress ← text by List compressed
Map symbolTable ← int%text[].with(256, <int i|int%text(i => chr(i)))
text working ← symbolTable[compressed[0]]
text output ← *working
for each int i in compressed.extract(1)
text s
if symbolTable.has(i)
s ← symbolTable[i]
else if i æ symbolTable.length # cScSc problem
s ← working + working[0]
else
error(65, "Error decompressing")
end
output.append(s)
symbolTable.insert(symbolTable.length, working + s[0])
working ← s
end
return output
end
List compressed = compress("TOBEORNOTTOBEORTOBEORNOT")
writeLine(compressed)
text decompressed = decompress(compressed)
writeLine(decompressed)
</syntaxhighlight>
{{out}}
<pre>
[84,79,66,69,79,82,78,79,84,256,258,260,265,259,261,263]
TOBEORNOTTOBEORTOBEORNOT
</pre>
=={{header|Erlang}}==
<
-export([test/0, encode/1, decode/1]).
Line 1,652 ⟶ 1,920:
init1(0, D) -> D;
init1(N, D) -> D1 = dict:store(N,[N],D), init1(N-1, D1).</
=={{header|Forth}}==
{{works with|GNU Forth|0.6.2}}
<
\ current string fragment
Line 1,754 ⟶ 2,022:
out out-size @ decompress cr
\ TOBEORNOTTOBEORTOBEORNOT</
=={{header|Fortran}}==
<syntaxhighlight lang="fortran">
!
! lzw_shared_parameters.f90
!
! LZW Common Variables Used by Coder and Decoder
!
! Author: Pedro Garcia Freitas <sawp@sawp.com.br>
! May, 2011
!
! License: Creative Commons http://creativecommons.org/licenses/by-nc-nd/3.0/
!
MODULE LZW_SHARED_PARAMETERS
IMPLICIT NONE
!
! PARAMETER definitions
!
INTEGER , PARAMETER :: COMPILER_INTEGER_SIZE = 32 , BITS = 12 , FILEIN = 66 , &
& FILEOUT = 99 , MAX_VALUE = (2**BITS) - 1 , &
& MAX_CODE = MAX_VALUE - 1 , MAX_DICTIONARY_SIZE = 5021 , &
& SYMBOL_SIZE = 8 , MISSING_BITS = COMPILER_INTEGER_SIZE - &
& SYMBOL_SIZE
!
! Local variables
!
INTEGER , DIMENSION(0:MAX_DICTIONARY_SIZE) :: concatenatedsymbols
INTEGER , DIMENSION(0:MAX_DICTIONARY_SIZE) :: prefixcodes
INTEGER :: the_status = 0
! change this if compiler dont use 32 bits for integer
END MODULE LZW_SHARED_PARAMETERS
!
! codecIO.f90
!
! bit IO routines for coder and encoder.
!
! Author: Pedro Garcia Freitas <sawp@sawp.com.br>
! May, 2011
!
! License: Creative Commons http://creativecommons.org/licenses/by-nc-nd/3.0/
!
MODULE CODECIO
USE LZW_SHARED_PARAMETERS
IMPLICIT NONE
!
CONTAINS
SUBROUTINE SETOUTPUTCODE(Code)
IMPLICIT NONE
!
! Dummy arguments
!
INTEGER :: Code
INTENT (IN) Code
!
! Local variables
!
INTEGER :: buffer
INTEGER :: outputbitbuffer = 0
INTEGER :: outputbitcount = 0
INTEGER :: shift
INTEGER :: shiftedsymbol
!
shift = COMPILER_INTEGER_SIZE - BITS - outputbitcount
shiftedsymbol = ISHFT(Code , shift)
outputbitbuffer = IOR(outputbitbuffer , shiftedsymbol)
outputbitcount = outputbitcount + BITS
DO WHILE(outputbitcount >= SYMBOL_SIZE)
! IF( outputbitcount<SYMBOL_SIZE )EXIT
buffer = ISHFT(outputbitbuffer , -MISSING_BITS)
CALL SETRAWBYTE(buffer)
outputbitbuffer = ISHFT(outputbitbuffer , SYMBOL_SIZE)
outputbitcount = outputbitcount - SYMBOL_SIZE
END DO
RETURN
END SUBROUTINE SETOUTPUTCODE
SUBROUTINE SETRAWBYTE(Symbol)
IMPLICIT NONE
!
! Dummy arguments
!
INTEGER :: Symbol
INTENT (IN) Symbol
!
CALL FPUTC(FILEOUT , ACHAR(Symbol))
END SUBROUTINE SETRAWBYTE
FUNCTION GETRAWBYTE()
IMPLICIT NONE
!
! Dummy arguments
!
INTEGER :: GETRAWBYTE
!
! Local variables
!
CHARACTER :: bufferedbyte
!
CALL FGETC(FILEIN , bufferedbyte , THE_status)
GETRAWBYTE = IACHAR(bufferedbyte)
END FUNCTION GETRAWBYTE
FUNCTION GETINPUTCODE()
IMPLICIT NONE
!
! Dummy arguments
!
INTEGER :: GETINPUTCODE
!
! Local variables
!
INTEGER :: inputbitbuffer = 0
INTEGER :: inputbitcounter = 0
INTEGER :: integerinputbuff
INTEGER :: returnn
INTEGER :: shiftedbit
!
DO WHILE( inputbitcounter <= MISSING_BITS )
! IF( inputbitcounter>MISSING_BITS )EXIT
integerinputbuff = GETRAWBYTE()
shiftedbit = ISHFT(integerinputbuff , MISSING_BITS - inputbitcounter)
inputbitbuffer = IOR(inputbitbuffer , shiftedbit)
inputbitcounter = inputbitcounter + SYMBOL_SIZE
END DO
returnn = ISHFT(inputbitbuffer , BITS - COMPILER_INTEGER_SIZE)
inputbitbuffer = ISHFT(inputbitbuffer , BITS)
inputbitcounter = inputbitcounter - BITS
GETINPUTCODE = returnn
RETURN
END FUNCTION GETINPUTCODE
end module codecIO
! lzw_encoder.f90
!
! LZW Coder (Compressor)
!
! Author: Pedro Garcia Freitas <sawp@sawp.com.br>
! May, 2011
!
! License: Creative Commons http://creativecommons.org/licenses/by-nc-nd/3.0/
!
MODULE LZW_ENCODER
USE LZW_SHARED_PARAMETERS
USE CODECIO
IMPLICIT NONE
!
! PARAMETER definitions
!
INTEGER , PARAMETER :: HASH_SHIFT = BITS - SYMBOL_SIZE
!
! Local variables
!
INTEGER , DIMENSION(0:MAX_DICTIONARY_SIZE) :: symbolvalues
CONTAINS
SUBROUTINE COMPRESS()
IMPLICIT NONE
!
! Local variables
!
INTEGER :: codedsymbol
INTEGER :: my_index
INTEGER :: nextsymbol
INTEGER :: symbol
CHARACTER :: bufferedbyte
!
nextsymbol = COMPILER_INTEGER_SIZE*SYMBOL_SIZE
SYMbolvalues(:) = -1
! codedsymbol = GETRAWBYTE()
CALL FGETC(FILEIN , bufferedbyte , THE_status)
codedsymbol = IACHAR(bufferedbyte)
!Can be hand optimized - optimization
DO WHILE(THE_status == 0)
! symbol = GETRAWBYTE() ! Manual inline of function
CALL FGETC(FILEIN , bufferedbyte , THE_status)
symbol = IACHAR(bufferedbyte)
IF( THE_status/=0 )CYCLE
my_index = GETPOSITIONONDICTIONARY(codedsymbol , symbol)
IF( SYMbolvalues(my_index)/= - 1 )THEN
codedsymbol = SYMbolvalues(my_index)
ELSE
IF( nextsymbol<=MAX_CODE )THEN
SYMbolvalues(my_index) = nextsymbol
nextsymbol = nextsymbol + 1
PREfixcodes(my_index) = codedsymbol
CONcatenatedsymbols(my_index) = symbol
END IF
CALL SETOUTPUTCODE(codedsymbol)
codedsymbol = symbol
END IF
END DO
CALL SETOUTPUTCODE(codedsymbol)
CALL SETOUTPUTCODE(MAX_VALUE)
CALL SETOUTPUTCODE(0)
END SUBROUTINE COMPRESS
function getPositionOnDictionary(hashPrefix, hashSymbol)
integer, intent(in) :: hashPrefix
integer, intent(in) :: hashSymbol
integer :: getPositionOnDictionary
integer :: index
integer :: offset
index = ishft(hashSymbol, HASH_SHIFT)
index = ieor(index, hashPrefix)
if (index == 0) then
offset = 1
else
offset = MAX_DICTIONARY_SIZE - index
endif
do
if (symbolValues(index) == -1) then
getPositionOnDictionary = index
exit
endif
if (prefixCodes(index) == hashPrefix .and. &
& concatenatedSymbols(index) == hashSymbol) then
getPositionOnDictionary = index
exit
endif
index = index - offset
if (index < 0) then
index = index + MAX_DICTIONARY_SIZE
endif
end do
return
end function
end module LZW_Encoder
! lzw_decoder.f90
!
! LZW Decoder (Expanssor)
!
! Author: Pedro Garcia Freitas <sawp@sawp.com.br>
! May, 2011
!
! License: Creative Commons http://creativecommons.org/licenses/by-nc-nd/3.0/
!
MODULE LZW_DECODER
USE LZW_SHARED_PARAMETERS
USE CODECIO
IMPLICIT NONE
!
! Derived Type definitions
!
TYPE :: DECODE_BUFFER_STACK
INTEGER , DIMENSION(0:MAX_DICTIONARY_SIZE) :: DECODERSTACK
INTEGER :: TOP
END TYPE DECODE_BUFFER_STACK
!
! Local variables
!
TYPE(DECODE_BUFFER_STACK) :: stack
CONTAINS
!
! Can be hand optimized - optimization
SUBROUTINE DECOMPRESS()
IMPLICIT NONE
!
! Local variables
!
INTEGER :: newsymbol
INTEGER :: nextsymbol
INTEGER :: oldsymbol
INTEGER :: popedsymbol
INTEGER :: symbol
!
nextsymbol = COMPILER_INTEGER_SIZE*SYMBOL_SIZE
oldsymbol = GETINPUTCODE()
symbol = oldsymbol
CALL SETRAWBYTE(oldsymbol)
DO
newsymbol = GETINPUTCODE()
IF( newsymbol==MAX_VALUE )RETURN
IF( newsymbol>=nextsymbol )THEN
STAck%DECODERSTACK(0) = symbol
CALL DECODESYMBOL(STAck%DECODERSTACK(1:) , oldsymbol)
ELSE
CALL DECODESYMBOL(STAck%DECODERSTACK(:) , newsymbol)
END IF
symbol = STAck%DECODERSTACK(STAck%TOP)
DO WHILE ( STAck%TOP>=0 )
popedsymbol = STAck%DECODERSTACK(STAck%TOP)
CALL SETRAWBYTE(popedsymbol)
STAck%TOP = STAck%TOP - 1
END DO
IF( nextsymbol<=MAX_CODE )THEN
PREfixcodes(nextsymbol) = oldsymbol
CONcatenatedsymbols(nextsymbol) = symbol
nextsymbol = nextsymbol + 1
END IF
oldsymbol = newsymbol
END DO
RETURN
END SUBROUTINE DECOMPRESS
SUBROUTINE DECODESYMBOL(Buffer , Code)
IMPLICIT NONE
!
! Dummy arguments
!
INTEGER :: Code
INTEGER , DIMENSION(:) :: Buffer
INTENT (IN) Code
INTENT (INOUT) Buffer
!
! Local variables
!
INTEGER :: i
INTEGER :: j
INTEGER :: symbol
!
j = 0
symbol = Code
STAck%TOP = 0
DO WHILE ( symbol>=COMPILER_INTEGER_SIZE*SYMBOL_SIZE )
! IF( symbol<COMPILER_INTEGER_SIZE*SYMBOL_SIZE )EXIT
IF( j>=MAX_CODE )THEN
PRINT * , "Decoding error"
STOP
END IF
i = STAck%TOP + 1
Buffer(i) = CONcatenatedsymbols(symbol)
symbol = PREfixcodes(symbol)
STAck%TOP = STAck%TOP + 1
j = j + 1
END DO
i = j + 1
Buffer(i) = symbol
END SUBROUTINE DECODESYMBOL
end module LZW_Decoder
! lzw.f90
!
! LZW Coder and Decoder
!
! Author: Pedro Garcia Freitas <sawp@sawp.com.br>
! May, 2011
!
! License: Creative Commons http://creativecommons.org/licenses/by-nc-nd/3.0/
!
MODULE LZW
USE LZW_SHARED_PARAMETERS
USE LZW_ENCODER
USE LZW_DECODER
IMPLICIT NONE
CONTAINS
SUBROUTINE INIT(Input , Output , Operation , Filename)
IMPLICIT NONE
!
! Dummy arguments
!
CHARACTER(100) :: Filename
CHARACTER(100) :: Input
CHARACTER(1) :: Operation
CHARACTER(100) :: Output
INTENT (IN) Filename , Input , Operation , Output
!
IF( Operation/='d' .AND. Operation/='e' )THEN
PRINT * , "Usage: " // TRIM(Filename) // " <operation> input output"
PRINT * , "Possible operations: "
PRINT * , " e -> encode (compress)"
PRINT * , " d -> decode (inflate)"
STOP
END IF
OPEN(UNIT = FILEIN , FILE = Input , ACTION = "read" , STATUS = "old" , &
&ACCESS = 'stream' , FORM = "formatted")
OPEN(UNIT = FILEOUT , FILE = Output , ACTION = "write" , STATUS = "replace" , &
& ACCESS = 'stream' , FORM = "formatted")
IF( Operation=='d' )THEN
PRINT * , "Decoding..."
CALL DECOMPRESS()
ELSE
PRINT * , "Encoding..."
CALL COMPRESS()
END IF
CLOSE(UNIT = FILEIN)
CLOSE(UNIT = FILEOUT)
END SUBROUTINE INIT
end module LZW
!
PROGRAM MAIN
USE LZW
IMPLICIT NONE
!
! Local variables
!
CHARACTER(100) :: filename
REAL :: finish
CHARACTER(100) :: input
CHARACTER(1) :: operation
CHARACTER(100) :: output
REAL :: start
!
CALL GETARG(0 , filename)
CALL GETARG(1 , operation)
CALL GETARG(2 , input)
CALL GETARG(3 , output)
CALL CPU_TIME(start)
CALL INIT(input , output , operation , filename)
CALL CPU_TIME(finish)
PRINT '("Time = ",f6.3," seconds.")' , finish - start
END PROGRAM MAIN</syntaxhighlight>
=={{header|FreeBASIC}}==
<
' compile with: fbc -s console
Line 1,865 ⟶ 2,558:
Print : Print "hit any key to end program"
Sleep
End</
{{out}}
<pre> input str: TOBEORNOTTOBEORTOBEORNOT
Line 1,880 ⟶ 2,573:
not just ASCII or valid UTF8 encoding
(tested with [https://github.com/dvyukov/go-fuzz go-fuzz]).
<
import (
Line 1,974 ⟶ 2,667:
}
fmt.Println(decompressed)
}</
{{out}}
<pre>
Line 1,982 ⟶ 2,675:
=={{header|Groovy}}==
<
def dictionary = (0..<256).inject([:]) { map, ch -> map."${(char)ch}" = ch; map }
def w = '', compressed = []
Line 2,018 ⟶ 2,711:
result.toString()
}</
Testing:
<
def compressed = compress(plaintext)
def result = decompress(compressed)
Line 2,027 ⟶ 2,720:
Plaintext: '$plaintext'
Compressed: $compressed
Uncompressed: '$result'""".stripIndent()</
{{out}}
<pre>Plaintext: 'TOBEORNOTTOBEORTOBEORNOT'
Line 2,035 ⟶ 2,728:
=={{header|Haskell}}==
<
import Data.Maybe (fromJust)
doLZW ::
doLZW _ [] = []
doLZW as (x:xs) = lzw (return <$> as) [x] xs
Line 2,049 ⟶ 2,742:
w_ = w ++ [x]
undoLZW ::
undoLZW _ [] = []
undoLZW a cs =
Line 2,072 ⟶ 2,765:
print $
((==) <*> ((.) <$> undoLZW <*> doLZW) ['\NUL' .. '\255'])
"TOBEORNOTTOBEORTOBEORNOT"</
{{Out}}
<pre>[84,79,66,69,79,82,78,79,84,256,258,260,265,259,261,263]
Line 2,083 ⟶ 2,776:
Straightforward implementations of encoding and decoding:
<
d=. ;/x
r=.0$0
Line 2,096 ⟶ 2,789:
end.
r, d i.<w
)</
Test:
<pre> a. encodeLZW 'TOBEORNOTTOBEORTOBEORNOT'
84 79 66 69 79 82 78 79 84 256 258 260 265 259 261 263</pre>
Decoding:
<
d=.;/x
w=.r=. >d{~{.y
Line 2,116 ⟶ 2,809:
end.
;r
)</
Test:
<pre> a. decodeLZW 84 79 66 69 79 82 78 79 84 256 258 260 265 259 261 263
Line 2,134 ⟶ 2,827:
=={{header|Java}}==
{{works with|Java|1.5+}}
<
public class LZW {
Line 2,200 ⟶ 2,893:
System.out.println(decompressed);
}
}</
{{out}} (Command Line direct output):
<
TOBEORNOTTOBEORTOBEORNOT</
=={{header|JavaScript}}==
<
var LZW = {
compress: function (uncompressed) {
Line 2,287 ⟶ 2,980:
comp = LZW.compress("TOBEORNOTTOBEORTOBEORNOT"),
decomp = LZW.decompress(comp);
document.write(comp + '<br>' + decomp);</
Line 2,294 ⟶ 2,987:
This is the the same thing, but for ES6. The code has been refactored and cleaned up a bit to look neater.
<
/**
Namespace for LZW compression and decompression.
Line 2,403 ⟶ 3,096:
console.log(`${comp}
${decomp}`);</
{{out}}
Line 2,412 ⟶ 3,105:
{{ works with|jq|1.4}}
{{trans|JavaScript}}
<
def lzw_compress:
def decode: [.] | implode;
Line 2,457 ⟶ 3,150:
| .[2] = $entry # w = entry
) | .[3]
;</
'''Example''':
<
{{Out}}
$ jq -n -f LZW.jq
Line 2,466 ⟶ 3,159:
=={{header|Julia}}==
{{works with|Julia|1.1.1}}
<
dictsize = 256
dict = Dict{String,Int}(string(Char(i)) => i for i in 0:dictsize)
Line 2,515 ⟶ 3,208:
comprate = (length(word) - length(comp)) / length(word) * 100
println("Original: $word\n-> Compressed: $comp (compr.rate: $(round(comprate, digits=2))%)\n-> Decompressed: $decomp")
end</
{{out}}
Line 2,530 ⟶ 3,223:
=={{header|Kotlin}}==
{{trans|Java}}
<
object Lzw {
Line 2,591 ⟶ 3,284:
val decompressed = Lzw.decompress(compressed)
println(decompressed)
}</
{{out}}
Line 2,604 ⟶ 3,297:
It also has the option to write the encoding/decoding dictionaries to file so the encoder can be checked for accuracy.
This code directly follows the methodology described in an excellent web article by Juha Nieminen entitled "An efficient LZW implementation".
<syntaxhighlight lang="text"> DIM LZW(1, 1)
DIM JDlzw(1)
DIM JDch$(1)
Line 2,883 ⟶ 3,576:
fileTag$ = STR$(tagCount) + "_"
RETURN
''''''''''''''''''''''''''''''''''''''''</
=={{header|Lua}}==
<syntaxhighlight lang="lua">local function compress(uncompressed) -- string
local dictionary, result, dictSize, w, c = {}, {}, 255, ""
for i = 0, 255 do
Line 2,908 ⟶ 3,603:
local function decompress(compressed) -- table
local dictionary, dictSize, entry
local result = {w}
for i = 0, 255 do
dictionary[i] = string.char(i)
end
for i =
k = compressed[i]
if dictionary[k] then
Line 2,921 ⟶ 3,617:
return nil, i
end
table.insert(result
dictionary[dictSize] = w .. string.sub(entry, 1, 1)
dictSize = dictSize + 1
w = entry
end
return table.concat(result)
end
Line 2,933 ⟶ 3,629:
local dec = decompress(com)
print(table.concat(com, ", "))
print(dec)</
{{Out}}
Line 2,940 ⟶ 3,636:
TOBEORNOTTOBEORTOBEORNOT
</pre>
=={{header|M2000 Interpreter}}==
{{trans|BBC BASIC}}
<syntaxhighlight lang="m2000 interpreter">
Module BBCtrans {
\\ LZW compression
Line 2,997 ⟶ 3,692:
}
BBCtrans
</syntaxhighlight>
And here a change for using Inventories, where we have hash function, and we find entry in O(1).
<syntaxhighlight lang="m2000 interpreter">
Module FastM2000 {
plaintext$="TOBEORNOTTOBEORTOBEORNOT"
Line 3,058 ⟶ 3,753:
}
FastM2000
</syntaxhighlight>
{{out}}
<pre >
Line 3,065 ⟶ 3,760:
</pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
{{trans|Ruby}}
<syntaxhighlight lang="text">compress[uncompressed_] :=
Module[{dictsize, dictionary, w, result, wc},
dictsize = 256;
Line 3,103 ⟶ 3,798:
(*How to use:*)
compress["TOBEORNOTTOBEORTOBEORNOT"]
decompress[%]</
{{Out}}
<pre>{"T", "O", "B", "E", "O", "R", "N", "O", "T", 256, 258, 260, 265, 259, 261, 263}
"TOBEORNOTTOBEORTOBEORNOT"</pre>
(* stm, 6 June 2022: I did not edit the above code, but I believe that there is a small error. The Range@dictsize should be replaced by Range[0,dictsize-1] for LZW standard; Mathematica runs from 1 to 256 instead of 0 to 255. The code works as is in String format in Mathematica because Mathematica distinguishes between Strings and numbers. However, this example fails when the code is adapted to byte type input and output, as used in images. Adjusting Range[...] fixes the problem.*)
=={{header|Nim}}==
<syntaxhighlight lang="text">import tables
proc compress*(uncompressed: string): seq[int] =
# Build the dictionary.
var
for i in 0..255: dictionary[$chr(i)] = i
var w = ""
for c in uncompressed:
var wc = w & c
if
w = wc
else:
#
# "wc" is a new sequence
dictionary
w = $c
#
if w.len > 0: result.add dictionary[w]
proc decompress*(compressed: var seq[int]): string =
var dictionary: Table[int, string]
for i in 0..255: dictionary[i] = $chr(i)
var w = dictionary[compressed[0]]
compressed.delete(0)
for k in compressed:
var entry: string
if k in dictionary:
entry = dictionary[k]
elif
entry = w & w[0]
else:
raise newException(ValueError, "Bad compressed k: " & $k)
result.add entry
# New sequence: add it to the dictionary.
dictionary[dictionary.len] = w & entry[0]
w = entry
when isMainModule:
Line 3,173 ⟶ 3,858:
echo compressed
var decompressed = decompress(compressed)
echo decompressed</syntaxhighlight>
{{
<pre>@[84, 79, 66, 69, 79, 82, 78, 79, 84, 256, 258, 260, 265, 259, 261, 263]
TOBEORNOTTOBEORTOBEORNOT</pre>
=={{header|Objeck}}==
{{trans|Java}}
<
class LZW {
Line 3,286 ⟶ 3,969:
"]"->PrintLine();
}
}</
<pre>[84, 79, 66, 69, 79, 82, 78, 79, 84, 256, 258, 260, 265, 259, 261, 263]
Line 3,297 ⟶ 3,980:
The class for the LZW compression algorithm:
<
#import <stdio.h>
Line 3,382 ⟶ 4,065:
}
@end</
Usage example:
<
int main()
Line 3,406 ⟶ 4,089:
}
return EXIT_SUCCESS;
}</
{{out}} (reformatted by hand):
Line 3,416 ⟶ 4,099:
=={{header|OCaml}}==
<
#load "extLib.cma"
open ExtString
Line 3,495 ⟶ 4,178:
in
(List.rev result)
;;</
here is the interface:
<
val decompress : compressed:int list -> string list</
How to use:<br />
Line 3,505 ⟶ 4,188:
So to know how many bits are required, you need to know how many bits are required for the greatest symbol in the list.
<
(** number of bits needed to encode the integer m *)
Line 3,543 ⟶ 4,226:
List.iter (Buffer.add_string buf) result;
(Buffer.contents buf)
;;</
=={{header|Ol}}==
This version use lazy streams which is pair (symbol . function-to-get-next-symbol).
<
(define (compress str)
(let loop ((dc (fold (lambda (f x) ; dictionary (simplest, not optimized), with reversed codes
Line 3,573 ⟶ 4,256:
(print (compress "TOBEORNOTTOBEORTOBEORNOT")) ; => (84 79 66 69 79 82 78 79 84 256 258 260 265 259 261 263)
</syntaxhighlight>
And decoder (runes->string used to unify functions - both used string iterators):
<
(define (decompress str)
(let loop ((dc (fold (lambda (f x) ; dictionary (simplest, not optimized), with reversed codes
Line 3,604 ⟶ 4,287:
(decompress (runes->string '(84 79 66 69 79 82 78 79 84 256 258 260 265 259 261 263)))))
; => TOBEORNOTTOBEORTOBEEORNOT
</syntaxhighlight>
=={{header|Perl}}==
In this version the hashes contain mixed typed data:
<
sub compress {
my $uncompressed = shift;
Line 3,673 ⟶ 4,356:
print "@compressed\n";
my $decompressed = decompress(@compressed);
print "$decompressed\n";</
{{out}}
<pre>
Line 3,734 ⟶ 4,366:
=={{header|Phix}}==
{{trans|Lua}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">compress</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">uncompressed</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">dict</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">new_dict</span><span style="color: #0000FF;">()</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">result</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">dictSize</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">255</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">c</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">word</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">255</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">setd</span><span style="color: #0000FF;">(</span><span style="color: #008000;">""</span><span style="color: #0000FF;">&</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dict</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">uncompressed</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">uncompressed</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">getd_index</span><span style="color: #0000FF;">(</span><span style="color: #000000;">word</span><span style="color: #0000FF;">&</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dict</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">word</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">c</span>
<span
<span style="color: #000000;">result</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">getd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">word</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dict</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">dictSize</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #7060A8;">setd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">word</span><span style="color: #0000FF;">&</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dictSize</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dict</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">word</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span><span style="color: #0000FF;">&</span><span style="color: #000000;">c</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">word</span><span style="color: #0000FF;">!=</span><span style="color: #008000;">""</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">result</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">getd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">word</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dict</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #7060A8;">destroy_dict</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dict</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">result</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">decompress</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">compressed</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">dict</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">new_dict</span><span style="color: #0000FF;">()</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">dictSize</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">255</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ki</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">dent</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">result</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">word</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">255</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">setd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">&</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dict</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">compressed</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">compressed</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">ki</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">getd_index</span><span style="color: #0000FF;">(</span><span style="color: #000000;">k</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dict</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">ki</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">dent</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">getd_by_index</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ki</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dict</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #000000;">dictSize</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">dent</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">word</span><span style="color: #0000FF;">&</span><span style="color: #000000;">word</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">else</span>
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #004600;">NULL</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">result</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">dent</span>
<span style="color: #7060A8;">setd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dictSize</span><span style="color: #0000FF;">,</span><span style="color: #000000;">word</span><span style="color: #0000FF;">&</span><span style="color: #000000;">dent</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span><span style="color: #000000;">dict</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">dictSize</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #000000;">word</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">dent</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">destroy_dict</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dict</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">result</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">example</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"TOBEORNOTTOBEORTOBEORNOT"</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">com</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">compress</span><span style="color: #0000FF;">(</span><span style="color: #000000;">example</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">com</span><span style="color: #0000FF;">,{</span><span style="color: #004600;">pp_IntCh</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #004600;">pp_Maxlen</span><span style="color: #0000FF;">,</span><span style="color: #000000;">90</span><span style="color: #0000FF;">})</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">decompress</span><span style="color: #0000FF;">(</span><span style="color: #000000;">com</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 3,800 ⟶ 4,433:
=={{header|PHP}}==
{{trans|JavaScript}}
<
{
function compress($unc) {
Line 3,841 ⟶ 4,474:
for ($i = 1; $i < count($com);$i++) {
$k = $com[$i];
if (isset($dictionary[$k])) {
$entry = $dictionary[$k];
} else {
Line 3,864 ⟶ 4,497:
$dec = $lzw->decompress($com);
echo $com . "<br>" . $dec;
</syntaxhighlight>
{{out}}
<pre>
Line 3,870 ⟶ 4,503:
TOBEORNOTTOBEORTOBEORNOT
</pre>
=={{header|Picat}}==
<syntaxhighlight lang="picat">go =>
S = "TOBEORNOTTOBEORTOBEORNOT",
println(s=S),
println(len=S.length),
Compressed = compress(S),
println(compressed=Compressed),
println(len=Compressed.length),
Uncompressed = uncompress(Compressed),
println(uncompressed=Uncompressed),
printf("compressed to %3.3f%%\n", 100*(Compressed.length / S.length)),
if S = Uncompressed then
println("Same!")
else
println("Error: S != Uncompressed!"),
printf("S.length: %d Uncompressed.length: %d\n", S.length, Uncompressed.length),
edit(S,Uncompressed,Distance,Diffs),
println(distance=Distance),
println(diffs=Diffs)
end,
nl.
compress(Uncompressed) = Compressed =>
DictSize = 256,
Dict = new_map([C=C : I in 1..DictSize-1, C=chr(I).to_string()]),
W = "",
Result = [],
foreach(C in Uncompressed)
C := C.to_string(),
WC = W ++ C,
if Dict.has_key(WC) then
W := WC
else
Result := Result ++ [Dict.get(W)],
Dict.put(WC, DictSize),
DictSize := DictSize + 1,
W := C
end
end,
if W.length > 0 then
Result := Result ++ [Dict.get(W)]
end,
Compressed = Result.
uncompress(Compressed) = Uncompressed =>
DictSize = 256,
Dict = new_map([ C=C : I in 1..DictSize-1, C=chr(I).to_string()]),
W = Compressed.first(),
Compressed := Compressed.tail(),
Result = W,
Entry = "",
foreach(K in Compressed)
if Dict.has_key(K) then
Entry := Dict.get(K)
elseif K == DictSize then
Entry := W ++ W[1].to_string()
else
printf("Bad compressed K: %w\n", K)
end,
Result := Result ++ Entry,
Dict.put(DictSize,(W ++ Entry[1].to_string())),
DictSize := DictSize + 1,
W := Entry
end,
Uncompressed = Result.flatten().
%
% Computing the minimal editing distance of two given lists
%
table(+,+,min,-)
edit([],[],D,Diffs) => D=0, Diffs=[].
edit([X|Xs],[X|Ys],D,Diffs) => % copy
edit(Xs,Ys,D,Diffs).
edit(Xs,[Y|Ys],D,Diffs) ?=> % insert
edit(Xs,Ys,D1,Diffs1),
D=D1+1,
Diffs = [insert=Y,xPos=Xs.length,yPos=Ys.length|Diffs1].
edit([X|Xs],Ys,D,Diffs) => % delete
edit(Xs,Ys,D1,Diffs1),
D=D1+1,
Diffs = [[delete=X,xPos=Xs.length,yPos=Ys.length]|Diffs1].</syntaxhighlight>
{{out}}
<pre>s = TOBEORNOTTOBEORTOBEORNOT
len = 24
compressed = [T,O,B,E,O,R,N,O,T,256,258,260,265,259,261,263]
len = 16
uncompressed = TOBEORNOTTOBEORTOBEORNOT
compressed to 66.667%
Same!</pre>
=={{header|PicoLisp}}==
<
(let (Codes 255 Dict)
(balance 'Dict
Line 3,902 ⟶ 4,631:
(when W
(idx 'Dict (cons (inc 'Codes) (cons (last WC) W)) T) )
(setq W WC) ) ) ) ) ) )</
Test:
<pre>: (lzwCompress (chop "TOBEORNOTTOBEORTOBEORNOT"))
Line 3,909 ⟶ 4,638:
: (pack (lzwDecompress @))
-> "TOBEORNOTTOBEORTOBEORNOT"</pre>
=={{header|PL/I}}==
{{trans|REXX}}
The interesting point is the implementation of REXX's associative array (compound variable).
<
lzwt: Proc Options(main);
Line 4,063 ⟶ 4,791:
Return;
End;</
{{out}}
<pre>str=TOBEORNOTTOBEORTOBEORNOT
Line 4,075 ⟶ 4,803:
This is because PureBasic uses these to terminate strings.
Only slight modifications are necessary to handle Null values that would be present for a more generic routine that could be used with a buffer containing any data type.
<
;Compress a string to a list of output symbols
Line 4,171 ⟶ 4,899:
Input()
CloseConsole()
EndIf</
Sample output:
<pre>Type something: TOBEORNOTTOBEORTOBEORNOT
Line 4,178 ⟶ 4,906:
=={{header|Python}}==
{{works with|Python|3.x}}
In this version the dicts contain mixed typed data:
<
"""Compress a string to a list of output symbols."""
# Build the dictionary.
dict_size = 256
dictionary = dict((chr(i), i) for i in
# in Python 3: dictionary = {chr(i): i for i in range(dict_size)}
Line 4,209 ⟶ 4,937:
def decompress(compressed):
"""Decompress a list of output ks to a string."""
from
# Build the dictionary.
dict_size = 256
dictionary = dict((i, chr(i)) for i in
# in Python 3: dictionary = {i: chr(i) for i in range(dict_size)}
Line 4,242 ⟶ 4,970:
print (compressed)
decompressed = decompress(compressed)
print (decompressed)</
Output:
Line 4,251 ⟶ 4,979:
=={{header|Racket}}==
<
#lang racket
; utilities
Line 4,309 ⟶ 5,037:
(def decompressed (decompress compressed))
(displayln decompressed)
</syntaxhighlight>
Output:
Line 4,316 ⟶ 5,044:
(T O B E O R N O T 256 258 260 265 259 261 263)
TOBEORNOTTOBEORTOBEORNOT
</pre>
=={{header|Raku}}==
(formerly Perl 6) I just came across [https://stackoverflow.com/questions/30531078/ this SO question] by chance hence the update. Notably the ancestor Perl entry simply works without any further tweak.
{{trans|Perl}}
<syntaxhighlight lang="raku" line># 20200421 Updated Raku programming solution ; add unicode support
sub compress(Str $uncompressed --> Seq) {
my $dict-size = 256;
my %dictionary = (.chr => .chr for ^$dict-size);
my $w = "";
gather {
for $uncompressed.encode('utf8').list.chrs.comb -> $c {
my $wc = $w ~ $c;
if %dictionary{$wc}:exists { $w = $wc }
else {
take %dictionary{$w};
%dictionary{$wc} = +%dictionary;
$w = $c;
}
}
take %dictionary{$w} if $w.chars;
}
}
sub decompress(@compressed --> Str) {
my $dict-size = 256;
my %dictionary = (.chr => .chr for ^$dict-size);
my $w = shift @compressed;
( Blob.new: flat ( gather {
take $w;
for @compressed -> $k {
my $entry;
if %dictionary{$k}:exists { take $entry = %dictionary{$k} }
elsif $k == $dict-size { take $entry = $w ~ $w.substr(0,1) }
else { die "Bad compressed k: $k" }
%dictionary{$dict-size++} = $w ~ $entry.substr(0,1);
$w = $entry;
}
} )».ords ).decode('utf-8')
}
say my @compressed = compress('TOBEORNOTTOBEORTOBEORNOT');
say decompress(@compressed);
@compressed = compress('こんにちは𝒳𝒴𝒵こんにちは𝒳𝒴𝒵こんにちは𝒳𝒴𝒵');
say decompress(@compressed);</syntaxhighlight>
{{out}}
<pre>
[T O B E O R N O T 256 258 260 265 259 261 263]
TOBEORNOTTOBEORTOBEORNOT
こんにちは𝒳𝒴𝒵こんにちは𝒳𝒴𝒵こんにちは𝒳𝒴𝒵
</pre>
Line 4,321 ⟶ 5,105:
===version 1===
{{trans|Java}}
<
* 20.07.2014 Walter Pachl translated from Java
* 21.07.2014 WP allow for blanks in the string
Line 4,396 ⟶ 5,180:
w=entry
End
Return res</
'''Output:'''
<pre>str=TOBEORNOTTOBEORTOBEORNOT
Line 4,414 ⟶ 5,198:
This REXX version can execute on '''ASCII''' or '''EBCDIC''' systems.
<
$$$= '"There is nothing permanent except change." ─── Heraclitus [540 ── 475 BCE]'
parse arg text; if text='' then text= $$$ /*get an optional argument from the CL.*/
say 'original text='
cypher= LZWc(
say 'reconstituted='
say; say ' LZW integers='
exit
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
do k=1 for length(y)+1;
else w=
end /*k*/;
/*──────────────────────────────────────────────────────────────────────────────────────*/
LZWd: procedure; parse arg x y; call LZWi 1; $= @.x;
do
if @.z\=='' |
else if z==# then ?= w || left(w, 1)
$= $ || ?
@.#= w || left(?, 1); w= ?; #= # + 1 /*bump dict. size*/
end /*k*/; return $</syntaxhighlight>
{{out|output|text= when using the default input:}}
<pre>
original text= "There is nothing permanent except change." ─── Heraclitus [540
reconstituted= "There is nothing permanent except change." ─── Heraclitus [540
LZW integers= 34 84 104 101 114 101 32 105 115 32 110 111 116 104 105 110 103 32 112 259 109 97 110 101 110 116 32 101 120 99 101 112 281 99 104 277 103 101 46 34 32 296 196 298 296 32 72 259 97 99 108 105 116 117 264 32 91 53 52 48 32 299 52 55 53 32 66 67 69 93
</pre>
=={{header|Ring}}==
<
# Project : LZW compression
Line 4,526 ⟶ 5,310:
svect = left(svect, len(svect) - 1)
see svect + nl
</syntaxhighlight>
Output:
<pre>
Line 4,536 ⟶ 5,320:
In this version the hashes contain mixed typed data:
<
def compress(uncompressed)
# Build the dictionary.
Line 4,592 ⟶ 5,376:
p compressed
decompressed = decompress(compressed)
puts decompressed</
Output:
<pre>
["T", "O", "B", "E", "O", "R", "N", "O", "T", 256, 258, 260, 265, 259, 261, 263]
TOBEORNOTTOBEORTOBEORNOT
</pre>
=={{header|Rust}}==
{{trans|C Sharp}}
Handles arbitrary byte sequences.
<syntaxhighlight lang="rust">use std::collections::HashMap;
fn compress(data: &[u8]) -> Vec<u32> {
// Build initial dictionary.
let mut dictionary: HashMap<Vec<u8>, u32> = (0u32..=255)
.map(|i| (vec![i as u8], i))
.collect();
let mut w = Vec::new();
let mut compressed = Vec::new();
for &b in data {
let mut wc = w.clone();
wc.push(b);
if dictionary.contains_key(&wc) {
w = wc;
} else {
// Write w to output.
compressed.push(dictionary[&w]);
// wc is a new sequence; add it to the dictionary.
dictionary.insert(wc, dictionary.len() as u32);
w.clear();
w.push(b);
}
}
// Write remaining output if necessary.
if !w.is_empty() {
compressed.push(dictionary[&w]);
}
compressed
}
fn decompress(mut data: &[u32]) -> Vec<u8> {
// Build the dictionary.
let mut dictionary: HashMap::<u32, Vec<u8>> = (0u32..=255)
.map(|i| (i, vec![i as u8]))
.collect();
let mut w = dictionary[&data[0]].clone();
data = &data[1..];
let mut decompressed = w.clone();
for &k in data {
let entry = if dictionary.contains_key(&k) {
dictionary[&k].clone()
} else if k == dictionary.len() as u32 {
let mut entry = w.clone();
entry.push(w[0]);
entry
} else {
panic!("Invalid dictionary!");
};
decompressed.extend_from_slice(&entry);
// New sequence; add it to the dictionary.
w.push(entry[0]);
dictionary.insert(dictionary.len() as u32, w);
w = entry;
}
decompressed
}
fn main() {
let compressed = compress("TOBEORNOTTOBEORTOBEORNOT".as_bytes());
println!("{:?}", compressed);
let decompressed = decompress(&compressed);
let decompressed = String::from_utf8(decompressed).unwrap();
println!("{}", decompressed);
}</syntaxhighlight>
Output:
<pre>
[84, 79, 66, 69, 79, 82, 78, 79, 84, 256, 258, 260, 265, 259, 261, 263]
TOBEORNOTTOBEORTOBEORNOT
</pre>
=={{header|Scala}}==
<
def compress(tc:String) = {
//initial dictionary
Line 4,647 ⟶ 5,518:
val result = decompress(compressed)
println(result)
</syntaxhighlight>
=={{header|Scheme}}==
<
(define (member-string-ref m l)
(define r #f)
Line 4,734 ⟶ 5,605:
(display compressed) (newline)
(define decompressed (lzw-decompress compressed))
(display decompressed) (newline)</
Output:<pre>(84 79 66 69 79 82 78 79 84 256 258 260 265 259 261 263)
TOBEORNOTTOBEORTOBEORNOT</pre>
=={{header|Seed7}}==
<
const func string: lzwCompress (in string: uncompressed) is func
Line 4,813 ⟶ 5,684:
uncompressed := lzwDecompress(compressed);
writeln(uncompressed);
end func;</
Output:
Line 4,825 ⟶ 5,696:
=={{header|Sidef}}==
{{trans|Perl}}
<
func compress(String uncompressed) -> Array {
Line 4,896 ⟶ 5,767:
say compressed.join(' ')
var decompressed = decompress(compressed)
say decompressed</
{{out}}
<pre>T O B E O R N O T 256 258 260 265 259 261 263
Line 4,903 ⟶ 5,774:
=={{header|Swift}}==
{{trans|JavaScript}}
<
class func compress(_ uncompressed:String) -> [Int] {
var dict = [String : Int]()
for i in 0 ..< 256 {
let s = String(
dict[s] = i
}
var dictSize = 256
var w = ""
var result = [Int]()
for c in uncompressed {
let wc = w + String(c)
if dict[wc] != nil {
w = wc
} else {
result.append(dict[w]!)
dict[wc] = dictSize
dictSize += 1
w = String(c)
}
}
if w != "" {
result.append(dict[w]!)
}
return result
}
class func decompress(_ compressed:[Int]) -> String? {
var
for
for k in compressed[1 ..< compressed.count] {
let entry : String
if let x = dict[k] {
entry = x
} else if k == dictSize {
entry = w + String(w[w.startIndex])
} else {
return nil
}
result += entry
dict[dictSize] = w + String(entry[entry.startIndex])
dictSize += 1
w = entry
}
return result
}
}
let comp = LZW.compress("TOBEORNOTTOBEORTOBEORNOT")
if let decomp = LZW.decompress(comp) {
}</
{{out}}
<pre>
Line 4,972 ⟶ 5,846:
=={{header|Tcl}}==
<
variable char2int
variable chars
Line 5,031 ⟶ 5,905:
# or
if {$s eq [LZW::decode [LZW::encode $s]]} then {puts success} else {puts fail} ;# ==> success</
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
Option Explicit
Const numchars=127 'plain ASCII
Function LZWCompress(si)
Dim oDict, intMaxCode, i,z,ii,ss,strCurrent,strNext,j
Set oDict = CreateObject("Scripting.Dictionary")
ReDim a(Len(si))
intMaxCode = numchars
For i = 0 To numchars
oDict.Add Chr(i), i
Next
'strCurrent = ofread.ReadText(1)
strCurrent = Left(si,1)
j=0
For ii=2 To Len(si)
strNext = Mid(si,ii,1)
ss=strCurrent & strNext
If oDict.Exists(ss) Then
strCurrent = ss
Else
a(j)=oDict.Item(strCurrent) :j=j+1
intMaxCode = intMaxCode + 1
oDict.Add ss, intMaxCode
strCurrent = strNext
End If
Next
a(j)=oDict.Item(strCurrent)
ReDim preserve a(j)
LZWCompress=a
Set oDict = Nothing
End Function
Function lzwUncompress(sc)
Dim intNext, intCurrent, intMaxCode, i,ss,istr,s,j
s=""
reDim dict(1000)
intMaxCode = numchars
For i = 0 To numchars : dict(i)= Chr(i) : Next
intCurrent=sc(0)
For j=1 To UBound(sc)
ss=dict(intCurrent)
s= s & ss
intMaxCode = intMaxCode + 1
intnext=sc(j)
If intNext<intMaxCode Then
dict(intMaxCode)=ss & Left(dict(intNext), 1)
Else
dict(intMaxCode)=ss & Left(ss, 1)
End If
intCurrent = intNext
Next
s= s & dict(intCurrent)
lzwUncompress=s
End function
Sub printvec(a)
Dim s,i,x
s="("
For i=0 To UBound (a)
s=s & x & a(i)
x=", "
Next
WScript.echo s &")"
End sub
Dim a,b
b="TOBEORNOTTOBEORTOBEORNOT"
WScript.Echo b
a=LZWCompress (b)
printvec(a)
WScript.echo lzwUncompress (a )
wscript.quit 1
</syntaxhighlight>
{{out}}
<small>
<pre>
TOBEORNOTTOBEORTOBEORNOT
(84, 79, 66, 69, 79, 82, 78, 79, 84, 128, 130, 132, 137, 131, 133, 135)
TOBEORNOTTOBEORTOBEORNOT
</pre>
</small>
=={{header|Wren}}==
{{trans|Kotlin}}
<syntaxhighlight lang="wren">class LZW {
/* Compress a string to a list of output symbols. */
static compress(uncompressed) {
// Build the dictionary.
var dictSize = 256
var dictionary = {}
for (i in 0...dictSize) dictionary[String.fromByte(i)] = i
var w = ""
var result = []
for (c in uncompressed.bytes) {
var cs = String.fromByte(c)
var wc = w + cs
if (dictionary.containsKey(wc)) {
w = wc
} else {
result.add(dictionary[w])
// Add wc to the dictionary.
dictionary[wc] = dictSize
dictSize = dictSize + 1
w = cs
}
}
// Output the code for w
if (w != "") result.add(dictionary[w])
return result
}
/* Decompress a list of output symbols to a string. */
static decompress(compressed) {
// Build the dictionary.
var dictSize = 256
var dictionary = {}
for (i in 0...dictSize) dictionary[i] = String.fromByte(i)
var w = String.fromByte(compressed[0])
var result = w
for (k in compressed.skip(1)) {
var entry
if (dictionary.containsKey(k)) {
entry = dictionary[k]
} else if (k == dictSize) {
entry = w + String.fromByte(w.bytes[0])
} else {
Fiber.abort("Bad compressed k: %(k)")
}
result = result + entry
// Add w + entry[0] to the dictionary.
dictionary[dictSize] = w + String.fromByte(entry.bytes[0])
dictSize = dictSize + 1
w = entry
}
return result
}
}
var compressed = LZW.compress("TOBEORNOTTOBEORTOBEORNOT")
System.print(compressed)
var decompressed = LZW.decompress(compressed)
System.print(decompressed)</syntaxhighlight>
{{out}}
<pre>
[84, 79, 66, 69, 79, 82, 78, 79, 84, 256, 258, 260, 265, 259, 261, 263]
TOBEORNOTTOBEORTOBEORNOT
</pre>
=={{header|Xojo}}==
{{trans|PHP}}
<syntaxhighlight lang="vb">
Function compress(str as String) As String
Dim i as integer
Line 5,142 ⟶ 6,171:
End Function
</syntaxhighlight>
Test:
Line 5,159 ⟶ 6,188:
=={{header|zkl}}==
<
dictionary:=(256).pump(Dictionary(),fcn(n){ return(n.toChar(),n) });
w,compressed:="",List();
Line 5,186 ⟶ 6,215:
}
decommpressed.text
}</
<
compressed.toString(*).println();
lzwUncompress(compressed).println();</
{{out}}
<pre>
|