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