This module implements a Ternary Search Tree which is an efficient container for a sorted set|mapping of strings in the family of digital search trees. Presently, NUL bytes('\0') are not allowed in keys. I have measured it as up to 1.5x faster than CritBitTree (at a cost of a 7x memory footprint) doing things like unique **.nim lines and never more than 1.1x slower. It mostly felt easier for me to extend this variant. It is API-compatible with CritBitTree (except a longestMatch -> longest parameter rename which will be trapped by the compiler if you use kwargs). CritBitTree itself is API-compatible with the union of both HashSet and Table.
Procs
proc containsOrIncl[T](t: var Tern[T]; key: string): bool {.discardable.}
- Returns true iff t contains key or inserts into t if missing.
proc containsOrIncl[T](t: var Tern[T]; key: string; val: T): bool
- Returns true iff t contains key or does t[key]=val if missing.
proc uniquePfxPat[T](t: Tern[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. The separator character is only used if it can shrink total rendered space.
proc uniquePfxPats(x: openArray[string]; sep = "*"): seq[string] {....raises: [], tags: [], forbids: [].}
- Return unique prefixes in x assuming non-empty-string&unique x[i].
proc uniqueSfxPats(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: Tern[T]; prefix: string; longest = false): string
- Yields all keys starting with prefix.
iterator mpairsWithPrefix[T](t: var Tern[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 Tern[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: Tern[T]; prefix: string; longest = false): tuple[ key: string, val: T]
- Yields all (key, value)-pairs of t starting with prefix.
iterator valuesWithPrefix[T](t: Tern[T]; prefix: string; longest = false): T
- Yields all values of t for keys starting with prefix.