using System.Collections.Generic; using MemoryPools.Collections.Specialized; namespace MemoryPools.Collections.Linq { internal class ExceptExprEnumerable : IPoolingEnumerable { private int _count; private IPoolingEnumerable _src; private IEqualityComparer _comparer; private PoolingDictionary _except; public ExceptExprEnumerable Init(IPoolingEnumerable src, PoolingDictionary except, IEqualityComparer comparer = default) { _src = src; _except = except; _comparer = comparer; _count = 0; return this; } public IPoolingEnumerator GetEnumerator() { _count++; return Pool.Get().Init(this, _src.GetEnumerator()); } private void Dispose() { if(_count == 0) return; _count--; if (_count == 0) { _src = default; _except?.Dispose(); Pool>.Return(_except); _except = default; Pool>.Return(this); } } internal class ExceptExprEnumerator : IPoolingEnumerator { private ExceptExprEnumerable _parent; private IPoolingEnumerator _src; public ExceptExprEnumerator Init(ExceptExprEnumerable parent, IPoolingEnumerator src) { _src = src; _parent = parent; return this; } public bool MoveNext() { while (_src.MoveNext()) { if(_parent._except.ContainsKey(_src.Current)) continue; return true; } return false; } public void Reset() => _src.Reset(); object IPoolingEnumerator.Current => Current; public T Current => _src.Current; public void Dispose() { _src?.Dispose(); _src = null; _parent?.Dispose(); _parent = default; Pool.Return(this); } } IPoolingEnumerator IPoolingEnumerable.GetEnumerator() => GetEnumerator(); } }