Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
60
Task/LZW-compression/Wren/lzw-compression.wren
Normal file
60
Task/LZW-compression/Wren/lzw-compression.wren
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
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)
|
||||
Loading…
Add table
Add a link
Reference in a new issue