using MemoryPools.Collections.Specialized; namespace MemoryPools.Collections.Linq { internal class ReverseExprEnumerable : IPoolingEnumerable { private int _count; private PoolingList _src; public ReverseExprEnumerable Init(PoolingList src) { _src = src; _count = 0; return this; } public IPoolingEnumerator GetEnumerator() { _count++; return Pool.Get().Init(_src, this); } private void Dispose() { if(_count == 0) return; _count--; if (_count == 0) { _src?.Dispose(); Pool>.Return(_src!); _src = default!; Pool>.Return(this!); } } internal class ReverseExprEnumerator : IPoolingEnumerator { private PoolingList _src; private ReverseExprEnumerable _parent; private int _position; public ReverseExprEnumerator Init(PoolingList src, ReverseExprEnumerable parent) { _position = src.Count; _src = src; _parent = parent; return this; } public bool MoveNext() { if (_position == 0) return false; _position--; return true; } public void Reset() => _position = _src.Count; object IPoolingEnumerator.Current => Current!; public T Current => _src[_position]; public void Dispose() { _parent?.Dispose(); _parent = default!; _src = default!; _position = default!; Pool.Return(this); } } IPoolingEnumerator IPoolingEnumerable.GetEnumerator() => GetEnumerator(); } }