using System; using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; // Randomized skip list (Pugh) with p = 1/4 and a maximum height of 16 // (4^16 = 2^32 entries). // // All nodes live in one native arena addressed by 32-bit indices in 8-byte // units (index 0 is the header, which is never the target of a link, so 0 // doubles as null). Node layout, 8-byte aligned: // [0] long key // [8] long value // [16] uint next[h] (level 0 .. h-1) // padded to a multiple of 8 bytes: 16 + 8 * ceil(h / 2) bytes. // The height is not stored: remove recovers it from the predecessor vector. // Freed nodes go on per-size-class free lists (linked through next[0]). [SkipLocalsInit] public sealed unsafe class SkipList { const int MaxH = 16; const uint HeaderUnits = 2 + MaxH / 2; // key+value slots + 16 links const ulong MaxUnits = 1UL << 32; byte* _base; ulong _capUnits; uint _top; int _level; int _count; ulong _rng; readonly uint[] _free = new uint[MaxH / 2 + 1]; public SkipList() { _capUnits = 8192; _base = (byte*)NativeMemory.AllocZeroed((nuint)(_capUnits * 8)); _top = HeaderUnits; _rng = 0x2545F4914F6CDD1DUL; } ~SkipList() { if (_base != null) { NativeMemory.Free(_base); _base = null; } } public int Count => _count; public int Height() => _level; public int LevelCount(int level) { if (level < 0 || level >= _level) return 0; byte* b = _base; int c = 0; uint x = *((uint*)(b + 16) + level); while (x != 0) { c++; x = *((uint*)(b + ((nuint)x << 3) + 16) + level); } return c; } [MethodImpl(MethodImplOptions.AggressiveInlining)] int RandomHeight() { unchecked { ulong z = (_rng += 0x9E3779B97F4A7C15UL); z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9UL; z = (z ^ (z >> 27)) * 0x94D049BB133111EBUL; z ^= z >> 31; int h = 1 + (BitOperations.TrailingZeroCount(z) >> 1); return h > MaxH ? MaxH : h; } } [MethodImpl(MethodImplOptions.NoInlining)] void Grow(ulong need) { ulong cap = _capUnits; while (cap < need) cap *= 2; if (cap > MaxUnits) { if (need > MaxUnits) throw new OutOfMemoryException("skip list arena full"); cap = MaxUnits; } _base = (byte*)NativeMemory.Realloc(_base, (nuint)(cap * 8)); _capUnits = cap; } [MethodImpl(MethodImplOptions.AggressiveInlining)] uint Alloc(int h) { int cls = (h + 1) >> 1; uint idx = _free[cls]; if (idx != 0) { _free[cls] = *(uint*)(_base + ((nuint)idx << 3) + 16); return idx; } idx = _top; ulong nt = (ulong)idx + 2 + (ulong)cls; if (nt > _capUnits) Grow(nt); _top = (uint)nt; return idx; } public bool TryGet(long key, out long value) { byte* b = _base; uint x = 0, last = 0; for (int L = _level - 1; L >= 0; L--) { while (true) { uint nx = *((uint*)(b + ((nuint)x << 3) + 16) + L); if (nx == last) break; if (*(long*)(b + ((nuint)nx << 3)) >= key) { last = nx; break; } x = nx; } } if (last != 0) { long* p = (long*)(b + ((nuint)last << 3)); if (p[0] == key) { value = p[1]; return true; } } value = 0; return false; } public bool Insert(long key, long value) { uint* upd = stackalloc uint[MaxH]; byte* b = _base; uint x = 0, last = 0; int level = _level; for (int L = level - 1; L >= 0; L--) { while (true) { uint nx = *((uint*)(b + ((nuint)x << 3) + 16) + L); if (nx == last) break; if (*(long*)(b + ((nuint)nx << 3)) >= key) { last = nx; break; } x = nx; } upd[L] = x; } if (last != 0) { long* p = (long*)(b + ((nuint)last << 3)); if (p[0] == key) { p[1] = value; return false; } } int h = RandomHeight(); if (h > level) { for (int L = level; L < h; L++) upd[L] = 0; _level = h; } uint n = Alloc(h); b = _base; long* np = (long*)(b + ((nuint)n << 3)); np[0] = key; np[1] = value; uint* nn = (uint*)(np + 2); for (int L = 0; L < h; L++) { uint* pl = (uint*)(b + ((nuint)upd[L] << 3) + 16) + L; nn[L] = *pl; *pl = n; } _count++; return true; } public bool Remove(long key) { uint* upd = stackalloc uint[MaxH]; byte* b = _base; uint x = 0, last = 0; int level = _level; for (int L = level - 1; L >= 0; L--) { while (true) { uint nx = *((uint*)(b + ((nuint)x << 3) + 16) + L); if (nx == last) break; if (*(long*)(b + ((nuint)nx << 3)) >= key) { last = nx; break; } x = nx; } upd[L] = x; } if (last == 0) return false; long* p = (long*)(b + ((nuint)last << 3)); if (p[0] != key) return false; uint* ln = (uint*)(p + 2); int h = 0; while (h < level) { uint* pl = (uint*)(b + ((nuint)upd[h] << 3) + 16) + h; if (*pl != last) break; *pl = ln[h]; h++; } int cnt = --_count; if (cnt == 0) { // Empty: reset the arena so it is reused from the start. uint* hdr = (uint*)(b + 16); for (int L = 0; L < MaxH; L++) hdr[L] = 0; Array.Clear(_free); _top = HeaderUnits; _level = 0; return true; } int cls = (h + 1) >> 1; ln[0] = _free[cls]; _free[cls] = last; uint* head = (uint*)(b + 16); while (level > 0 && head[level - 1] == 0) level--; _level = level; return true; } 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; byte* b = _base; uint x = 0, last = 0; for (int L = _level - 1; L >= 0; L--) { while (true) { uint nx = *((uint*)(b + ((nuint)x << 3) + 16) + L); if (nx == last) break; if (*(long*)(b + ((nuint)nx << 3)) >= lo) { last = nx; break; } x = nx; } } int c = 0; fixed (long* ok = outKeys, ov = outVals) { while (last != 0 && c < limit) { long* p = (long*)(b + ((nuint)last << 3)); long k = p[0]; if (k > hi) break; ok[c] = k; ov[c] = p[1]; c++; last = *(uint*)(p + 2); } } return c; } }