Several hints removed & code cleaned up.

This commit is contained in:
mlemanczyk committed 2023-05-01 17:45:03 +02:00
1 parent b038bcfdc9
commit 52dc663ada
26 files changed
+107 -139

No files matched your search

+7 -7
View File
@@ -5,13 +5,13 @@ namespace Recyclable.Collections
{
public static class MathUtils
{
private static readonly long[] Pow2 = new long[]
{
1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384,
32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304,
8388608, 16777216, 33554432, 67108864, 134217728, 268435456,
536870912, 1073741824, 2147483648
};
// private static readonly long[] Pow2 = new long[]
// {
// 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384,
// 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304,
// 8388608, 16777216, 33554432, 67108864, 134217728, 268435456,
// 536870912, 1073741824, 2147483648
// };
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsPow2(long value) => (value != 0) && ((value & (value - 1)) == 0);
+20 -24
View File
@@ -8,7 +8,6 @@ namespace Recyclable.Collections
{
private const int ItemNotFound = -1;
private static readonly ArrayPool<T> _arrayPool = ArrayPool<T>.Create();
private static readonly IEqualityComparer<T> _equalityComparer = EqualityComparer<T>.Default;
protected static readonly bool NeedsClearing = !typeof(T).IsValueType;
@@ -129,7 +128,7 @@ namespace Recyclable.Collections
}
protected int _capacity;
public int Capacity
public int Capacity
{
get => _capacity;
set
@@ -340,30 +339,26 @@ namespace Recyclable.Collections
if (enumerator.MoveNext())
{
int available = capacity - targetItemIdx;
while (true)
{
if (targetItemIdx + growByCount > capacity)
{
do
{
if (targetItemIdx + growByCount > capacity)
{
capacity = EnsureCapacity(capacity + growByCount);
memorySpan = new(_memoryBlock);
available = capacity - targetItemIdx;
}
memorySpan = new(_memoryBlock);
available = capacity - targetItemIdx;
}
for (i = 0; i < available; i++)
{
memorySpan[targetItemIdx++] = enumerator.Current;
if (!enumerator.MoveNext())
{
break;
}
}
if (i < available)
{
break;
}
}
}
for (i = 0; i < available; i++)
{
memorySpan[targetItemIdx++] = enumerator.Current;
if (!enumerator.MoveNext())
{
break;
}
}
}
while (i >= available);
}
_count = targetItemIdx;
}
@@ -459,6 +454,7 @@ namespace Recyclable.Collections
}
public IEnumerator<T> GetEnumerator() => Enumerate(this).GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => Enumerate(this).GetEnumerator();
public void Dispose()
+30 -30
View File
@@ -373,10 +373,11 @@ namespace Recyclable.Collections
/// <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>
/// <remarks>For performance reasons, <paramref name="minBlockSize"/> MUST a power of 2. This simplifies a lot block & item
/// <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.
///
/// This method checks for integral overflow.
/// </para>
/// <para>This method checks for integral overflow.</para>
/// </remarks>
/// <remarks>
/// This method doesn't support downsizing the memory block. As such it doesn't release excessive blocks. This is to for additional performance
@@ -426,35 +427,35 @@ namespace Recyclable.Collections
}
blockIndex = sourceBlockCount;
do
{
newMemoryBlocks[blockIndex] = blockArrayPool.Rent(minBlockSize);
if (blockIndex + 1 < requiredBlockCount)
{
blockIndex++;
}
else
{
break;
}
} while (true);
}
while (true)
{
newMemoryBlocks[blockIndex] = blockArrayPool.Rent(minBlockSize);
if (blockIndex + 1 < requiredBlockCount)
{
blockIndex++;
}
else
{
break;
}
}
}
else
{
blockIndex = sourceBlockCount;
do
{
newMemoryBlocks[blockIndex] = new T[minBlockSize];
if (blockIndex + 1 < requiredBlockCount)
{
blockIndex++;
}
else
{
break;
}
} while (true);
}
while (true)
{
newMemoryBlocks[blockIndex] = new T[minBlockSize];
if (blockIndex + 1 < requiredBlockCount)
{
blockIndex++;
}
else
{
break;
}
}
}
}
list._reservedBlockCount = requiredBlockCount;
@@ -742,7 +743,6 @@ namespace Recyclable.Collections
{
_nextItemIndex = checked((int)(targetCapacity & _blockSizeMinus1));
_nextItemBlockIndex = targetBlockIndex;
}
_longCount = targetCapacity;
@@ -112,7 +112,7 @@ namespace Recyclable.Collections
return scannedCount;
}
scannedCount++;
scannedCount++;
if (scannedCount == itemsCount)
{
return -1;
+1 -6
View File
@@ -193,19 +193,14 @@ namespace Recyclable.Collections
}
}
protected void Dispose(bool disposing)
public void Dispose()
{
if (!_disposedValue)
{
Clear();
_disposedValue = true;
}
}
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
@@ -27,7 +27,7 @@
QuickSort();
}
public RecyclableSortedList(int blockSize = RecyclableDefaults.BlockSize)
public RecyclableSortedList(int blockSize = RecyclableDefaults.BlockSize)
: base(blockSize)
{
}
@@ -73,7 +73,7 @@
finally
{
EndUpdate(false);
}
}
}
}
}