ValueList<T> extensions (#5534)

Stack-like functions. Just some code I had lying around and never committed.

Add ROS overload for AddRange
This commit is contained in:
Pieter-Jan Briers
2024-11-28 19:49:51 +01:00
committed by GitHub
parent 8563466011
commit ce240773e8
2 changed files with 76 additions and 1 deletions

View File

@@ -39,7 +39,7 @@ END TEMPLATE-->
### New features
*None yet*
* Added stack-like functions to `ValueList<T>` and added an `AddRange(ReadOnlySpan<T>)` overload.
### Bugfixes

View File

@@ -5,6 +5,7 @@ using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;
@@ -589,6 +590,12 @@ public struct ValueList<T> : IEnumerable<T>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddRange(Span<T> span)
{
AddRange((ReadOnlySpan<T>) span);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddRange(ReadOnlySpan<T> span)
{
var spanCount = span.Length;
EnsureCapacity(Count + spanCount);
@@ -618,4 +625,72 @@ public struct ValueList<T> : IEnumerable<T>
Add(result);
}
}
/// <summary>
/// Push a value onto the end of this list. This is equivalent to <see cref="Add"/>.
/// </summary>
/// <remarks>
/// This method is added to provide completeness with other stack-like functions.
/// </remarks>
/// <param name="item">The item to add to the list.</param>
public void Push(T item)
{
Add(item);
}
/// <summary>
/// Remove and return the value at the end of the list.
/// </summary>
/// <exception cref="InvalidOperationException">Thrown if the list is empty.</exception>
public T Pop()
{
if (!TryPop(out var value))
throw new InvalidOperationException("List is empty");
return value;
}
/// <summary>
/// Return the value at the end of the list, but do not remove it.
/// </summary>
/// <exception cref="InvalidOperationException">Thrown if the list is empty.</exception>
public T Peek()
{
if (!TryPeek(out var value))
throw new InvalidOperationException("List is empty");
return value;
}
/// <summary>
/// Remove and return the value at the end of the list, only if the list is not empty.
/// </summary>
/// <returns>True if the list was not empty and an item was removed.</returns>
public bool TryPop([MaybeNullWhen(false)] out T value)
{
if (Count == 0)
{
value = default;
return false;
}
value = _items![--Count];
return true;
}
/// <summary>
/// Remove and return the value at the end of the list, only if the list is not empty.
/// </summary>
/// <returns>True if the list was not empty and an item was removed.</returns>
public bool TryPeek([MaybeNullWhen(false)] out T value)
{
if (Count == 0)
{
value = default;
return false;
}
value = _items![Count];
return true;
}
}