Skip to content

Commit

Permalink
0.3.8, simpler
Browse files Browse the repository at this point in the history
  • Loading branch information
guzba committed Nov 22, 2020
1 parent 803d67c commit 4325677
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 32 deletions.
48 changes: 17 additions & 31 deletions src/zippy/deflate.nim
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,22 @@ func huffmanCodeLengths(
symbols: seq[uint16]
weight: int

func quickSort(a: var seq[Coin], hi: int) =
## Assumes a.len and lo, hi are <= uint16.high
var
stack: array[32, (uint16, uint16)]
top = 0
stack[0] = (0.uint16, hi.uint16)

while top >= 0:
var (inl, inr) = stack[top]
dec top
var
r = inr
l = inl
let n = r - l + 1
if n < 2:
continue
let p = a[l + 3 * n div 4].weight
while l <= r:
if a[l].weight < p:
inc l
elif a[r].weight > p:
dec r
else:
swap(a[l], a[r])
inc l
dec r
proc quickSort(s: var seq[Coin], lo, hi: int) =
if lo >= hi:
return

stack[top + 1] = (l, inr)
stack[top + 2] = (inl, r)
inc(top, 2)
var
pivot = lo
swapPos = lo + 1
for i in lo + 1 .. hi:
if s[i].weight < s[pivot].weight:
swap(s[i], s[swapPos])
swap(s[pivot], s[swapPos])
inc pivot
inc swapPos

quickSort(s, lo, pivot - 1)
quickSort(s, pivot + 1, hi)

var
highestSymbol: int
Expand Down Expand Up @@ -98,7 +84,7 @@ func huffmanCodeLengths(

addSymbolCoins(coins, 0)

quickSort(coins, numSymbolsUsed - 1)
quickSort(coins, 0, numSymbolsUsed - 1)

var
numCoins = numSymbolsUsed
Expand Down Expand Up @@ -127,7 +113,7 @@ func huffmanCodeLengths(
addSymbolCoins(coins, numCoins)
inc(numCoins, numSymbolsUsed)

quickSort(coins, numCoins - 1)
quickSort(coins, 0, numCoins - 1)

lastTime = numCoins == numCoinsPrev

Expand Down
2 changes: 1 addition & 1 deletion zippy.nimble
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = "0.3.7"
version = "0.3.8"
author = "Ryan Oldenburg"
description = "Pure Nim implementation of deflate, zlib, gzip and zip."
license = "MIT"
Expand Down

0 comments on commit 4325677

Please sign in to comment.