From 49b25e755b70ec412feaaf0b898d6f7e09d2bea6 Mon Sep 17 00:00:00 2001 From: chai Date: Tue, 28 Jun 2022 09:40:37 +0800 Subject: +node example --- .../Assets/UNEB/Utility/FiniteStack.cs | 78 ++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 Other/NodeEditorExamples/Assets/UNEB/Utility/FiniteStack.cs (limited to 'Other/NodeEditorExamples/Assets/UNEB/Utility/FiniteStack.cs') diff --git a/Other/NodeEditorExamples/Assets/UNEB/Utility/FiniteStack.cs b/Other/NodeEditorExamples/Assets/UNEB/Utility/FiniteStack.cs new file mode 100644 index 00000000..2faac0f1 --- /dev/null +++ b/Other/NodeEditorExamples/Assets/UNEB/Utility/FiniteStack.cs @@ -0,0 +1,78 @@ + +using System; +using System.Collections; +using System.Collections.Generic; + +namespace UNEB.Utility +{ + /// + /// A simple stack with a limited capacity. + /// In order to make more room, the first element (not the top) in the stack is removed. + /// + /// + public class FiniteStack : IEnumerable + { + private LinkedList _container; + private int _capacity; + + /// + /// Called when the stack runs out of space and removes + /// the first item (bottom of stack) to make room. + /// + public event Action OnRemoveBottomItem; + + public FiniteStack(int capacity) + { + _container = new LinkedList(); + _capacity = capacity; + } + + public void Push(T value) + { + _container.AddLast(value); + + // Out of room, remove the first element in the stack. + if (_container.Count == _capacity) { + + T first = _container.First.Value; + _container.RemoveFirst(); + + if (OnRemoveBottomItem != null) + OnRemoveBottomItem(first); + } + } + + public T Peek() + { + return _container.Last.Value; + } + + public T Pop() + { + var lastVal = _container.Last.Value; + _container.RemoveLast(); + + return lastVal; + } + + public void Clear() + { + _container.Clear(); + } + + public int Count + { + get { return _container.Count; } + } + + public IEnumerator GetEnumerator() + { + return _container.GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return _container.GetEnumerator(); + } + } +} \ No newline at end of file -- cgit v1.1-26-g67d0