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