diff options
-rw-r--r-- | src/huffmantree.nim | 70 | ||||
-rw-r--r-- | src/integers.nim | 6 | ||||
-rw-r--r-- | tests/thuffmantree.nim | 74 | ||||
-rw-r--r-- | tests/tintegers.nim | 4 |
4 files changed, 154 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 | |||
17 | import integers, bitreader, bitwriter | ||
18 | |||
19 | const valueLengthFieldBitLength* = 6 # 64 | ||
20 | |||
21 | type HuffmanTreeNodeKind* = enum | ||
22 | branch, | ||
23 | leaf | ||
24 | |||
25 | type HuffmanTreeNode*[T: SomeUnsignedInt] = ref object | ||
26 | case kind: HuffmanTreeNodeKind | ||
27 | of branch: | ||
28 | left, right: HuffmanTreeNode[T] | ||
29 | of leaf: | ||
30 | value: T | ||
31 | |||
32 | proc huffmanBranch*[T](left, right: HuffmanTreeNode[T]): HuffmanTreeNode[T] = | ||
33 | HuffmanTreeNode[T](kind: branch, left: left, right: right) | ||
34 | |||
35 | proc huffmanLeaf*[T](value: T): HuffmanTreeNode[T] = | ||
36 | HuffmanTreeNode[T](kind: leaf, value: value) | ||
37 | |||
38 | proc `==`*[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 | |||
44 | proc 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 | |||
49 | proc 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 | |||
57 | proc 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 = | |||
22 | proc truncateToUint8*(x: SomeUnsignedInt): uint8 = | 22 | proc truncateToUint8*(x: SomeUnsignedInt): uint8 = |
23 | (x and uint8.high).uint8 | 23 | (x and uint8.high).uint8 |
24 | 24 | ||
25 | proc bitLength*[T: SomeUnsignedInt](x: T): int = | ||
26 | var buf = x | ||
27 | while buf > 0.T: | ||
28 | buf = buf shr 1 | ||
29 | result += 1 | ||
30 | |||
25 | proc leastSignificantBits*[T: SomeUnsignedInt](x: T, bits: int): T = | 31 | proc 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 |
diff --git a/tests/thuffmantree.nim b/tests/thuffmantree.nim new file mode 100644 index 0000000..ec40bdb --- /dev/null +++ b/tests/thuffmantree.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 unittest, streams | ||
18 | import bitreader, bitwriter, huffmantree | ||
19 | |||
20 | suite "huffmantree": | ||
21 | let tree = huffmanBranch( | ||
22 | huffmanLeaf(1'u), | ||
23 | huffmanBranch( | ||
24 | huffmanLeaf(2'u), | ||
25 | huffmanLeaf(3'u))) | ||
26 | |||
27 | test "equality": | ||
28 | check huffmanLeaf(12'u) == huffmanLeaf(12'u) | ||
29 | check huffmanLeaf(12'u) != huffmanLeaf(21'u) | ||
30 | check huffmanLeaf(12'u) != huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(12'u)) | ||
31 | check huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(21'u)) == huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(21'u)) | ||
32 | check huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(21'u)) != huffmanBranch(huffmanLeaf(12'u), huffmanLeaf(1'u)) | ||
33 | check tree == tree | ||
34 | |||
35 | test "maxValue": | ||
36 | check tree.maxValue() == 3 | ||
37 | |||
38 | test "deserialise": | ||
39 | let stream = newStringStream() | ||
40 | defer: stream.close() | ||
41 | let bitWriter = stream.bitWriter() | ||
42 | bitWriter.writeBits(valueLengthFieldBitLength, 2'u8) | ||
43 | bitWriter.writeBool(false) # root | ||
44 | bitWriter.writeBool(true) # 1 leaf | ||
45 | bitWriter.writeBits(2, 1'u) | ||
46 | bitWriter.writeBool(false) # right branch | ||
47 | bitWriter.writeBool(true) # 2 leaf | ||
48 | bitWriter.writeBits(2, 2'u) | ||
49 | bitWriter.writeBool(true) # 3 leaf | ||
50 | bitWriter.writeBits(2, 3'u) | ||
51 | bitWriter.flush() | ||
52 | |||
53 | stream.setPosition(0) | ||
54 | let bitReader = stream.bitReader() | ||
55 | check huffmantree.deserialise(bitReader, uint) == tree | ||
56 | |||
57 | test "serialise": | ||
58 | let stream = newStringStream() | ||
59 | defer: stream.close() | ||
60 | let bitWriter = stream.bitWriter() | ||
61 | tree.serialise(bitWriter) | ||
62 | bitWriter.flush() | ||
63 | |||
64 | stream.setPosition(0) | ||
65 | let bitReader = stream.bitReader() | ||
66 | check bitReader.readBits(valueLengthFieldBitLength, uint8) == 2 | ||
67 | check bitReader.readBool() == false # root | ||
68 | check bitReader.readBool() == true # 1 leaf | ||
69 | check bitReader.readBits(2, uint8) == 1 | ||
70 | check bitReader.readBool() == false # right branch | ||
71 | check bitReader.readBool() == true # 2 leaf | ||
72 | check bitReader.readBits(2, uint8) == 2 | ||
73 | check bitReader.readBool() == true # 3 leaf | ||
74 | check bitReader.readBits(2, uint8) == 3 | ||
diff --git a/tests/tintegers.nim b/tests/tintegers.nim index 956e4aa..851e926 100644 --- a/tests/tintegers.nim +++ b/tests/tintegers.nim | |||
@@ -27,6 +27,10 @@ suite "integers": | |||
27 | check truncateToUint8(0x00FA'u16) == 0xFA'u8 | 27 | check truncateToUint8(0x00FA'u16) == 0xFA'u8 |
28 | check truncateToUint8(0xFFFA'u16) == 0xFA'u8 | 28 | check truncateToUint8(0xFFFA'u16) == 0xFA'u8 |
29 | 29 | ||
30 | test "bitLength": | ||
31 | check bitLength(0b1_1111) == 5 | ||
32 | check bitLength(0b1000_0000) == 8 | ||
33 | |||
30 | test "leastSignificantBits": | 34 | test "leastSignificantBits": |
31 | check leastSignificantBits(0xFF'u8, 3) == 0b0000_0111'u8 | 35 | check leastSignificantBits(0xFF'u8, 3) == 0b0000_0111'u8 |
32 | check leastSignificantBits(0b0001_0101'u8, 3) == 0b0000_0101'u8 | 36 | check leastSignificantBits(0b0001_0101'u8, 3) == 0b0000_0101'u8 |