LZW compression: Difference between revisions

m
{{out}}
(→‎{{header|jq}}: translation of JavaScript)
m ({{out}})
Line 212:
NEXT
= o$</lang>
{{out}}
'''Output:'''
<pre>
84 79 66 69 79 82 78 79 84 256 258 260 265 259 261 263
Line 219:
 
=={{header|C}}==
LZW encoder/decoder. Using variable bit length from 9 to up to 15. Encoder needs to know max allow bits, decoder doesn't. Code 256 for clear table, 257 for end of data, everything are either byte values (<256) or code values.
Encoder needs to know max allow bits, decoder doesn't.
Code 256 for clear table, 257 for end of data,
everything else are either byte values (<256) or code values.
<lang c>#include <stdio.h>
#include <stdlib.h>
Line 520 ⟶ 523:
console.log lzw "TOBEORNOTTOBEORTOBEORNOT"
</lang>
{{out}}
output
<langpre>
> coffee lzw.coffee
[ 84,
Line 539 ⟶ 542:
261,
263 ]
</langpre>
 
 
=={{header|Common Lisp}}==
Line 548 ⟶ 550:
This version is based upon the Perl one. It doesn't contain mixed type data at the cost of being more consy. It includes vector operation routines, since using <code>VECTOR-PUSH-APPEND</code> reallocates the whole vector with each call.
 
The Babel library is required to convert octet vectors to strings. Lisp strings can contain characters out of the ASCII/latin1 character set, including the whole Unicode range in them. The exact encoding used is dependent upon the user's locale (<code>LC_CTYPE</code> on Unix).
Lisp strings can contain characters out of the ASCII/latin1 character set, including the whole Unicode range in them.
The exact encoding used is dependent upon the user's locale (<code>LC_CTYPE</code> on Unix).
 
<lang lisp>(declaim (ftype (function (vector vector &optional fixnum fixnum) vector)
Line 837 ⟶ 841:
}</lang>
 
{{out}}
Output:
<pre>84, 79, 66, 69, 79, 82, 78, 79, 84, 256, 258, 260, 265, 259, 261, 263
TOBEORNOTTOBEORTOBEORNOT</pre>
Line 861 ⟶ 865:
 
(compress "TOBEORNOTTOBEORTOBEORNOT")</lang>
{{out}}
The output:
<lang lisp>(84 79 66 69 79 82 78 79 84 256 258 260 265 259 261 263)</lang>
 
Line 1,572 ⟶ 1,576:
Compressed: $compressed
Uncompressed: '$result'""".stripIndent()</lang>
{{out}}
Output:
<pre>Plaintext: 'TOBEORNOTTOBEORTOBEORNOT'
Compressed: [79, 66, 69, 79, 82, 78, 79, 84, 256, 258, 260, 265, 259, 261, 263]
Line 1,734 ⟶ 1,738:
}</lang>
 
Output{{out}} (Command Line direct output):
<lang java5>[84, 79, 66, 69, 79, 82, 78, 79, 84, 256, 258, 260, 265, 259, 261, 263]
TOBEORNOTTOBEORTOBEORNOT</lang>
Line 1,821 ⟶ 1,825:
document.write(comp + '<br>' + decomp);</lang>
 
{{out}}
Output:
<lang javascriptpre>84,79,66,69,79,82,78,79,84,256,258,260,265,259,261,263
TOBEORNOTTOBEORTOBEORNOT</langpre>
 
=={{header|jq}}==
Line 1,881 ⟶ 1,885:
 
=={{header|Liberty BASIC}}==
The encoder features variable-bit output, a 12 to 21 bit rotating dictionary (that can also be set to "Static"), and an unbalanced binary search tree that assures a worst-case-scenario maximum of 256 searches to find any given index, regardless of the dictionary's size. It uses both read and write buffers so is capable of handling files of any size, and it adds a settings-byte to the beginning of the encoded file to retain the maximum bit-width and rotating status of the dictionary. It also has the option to write the encoding/decoding dictionaries to file so the encoder can be checked for accuracy.
It uses both read and write buffers so is capable of handling files of any size, and it adds a settings-byte to the beginning of the encoded file to retain the maximum bit-width and rotating status of the dictionary.
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".
<lang> DIM LZW(1, 1)
Line 2,435 ⟶ 2,441:
}</lang>
 
Output{{out}} (reformatted by hand):
 
<pre>
84 79 66 69 79 82 78 79
Line 2,642 ⟶ 2,647:
print "$decompressed\n";</lang>
 
{{out}}
Output:
<pre>
T O B E O R N O T 256 258 260 265 259 261 263
Line 2,812 ⟶ 2,817:
 
=={{header|PureBasic}}==
This version encodes character sequences as 16-bit values.
This version encodes character sequences as 16-bit values. Because this version only encodes an input string it won't handle Null values. 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.
Because this version only encodes an input string it won't handle Null values.
This is because PureBasic uses these to terminate strings.
This version encodes character sequences as 16-bit values. Because this version only encodes an input string it won't handle Null values. 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.
<lang PureBasic>Procedure compress(uncompressed.s, List result.u())
;Compress a string to a list of output symbols
Anonymous user