Skip to content

Commit f4d6bf4

Browse files
committed
Added ESE_Stack and ESE_DynamicStack classes
1 parent b82d4e6 commit f4d6bf4

File tree

2 files changed

+328
-0
lines changed

2 files changed

+328
-0
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
// -----------------------------------------------------------------------------------------------------------
2+
// Enforce Script Extensions - By NarcoMarshmallow
3+
//
4+
// Message me @narcoleptic marshmallow #1188 on discord to give feedback or go to https://github.com/NarcoMarshDev
5+
// -----------------------------------------------------------------------------------------------------------
6+
/**
7+
ESE_DynamicStack Class
8+
9+
Template class for first-in, last-out array of objects. The dynamic stack has no max size, and will continue to grow
10+
as more objects are pushed onto it, and shrink when popped off. Use TrimTo() to reduce the size and lose the most
11+
recent entries.
12+
13+
For full documentation check the wiki @ https://github.com/NarcoMarshDev/Enforce-Script-Extensions/wiki/ESE_DynamicStack-Class
14+
15+
Code example:
16+
@code
17+
// See ESE_Stack
18+
@endcode
19+
*/
20+
21+
class ESE_DynamicStack<Class T>
22+
{
23+
ref array<T> Raw = {};
24+
T nullValue = null;
25+
26+
// ----------------------------------------------------------------------------------------------------------- //
27+
void Push(T value)
28+
{
29+
Raw.Insert(value);
30+
}
31+
// ----------------------------------------------------------------------------------------------------------- //
32+
T Pop()
33+
{
34+
int topIndex = Raw.Count() - 1;
35+
if (topIndex < 0)
36+
{
37+
#ifdef ESE_VERBOSE
38+
Print("" + this + ": Attempted to peek index out of range ["+topIndex+"]", LogLevel.ERROR);
39+
#endif
40+
return nullValue;
41+
}
42+
T value = Raw.Get(topIndex);
43+
Raw.Remove(topIndex);
44+
return value;
45+
}
46+
// ----------------------------------------------------------------------------------------------------------- //
47+
T Peek()
48+
{
49+
int topIndex = Raw.Count() - 1;
50+
if (topIndex < 0)
51+
{
52+
#ifdef ESE_VERBOSE
53+
Print("" + this + ": Attempted to peek index out of range ["+topIndex+"]", LogLevel.ERROR);
54+
#endif
55+
return nullValue;
56+
}
57+
T value = Raw.Get(topIndex);
58+
return value;
59+
}
60+
// ----------------------------------------------------------------------------------------------------------- //
61+
bool TryPop(out T output)
62+
{
63+
int topIndex = Raw.Count() - 1;
64+
if (topIndex < 0)
65+
{
66+
#ifdef ESE_VERBOSE
67+
Print("" + this + ": Attempted to peek index out of range ["+topIndex+"]", LogLevel.ERROR);
68+
#endif
69+
output = nullValue;
70+
return false;
71+
}
72+
output = Raw.Get(topIndex);
73+
Raw.Remove(topIndex);
74+
return true;
75+
}
76+
// ----------------------------------------------------------------------------------------------------------- //
77+
bool TryPeek(out T output)
78+
{
79+
int topIndex = Raw.Count() - 1;
80+
if (topIndex < 0)
81+
{
82+
#ifdef ESE_VERBOSE
83+
Print("" + this + ": Attempted to peek index out of range ["+topIndex+"]", LogLevel.ERROR);
84+
#endif
85+
output = nullValue;
86+
return false;
87+
}
88+
output = Raw.Get(topIndex);
89+
return true;
90+
}
91+
// ----------------------------------------------------------------------------------------------------------- //
92+
bool Contains(T value)
93+
{
94+
return Raw.Contains(value);
95+
}
96+
// ----------------------------------------------------------------------------------------------------------- //
97+
void Clear()
98+
{
99+
Raw.Clear();
100+
}
101+
// ----------------------------------------------------------------------------------------------------------- //
102+
int Count()
103+
{
104+
return Raw.Count();
105+
}
106+
// ----------------------------------------------------------------------------------------------------------- //
107+
void TrimTo(int size)
108+
{
109+
int sizeDifference = Raw.Count() - size;
110+
if (sizeDifference < 0) {
111+
return;
112+
}
113+
Raw.Resize(size);
114+
Raw.Compact();
115+
}
116+
// ----------------------------------------------------------------------------------------------------------- //
117+
void Compact()
118+
{
119+
Raw.Compact();
120+
}
121+
// ----------------------------------------------------------------------------------------------------------- //
122+
typename GetDataType()
123+
{
124+
return T;
125+
}
126+
// ----------------------------------------------------------------------------------------------------------- //
127+
void CopyToArray(notnull inout array<T> newArray)
128+
{
129+
newArray.Copy(Raw);
130+
}
131+
// ----------------------------------------------------------------------------------------------------------- //
132+
void CopyToStaticStack(notnull inout ESE_Stack<T> newStack)
133+
{
134+
newStack.Raw.Copy(this.Raw);
135+
newStack.Resize(this.Raw.Count());
136+
}
137+
// ----------------------------------------------------------------------------------------------------------- //
138+
void CopyFromArray(notnull array<T> oldArray)
139+
{
140+
Raw.Copy(oldArray);
141+
Resize(oldArray.Count());
142+
}
143+
// ----------------------------------------------------------------------------------------------------------- //
144+
void ESE_DynamicStack()
145+
{
146+
}
147+
// ----------------------------------------------------------------------------------------------------------- //
148+
void ~ESE_DynamicStack()
149+
{
150+
delete Raw;
151+
}
152+
}

