using System.Collections; using System.Diagnostics.Contracts; namespace Recyclable.Collections { public partial class RecyclableLongList { public struct Enumerator : IEnumerator { #nullable disable private static readonly T _default = default; #nullable restore private int _currentBlockIndex; private int _currentItemIndex; private readonly RecyclableLongList _list; #nullable disable private T _current; #nullable restore [Pure] public readonly T Current => _current; public bool MoveNext() { var list = _list; if (_currentBlockIndex <= list._lastBlockWithData && _currentItemIndex <= (_currentBlockIndex == list._lastBlockWithData && list._nextItemIndex > 0 ? list._nextItemIndex - 1: list._blockSizeMinus1)) { _current = list._memoryBlocks[_currentBlockIndex][_currentItemIndex]; if (_currentItemIndex + 1 < list._blockSize) { _currentItemIndex++; } else { _currentBlockIndex++; _currentItemIndex = 0; } return true; } // Move to the next item if we're within block boundaries, otherwise report end of collection. return false; } #nullable disable [Pure] readonly object IEnumerator.Current => _current; #nullable restore public Enumerator(RecyclableLongList list) { _current = _default; _currentBlockIndex = 0; _currentItemIndex = 0; _list = list; } public void Reset() { if (_needsClearing) { _current = _default; } _currentBlockIndex = 0; _currentItemIndex = 0; } public void Dispose() { if (_needsClearing) { _current = _default; } } } } }