namespace KumiScript.Common { public class FastLinkedList { ListNode? _first; ListNode? _last; uint _length; public FastLinkedList() { _length = 0; } public void InsertLast(T addend) { ListNode n = new ListNode(addend); if (_length == 0) { _first = n; _last = n; _length = 1; return; } _last!.SetNext(n); n.SetPrev(_last); _last = n; _length++; return; } public void InsertFirst(T addend) { ListNode n = new ListNode(addend); if (_length == 0) { _first = n; _last = n; _length = 1; return; } _first!.SetPrev(n); n.SetNext(_first); _first = n; _length++; return; } private ListNode? FindNth(uint index) { if (index > _length - 1) return null; if (index - 1 == _length) return _last; ListNode? ni = _first; if (ni is null) return null; for (uint i = 0; i < index; i++) { ni = ni!.GetNext(); } return ni; } public void InsertAfter(uint index, T value) { ListNode nn = new ListNode(value); ListNode? ni = FindNth(index); } public T? Find(IEqualityComparer eq, T other) { ListNode? n = _first; if (n is null) return default; do { T nv = n.GetValue(); if (eq.Equals(nv, other)) { return nv; } n = n.GetNext(); } while (n is not null); return default; } public T? GetFirst() { if (_first is null) return default; return _first.GetValue(); } public T? GetLast() { if (_last is null) return default; return _last.GetValue(); } public uint GetLength() { return _length; } } internal class ListNode { ListNode? _next; ListNode? _prev; T _value; internal ListNode(T value) { _value = value; } internal T GetValue() { return _value; } internal void SetValue(T value) { _value = value; return; } internal void SetNext(ListNode next) { _next = next; } internal void SetPrev(ListNode prev) { _prev = prev; } internal bool HasNext() { return _next is not null; } internal bool HasPrev() { return _prev is not null; } internal ListNode? GetNext() { return _next; } internal ListNode? GetPrev() { return _prev; } } }