From 743e88f0e67f2ce7b317e5cd16df345cf1616062 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Lema=C5=84czyk?= Date: Sun, 5 Feb 2023 19:49:28 +0100 Subject: [PATCH] RecyclableList.Contains & .IndexOf fixed. Tests modified to validate operations on multiple blocks. --- .../RecyclableCollectionsBenchmarks.List.cs | 4 +- ...bleCollectionsBenchmarks.RecyclableList.cs | 4 +- Recyclable.Collections/RecyclableList.cs | 70 +++++++++---------- .../RecyclableListTests.cs | 2 +- 4 files changed, 40 insertions(+), 40 deletions(-) diff --git a/Recyclable.Collections.Benchmarks/RecyclableCollectionsBenchmarks.List.cs b/Recyclable.Collections.Benchmarks/RecyclableCollectionsBenchmarks.List.cs index 1d4b6e5..d1c3a12 100644 --- a/Recyclable.Collections.Benchmarks/RecyclableCollectionsBenchmarks.List.cs +++ b/Recyclable.Collections.Benchmarks/RecyclableCollectionsBenchmarks.List.cs @@ -117,7 +117,7 @@ namespace Recyclable.Collections.Benchmarks { var data = TestObjects; var list = TestObjectsAsList; - var dataCount = TestObjectCount; + var dataCount = 100_000; for (var i = 0; i < dataCount; i++) { DoNothing(list.Contains(data[i])); @@ -129,7 +129,7 @@ namespace Recyclable.Collections.Benchmarks { var data = TestObjects; var list = TestObjectsAsList; - var dataCount = TestObjectCount; + var dataCount = 100_000; for (var i = 0; i < dataCount; i++) { DoNothing(list.IndexOf(data[i])); diff --git a/Recyclable.Collections.Benchmarks/RecyclableCollectionsBenchmarks.RecyclableList.cs b/Recyclable.Collections.Benchmarks/RecyclableCollectionsBenchmarks.RecyclableList.cs index 1bf5c35..b39689b 100644 --- a/Recyclable.Collections.Benchmarks/RecyclableCollectionsBenchmarks.RecyclableList.cs +++ b/Recyclable.Collections.Benchmarks/RecyclableCollectionsBenchmarks.RecyclableList.cs @@ -83,7 +83,7 @@ namespace Recyclable.Collections.Benchmarks { var data = TestObjects; var list = TestObjectsAsRecyclableList; - var dataCount = TestObjectCount; + var dataCount = 100_000; for (var i = 0; i < dataCount; i++) { DoNothing(list.Contains(data[i])); @@ -95,7 +95,7 @@ namespace Recyclable.Collections.Benchmarks { var data = TestObjects; var list = TestObjectsAsRecyclableList; - var dataCount = TestObjectCount; + var dataCount = 100_000; for (var i = 0; i < dataCount; i++) { DoNothing(list.IndexOf(data[i])); diff --git a/Recyclable.Collections/RecyclableList.cs b/Recyclable.Collections/RecyclableList.cs index b650aa9..8bb4db8 100644 --- a/Recyclable.Collections/RecyclableList.cs +++ b/Recyclable.Collections/RecyclableList.cs @@ -518,7 +518,7 @@ namespace Recyclable.Collections int blockSize = _blockSize; int itemIndex = 0; int blockIndex = 0; - int lastBlockIndex = 0; + int lastBlockIndex = _lastBlockIndex; Span memoryBlocksSpan = new(_memoryBlocks); Span blockArraySpan = new(memoryBlocksSpan[blockIndex]); @@ -740,7 +740,7 @@ namespace Recyclable.Collections int blockSize = _blockSize; int itemIndex = 0; int blockIndex = 0; - int lastBlockIndex = 0; + int lastBlockIndex = _lastBlockIndex; Span memoryBlocksSpan = new(_memoryBlocks); Span blockArraySpan = new(memoryBlocksSpan[blockIndex]); @@ -750,42 +750,42 @@ namespace Recyclable.Collections { if (item.Equals(blockArraySpan[itemIndex])) { - return itemIndex; + return (blockIndex * blockSize) + itemIndex; } if (itemIndex + 1 < blockSize && item.Equals(blockArraySpan[itemIndex + 1])) { - return itemIndex + 1; + return (blockIndex * blockSize) + itemIndex + 1; } if (itemIndex + 2 < blockSize && item.Equals(blockArraySpan[itemIndex + 2])) { - return itemIndex + 2; + return (blockIndex * blockSize) + itemIndex + 2; } if (itemIndex + 3 < blockSize && item.Equals(blockArraySpan[itemIndex + 3])) { - return itemIndex + 3; + return (blockIndex * blockSize) + itemIndex + 3; } if (itemIndex + 4 < blockSize && item.Equals(blockArraySpan[itemIndex + 4])) { - return itemIndex + 4; + return (blockIndex * blockSize) + itemIndex + 4; } if (itemIndex + 5 < blockSize && item.Equals(blockArraySpan[itemIndex + 5])) { - return itemIndex + 5; + return (blockIndex * blockSize) + itemIndex + 5; } if (itemIndex + 6 < blockSize && item.Equals(blockArraySpan[itemIndex + 6])) { - return itemIndex + 6; + return (blockIndex * blockSize) + itemIndex + 6; } if (itemIndex + 7 < blockSize && item.Equals(blockArraySpan[itemIndex + 7])) { - return itemIndex + 7; + return (blockIndex * blockSize) + itemIndex + 7; } itemIndex += 8; @@ -797,48 +797,48 @@ namespace Recyclable.Collections } } - blockArraySpan = new(memoryBlocksSpan[lastBlockIndex]); + blockArraySpan = new(memoryBlocksSpan[blockIndex]); var nextItemIndex = _nextItemIndex; while (itemIndex < nextItemIndex) { if (item.Equals(blockArraySpan[itemIndex])) { - return itemIndex; + return (blockIndex * blockSize) + itemIndex; } if (itemIndex + 1 < nextItemIndex && item.Equals(blockArraySpan[itemIndex + 1])) { - return itemIndex + 1; + return (blockIndex * blockSize) + itemIndex + 1; } if (itemIndex + 2 < nextItemIndex && item.Equals(blockArraySpan[itemIndex + 2])) { - return itemIndex + 2; + return (blockIndex * blockSize) + itemIndex + 2; } if (itemIndex + 3 < nextItemIndex && item.Equals(blockArraySpan[itemIndex + 3])) { - return itemIndex + 3; + return (blockIndex * blockSize) + itemIndex + 3; } if (itemIndex + 4 < nextItemIndex && item.Equals(blockArraySpan[itemIndex + 4])) { - return itemIndex + 4; + return (blockIndex * blockSize) + itemIndex + 4; } if (itemIndex + 5 < nextItemIndex && item.Equals(blockArraySpan[itemIndex + 5])) { - return itemIndex + 5; + return (blockIndex * blockSize) + itemIndex + 5; } if (itemIndex + 6 < nextItemIndex && item.Equals(blockArraySpan[itemIndex + 6])) { - return itemIndex + 6; + return (blockIndex * blockSize) + itemIndex + 6; } if (itemIndex + 7 < nextItemIndex && item.Equals(blockArraySpan[itemIndex + 7])) { - return itemIndex + 7; + return (blockIndex * blockSize) + itemIndex + 7; } itemIndex += 8; @@ -850,42 +850,42 @@ namespace Recyclable.Collections { if (blockArraySpan[itemIndex] == null) { - return itemIndex; + return (blockIndex * blockSize) + itemIndex; } if (itemIndex + 1 < blockSize && blockArraySpan[itemIndex + 1] == null) { - return itemIndex + 1; + return (blockIndex * blockSize) + itemIndex + 1; } if (itemIndex + 2 < blockSize && blockArraySpan[itemIndex + 2] == null) { - return itemIndex + 2; + return (blockIndex * blockSize) + itemIndex + 2; } if (itemIndex + 3 < blockSize && blockArraySpan[itemIndex + 3] == null) { - return itemIndex + 3; + return (blockIndex * blockSize) + itemIndex + 3; } if (itemIndex + 4 < blockSize && blockArraySpan[itemIndex + 4] == null) { - return itemIndex + 4; + return (blockIndex * blockSize) + itemIndex + 4; } if (itemIndex + 5 < blockSize && blockArraySpan[itemIndex + 5] == null) { - return itemIndex + 5; + return (blockIndex * blockSize) + itemIndex + 5; } if (itemIndex + 6 < blockSize && blockArraySpan[itemIndex + 6] == null) { - return itemIndex + 6; + return (blockIndex * blockSize) + itemIndex + 6; } if (itemIndex + 7 < blockSize && blockArraySpan[itemIndex + 7] == null) { - return itemIndex + 7; + return (blockIndex * blockSize) + itemIndex + 7; } itemIndex += 8; @@ -903,42 +903,42 @@ namespace Recyclable.Collections { if (blockArraySpan[itemIndex] == null) { - return itemIndex; + return (blockIndex * blockSize) + itemIndex; } if (itemIndex + 1 < nextItemIndex && blockArraySpan[itemIndex + 1] == null) { - return itemIndex + 1; + return (blockIndex * blockSize) + itemIndex + 1; } if (itemIndex + 2 < nextItemIndex && blockArraySpan[itemIndex + 2] == null) { - return itemIndex + 2; + return (blockIndex * blockSize) + itemIndex + 2; } if (itemIndex + 3 < nextItemIndex && blockArraySpan[itemIndex + 3] == null) { - return itemIndex + 3; + return (blockIndex * blockSize) + itemIndex + 3; } if (itemIndex + 4 < nextItemIndex && blockArraySpan[itemIndex + 4] == null) { - return itemIndex + 4; + return (blockIndex * blockSize) + itemIndex + 4; } if (itemIndex + 5 < nextItemIndex && blockArraySpan[itemIndex + 5] == null) { - return itemIndex + 5; + return (blockIndex * blockSize) + itemIndex + 5; } if (itemIndex + 6 < nextItemIndex && blockArraySpan[itemIndex + 6] == null) { - return itemIndex + 6; + return (blockIndex * blockSize) + itemIndex + 6; } if (itemIndex + 7 < nextItemIndex && blockArraySpan[itemIndex + 7] == null) { - return itemIndex + 7; + return (blockIndex * blockSize) + itemIndex + 7; } itemIndex += 8; diff --git a/Recyclable.CollectionsTests/RecyclableListTests.cs b/Recyclable.CollectionsTests/RecyclableListTests.cs index 9a1fdc4..e89378a 100644 --- a/Recyclable.CollectionsTests/RecyclableListTests.cs +++ b/Recyclable.CollectionsTests/RecyclableListTests.cs @@ -8,7 +8,7 @@ namespace Recyclable.CollectionsTests private const int _totalObjectCount = 5_000; private static RecyclableList NewRecyclableList => CreateReversedRecyclableList(_testData); private static readonly IEnumerable _testData = Enumerable.Range(1, _totalObjectCount); - private static RecyclableList CreateReversedRecyclableList(IEnumerable source) => new(source); + private static RecyclableList CreateReversedRecyclableList(IEnumerable source) => new(source, 1024); [Fact] public void AddShouldAddItems()