This module provides a directly indexed set/table representation via a dense seq[T] with an auxiliary sparse direct index to accelerate searching. "direct" means the same size as the key space or "alphabet". The index says what seq index has each (unique) key. This can do any unordered set op in guaranteed unit time cost per element -- make, insert, delete, member query, iterate, union/intersect/etc. -- all with arbitrarily "aged" sets.
The catch is that the "unit" in said cost will only be small if the key space is small enough to afford allocating index space & if the working set of the index fits into low enough latency memory to not be too slow. Otherwise, other data structures like hash tables & B-trees will outperform this. Iteration is always in insertion order whenever no deletes have occurred. The "unit" is also 2 memory accesses per operation, vs. often 1 for lptabz. So, very large scale can make this guaranteed to be ~2X slower than a good average case for lptabz, all depending upon exact requirements, of course.
The earliest reference I have elaborating this approach is _An Efficient Representation for Sparse Sets by Preston Briggs & Linda Torczon from Rice in 1993 referencing an exercise in Aho, Croft & Ullman 1974. It's simple enough that the idea may date to early 1960s DB work (e.g. by Codd), maybe under another term like "direct indexing". K below must have an available conversion to int. Duplicate keys cannot be allowed for this one.
Below here is pretty standard except for the generic signaturesVars
diGrowPow2 = 0
- default growth power of 2 (unused) Source Edit
diInitialSize = 0
- default numerator (unused) Source Edit
diRobinHood = false
- default to Robin Hood (unused) Source Edit
Procs
proc containsOrIncl[K](t: var DITab[K, void]; key: K): bool {.inline.}
- Source Edit
proc depthStats[K, V](s: DITab[K, V]): tuple[m1, m2: float, mx: int]
- Source Edit
proc difference[K](s1, s2: DITab[K, void]): DITab[K, void]
- Source Edit
proc getOrDefault[K, V](t: DITab[K, V]; key: K; def = default(V)): V {.inline.}
- Source Edit
proc hasKeyOrPut[K, V](t: var DITab[K, V]; key: K; val: V): bool {.inline.}
- Source Edit
proc intersection[K](s1, s2: DITab[K, void]): DITab[K, void]
- Source Edit
proc missingOrExcl[K, V](t: var DITab[K, V]; key: K): bool
- Source Edit
proc symmetricDifference[K](s1, s2: DITab[K, void]): DITab[K, void]
- Source Edit
Templates
template editOrInit[K, V](t: var DITab[K, V]; key: K; v, body1, body2: untyped)
- Source Edit