From 099d5368db5ac884535f70893163d77ccb40a0c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joaquin=20Mu=C3=B1iz?= Date: Fri, 14 Nov 2025 09:35:58 -0300 Subject: [PATCH] Add RemoveRange to UnsafeList (cherry picked from commit 44d96fc254aa889c4221288b7fdc9603f633c6b8) --- Arch.LowLevel/UnsafeList.cs | 40 +++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/Arch.LowLevel/UnsafeList.cs b/Arch.LowLevel/UnsafeList.cs index b028aad..f4545f2 100644 --- a/Arch.LowLevel/UnsafeList.cs +++ b/Arch.LowLevel/UnsafeList.cs @@ -162,6 +162,46 @@ public bool Remove(T item) return true; } + /// + /// Removes a contiguous range of elements from the list and shifts the remaining elements down + /// to fill the gap. + /// + /// + /// The starting index of the range to remove. Must be within the bounds of the list. + /// + /// + /// The number of elements to remove. Must be non-negative and not exceed the remaining elements + /// beyond . + /// + /// + /// Thrown if or define a range outside + /// the bounds of the list. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void RemoveRange(int start, int count) + { + if (start > Count) + throw new ArgumentOutOfRangeException(nameof(start)); + if (count > Count - start) + throw new ArgumentOutOfRangeException(nameof(count)); + if (count == 0) + return; + + // Calculate the tail size to move + var tail = Count - (start + count); + if (tail > 0) + { + // Move the tail down over the removed range + Unsafe.CopyBlock( + destination: (byte*)(_array._ptr + start), + source: (byte*)(_array._ptr + start + count), + byteCount: (uint)(tail * sizeof(T)) + ); + } + + Count -= count; + } + /// /// Checks if the item is containted in this instance and returns its index. ///