From 878ec4924a34f785ba41b4e162fcf944e9c776de Mon Sep 17 00:00:00 2001 From: mlemanczyk Date: Sat, 14 Jan 2023 20:31:57 +0100 Subject: [PATCH] RecyclableArrayList.Insert failing test fixed. Tiny cleanup. --- Recyclable.Collections/RecyclableArrayList.cs | 26 +++++++------------ 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/Recyclable.Collections/RecyclableArrayList.cs b/Recyclable.Collections/RecyclableArrayList.cs index 436aa53..27fa4ea 100644 --- a/Recyclable.Collections/RecyclableArrayList.cs +++ b/Recyclable.Collections/RecyclableArrayList.cs @@ -162,12 +162,6 @@ namespace Recyclable.Collections } items.CopyTo(_memory, _count); - //Span targetSpan = new(_memory); - //for (int sourceItemIdx = 0, targetItemIdx = _count; sourceItemIdx < sourceItemsCount;) - //{ - // targetSpan[targetItemIdx++] = items[sourceItemIdx++]; - //} - _count = targetCapacity; } @@ -183,11 +177,6 @@ namespace Recyclable.Collections var targetSpan = new Span(_memory)[_count..]; Span itemsSpan = new(items._memory); itemsSpan.CopyTo(targetSpan); - //for (int sourceItemIdx = 0, targetItemIdx = _count; sourceItemIdx < sourceItemsCount;) - //{ - // targetSpan[targetItemIdx++] = itemsSpan[sourceItemIdx++]; - //} - // _count = targetCapacity; } @@ -237,16 +226,21 @@ namespace Recyclable.Collections public void Insert(int index, T item) { - int requestedCapacity = _count + 1; + int oldCount = _count; + int requestedCapacity = oldCount + 1; if (_capacity < requestedCapacity) { _ = EnsureCapacity(requestedCapacity); } - var sourceSpan = new Span(_memory)[index..]; - var targetSpan = new Span(_memory)[(index + 1)..]; - sourceSpan.CopyTo(targetSpan); - sourceSpan[index] = item; + if (oldCount > 0) + { + var sourceSpan = new Span(_memory)[index..oldCount]; + var targetSpan = new Span(_memory)[(index + 1)..]; + sourceSpan.CopyTo(targetSpan); + } + + _memory[index] = item; _count++; }