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. ///