using System; namespace MemoryPools.Collections.Linq { internal class SelectExprEnumerable : IPoolingEnumerable { private IPoolingEnumerable _src; private Func _mutator; private int _count; public SelectExprEnumerable Init(IPoolingEnumerable src, Func mutator) { _src = src; _count = 0; _mutator = mutator; return this; } public IPoolingEnumerator GetEnumerator() { _count++; return Pool.Get().Init(this, _src.GetEnumerator(), _mutator); } private void Dispose() { if (_count == 0) return; _count--; if (_count == 0) { _src = default; _count = 0; _mutator = default; Pool>.Return(this); } } internal class SelectExprEnumerator : IPoolingEnumerator { private Func _mutator; private SelectExprEnumerable _parent; private IPoolingEnumerator _src; public SelectExprEnumerator Init(SelectExprEnumerable parent, IPoolingEnumerator src, Func mutator) { _src = src; _parent = parent; _mutator = mutator; return this; } public bool MoveNext() => _src.MoveNext(); public void Reset() => _src.Reset(); object IPoolingEnumerator.Current => Current; public TR Current => _mutator( _src.Current); public void Dispose() { _parent?.Dispose(); _parent = default; _src?.Dispose(); _src = default; Pool.Return(this); } } IPoolingEnumerator IPoolingEnumerable.GetEnumerator() { return GetEnumerator(); } } }