RecyclableArrayList fixes to always stay within the no. of items on the list, not the size of memory block.

This commit is contained in:
mlemanczyk committed 2023-03-24 19:02:45 +01:00
1 parent 97d5d9c6a3
commit 6fc2a10e28
1 file changed
+6 -6
@@ -176,7 +176,7 @@ namespace Recyclable.Collections
_ = EnsureCapacity(targetCapacity); _ = EnsureCapacity(targetCapacity);
} }
var targetSpan = new Span<T>(_memoryBlock)[_count..]; var targetSpan = new Span<T>(_memoryBlock, _count, sourceItemsCount);
Span<T> itemsSpan = new(items); Span<T> itemsSpan = new(items);
itemsSpan.CopyTo(targetSpan); itemsSpan.CopyTo(targetSpan);
_count = targetCapacity; _count = targetCapacity;
@@ -232,8 +232,8 @@ namespace Recyclable.Collections
_ = EnsureCapacity(targetCapacity); _ = EnsureCapacity(targetCapacity);
} }
var targetSpan = new Span<T>(_memoryBlock)[_count..]; var targetSpan = new Span<T>(_memoryBlock, _count, sourceItemsCount);
Span<T> itemsSpan = new(items._memoryBlock); Span<T> itemsSpan = new(items._memoryBlock, 0, sourceItemsCount);
itemsSpan.CopyTo(targetSpan); itemsSpan.CopyTo(targetSpan);
_count = targetCapacity; _count = targetCapacity;
} }
@@ -390,8 +390,8 @@ namespace Recyclable.Collections
public int IndexOf(T itemToFind) public int IndexOf(T itemToFind)
{ {
Span<T> memorySpan = new(_memoryBlock);
int itemCount = _count; int itemCount = _count;
Span<T> memorySpan = new(_memoryBlock, 0, itemCount);
var equalityComparer = _equalityComparer; var equalityComparer = _equalityComparer;
for (var itemIdx = 0; itemIdx < itemCount; itemIdx++) for (var itemIdx = 0; itemIdx < itemCount; itemIdx++)
{ {
@@ -416,8 +416,8 @@ namespace Recyclable.Collections
if (oldCount > 0) if (oldCount > 0)
{ {
var sourceSpan = new Span<T>(_memoryBlock)[index..oldCount]; var sourceSpan = new Span<T>(_memoryBlock, index, oldCount);
var targetSpan = new Span<T>(_memoryBlock)[(index + 1)..]; var targetSpan = new Span<T>(_memoryBlock, index + 1, oldCount);
sourceSpan.CopyTo(targetSpan); sourceSpan.CopyTo(targetSpan);
} }