RecyclableBlockList renamed to RecyclableArrayList for clarity. First set of optimizations and implementation.
This commit is contained in:
1 parent
8147c4ecbf
commit
880abd605e
16 files changed
+1040
-800
No files matched your search
@@ -7,10 +7,10 @@ namespace Recyclable.Collections
|
||||
public static class ListExtensions
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool Contains<T>(this RecyclableBlockList<T[]> arrays, T item) => arrays.Any(x => x.Contains(item));
|
||||
public static bool Contains<T>(this RecyclableArrayList<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)
|
||||
public static void CopyTo<T>(this RecyclableArrayList<T[]> arrays, long startingIndex, int blockSize, int lastBlockSize, T[] destinationArray, int destinationArrayIndex)
|
||||
{
|
||||
Span<T> arrayMemory = destinationArray.AsSpan();
|
||||
arrayMemory = arrayMemory[destinationArrayIndex..];
|
||||
@@ -49,7 +49,7 @@ namespace Recyclable.Collections
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static IEnumerable<T> Enumerate<T>(this RecyclableBlockList<T[]> arrays, int chunkSize, long totalCount)
|
||||
public static IEnumerable<T> Enumerate<T>(this RecyclableArrayList<T[]> arrays, int chunkSize, long totalCount)
|
||||
{
|
||||
long currentCount = 0;
|
||||
for (var arrayIdx = 0; (arrayIdx < arrays.Count) && (currentCount < totalCount); arrayIdx++)
|
||||
@@ -104,7 +104,7 @@ namespace Recyclable.Collections
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static long LongIndexOf<T>(this RecyclableBlockList<T[]> arrays, int blockSize, T item, IEqualityComparer<T> comparer)
|
||||
public static long LongIndexOf<T>(this RecyclableArrayList<T[]> arrays, int blockSize, T item, IEqualityComparer<T> comparer)
|
||||
{
|
||||
for (var arrayIdx = 0; arrayIdx < arrays.Count; arrayIdx++)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,220 @@
|
||||
using System.Collections;
|
||||
|
||||
namespace Recyclable.Collections
|
||||
{
|
||||
public class RecyclableArrayList<T> : IEnumerable<T>, IDisposable
|
||||
{
|
||||
private static readonly IEqualityComparer<T> _equalityComparer = EqualityComparer<T>.Default;
|
||||
|
||||
protected T[] _memory;
|
||||
|
||||
protected static T[] SetNewLength(in T[]? source, in int newSize)
|
||||
{
|
||||
var sourceLength = source?.Length ?? 0;
|
||||
T[] newArray;
|
||||
newArray = newSize.RentArrayFromPool<T>();
|
||||
switch (newSize >= sourceLength)
|
||||
{
|
||||
case true:
|
||||
if (sourceLength > 0)
|
||||
{
|
||||
Memory<T> sourceMemory = new(source);
|
||||
Memory<T> newArrayMemory = new(newArray);
|
||||
sourceMemory.CopyTo(newArrayMemory);
|
||||
source!.ReturnToPool();
|
||||
}
|
||||
|
||||
return newArray;
|
||||
|
||||
case false:
|
||||
Span<T> sourceSpan = source.AsSpan();
|
||||
Span<T> newArraySpan = newArray.AsSpan();
|
||||
sourceSpan = sourceSpan[..newSize];
|
||||
sourceSpan.CopyTo(newArraySpan);
|
||||
source!.ReturnToPool();
|
||||
return newArray;
|
||||
}
|
||||
}
|
||||
|
||||
protected void EnsureCapacity(in int requestedCapacity)
|
||||
{
|
||||
int newCapacity;
|
||||
switch (_capacity > 0)
|
||||
{
|
||||
case true:
|
||||
newCapacity = _capacity;
|
||||
while (newCapacity < requestedCapacity)
|
||||
{
|
||||
newCapacity *= 2;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case false:
|
||||
newCapacity = requestedCapacity;
|
||||
break;
|
||||
}
|
||||
|
||||
_memory = SetNewLength(_memory, newCapacity);
|
||||
_capacity = _memory.Length;
|
||||
}
|
||||
|
||||
#pragma warning disable CS8618 // _memory will be initialized when the 1st item is added
|
||||
public RecyclableArrayList()
|
||||
#pragma warning restore CS8618
|
||||
{
|
||||
}
|
||||
|
||||
#pragma warning disable CS8618 // _memory will be initialized when the 1st item is added
|
||||
public RecyclableArrayList(int initialCapacity)
|
||||
#pragma warning restore CS8618
|
||||
{
|
||||
// _memory will be initialized when the 1st item is added
|
||||
if (initialCapacity > 0)
|
||||
{
|
||||
_memory = SetNewLength(_memory, initialCapacity);
|
||||
_capacity = initialCapacity;
|
||||
}
|
||||
}
|
||||
|
||||
public RecyclableArrayList(IEnumerable<T> source, int initialCapacity = RecyclableDefaults.Capacity)
|
||||
{
|
||||
_memory = SetNewLength(_memory, initialCapacity);
|
||||
_capacity = initialCapacity;
|
||||
|
||||
foreach (var item in source)
|
||||
{
|
||||
Add(item);
|
||||
}
|
||||
}
|
||||
|
||||
public T this[int index]
|
||||
{
|
||||
get => _memory[index];
|
||||
set => _memory[index] = value;
|
||||
}
|
||||
|
||||
internal int _capacity;
|
||||
public int Capacity
|
||||
{
|
||||
get => _capacity;
|
||||
set
|
||||
{
|
||||
if (_capacity != value)
|
||||
{
|
||||
_memory = SetNewLength(_memory, value);
|
||||
_capacity = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
public int Count { get; set; }
|
||||
public bool IsReadOnly { get; } = false;
|
||||
|
||||
public void Add(in T item)
|
||||
{
|
||||
int requestedCapacity = Count + 1;
|
||||
if (_capacity < requestedCapacity)
|
||||
{
|
||||
EnsureCapacity(requestedCapacity);
|
||||
}
|
||||
|
||||
_memory[Count] = item;
|
||||
Count++;
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
Count = 0;
|
||||
}
|
||||
|
||||
public bool Contains(T item) => _memory.Contains(item);
|
||||
|
||||
public void CopyTo(T[] array, int arrayIndex) => _memory.CopyTo(array, arrayIndex);
|
||||
|
||||
protected static IEnumerable<T> Enumerate(RecyclableArrayList<T> list)
|
||||
{
|
||||
for (var i = 0; i < list.Count; i++)
|
||||
{
|
||||
yield return list._memory[i];
|
||||
}
|
||||
}
|
||||
|
||||
public int IndexOf(T itemToFind)
|
||||
{
|
||||
var memory = _memory;
|
||||
int itemCount = Count;
|
||||
var equalityComparer = _equalityComparer;
|
||||
for (var itemIdx = 0; itemIdx < itemCount; itemIdx++)
|
||||
{
|
||||
var item = memory[itemIdx];
|
||||
if (equalityComparer.Equals(item, itemToFind))
|
||||
{
|
||||
return itemIdx;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
public void Insert(int index, T item)
|
||||
{
|
||||
int requestedCapacity = Count + 1;
|
||||
if (Capacity < requestedCapacity)
|
||||
{
|
||||
EnsureCapacity(requestedCapacity);
|
||||
}
|
||||
|
||||
for (var toMoveIdx = Count - 1; toMoveIdx >= index; toMoveIdx--)
|
||||
{
|
||||
_memory[toMoveIdx + 1] = _memory[toMoveIdx];
|
||||
}
|
||||
|
||||
_memory[index] = item;
|
||||
Count++;
|
||||
}
|
||||
|
||||
public bool Remove(T itemToRemove)
|
||||
{
|
||||
var itemIdx = IndexOf(itemToRemove);
|
||||
if (itemIdx >= 0)
|
||||
{
|
||||
RemoveAt(itemIdx);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
public void RemoveAt(int index)
|
||||
{
|
||||
int oldCount = Count;
|
||||
int oldCountMinus1 = oldCount - 1;
|
||||
if (index == oldCountMinus1)
|
||||
{
|
||||
Count--;
|
||||
return;
|
||||
}
|
||||
|
||||
if (index >= oldCount)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(index));
|
||||
}
|
||||
|
||||
_memory.CopyItems(index, oldCountMinus1, ref _memory);
|
||||
Count--;
|
||||
}
|
||||
|
||||
public IEnumerator<T> GetEnumerator() => Enumerate(this).GetEnumerator();
|
||||
IEnumerator IEnumerable.GetEnumerator() => Enumerate(this).GetEnumerator();
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (Capacity > 0)
|
||||
{
|
||||
Clear();
|
||||
Capacity = 0;
|
||||
_memory.ReturnToPool();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,327 +0,0 @@
|
||||
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(in long newSize)
|
||||
{
|
||||
if (newSize == _capacity)
|
||||
{
|
||||
return _memory;
|
||||
}
|
||||
|
||||
var source = _memory;
|
||||
var sourceLength = source.LongLength;
|
||||
var is32bit = newSize <= int.MaxValue;
|
||||
T[] newArray;
|
||||
if (is32bit)
|
||||
{
|
||||
var newSize32bit = (int)newSize;
|
||||
newArray = newSize32bit.RentArrayFromPool<T>();
|
||||
if (newSize32bit >= sourceLength)
|
||||
{
|
||||
if (sourceLength > 0)
|
||||
{
|
||||
Memory<T> sourceMemory = new(source);
|
||||
Memory<T> newArrayMemory = new(newArray);
|
||||
sourceMemory.CopyTo(newArrayMemory);
|
||||
source.ReturnToPool();
|
||||
}
|
||||
|
||||
return newArray;
|
||||
}
|
||||
else
|
||||
{
|
||||
Span<T> sourceSpan = source.AsSpan();
|
||||
Span<T> newArraySpan = newArray.AsSpan();
|
||||
sourceSpan = sourceSpan[..newSize32bit];
|
||||
sourceSpan.CopyTo(newArraySpan);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
newArray = newSize.RentArrayFromPool<T>();
|
||||
if (newSize >= sourceLength)
|
||||
{
|
||||
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)
|
||||
{
|
||||
_memory = SetNewLength(initialCapacity);
|
||||
_capacity = initialCapacity;
|
||||
}
|
||||
|
||||
public RecyclableBlockList(IEnumerable<T> source, int initialCapacity = RecyclableDefaults.Capacity)
|
||||
{
|
||||
_memory = SetNewLength(initialCapacity);
|
||||
_capacity = initialCapacity;
|
||||
|
||||
foreach (var item in source)
|
||||
{
|
||||
Add(item);
|
||||
}
|
||||
}
|
||||
|
||||
public T this[long index]
|
||||
{
|
||||
get => _memory[index];
|
||||
set => _memory[index] = value;
|
||||
}
|
||||
|
||||
public T this[int index]
|
||||
{
|
||||
get => _memory[index];
|
||||
set => _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;
|
||||
}
|
||||
|
||||
_memory = SetNewLength(newCapacity);
|
||||
_capacity = 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;
|
||||
// }
|
||||
|
||||
// _memory = SetNewLength(newCapacity);
|
||||
// _capacity = 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;
|
||||
}
|
||||
|
||||
_memory = SetNewLength(newCapacity);
|
||||
_capacity = 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,5 +4,6 @@
|
||||
{
|
||||
public const int BlockSize = 10_240;
|
||||
public const int Capacity = 8;
|
||||
public const int MinPooledArrayLength = 100;
|
||||
}
|
||||
}
|
||||
@@ -1,51 +1,102 @@
|
||||
using System.Collections;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Buffers;
|
||||
using System.Collections;
|
||||
|
||||
namespace Recyclable.Collections
|
||||
{
|
||||
public class RecyclableList<T> : IDisposable, IList<T>
|
||||
{
|
||||
private static readonly IEqualityComparer<T> _equalityComparer = EqualityComparer<T>.Default;
|
||||
private const int _minPooledArraySize = 100;
|
||||
private readonly int _blockSize;
|
||||
private readonly RecyclableArrayList<T[]> _memoryBlocks;
|
||||
|
||||
private readonly RecyclableBlockList<T[]> _memoryBlocks;
|
||||
private bool _disposedValue;
|
||||
|
||||
public RecyclableList(int blockSize = RecyclableDefaults.BlockSize, long? initialCapacity = default)
|
||||
private static void AddCapacity(RecyclableList<T> list, RecyclableArrayList<T[]> memoryBlocks, int additionalCapacity)
|
||||
{
|
||||
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;
|
||||
T[] newMemoryBlock = RentArrayFromPool(additionalCapacity);
|
||||
memoryBlocks.Add(newMemoryBlock);
|
||||
list.Capacity += additionalCapacity;
|
||||
}
|
||||
|
||||
public RecyclableList(IEnumerable<T> source, int blockSize = RecyclableDefaults.BlockSize, long? initialCapacity = default)
|
||||
private static void AddRange(RecyclableList<T> destination, IEnumerable<T> source)
|
||||
{
|
||||
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);
|
||||
destination.Add(item);
|
||||
}
|
||||
}
|
||||
|
||||
public int BlockSize { get; }
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
private static T[] RentArrayFromPool(int minSize) => (minSize >= _minPooledArraySize)
|
||||
? ArrayPool<T>.Shared.Rent(minSize)
|
||||
: new T[minSize];
|
||||
|
||||
private static void ReturnToPool(in T[] array)
|
||||
{
|
||||
if (array.LongLength is < _minPooledArraySize or > int.MaxValue)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ArrayPool<T>.Shared.Return(array);
|
||||
}
|
||||
|
||||
private static void RemoveBlock(RecyclableList<T> owner, RecyclableArrayList<T[]> memoryBlocks, int blockSize, int index)
|
||||
{
|
||||
try
|
||||
{
|
||||
ReturnToPool(memoryBlocks[index]);
|
||||
memoryBlocks.RemoveAt(index);
|
||||
}
|
||||
finally
|
||||
{
|
||||
owner.Capacity -= blockSize;
|
||||
}
|
||||
}
|
||||
|
||||
private static RecyclableArrayList<T[]> SetupMemoryBlocks(RecyclableList<T> owner, int blockSize, long? totalItemsCount)
|
||||
{
|
||||
totalItemsCount ??= 8 * blockSize;
|
||||
int additionalArray = totalItemsCount % blockSize > 0 ? 1 : 0;
|
||||
int memoryBlockCount = (int)(totalItemsCount.Value / blockSize).LimitTo(int.MaxValue) + additionalArray;
|
||||
RecyclableArrayList<T[]> memoryBlocks = new(memoryBlockCount);
|
||||
for (var blockIdx = 0; blockIdx < memoryBlockCount; blockIdx++)
|
||||
{
|
||||
memoryBlocks[blockIdx] = RentArrayFromPool(blockSize);
|
||||
}
|
||||
|
||||
memoryBlocks.Count = memoryBlockCount;
|
||||
owner.Capacity = totalItemsCount.Value;
|
||||
return memoryBlocks;
|
||||
}
|
||||
|
||||
|
||||
public RecyclableList(int blockSize = RecyclableDefaults.BlockSize, long? totalItemsCount = default)
|
||||
{
|
||||
_blockSize = blockSize;
|
||||
_memoryBlocks = SetupMemoryBlocks(this, blockSize, totalItemsCount);
|
||||
}
|
||||
|
||||
public RecyclableList(IEnumerable<T> source, int blockSize = RecyclableDefaults.BlockSize, long? totalItemsCount = default)
|
||||
{
|
||||
_blockSize = blockSize;
|
||||
_memoryBlocks = SetupMemoryBlocks(this, blockSize, totalItemsCount);
|
||||
AddRange(this, source);
|
||||
}
|
||||
|
||||
public T this[long index]
|
||||
{
|
||||
@@ -59,23 +110,22 @@ namespace Recyclable.Collections
|
||||
set => _memoryBlocks[index / _blockSize][index % _blockSize] = value;
|
||||
}
|
||||
|
||||
public long Capacity { get; set; }
|
||||
public long Capacity { get; protected set; }
|
||||
public int Count => (int)LongCount;
|
||||
public long LongCount { get; set; }
|
||||
public bool IsReadOnly { get; } = false;
|
||||
public long BlockCount => _memoryBlocks.LongCount;
|
||||
public int BlockCount => _memoryBlocks.Count;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Add(T item)
|
||||
{
|
||||
if (LongCount == Capacity)
|
||||
long oldCount = LongCount;
|
||||
int blockSize = _blockSize;
|
||||
if (oldCount == Capacity)
|
||||
{
|
||||
T[] newMemoryBlock = BlockSize.RentArrayFromPool<T>();
|
||||
_memoryBlocks.Add(newMemoryBlock);
|
||||
Capacity += BlockSize;
|
||||
AddCapacity(this, _memoryBlocks, blockSize);
|
||||
}
|
||||
|
||||
memoryBlocks[oldCount / blockSize][oldCount % blockSize] = item;
|
||||
_memoryBlocks[(int)(oldCount / blockSize)][oldCount % blockSize] = item;
|
||||
LongCount++;
|
||||
}
|
||||
|
||||
@@ -88,7 +138,7 @@ namespace Recyclable.Collections
|
||||
{
|
||||
try
|
||||
{
|
||||
RemoveBlock(_memoryBlocks.Count - 1);
|
||||
RemoveBlock(this, _memoryBlocks, _blockSize, _memoryBlocks.Count - 1);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
@@ -106,81 +156,29 @@ namespace Recyclable.Collections
|
||||
|
||||
public bool Contains(T item) => _memoryBlocks.Contains(item);
|
||||
|
||||
public void CopyTo(T[] array, int arrayIndex) => _memoryBlocks.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() => _memoryBlocks.Enumerate(BlockSize, LongCount).GetEnumerator();
|
||||
public IEnumerator<T> GetEnumerator() => _memoryBlocks.Enumerate(_blockSize, LongCount).GetEnumerator();
|
||||
|
||||
public int IndexOf(T item) => (int)_memoryBlocks.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) => _memoryBlocks.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) => RemoveAt(this, index);
|
||||
public void RemoveAt(long index) => RemoveAt(this, index);
|
||||
|
||||
[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)
|
||||
{
|
||||
if (!_disposedValue)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
_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);
|
||||
// }
|
||||
IEnumerator IEnumerable.GetEnumerator() => _memoryBlocks.Enumerate(_blockSize, LongCount).GetEnumerator();
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
|
||||
Dispose(disposing: true);
|
||||
Clear();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
public void RemoveBlock(long index)
|
||||
{
|
||||
try
|
||||
{
|
||||
_memoryBlocks[index].ReturnToPool();
|
||||
_memoryBlocks.RemoveAt(index);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Capacity -= BlockSize;
|
||||
LongCount -= BlockSize;
|
||||
}
|
||||
}
|
||||
public void RemoveBlock(int index) => RemoveBlock(this, _memoryBlocks, _blockSize, index);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
using System.Buffers;
|
||||
using System.Buffers;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Recyclable.Collections
|
||||
@@ -8,18 +7,25 @@ namespace Recyclable.Collections
|
||||
{
|
||||
private const int _minPooledArraySize = 100;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void CopyItems<T>(this T[] memory, 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];
|
||||
}
|
||||
}
|
||||
|
||||
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..];
|
||||
int startingArrayIndex = (int)(startingIndex / blockSize);
|
||||
startingIndex %= blockSize;
|
||||
if (arrays.Count > 0)
|
||||
if (arrays.LongCount > 0)
|
||||
{
|
||||
ReadOnlySpan<T> sourceMemory = arrays[0].AsSpan();
|
||||
var maxToCopy = (int)Math.Min(blockSize - startingIndex, arrayMemory.Length);
|
||||
if (arrays.Count == 1)
|
||||
if (arrays.LongCount == 1)
|
||||
{
|
||||
maxToCopy = (int)Math.Min(maxToCopy, lastBlockSize);
|
||||
}
|
||||
@@ -47,91 +53,122 @@ namespace Recyclable.Collections
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool Contains<T>(this RecyclableList<T[]> arrays, T item) => arrays.Any(x => x.Contains(item));
|
||||
//[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
//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 RecyclableList<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++)
|
||||
// {
|
||||
// var array = arrays[arrayIdx];
|
||||
// currentCount += chunkSize;
|
||||
// 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;
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
//[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
//public static T LimitTo<T>(this T value, T maxValue, IComparer<T> comparer)
|
||||
// => comparer.Compare(value, maxValue) > 0 ? maxValue : value;
|
||||
|
||||
//[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static long LongIndexOf<T>(this RecyclableList<T> arrays, long itemsCount, T itemToFind, IEqualityComparer<T> comparer)
|
||||
{
|
||||
long currentCount = 0;
|
||||
for (var arrayIdx = 0; arrayIdx < arrays.Count; arrayIdx++)
|
||||
for (var itemIdx = 0L; itemIdx < itemsCount; itemIdx++)
|
||||
{
|
||||
var array = arrays[arrayIdx];
|
||||
currentCount += chunkSize;
|
||||
switch (currentCount < totalCount)
|
||||
var item = arrays[itemIdx];
|
||||
if (comparer.Equals(itemToFind, item))
|
||||
{
|
||||
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;
|
||||
return itemIdx;
|
||||
}
|
||||
|
||||
itemIdx++;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static T LimitTo<T>(this T value, T maxValue, IComparer<T> comparer)
|
||||
=> comparer.Compare(value, maxValue) > 0 ? maxValue : value;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static long LongIndexOf<T>(this RecyclableList<T[]> arrays, int blockSize, T item, IEqualityComparer<T> comparer)
|
||||
public static long LongIndexOf<T>(this RecyclableArrayList<T> arrays, int itemsCount, T itemToFind, IEqualityComparer<T> comparer)
|
||||
{
|
||||
for (var arrayIdx = 0; arrayIdx < arrays.Count; arrayIdx++)
|
||||
for (var itemIdx = 0; itemIdx < itemsCount; itemIdx++)
|
||||
{
|
||||
var array = arrays[arrayIdx];
|
||||
Span<T> arrayMemory = array.AsSpan();
|
||||
for (int memoryIdx = 0; memoryIdx < arrayMemory.Length; memoryIdx++)
|
||||
var item = arrays[itemIdx];
|
||||
if (comparer.Equals(itemToFind, item))
|
||||
{
|
||||
if (comparer.Equals(arrayMemory[memoryIdx], item))
|
||||
{
|
||||
return (arrayIdx * (long)blockSize) + memoryIdx;
|
||||
}
|
||||
return itemIdx;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static RecyclableList<T> ToRecyclableList<T>(this IList<T> values, int blockSize = RecyclableDefaults.BlockSize) => new(values, blockSize);
|
||||
//[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
//public static RecyclableList<T> ToRecyclableList<T>(this IList<T> values, int blockSize = RecyclableDefaults.BlockSize) => new(values, blockSize);
|
||||
|
||||
//[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
//public static RecyclableList<T> ToRecyclableList<T>(this IEnumerable<T> values, int blockSize = RecyclableDefaults.BlockSize) => new(values, blockSize);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static RecyclableList<T> ToRecyclableList<T>(this IEnumerable<T> values, int blockSize = RecyclableDefaults.BlockSize) => new(values, blockSize);
|
||||
public static T[] RentArrayFromPool<T>(this int minSize) => (minSize >= _minPooledArraySize)
|
||||
? ArrayPool<T>.Shared.Rent(minSize)
|
||||
: new T[minSize];
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static T[] RentArrayFromPool<T>(this int minSize) => RentArrayFromPool<T>((long)minSize);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
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]
|
||||
};
|
||||
}
|
||||
public static T[] RentArrayFromPool<T>(this long minSize) => (minSize is >= _minPooledArraySize and <= int.MaxValue)
|
||||
? ArrayPool<T>.Shared.Rent((int)minSize)
|
||||
: new T[minSize];
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void ReturnToPool<T>(this T[] array)
|
||||
{
|
||||
long arrayLength = array.LongLength;
|
||||
if (arrayLength is >= _minPooledArraySize and <= int.MaxValue)
|
||||
if (array.LongLength is > _minPooledArraySize and <= int.MaxValue)
|
||||
{
|
||||
ArrayPool<T>.Shared.Return(array);
|
||||
}
|
||||
}
|
||||
|
||||
//public static void AddRange<T>(this RecyclableList<T> destination, IEnumerable<T> source)
|
||||
//{
|
||||
// foreach (var item in source)
|
||||
// {
|
||||
// destination.Add(item);
|
||||
// }
|
||||
//}
|
||||
|
||||
public static long LimitTo(this long value, int limit)
|
||||
=> (value <= limit) ? value : limit;
|
||||
|
||||
public static int IndexOf<T>(this T[] memory, int 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ namespace Recyclable.Collections
|
||||
private static readonly IEqualityComparer<T> _equalityComparer = EqualityComparer<T>.Default;
|
||||
|
||||
private bool _disposedValue;
|
||||
private RecyclableBlockList<T[]> Memory { get; }
|
||||
private RecyclableArrayList<T[]> Memory { get; }
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static T Dequeue(RecyclableQueue<T> queue)
|
||||
|
||||
Reference in new issue
Block a user