RecyclableList renamed to RecyclableLongList.

This commit is contained in:
mlemanczyk committed 2023-05-21 20:09:38 +02:00
1 parent 0894dbd4bf
commit 9429218c62
45 files changed
+576 -532

No files matched your search

@@ -249,7 +249,7 @@ namespace Recyclable.Collections
}
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
public void AddRange(RecyclableList<T> items)
public void AddRange(RecyclableLongList<T> items)
{
if (items.LongCount == 0)
{
@@ -259,12 +259,12 @@ namespace Recyclable.Collections
var sourceItemsCount = items.LongCount;
if (sourceItemsCount > int.MaxValue)
{
ThrowArgumentOutOfRangeException($"The number of items exceeds the maximum capacity of {nameof(RecyclableArrayList<T>)}, equal {int.MaxValue}, equal {int.MaxValue}. Please consider using {nameof(RecyclableList<T>)}, instead");
ThrowArgumentOutOfRangeException($"The number of items exceeds the maximum capacity of {nameof(RecyclableArrayList<T>)}, equal {int.MaxValue}, equal {int.MaxValue}. Please consider using {nameof(RecyclableLongList<T>)}, instead");
}
if (_count + sourceItemsCount > int.MaxValue)
{
ThrowArgumentOutOfRangeException($"The total number of items in source and target table exceeds the maximum capacity of {nameof(RecyclableArrayList<T>)}, equal {int.MaxValue}. Please consider using {nameof(RecyclableList<T>)}, instead");
ThrowArgumentOutOfRangeException($"The total number of items in source and target table exceeds the maximum capacity of {nameof(RecyclableArrayList<T>)}, equal {int.MaxValue}. Please consider using {nameof(RecyclableLongList<T>)}, instead");
}
int targetCapacity = _count + (int)sourceItemsCount;
@@ -3,14 +3,14 @@ using Recyclable.Collections.Parallel;
namespace Recyclable.Collections
{
public partial class RecyclableList<T>
public partial class RecyclableLongList<T>
{
public static class IndexOfHelpers
{
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
// [MethodImpl(MethodImplOptions.AggressiveInlining)]
// [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.NoOptimization)]
private static void ScheduleIndexOfTask(IndexOfSynchronizationContext context, RecyclableList<T> list, int blockIndex, T itemToFind)
private static void ScheduleIndexOfTask(IndexOfSynchronizationContext context, RecyclableLongList<T> list, int blockIndex, T itemToFind)
{
_ = context.AllDoneSignal.AddParticipant();
// & WAS SLOWER
@@ -123,7 +123,7 @@ namespace Recyclable.Collections
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
// [MethodImpl(MethodImplOptions.AggressiveInlining)]
// [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.NoOptimization)]
private static void ScheduleIndexOfTaskSingle(IndexOfSynchronizationContext context, RecyclableList<T> list, int blockIndex, T itemToFind)
private static void ScheduleIndexOfTaskSingle(IndexOfSynchronizationContext context, RecyclableLongList<T> list, int blockIndex, T itemToFind)
{
_ = context.AllDoneSignal.AddParticipant();
// & WAS SLOWER
@@ -162,7 +162,7 @@ namespace Recyclable.Collections
private const int BlocksForEachTask = 4;
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
public static long IndexOfParallel(RecyclableList<T> list, in T item)
public static long IndexOfParallel(RecyclableLongList<T> list, in T item)
{
// & WAS SLOWER
//long itemsCount = Array.IndexOf(list._memoryBlocks[0], item, 0, (int)Math.Min(list._blockSize, list._longCount));
@@ -214,25 +214,43 @@ namespace Recyclable.Collections
}
}
int itemIndex = Array.IndexOf(list._memoryBlocks[list._lastBlockWithData], item, 0, list._nextItemIndex > 0 ? list._nextItemIndex : list._blockSize);
if (itemIndex >= 0)
if (!context._isItemFound)
{
context._isItemFound = true;
_ = Interlocked.Exchange(ref context.FoundItemIndex, ((long)blockIndex << list._blockSizePow2BitShift) + itemIndex);
int itemIndex = Array.IndexOf(list._memoryBlocks[list._lastBlockWithData], item, 0, list._nextItemIndex > 0 ? list._nextItemIndex : list._blockSize);
if (!context._isItemFound && itemIndex >= 0)
{
context._isItemFound = true;
_ = Interlocked.Exchange(ref context.FoundItemIndex, ((long)blockIndex << list._blockSizePow2BitShift) + itemIndex);
}
}
context.AllDoneSignal.SignalAndWait();
if (context.Exception == null)
{
return context.FoundItemIndex;
try
{
return context.FoundItemIndex;
}
finally
{
context.Dispose();
}
}
context.Exception.CaptureAndRethrow();
try
{
context.Exception.CaptureAndRethrow();
}
finally
{
context.Dispose();
}
return RecyclableDefaults.ItemNotFoundIndexLong;
}
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
public static long IndexOfSequential(RecyclableList<T> list, in T item) //, in T[][] memoryBlocks, int lastBlockIndex, int blockSize, int nextItemIndex)
public static long IndexOfSequential(RecyclableLongList<T> list, in T item) //, in T[][] memoryBlocks, int lastBlockIndex, int blockSize, int nextItemIndex)
{
int itemIndex;
// & WAS SLOWER
@@ -8,7 +8,7 @@ using Recyclable.Collections.Pools;
namespace Recyclable.Collections
{
public partial class RecyclableList<T> : IDisposable, IList<T>
public partial class RecyclableLongList<T> : IDisposable, IList<T>
{
private static readonly ArrayPool<T[]> _defaultMemoryBlocksPool = ArrayPool<T[]>.Create();
private static readonly ArrayPool<T> _defaultBlockArrayPool = ArrayPool<T>.Create();
@@ -55,7 +55,7 @@ namespace Recyclable.Collections
public int NextItemIndex => _nextItemIndex;
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
private static void CopyItems(RecyclableList<T> list, long index)
private static void CopyItems(RecyclableLongList<T> list, long index)
{
T[][] memoryBlocks = list._memoryBlocks;
int nextItemIndex = list._nextItemIndex;
@@ -219,10 +219,10 @@ namespace Recyclable.Collections
/// <summary>
/// Creates a set of new memory buffers, if needed, to allow storing at minimum <paramref name="newCapacity"/> no. of items.
/// </summary>
/// <param name="list"><see cref="RecyclableList{T}"/> that needs to be resized.</param>
/// <param name="list"><see cref="RecyclableLongList{T}"/> that needs to be resized.</param>
/// <param name="minBlockSize">Minimal requested block size. It MUST be rounded to the power of 2, see remarks.</param>
/// <param name="minBlockSizePow2Shift">Pre-calculated bit shifting value for left & right shift operations against<paramref name="minBlockSize"/>.</param>
/// <param name="newCapacity">The minimum no. of items <paramref name="list"/> MUST be able to store after <see cref="RecyclableList{T}.Resize(RecyclableList{T}, int, byte, long)"/>.</param>
/// <param name="newCapacity">The minimum no. of items <paramref name="list"/> MUST be able to store after <see cref="RecyclableLongList{T}.Resize(RecyclableLongList{T}, int, byte, long)"/>.</param>
/// <remarks><para>
/// For performance reasons, <paramref name="minBlockSize"/> MUST a power of 2. This simplifies a lot block & item
/// index calculations, i.e. makes them logical operations on bits.
@@ -235,7 +235,7 @@ namespace Recyclable.Collections
/// </remarks>
/// <returns>The maximum no. of items <paramref name="list"/> can store.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
protected static long Resize(RecyclableList<T> list, int minBlockSize, byte minBlockSizePow2Shift, long newCapacity)
protected static long Resize(RecyclableLongList<T> list, int minBlockSize, byte minBlockSizePow2Shift, long newCapacity)
{
ArrayPool<T> blockArrayPool = list._blockArrayPool;
int sourceBlockCount = list._reservedBlockCount;
@@ -329,7 +329,7 @@ namespace Recyclable.Collections
// [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.NoOptimization)]
// & WAS SLOWER
// [MethodImpl(MethodImplOptions.NoInlining)]
//private static void Iterate(RecyclableList<T> list, in ParallelSynchronizationContext context, in T itemToFind)
//private static void Iterate(RecyclableLongList<T> list, in ParallelSynchronizationContext context, in T itemToFind)
//{
// // TODO: Switch to use _blockSize instead of list._longCountIndeexOfStep for simplifications & better performance.
@@ -357,7 +357,7 @@ namespace Recyclable.Collections
//}
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
protected static void SetupBlockArrayPooling(RecyclableList<T> list, int blockSize, ArrayPool<T>? blockArrayPool = null)
protected static void SetupBlockArrayPooling(RecyclableLongList<T> list, int blockSize, ArrayPool<T>? blockArrayPool = null)
{
list._blockSize = blockSize;
list._blockSizePow2BitShift = (byte)(31 - BitOperations.LeadingZeroCount((uint)blockSize));
@@ -367,7 +367,7 @@ namespace Recyclable.Collections
}
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
protected static long EnsureCapacity(RecyclableList<T> list, long requestedCapacity)
protected static long EnsureCapacity(RecyclableLongList<T> list, long requestedCapacity)
{
long newCapacity = list._capacity > 0
? checked((long)BitOperations.RoundUpToPowerOf2((ulong)requestedCapacity))
@@ -387,7 +387,7 @@ namespace Recyclable.Collections
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
#pragma warning disable CS8618 // It's set by SetupBlockArrayPooling
public RecyclableList(int minBlockSize = RecyclableDefaults.BlockSize, long? expectedItemsCount = default, ArrayPool<T[]>? memoryBlocksPool = default, ArrayPool<T>? blockArrayPool = default)
public RecyclableLongList(int minBlockSize = RecyclableDefaults.BlockSize, long? expectedItemsCount = default, ArrayPool<T[]>? memoryBlocksPool = default, ArrayPool<T>? blockArrayPool = default)
#pragma warning restore CS8618
{
_memoryBlocksPool = memoryBlocksPool ?? _defaultMemoryBlocksPool;
@@ -416,7 +416,7 @@ namespace Recyclable.Collections
}
#pragma warning disable CS8618 // It's set by SetupBlockArrayPooling
public RecyclableList(IEnumerable<T> source, int minBlockSize = RecyclableDefaults.BlockSize, long? expectedItemsCount = default, ArrayPool<T[]>? memoryBlocksPool = default, ArrayPool<T>? blockArrayPool = default)
public RecyclableLongList(IEnumerable<T> source, int minBlockSize = RecyclableDefaults.BlockSize, long? expectedItemsCount = default, ArrayPool<T[]>? memoryBlocksPool = default, ArrayPool<T>? blockArrayPool = default)
#pragma warning restore CS8618
{
_memoryBlocksPool = memoryBlocksPool ?? _defaultMemoryBlocksPool;
@@ -565,7 +565,7 @@ namespace Recyclable.Collections
_lastBlockWithData = targetBlockIndex - (itemsSpan.Length > 0 ? 0 : 1);
}
public void AddRange(RecyclableList<T> items)
public void AddRange(RecyclableLongList<T> items)
{
if (items.LongCount == 0)
{
@@ -741,9 +741,9 @@ namespace Recyclable.Collections
public void AddRange(IEnumerable<T> source, int growByCount = RecyclableDefaults.BlockSize)
{
if (source is RecyclableList<T> sourceRecyclableList)
if (source is RecyclableLongList<T> sourceRecyclableLongList)
{
AddRange(sourceRecyclableList);
AddRange(sourceRecyclableLongList);
return;
}
@@ -1055,7 +1055,7 @@ namespace Recyclable.Collections
IEnumerator IEnumerable.GetEnumerator() => _memoryBlocks.Enumerate(_blockSize, LongCount).GetEnumerator();
~RecyclableList()
~RecyclableLongList()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose();
@@ -3,7 +3,7 @@ using System.Runtime.CompilerServices;
namespace Recyclable.Collections
{
public static class RecyclableListExtensions
public static class RecyclableLongListExtensions
{
private const int _minPooledArraySize = RecyclableDefaults.MinPooledArrayLength;
private const long ItemNotFoundIndexLong = -1L;
@@ -46,7 +46,7 @@ namespace Recyclable.Collections
}
//[MethodImpl(MethodImplOptions.AggressiveInlining)]
//public static bool Contains<T>(this RecyclableList<T[]> arrays, T item) => arrays.Any(x => x.Contains(item));
//public static bool Contains<T>(this RecyclableLongList<T[]> arrays, T item) => arrays.Any(x => x.Contains(item));
public static IEnumerable<T> Enumerate<T>(this T[][] arrays, int chunkSize, long totalCount)
{
@@ -83,7 +83,7 @@ namespace Recyclable.Collections
// => 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)
public static long LongIndexOf<T>(this RecyclableLongList<T> arrays, long itemsCount, T itemToFind, IEqualityComparer<T> comparer)
{
for (var itemIdx = 0L; itemIdx < itemsCount; itemIdx++)
{
@@ -126,10 +126,10 @@ namespace Recyclable.Collections
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RecyclableList<T> ToRecyclableList<T>(this IList<T> values, int blockSize = RecyclableDefaults.BlockSize) => new(values, blockSize);
public static RecyclableLongList<T> ToRecyclableLongList<T>(this IList<T> values, int blockSize = RecyclableDefaults.BlockSize) => new(values, blockSize);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RecyclableList<T> ToRecyclableList<T>(this List<T> values, int blockSize = RecyclableDefaults.BlockSize) => new(values, blockSize);
public static RecyclableLongList<T> ToRecyclableLongList<T>(this List<T> values, int blockSize = RecyclableDefaults.BlockSize) => new(values, blockSize);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RecyclableArrayList<T> ToRecyclableArrayList<T>(this IEnumerable<T> values) => new(values);
@@ -144,7 +144,7 @@ namespace Recyclable.Collections
public static RecyclableArrayList<T> ToRecyclableArrayList<T>(this RecyclableArrayList<T> values) => new(values);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RecyclableList<T> ToRecyclableList<T>(this IEnumerable<T> values, int minBlockSize = RecyclableDefaults.BlockSize) => new(values, minBlockSize: minBlockSize);
public static RecyclableLongList<T> ToRecyclableLongList<T>(this IEnumerable<T> values, int minBlockSize = RecyclableDefaults.BlockSize) => new(values, minBlockSize: minBlockSize);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T[] RentArrayFromPool<T>(this int minSize, ArrayPool<T> arrayPool) => (minSize >= _minPooledArraySize)
@@ -1,6 +1,6 @@
namespace Recyclable.Collections
{
public class RecyclableSortedList<TKey, TValue> : RecyclableList<(TKey Key, TValue Value)>
public class RecyclableSortedList<TKey, TValue> : RecyclableLongList<(TKey Key, TValue Value)>
where TKey : notnull
{
private static readonly IComparer<(TKey, TValue)> _comparer = new FrequencyTupleComparer<TKey, TValue>();
+2 -2
View File
@@ -7,7 +7,7 @@ namespace Recyclable.Collections
private bool _disposedValue;
private readonly int _blockSize;
protected RecyclableList<T> List { get; }
protected RecyclableLongList<T> List { get; }
public int Count => List.Count;
public long LongCount
{
@@ -32,7 +32,7 @@ namespace Recyclable.Collections
List = new(list, blockSize);
}
public RecyclableStack(RecyclableList<T> list, int blockSize = RecyclableDefaults.BlockSize)
public RecyclableStack(RecyclableLongList<T> list, int blockSize = RecyclableDefaults.BlockSize)
{
_blockSize = blockSize;
List = new(list, blockSize);
@@ -16,7 +16,7 @@ namespace Recyclable.Collections
// List = list;
// }
public static SearchConfiguration<RecyclableList<TItem>, TItem> CreateFrom(RecyclableList<TItem> list, in TItem itemToFind) => new()
public static SearchConfiguration<RecyclableLongList<TItem>, TItem> CreateFrom(RecyclableLongList<TItem> list, in TItem itemToFind) => new()
{
List = list,
ItemToFind = itemToFind,