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