June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
76
Task/LZW-compression/BaCon/lzw-compression.bacon
Normal file
76
Task/LZW-compression/BaCon/lzw-compression.bacon
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
CONST lzw_data$ = "TOBEORNOTTOBEORTOBEORNOT"
|
||||
|
||||
PRINT "LZWData: ", lzw_data$
|
||||
encoded$ = Encode_LZW$(lzw_data$)
|
||||
PRINT "Encoded: ", encoded$
|
||||
PRINT "Decoded: ", Decode_LZW$(encoded$)
|
||||
|
||||
'----------------------------------------------------------
|
||||
|
||||
FUNCTION Encode_LZW$(sample$)
|
||||
|
||||
LOCAL dict ASSOC int
|
||||
LOCAL ch$, buf$, result$
|
||||
LOCAL nr, x
|
||||
|
||||
FOR nr = 0 TO 255
|
||||
dict(CHR$(nr)) = nr
|
||||
NEXT
|
||||
|
||||
FOR x = 1 TO LEN(sample$)
|
||||
|
||||
ch$ = MID$(sample$, x, 1)
|
||||
|
||||
IF dict(buf$ & ch$) THEN
|
||||
buf$ = buf$ & ch$
|
||||
ELSE
|
||||
result$ = APPEND$(result$, 0, STR$(dict(buf$)))
|
||||
dict(buf$ & ch$) = nr
|
||||
INCR nr
|
||||
buf$ = ch$
|
||||
END IF
|
||||
NEXT
|
||||
|
||||
result$ = APPEND$(result$, 0, STR$(dict(buf$)))
|
||||
|
||||
RETURN result$
|
||||
|
||||
END FUNCTION
|
||||
|
||||
'----------------------------------------------------------
|
||||
|
||||
FUNCTION Decode_LZW$(sample$)
|
||||
|
||||
LOCAL list$ ASSOC STRING
|
||||
LOCAL old$, ch$, x$, out$, result$
|
||||
LOCAL nr
|
||||
|
||||
FOR nr = 0 TO 255
|
||||
list$(STR$(nr)) = CHR$(nr)
|
||||
NEXT
|
||||
|
||||
old$ = TOKEN$(sample$, 1)
|
||||
|
||||
ch$ = list$(old$)
|
||||
result$ = ch$
|
||||
|
||||
FOR x$ IN LAST$(sample$, 1)
|
||||
|
||||
IF NOT(LEN(list$(x$))) THEN
|
||||
out$ = list$(old$)
|
||||
out$ = out$ & ch$
|
||||
ELSE
|
||||
out$ = list$(x$)
|
||||
END IF
|
||||
|
||||
result$ = result$ & out$
|
||||
ch$ = LEFT$(out$, 1)
|
||||
list$(STR$(nr)) = list$(old$) & ch$
|
||||
|
||||
INCR nr
|
||||
old$ = x$
|
||||
NEXT
|
||||
|
||||
RETURN result$
|
||||
|
||||
END FUNCTION
|
||||
Loading…
Add table
Add a link
Reference in a new issue