September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,5 +1,5 @@
|
|||
def compress = { text ->
|
||||
def dictionary = (1..255).inject([:]) { map, ch -> map."${(char)ch}" = ch; map }
|
||||
def dictionary = (0..<256).inject([:]) { map, ch -> map."${(char)ch}" = ch; map }
|
||||
def w = '', compressed = []
|
||||
text.each { ch ->
|
||||
def wc = "$w$ch"
|
||||
|
|
@ -7,7 +7,7 @@ def compress = { text ->
|
|||
w = wc
|
||||
} else {
|
||||
compressed << dictionary[w]
|
||||
dictionary[wc] = dictionary.size() + 1
|
||||
dictionary[wc] = dictionary.size()
|
||||
w = "$ch"
|
||||
}
|
||||
}
|
||||
|
|
@ -16,18 +16,22 @@ def compress = { text ->
|
|||
}
|
||||
|
||||
def decompress = { compressed ->
|
||||
def dictionary = (1..255).inject([:]) { map, ch -> map[ch] = "${(char)ch}"; map }
|
||||
String w = "${(char)compressed.remove(0)}"
|
||||
def dictionary = (0..<256).inject([:]) { map, ch -> map[ch] = "${(char)ch}"; map }
|
||||
int dictSize = 128;
|
||||
String w = "${(char)compressed[0]}"
|
||||
StringBuffer result = new StringBuffer(w)
|
||||
compressed.each { k ->
|
||||
|
||||
compressed.drop(1).each { k ->
|
||||
String entry = dictionary[k]
|
||||
if (!entry) {
|
||||
if (k != dictionary.size()) throw new IllegalArgumentException("Bad compressed k $k")
|
||||
entry = "$w${w[0]}"
|
||||
}
|
||||
result << entry
|
||||
dictionary[dictionary.size() + 1] = "$w${entry[0]}"
|
||||
|
||||
dictionary[dictionary.size()] = "$w${entry[0]}"
|
||||
w = entry
|
||||
}
|
||||
|
||||
result.toString()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue