forked from PavelTorgashov/FastColoredTextBox
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLimitedStack.cs
More file actions
105 lines (90 loc) · 2.16 KB
/
LimitedStack.cs
File metadata and controls
105 lines (90 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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;
}
}
}