using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; // One tree node: 32 bytes, so nodes never straddle a cache line. // Links are 32-bit indices into a single native node pool; index 0 is the // CLRS nil sentinel (always black, children always 0). [StructLayout(LayoutKind.Sequential, Size = 32)] internal struct RBNode { public long Key; // 0 public int Left; // 8 public int Right; // 12 public long Value; // 16 public int Parent; // 24 public int Red; // 28 (0 = black, 1 = red) } public sealed unsafe class RBTree { private RBNode* _n; // node pool (index 0 = nil sentinel) private int _cap; // pool capacity in nodes private int _used; // next never-used index private int _free; // head of free list (linked through Left), 0 = empty private int _root; private int _count; public RBTree() { _cap = 64; _n = (RBNode*)NativeMemory.Alloc((nuint)_cap * (nuint)sizeof(RBNode)); _n[0] = default; _used = 1; } ~RBTree() { if (_n != null) { NativeMemory.Free(_n); _n = null; } } public int Count => _count; [MethodImpl(MethodImplOptions.NoInlining)] private void Grow() { long newCap = (long)_cap * 2; if (newCap > int.MaxValue) newCap = int.MaxValue; _n = (RBNode*)NativeMemory.Realloc(_n, (nuint)newCap * (nuint)sizeof(RBNode)); _cap = (int)newCap; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool TryGet(long key, out long value) { RBNode* N = _n; int x = _root; while (x != 0) { RBNode* p = N + x; long k = p->Key; if (key == k) { value = p->Value; return true; } x = (&p->Left)[key > k ? 1 : 0]; } value = 0; return false; } public bool Insert(long key, long value) { RBNode* N = _n; int x = _root, y = 0; int dir = 0; while (x != 0) { RBNode* p = N + x; long k = p->Key; if (key == k) { p->Value = value; return false; } y = x; dir = key > k ? 1 : 0; x = (&p->Left)[dir]; } int z; if (_free != 0) { z = _free; _free = N[z].Left; } else { if (_used == _cap) { Grow(); N = _n; } z = _used++; } RBNode* zp = N + z; zp->Key = key; zp->Value = value; zp->Left = 0; zp->Right = 0; zp->Parent = y; zp->Red = 1; _count++; if (y == 0) { _root = z; zp->Red = 0; return true; } (&N[y].Left)[dir] = z; if (N[y].Red != 0) InsertFixup(N, z); return true; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private void RotateLeft(RBNode* N, int x) { RBNode* xp = N + x; int y = xp->Right; RBNode* yp = N + y; int b = yp->Left; xp->Right = b; if (b != 0) N[b].Parent = x; int p = xp->Parent; yp->Parent = p; if (p == 0) _root = y; else if (N[p].Left == x) N[p].Left = y; else N[p].Right = y; yp->Left = x; xp->Parent = y; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private void RotateRight(RBNode* N, int x) { RBNode* xp = N + x; int y = xp->Left; RBNode* yp = N + y; int b = yp->Right; xp->Left = b; if (b != 0) N[b].Parent = x; int p = xp->Parent; yp->Parent = p; if (p == 0) _root = y; else if (N[p].Right == x) N[p].Right = y; else N[p].Left = y; yp->Right = x; xp->Parent = y; } private void InsertFixup(RBNode* N, int z) { while (true) { int p = N[z].Parent; if (N[p].Red == 0) break; // also covers p == 0 (sentinel is black) int g = N[p].Parent; // p is red, so it is not the root if (p == N[g].Left) { int u = N[g].Right; if (N[u].Red != 0) { N[p].Red = 0; N[u].Red = 0; N[g].Red = 1; z = g; continue; } if (z == N[p].Right) { RotateLeft(N, p); p = z; } N[p].Red = 0; N[g].Red = 1; RotateRight(N, g); break; } else { int u = N[g].Left; if (N[u].Red != 0) { N[p].Red = 0; N[u].Red = 0; N[g].Red = 1; z = g; continue; } if (z == N[p].Left) { RotateRight(N, p); p = z; } N[p].Red = 0; N[g].Red = 1; RotateLeft(N, g); break; } } N[_root].Red = 0; } public bool Remove(long key) { RBNode* N = _n; int z = _root; RBNode* zp = null; while (z != 0) { zp = N + z; long k = zp->Key; if (key == k) break; z = (&zp->Left)[key > k ? 1 : 0]; } if (z == 0) return false; if (zp->Left != 0 && zp->Right != 0) { // Two children: move the successor's entry here and unlink the successor. int y = zp->Right; int l; while ((l = N[y].Left) != 0) y = l; RBNode* yp = N + y; zp->Key = yp->Key; zp->Value = yp->Value; z = y; zp = yp; } int x = zp->Left != 0 ? zp->Left : zp->Right; int par = zp->Parent; N[x].Parent = par; // x may be the sentinel; its parent is used by the fixup if (par == 0) _root = x; else if (N[par].Left == z) N[par].Left = x; else N[par].Right = x; if (zp->Red == 0) { if (N[x].Red != 0) N[x].Red = 0; else DeleteFixup(N, x); } zp->Left = _free; _free = z; _count--; if (_count == 0) { // Reset the pool so a refilled tree gets fresh, sequential slots. _free = 0; _used = 1; _root = 0; N[0].Parent = 0; } return true; } private void DeleteFixup(RBNode* N, int x) { while (x != _root && N[x].Red == 0) { int p = N[x].Parent; if (x == N[p].Left) { int w = N[p].Right; if (N[w].Red != 0) { N[w].Red = 0; N[p].Red = 1; RotateLeft(N, p); w = N[p].Right; } int wl = N[w].Left, wr = N[w].Right; if (N[wl].Red == 0 && N[wr].Red == 0) { N[w].Red = 1; x = p; } else { if (N[wr].Red == 0) { N[wl].Red = 0; N[w].Red = 1; RotateRight(N, w); w = N[p].Right; } N[w].Red = N[p].Red; N[p].Red = 0; N[N[w].Right].Red = 0; RotateLeft(N, p); x = _root; break; } } else { int w = N[p].Left; if (N[w].Red != 0) { N[w].Red = 0; N[p].Red = 1; RotateRight(N, p); w = N[p].Left; } int wl = N[w].Left, wr = N[w].Right; if (N[wl].Red == 0 && N[wr].Red == 0) { N[w].Red = 1; x = p; } else { if (N[wl].Red == 0) { N[wr].Red = 0; N[w].Red = 1; RotateLeft(N, w); w = N[p].Left; } N[w].Red = N[p].Red; N[p].Red = 0; N[N[w].Left].Red = 0; RotateRight(N, p); x = _root; break; } } } N[x].Red = 0; } public int Range(long lo, long hi, long[] outKeys, long[] outVals, int limit) { if (lo > hi || limit <= 0) return 0; if (limit > outKeys.Length) limit = outKeys.Length; if (limit > outVals.Length) limit = outVals.Length; RBNode* N = _n; // Lowest node with key >= lo. int x = _root, cand = 0; while (x != 0) { RBNode* p = N + x; if (p->Key >= lo) { cand = x; x = p->Left; } else x = p->Right; } ref long ok = ref MemoryMarshal.GetArrayDataReference(outKeys); ref long ov = ref MemoryMarshal.GetArrayDataReference(outVals); int c = 0; x = cand; while (x != 0) { RBNode* p = N + x; long k = p->Key; if (k > hi) break; Unsafe.Add(ref ok, c) = k; Unsafe.Add(ref ov, c) = p->Value; if (++c == limit) break; // In-order successor. int r = p->Right; if (r != 0) { int l; while ((l = N[r].Left) != 0) r = l; x = r; } else { int y = p->Parent; while (y != 0 && N[y].Right == x) { x = y; y = N[y].Parent; } x = y; } } return c; } public int Height() => HeightOf(_root); private int HeightOf(int x) { if (x == 0) return 0; int l = HeightOf(_n[x].Left); int r = HeightOf(_n[x].Right); return 1 + (l > r ? l : r); } }