Merge pull request #4 from mlemanczyk/codex/implementuj-klase-recyclablestack
Refactor stack enumeration
This commit is contained in:
2 files changed
+200
-54
No files matched your search
@@ -1,94 +1,240 @@
|
|||||||
using System.Collections;
|
using System.Collections;
|
||||||
|
using System.Numerics;
|
||||||
|
using Microsoft.Extensions.ObjectPool;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using Recyclable.Collections.Pools;
|
||||||
|
|
||||||
namespace Recyclable.Collections
|
namespace Recyclable.Collections
|
||||||
{
|
{
|
||||||
internal class RecyclableStack<T> : IList<T>, IDisposable
|
internal sealed class RecyclableStack<T> : IEnumerable<T>, IDisposable
|
||||||
{
|
{
|
||||||
private static readonly bool _needsClearing = !typeof(T).IsValueType;
|
private static readonly bool _needsClearing = !typeof(T).IsValueType;
|
||||||
private bool _disposedValue;
|
private static readonly ObjectPool<Chunk> _chunkPool = new DefaultObjectPool<Chunk>(new DefaultPooledObjectPolicy<Chunk>());
|
||||||
|
|
||||||
protected RecyclableLongList<T> List { get; }
|
private sealed class Chunk
|
||||||
public int Count => checked((int)List._longCount);
|
|
||||||
public long LongCount
|
|
||||||
{
|
{
|
||||||
get => List._longCount;
|
internal T[] Buffer = Array.Empty<T>();
|
||||||
set => List._longCount = value;
|
internal int Index;
|
||||||
}
|
internal Chunk? Previous;
|
||||||
public bool IsReadOnly => false;
|
internal Chunk? Next;
|
||||||
|
|
||||||
public T this[int index] { get => this[(long)index]; set => this[(long)index] = value; }
|
|
||||||
public T this[long index] { get => List[index]; set => List[index] = value; }
|
|
||||||
|
|
||||||
public RecyclableStack(int blockSize = RecyclableDefaults.BlockSize)
|
|
||||||
{
|
|
||||||
List = new(blockSize);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public RecyclableStack(IEnumerable<T> list, int blockSize = RecyclableDefaults.BlockSize)
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
private static Chunk RentChunk(int size, Chunk? previous)
|
||||||
{
|
{
|
||||||
List = new(list, blockSize);
|
var chunk = _chunkPool.Get();
|
||||||
|
chunk.Buffer = size >= RecyclableDefaults.MinPooledArrayLength
|
||||||
|
? RecyclableArrayPool<T>.RentShared(size)
|
||||||
|
: new T[size];
|
||||||
|
chunk.Index = 0;
|
||||||
|
chunk.Previous = previous;
|
||||||
|
chunk.Next = null;
|
||||||
|
return chunk;
|
||||||
}
|
}
|
||||||
|
|
||||||
public RecyclableStack(RecyclableLongList<T> list, int blockSize = RecyclableDefaults.BlockSize)
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
private static void ReturnChunk(Chunk chunk)
|
||||||
{
|
{
|
||||||
List = new(list, blockSize);
|
var buffer = chunk.Buffer;
|
||||||
|
if (buffer.Length >= RecyclableDefaults.MinPooledArrayLength)
|
||||||
|
{
|
||||||
|
RecyclableArrayPool<T>.ReturnShared(buffer, _needsClearing);
|
||||||
}
|
}
|
||||||
|
chunk.Buffer = Array.Empty<T>();
|
||||||
|
chunk.Index = 0;
|
||||||
|
chunk.Previous = null;
|
||||||
|
chunk.Next = null;
|
||||||
|
_chunkPool.Return(chunk);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Chunk _current;
|
||||||
|
private Chunk _bottom;
|
||||||
|
private bool _disposed;
|
||||||
|
private long _capacity;
|
||||||
|
private long _count;
|
||||||
|
|
||||||
|
public RecyclableStack(int initialCapacity = RecyclableDefaults.InitialCapacity)
|
||||||
|
{
|
||||||
|
if (initialCapacity < 1)
|
||||||
|
{
|
||||||
|
initialCapacity = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!BitOperations.IsPow2((uint)initialCapacity))
|
||||||
|
{
|
||||||
|
initialCapacity = (int)BitOperations.RoundUpToPowerOf2((uint)initialCapacity);
|
||||||
|
}
|
||||||
|
|
||||||
|
_current = RentChunk(initialCapacity, null);
|
||||||
|
_bottom = _current;
|
||||||
|
_capacity = initialCapacity;
|
||||||
|
_count = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public RecyclableStack(IEnumerable<T> source, int initialCapacity = RecyclableDefaults.InitialCapacity) : this(initialCapacity)
|
||||||
|
{
|
||||||
|
foreach (var item in source)
|
||||||
|
{
|
||||||
|
Push(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int Count => checked((int)_count);
|
||||||
|
public long LongCount => _count;
|
||||||
|
|
||||||
public void Push(T item)
|
public void Push(T item)
|
||||||
{
|
{
|
||||||
List.Add(item);
|
if (_current.Index == _current.Buffer.Length)
|
||||||
|
{
|
||||||
|
Grow();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_current.Buffer[_current.Index++] = item;
|
||||||
|
_count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Add(T item) => Push(item);
|
||||||
|
|
||||||
public T Pop()
|
public T Pop()
|
||||||
{
|
{
|
||||||
RecyclableLongList<T> list = List;
|
if (_count == 0)
|
||||||
var itemIndex = (int)(list.LongCount & list._blockSizeMinus1) - 1;
|
{
|
||||||
var blockIndex = (int)(LongCount >> list._blockSizePow2BitShift);
|
ThrowHelper.ThrowArgumentOutOfRangeException(nameof(_count), "Stack is empty");
|
||||||
var toRemove = list._memoryBlocks[blockIndex][itemIndex];
|
}
|
||||||
|
|
||||||
LongCount--;
|
|
||||||
|
|
||||||
|
_count--;
|
||||||
|
_current.Index--;
|
||||||
|
var value = _current.Buffer[_current.Index];
|
||||||
if (_needsClearing)
|
if (_needsClearing)
|
||||||
{
|
{
|
||||||
#nullable disable
|
_current.Buffer[_current.Index] = default!;
|
||||||
new Span<T>(list._memoryBlocks[blockIndex])[itemIndex] = default;
|
|
||||||
#nullable restore
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (itemIndex == 0)
|
if (_current.Index == 0 && _current.Previous != null)
|
||||||
{
|
{
|
||||||
list.RemoveBlock(blockIndex);
|
ReleaseCurrentChunk();
|
||||||
}
|
}
|
||||||
|
|
||||||
return toRemove;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int IndexOf(T item) => List.IndexOf(item);
|
public void Clear()
|
||||||
public long LongIndexOf(T item) => List.LongIndexOf(item);
|
|
||||||
public void Insert(int index, T item) => List.Insert(index, item);
|
|
||||||
public void RemoveAt(int index) => List.RemoveAt(index);
|
|
||||||
public void Add(T item) => List.Add(item);
|
|
||||||
public void Clear() => List.Clear();
|
|
||||||
public bool Contains(T item) => List.Contains(item);
|
|
||||||
public void CopyTo(T[] array, int arrayIndex) => List.CopyTo(array, arrayIndex);
|
|
||||||
public bool Remove(T item) => List.Remove(item);
|
|
||||||
public IEnumerator<T> GetEnumerator() => List.Reverse().GetEnumerator();
|
|
||||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
|
||||||
|
|
||||||
protected virtual void Dispose(bool disposing)
|
|
||||||
{
|
{
|
||||||
if (!_disposedValue)
|
while (_current.Previous != null)
|
||||||
{
|
{
|
||||||
Clear();
|
ReleaseCurrentChunk();
|
||||||
_disposedValue = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (_needsClearing && _current.Index > 0)
|
||||||
|
{
|
||||||
|
Array.Clear(_current.Buffer, 0, _current.Index);
|
||||||
|
}
|
||||||
|
|
||||||
|
_capacity = _current.Buffer.Length;
|
||||||
|
_current.Index = 0;
|
||||||
|
_count = 0;
|
||||||
|
_bottom = _current;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Enumerator GetEnumerator() => new(this);
|
||||||
|
IEnumerator<T> IEnumerable<T>.GetEnumerator() => new Enumerator(this);
|
||||||
|
IEnumerator IEnumerable.GetEnumerator() => new Enumerator(this);
|
||||||
|
|
||||||
|
private void Grow()
|
||||||
|
{
|
||||||
|
int newSize;
|
||||||
|
if (_capacity >= RecyclableDefaults.MaxPooledBlockSize)
|
||||||
|
{
|
||||||
|
newSize = RecyclableDefaults.MaxPooledBlockSize;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var doubled = _capacity << 1;
|
||||||
|
newSize = (int)Math.Min(doubled - _capacity, RecyclableDefaults.MaxPooledBlockSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
var newChunk = RentChunk(newSize, _current);
|
||||||
|
_current.Next = newChunk;
|
||||||
|
_current = newChunk;
|
||||||
|
_capacity += newSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ReleaseCurrentChunk()
|
||||||
|
{
|
||||||
|
var toReturn = _current;
|
||||||
|
_current = toReturn.Previous!;
|
||||||
|
_current.Next = null;
|
||||||
|
_capacity -= toReturn.Buffer.Length;
|
||||||
|
if (_current.Previous == null)
|
||||||
|
{
|
||||||
|
_bottom = _current;
|
||||||
|
}
|
||||||
|
ReturnChunk(toReturn);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
if (_disposed)
|
||||||
Dispose(disposing: true);
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Clear();
|
||||||
|
ReturnChunk(_current);
|
||||||
|
_disposed = true;
|
||||||
GC.SuppressFinalize(this);
|
GC.SuppressFinalize(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public struct Enumerator : IEnumerator<T>
|
||||||
|
{
|
||||||
|
private readonly Chunk? _start;
|
||||||
|
private Chunk? _chunk;
|
||||||
|
private int _index;
|
||||||
|
private T? _current;
|
||||||
|
|
||||||
|
internal Enumerator(RecyclableStack<T> stack)
|
||||||
|
{
|
||||||
|
_start = stack._current;
|
||||||
|
_chunk = stack._current;
|
||||||
|
_index = _chunk != null ? _chunk.Index - 1 : -1;
|
||||||
|
_current = default;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T Current => _current!;
|
||||||
|
object IEnumerator.Current => _current!;
|
||||||
|
|
||||||
|
public bool MoveNext()
|
||||||
|
{
|
||||||
|
if (_chunk == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_index < 0)
|
||||||
|
{
|
||||||
|
_chunk = _chunk.Previous;
|
||||||
|
if (_chunk == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
_index = _chunk.Index - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
_current = _chunk.Buffer[_index--];
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Reset()
|
||||||
|
{
|
||||||
|
_chunk = _start;
|
||||||
|
_index = _chunk != null ? _chunk.Index - 1 : -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
_chunk = null;
|
||||||
|
_current = default;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -33,7 +33,7 @@ namespace Recyclable.CollectionsTests
|
|||||||
|
|
||||||
// Validate
|
// Validate
|
||||||
_ = sortedList.Should().HaveCount(_testData.Length)
|
_ = sortedList.Should().HaveCount(_testData.Length)
|
||||||
.And.ContainInConsecutiveOrder(_testData);
|
.And.ContainInConsecutiveOrder(_testData.Reverse());
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -61,7 +61,7 @@ namespace Recyclable.CollectionsTests
|
|||||||
|
|
||||||
// Validate
|
// Validate
|
||||||
_ = sortedList.Should().NotBeEmpty()
|
_ = sortedList.Should().NotBeEmpty()
|
||||||
.And.ContainInConsecutiveOrder(_testData)
|
.And.ContainInConsecutiveOrder(_testData.Reverse())
|
||||||
.And.BeEquivalentTo(_testData);
|
.And.BeEquivalentTo(_testData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user