using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; public sealed unsafe class DList { private const uint InitialCapacity = 64; private const uint FastGrowthThreshold = 4 * 1024 * 1024; // 64 MB of 16-byte nodes [StructLayout(LayoutKind.Sequential)] private struct Node { public uint Next; public uint Prev; public long Value; } private Node* _nodes; private uint _capacity; private uint _allocated; private uint _freeHead; private int _count; public DList() { Node* nodes = (Node*)NativeMemory.Alloc((nuint)InitialCapacity * (nuint)sizeof(Node)); nodes[0].Next = 0; nodes[0].Prev = 0; nodes[0].Value = 0; _nodes = nodes; _capacity = InitialCapacity; _allocated = 1; _freeHead = 0; _count = 0; } ~DList() { Node* nodes = _nodes; if (nodes != null) { _nodes = null; NativeMemory.Free(nodes); } } public int Count { [MethodImpl(MethodImplOptions.AggressiveInlining)] get => _count; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private uint AllocNode() { uint idx = _freeHead; if (idx != 0) { _freeHead = _nodes[idx].Next; return idx; } idx = _allocated; if (idx < _capacity) { _allocated = idx + 1; return idx; } return GrowAndAlloc(); } [MethodImpl(MethodImplOptions.NoInlining)] private uint GrowAndAlloc() { uint oldCap = _capacity; uint newCap = oldCap < FastGrowthThreshold ? (oldCap << 2) : (oldCap << 1); if (newCap <= oldCap) { newCap = int.MaxValue; if (newCap <= oldCap) { throw new OutOfMemoryException(); } } _nodes = (Node*)NativeMemory.Realloc(_nodes, (nuint)newCap * (nuint)sizeof(Node)); _capacity = newCap; uint idx = _allocated; _allocated = idx + 1; return idx; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public ulong PushFront(long value) { uint idx = AllocNode(); Node* nodes = _nodes; uint first = nodes[0].Next; nodes[first].Prev = idx; nodes[0].Next = idx; Node* p = nodes + idx; p->Next = first; p->Prev = 0; p->Value = value; _count++; return idx; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public ulong PushBack(long value) { uint idx = AllocNode(); Node* nodes = _nodes; uint last = nodes[0].Prev; nodes[last].Next = idx; nodes[0].Prev = idx; Node* p = nodes + idx; p->Next = 0; p->Prev = last; p->Value = value; _count++; return idx; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public ulong InsertBefore(ulong node, long value) { uint idx = AllocNode(); Node* nodes = _nodes; Node* target = nodes + node; uint prev = target->Prev; nodes[prev].Next = idx; target->Prev = idx; Node* p = nodes + idx; p->Next = (uint)node; p->Prev = prev; p->Value = value; _count++; return idx; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public ulong InsertAfter(ulong node, long value) { uint idx = AllocNode(); Node* nodes = _nodes; Node* target = nodes + node; uint next = target->Next; nodes[next].Prev = idx; target->Next = idx; Node* p = nodes + idx; p->Next = next; p->Prev = (uint)node; p->Value = value; _count++; return idx; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public long Remove(ulong node) { Node* nodes = _nodes; Node* p = nodes + node; uint next = p->Next; uint prev = p->Prev; long value = p->Value; nodes[prev].Next = next; nodes[next].Prev = prev; p->Next = _freeHead; _freeHead = (uint)node; _count--; return value; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool TryPopFront(out long value) { Node* nodes = _nodes; uint first = nodes[0].Next; if (first == 0) { value = 0; return false; } Node* p = nodes + first; uint next = p->Next; value = p->Value; nodes[0].Next = next; nodes[next].Prev = 0; p->Next = _freeHead; _freeHead = first; _count--; return true; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool TryPopBack(out long value) { Node* nodes = _nodes; uint last = nodes[0].Prev; if (last == 0) { value = 0; return false; } Node* p = nodes + last; uint prev = p->Prev; value = p->Value; nodes[0].Prev = prev; nodes[prev].Next = 0; p->Next = _freeHead; _freeHead = last; _count--; return true; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void MoveToFront(ulong node) { Node* nodes = _nodes; uint first = nodes[0].Next; if (node == first) { return; } Node* p = nodes + node; uint prev = p->Prev; uint next = p->Next; nodes[prev].Next = next; nodes[next].Prev = prev; uint n = (uint)node; p->Prev = 0; p->Next = first; nodes[first].Prev = n; nodes[0].Next = n; } [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[node].Next; [MethodImpl(MethodImplOptions.AggressiveInlining)] public ulong Prev(ulong node) => _nodes[node].Prev; [MethodImpl(MethodImplOptions.AggressiveInlining)] public long Get(ulong node) => _nodes[node].Value; }