RecyclableList.RecyclableList.AddRange(RecyclableArrayList<T> items) fixed to break the loop when we reach the last block.

This commit is contained in:
Marek Lemańczyk committed 2023-03-11 19:38:50 +01:00
1 parent a7d6a9b7e5
commit 44c8286f85
1 file changed
+10 -2
+10 -2
View File
@@ -315,15 +315,23 @@ namespace Recyclable.Collections
_ = EnsureCapacity(targetCapacity); _ = EnsureCapacity(targetCapacity);
} }
int blockSize = _blockSize, targetBlockIndex = _lastBlockIndex; int blockSize = _blockSize,
targetBlockIndex = _lastBlockIndex,
memoryBlockCount = _memoryBlocks.Length;
Span<T> itemsSpan = new(items.AsArray(), 0, items.Count); Span<T> itemsSpan = new(items.AsArray(), 0, items.Count);
Span<T[]> memoryBlocksSpan = new(_memoryBlocks, 0, _memoryBlocks.Length); Span<T[]> memoryBlocksSpan = new(_memoryBlocks, 0, memoryBlockCount);
Span<T> targetBlockArraySpan = new(memoryBlocksSpan[targetBlockIndex], _nextItemIndex, blockSize - _nextItemIndex); Span<T> targetBlockArraySpan = new(memoryBlocksSpan[targetBlockIndex], _nextItemIndex, blockSize - _nextItemIndex);
while (targetBlockArraySpan.Length <= itemsSpan.Length) while (targetBlockArraySpan.Length <= itemsSpan.Length)
{ {
itemsSpan[..targetBlockArraySpan.Length].CopyTo(targetBlockArraySpan); itemsSpan[..targetBlockArraySpan.Length].CopyTo(targetBlockArraySpan);
itemsSpan = itemsSpan[targetBlockArraySpan.Length..]; itemsSpan = itemsSpan[targetBlockArraySpan.Length..];
targetBlockIndex++; targetBlockIndex++;
if (targetBlockIndex == memoryBlockCount)
{
break;
}
targetBlockArraySpan = new(memoryBlocksSpan[targetBlockIndex], 0, blockSize); targetBlockArraySpan = new(memoryBlocksSpan[targetBlockIndex], 0, blockSize);
} }