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_DynamicQueue Class
8+
9+ Template class for first-in, first-out array of objects. Unlike ESE_Queue, this class is dynamically sized and will expand
10+ whenever a new object is enqueued, and old objects are never automatically dequeued.
11+
12+ For full documentation check the wiki @ https://github.com/NarcoMarshDev/Enforce-Script-Extensions/wiki/ESE_DynamicQueue-Class
13+
14+ Code example:
15+ @code
16+ // See ESE_Queue
17+ @endcode
18+ */
19+
20+ class ESE_DynamicQueue < Class T >
21+ {
22+ ref array < T > Raw = {};
23+ T nullValue = null ;
24+
25+ // ----------------------------------------------------------------------------------------------------------- //
26+ void Enqueue (T value )
27+ {
28+ Raw .InsertAt (value , 0 );
29+ }
30+ // ----------------------------------------------------------------------------------------------------------- //
31+ T Dequeue ()
32+ {
33+ int lastIndex = Raw .Count () - 1 ;
34+ if (lastIndex < 0 )
35+ {
36+ #ifdef ESE_VERBOSE
37+ Print ("" + this + ": Attempted to dequeue index out of range [" + lastIndex + "]" , LogLevel .ERROR );
38+ #endif
39+ return nullValue ;
40+ }
41+ T value = Raw .Get (lastIndex );
42+ Raw .Remove (lastIndex );
43+ return value ;
44+ }
45+ // ----------------------------------------------------------------------------------------------------------- //
46+ T Peek ()
47+ {
48+ int lastIndex = Raw .Count () - 1 ;
49+ if (lastIndex < 0 )
50+ {
51+ #ifdef ESE_VERBOSE
52+ Print ("" + this + ": Attempted to peek index out of range [" + lastIndex + "]" , LogLevel .ERROR );
53+ #endif
54+ return nullValue ;
55+ }
56+ T value = Raw .Get (lastIndex );
57+ return value ;
58+ }
59+ // ----------------------------------------------------------------------------------------------------------- //
60+ bool TryDequeue (T output )
61+ {
62+ int lastIndex = Raw .Count () - 1 ;
63+ if (lastIndex < 0 )
64+ {
65+ #ifdef ESE_VERBOSE
66+ Print ("" + this + ": Attempted to dequeue index out of range [" + lastIndex + "]" , LogLevel .ERROR );
67+ #endif
68+ output = nullValue ;
69+ return false;
70+ }
71+ output = Raw .Get (lastIndex );
72+ Raw .Remove (lastIndex );
73+ return true;
74+ }
75+ // ----------------------------------------------------------------------------------------------------------- //
76+ bool TryPeek (T output )
77+ {
78+ int lastIndex = Raw .Count () - 1 ;
79+ if (lastIndex < 0 )
80+ {
81+ #ifdef ESE_VERBOSE
82+ Print ("" + this + ": Attempted to peek index out of range [" + lastIndex + "]" , LogLevel .ERROR );
83+ #endif
84+ output = nullValue ;
85+ return false;
86+ }
87+ output = Raw .Get (lastIndex );
88+ return true;
89+ }
90+ // ----------------------------------------------------------------------------------------------------------- //
91+ bool Contains (T value )
92+ {
93+ return Raw .Contains (value );
94+ }
95+ // ----------------------------------------------------------------------------------------------------------- //
96+ void Clear ()
97+ {
98+ Raw .Clear ();
99+ }
100+ // ----------------------------------------------------------------------------------------------------------- //
101+ int Count ()
102+ {
103+ return Raw .Count ();
104+ }
105+ // ----------------------------------------------------------------------------------------------------------- //
106+ void TrimTo (int size )
107+ {
108+ int sizeDifference = Raw .Count () - size ;
109+ if (sizeDifference < 0 ) {
110+ return ;
111+ }
112+ Raw .Resize (size );
113+ Raw .Compact ();
114+ }
115+ // ----------------------------------------------------------------------------------------------------------- //
116+ typename GetDataType ()
117+ {
118+ return T ;
119+ }
120+ // ----------------------------------------------------------------------------------------------------------- //
121+ void CopyToArray (notnull inout array < T > newArray )
122+ {
123+ newArray .Copy (Raw );
124+ }
125+ // ----------------------------------------------------------------------------------------------------------- //
126+ void CopyToStaticQueue (notnull inout ESE_Queue < T > newQueue )
127+ {
128+ newQueue .Raw .Copy (this .Raw );
129+ newQueue .Resize (this .Raw .Count ());
130+ }
131+ // ----------------------------------------------------------------------------------------------------------- //
132+ void CopyFromArray (notnull array < T > oldArray )
133+ {
134+ Raw .Copy (oldArray );
135+ TrimTo (oldArray .Count ());
136+ }
137+ // ----------------------------------------------------------------------------------------------------------- //
138+ void ESE_DynamicQueue ()
139+ {
140+ }
141+ // ----------------------------------------------------------------------------------------------------------- //
142+ void ~ESE_DynamicQueue ()
143+ {
144+ delete Raw ;
145+ }
146+ }
0 commit comments