Files

lzw.src
  • // require lst
  • // require SafeMap
  • Lzw = {}
  • Lzw.generate_dict = function(length)
  • result = {}
  • for i in range(length - 1).sort
  • result[char(i)] = char(i)
  • end for
  • return result
  • end function
  • Lzw.compress = function(uncompressed)
  • dict_size = 256
  • dictionary = self.generate_dict(dict_size)
  • w = ""
  • result = []
  • for c in uncompressed.values
  • wc = w + c
  • if dictionary.hasIndex(wc) then
  • //if dictionary.hasIndex(wc) then
  • if SafeMap.hasIndex(dictionary, wc) then
  • w = wc
  • else
  • result.push(dictionary[w])
  • dictionary[wc] = dict_size
  • //result.push(dictionary[w])
  • result.push(SafeMap.get(dictionary, w))
  • //dictionary[wc] = dict_size
  • SafeMap.set(dictionary, wc, dict_size)
  • dict_size = dict_size + 1
  • w = c
  • end if
  • end for
  • if w != "" then result.push(dictionary[w])
  • //if w != "" then result.push(dictionary[w])
  • if w != "" then result.push(SafeMap.get(dictionary, w))
  • return result
  • end function
  • Lzw.decompress = function(compressed)
  • dict_size = 256
  • dictionary = self.generate_dict(dict_size)
  • w = compressed[0]
  • result = compressed[0]
  • compressed = compressed[1:]
  • for k in compressed
  • if dictionary.hasIndex(k) then
  • entry = dictionary[k]
  • //if dictionary.hasIndex(k) then
  • if SafeMap.hasIndex(dictionary, k) then
  • //entry = dictionary[k]
  • entry = SafeMap.get(dictionary, k)
  • else if k == dict_size then
  • entry = w + w[0]
  • else
  • exit("Bad compressed k: " + k)
  • end if
  • result = result + entry
  • dictionary[dict_size] = w + entry[0]
  • //dictionary[dict_size] = w + entry[0]
  • SafeMap.set(dictionary, dict_size, w + entry[0])
  • dict_size = dict_size + 1
  • w = entry
  • end for
  • return result
  • end function
test.src
  • import_code("/home/me/projects/lzw/libs/lst.src")
  • import_code("/home/me/projects/lzw/libs/safemap.src")
  • import_code("/home/me/projects/lzw/lzw.src")
  • import_code("/home/me/projects/lzw/encoder.src")
  • input = user_input("in: ")
  • compressed = Lzw.compress(input)
  • //compressed = Lzw.compress("TOBEORNOTTOBEORTOBEORNOT")
  • //compressed = Lzw.compress("ffffffffffffffffffffffff")
  • //print "compressed: " + compressed
  • e = Encoder.encode(compressed)
  • print "encoded: " + e
  • dd = Lzw.decompress(Encoder.decode(e))
  • //print "decoded and decompressed: " + dd
  • if input != dd then exit("<color=red>ERROR</color>")
  • print "input len: " + input.len
  • print "encoded len: " + e.len
  • print "text reduced by: " + round(100 - (e.len / input.len * 100), 2) + "%"
  • //print input
  • //print dd
  • //print input == dd
libs/safemap.src
  • SafeMap = {}
  • SafeMap.methods = ["hasIndex", "indexOf", "indexes", "len", "pop", "push", "remove", "shuffle", "sum", "values"]
  • SafeMap.escaped = []
  • for i in SafeMap.methods.indexes
  • SafeMap.escaped.push(char(2000 + i))
  • end for
  • SafeMap.escape = function(index)
  • if self.methods.indexOf(index) != null then
  • index = self.escaped[self.methods.indexOf(index)]
  • end if
  • return index
  • end function
  • SafeMap.unescape = function(index)
  • if self.escaped.indexOf(index) != null then
  • index = self.methods[self.escaped.indexOf(index)]
  • end if
  • return index
  • end function
  • SafeMap.set = function(dict, index, value)
  • index = self.escape(index)
  • dict[index] = value
  • return dict
  • end function
  • SafeMap.get = function(dict, index)
  • index = self.escape(index)
  • return dict[index]
  • end function
  • SafeMap.hasIndex = function(dict, index)
  • index = self.escape(index)
  • return dict.hasIndex(index)
  • end function
  • SafeMap.indexOf = function(dict, value)
  • index = dict.indexOf(value)
  • index = self.unescape(index)
  • return index
  • end function
  • SafeMap.indexes = function(dict)
  • indexes = dict.indexes
  • for i in indexes.indexes
  • indexes[i] = self.unescape(indexes[i])
  • end for
  • return indexes
  • end function
  • SafeMap.len = function(dict)
  • return dict.len
  • end function
  • SafeMap.pop = function(dict)
  • return dict.pop
  • end function
  • SafeMap.push = function(dict, index)
  • index = self.escape(index)
  • return dict.push(index)
  • end function
  • SafeMap.remove = function(dict, index)
  • index = self.escape(index)
  • return dict.remove(index)
  • end function
  • SafeMap.shuffle = function(dict)
  • return dict.shuffle
  • end function
  • SafeMap.sum = function(dict)
  • return dict.sum
  • end function
  • SafeMap.values = function(dict)
  • return dict.values
  • end function