This repository has been archived by the owner on May 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathSimpleQueue.cs
235 lines (216 loc) · 8.21 KB
/
SimpleQueue.cs
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
using System;
using System.Runtime.CompilerServices;
namespace RCNet.Queue
{
/// <summary>
/// Implements a simple FIFO queue template.
/// </summary>
/// <remarks>
/// Supports access to enqueued elements so it can be also used as the moving data window.
/// </remarks>
[Serializable]
public class SimpleQueue<T>
{
//Attribute properties
/// <summary>
/// The maximum capacity of the queue.
/// </summary>
public int Capacity { get; private set; }
/// <summary>
/// The number of elements in the queue.
/// </summary>
public int Count { get; private set; }
//Attributes
private T[] _queueBuffer;
private int _enqueueIndex;
private int _dequeueIndex;
//Constructor
/// <summary>
/// Creates an initialized instance.
/// </summary>
/// <param name="capacity">The maximum capacity of the queue.</param>
public SimpleQueue(int capacity)
{
Capacity = capacity;
_queueBuffer = new T[Capacity];
Reset();
return;
}
//Properties
/// <summary>
/// Indicates the queue is full.
/// </summary>
public bool Full { get { return (Count == Capacity); } }
//Methods
/// <summary>
/// Throws the InvalidOperationException.
/// </summary>
/// <param name="text">The exception text.</param>
private void ThrowInvalidOperationException(string text)
{
throw new InvalidOperationException(text);
}
/// <summary>
/// Throws the IndexOutOfRangeException.
/// </summary>
/// <param name="text">The exception text.</param>
private void ThrowIndexOutOfRangeException(string text)
{
throw new IndexOutOfRangeException(text);
}
/// <summary>
/// Resets the queue to its initial state.
/// </summary>
public void Reset()
{
_enqueueIndex = 0;
_dequeueIndex = 0;
Count = 0;
return;
}
/// <summary>
/// Resets the queue and changes the queue capacity.
/// </summary>
/// <param name="newCapacity">The new maximum capacity of the queue.</param>
/// <param name="forceShrink">Specifies whether to reallocate queue buffer even if the new capacity is smaller than the current buffer size.</param>
public void Resize(int newCapacity, bool forceShrink = false)
{
Reset();
if (forceShrink || newCapacity > _queueBuffer.Length)
{
_queueBuffer = new T[newCapacity];
}
Capacity = newCapacity;
return;
}
/// <summary>
/// Gets an element from queue buffer at the next enqueue position.
/// </summary>
/// <remarks>
/// If the element exists then it can be reused in immediately following Enqueue call.
/// </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T GetElementAtEnqueuePosition()
{
return _queueBuffer[_enqueueIndex];
}
/// <summary>
/// Gets the physical zero-based index within the queue buffer corresponding to a logical position 0..(Count-1) following the specified logical order.
/// </summary>
/// <param name="logicalPos">The logical position 0..(Count-1).</param>
/// <param name="latestFirst">Specifies the logical order (latest..oldest or vice versa).</param>
/// <returns>The physical zero-based index.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private int GetElementIndex(int logicalPos, bool latestFirst = false)
{
if (logicalPos < 0 || logicalPos >= Count)
{
ThrowIndexOutOfRangeException($"Specified logicalPos {logicalPos} is outside of the range 0..{Count - 1}");
//This will never happen
return -1;
}
else if (latestFirst)
{
int bufferOffset = (_enqueueIndex - logicalPos) - 1;
if (bufferOffset < 0) bufferOffset += Count;
return bufferOffset;
}
else
{
int bufferOffset = (_dequeueIndex + logicalPos);
if (bufferOffset >= Count) bufferOffset -= Count;
return bufferOffset;
}
}
/// <summary>
/// Gets an enqueued element at the zero-based logical position 0..(Count-1) respecting desired logical order of elements.
/// </summary>
/// <param name="logicalPos">The logical position 0..(Count-1).</param>
/// <param name="latestFirst">Specifies the logical order (latest..oldest or oldest..latest).</param>
/// <returns>An element at the logical position.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T GetElementAt(int logicalPos, bool latestFirst = false)
{
return _queueBuffer[GetElementIndex(logicalPos, latestFirst)];
}
/// <summary>
/// Sets an element at the zero-based logical position 0..(Count-1) respecting desired logical order of elements.
/// </summary>
/// <param name="elem">An element to be set.</param>
/// <param name="logicalPos">The logical position 0..(Count-1).</param>
/// <param name="latestFirst">Specifies the logical order (latest..oldest or oldest..latest).</param>
/// <returns>The replaced element at the logical position.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T SetElementAt(T elem, int logicalPos, bool latestFirst = false)
{
int index = GetElementIndex(logicalPos, latestFirst);
T orgItem = _queueBuffer[index];
_queueBuffer[index] = elem;
return orgItem;
}
/// <summary>
/// Adds an element into the queue.
/// </summary>
/// <param name="elem">An element to be added.</param>
/// <param name="autoDequeue">Specifies whether to atomatically dequeue when queue is full.</param>
/// <returns>True if success, False if queue is full.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Enqueue(T elem, bool autoDequeue = false)
{
//Atomatically dequeue when queue is full and parameter autoDequeue is true
if (Count == Capacity && autoDequeue)
{
Dequeue();
}
//Enqueue when there is a free capacity
if (Count < Capacity)
{
_queueBuffer[_enqueueIndex] = elem;
++_enqueueIndex;
if (_enqueueIndex == Capacity)
{
_enqueueIndex = 0;
}
++Count;
return true;
}
return false;
}
/// <summary>
/// Dequeues an element from the queue (FIFO order).
/// </summary>
/// <returns>The dequeued element.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public T Dequeue()
{
if (Count > 0)
{
T elem = _queueBuffer[_dequeueIndex];
++_dequeueIndex;
if (_dequeueIndex == Capacity)
{
_dequeueIndex = 0;
}
--Count;
return elem;
}
else
{
ThrowInvalidOperationException($"Queue is empty.");
//This will never happen
return default;
}
}
/// <summary>
/// Creates the shallow copy of this queue.
/// </summary>
public SimpleQueue<T> ShallowClone()
{
SimpleQueue<T> newQueue = new SimpleQueue<T>(Capacity);
_queueBuffer.CopyTo(newQueue._queueBuffer, 0);
newQueue._enqueueIndex = _enqueueIndex;
newQueue._dequeueIndex = _dequeueIndex;
return newQueue;
}
}//SimpleQueue
}//Namespace