diff options
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 |