diff options
Diffstat (limited to 'src/huffman/huffmantree.nim')
-rw-r--r-- | src/huffman/huffmantree.nim | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/src/huffman/huffmantree.nim b/src/huffman/huffmantree.nim new file mode 100644 index 0000000..1140694 --- /dev/null +++ b/src/huffman/huffmantree.nim | |||
@@ -0,0 +1,74 @@ | |||
1 | # gzip-like LZSS compressor | ||
2 | # Copyright (C) 2018 Pacien TRAN-GIRARD | ||
3 | # | ||
4 | # This program is free software: you can redistribute it and/or modify | ||
5 | # it under the terms of the GNU Affero General Public License as | ||
6 | # published by the Free Software Foundation, either version 3 of the | ||
7 | # License, or (at your option) any later version. | ||
8 | # | ||
9 | # This program is distributed in the hope that it will be useful, | ||
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | # GNU Affero General Public License for more details. | ||
13 | # | ||
14 | # You should have received a copy of the GNU Affero General Public License | ||
15 | # along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
16 | |||
17 | import tables, heapqueue | ||
18 | import ../integers, ../bitio/bitreader, ../bitio/bitwriter | ||
19 | |||
20 | const valueLengthFieldBitLength* = 6 # 64 | ||
21 | |||
22 | type HuffmanTreeNodeKind* = enum | ||
23 | branch, | ||
24 | leaf | ||
25 | |||
26 | type HuffmanTreeNode*[T: SomeUnsignedInt] = ref object | ||
27 | case kind*: HuffmanTreeNodeKind | ||
28 | of branch: | ||
29 | left*, right*: HuffmanTreeNode[T] | ||
30 | maxChildValue: T | ||
31 | of leaf: | ||
32 | value*: T | ||
33 | |||
34 | proc huffmanBranch*[T](left, right: HuffmanTreeNode[T]): HuffmanTreeNode[T] = | ||
35 | HuffmanTreeNode[T]( | ||
36 | kind: branch, left: left, right: right, | ||
37 | maxChildValue: max(left.maxValue(), right.maxValue())) | ||
38 | |||
39 | proc huffmanLeaf*[T](value: T): HuffmanTreeNode[T] = | ||
40 | HuffmanTreeNode[T](kind: leaf, value: value) | ||
41 | |||
42 | proc `==`*[T](a, b: HuffmanTreeNode[T]): bool = | ||
43 | if a.kind != b.kind: return false | ||
44 | case a.kind: | ||
45 | of branch: a.left == b.left and a.right == b.right | ||
46 | of leaf: a.value == b.value | ||
47 | |||
48 | proc maxValue*[T](node: HuffmanTreeNode[T]): T = | ||
49 | case node.kind: | ||
50 | of branch: node.maxChildValue | ||
51 | of leaf: node.value | ||
52 | |||
53 | proc deserialise*[T](bitReader: BitReader, valueType: typedesc[T]): HuffmanTreeNode[T] = | ||
54 | let valueBitLength = bitReader.readBits(valueLengthFieldBitLength, uint8).int | ||
55 | proc readNode(): HuffmanTreeNode[T] = | ||
56 | case bitReader.readBool(): | ||
57 | of false: huffmanBranch(readNode(), readNode()) | ||
58 | of true: huffmanLeaf(bitReader.readBits(valueBitLength, valueType)) | ||
59 | readNode() | ||
60 | |||
61 | proc serialise*[T](tree: HuffmanTreeNode[T], bitWriter: BitWriter) = | ||
62 | let maxValue = tree.maxValue() | ||
63 | let valueBitLength = maxValue.bitLength() | ||
64 | proc writeNode(node: HuffmanTreeNode[T]) = | ||
65 | case node.kind: | ||
66 | of branch: | ||
67 | bitWriter.writeBool(false) | ||
68 | writeNode(node.left) | ||
69 | writeNode(node.right) | ||
70 | of leaf: | ||
71 | bitWriter.writeBool(true) | ||
72 | bitWriter.writeBits(valueBitLength, node.value) | ||
73 | bitWriter.writeBits(valueLengthFieldBitLength, valueBitLength.uint8) | ||
74 | writeNode(tree) | ||