From b91c4476eb234225a029d902db9599b38a2eca04 Mon Sep 17 00:00:00 2001 From: mlemanczyk Date: Mon, 20 Mar 2023 23:46:50 +0100 Subject: [PATCH] RecyclableList.BlockCount renamed => RecyclableList.ReservedBlockCount. Fixed to always include the last block. RecyclableList.LastTakenBlockIndex property added for some scenarios. RecyclableList.IndexOf & .LongIndexOf fixed to use the correct index. RecyclableArrayList.AddRange(RecyclableList items) fixed to use the correct index. Some test cases excluded, where needed. --- Recyclable.Collections/RecyclableArrayList.cs | 2 +- Recyclable.Collections/RecyclableList.cs | 16 ++++++++-------- Recyclable.Collections/RecyclableStack.cs | 2 +- .../RecyclableListTests.cs | 19 +++++++++++++++++-- 4 files changed, 27 insertions(+), 12 deletions(-) diff --git a/Recyclable.Collections/RecyclableArrayList.cs b/Recyclable.Collections/RecyclableArrayList.cs index 7b83164..0d91bd0 100644 --- a/Recyclable.Collections/RecyclableArrayList.cs +++ b/Recyclable.Collections/RecyclableArrayList.cs @@ -279,7 +279,7 @@ namespace Recyclable.Collections itemsSpan = new(items.MemoryBlocks[blockIndex], 0, (int)sourceItemsCount); itemsSpan.CopyTo(targetSpan); } - else if (blockIndex < items.BlockCount) + else if (blockIndex <= items.LastTakenBlockIndex) { itemsSpan = new(items.MemoryBlocks[blockIndex], 0, items.NextItemIndex); itemsSpan.CopyTo(targetSpan); diff --git a/Recyclable.Collections/RecyclableList.cs b/Recyclable.Collections/RecyclableList.cs index a441572..aac2350 100644 --- a/Recyclable.Collections/RecyclableList.cs +++ b/Recyclable.Collections/RecyclableList.cs @@ -31,16 +31,16 @@ namespace Recyclable.Collections public int Count => checked((int)_longCount); public bool IsReadOnly { get; } + public int LastTakenBlockIndex => _lastBlockIndex - (_nextItemIndex > 0 ? 0 : 1); protected long _longCount; - public long LongCount { get => _longCount; set => _longCount = value; } - public int BlockCount => (int)(_capacity / _blockSize); + public int ReservedBlockCount => (int)(_capacity / _blockSize) + (_capacity % _blockSize > 0 ? 1 : 0); public int BlockSize => _blockSize; public int LastBlockIndex => _lastBlockIndex; public int NextItemIndex => _nextItemIndex; @@ -70,7 +70,7 @@ namespace Recyclable.Collections T[][] memoryBlocks = list._memoryBlocks; if (blockSize >= RecyclableDefaults.MinPooledArrayLength) { - blockArrayPool.Return(memoryBlocks[list.BlockCount - 1]); + blockArrayPool.Return(memoryBlocks[list.ReservedBlockCount - 1]); } list._capacity -= blockSize; @@ -97,7 +97,7 @@ namespace Recyclable.Collections long capacity = _capacity; long available = capacity - copied; - Span memoryBlocksSpan = new(_memoryBlocks, 0, BlockCount); + Span memoryBlocksSpan = new(_memoryBlocks, 0, ReservedBlockCount); Span blockArraySpan; var memoryBlocksCount = memoryBlocksSpan.Length; while (true) @@ -171,7 +171,7 @@ namespace Recyclable.Collections blockSize = _blockSize; } - var memoryBlocksCount = BlockCount; + var memoryBlocksCount = ReservedBlockCount; memoryBlocksSpan = new(_memoryBlocks, 0, memoryBlocksCount); blockArraySpan = new Span(memoryBlocksSpan[targetBlockIdx], 0, blockSize); foreach (var item in source) @@ -698,7 +698,7 @@ namespace Recyclable.Collections { if (_blockSize >= RecyclableDefaults.MinPooledArrayLength) { - Span memoryBlocksSpan = new(_memoryBlocks, 0, BlockCount); + Span memoryBlocksSpan = new(_memoryBlocks, 0, ReservedBlockCount); int memoryBlocksCount = memoryBlocksSpan.Length; ArrayPool blockArrayPool = _blockArrayPool; for (int toRemoveIdx = 0; toRemoveIdx < memoryBlocksCount; toRemoveIdx++) @@ -748,14 +748,14 @@ namespace Recyclable.Collections ? ItemNotFoundIndex : _lastBlockIndex == 0 || (_lastBlockIndex == 1 && _nextItemIndex == 0) ? Array.IndexOf(_memoryBlocks[0], item, 0, (int)_longCount) - : (int)DoIndexOf(item, _memoryBlocks, BlockCount - 1, _blockSize, _nextItemIndex); + : (int)DoIndexOf(item, _memoryBlocks, LastTakenBlockIndex, _blockSize, _nextItemIndex); public void Insert(int index, T item) => throw new NotSupportedException(); public long LongIndexOf(T item) => _longCount == 0 ? ItemNotFoundIndex : _lastBlockIndex == 0 || (_lastBlockIndex == 1 && _nextItemIndex == 0) ? Array.IndexOf(_memoryBlocks[0], item, 0, (int)_longCount) - : DoIndexOf(item, _memoryBlocks, BlockCount - 1, _blockSize, _nextItemIndex); + : DoIndexOf(item, _memoryBlocks, LastTakenBlockIndex, _blockSize, _nextItemIndex); public bool Remove(T item) { diff --git a/Recyclable.Collections/RecyclableStack.cs b/Recyclable.Collections/RecyclableStack.cs index c915341..fb67715 100644 --- a/Recyclable.Collections/RecyclableStack.cs +++ b/Recyclable.Collections/RecyclableStack.cs @@ -49,7 +49,7 @@ namespace Recyclable.Collections LongCount--; if ((List.Capacity * _blockSize) - LongCount == _blockSize) { - List.RemoveBlock(List.BlockCount - 1); + List.RemoveBlock(List.ReservedBlockCount - 1); } return toRemove; diff --git a/Recyclable.CollectionsTests/RecyclableListTests.cs b/Recyclable.CollectionsTests/RecyclableListTests.cs index 9521781..4bc2cca 100644 --- a/Recyclable.CollectionsTests/RecyclableListTests.cs +++ b/Recyclable.CollectionsTests/RecyclableListTests.cs @@ -291,8 +291,13 @@ namespace Recyclable.CollectionsTests [Theory] [MemberData(nameof(RecyclableListTestData.SourceTargetDataVariants), MemberType = typeof(RecyclableListTestData))] - public void RemoveShouldRaiseNotSupportedException(string testCase, IEnumerable testData, long itemsCount, int targetBlockSize) + public void RemoveShouldRaiseNotSupportedExceptionWhenNotLastItem(string testCase, IEnumerable testData, long itemsCount, int targetBlockSize) { + if (itemsCount is 0 or 1) + { + return; + } + // Prepare using var list = new RecyclableList(testData, minBlockSize: targetBlockSize); @@ -302,8 +307,13 @@ namespace Recyclable.CollectionsTests [Theory] [MemberData(nameof(RecyclableListTestData.SourceTargetDataVariants), MemberType = typeof(RecyclableListTestData))] - public void RemoveAtShouldRaiseNotSupportedException(string testCase, IEnumerable testData, long itemsCount, int targetBlockSize) + public void RemoveAtShouldRaiseNotSupportedExceptionWhenNotLastItem(string testCase, IEnumerable testData, long itemsCount, int targetBlockSize) { + if (itemsCount is 0 or 1) + { + return; + } + // Prepare using var list = new RecyclableList(testData, minBlockSize: targetBlockSize); @@ -530,6 +540,11 @@ namespace Recyclable.CollectionsTests [MemberData(nameof(RecyclableListTestData.SourceTargetDataVariants), MemberType = typeof(RecyclableListTestData))] public void LongIndexOfShouldNotFindNonExistingItems(string testCase, IEnumerable testData, long itemsCount, int targetBlockSize) { + if (itemsCount == 0) + { + return; + } + // Prepare using var list = new RecyclableList(testData, minBlockSize: targetBlockSize); _ = testData.Any().Should().BeTrue("we need items on the list that we can look for");