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