Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions Arch.LowLevel/UnsafeList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,46 @@ public bool Remove(T item)
return true;
}

/// <summary>
/// Removes a contiguous range of elements from the list and shifts the remaining elements down
/// to fill the gap.
/// </summary>
/// <param name="start">
/// The starting index of the range to remove. Must be within the bounds of the list.
/// </param>
/// <param name="count">
/// The number of elements to remove. Must be non-negative and not exceed the remaining elements
/// beyond <paramref name="start"/>.
/// </param>
/// <exception cref="ArgumentOutOfRangeException">
/// Thrown if <paramref name="start"/> or <paramref name="count"/> define a range outside
/// the bounds of the list.
/// </exception>
[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;
}

/// <summary>
/// Checks if the item is containted in this <see cref="UnsafeList{T}"/> instance and returns its index.
/// </summary>
Expand Down
Loading