aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpacien2018-11-30 17:08:01 +0100
committerpacien2018-11-30 17:08:01 +0100
commite88f60b63cb05f56a61060a953c726b7a78c0652 (patch)
treef7ee9b401ae92e7eb3fe8d9e352e20da3d2e9243
parent8af38da097b8358cb273baa37c748120461c718e (diff)
downloadgziplike-e88f60b63cb05f56a61060a953c726b7a78c0652.tar.gz
isolate huffman encoding module
-rw-r--r--src/huffman/huffmandecoder.nim (renamed from src/huffmandecoder.nim)3
-rw-r--r--src/huffman/huffmanencoder.nim (renamed from src/huffmanencoder.nim)3
-rw-r--r--src/huffman/huffmantree.nim (renamed from src/huffmantree.nim)2
-rw-r--r--src/huffman/huffmantreebuilder.nim (renamed from src/huffmantreebuilder.nim)2
-rw-r--r--src/lzsschain.nim2
-rw-r--r--tests/thuffman.nim (renamed from tests/thuffmantree.nim)51
-rw-r--r--tests/thuffmandecoder.nim43
-rw-r--r--tests/thuffmanencoder.nim39
-rw-r--r--tests/thuffmantreebuilder.nim29
9 files changed, 55 insertions, 119 deletions
<
diff --git a/src/huffmandecoder.nim b/src/huffman/huffmandecoder.nim
index 5cf4ca5..9a58b55 100644
--- a/src/huffmandecoder.nim
+++ b/src/huffman/huffmandecoder.nim
@@ -14,7 +14,8 @@
14# You should have received a copy of the GNU Affero General Public License 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/>. 15# along with this program. If not, see <https://www.gnu.org/licenses/>.
16 16
17import huffmantree, bitio/bitreader 17import ../bitio/bitreader
18import huffmantree
18 19
19type HuffmanDecoder*[T: SomeUnsignedInt] = object 20type HuffmanDecoder*[T: SomeUnsignedInt] = object
20 tree: HuffmanTreeNode[T] 21 tree: HuffmanTreeNode[T]
diff --git a/src/huffmanencoder.nim b/src/huffman/huffmanencoder.nim
index 05ed64f..3ed41ec 100644
--- a/src/huffmanencoder.nim
+++ b/src/huffman/huffmanencoder.nim
@@ -15,7 +15,8 @@
15# along with this program. If not, see <https://www.gnu.org/licenses/>. 15# along with this program. If not, see <https://www.gnu.org/licenses/>.
16 16
17import tables 17import tables
18import integers, huffmantree, bitio/bitwriter 18import ../integers, ../bitio/bitwriter
19import huffmantree
19 20
20type HuffmanEncoder*[T, U: SomeUnsignedInt] = object 21type HuffmanEncoder*[T, U: SomeUnsignedInt] = object
21 codebook: TableRef[T, U] 22 codebook: TableRef[T, U]
diff --git a/src/huffmantree.nim b/src/huffman/huffmantree.nim
index 6208ecf..1140694 100644
--- a/src/huffmantree.nim
+++ b/src/huffman/huffmantree.nim
@@ -15,7 +15,7 @@
15# along with this program. If not, see <https://www.gnu.org/licenses/>. 15# along with this program. If not, see <https://www.gnu.org/licenses/>.
16 16
17import tables, heapqueue 17import tables, heapqueue
18import integers, bitio/bitreader, bitio/bitwriter 18import ../integers, ../bitio/bitreader, ../bitio/bitwriter
19 19
20const valueLengthFieldBitLength* = 6 # 64 20const valueLengthFieldBitLength* = 6 # 64
21 21
diff --git a/src/huffmantreebuilder.nim b/src/huffman/huffmantreebuilder.nim
index 5b33d34..4169099 100644
--- a/src/huffmantreebuilder.nim
+++ b/src/huffman/huffmantreebuilder.nim
@@ -15,7 +15,7 @@
15# along with this program. If not, see <https://www.gnu.org/licenses/>. 15# along with this program. If not, see <https://www.gnu.org/licenses/>.
16 16
17import tables, heapqueue 17import tables, heapqueue
18import huffmantree, lzssencoder 18import huffmantree
19 19
20type WeighedHuffmanTreeNode[T] = ref object 20type WeighedHuffmanTreeNode[T] = ref object
21 weight: int 21 weight: int
diff --git a/src/lzsschain.nim b/src/lzsschain.nim
index 073aa5e..44200f2 100644
--- a/src/lzsschain.nim
+++ b/src/lzsschain.nim
@@ -15,7 +15,7 @@
15# along with this program. If not, see <https://www.gnu.org/licenses/>. 15# along with this program. If not, see <https://www.gnu.org/licenses/>.
16 16
17import lists, tables, sugar 17import lists, tables, sugar
18import polyfill, integers, lzssnode, huffmantree 18import polyfill, integers, lzssnode, huffman/huffmantree
19 19
20const maxChainByteLength = 32_000 * wordBitLength 20const maxChainByteLength = 32_000 * wordBitLength
21 21
diff --git a/tests/thuffmantree.nim b/tests/thuffman.nim
index 467fac5..0294694 100644
--- a/tests/thuffmantree.nim
+++ b/tests/thuffman.nim
@@ -15,15 +15,18 @@
15# along with this program. If not, see <https://www.gnu.org/licenses/>. 15# along with this program. If not, see <https://www.gnu.org/licenses/>.
16 16
17import unittest, streams, sequtils, tables, heapqueue 17import unittest, streams, sequtils, tables, heapqueue
18import bitio/bitreader, bitio/bitwriter, huffmantree 18import bitio/bitreader, bitio/bitwriter
19import huffman/huffmantree, huffman/huffmantreebuilder, huffman/huffmanencoder, huffman/huffmandecoder
19 20
20suite "huffmantree": 21let
21 let tree = huffmanBranch( 22 stats = newCountTable(concat(repeat(1'u, 3), repeat(2'u, 1), repeat(3'u, 2)))
23 tree = huffmanBranch(
22 huffmanLeaf(1'u), 24 huffmanLeaf(1'u),
23 huffmanBranch( 25 huffmanBranch(
24 huffmanLeaf(2'u), 26 huffmanLeaf(2'u),
25 huffmanLeaf(3'u))) 27 huffmanLeaf(3'u)))
26 28
29suite "huffmantree":
27 test "equality": 30 test "equality":
28 check huffmanLeaf(12'u) == huffmanLeaf(12'u) 31 check huffmanLeaf(12'u) == huffmanLeaf(12'u)
29 check huffmanLeaf(12'u) != huffmanLeaf(21'u) 32 check huffmanLeaf(12'u) != huffmanLeaf(21'u)
@@ -72,3 +75,45 @@ suite "huffmantree":
72 check bitReader.readBits(2, uint8) == 2 75 check bitReader.readBits(2, uint8) == 2
73 check bitReader.readBool() == true # 3 leaf 76 check bitReader.readBool() == true # 3 leaf
74 check bitReader.readBits(2, uint8) == 3 77 check bitReader.readBits(2, uint8) == 3
78
79suite "huffmantreebuilder":
80 test "buildHuffmanTree":
81 check buildHuffmanTree(stats) == tree
82
83suite "huffencoder":
84 let tree = huffmanBranch(
85 huffmanLeaf(1'u),
86 huffmanBranch(
87 huffmanLeaf(2'u),
88 huffmanLeaf(3'u)))
89
90 test "buildCodebook":
91 let codebook = buildCodebook(tree, uint)
92 check codebook.len == 3
93 check codebook[1'u] == 0b0
94 check codebook[2'u] == 0b01
95 check codebook[3'u] == 0b11
96
97 test "encode":
98 let encoder = tree.encoder(uint)
99 check encoder.encode(1'u) == 0b0
100 check encoder.encode(2'u) == 0b01
101 check encoder.encode(3'u) == 0b11
102
103suite "huffdecoder":
104 test "decode":
105 let stream = newStringStream()
106 defer: stream.close()
107 let bitWriter = stream.bitWriter()
108 bitWriter.writeBool(true) # 2
109 bitWriter.writeBool(false)
110 bitWriter.writeBool(false) # 1
111 bitWriter.writeBool(true) # 3
112 bitWriter.writeBool(true)
113 bitWriter.flush()
114 stream.setPosition(0)
115 let bitReader = stream.bitReader()
116 let decoder = tree.decoder()
117 check decoder.decode(bitReader) == 2'u
118 check decoder.decode(bitReader) == 1'u
119 check decoder.decode(bitReader) == 3'u
diff --git a/tests/thuffmandecoder.nim b/tests/thuffmandecoder.nim
deleted file mode 100644
index 9b44e9d..0000000
--- a/tests/thuffmandecoder.nim
+++ /dev/null
@@ -1,43 +0,0 @@
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 unittest, streams
18import bitio/bitreader, bitio/bitwriter
19import huffmantree, huffmandecoder
20
21suite "huffdecoder":
22 let tree = huffmanBranch(
23 huffmanLeaf(1'u),
24 huffmanBranch(
25 huffmanLeaf(2'u),
26 huffmanLeaf(3'u)))
27
28 test "decode":
29 let stream = newStringStream()
30 defer: stream.close()
31 let bitWriter = stream.bitWriter()
32 bitWriter.writeBool(true) # 2
33 bitWriter.writeBool(false)
34 bitWriter.writeBool(false) # 1
35 bitWriter.writeBool(true) # 3
36 bitWriter.writeBool(true)
37 bitWriter.flush()
38 stream.setPosition(0)
39 let bitReader = stream.bitReader()
40 let decoder = tree.decoder()
41 check decoder.decode(bitReader) == 2'u
42 check decoder.decode(bitReader) == 1'u
43 check decoder.decode(bitReader) == 3'u
diff --git a/tests/thuffmanencoder.nim b/tests/thuffmanencoder.nim
deleted file mode 100644
index 9c46eed..0000000
--- a/tests/thuffmanencoder.nim
+++ /dev/null
@@ -1,39 +0,0 @@
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 unittest, streams, tables
18import bitio/bitreader, bitio/bitwriter
19import huffmantree, huffmanencoder
20
21suite "huffencoder":
22 let tree = huffmanBranch(
23 huffmanLeaf(1'u),
24 huffmanBranch(
25 huffmanLeaf(2'u),
26 huffmanLeaf(3'u)))
27
28 test "buildCodebook":
29 let codebook = buildCodebook(tree, uint)
30 check codebook.len == 3
31 check codebook[1'u] == 0b0
32 check codebook[2'u] == 0b01
33 check codebook[3'u] == 0b11
34
35 test "encode":
36 let encoder = tree.encoder(uint)
37 check encoder.encode(1'u) == 0b0
38 check encoder.encode(2'u) == 0b01
39 check encoder.encode(3'u) == 0b11
diff --git a/tests/thuffmantreebuilder.nim b/tests/thuffmantreebuilder.nim
deleted file mode 100644
index f782b37..0000000
--- a/tests/thuffmantreebuilder.nim
+++ /dev/null
@@ -1,29 +0,0 @@
1# gzip-like LZSS compressor
2# Copyright (C) 2018 Pacien TRAN-GIRARD
3#