From ce240773e8e406c480b869d9ccc981f6c671865a Mon Sep 17 00:00:00 2001 From: Pieter-Jan Briers Date: Thu, 28 Nov 2024 19:49:51 +0100 Subject: [PATCH] ValueList extensions (#5534) Stack-like functions. Just some code I had lying around and never committed. Add ROS overload for AddRange --- RELEASE-NOTES.md | 2 +- Robust.Shared/Collections/ValueList.cs | 75 ++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md index 600a7564d..c2e09a344 100644 --- a/RELEASE-NOTES.md +++ b/RELEASE-NOTES.md @@ -39,7 +39,7 @@ END TEMPLATE--> ### New features -*None yet* +* Added stack-like functions to `ValueList` and added an `AddRange(ReadOnlySpan)` overload. ### Bugfixes diff --git a/Robust.Shared/Collections/ValueList.cs b/Robust.Shared/Collections/ValueList.cs index 7ea17a6dc..4d45c1b4b 100644 --- a/Robust.Shared/Collections/ValueList.cs +++ b/Robust.Shared/Collections/ValueList.cs @@ -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 : IEnumerable [MethodImpl(MethodImplOptions.AggressiveInlining)] public void AddRange(Span span) + { + AddRange((ReadOnlySpan) span); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void AddRange(ReadOnlySpan span) { var spanCount = span.Length; EnsureCapacity(Count + spanCount); @@ -618,4 +625,72 @@ public struct ValueList : IEnumerable Add(result); } } + + /// + /// Push a value onto the end of this list. This is equivalent to . + /// + /// + /// This method is added to provide completeness with other stack-like functions. + /// + /// The item to add to the list. + public void Push(T item) + { + Add(item); + } + + /// + /// Remove and return the value at the end of the list. + /// + /// Thrown if the list is empty. + public T Pop() + { + if (!TryPop(out var value)) + throw new InvalidOperationException("List is empty"); + + return value; + } + + /// + /// Return the value at the end of the list, but do not remove it. + /// + /// Thrown if the list is empty. + public T Peek() + { + if (!TryPeek(out var value)) + throw new InvalidOperationException("List is empty"); + + return value; + } + + /// + /// Remove and return the value at the end of the list, only if the list is not empty. + /// + /// True if the list was not empty and an item was removed. + public bool TryPop([MaybeNullWhen(false)] out T value) + { + if (Count == 0) + { + value = default; + return false; + } + + value = _items![--Count]; + return true; + } + + /// + /// Remove and return the value at the end of the list, only if the list is not empty. + /// + /// True if the list was not empty and an item was removed. + public bool TryPeek([MaybeNullWhen(false)] out T value) + { + if (Count == 0) + { + value = default; + return false; + } + + value = _items![Count]; + return true; + } }