This module implements a Trie, a container for a set|mapping of strings in the digital search tree family. It is drop-in compatible-ish with CritBitTree itself compatible with both HashSet[string] & Table[string,*]. It was easier for me to extend this with match&nearLev than CritBitTree.
Procs
proc containsOrIncl[T](t: var Trie[T]; key: string): bool {.discardable, inline.}
- Returns true iff t contains key or inserts into t if missing.
proc containsOrIncl[T](t: var Trie[T]; key: string; val: T): bool {.inline.}
- Returns true iff t contains key or does t[key]=val if missing.
proc missingOrExcl[T](t: var Trie[T]; key: string): bool {.inline.}
- t.excl(key) if present in t and return true else just return false.
proc simplifyPattern(pat: string; a1 = '?'; aN = '*'): string {....raises: [], tags: [], forbids: [].}
- Map "(>1 of ?)" --> "?" or "(>1 '?')" --> just "?".
proc uniquePfxPat(x: openArray[string]; sep = "*"): seq[string] {....raises: [], tags: [], forbids: [].}
- Return unique prefixes in x assuming non-empty-string&unique x[i].
proc uniquePfxPat[T](t: Trie[T]; key: string; sep = "*"): string
- Return shortest unique prefix pattern for key known to be in t. Unlike a shortest unique prefix string, this is well-defined for all sets. sep is only used if it can shrink total rendered space.
proc uniqueSfxPat(x: openArray[string]; sep = "*"): seq[string] {....raises: [], tags: [], forbids: [].}
- Return unique suffixes in x assuming non-empty-string&unique x[i].
Iterators
iterator keysWithPrefix[T](t: Trie[T]; prefix: string; longest = false): string
- Yields all keys starting with prefix.
iterator mpairsWithPrefix[T](t: var Trie[T]; prefix: string; longest = false): tuple[ key: string, val: var T]
- Yields all (key, value)-pairs of t starting with prefix. The yielded values can be modified.
iterator mvaluesWithPrefix[T](t: var Trie[T]; prefix: string; longest = false): var T
- Yields all values of t for keys starting with prefix. The values can be modified.
iterator pairsWithPrefix[T](t: Trie[T]; prefix: string; longest = false): tuple[ key: string, val: T]
- Yields all (key, value)-pairs of t starting with prefix.
iterator valuesWithPrefix[T](t: Trie[T]; prefix: string; longest = false): T
- Yields all values of t for keys starting with prefix.