This commit is contained in:
Ryan Oldenburg 2020-11-05 10:00:09 -06:00
parent fdd34e349f
commit 582f56b31d
3 changed files with 2 additions and 5 deletions

View File

@ -164,7 +164,7 @@ func huffmanCodeLengths(
func findCodeIndex(a: openarray[uint16], value: uint16): uint16 =
for i in 1 .. a.high:
if value < a[i]:
if value <= a[i]:
return i.uint16 - 1
func lz77Encode(src: seq[uint8]): (seq[uint16], seq[uint64]) =
@ -370,7 +370,6 @@ func compress*(src: seq[uint8]): seq[uint8] =
var i: int
while i < encoded.len:
let symbol = encoded[i]
# debugEcho "c: ", symbol, " ", llCodes[symbol], " ", if symbol <= 255: symbol.char else: 0.char
b.addBits(llCodes[symbol], llLengths[symbol].int)
inc i
if symbol > 256:
@ -386,7 +385,6 @@ func compress*(src: seq[uint8]): seq[uint8] =
b.addBits(lengthExtra, lengthExtraBits)
b.addBits(distCodes[distIndex], distLengths[distIndex].int)
b.addBits(distExtra, distExtraBits)
# debugEcho "cd: ", distIndex, " ", distCodes[distIndex]
if llLengths[256] == 0:
failCompress()

View File

@ -137,7 +137,6 @@ func inflateBlock(b: var BitStream, dst: var seq[uint8], fixedCodes: bool) =
var pos = dst.len
while true:
let symbol = decodeSymbol(b, literalHuffman)
# debugEcho "u: ", symbol, " ", if symbol <= 255: symbol.char else: 0.char
if symbol <= 255:
if pos >= dst.len:
dst.setLen((pos + 1) * 2)

View File

@ -120,7 +120,7 @@ timeIt "lz77":
for i in 0 ..< 1:
for file in files:
let
original = cast[seq[uint8]](readFile(&"data/{file}"))
original = cast[seq[uint8]](readFile(&"tests/data/{file}"))
encoded = lz77Encode(original)
decoded = lz77Decode(encoded)
echo &"{file} original: {original.len} encoded: {encoded.len}"