/
ESE_DynamicQueue.c
166 lines (161 loc) · 5.14 KB
/
ESE_DynamicQueue.c
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// -----------------------------------------------------------------------------------------------------------
// Enforce Script Extensions - By NarcoMarshmallow
//
// Message me @narcoleptic marshmallow #1188 on discord to give feedback or go to https://github.com/NarcoMarshDev
// -----------------------------------------------------------------------------------------------------------
/**
ESE_DynamicQueue Class
Template class for first-in, first-out array of objects. Unlike ESE_Queue, this class is dynamically sized and will expand
whenever a new object is enqueued, and old objects are never automatically dequeued.
For full documentation check the wiki @ https://github.com/NarcoMarshDev/Enforce-Script-Extensions/wiki/ESE_DynamicQueue-Class
Code example:
@code
// See ESE_Queue
@endcode
*/
class ESE_DynamicQueue<Class T>: Managed
{
ref array<T> Raw = {};
T nullValue;
// ----------------------------------------------------------------------------------------------------------- //
void Enqueue(T value)
{
Raw.InsertAt(value, 0);
}
// ----------------------------------------------------------------------------------------------------------- //
T Dequeue()
{
int lastIndex = Raw.Count() - 1;
if (lastIndex < 0)
{
#ifdef ESE_VERBOSE
Print("" + this + ": Attempted to dequeue index out of range ["+lastIndex+"]", LogLevel.ERROR);
#endif
return nullValue;
}
T value = Raw.Get(lastIndex);
Raw.Remove(lastIndex);
return value;
}
// ----------------------------------------------------------------------------------------------------------- //
T Peek()
{
int lastIndex = Raw.Count() - 1;
if (lastIndex < 0)
{
#ifdef ESE_VERBOSE
Print("" + this + ": Attempted to peek index out of range ["+lastIndex+"]", LogLevel.ERROR);
#endif
return nullValue;
}
T value = Raw.Get(lastIndex);
return value;
}
// ----------------------------------------------------------------------------------------------------------- //
bool TryDequeue(out T output)
{
int lastIndex = Raw.Count() - 1;
if (lastIndex < 0)
{
#ifdef ESE_VERBOSE
Print("" + this + ": Attempted to dequeue index out of range ["+lastIndex+"]", LogLevel.ERROR);
#endif
output = nullValue;
return false;
}
output = Raw.Get(lastIndex);
Raw.Remove(lastIndex);
return true;
}
// ----------------------------------------------------------------------------------------------------------- //
bool TryPeek(out T output)
{
int lastIndex = Raw.Count() - 1;
if (lastIndex < 0)
{
#ifdef ESE_VERBOSE
Print("" + this + ": Attempted to peek index out of range ["+lastIndex+"]", LogLevel.ERROR);
#endif
output = nullValue;
return false;
}
output = Raw.Get(lastIndex);
return true;
}
// ----------------------------------------------------------------------------------------------------------- //
bool Contains(T value)
{
return Raw.Contains(value);
}
// ----------------------------------------------------------------------------------------------------------- //
void Clear()
{
Raw.Clear();
}
// ----------------------------------------------------------------------------------------------------------- //
int Count()
{
return Raw.Count();
}
// ----------------------------------------------------------------------------------------------------------- //
void TrimTo(int size)
{
int sizeDifference = Raw.Count() - size;
if (sizeDifference < 0) {
return;
}
Raw.Resize(size);
Raw.Compact();
}
// ----------------------------------------------------------------------------------------------------------- //
void Compact()
{
Raw.Compact();
}
// ----------------------------------------------------------------------------------------------------------- //
typename GetDataType()
{
return T;
}
// ----------------------------------------------------------------------------------------------------------- //
void CopyToArray(notnull inout array<T> newArray)
{
newArray.Copy(Raw);
}
// ----------------------------------------------------------------------------------------------------------- //
void CopyToStaticQueue(notnull inout ESE_Queue<T> newQueue)
{
newQueue.Raw.Copy(this.Raw);
newQueue.Resize(this.Raw.Count());
}
// ----------------------------------------------------------------------------------------------------------- //
void CopyFromArray(notnull array<T> oldArray)
{
Raw.Copy(oldArray);
TrimTo(oldArray.Count());
}
// ----------------------------------------------------------------------------------------------------------- //
void PrintRaw()
{
Print(Raw);
}
// ----------------------------------------------------------------------------------------------------------- //
T Get(int n)
{
return Raw[n];
}
// ----------------------------------------------------------------------------------------------------------- //
void Set(int n, T value)
{
Raw[n] = value;
}
// ----------------------------------------------------------------------------------------------------------- //
void ESE_DynamicQueue()
{
}
// ----------------------------------------------------------------------------------------------------------- //
void ~ESE_DynamicQueue()
{
delete Raw;
}
}