using System.Collections.Generic; using MemoryPools.Collections.Specialized; namespace MemoryPools.Collections.Linq { internal class UnionExprEnumerable : IPoolingEnumerable { private int _count; private PoolingDictionary _src; public UnionExprEnumerable Init(PoolingDictionary src) { _src = src; _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?.Dispose(); Pool>.Return(_src); _src = default; Pool>.Return(this); } } internal class UnionExprEnumerator : IPoolingEnumerator { private UnionExprEnumerable _parent; private IPoolingEnumerator> _src; public UnionExprEnumerator Init(UnionExprEnumerable parent, IPoolingEnumerator> src) { _src = src; _parent = parent; return this; } public bool MoveNext() => _src.MoveNext(); public void Reset() => _src.Reset(); object IPoolingEnumerator.Current => Current; public T Current => _src.Current.Key; public void Dispose() { _src?.Dispose(); _src = null; _parent?.Dispose(); _parent = default; Pool.Return(this); } } IPoolingEnumerator IPoolingEnumerable.GetEnumerator() => GetEnumerator(); } }