using System; namespace MemoryPools.Collections.Linq { internal class WhereExprEnumerable : IPoolingEnumerable { private int _count; private IPoolingEnumerable _src; private Func _condition; public WhereExprEnumerable Init(IPoolingEnumerable src, Func condition) { _count = 0; _src = src; _condition = condition; return this; } public IPoolingEnumerator GetEnumerator() { _count++; return Pool.Get().Init(_src.GetEnumerator(), this, _condition); } private void Dispose() { if (_count == 0) return; _count--; if (_count == 0) { _src = default!; _condition = default!; Pool>.Return(this); } } internal class WhereExprEnumerator : IPoolingEnumerator { private Func _mutator; private IPoolingEnumerator _src; private WhereExprEnumerable _parent; public WhereExprEnumerator Init(IPoolingEnumerator src, WhereExprEnumerable parent, Func mutator) { _src = src; _mutator = mutator; _parent = parent; return this; } public bool MoveNext() { do { var next = _src.MoveNext(); if (!next) return false; } while (!_mutator(_src.Current)); return true; } public void Reset() { _src.Reset(); } object IPoolingEnumerator.Current => Current!; public T Current => _src.Current; public void Dispose() { _parent?.Dispose(); _parent = default!; _src?.Dispose(); _src = default!; Pool.Return(this); } } IPoolingEnumerator IPoolingEnumerable.GetEnumerator() { return GetEnumerator(); } } }