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