using System; using System.Collections.Generic; using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; // Classic CLRS B-tree with minimum degree t = 16, nodes in native memory. // // Node layout (bytes): // 0..247 keys[31] (unused slots hold long.MaxValue so the in-node rank // count "keys < key" can scan all slots branch-free) // 248 int N (number of keys) // 252 int Leaf (1 = leaf) // 256..503 vals[31] // 512..767 kids[32] (internal nodes only; leaves are allocated 512 bytes) public unsafe sealed class BTree { const int T = 16; const int MAXK = 2 * T - 1; const int LEAF_SIZE = 512; const int INNER_SIZE = 768; const long PAD = long.MaxValue; [StructLayout(LayoutKind.Sequential)] struct Node { public fixed long Keys[31]; public int N; public int Leaf; public fixed long Vals[31]; public long Unused; public fixed long Kids[32]; } Node* root; int count; int height; // ---------------------------------------------------------------- memory readonly List chunks = new List(); byte* bumpCur; byte* bumpEnd; nuint nextChunk = 64 * 1024; Node* freeLeaf; Node* freeInner; public BTree() { } ~BTree() { foreach (IntPtr p in chunks) NativeMemory.AlignedFree((void*)p); chunks.Clear(); } [MethodImpl(MethodImplOptions.NoInlining)] void NewChunk() { nuint size = nextChunk; if (nextChunk < 8u * 1024 * 1024) nextChunk *= 2; byte* p = (byte*)NativeMemory.AlignedAlloc(size, 128); chunks.Add((IntPtr)p); bumpCur = p; bumpEnd = p + size; } Node* Alloc(bool leaf) { Node* n; if (leaf) { n = freeLeaf; if (n != null) freeLeaf = *(Node**)n; else { if (bumpEnd - bumpCur < LEAF_SIZE) NewChunk(); n = (Node*)bumpCur; bumpCur += LEAF_SIZE; } } else { n = freeInner; if (n != null) freeInner = *(Node**)n; else { if (bumpEnd - bumpCur < INNER_SIZE) NewChunk(); n = (Node*)bumpCur; bumpCur += INNER_SIZE; } } new Span(n->Keys, MAXK).Fill(PAD); n->N = 0; n->Leaf = leaf ? 1 : 0; return n; } void Free(Node* n) { if (n->Leaf != 0) { *(Node**)n = freeLeaf; freeLeaf = n; } else { *(Node**)n = freeInner; freeInner = n; } } // --------------------------------------------------------------- helpers [MethodImpl(MethodImplOptions.AggressiveInlining)] static Node** Kids(Node* n) => (Node**)n->Kids; [MethodImpl(MethodImplOptions.AggressiveInlining)] static void Move(void* src, void* dst, int n) { long bytes = (long)n * 8; Buffer.MemoryCopy(src, dst, bytes, bytes); } [MethodImpl(MethodImplOptions.AggressiveInlining)] static Vector Ld(long* p) => Unsafe.ReadUnaligned>(p); // Number of keys in the node strictly less than `key`. [MethodImpl(MethodImplOptions.AggressiveInlining)] static int Rank(Node* x, long key) { long* k = x->Keys; if (Vector.IsHardwareAccelerated && Vector.Count == 2) { Vector kv = new Vector(key); Vector a = Vector.LessThan(Ld(k + 0), kv) + Vector.LessThan(Ld(k + 2), kv); Vector b = Vector.LessThan(Ld(k + 4), kv) + Vector.LessThan(Ld(k + 6), kv); Vector c = Vector.LessThan(Ld(k + 8), kv) + Vector.LessThan(Ld(k + 10), kv); Vector d = Vector.LessThan(Ld(k + 12), kv) + Vector.LessThan(Ld(k + 14), kv); Vector e = Vector.LessThan(Ld(k + 16), kv) + Vector.LessThan(Ld(k + 18), kv); Vector f = Vector.LessThan(Ld(k + 20), kv) + Vector.LessThan(Ld(k + 22), kv); Vector g = Vector.LessThan(Ld(k + 24), kv) + Vector.LessThan(Ld(k + 26), kv); Vector h = Vector.LessThan(Ld(k + 28), kv); Vector s = ((a + b) + (c + d)) + ((e + f) + (g + h)); return (int)(-Vector.Sum(s)) + (k[30] < key ? 1 : 0); } int r = 0; for (int j = 0; j < MAXK; j++) r += k[j] < key ? 1 : 0; return r; } // ------------------------------------------------------------ public API public int Count => count; public int Height() => height; public bool TryGet(long key, out long value) { Node* x = root; while (x != null) { int i = Rank(x, key); if (i < x->N && x->Keys[i] == key) { value = x->Vals[i]; return true; } if (x->Leaf != 0) break; x = Kids(x)[i]; } value = 0; return false; } public bool Insert(long key, long value) { Node* x = root; if (x == null) { x = Alloc(true); x->Keys[0] = key; x->Vals[0] = value; x->N = 1; root = x; height = 1; count = 1; return true; } if (x->N == MAXK) { Node* nr = Alloc(false); Kids(nr)[0] = x; Split(nr, 0, x); root = nr; height++; x = nr; } while (true) { int i = Rank(x, key); int n = x->N; if (i < n && x->Keys[i] == key) { x->Vals[i] = value; return false; } if (x->Leaf != 0) { long* k = x->Keys; long* v = x->Vals; int m = n - i; if (m > 0) { Move(k + i, k + i + 1, m); Move(v + i, v + i + 1, m); } k[i] = key; v[i] = value; x->N = n + 1; count++; return true; } Node** xc = Kids(x); Node* c = xc[i]; if (c->N == MAXK) { Split(x, i, c); long mk = x->Keys[i]; if (key == mk) { x->Vals[i] = value; return false; } if (key > mk) c = xc[i + 1]; } x = c; } } // Split full child c = kids(x)[i]; x is not full. void Split(Node* x, int i, Node* c) { Node* z = Alloc(c->Leaf != 0); Move(c->Keys + T, z->Keys, T - 1); Move(c->Vals + T, z->Vals, T - 1); if (c->Leaf == 0) Move(Kids(c) + T, Kids(z), T); z->N = T - 1; long mk = c->Keys[T - 1], mv = c->Vals[T - 1]; new Span(c->Keys + (T - 1), MAXK - (T - 1)).Fill(PAD); c->N = T - 1; int xn = x->N; Node** xc = Kids(x); int m = xn - i; if (m > 0) { Move(x->Keys + i, x->Keys + i + 1, m); Move(x->Vals + i, x->Vals + i + 1, m); Move(xc + i + 1, xc + i + 2, m); } x->Keys[i] = mk; x->Vals[i] = mv; xc[i + 1] = z; x->N = xn + 1; } public bool Remove(long key) { Node* x = root; if (x == null) return false; while (true) { int n = x->N; int i = Rank(x, key); bool found = i < n && x->Keys[i] == key; if (x->Leaf != 0) { if (!found) return false; long* k = x->Keys; long* v = x->Vals; int m = n - 1 - i; if (m > 0) { Move(k + i + 1, k + i, m); Move(v + i + 1, v + i, m); } k[n - 1] = PAD; x->N = n - 1; count--; if (n == 1) { // only the root leaf can get here with one key Free(x); root = null; height = 0; } return true; } Node** xc = Kids(x); if (found) { Node* y = xc[i]; if (y->N >= T) { RemoveMax(y, x->Keys + i, x->Vals + i); count--; return true; } Node* z = xc[i + 1]; if (z->N >= T) { RemoveMin(z, x->Keys + i, x->Vals + i); count--; return true; } Merge(x, i); if (x->N == 0) { root = y; Free(x); height--; } x = y; continue; } Node* ch = xc[i]; if (ch->N < T) { ch = FixChild(x, i); if (x->N == 0) { root = ch; Free(x); height--; } } x = ch; } } // Remove the maximum entry of the subtree at y (y has >= t keys). void RemoveMax(Node* y, long* kd, long* vd) { while (y->Leaf == 0) { int yn = y->N; Node* c = Kids(y)[yn]; if (c->N < T) c = FixChild(y, yn); y = c; } int n = y->N - 1; *kd = y->Keys[n]; *vd = y->Vals[n]; y->Keys[n] = PAD; y->N = n; } // Remove the minimum entry of the subtree at z (z has >= t keys). void RemoveMin(Node* z, long* kd, long* vd) { while (z->Leaf == 0) { Node* c = Kids(z)[0]; if (c->N < T) c = FixChild(z, 0); z = c; } int n = z->N; long* k = z->Keys; long* v = z->Vals; *kd = k[0]; *vd = v[0]; Move(k + 1, k, n - 1); Move(v + 1, v, n - 1); k[n - 1] = PAD; z->N = n - 1; } // Child i of x has t-1 keys; give it at least t keys by borrowing from a // sibling or merging. Returns the node that now covers child i's range. Node* FixChild(Node* x, int i) { Node** xc = Kids(x); Node* c = xc[i]; int cn = c->N; if (i > 0) { Node* l = xc[i - 1]; int ln = l->N; if (ln >= T) { Move(c->Keys, c->Keys + 1, cn); Move(c->Vals, c->Vals + 1, cn); c->Keys[0] = x->Keys[i - 1]; c->Vals[0] = x->Vals[i - 1]; x->Keys[i - 1] = l->Keys[ln - 1]; x->Vals[i - 1] = l->Vals[ln - 1]; if (c->Leaf == 0) { Node** cc = Kids(c); Move(cc, cc + 1, cn + 1); cc[0] = Kids(l)[ln]; } l->Keys[ln - 1] = PAD; l->N = ln - 1; c->N = cn + 1; return c; } } if (i < x->N) { Node* r = xc[i + 1]; int rn = r->N; if (rn >= T) { c->Keys[cn] = x->Keys[i]; c->Vals[cn] = x->Vals[i]; x->Keys[i] = r->Keys[0]; x->Vals[i] = r->Vals[0]; if (c->Leaf == 0) { Node** rc = Kids(r); Kids(c)[cn + 1] = rc[0]; Move(rc + 1, rc, rn); } Move(r->Keys + 1, r->Keys, rn - 1); Move(r->Vals + 1, r->Vals, rn - 1); r->Keys[rn - 1] = PAD; r->N = rn - 1; c->N = cn + 1; return c; } Merge(x, i); return c; } Node* left = xc[i - 1]; Merge(x, i - 1); return left; } // Merge kids(x)[i], key i of x, and kids(x)[i+1] into kids(x)[i]. void Merge(Node* x, int i) { Node** xc = Kids(x); Node* l = xc[i]; Node* r = xc[i + 1]; int ln = l->N, rn = r->N, xn = x->N; l->Keys[ln] = x->Keys[i]; l->Vals[ln] = x->Vals[i]; Move(r->Keys, l->Keys + ln + 1, rn); Move(r->Vals, l->Vals + ln + 1, rn); if (l->Leaf == 0) Move(Kids(r), Kids(l) + ln + 1, rn + 1); l->N = ln + rn + 1; int m = xn - 1 - i; if (m > 0) { Move(x->Keys + i + 1, x->Keys + i, m); Move(x->Vals + i + 1, x->Vals + i, m); Move(xc + i + 2, xc + i + 1, m); } x->Keys[xn - 1] = PAD; x->N = xn - 1; Free(r); } public int Range(long lo, long hi, long[] outKeys, long[] outVals, int limit) { if (lo > hi || limit <= 0 || root == null) return 0; int pos = 0; fixed (long* ok = outKeys, ov = outVals) { RangeRec(root, lo, hi, ok, ov, ref pos, limit); } return pos; } // Returns false once the scan is finished (key > hi seen or limit reached). static bool RangeRec(Node* x, long lo, long hi, long* ok, long* ov, ref int pos, int limit) { int n = x->N; int i = lo == long.MinValue ? 0 : Rank(x, lo); long* k = x->Keys; long* v = x->Vals; if (x->Leaf != 0) { if (i >= n) return true; if (k[n - 1] <= hi) { int cnt = n - i; int room = limit - pos; if (cnt > room) cnt = room; Move(k + i, ok + pos, cnt); Move(v + i, ov + pos, cnt); pos += cnt; return pos < limit; } while (k[i] <= hi) { ok[pos] = k[i]; ov[pos] = v[i]; if (++pos == limit) return false; i++; } return false; } Node** xc = Kids(x); if (!RangeRec(xc[i], lo, hi, ok, ov, ref pos, limit)) return false; for (; i < n; i++) { if (k[i] > hi) return false; ok[pos] = k[i]; ov[pos] = v[i]; if (++pos == limit) return false; if (!RangeRec(xc[i + 1], long.MinValue, hi, ok, ov, ref pos, limit)) return false; } return true; } }