Skip to content

10. DataStructures

ogoun edited this page Apr 23, 2026 · 1 revision

DataStructures

Specialized data structures that don't fit neatly into any subsystem — probabilistic membership, sparse vectors, bitmap indexes. All live under ZeroLevel.DataStructures.

BloomFilter

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 inserted

Contract:

  • Contains returns false → the item was definitely not added.
  • Contains returns true → probably added (false-positives bounded by fpRate).

Good for pre-filtering expensive lookups (disk reads, DB queries).

HyperBloomBloom

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");   // ~3

Approximate — but uses a constant amount of memory regardless of stream length.

BitMapCardTable

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).

SafeBit32Vector

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.

SparceVector / SparseMatrix

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.

Choosing a structure

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

Tips

  • Bloom false-positive rate rises as you approach capacity. Size generously (2–4× expected element count) if memory allows.
  • BloomFilter is not thread-safe for concurrent Add — serialize writes, or partition by hash.
  • SparceVector preserves insertion order; don't rely on numeric order unless you sort the keys yourself.

Clone this wiki locally