September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,14 +1,14 @@
|
|||
# Compress a string to a list of output symbols.
|
||||
func compress(String uncompressed) -> Array {
|
||||
|
||||
|
||||
# Build the dictionary.
|
||||
var dict_size = 256
|
||||
var dictionary = Hash()
|
||||
|
||||
|
||||
for i in range(dict_size) {
|
||||
dictionary{i.chr} = i.chr
|
||||
}
|
||||
|
||||
|
||||
var w = ''
|
||||
var result = []
|
||||
uncompressed.each { |c|
|
||||
|
|
@ -23,26 +23,26 @@ func compress(String uncompressed) -> Array {
|
|||
w = c
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# Output the code for w.
|
||||
if (w != '') {
|
||||
if (w != '') {
|
||||
result << dictionary{w}
|
||||
}
|
||||
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
|
||||
# Decompress a list of output ks to a string.
|
||||
func decompress(Array compressed) -> String {
|
||||
|
||||
|
||||
# Build the dictionary.
|
||||
var dict_size = 256
|
||||
var dictionary = Hash()
|
||||
|
||||
|
||||
for i in range(dict_size) {
|
||||
dictionary{i.chr} = i.chr
|
||||
}
|
||||
|
||||
|
||||
var w = compressed.shift
|
||||
var result = w
|
||||
compressed.each { |k|
|
||||
|
|
@ -55,16 +55,16 @@ func decompress(Array compressed) -> String {
|
|||
die "Bad compressed k: #{k}"
|
||||
}
|
||||
result += entry
|
||||
|
||||
|
||||
# Add w+entry[0] to the dictionary.
|
||||
dictionary{dict_size} = w+entry.first
|
||||
dict_size++
|
||||
|
||||
|
||||
w = entry
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
|
||||
# How to use:
|
||||
var compressed = compress('TOBEORNOTTOBEORTOBEORNOT')
|
||||
say compressed.join(' ')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue