aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpacien2018-12-02 18:09:22 +0100
committerpacien2018-12-02 18:09:22 +0100
commit200cb18aafa7f62fdca37ae8952b5e9c5bb3f25f (patch)
tree4ac6cecfe97df7204c4d689fafd5ef8a3c0409bf
parent31ecb9abcdfea2e2f559a7fe15d64458e5c0a901 (diff)
downloadgziplike-200cb18aafa7f62fdca37ae8952b5e9c5bb3f25f.tar.gz
avoid useless allocation and limit match seq
-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