From b03508ea5e20370de26c6faf23bbbdd4e89ab1a9 Mon Sep 17 00:00:00 2001 From: pacien Date: Fri, 30 Nov 2018 16:38:17 +0100 Subject: isolate huffman tree construction --- src/huffmantree.nim | 31 ++++--------------------------- src/huffmantreebuilder.nim | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 27 deletions(-) create mode 100644 src/huffmantreebuilder.nim (limited to 'src') diff --git a/src/huffmantree.nim b/src/huffmantree.nim index 44c9990..0266dfb 100644 --- a/src/huffmantree.nim +++ b/src/huffmantree.nim @@ -30,35 +30,21 @@ type HuffmanTreeNode*[T: SomeUnsignedInt] = ref object maxChildValue: T of leaf: value*: T - weight: int proc huffmanBranch*[T](left, right: HuffmanTreeNode[T]): HuffmanTreeNode[T] = HuffmanTreeNode[T]( kind: branch, left: left, right: right, - maxChildValue: max(left.maxValue(), right.maxValue()), - weight: left.weight + right.weight) + maxChildValue: max(left.maxValue(), right.maxValue())) -proc huffmanLeaf*[T](value: T, weight = 0): HuffmanTreeNode[T] = - HuffmanTreeNode[T](kind: leaf, value: value, weight: weight) +proc huffmanLeaf*[T](value: T): HuffmanTreeNode[T] = + HuffmanTreeNode[T](kind: leaf, value: value) proc `==`*[T](a, b: HuffmanTreeNode[T]): bool = - if a.kind != b.kind or a.weight != b.weight: return false - case a.kind: - of branch: a.left == b.left and a.right == b.right - of leaf: a.value == b.value - -proc `~=`*[T](a, b: HuffmanTreeNode[T]): bool = if a.kind != b.kind: return false case a.kind: - of branch: a.left ~= b.left and a.right ~= b.right + of branch: a.left == b.left and a.right == b.right of leaf: a.value == b.value -proc `!~`*[T](a, b: HuffmanTreeNode[T]): bool = - not (a ~= b) - -proc `<`*[T](left, right: HuffmanTreeNode[T]): bool = - left.weight < right.weight - proc maxValue*[T](node: HuffmanTreeNode[T]): T = case node.kind: of branch: node.maxChildValue @@ -86,12 +72,3 @@ proc serialise*[T](tree: HuffmanTreeNode[T], bitWriter: BitWriter) = bitWriter.writeBits(valueBitLength, node.value) bitWriter.writeBits(valueLengthFieldBitLength, valueBitLength.uint8) writeNode(tree) - -proc symbolQueue*[T](stats: CountTableRef[T]): HeapQueue[HuffmanTreeNode[T]] = - result = newHeapQueue[HuffmanTreeNode[T]]() - for item, count in stats.pairs: result.push(huffmanLeaf(item, count)) - -proc buildHuffmanTree*[T: SomeUnsignedInt](stats: CountTableRef[T]): HuffmanTreeNode[T] = - var symbolQueue = symbolQueue(stats) - while symbolQueue.len > 1: symbolQueue.push(huffmanBranch(symbolQueue.pop(), symbolQueue.pop())) - symbolQueue[0] diff --git a/src/huffmantreebuilder.nim b/src/huffmantreebuilder.nim new file mode 100644 index 0000000..5b33d34 --- /dev/null +++ b/src/huffmantreebuilder.nim @@ -0,0 +1,44 @@ +# gzip-like LZSS compressor +# Copyright (C) 2018 Pacien TRAN-GIRARD +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +import tables, heapqueue +import huffmantree, lzssencoder + +type WeighedHuffmanTreeNode[T] = ref object + weight: int + huffmanTreeNode: HuffmanTreeNode[T] + +proc weighedHuffmanBranch[T](left, right: WeighedHuffmanTreeNode[T]): WeighedHuffmanTreeNode[T] = + WeighedHuffmanTreeNode[T]( + weight: left.weight + right.weight, + huffmanTreeNode: huffmanBranch(left.huffmanTreeNode, right.huffmanTreeNode)) + +proc weighedHuffmanLeaf[T](value: T, weight: int): WeighedHuffmanTreeNode[T] = + WeighedHuffmanTreeNode[T]( + weight: weight, + huffmanTreeNode: huffmanLeaf(value)) + +proc `<`*[T](left, right: WeighedHuffmanTreeNode[T]): bool = + left.weight < right.weight + +proc symbolQueue[T](stats: CountTableRef[T]): HeapQueue[WeighedHuffmanTreeNode[T]] = + result = newHeapQueue[WeighedHuffmanTreeNode[T]]() + for item, count in stats.pairs: result.push(weighedHuffmanLeaf(item, count)) + +proc buildHuffmanTree*[T: SomeUnsignedInt](stats: CountTableRef[T]): HuffmanTreeNode[T] = + var symbolQueue = symbolQueue(stats) + while symbolQueue.len > 1: symbolQueue.push(weighedHuffmanBranch(symbolQueue.pop(), symbolQueue.pop())) + symbolQueue[0].huffmanTreeNode -- cgit v1.2.3