You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Zero/ZeroLevel/Services/MemoryPools/Collections/Linq/Append.Enumerable.cs

91 lines
2.4 KiB

namespace MemoryPools.Collections.Linq
{
internal class AppendExprEnumerable<T> : IPoolingEnumerable<T>
{
private int _count;
private IPoolingEnumerable<T> _src;
private T _element;
public AppendExprEnumerable<T> Init(IPoolingEnumerable<T> src, T element)
{
_src = src;
_count = 0;
_element = element;
return this;
}
public IPoolingEnumerator<T> GetEnumerator()
{
_count++;
return Pool<AppendExprEnumerator>.Get().Init(_src.GetEnumerator(), this, _element);
}
private void Dispose()
{
if(_count == 0) return;
_count--;
if (_count == 0)
{
7 months ago
_src = default!;
_element = default!;
Pool<AppendExprEnumerable<T>>.Return(this);
}
}
internal class AppendExprEnumerator : IPoolingEnumerator<T>
{
private IPoolingEnumerator _src;
private AppendExprEnumerable<T> _parent;
private T _element;
private int _overcount;
public AppendExprEnumerator Init(IPoolingEnumerator src, AppendExprEnumerable<T> parent, T element)
{
_src = src;
_parent = parent;
_element = element;
_overcount = 0;
return this;
}
public bool MoveNext()
{
if (!_src.MoveNext())
{
if (_overcount == 0)
{
_overcount++;
return true;
}
_overcount++;
return false;
}
return true;
}
public void Reset()
{
_overcount = 0;
_src.Reset();
}
7 months ago
object IPoolingEnumerator.Current => Current!;
public T Current => _overcount == 1 ? _element : (T) _src.Current;
public void Dispose()
{
_parent?.Dispose();
7 months ago
_parent = null!;
_src?.Dispose();
7 months ago
_src = default!;
Pool<AppendExprEnumerator>.Return(this);
}
}
IPoolingEnumerator IPoolingEnumerable.GetEnumerator() => GetEnumerator();
}
}

Powered by TurnKey Linux.