scripts/Game/Global/ESE_Stack.c

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
// -----------------------------------------------------------------------------------------------------------
2+
// Enforce Script Extensions - By NarcoMarshmallow
3+
//
4+
// Message me @narcoleptic marshmallow #1188 on discord to give feedback or go to https://github.com/NarcoMarshDev
5+
// -----------------------------------------------------------------------------------------------------------
6+
/**
7+
ESE_Stack Class
8+
9+
Template class for first-in, last-out array of objects. Attempting to push an object to the stack when it's count has
10+
reached it's maxsize will make Push() return false and no object will be added.
11+
12+
For full documentation check the wiki @ https://github.com/NarcoMarshDev/Enforce-Script-Extensions/wiki/ESE_Stack-Class
13+
14+
Code example:
15+
@code
16+
// creates new stack with a size of 5
17+
auto m_stack = new ESE_Stack<int>(5);
18+
19+
for (int i = 0; i < 5; i++)
20+
{
21+
m_stack.Push(i); // push i onto stack
22+
}
23+
Print(m_stack.Raw); >> {0,1,2,3,4}
24+
25+
// pop most recent entry off the stack and return it
26+
int val = m_stack.Pop();
27+
Print(val); >> 4
28+
Print(m_stack.Raw); >> {0,1,2,3}
29+
@endcode
30+
*/
31+
32+
class ESE_Stack<Class T>
33+
{
34+
ref array<T> Raw = {};
35+
int MaxSize;
36+
T nullValue = null;
37+
38+
// ----------------------------------------------------------------------------------------------------------- //
39+
bool Push(T value)
40+
{
41+
if (Raw.Count() >= MaxSize)
42+
{
43+
return false;
44+
}
45+
Raw.Insert(value);
46+
return true;
47+
}
48+
// ----------------------------------------------------------------------------------------------------------- //
49+
T Pop()
50+
{
51+
int topIndex = Raw.Count() - 1;
52+
if (topIndex < 0)
53+
{
54+
#ifdef ESE_VERBOSE
55+
Print("" + this + ": Attempted to peek index out of range ["+topIndex+"]", LogLevel.ERROR);
56+
#endif
57+
return nullValue;
58+
}
59+
T value = Raw.Get(topIndex);
60+
Raw.Remove(topIndex);
61+
return value;
62+
}
63+
// ----------------------------------------------------------------------------------------------------------- //
64+
T Peek()
65+
{
66+
int topIndex = Raw.Count() - 1;
67+
if (topIndex < 0)
68+
{
69+
#ifdef ESE_VERBOSE
70+
Print("" + this + ": Attempted to peek index out of range ["+topIndex+"]", LogLevel.ERROR);
71+
#endif
72+
return nullValue;
73+
}
74+
T value = Raw.Get(topIndex);
75+
return value;
76+
}
77+
// ----------------------------------------------------------------------------------------------------------- //
78+
bool TryPop(out T output)
79+
{
80+
int topIndex = Raw.Count() - 1;
81+
if (topIndex < 0)
82+
{
83+
#ifdef ESE_VERBOSE
84+
Print("" + this + ": Attempted to peek index out of range ["+topIndex+"]", LogLevel.ERROR);
85+
#endif
86+
output = nullValue;
87+
return false;
88+
}
89+
output = Raw.Get(topIndex);
90+
Raw.Remove(topIndex);
91+
return true;
92+
}
93+
// ----------------------------------------------------------------------------------------------------------- //
94+
bool TryPeek(out T output)
95+
{
96+
int topIndex = Raw.Count() - 1;
97+
if (topIndex < 0)
98+
{
99+
#ifdef ESE_VERBOSE
100+
Print("" + this + ": Attempted to peek index out of range ["+topIndex+"]", LogLevel.ERROR);
101+
#endif
102+
output = nullValue;
103+
return false;
104+
}
105+
output = Raw.Get(topIndex);
106+
return true;
107+
}
108+
// ----------------------------------------------------------------------------------------------------------- //
109+
bool Contains(T value)
110+
{
111+
return Raw.Contains(value);
112+
}
113+
// ----------------------------------------------------------------------------------------------------------- //
114+
void Clear()
115+
{
116+
Raw.Clear();
117+
}
118+
// ----------------------------------------------------------------------------------------------------------- //
119+
int Count()
120+
{
121+
return Raw.Count();
122+
}
123+
// ----------------------------------------------------------------------------------------------------------- //
124+
int GetMaxSize()
125+
{
126+
return MaxSize;
127+
}
128+
// ----------------------------------------------------------------------------------------------------------- //
129+
void Resize(int size)
130+
{
131+
int sizeDifference = MaxSize - size;
132+
if (sizeDifference > 0)
133+
{
134+
Raw.Resize(size);
135+
}
136+
MaxSize = size;
137+
Raw.Compact();
138+
}
139+
// ----------------------------------------------------------------------------------------------------------- //
140+
void Compact()
141+
{
142+
Raw.Compact();
143+
}
144+
// ----------------------------------------------------------------------------------------------------------- //
145+
typename GetDataType()
146+
{
147+
return T;
148+
}
149+
// ----------------------------------------------------------------------------------------------------------- //
150+
void CopyToArray(notnull inout array<T> newArray)
151+
{
152+
newArray.Copy(Raw);
153+
}
154+
// ----------------------------------------------------------------------------------------------------------- //
155+
void CopyToDynamicStack(notnull inout ESE_DynamicStack<T> newStack)
156+
{
157+
newStack.Raw.Copy(this.Raw);
158+
newStack.Compact();
159+
}
160+
// ----------------------------------------------------------------------------------------------------------- //
161+
void CopyFromArray(notnull array<T> oldArray)
162+
{
163+
Raw.Copy(oldArray);
164+
Resize(oldArray.Count());
165+
}
166+
// ----------------------------------------------------------------------------------------------------------- //
167+
void ESE_Stack(int size)
168+
{
169+
MaxSize = size;
170+
}
171+
// ----------------------------------------------------------------------------------------------------------- //
172+
void ~ESE_Stack()
173+
{
174+
delete Raw;
175+
}
176+
}

0 commit comments

Comments
 (0)