aboutsummaryrefslogtreecommitdiff
path: root/src/lzss/matchring.nim
diff options
context:
space:
mode:
Diffstat (limited to 'src/lzss/matchring.nim')
-rw-r--r--src/lzss/matchring.nim37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/lzss/matchring.nim b/src/lzss/matchring.nim
new file mode 100644
index 0000000..a2d45f7
--- /dev/null
+++ b/src/lzss/matchring.nim
@@ -0,0 +1,37 @@
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
17const matchLimit* = 4
18
19type MatchRing* = object
20 offset, size: int
21 indices: array[matchLimit, int]
22
23proc initMatchRing*(): MatchRing =
24 MatchRing()
25
26proc addMatch*(ring: var MatchRing, index: int) =
27 if ring.size < matchLimit:
28 ring.indices[ring.size] = index
29 ring.size += 1
30 else:
31 let ringIndex = (ring.offset + ring.size) mod matchLimit
32 ring.indices[ringIndex] = index
33 ring.offset = (ring.offset + 1) mod ring.indices.len
34
35iterator items*(ring: MatchRing): int {.closure.} =
36 for i in countdown(ring.size - 1, 0):
37 yield ring.indices[(ring.offset + i) mod ring.indices.len]