|
using System;
namespace FastColoredTextBoxNS
{
/// <summary>
/// Limited stack
/// </summary>
public class LimitedStack<T>
{
T[] items;
int count;
int start;
/// <summary>
/// Max stack length
/// </summary>
public int MaxItemCount
{
get { return items.Length; }
}
/// <summary>
/// Current length of stack
/// </summary>
public int Count
{
get { return count; }
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="maxItemCount">Maximum length of stack</param>
public LimitedStack(int maxItemCount)
{
items = new T[maxItemCount];
count = 0;
start = 0;
}
/// <summary>
/// Pop item
/// </summary>
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; }
}
/// <summary>
/// Peek item
/// </summary>
public T Peek()
{
if (count == 0)
return default(T);
return items[LastIndex];
}
/// <summary>
/// Push item
/// </summary>
public void Push(T item)
{
if (count == items.Length)
start = (start + 1) % items.Length;
else
count++;
items[LastIndex] = item;
}
/// <summary>
/// Clear stack
/// </summary>
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;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I am Pavеl Tоrgаshоv, and I live in Kyiv, Ukraine.
I've been developing software since 1998.
Main activities: processing of large volumes of data, statistics, computer vision and graphics.