Initial version of RecyclableBlockList implemented.
Tests switched to xUnit in hope to avoid stability issues.
This commit is contained in:
1 parent
aba28028b1
commit
93b8dc5317
17 files changed
+969
-228
No files matched your search
@@ -6,6 +6,79 @@ namespace Recyclable.Collections
|
||||
// It was only beautified for .Net 6.0+.
|
||||
public static class ListExtensions
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool Contains<T>(this RecyclableBlockList<T[]> arrays, T item) => arrays.Any(x => x.Contains(item));
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void CopyTo<T>(this RecyclableBlockList<T[]> arrays, long startingIndex, int blockSize, int lastBlockSize, T[] destinationArray, int destinationArrayIndex)
|
||||
{
|
||||
Span<T> arrayMemory = destinationArray.AsSpan();
|
||||
arrayMemory = arrayMemory[destinationArrayIndex..];
|
||||
int startingArrayIndex = (int)(startingIndex / blockSize);
|
||||
startingIndex %= blockSize;
|
||||
if (arrays.Count > 0)
|
||||
{
|
||||
ReadOnlySpan<T> sourceMemory = arrays[0].AsSpan();
|
||||
var maxToCopy = (int)Math.Min(blockSize - startingIndex, arrayMemory.Length);
|
||||
if (arrays.Count == 1)
|
||||
{
|
||||
maxToCopy = (int)Math.Min(maxToCopy, lastBlockSize);
|
||||
}
|
||||
|
||||
sourceMemory = sourceMemory[(int)startingIndex..maxToCopy];
|
||||
sourceMemory.CopyTo(arrayMemory);
|
||||
arrayMemory = arrayMemory[maxToCopy..];
|
||||
}
|
||||
|
||||
for (var arrayIdx = startingArrayIndex + 1; arrayIdx < arrays.Count - 1 && arrayMemory.Length > 0; arrayIdx++)
|
||||
{
|
||||
ReadOnlySpan<T> sourceMemory = arrays[arrayIdx].AsSpan();
|
||||
var maxToCopy = Math.Min(blockSize, arrayMemory.Length);
|
||||
sourceMemory = sourceMemory[..maxToCopy];
|
||||
sourceMemory.CopyTo(arrayMemory);
|
||||
arrayMemory = arrayMemory[maxToCopy..];
|
||||
}
|
||||
|
||||
if (arrays.Count > 1 && arrayMemory.Length > 0)
|
||||
{
|
||||
ReadOnlySpan<T> sourceMemory = arrays[^1].AsSpan();
|
||||
var maxToCopy = Math.Min(lastBlockSize, arrayMemory.Length);
|
||||
sourceMemory = sourceMemory[..maxToCopy];
|
||||
sourceMemory.CopyTo(arrayMemory);
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static IEnumerable<T> Enumerate<T>(this RecyclableBlockList<T[]> arrays, int chunkSize, long totalCount)
|
||||
{
|
||||
long currentCount = 0;
|
||||
for (var arrayIdx = 0; (arrayIdx < arrays.Count) && (currentCount < totalCount); arrayIdx++)
|
||||
{
|
||||
var array = arrays[arrayIdx];
|
||||
currentCount += Math.Min(chunkSize, totalCount);
|
||||
switch (currentCount < totalCount)
|
||||
{
|
||||
case true:
|
||||
for (var valueIdx = 0; valueIdx < chunkSize; valueIdx++)
|
||||
{
|
||||
yield return array[valueIdx];
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case false:
|
||||
var partialCount = (int)(totalCount % chunkSize);
|
||||
int maxCount = partialCount > 0 ? partialCount : chunkSize;
|
||||
for (var valueIdx = 0; valueIdx < maxCount; valueIdx++)
|
||||
{
|
||||
yield return array[valueIdx];
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void InsertionSort<T>(this IList<T> values, int startIndex, int endIndex, IComparer<T> comparer)
|
||||
{
|
||||
var left = startIndex;
|
||||
@@ -29,6 +102,26 @@ namespace Recyclable.Collections
|
||||
{
|
||||
values.InsertionSort(Comparer<T>.Default);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static long LongIndexOf<T>(this RecyclableBlockList<T[]> arrays, int blockSize, T item, IEqualityComparer<T> comparer)
|
||||
{
|
||||
for (var arrayIdx = 0; arrayIdx < arrays.Count; arrayIdx++)
|
||||
{
|
||||
var array = arrays[arrayIdx];
|
||||
Span<T> arrayMemory = array.AsSpan();
|
||||
for (int memoryIdx = 0; memoryIdx < arrayMemory.Length; memoryIdx++)
|
||||
{
|
||||
if (comparer.Equals(arrayMemory[memoryIdx], item))
|
||||
{
|
||||
return (arrayIdx * (long)blockSize) + memoryIdx;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static void QuickSort<T>(this IList<T> values, int startIndex, int endIndex, IComparer<T> comparer, IRandomNumberGenerator randomNumberGenerator)
|
||||
{
|
||||
var range = (startIndex, endIndex);
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
<Nullable>enable</Nullable>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<PackageId>Recyclable.Collections</PackageId>
|
||||
<PackageVersion>0.0.2</PackageVersion>
|
||||
<PackageVersion>0.0.3</PackageVersion>
|
||||
<Authors>Marek Lemańczyk</Authors>
|
||||
<Company>N/A</Company>
|
||||
<Product>Recyclable.Collections</Product>
|
||||
|
||||
@@ -0,0 +1,334 @@
|
||||
using System.Collections;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Recyclable.Collections
|
||||
{
|
||||
public class RecyclableBlockList<T> : IEnumerable<T>, IDisposable
|
||||
{
|
||||
private static readonly IEqualityComparer<T> _equalityComparer = EqualityComparer<T>.Default;
|
||||
private bool _disposedValue;
|
||||
|
||||
|
||||
private void CopyItems(long startingIndex, long count, ref T[] destArray, long offset = 1)
|
||||
{
|
||||
for (long destItemIndex = startingIndex, sourceItemIndex = offset; destItemIndex < startingIndex + count; destItemIndex++, sourceItemIndex++)
|
||||
{
|
||||
destArray[destItemIndex] = _memory[sourceItemIndex];
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static T GetItem(in T[] arrays, in long index) => arrays[index];
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private void InternalRemoveAt(long index)
|
||||
{
|
||||
long oldCount = LongCount;
|
||||
long oldCountMinus1 = oldCount - 1;
|
||||
if (index == oldCountMinus1)
|
||||
{
|
||||
LongCount--;
|
||||
return;
|
||||
}
|
||||
|
||||
if (index >= oldCount)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(index));
|
||||
}
|
||||
|
||||
CopyItems(index, oldCountMinus1, ref _memory);
|
||||
|
||||
LongCount--;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static long LongIndexOf(in T[] memory, in long itemCount, T? itemToFind, IEqualityComparer<T> equalityComparer)
|
||||
{
|
||||
for (var itemIdx = 0; itemIdx < itemCount; itemIdx++)
|
||||
{
|
||||
var item = memory[itemIdx];
|
||||
if (equalityComparer.Equals(item, itemToFind))
|
||||
{
|
||||
return itemIdx;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static void SetItem(in T[] arrays, in long index, in T value) => arrays[index] = value;
|
||||
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private T[] SetNewLength(long newSize)
|
||||
{
|
||||
var source = _memory;
|
||||
T[] newArray = newSize.RentArrayFromPool<T>();
|
||||
if (newArray.LongLength >= source.LongLength)
|
||||
{
|
||||
source.CopyTo(newArray, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
CopyItems(0, newArray.LongLength, ref newArray, 0);
|
||||
}
|
||||
|
||||
source.ReturnToPool();
|
||||
return newArray;
|
||||
}
|
||||
|
||||
protected T[] _memory = Array.Empty<T>();
|
||||
|
||||
public RecyclableBlockList(long initialCapacity = RecyclableDefaults.Capacity)
|
||||
{
|
||||
//EnsureCapacity(capacity);
|
||||
if (_capacity < initialCapacity)
|
||||
{
|
||||
long newCapacity;
|
||||
switch (_capacity > 0)
|
||||
{
|
||||
case true:
|
||||
newCapacity = _capacity * 2;
|
||||
while (newCapacity < initialCapacity)
|
||||
{
|
||||
newCapacity *= 2;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case false:
|
||||
newCapacity = initialCapacity;
|
||||
break;
|
||||
}
|
||||
|
||||
_capacity = newCapacity;
|
||||
_memory = SetNewLength(newCapacity);
|
||||
}
|
||||
}
|
||||
|
||||
public RecyclableBlockList(IEnumerable<T> source, int initialCapacity = RecyclableDefaults.Capacity)
|
||||
{
|
||||
//EnsureCapacity(capacity);
|
||||
if (_capacity < initialCapacity)
|
||||
{
|
||||
long newCapacity;
|
||||
switch (_capacity > 0)
|
||||
{
|
||||
case true:
|
||||
newCapacity = _capacity * 2;
|
||||
while (newCapacity < initialCapacity)
|
||||
{
|
||||
newCapacity *= 2;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case false:
|
||||
newCapacity = initialCapacity;
|
||||
break;
|
||||
}
|
||||
|
||||
_capacity = newCapacity;
|
||||
_memory = SetNewLength(newCapacity);
|
||||
}
|
||||
|
||||
foreach (var item in source)
|
||||
{
|
||||
Add(item);
|
||||
}
|
||||
}
|
||||
|
||||
public T this[long index]
|
||||
{
|
||||
get => GetItem(_memory, index);
|
||||
set => SetItem(_memory, index, value);
|
||||
}
|
||||
|
||||
public T this[int index]
|
||||
{
|
||||
get => GetItem(_memory, index);
|
||||
set => SetItem(_memory, index, value);
|
||||
}
|
||||
|
||||
private long _capacity;
|
||||
public long Capacity
|
||||
{
|
||||
get => _capacity;
|
||||
set
|
||||
{
|
||||
if (_capacity != value)
|
||||
{
|
||||
_memory = SetNewLength(value);
|
||||
_capacity = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
public int Count => (int)LongCount;
|
||||
public long LongCount { get; set; }
|
||||
public bool IsReadOnly { get; } = false;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Add(in T item)
|
||||
{
|
||||
//EnsureCapacity(LongCount + 1);
|
||||
long requestedCapacity = LongCount + 1;
|
||||
if (_capacity < requestedCapacity)
|
||||
{
|
||||
long newCapacity;
|
||||
switch (_capacity > 0)
|
||||
{
|
||||
case true:
|
||||
newCapacity = _capacity * 2;
|
||||
while (newCapacity < requestedCapacity)
|
||||
{
|
||||
newCapacity *= 2;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case false:
|
||||
newCapacity = requestedCapacity;
|
||||
break;
|
||||
}
|
||||
|
||||
_capacity = newCapacity;
|
||||
_memory = SetNewLength(newCapacity);
|
||||
}
|
||||
|
||||
_memory[LongCount] = item;
|
||||
LongCount++;
|
||||
}
|
||||
|
||||
//[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
//public void EnsureCapacity(long requestedCapacity)
|
||||
//{
|
||||
// if (Capacity < requestedCapacity)
|
||||
// {
|
||||
// long newCapacity;
|
||||
// switch (Capacity > 0)
|
||||
// {
|
||||
// case true:
|
||||
// newCapacity = Capacity * 2;
|
||||
// while (newCapacity < requestedCapacity)
|
||||
// {
|
||||
// newCapacity *= 2;
|
||||
// }
|
||||
|
||||
// break;
|
||||
|
||||
// case false:
|
||||
// newCapacity = requestedCapacity;
|
||||
// break;
|
||||
// }
|
||||
|
||||
// _capacity = newCapacity;
|
||||
// _memory = SetNewLength(newCapacity);
|
||||
// }
|
||||
//}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
LongCount = 0;
|
||||
}
|
||||
|
||||
public bool Contains(T item) => _memory.Contains(item);
|
||||
|
||||
public void CopyTo(T[] array, int arrayIndex) => _memory.CopyTo(array, arrayIndex);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public IEnumerable<T> Enumerate()
|
||||
{
|
||||
for (var i = 0; i < LongCount; i++)
|
||||
{
|
||||
yield return _memory[i];
|
||||
}
|
||||
}
|
||||
|
||||
public int IndexOf(T item) => (int)LongIndexOf(_memory, LongCount, item, _equalityComparer);
|
||||
|
||||
public void Insert(int index, T item)
|
||||
{
|
||||
long requestedCapacity = LongCount + 1;
|
||||
if (Capacity < requestedCapacity)
|
||||
{
|
||||
long newCapacity;
|
||||
switch (Capacity > 0)
|
||||
{
|
||||
case true:
|
||||
newCapacity = Capacity * 2;
|
||||
while (newCapacity < requestedCapacity)
|
||||
{
|
||||
newCapacity *= 2;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case false:
|
||||
newCapacity = requestedCapacity;
|
||||
break;
|
||||
}
|
||||
|
||||
_capacity = newCapacity;
|
||||
_memory = SetNewLength(newCapacity);
|
||||
}
|
||||
|
||||
for (var toMoveIdx = LongCount - 1; toMoveIdx >= index; toMoveIdx--)
|
||||
{
|
||||
_memory[toMoveIdx + 1] = _memory[toMoveIdx];
|
||||
}
|
||||
|
||||
_memory[index] = item;
|
||||
LongCount++;
|
||||
}
|
||||
|
||||
public long LongIndexOf(T item) => LongIndexOf(_memory, LongCount, item, _equalityComparer);
|
||||
|
||||
public bool Remove(T item)
|
||||
{
|
||||
var itemIdx = LongIndexOf(item);
|
||||
if (itemIdx >= 0)
|
||||
{
|
||||
RemoveAt(itemIdx);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public void RemoveAt(int index) => InternalRemoveAt(index);
|
||||
public void RemoveAt(long index) => InternalRemoveAt(index);
|
||||
|
||||
public IEnumerator<T> GetEnumerator() => Enumerate().GetEnumerator();
|
||||
IEnumerator IEnumerable.GetEnumerator() => Enumerate().GetEnumerator();
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (!_disposedValue)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
Clear();
|
||||
_memory.ReturnToPool();
|
||||
Capacity = 0;
|
||||
}
|
||||
|
||||
_disposedValue = true;
|
||||
}
|
||||
}
|
||||
|
||||
// override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
|
||||
// ~RecyclableList()
|
||||
// {
|
||||
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
||||
// Dispose(disposing: false);
|
||||
// }
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
||||
Dispose(disposing: true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,5 +3,6 @@
|
||||
public class RecyclableDefaults
|
||||
{
|
||||
public const int BlockSize = 10_240;
|
||||
public const int Capacity = 8;
|
||||
}
|
||||
}
|
||||
@@ -7,17 +7,38 @@ namespace Recyclable.Collections
|
||||
{
|
||||
private static readonly IEqualityComparer<T> _equalityComparer = EqualityComparer<T>.Default;
|
||||
|
||||
private readonly List<T[]> _arrays = new();
|
||||
private readonly RecyclableBlockList<T[]> _memoryBlocks;
|
||||
private bool _disposedValue;
|
||||
|
||||
public RecyclableList(int blockSize = RecyclableDefaults.BlockSize)
|
||||
public RecyclableList(int blockSize = RecyclableDefaults.BlockSize, long? initialCapacity = default)
|
||||
{
|
||||
BlockSize = blockSize;
|
||||
initialCapacity ??= 8 * BlockSize;
|
||||
int additionalArray = initialCapacity % BlockSize > 0 ? 1 : 0;
|
||||
long targetCapacity = (initialCapacity.Value / BlockSize) + additionalArray;
|
||||
_memoryBlocks = new(targetCapacity);
|
||||
Capacity = initialCapacity.Value;
|
||||
for (var blockIdx = 0; blockIdx < targetCapacity; blockIdx++)
|
||||
{
|
||||
_memoryBlocks.Add(BlockSize.RentArrayFromPool<T>());
|
||||
}
|
||||
|
||||
//MemoryBlocks.LongCount = MemoryBlocks.Capacity;
|
||||
}
|
||||
|
||||
public RecyclableList(IEnumerable<T> source, int blockSize = RecyclableDefaults.BlockSize)
|
||||
public RecyclableList(IEnumerable<T> source, int blockSize = RecyclableDefaults.BlockSize, long? initialCapacity = default)
|
||||
{
|
||||
BlockSize = blockSize;
|
||||
initialCapacity ??= 8 * BlockSize;
|
||||
int additionalCapacity = initialCapacity % BlockSize > 0 ? 1 : 0;
|
||||
long targetCapacity = (initialCapacity.Value / BlockSize) + additionalCapacity;
|
||||
_memoryBlocks = new(targetCapacity);
|
||||
Capacity = initialCapacity.Value;
|
||||
for (var blockIdx = 0; blockIdx < targetCapacity; blockIdx++)
|
||||
{
|
||||
_memoryBlocks.Add(BlockSize.RentArrayFromPool<T>());
|
||||
}
|
||||
|
||||
foreach (var item in source)
|
||||
{
|
||||
Add(item);
|
||||
@@ -27,42 +48,47 @@ namespace Recyclable.Collections
|
||||
public int BlockSize { get; }
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void SetItem(List<T[]> arrays, in int blockSize, in long index, in T value)
|
||||
public static void SetItem(RecyclableBlockList<T[]> arrays, in int blockSize, in long index, in T value)
|
||||
=> arrays[(int)(index / blockSize)][index % blockSize] = value;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static T GetItem(List<T[]> arrays, in int blockSize, in long index)
|
||||
public static T GetItem(RecyclableBlockList<T[]> arrays, in int blockSize, in long index)
|
||||
=> arrays[(int)(index / blockSize)][index % blockSize];
|
||||
|
||||
public T this[long index]
|
||||
{
|
||||
get => GetItem(_arrays, BlockSize, index);
|
||||
set => SetItem(_arrays, BlockSize, index, value);
|
||||
//get => GetItem(MemoryBlocks, BlockSize, index);
|
||||
//set => SetItem(MemoryBlocks, BlockSize, index, value);
|
||||
get => _memoryBlocks[(int)(index / BlockSize)][index % BlockSize];
|
||||
set => _memoryBlocks[(int)(index / BlockSize)][index % BlockSize] = value;
|
||||
}
|
||||
|
||||
public T this[int index]
|
||||
{
|
||||
get => GetItem(_arrays, BlockSize, index);
|
||||
set => SetItem(_arrays, BlockSize, index, value);
|
||||
//get => GetItem(MemoryBlocks, BlockSize, index);
|
||||
//set => SetItem(MemoryBlocks, BlockSize, index, value);
|
||||
get => _memoryBlocks[(int)(index / BlockSize)][index % BlockSize];
|
||||
set => _memoryBlocks[(int)(index / BlockSize)][index % BlockSize] = value;
|
||||
}
|
||||
|
||||
public long Capacity { get; set; }
|
||||
public int Count => (int)LongCount;
|
||||
public long LongCount { get; set; }
|
||||
public bool IsReadOnly { get; } = false;
|
||||
public int BlockCount => _arrays.Count;
|
||||
public long BlockCount => _memoryBlocks.LongCount;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Add(T item)
|
||||
{
|
||||
if (LongCount == Capacity)
|
||||
{
|
||||
T[] newArray = BlockSize.RentArrayFromPool<T>();
|
||||
_arrays.Add(newArray);
|
||||
T[] newMemoryBlock = BlockSize.RentArrayFromPool<T>();
|
||||
_memoryBlocks.Add(newMemoryBlock);
|
||||
Capacity += BlockSize;
|
||||
}
|
||||
|
||||
this[LongCount] = item;
|
||||
SetItem(_memoryBlocks, BlockSize, LongCount, item);
|
||||
//_arrays[_arrays.LongCount - 1][LongCount % BlockSize] = item;
|
||||
LongCount++;
|
||||
}
|
||||
|
||||
@@ -71,11 +97,11 @@ namespace Recyclable.Collections
|
||||
try
|
||||
{
|
||||
// Remove in reversed order for performance savings
|
||||
while (_arrays.Count > 0)
|
||||
while (_memoryBlocks.Count > 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
RemoveBlock(_arrays.Count - 1);
|
||||
RemoveBlock(_memoryBlocks.Count - 1);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
@@ -87,29 +113,47 @@ namespace Recyclable.Collections
|
||||
finally
|
||||
{
|
||||
LongCount = 0;
|
||||
_arrays.Clear();
|
||||
_memoryBlocks.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
public bool Contains(T item) => _arrays.Contains(item);
|
||||
public bool Contains(T item) => _memoryBlocks.Contains(item);
|
||||
|
||||
public void CopyTo(T[] array, int arrayIndex) => _arrays.CopyTo(0, BlockSize, (int)LongCount % BlockSize, array, arrayIndex);
|
||||
public void CopyTo(T[] array, int arrayIndex) => _memoryBlocks.CopyTo(0, BlockSize, (int)LongCount % BlockSize, array, arrayIndex);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public IEnumerator<T> GetEnumerator() => _arrays.Enumerate(BlockSize, LongCount).GetEnumerator();
|
||||
public IEnumerator<T> GetEnumerator() => _memoryBlocks.Enumerate(BlockSize, LongCount).GetEnumerator();
|
||||
|
||||
public int IndexOf(T item) => (int)_arrays.LongIndexOf(BlockSize, item, _equalityComparer);
|
||||
public int IndexOf(T item) => (int)_memoryBlocks.LongIndexOf(BlockSize, item, _equalityComparer);
|
||||
|
||||
public void Insert(int index, T item) => throw new NotSupportedException();
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public long LongIndexOf(T item) => _arrays.LongIndexOf(BlockSize, item, _equalityComparer);
|
||||
public long LongIndexOf(T item) => _memoryBlocks.LongIndexOf(BlockSize, item, _equalityComparer);
|
||||
|
||||
public bool Remove(T item) => throw new NotSupportedException();
|
||||
|
||||
public void RemoveAt(int index) => throw new NotSupportedException();
|
||||
public void RemoveAt(int index) => RemoveAt(this, index);
|
||||
public void RemoveAt(long index) => RemoveAt(this, index);
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => _arrays.Enumerate(BlockSize, LongCount).GetEnumerator();
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static void RemoveAt(RecyclableList<T> list, long index)
|
||||
{
|
||||
if (index == list.LongCount - 1)
|
||||
{
|
||||
list.LongCount--;
|
||||
if (list.Capacity - list.LongCount == list.BlockSize)
|
||||
{
|
||||
list.RemoveBlock(list.BlockCount - 1);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new NotSupportedException("Only removal of the last element is supported");
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => _memoryBlocks.Enumerate(BlockSize, LongCount).GetEnumerator();
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
@@ -138,12 +182,12 @@ namespace Recyclable.Collections
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
public void RemoveBlock(int index)
|
||||
public void RemoveBlock(long index)
|
||||
{
|
||||
try
|
||||
{
|
||||
_arrays[index].ReturnToPool();
|
||||
_arrays.RemoveAt(index);
|
||||
_memoryBlocks[index].ReturnToPool();
|
||||
_memoryBlocks.RemoveAt(index);
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
using System.Buffers;
|
||||
using System.Buffers;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Recyclable.Collections
|
||||
{
|
||||
public static class RecyclableListExtensions
|
||||
{
|
||||
private const int _minPooledArraySize = 100;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void CopyTo<T>(this List<T[]> arrays, long startingIndex, int blockSize, int lastBlockSize, T[] destinationArray, int destinationArrayIndex)
|
||||
public static void CopyTo<T>(this RecyclableList<T[]> arrays, long startingIndex, int blockSize, int lastBlockSize, T[] destinationArray, int destinationArrayIndex)
|
||||
{
|
||||
Span<T> arrayMemory = destinationArray.AsSpan();
|
||||
arrayMemory = arrayMemory[destinationArrayIndex..];
|
||||
@@ -45,10 +48,10 @@ namespace Recyclable.Collections
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool Contains<T>(this List<T[]> arrays, T item) => arrays.Any(x => x.Contains(item));
|
||||
public static bool Contains<T>(this RecyclableList<T[]> arrays, T item) => arrays.Any(x => x.Contains(item));
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static IEnumerable<T> Enumerate<T>(this List<T[]> arrays, int chunkSize, long totalCount)
|
||||
public static IEnumerable<T> Enumerate<T>(this RecyclableList<T[]> arrays, int chunkSize, long totalCount)
|
||||
{
|
||||
long currentCount = 0;
|
||||
for (var arrayIdx = 0; arrayIdx < arrays.Count; arrayIdx++)
|
||||
@@ -83,7 +86,7 @@ namespace Recyclable.Collections
|
||||
=> comparer.Compare(value, maxValue) > 0 ? maxValue : value;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static long LongIndexOf<T>(this List<T[]> arrays, int blockSize, T item, IEqualityComparer<T> comparer)
|
||||
public static long LongIndexOf<T>(this RecyclableList<T[]> arrays, int blockSize, T item, IEqualityComparer<T> comparer)
|
||||
{
|
||||
for (var arrayIdx = 0; arrayIdx < arrays.Count; arrayIdx++)
|
||||
{
|
||||
@@ -101,12 +104,6 @@ namespace Recyclable.Collections
|
||||
return -1;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int ToArrayIndex(this long index, int blockSize) => (int)(index / blockSize);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static long ToItemIndex(this long index, int blockSize) => index % blockSize;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static RecyclableList<T> ToRecyclableList<T>(this IList<T> values, int blockSize = RecyclableDefaults.BlockSize) => new(values, blockSize);
|
||||
|
||||
@@ -114,18 +111,27 @@ namespace Recyclable.Collections
|
||||
public static RecyclableList<T> ToRecyclableList<T>(this IEnumerable<T> values, int blockSize = RecyclableDefaults.BlockSize) => new(values, blockSize);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static T[] RentArrayFromPool<T>(this int minSize) => ArrayPool<T>.Shared.Rent(minSize);
|
||||
public static T[] RentArrayFromPool<T>(this int minSize) => RentArrayFromPool<T>((long)minSize);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void ReturnToPool<T>(this T[] array) => ArrayPool<T>.Shared.Return(array);
|
||||
public static T[] RentArrayFromPool<T>(this long minSize)
|
||||
{
|
||||
return minSize switch
|
||||
{
|
||||
< _minPooledArraySize => new T[minSize],
|
||||
<= int.MaxValue => ArrayPool<T>.Shared.Rent((int)minSize),
|
||||
_ => new T[minSize]
|
||||
};
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void SetItem<T>(this List<T[]> arrays, in int blockSize, in long index, in T value)
|
||||
=> arrays[(int)(index / blockSize)][index % blockSize] = value;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static T GetItem<T>(this List<T[]> arrays, in int blockSize, in long index)
|
||||
=> arrays[(int)(index / blockSize)][index % blockSize];
|
||||
|
||||
public static void ReturnToPool<T>(this T[] array)
|
||||
{
|
||||
long arrayLength = array.LongLength;
|
||||
if (arrayLength is >= _minPooledArraySize and <= int.MaxValue)
|
||||
{
|
||||
ArrayPool<T>.Shared.Return(array);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
using System.Buffers;
|
||||
using System.Collections;
|
||||
using System.Collections;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Recyclable.Collections
|
||||
@@ -7,25 +6,55 @@ namespace Recyclable.Collections
|
||||
public class RecyclableQueue<T> : IRecyclableOwner<T>, IList<T>, IDisposable
|
||||
{
|
||||
private static readonly IEqualityComparer<T> _equalityComparer = EqualityComparer<T>.Default;
|
||||
private static readonly IComparer<long> _comparer = Comparer<long>.Default;
|
||||
|
||||
private bool _disposedValue;
|
||||
private RecyclableBlockList<T[]> Memory { get; }
|
||||
|
||||
protected List<T[]> Arrays { get; }
|
||||
protected long GetAbsoluteIndex(long index) => index + RemovedCount;
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static T Dequeue(RecyclableQueue<T> queue)
|
||||
{
|
||||
if (queue.LongCount <= 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(queue), "There are no items in the queue");
|
||||
}
|
||||
|
||||
var toRemove = queue[0];
|
||||
queue.RemovedCount++;
|
||||
queue.LongCount--;
|
||||
if (queue.Capacity - queue.LongCount == queue.BlockSize)
|
||||
{
|
||||
queue.RemoveBlock(0);
|
||||
queue.RemovedCount -= queue.BlockSize;
|
||||
}
|
||||
|
||||
return toRemove;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static bool TryDequeue(RecyclableQueue<T> queue, out T? item)
|
||||
{
|
||||
if (queue.LongCount > 0)
|
||||
{
|
||||
item = queue.Dequeue();
|
||||
return true;
|
||||
}
|
||||
|
||||
item = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected static long GetAbsoluteIndex(long index, long removedCount) => index + removedCount;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected long GetRelativeIndex(long index) => index - RemovedCount;
|
||||
|
||||
protected long RemovedCount { get; set; }
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static T[] RentArrayFromPool(int minSize) => ArrayPool<T>.Shared.Rent(minSize);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void ReturnToPool(T[] array) => ArrayPool<T>.Shared.Return(array);
|
||||
|
||||
public RecyclableQueue(IEnumerable<T> source, int blockSize = RecyclableDefaults.BlockSize)
|
||||
{
|
||||
BlockSize = blockSize;
|
||||
Arrays = new();
|
||||
Memory = new(1);
|
||||
foreach (var item in source)
|
||||
{
|
||||
Enqueue(item);
|
||||
@@ -35,7 +64,7 @@ namespace Recyclable.Collections
|
||||
public RecyclableQueue(int blockSize = RecyclableDefaults.BlockSize)
|
||||
{
|
||||
BlockSize = blockSize;
|
||||
Arrays = new();
|
||||
Memory = new(1);
|
||||
}
|
||||
|
||||
public int BlockSize { get; protected set; }
|
||||
@@ -43,31 +72,47 @@ namespace Recyclable.Collections
|
||||
|
||||
public int Count
|
||||
{
|
||||
get => (int)LongCount.LimitTo(int.MaxValue, _comparer);
|
||||
get => (int)LongCount;
|
||||
set => LongCount = value;
|
||||
}
|
||||
|
||||
public long LongCount { get; set; }
|
||||
public bool IsReadOnly { get; } = false;
|
||||
|
||||
public T this[int index] { get => this[(long)index]; set => this[(long)index] = value; }
|
||||
public T this[int index]
|
||||
{
|
||||
get => GetItem(index);
|
||||
set => SetItem(index, value);
|
||||
}
|
||||
|
||||
public T this[long index]
|
||||
{
|
||||
get
|
||||
{
|
||||
long absoluteIndex = GetAbsoluteIndex(index);
|
||||
int arrayIndex = absoluteIndex.ToArrayIndex(BlockSize);
|
||||
long itemIndex = absoluteIndex.ToItemIndex(BlockSize);
|
||||
return Arrays[arrayIndex][itemIndex];
|
||||
}
|
||||
get => GetItem(index);
|
||||
set => SetItem(index, value);
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
long absoluteIndex = GetAbsoluteIndex(index);
|
||||
int arrayIndex = absoluteIndex.ToArrayIndex(BlockSize);
|
||||
long itemIndex = absoluteIndex.ToItemIndex(BlockSize);
|
||||
Arrays[arrayIndex][itemIndex] = value;
|
||||
}
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int ToArrayIndex(long index, int blockSize) => (int)(index / blockSize);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static long ToItemIndex(long index, int blockSize) => index % blockSize;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private void SetItem(long index, T value)
|
||||
{
|
||||
long absoluteIndex = GetAbsoluteIndex(index, RemovedCount);
|
||||
int arrayIndex = ToArrayIndex(absoluteIndex, BlockSize);
|
||||
long itemIndex = ToItemIndex(absoluteIndex, BlockSize);
|
||||
Memory[arrayIndex][itemIndex] = value;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private T GetItem(long index)
|
||||
{
|
||||
long absoluteIndex = GetAbsoluteIndex(index, RemovedCount);
|
||||
int arrayIndex = ToArrayIndex(absoluteIndex, BlockSize);
|
||||
long itemIndex = ToItemIndex(absoluteIndex, BlockSize);
|
||||
return Memory[arrayIndex][itemIndex];
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
@@ -75,33 +120,22 @@ namespace Recyclable.Collections
|
||||
{
|
||||
if (LongCount == Capacity)
|
||||
{
|
||||
T[] newArray = BlockSize.RentArrayFromPool<T>();
|
||||
Arrays.Add(newArray);
|
||||
Capacity += BlockSize;
|
||||
AddBlock();
|
||||
}
|
||||
|
||||
var newIndex = LongCount;
|
||||
// We don't want this[newIndex] set to raise the update event, because
|
||||
// the count would be wrong. We need to increase it, first.
|
||||
this[newIndex] = item;
|
||||
SetItem(LongCount, item);
|
||||
LongCount++;
|
||||
}
|
||||
|
||||
|
||||
public T Dequeue()
|
||||
public void AddBlock()
|
||||
{
|
||||
var toRemove = this[0];
|
||||
RemovedCount++;
|
||||
LongCount--;
|
||||
if (Capacity - LongCount == BlockSize)
|
||||
{
|
||||
RemoveBlock(0);
|
||||
RemovedCount -= BlockSize;
|
||||
}
|
||||
|
||||
return toRemove;
|
||||
T[] newArray = BlockSize.RentArrayFromPool<T>();
|
||||
Memory.Add(newArray);
|
||||
Capacity += BlockSize;
|
||||
}
|
||||
|
||||
public T Dequeue() => Dequeue(this);
|
||||
|
||||
public void Add(T item) => Enqueue(item);
|
||||
|
||||
public void Clear()
|
||||
@@ -109,11 +143,11 @@ namespace Recyclable.Collections
|
||||
try
|
||||
{
|
||||
// Remove in reversed order for performance savings
|
||||
while (Arrays.Count > 0)
|
||||
while (Memory.Count > 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
RemoveBlock(Arrays.Count - 1);
|
||||
RemoveBlock(Memory.Count - 1);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
@@ -126,46 +160,32 @@ namespace Recyclable.Collections
|
||||
{
|
||||
LongCount = 0;
|
||||
RemovedCount = 0;
|
||||
Arrays.Clear();
|
||||
Memory.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
public bool Contains(T item) => Arrays.Contains(item);
|
||||
public void CopyTo(T[] array, int arrayIndex) => Arrays.CopyTo(RemovedCount, BlockSize, (int)(LongCount % BlockSize), array, arrayIndex);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public IEnumerable<T> Enumerate() => Arrays.Enumerate(BlockSize, LongCount);
|
||||
public IEnumerator<T> GetEnumerator() => Enumerate().GetEnumerator();
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
public int IndexOf(T item) => (int)LongIndexOf(item).LimitTo(int.MaxValue, _comparer);
|
||||
public long LongIndexOf(T item) => GetRelativeIndex(Arrays.LongIndexOf(BlockSize, item, _equalityComparer));
|
||||
public bool Contains(T item) => Memory.Contains(item);
|
||||
public void CopyTo(T[] array, int arrayIndex) => Memory.CopyTo(RemovedCount, BlockSize, (int)(LongCount % BlockSize), array, arrayIndex);
|
||||
public IEnumerable<T> Enumerate() => Memory.Enumerate(BlockSize, LongCount);
|
||||
public IEnumerator<T> GetEnumerator() => Memory.Enumerate(BlockSize, LongCount).GetEnumerator();
|
||||
IEnumerator IEnumerable.GetEnumerator() => Memory.Enumerate(BlockSize, LongCount).GetEnumerator();
|
||||
public int IndexOf(T item) => (int)LongIndexOf(item);
|
||||
public long LongIndexOf(T item) => GetRelativeIndex(Memory.LongIndexOf(BlockSize, item, _equalityComparer));
|
||||
public void Insert(int index, T item) => throw new NotSupportedException();
|
||||
public void RemoveAt(int index) => throw new NotSupportedException();
|
||||
|
||||
public bool TryDequeue(out T? item)
|
||||
{
|
||||
if (LongCount > 0)
|
||||
{
|
||||
item = Dequeue();
|
||||
return true;
|
||||
}
|
||||
|
||||
item = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool Remove(T item) => throw new NotSupportedException();
|
||||
public bool TryDequeue(out T? item) => TryDequeue(this, out item);
|
||||
public bool Remove(T item) => TryDequeue(out var _);
|
||||
public void RemoveBlock(int index)
|
||||
{
|
||||
try
|
||||
{
|
||||
ReturnToPool(Arrays[index]);
|
||||
Arrays.RemoveAt(index);
|
||||
Memory[index].ReturnToPool();
|
||||
Memory.RemoveAt(index);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Capacity -= BlockSize;
|
||||
LongCount -= BlockSize;
|
||||
//LongCount -= BlockSize;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user