-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Queue.cs
248 lines (202 loc) · 7.21 KB
/
Queue.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
using System;
using System.Collections.Generic;
namespace DataStructures.Lists
{
/// <summary>
/// The Queue (FIFO) Data Structure.
/// </summary>
public class Queue<T> : IEnumerable<T> where T : IComparable<T>
{
/// <summary>
/// INSTANCE VARIABLE.
/// </summary>
private int _size { get; set; }
private int _headPointer { get; set; }
private int _tailPointer { get; set; }
// The internal collection.
private T[] _collection { get; set; }
private const int _defaultCapacity = 8;
// This sets the default maximum array length to refer to MAXIMUM_ARRAY_LENGTH_x64
// Set the flag IsMaximumCapacityReached to false
bool DefaultMaxCapacityIsX64 = true;
bool IsMaximumCapacityReached = false;
// The C# Maximum Array Length (before encountering overflow)
// Reference: http://referencesource.microsoft.com/#mscorlib/system/array.cs,2d2b551eabe74985
public const int MAXIMUM_ARRAY_LENGTH_x64 = 0X7FEFFFFF; //x64
public const int MAXIMUM_ARRAY_LENGTH_x86 = 0x8000000; //x86
/// <summary>
/// CONSTRUCTOR
/// </summary>
public Queue() : this(_defaultCapacity) { }
public Queue(int initialCapacity)
{
if (initialCapacity < 0)
{
throw new ArgumentOutOfRangeException();
}
_size = 0;
_headPointer = 0;
_tailPointer = 0;
_collection = new T[initialCapacity];
}
/// <summary>
/// Resize the internal array to a new size.
/// </summary>
private void _resize(int newSize)
{
if (newSize > _size && !IsMaximumCapacityReached)
{
int capacity = (_collection.Length == 0 ? _defaultCapacity : _collection.Length * 2);
// Allow the list to grow to maximum possible capacity (~2G elements) before encountering overflow.
// Note that this check works even when _items.Length overflowed thanks to the (uint) cast
int maxCapacity = (DefaultMaxCapacityIsX64 == true ? MAXIMUM_ARRAY_LENGTH_x64 : MAXIMUM_ARRAY_LENGTH_x86);
// Handle the new proper size
if (capacity < newSize)
capacity = newSize;
if (capacity >= maxCapacity)
{
capacity = maxCapacity - 1;
IsMaximumCapacityReached = true;
}
// Try resizing and handle overflow
try
{
//Array.Resize (ref _collection, newSize);
var tempCollection = new T[newSize];
Array.Copy(_collection, _headPointer, tempCollection, 0, _size);
_collection = tempCollection;
}
catch (OutOfMemoryException)
{
if (DefaultMaxCapacityIsX64 == true)
{
DefaultMaxCapacityIsX64 = false;
_resize(capacity);
}
else
{
throw;
}
}
}
}
/// <summary>
/// Returns count of elements in queue
/// </summary>
public int Count
{
get { return _size; }
}
/// <summary>
/// Checks whether the queue is empty.
/// </summary>
public bool IsEmpty
{
get { return _size == 0; }
}
/// <summary>
/// Returns the top element in queue
/// </summary>
public T Top
{
get
{
if (IsEmpty)
throw new Exception("Queue is empty.");
return _collection[_headPointer];
}
}
/// <summary>
/// Inserts an element at the end of the queue
/// </summary>
/// <param name="dataItem">Element to be inserted.</param>
public void Enqueue(T dataItem)
{
if (_size == _collection.Length)
{
try
{
_resize(_collection.Length * 2);
}
catch (OutOfMemoryException ex)
{
throw ex;
}
}
// Enqueue item at tail and then increment tail
_collection[_tailPointer++] = dataItem;
// Wrap around
if (_tailPointer == _collection.Length)
_tailPointer = 0;
// Increment size
_size++;
}
/// <summary>
/// Removes the Top Element from queue, and assigns it's value to the "top" parameter.
/// </summary>
/// <return>The top element container.</return>
public T Dequeue()
{
if (IsEmpty)
throw new Exception("Queue is empty.");
var topItem = _collection[_headPointer];
_collection[_headPointer] = default(T);
// Decrement the size
_size--;
// Increment the head pointer
_headPointer++;
// Reset the pointer
if (_headPointer == _collection.Length)
_headPointer = 0;
// Shrink the internal collection
if (_size > 0 && _collection.Length > _defaultCapacity && _size <= _collection.Length / 4)
{
// Get head and tail
var head = _collection[_headPointer];
var tail = _collection[_tailPointer];
// Shrink
_resize((_collection.Length / 3) * 2);
// Update head and tail pointers
_headPointer = Array.IndexOf(_collection, head);
_tailPointer = Array.IndexOf(_collection, tail);
}
return topItem;
}
/// <summary>
/// Returns an array version of this queue.
/// </summary>
/// <returns>System.Array.</returns>
public T[] ToArray()
{
var array = new T[_size];
int j = 0;
for (int i = 0; i < _size; ++i)
{
array[j] = _collection[_headPointer + i];
j++;
}
return array;
}
/// <summary>
/// Returns a human-readable, multi-line, print-out (string) of this queue.
/// </summary>
public string ToHumanReadable()
{
var array = ToArray();
string listAsString = string.Empty;
int i = 0;
for (i = 0; i < Count; ++i)
listAsString = String.Format("{0}[{1}] => {2}\r\n", listAsString, i, array[i]);
return listAsString;
}
/********************************************************************************/
public IEnumerator<T> GetEnumerator()
{
return _collection.GetEnumerator() as IEnumerator<T>;
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
}