Repository navigation
10. DataStructures
Specialized data structures that don't fit neatly into any subsystem — probabilistic membership, sparse vectors, bitmap indexes. All live under ZeroLevel.DataStructures.
Classical Bloom filter with Murmur3 + xxHash hashing, configurable false-positive rate.
using ZeroLevel.DataStructures;
var bf = new BloomFilter(expectedCapacity: 1_000_000, fpRate: 0.01);
bf.Add("hello");
bf.Add("world");
bool maybe = bf.Contains("hello"); // true
bool no = bf.Contains("xyzzy"); // false — definitely not insertedContract:
-
Containsreturnsfalse→ the item was definitely not added. -
Containsreturnstrue→ probably added (false-positives bounded byfpRate).
Good for pre-filtering expensive lookups (disk reads, DB queries).
A layered Bloom variant that tracks approximate counts ("seen N times") instead of binary presence — handy for trend detection and deduplication over streams:
var hbb = new HyperBloomBloom(levels: 4, capacity: 1_000_000);
hbb.Add("click");
hbb.Add("click");
hbb.Add("click");
int approxCount = hbb.EstimateCount("click"); // ~3Approximate — but uses a constant amount of memory regardless of stream length.
A concurrent bitmap-backed card table: every slot in a fixed-size grid can be set independently by multiple threads. Useful for "track-changed-regions-in-GC-style" bookkeeping, dirty-page tracking, presence maps.
var cards = new BitMapCardTable(size: 1 << 16);
cards.Set(42);
bool on = cards.Get(42); // true
cards.Clear(42);Operations are lock-free (Interlocked.Or / And over 32-bit words).
A thread-safe 32-bit-bucketed bit vector that scales to tens of millions of bits without per-bit locks.
var v = new SafeBit32Vector(capacity: 10_000_000);
v.Set(12345);
v.Clear(12345);
bool b = v.Get(12345);Same role as System.Collections.BitArray, but with Interlocked primitives instead of exterior locking.
Sparse containers for numeric workloads where most entries are zero:
var vec = new SparceVector<double>();
vec[42] = 3.14;
vec[1_000_000] = -1.0;
foreach (var (index, value) in vec)
Console.WriteLine($"{index} = {value}");SparseMatrix<T> is the 2-D analogue, indexed by (row, col) pairs. Iteration only visits set cells.
| Need | Pick |
|---|---|
| "Did I already see this URL?" (memory-bounded, approximate) | BloomFilter |
| "How many times did I see it?" (approximate, streaming) | HyperBloomBloom |
| Lock-free "region dirty" bookkeeping | BitMapCardTable |
| Exact membership over 10⁷+ integer ids | SafeBit32Vector |
| Numeric vector with 99% zero entries | SparceVector |
| Adjacency matrix / feature grid mostly empty | SparseMatrix |
- Bloom false-positive rate rises as you approach capacity. Size generously (2–4× expected element count) if memory allows.
-
BloomFilteris not thread-safe for concurrentAdd— serialize writes, or partition by hash. -
SparceVectorpreserves insertion order; don't rely on numeric order unless you sort the keys yourself.
Application basics
Data & serialization
Networking
Text & search