RecyclableCollections.AddRange(List<T> items) fixes to exceptions found by tests.
This commit is contained in:
1 parent
bdf69f300d
commit
ca6ee45bb6
1 file changed
+15
-8
@@ -32,6 +32,7 @@ namespace Recyclable.Collections
|
|||||||
public int Count => (int)_longCount;
|
public int Count => (int)_longCount;
|
||||||
|
|
||||||
protected long _longCount;
|
protected long _longCount;
|
||||||
|
|
||||||
public long LongCount
|
public long LongCount
|
||||||
{
|
{
|
||||||
get => _longCount;
|
get => _longCount;
|
||||||
@@ -552,26 +553,32 @@ namespace Recyclable.Collections
|
|||||||
|
|
||||||
int blockSize = _blockSize,
|
int blockSize = _blockSize,
|
||||||
targetBlockIndex = _lastBlockIndex,
|
targetBlockIndex = _lastBlockIndex,
|
||||||
copied = blockSize - _nextItemIndex,
|
itemsCount = items.Count,
|
||||||
itemsCount = items.Count;
|
copied = Math.Min(blockSize - _nextItemIndex, itemsCount);
|
||||||
|
|
||||||
Span<T[]> memoryBlocksSpan = new(_memoryBlocks, 0, _memoryBlocks.Length);
|
Span<T[]> memoryBlocksSpan = new(_memoryBlocks, 0, _memoryBlocks.Length);
|
||||||
items.CopyTo(0, memoryBlocksSpan[targetBlockIndex++], _nextItemIndex, copied);
|
items.CopyTo(0, memoryBlocksSpan[targetBlockIndex], _nextItemIndex, copied);
|
||||||
|
if (_nextItemIndex + copied == blockSize)
|
||||||
|
{
|
||||||
|
targetBlockIndex++;
|
||||||
|
_nextItemIndex = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_nextItemIndex += copied;
|
||||||
|
}
|
||||||
|
|
||||||
while (blockSize <= itemsCount - copied)
|
while (blockSize <= itemsCount - copied)
|
||||||
{
|
{
|
||||||
items.CopyTo(copied, memoryBlocksSpan[targetBlockIndex++], 0, blockSize);
|
items.CopyTo(copied, memoryBlocksSpan[targetBlockIndex++], 0, blockSize);
|
||||||
copied += blockSize;
|
copied += blockSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (itemsCount - copied != 0)
|
if ((itemsCount - copied < blockSize) && (itemsCount - copied != 0))
|
||||||
{
|
{
|
||||||
_nextItemIndex = itemsCount - copied;
|
_nextItemIndex = itemsCount - copied;
|
||||||
items.CopyTo(copied, memoryBlocksSpan[targetBlockIndex], 0, _nextItemIndex);
|
items.CopyTo(copied, memoryBlocksSpan[targetBlockIndex], 0, _nextItemIndex);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
_nextItemIndex = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
_lastBlockIndex = targetBlockIndex;
|
_lastBlockIndex = targetBlockIndex;
|
||||||
_longCount = targetCapacity;
|
_longCount = targetCapacity;
|
||||||
|
|||||||
Reference in new issue
Block a user