From 02153bea2a3c6dc03d18d6acf34a2756772524cd Mon Sep 17 00:00:00 2001 From: mlemanczyk Date: Sun, 10 Dec 2023 19:53:28 +0100 Subject: [PATCH] RecyclableLongList.Compatibility.List.Exists() implemented. --- ...yclableLongList.Compatibility.List.Find.cs | 61 ++++++++++++++----- 1 file changed, 46 insertions(+), 15 deletions(-) diff --git a/Recyclable.Collections.Compatibility.List/RecyclableLongList.Compatibility.List.Find.cs b/Recyclable.Collections.Compatibility.List/RecyclableLongList.Compatibility.List.Find.cs index 1cf780b..62c8883 100644 --- a/Recyclable.Collections.Compatibility.List/RecyclableLongList.Compatibility.List.Find.cs +++ b/Recyclable.Collections.Compatibility.List/RecyclableLongList.Compatibility.List.Find.cs @@ -8,24 +8,55 @@ namespace Recyclable.Collections { public static bool Exists(this RecyclableLongList list, Predicate match) { - //int sourceItemsCount = list._count; - //if (sourceItemsCount == 0) - //{ - // return false; - //} + if (list._longCount == 0) + { + return false; + } - //ReadOnlySpan sourceSpan = list._memoryBlock; - //for (var itemIndex = 0; itemIndex < sourceItemsCount; itemIndex++) - //{ - // if (match(sourceSpan[itemIndex])) - // { - // return true; - // } - //} + int blockIndex = 0, + blockSize = list._blockSize; + ReadOnlySpan sourceMemoryBlocksSpan = list._memoryBlocks; + ReadOnlySpan sourceMemoryBlockSpan = sourceMemoryBlocksSpan[0]; + var itemIndex = 0; + int lastBlockWithData = list._lastBlockWithData; + while (blockIndex < lastBlockWithData) + { + if (match(sourceMemoryBlockSpan[itemIndex])) + { + return true; + } - //return false; + if (itemIndex + 1 == blockSize) + { + itemIndex = 0; + blockIndex++; + sourceMemoryBlockSpan = sourceMemoryBlocksSpan[blockIndex]; - throw new NotImplementedException(); + if (blockIndex == lastBlockWithData) + { + break; + } + } + else + { + itemIndex++; + } + } + + if (blockIndex == lastBlockWithData) + { + // We're re-using another variable for better performance + lastBlockWithData = list._nextItemIndex > 0 ? list._nextItemIndex : blockSize; + while (itemIndex < lastBlockWithData) + { + if (match(sourceMemoryBlockSpan[itemIndex++])) + { + return true; + } + } + } + + return false; } public static T? Find(this RecyclableLongList list, Predicate match)