mirror of
https://github.com/misternebula/quantum-space-buddies.git
synced 2024-12-27 06:18:52 +00:00
improve ListStack
This commit is contained in:
parent
e8e3763203
commit
c74d36d714
@ -94,7 +94,7 @@ internal class MultiplayerHUDManager : MonoBehaviour, IAddComponentOnStart
|
||||
|
||||
if (_messages.Count > LINE_COUNT)
|
||||
{
|
||||
_messages.RemoveFirstElementAndShift();
|
||||
_messages.PopFromBack();
|
||||
}
|
||||
|
||||
var currentLineIndex = 10;
|
||||
|
@ -16,7 +16,7 @@ public class PlanetTrigger : SectoredMonoBehaviour
|
||||
}
|
||||
|
||||
MultiplayerHUDManager.HUDIconStack.Push(Icon);
|
||||
var top = MultiplayerHUDManager.HUDIconStack.Peek();
|
||||
var top = MultiplayerHUDManager.HUDIconStack.PeekFront();
|
||||
new PlanetMessage(top).Send();
|
||||
}
|
||||
|
||||
@ -28,7 +28,7 @@ public class PlanetTrigger : SectoredMonoBehaviour
|
||||
}
|
||||
|
||||
MultiplayerHUDManager.HUDIconStack.Remove(Icon);
|
||||
var top = MultiplayerHUDManager.HUDIconStack.Peek();
|
||||
var top = MultiplayerHUDManager.HUDIconStack.PeekFront();
|
||||
new PlanetMessage(top).Send();
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,9 @@ using System.Collections.Generic;
|
||||
|
||||
namespace QSB.Utility;
|
||||
|
||||
/// <summary>
|
||||
/// A LIFO collection with List<> functionality.
|
||||
/// </summary>
|
||||
public class ListStack<T> : IEnumerable<T>
|
||||
{
|
||||
private List<T> _items = new();
|
||||
@ -12,14 +15,21 @@ public class ListStack<T> : IEnumerable<T>
|
||||
|
||||
private readonly bool _removeDuplicates;
|
||||
|
||||
/// <param name="removeDuplicates">If true, all elements equal to the added item will be removed prior to adding the new element.</param>
|
||||
public ListStack(bool removeDuplicates)
|
||||
{
|
||||
_removeDuplicates = removeDuplicates;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes all items from the stack.
|
||||
/// </summary>
|
||||
public void Clear()
|
||||
=> _items.Clear();
|
||||
|
||||
/// <summary>
|
||||
/// Pushes an element onto the front of the stack.
|
||||
/// </summary>
|
||||
public void Push(T item)
|
||||
{
|
||||
if (_removeDuplicates && _items.Contains(item))
|
||||
@ -30,7 +40,10 @@ public class ListStack<T> : IEnumerable<T>
|
||||
_items.Add(item);
|
||||
}
|
||||
|
||||
public T Pop()
|
||||
/// <summary>
|
||||
/// Pops an element off the front of the stack.
|
||||
/// </summary>
|
||||
public T PopFromFront()
|
||||
{
|
||||
if (_items.Count > 0)
|
||||
{
|
||||
@ -42,7 +55,10 @@ public class ListStack<T> : IEnumerable<T>
|
||||
return default;
|
||||
}
|
||||
|
||||
public T RemoveFirstElementAndShift()
|
||||
/// <summary>
|
||||
/// Pops an element off the back of the stack and shifts the entire stack backwards.
|
||||
/// </summary>
|
||||
public T PopFromBack()
|
||||
{
|
||||
if (_items.Count == 0)
|
||||
{
|
||||
@ -50,29 +66,39 @@ public class ListStack<T> : IEnumerable<T>
|
||||
}
|
||||
|
||||
var firstElement = _items[0];
|
||||
|
||||
if (_items.Count == 0)
|
||||
{
|
||||
return firstElement;
|
||||
}
|
||||
|
||||
// shift list left
|
||||
// allocates blehhh who cares
|
||||
_items = _items.GetRange(1, _items.Count - 1);
|
||||
|
||||
_items.RemoveAt(0);
|
||||
return firstElement;
|
||||
}
|
||||
|
||||
public T Peek() => _items.Count > 0
|
||||
/// <summary>
|
||||
/// Returns the element at the front of the stack.
|
||||
/// </summary>
|
||||
public T PeekFront() => _items.Count > 0
|
||||
? _items[_items.Count - 1]
|
||||
: default;
|
||||
|
||||
/// <summary>
|
||||
/// Returns the element at the back of the stack.
|
||||
/// </summary>
|
||||
public T PeekBack() => _items.Count > 0
|
||||
? _items[0]
|
||||
: default;
|
||||
|
||||
/// <summary>
|
||||
/// Removes the element at the given index, where 0 is the back of the stack. The stack will shift backwards to fill empty space.
|
||||
/// </summary>
|
||||
public void RemoveAt(int index)
|
||||
=> _items.RemoveAt(index);
|
||||
|
||||
/// <summary>
|
||||
/// Removes the first occurence (back to front) of an item.
|
||||
/// </summary>
|
||||
public bool Remove(T item)
|
||||
=> _items.Remove(item);
|
||||
|
||||
/// <summary>
|
||||
/// Removes all elements that match the given predicate.
|
||||
/// </summary>
|
||||
public int RemoveAll(Predicate<T> match)
|
||||
=> _items.RemoveAll(match);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user