aboutsummaryrefslogtreecommitdiff
path: root/src/lzss/matchtable.nim
diff options
context:
space:
mode:
Diffstat (limited to 'src/lzss/matchtable.nim')
-rw-r--r--src/lzss/matchtable.nim32
1 files changed, 15 insertions, 17 deletions
diff --git a/src/lzss/matchtable.nim b/src/lzss/matchtable.nim
index cc04f49..879f47d 100644
--- a/src/lzss/matchtable.nim
+++ b/src/lzss/matchtable.nim
@@ -15,25 +15,23 @@
15# along with this program. If not, see <https://www.gnu.org/licenses/>. 15# along with this program. If not, see <https://www.gnu.org/licenses/>.
16 16
17import tables 17import tables
18import matchring
18 19
19type MatchTable*[K, V] = ref object 20const matchGroupLength = 3
20 matchLimit: int 21const hashShift = 5
21 table: TableRef[K, seq[V]] 22const tableHeight = 0b1 shl 15
22 23
23proc initMatchTable*[K, V](keyType: typedesc[K], valueType: typedesc[V], matchLimit = 5): MatchTable[K, V] = 24type MatchTable* = object
24 MatchTable[K, V](matchLimit: matchLimit, table: newTable[K, seq[V]]()) 25 table: array[tableHeight, MatchRing]
25 26
26proc len*[K, V](matchTable: MatchTable[K, V]): int = 27proc initMatchTable*(): MatchTable =
27 matchTable.table.len 28 result = MatchTable()
28 29
29proc matchList*[K, V](matchTable: MatchTable[K, V], pattern: K): seq[V] = 30proc hash(pattern: array[matchGroupLength, uint8]): int =
30 if matchTable.table.hasKey(pattern): 31 ((pattern[0].int shl (hashShift * 2)) xor (pattern[1].int shl hashShift) xor pattern[2].int) mod tableHeight
31 matchTable.table[pattern]
32 else:
33 newSeqOfCap[V](matchTable.matchLimit)
34 32
35proc addMatch*[K, V](matchTable: MatchTable[K, V], pattern: K, value: V) = 33proc addMatch*(matchTable: var MatchTable, pattern: array[matchGroupLength, uint8], index: int) =
36 var matchList = matchTable.matchList(pattern) 34 matchTable.table[hash(pattern)].addMatch(index)
37 if matchList.len >= matchTable.matchLimit: matchList.del(matchList.len - 1) 35
38 matchList.insert(value) 36proc candidates*(matchTable: MatchTable, pattern: array[matchGroupLength, uint8]): MatchRing =
39 matchTable.table[pattern] = matchList 37 matchTable.table[hash(pattern)]