using System; namespace FastColoredTextBoxNS { /// /// Limited stack /// public class LimitedStack { T[] items; int count; int start; /// /// Max stack length /// public int MaxItemCount { get { return items.Length; } } /// /// Current length of stack /// public int Count { get { return count; } } /// /// Constructor /// /// Maximum length of stack public LimitedStack(int maxItemCount) { items = new T[maxItemCount]; count = 0; start = 0; } /// /// Pop item /// public T Pop() { if (count == 0) throw new Exception("Stack is empty"); int i = LastIndex; T item = items[i]; items[i] = default(T); count--; return item; } int LastIndex { get { return (start + count - 1) % items.Length; } } /// /// Peek item /// public T Peek() { if (count == 0) return default(T); return items[LastIndex]; } /// /// Push item /// public void Push(T item) { if (count == items.Length) start = (start + 1) % items.Length; else count++; items[LastIndex] = item; } /// /// Clear stack /// public void Clear() { items = new T[items.Length]; count = 0; start = 0; } public T[] ToArray() { T[] result = new T[count]; for (int i = 0; i < count; i++) result[i] = items[(start + i) % items.Length]; return result; } } }