aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorpacien2018-11-28 15:20:14 +0100
committerpacien2018-11-28 15:20:14 +0100
commitd661132528d5c27148a0b55d52709ce97124000a (patch)
tree31aeb46872fd70b409633e163c0bc0bfbb825429 /src
parent3d44208aaaeca516eb08a90c98635543cae2bd4d (diff)
downloadgziplike-d661132528d5c27148a0b55d52709ce97124000a.tar.gz
add huffman tree structure and serialisation
Diffstat (limited to 'src')
-rw-r--r--src/huffmantree.nim70
-rw-r--r--src/integers.nim6
2 files changed, 76 insertions, 0 deletions
diff --git a/src/huffmantree.nim b/src/huffmantree.nim
new file mode 100644
index 0000000..1711879
--- /dev/null
+++ b/src/huffmantree.nim
@@ -0,0 +1,70 @@
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 integers, bitreader, bitwriter
18
19const valueLengthFieldBitLength* = 6 # 64
20
21type HuffmanTreeNodeKind* = enum
22 branch,
23 leaf
24
25type HuffmanTreeNode*[T: SomeUnsignedInt] = ref object
26 case kind: HuffmanTreeNodeKind
27 of branch:
28 left, right: HuffmanTreeNode[T]
29 of leaf:
30 value: T
31
32proc huffmanBranch*[T](left, right: HuffmanTreeNode[T]): HuffmanTreeNode[T] =
33 HuffmanTreeNode[T](kind: branch, left: left, right: right)
34
35proc huffmanLeaf*[T](value: T): HuffmanTreeNode[T] =
36 HuffmanTreeNode[T](kind: leaf, value: value)
37
38proc `==`*[T](a, b: HuffmanTreeNode[T]): bool =
39 if a.kind != b.kind: return false
40 case a.kind:
41 of branch: a.left == b.left and a.right == b.right
42 of leaf: a.value == b.value
43
44proc maxValue*[T](node: HuffmanTreeNode[T]): T =
45 case node.kind:
46 of branch: max(node.left.maxValue(), node.right.maxValue())
47 of leaf: node.value
48
49proc deserialise*[T](bitReader: BitReader, valueType: typedesc[T]): HuffmanTreeNode[T] =
50 let valueBitLength = bitReader.readBits(valueLengthFieldBitLength, uint8).int
51 proc readNode(): HuffmanTreeNode[T] =
52 case bitReader.readBool():
53 of false: huffmanBranch(readNode(), readNode())
54 of true: huffmanLeaf(bitReader.readBits(valueBitLength, valueType))
55 readNode()
56
57proc serialise*[T](tree: HuffmanTreeNode[T], bitWriter: BitWriter) =
58 let maxValue = tree.maxValue()
59 let valueBitLength = maxValue.bitLength()
60 proc writeNode(node: HuffmanTreeNode[T]) =
61 case node.kind:
62 of branch:
63 bitWriter.writeBool(false)
64 writeNode(node.left)
65 writeNode(node.right)
66 of leaf:
67 bitWriter.writeBool(true)
68 bitWriter.writeBits(valueBitLength, node.value)
69 bitWriter.writeBits(valueLengthFieldBitLength, valueBitLength.uint8)
70 writeNode(tree)
diff --git a/src/integers.nim b/src/integers.nim
index 7b0f166..c93c9b8 100644
--- a/src/integers.nim
+++ b/src/integers.nim
@@ -22,6 +22,12 @@ proc `/^`*[T: Natural](x, y: T): T =
22proc truncateToUint8*(x: SomeUnsignedInt): uint8 = 22proc truncateToUint8*(x: SomeUnsignedInt): uint8 =
23 (x and uint8.high).uint8 23 (x and uint8.high).uint8
24 24
25proc bitLength*[T: SomeUnsignedInt](x: T): int =
26 var buf = x
27 while buf > 0.T:
28 buf = buf shr 1
29 result += 1
30
25proc leastSignificantBits*[T: SomeUnsignedInt](x: T, bits: int): T = 31proc leastSignificantBits*[T: SomeUnsignedInt](x: T, bits: int): T =
26 let maskOffset = sizeof(T) * wordBitLength - bits 32 let maskOffset = sizeof(T) * wordBitLength - bits
27 if maskOffset >= 0: (x shl maskOffset) shr maskOffset else: x 33 if maskOffset >= 0: (x shl maskOffset) shr maskOffset else: x