// Crest Water System // Copyright © 2024 Wave Harmonic. All rights reserved. using System.Collections.Generic; using UnityEngine; namespace WaveHarmonic.Crest.Utility.Internal { /// /// A less rigid stack implementation which is easier to use. Prevents duplicates. /// /// Type to store. public sealed class Stack { readonly List _Items = new(); internal Stack() { } /// /// Add item to the end of the stack. /// /// Item to add. public void Push(T item) { Debug.Assert(item != null, "Null item pushed"); // Remove any instances of item already in the stack. Pop(item); // Add it to the top. _Items.Add(item); } /// /// Removes all instances of item. /// /// The item to be removed. public void Pop(T item) { Debug.Assert(item != null, "Null item popped"); _Items.RemoveAll(candidate => candidate.Equals(item)); } /// /// Returns the object at the top of the Stack without removing it. /// /// Object at the top of the Stack. public T Peek() => _Items[^1]; /// /// Number of items. /// public int Count => _Items.Count; internal void Clear() { _Items.Clear(); } } }