diff --git a/Recyclable.Collections.Compatibility.List/RecyclableLongList.Compatibility.List.cs b/Recyclable.Collections.Compatibility.List/RecyclableLongList.Compatibility.List.cs index d52303f..60055dc 100644 --- a/Recyclable.Collections.Compatibility.List/RecyclableLongList.Compatibility.List.cs +++ b/Recyclable.Collections.Compatibility.List/RecyclableLongList.Compatibility.List.cs @@ -395,7 +395,7 @@ namespace Recyclable.Collections RecyclableLongList result = new(minBlockSize: blockSize, initialCapacity: count); ReadOnlySpan sourceMemoryBlocksSpan = new (list._memoryBlocks), targetMemoryBlocksSpan = new (result._memoryBlocks); - ReadOnlySpan sourceMemoryBlockSpan = sourceMemoryBlocksSpan[0]; + ReadOnlySpan sourceMemoryBlockSpan = sourceMemoryBlocksSpan[blockIndex]; Span targetMemoryBlockSpan = new(targetMemoryBlocksSpan[0]); while (blockIndex < lastBlockWithData) { @@ -439,6 +439,11 @@ namespace Recyclable.Collections while (itemIndex < lastBlockWithData) { targetMemoryBlockSpan[targetItemIndex] = sourceMemoryBlockSpan[itemIndex]; + if (itemIndex + 1 == lastBlockWithData) + { + break; + } + itemIndex++; if (targetItemIndex + 1 == blockSize) { @@ -456,9 +461,9 @@ namespace Recyclable.Collections } } - result._lastBlockWithData = lastBlockWithData; - result._longCount = count; result._nextItemIndex = count & list._blockSizeMinus1; + result._lastBlockWithData = result._nextItemIndex > 0 ? targetBlockIndex : targetBlockIndex - 1; + result._longCount = count; result._nextItemBlockIndex = targetBlockIndex; return result; }