using System.Collections.Generic; namespace Sarand.Net.Standard.Base.Core { //======================================================================= /// /// Stack (LIFO) with limit on length. /// /// public class StackLimited { //------------------------------------------------------------------- // Data members. private int limit = 0; private readonly LinkedList items = new LinkedList(); //------------------------------------------------------------------- // Construction. public StackLimited (int limit = 0) { this.limit = limit; } //------------------------------------------------------------------- // Utilities. public int Count { get { return items.Count; } } public void Clear () { items.Clear(); } //------------------------------------------------------------------- /// /// Add item to the stack and ensure limit isn't exceeded. /// /// public void Push (ITEM item) { items.AddFirst(item); while (limit > 0 && items.Count > limit) items.RemoveLast(); } //------------------------------------------------------------------- /// /// Retrieve and remove last item added to the stack. /// /// public ITEM Pop () { ITEM item = items.First.Value; items.RemoveFirst(); return item; } //------------------------------------------------------------------- } //======================================================================= }