aboutsummaryrefslogtreecommitdiff
path: root/src/huffman
diff options
context:
space:
mode:
Diffstat (limited to 'src/huffman')
-rw-r--r--src/huffman/huffmandecoder.nim35
-rw-r--r--src/huffman/huffmanencoder.nim40
-rw-r--r--src/huffman/huffmantree.nim74
-rw-r--r--src/huffman/huffmantreebuilder.nim44
4 files changed, 193 insertions, 0 deletions
diff --git a/src/huffman/huffmandecoder.nim b/src/huffman/huffmandecoder.nim
new file mode 100644
index 0000000..9a58b55
--- /dev/null
+++ b/src/huffman/huffmandecoder.nim
@@ -0,0 +1,35 @@
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
17import ../bitio/bitreader
18import huffmantree
19
20type HuffmanDecoder*[T: SomeUnsignedInt] = object
21 tree: HuffmanTreeNode[T]
22
23proc decoder*[T](tree: HuffmanTreeNode[T]): HuffmanDecoder[T] =
24 HuffmanDecoder[T](tree: tree)
25
26proc decode*[T](decoder: HuffmanDecoder[T], bitReader: BitReader): T =
27 proc walk(node: HuffmanTreeNode[T]): T =
28 case node.kind:
29 of branch:
30 case bitReader.readBool():
31 of false: walk(node.left)
32 of true: walk(node.right)
33 of leaf:
34 node.value
35 walk(decoder.tree)
diff --git a/src/huffman/huffmanencoder.nim b/src/huffman/huffmanencoder.nim
new file mode 100644
index 0000000..3ed41ec
--- /dev/null
+++ b/src/huffman/huffmanencoder.nim
@@ -0,0 +1,40 @@
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
17import tables
18import ../integers, ../bitio/bitwriter
19import huffmantree
20
21type HuffmanEncoder*[T, U: SomeUnsignedInt] = object
22 codebook: TableRef[T, U]
23
24proc buildCodebook*[T, U](tree: HuffmanTreeNode[T], codeType: typedesc[U]): TableRef[T, U] =
25 var codebook = newTable[T, U]()
26 proc addCode(node: HuffmanTreeNode[T], path: U, depth: int) =
27 case node.kind:
28 of branch:
29 addCode(node.left, path, depth + 1)
30 addCode(node.right, path or (1.U shl depth), depth + 1)
31 of leaf:
32 codebook[node.value] = path
33 addCode(tree, 0.U, 0)
34 codebook
35
36proc encoder*[T, U](tree: HuffmanTreeNode[T], codeType: typedesc[U]): HuffmanEncoder[T, U] =
37 HuffmanEncoder[T, U](codebook: buildCodebook(tree, codeType))
38
39proc encode*[T, U](decoder: HuffmanEncoder[T, U], value: T): U =
40 decoder.codebook[value]
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
17import tables, heapqueue
18import ../integers, ../bitio/bitreader, ../bitio/bitwriter
19
20const valueLengthFieldBitLength* = 6 # 64
21
22type HuffmanTreeNodeKind* = enum
23 branch,
24 leaf
25
26type 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
34proc 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
39proc huffmanLeaf*[T](value: T): HuffmanTreeNode[T] =
40 HuffmanTreeNode[T](kind: leaf, value: value)
41
42proc `==`*[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
48proc maxValue*[T](node: HuffmanTreeNode[T]): T =
49 case node.kind:
50 of branch: node.maxChildValue
51 of leaf: node.value
52
53proc 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
61proc 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)
diff --git a/src/huffman/huffmantreebuilder.nim b/src/huffman/huffmantreebuilder.nim
new file mode 100644
index 0000000..4169099
--- /dev/null
+++ b/src/huffman/huffmantreebuilder.nim
@@ -0,0 +1,44 @@
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
17import tables, heapqueue
18import huffmantree
19
20type WeighedHuffmanTreeNode[T] = ref object
21 weight: int
22 huffmanTreeNode: HuffmanTreeNode[T]
23
24proc weighedHuffmanBranch[T](left, right: WeighedHuffmanTreeNode[T]): WeighedHuffmanTreeNode[T] =
25 WeighedHuffmanTreeNode[T](
26 weight: left.weight + right.weight,
27 huffmanTreeNode: huffmanBranch(left.huffmanTreeNode, right.huffmanTreeNode))
28
29proc weighedHuffmanLeaf[T](value: T, weight: int): WeighedHuffmanTreeNode[T] =
30 WeighedHuffmanTreeNode[T](
31 weight: weight,
32 huffmanTreeNode: huffmanLeaf(value))
33
34proc `<`*[T](left, right: WeighedHuffmanTreeNode[T]): bool =
35 left.weight < right.weight
36
37proc symbolQueue[T](stats: CountTableRef[T]): HeapQueue[WeighedHuffmanTreeNode[T]] =
38 result = newHeapQueue[WeighedHuffmanTreeNode[T]]()
39 for item, count in stats.pairs: result.push(weighedHuffmanLeaf(item, count))
40
41proc buildHuffmanTree*[T: SomeUnsignedInt](stats: CountTableRef[T]): HuffmanTreeNode[T] =
42 var symbolQueue = symbolQueue(stats)
43 while symbolQueue.len > 1: symbolQueue.push(weighedHuffmanBranch(symbolQueue.pop(), symbolQueue.pop()))
44 symbolQueue[0].huffmanTreeNode