Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
36
Task/LZW-compression/CoffeeScript/lzw-compression.coffee
Normal file
36
Task/LZW-compression/CoffeeScript/lzw-compression.coffee
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
lzw = (s) ->
|
||||
dct = {} # map substrings to codes between 256 and 4096
|
||||
stream = [] # array of compression results
|
||||
|
||||
# initialize basic ASCII characters
|
||||
for code_num in [0..255]
|
||||
c = String.fromCharCode(code_num)
|
||||
dct[c] = code_num
|
||||
code_num = 256
|
||||
|
||||
i = 0
|
||||
while i < s.length
|
||||
# Find word and new_word
|
||||
# word = longest substr already encountered, or next character
|
||||
# new_word = word plus next character, a new substr to encode
|
||||
word = ''
|
||||
j = i
|
||||
while j < s.length
|
||||
new_word = word + s[j]
|
||||
break if !dct[new_word]
|
||||
word = new_word
|
||||
j += 1
|
||||
|
||||
# stream out the code for the substring
|
||||
stream.push dct[word]
|
||||
|
||||
# build up our encoding dictionary
|
||||
if code_num < 4096
|
||||
dct[new_word] = code_num
|
||||
code_num += 1
|
||||
|
||||
# advance thru the string
|
||||
i += word.length
|
||||
stream
|
||||
|
||||
console.log lzw "TOBEORNOTTOBEORTOBEORNOT"
|
||||
Loading…
Add table
Add a link
Reference in a new issue