using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; // Doubly linked list over a pool of 16-byte nodes in native memory. // // - Node i lives at _nodes[i]; its handle is i. Index 0 is a sentinel that // closes the list into a ring: sentinel.Next is the front, sentinel.Prev is // the back, and the first/last real nodes link to 0. So "no node" == 0 == // the sentinel, and every operation is branch-free pointer surgery. // - Removed nodes go on an intrusive free list (chained through Next) and are // reused before the pool's bump index advances. The pool grows by doubling // with NativeMemory.Realloc (handles are indices, so moving is harmless). // - The native block is released by the finalizer. public sealed unsafe class DList { [StructLayout(LayoutKind.Sequential)] private struct Node { public long Value; public uint Next; public uint Prev; } private Node* _nodes; private uint _free; // head of the free list, 0 = empty private uint _used; // next never-used index private uint _cap; // capacity in nodes (including the sentinel) private int _count; private const uint InitialCap = 256; public DList() { _nodes = (Node*)NativeMemory.Alloc((nuint)InitialCap * (nuint)sizeof(Node)); _cap = InitialCap; _used = 1; _nodes[0].Value = 0; _nodes[0].Next = 0; _nodes[0].Prev = 0; } ~DList() { Node* p = _nodes; if (p != null) { _nodes = null; NativeMemory.Free(p); } } [MethodImpl(MethodImplOptions.NoInlining)] private void Grow() { ulong newCap = (ulong)_cap * 2; if (newCap > uint.MaxValue) { if (_cap == uint.MaxValue) throw new OutOfMemoryException("DList node pool exhausted"); newCap = uint.MaxValue; } _nodes = (Node*)NativeMemory.Realloc(_nodes, (nuint)newCap * (nuint)sizeof(Node)); _cap = (uint)newCap; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private uint Alloc() { uint n = _free; if (n != 0) { _free = _nodes[n].Next; } else { n = _used; if (n == _cap) Grow(); _used = n + 1; } return n; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private ulong LinkAfter(uint x, long value) { uint n = Alloc(); Node* b = _nodes; uint nx = b[x].Next; Node* nn = b + n; nn->Value = value; nn->Next = nx; nn->Prev = x; b[nx].Prev = n; b[x].Next = n; _count++; return n; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private ulong LinkBefore(uint x, long value) { uint n = Alloc(); Node* b = _nodes; uint pv = b[x].Prev; Node* nn = b + n; nn->Value = value; nn->Next = x; nn->Prev = pv; b[pv].Next = n; b[x].Prev = n; _count++; return n; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private long Unlink(uint x) { Node* b = _nodes; Node* nx = b + x; uint p = nx->Prev; uint n = nx->Next; long v = nx->Value; b[p].Next = n; b[n].Prev = p; nx->Next = _free; _free = x; _count--; return v; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public ulong PushFront(long value) => LinkAfter(0, value); [MethodImpl(MethodImplOptions.AggressiveInlining)] public ulong PushBack(long value) => LinkBefore(0, value); [MethodImpl(MethodImplOptions.AggressiveInlining)] public ulong InsertBefore(ulong node, long value) => LinkBefore((uint)node, value); [MethodImpl(MethodImplOptions.AggressiveInlining)] public ulong InsertAfter(ulong node, long value) => LinkAfter((uint)node, value); [MethodImpl(MethodImplOptions.AggressiveInlining)] public long Remove(ulong node) => Unlink((uint)node); [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool TryPopFront(out long value) { if (_count == 0) { value = 0; return false; } value = Unlink(_nodes[0].Next); return true; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool TryPopBack(out long value) { if (_count == 0) { value = 0; return false; } value = Unlink(_nodes[0].Prev); return true; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void MoveToFront(ulong node) { uint x = (uint)node; Node* b = _nodes; uint first = b[0].Next; if (first == x) return; Node* nx = b + x; uint p = nx->Prev; uint n = nx->Next; b[p].Next = n; b[n].Prev = p; // first != x, so first is still the front after unlinking x nx->Next = first; nx->Prev = 0; b[first].Prev = x; b[0].Next = x; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public ulong Front() => _nodes[0].Next; [MethodImpl(MethodImplOptions.AggressiveInlining)] public ulong Back() => _nodes[0].Prev; [MethodImpl(MethodImplOptions.AggressiveInlining)] public ulong Next(ulong node) => _nodes[(uint)node].Next; [MethodImpl(MethodImplOptions.AggressiveInlining)] public ulong Prev(ulong node) => _nodes[(uint)node].Prev; [MethodImpl(MethodImplOptions.AggressiveInlining)] public long Get(ulong node) => _nodes[(uint)node].Value; public int Count { [MethodImpl(MethodImplOptions.AggressiveInlining)] get => _count; } }