LZW compression: Difference between revisions

Applesoft BASIC
(Applesoft BASIC)
 
(10 intermediate revisions by 3 users not shown)
Line 209:
end;
end Test;</syntaxhighlight>
 
=={{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}}==
Line 1,747 ⟶ 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>
 
Line 2,864 ⟶ 2,978:
}
}, // For Test Purposes
comp = LZW.compress("Misbah Mansuri AmmarTOBEORNOTTOBEORTOBEORNOT"),
decomp = LZW.decompress(comp);
document.write(comp + '<br>' + decomp);</syntaxhighlight>
 
 
 
Line 5,882 ⟶ 5,995:
=={{header|Wren}}==
{{trans|Kotlin}}
<syntaxhighlight lang="ecmascriptwren">class LZW {
/* Compress a string to a list of output symbols. */
static compress(uncompressed) {
413

edits