aboutsummaryrefslogtreecommitdiff
path: root/src/lzss/lzsschain.nim
diff options
context:
space:
mode:
Diffstat (limited to 'src/lzss/lzsschain.nim')
-rw-r--r--src/lzss/lzsschain.nim47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/lzss/lzsschain.nim b/src/lzss/lzsschain.nim
new file mode 100644
index 0000000..2ecff9e
--- /dev/null
+++ b/src/lzss/lzsschain.nim
@@ -0,0 +1,47 @@
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 lists, tables, sugar
18import ../integers, ../huffman/huffmantree
19import listpolyfill, lzssnode
20
21const maxChainByteLength = 32_000 * wordBitLength
22
23type LzssChain* =
24 SinglyLinkedList[LzssNode]
25
26proc lzssChain*(): LzssChain =
27 initSinglyLinkedList[LzssNode]()
28
29proc decode*(lzssChain: LzssChain): seq[uint8] =
30 result = newSeqOfCap[uint8](maxChainByteLength)
31 for node in lzssChain.items:
32 case node.kind:
33 of character:
34 result.add(node.character)
35 of reference:
36 let absolutePos = result.len - node.relativePos
37 result.add(result.toOpenArray(absolutePos, absolutePos + node.length - 1))
38
39proc stats*(lzssChain: LzssChain): tuple[characters: CountTableRef[uint8], lengths, positions: CountTableRef[int]] =
40 result = (newCountTable[uint8](), newCountTable[int](), newCountTable[int]())
41 for node in lzssChain.items:
42 case node.kind:
43 of character:
44 result.characters.inc(node.character)
45 of reference:
46 result.lengths.inc(node.length)
47 result.positions.inc(node.relativePos)