diff options
author | pacien | 2018-12-02 18:09:22 +0100 |
---|---|---|
committer | pacien | 2018-12-02 18:09:22 +0100 |
commit | 200cb18aafa7f62fdca37ae8952b5e9c5bb3f25f (patch) | |
tree | 4ac6cecfe97df7204c4d689fafd5ef8a3c0409bf /src | |
parent | 31ecb9abcdfea2e2f559a7fe15d64458e5c0a901 (diff) | |
download | gziplike-200cb18aafa7f62fdca37ae8952b5e9c5bb3f25f.tar.gz |
avoid useless allocation and limit match seq
Diffstat (limited to 'src')
-rw-r--r-- | src/lzss/matchtable.nim | 19 |
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 | ||
17 | import tables | 17 | import tables |
18 | 18 | ||
19 | type MatchTable*[K, V] = TableRef[K, seq[V]] | 19 | type MatchTable*[K, V] = ref object |
20 | matchLimit: int | ||
21 | table: TableRef[K, seq[V]] | ||
20 | 22 | ||
21 | proc initMatchTable*[K, V](keyType: typedesc[K], valueType: typedesc[V]): MatchTable[K, V] = | 23 | proc 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 | |||
26 | proc len*[K, V](matchTable: MatchTable[K, V]): int = | ||
27 | matchTable.table.len | ||
23 | 28 | ||
24 | proc matchList*[K, V](matchTable: MatchTable[K, V], pattern: K): seq[V] = | 29 | proc 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 | ||
27 | proc addMatch*[K, V](matchTable: MatchTable[K, V], pattern: K, value: V) = | 35 | proc 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 |