This commit is contained in:
Ryan Oldenburg 2020-11-02 22:30:10 -06:00
parent 6f95e3d9b7
commit 568df00fdc
1 changed files with 21 additions and 22 deletions

View File

@ -26,11 +26,6 @@ const
0xDF, 0x3F, 0xBF, 0x7F, 0xFF 0xDF, 0x3F, 0xBF, 0x7F, 0xFF
] ]
type
Coin = object
symbols: seq[uint16]
weight: uint64
# {.push checks: off.} # {.push checks: off.}
template failCompress() = template failCompress() =
@ -38,7 +33,16 @@ template failCompress() =
ZippyError, "Unexpected error while compressing" ZippyError, "Unexpected error while compressing"
) )
func quickSort(s: var seq[Coin], lo, hi: int) = func huffmanCodeLengths(
frequencies: seq[uint64], minCodes, maxBitLen: int
): (int, seq[uint8], seq[uint16]) =
# See https://en.wikipedia.org/wiki/Huffman_coding#Length-limited_Huffman_coding
type Coin = object
symbols: seq[uint16]
weight: uint64
func quickSort(s: var seq[Coin], lo, hi: int) =
if lo >= hi: if lo >= hi:
return return
@ -55,11 +59,6 @@ func quickSort(s: var seq[Coin], lo, hi: int) =
quickSort(s, lo, pivot - 1) quickSort(s, lo, pivot - 1)
quickSort(s, pivot + 1, hi) quickSort(s, pivot + 1, hi)
func huffmanCodeLengths(
frequencies: seq[uint64], minCodes, maxBitLen: int
): (int, seq[uint8], seq[uint16]) =
# See https://en.wikipedia.org/wiki/Huffman_coding#Length-limited_Huffman_coding
var numSymbolsUsed: int var numSymbolsUsed: int
for freq in frequencies: for freq in frequencies:
if freq > 0: if freq > 0: