Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ChainedList<T>
- {
- internal readonly RecycleList<Link> _list = [];
- internal readonly record struct Link(T Value, int Next);
- }
- public sealed class Chain<T>
- {
- public ChainedList<T> List { get; }
- public T Current
- {
- get => List[_head].Value;
- set => List[_head] = new(value, List[_head].Next);
- }
- internal Chain(ChainedList<T> list, int index)
- {
- List = list;
- _prev = -1;
- _head = index;
- }
- public bool MoveNext()
- {
- int next = List[_head].Next;
- _prev = _head;
- return (_head = next) > 0;
- }
- public void AddBefore(T value)
- {
- int index = List.NextIndex();
- List[_prev] = new(List[_prev].Value, index);
- List[index] = new(value, _head);
- }
- public void AddAfter(T value)
- {
- int index = NextIndex();
- List[index] = new(value, List[_head].Next);
- List[_head] = new(List[_head].Value, index);
- }
- public bool Remove()
- {
- int next = List[_head].Next;
- List[_prev] = new(List[_prev].Value, next);
- List.Recycle(_head, default);
- return (_head = next) > 0;
- }
- private int _prev;
- private int _head;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement