aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/lzss/matchtable.nim19
1 files changed, 14 insertions, 5 deletions
diff --git a/src/lzss/matchtable.nim b/src/lzss/matchtable.nim
index 94fe208..cc04f49 100644
--- a/src/lzss/matchtable.nim
+++ b/src/lzss/matchtable.nim
@@ -16,15 +16,24 @@
16 16
17import tables 17import tables
18 18
19type MatchTable*[K, V] = TableRef[K, seq[V]] 19type MatchTable*[K, V] = ref object
20 matchLimit: int
21 table: TableRef[K, seq[V]]
20 22
21proc initMatchTable*[K, V](keyType: typedesc[K], valueType: typedesc[V]): MatchTable[K, V] = 23proc initMatchTable*[K, V](keyType: typedesc[K], valueType: typedesc[V], matchLimit = 5): MatchTable[K, V] =
22 newTable[K, seq[V]]() 24 MatchTable[K, V](matchLimit: matchLimit, table: newTable[K, seq[V]]())
25
26proc len*[K, V](matchTable: MatchTable[K, V]): int =
27 matchTable.table.len
23 28
24proc matchList*[K, V](matchTable: MatchTable[K, V], pattern: K): seq[V] = 29proc matchList*[K, V](matchTable: MatchTable[K, V], pattern: K): seq[V] =
25 matchTable.getOrDefault(pattern, newSeq[V]()) 30 if matchTable.table.hasKey(pattern):
31 matchTable.table[pattern]
32 else:
33 newSeqOfCap[V](matchTable.matchLimit)
26 34
27proc addMatch*[K, V](matchTable: MatchTable[K, V], pattern: K, value: V) = 35proc addMatch*[K, V](matchTable: MatchTable[K, V], pattern: K, value: V) =
28 var matchList = matchTable.matchList(pattern) 36 var matchList = matchTable.matchList(pattern)
37 if matchList.len >= matchTable.matchLimit: matchList.del(matchList.len - 1)
29 matchList.insert(value) 38 matchList.insert(value)
30 matchTable[pattern] = matchList 39 matchTable.table[pattern] = matchList