- Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathQueue.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
usingSystem;
usingSystem.Collections.Generic;
namespaceDataStructures.Lists
{
/// <summary>
/// The Queue (FIFO) Data Structure.
/// </summary>
publicclassQueue<T>:IEnumerable<T>whereT:IComparable<T>
{
/// <summary>
/// INSTANCE VARIABLE.
/// </summary>
privateint_size{get;set;}
privateint_headPointer{get;set;}
privateint_tailPointer{get;set;}
// The internal collection.
privateT[]_collection{get;set;}
privateconstint_defaultCapacity=8;
// This sets the default maximum array length to refer to MAXIMUM_ARRAY_LENGTH_x64
// Set the flag IsMaximumCapacityReached to false
boolDefaultMaxCapacityIsX64=true;
boolIsMaximumCapacityReached=false;
// The C# Maximum Array Length (before encountering overflow)
// Reference: http://referencesource.microsoft.com/#mscorlib/system/array.cs,2d2b551eabe74985
publicconstintMAXIMUM_ARRAY_LENGTH_x64=0X7FEFFFFF;//x64
publicconstintMAXIMUM_ARRAY_LENGTH_x86=0x8000000;//x86
/// <summary>
/// CONSTRUCTOR
/// </summary>
publicQueue():this(_defaultCapacity){}
publicQueue(intinitialCapacity)
{
if(initialCapacity<0)
{
thrownewArgumentOutOfRangeException();
}
_size=0;
_headPointer=0;
_tailPointer=0;
_collection=newT[initialCapacity];
}
/// <summary>
/// Resize the internal array to a new size.
/// </summary>
privatevoid_resize(intnewSize)
{
if(newSize>_size&&!IsMaximumCapacityReached)
{
intcapacity=(_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
intmaxCapacity=(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);
vartempCollection=newT[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>
publicintCount
{
get{return_size;}
}
/// <summary>
/// Checks whether the queue is empty.
/// </summary>
publicboolIsEmpty
{
get{return_size==0;}
}
/// <summary>
/// Returns the top element in queue
/// </summary>
publicTTop
{
get
{
if(IsEmpty)
thrownewException("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>
publicvoidEnqueue(TdataItem)
{
if(_size==_collection.Length)
{
try
{
_resize(_collection.Length*2);
}
catch(OutOfMemoryExceptionex)
{
throwex;
}
}
// 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>
publicTDequeue()
{
if(IsEmpty)
thrownewException("Queue is empty.");
vartopItem=_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
varhead=_collection[_headPointer];
vartail=_collection[_tailPointer];
// Shrink
_resize((_collection.Length/3)*2);
// Update head and tail pointers
_headPointer=Array.IndexOf(_collection,head);
_tailPointer=Array.IndexOf(_collection,tail);
}
returntopItem;
}
/// <summary>
/// Returns an array version of this queue.
/// </summary>
/// <returns>System.Array.</returns>
publicT[]ToArray()
{
vararray=newT[_size];
intj=0;
for(inti=0;i<_size;++i)
{
array[j]=_collection[_headPointer+i];
j++;
}
returnarray;
}
/// <summary>
/// Returns a human-readable, multi-line, print-out (string) of this queue.
/// </summary>
publicstringToHumanReadable()
{
vararray=ToArray();
stringlistAsString=string.Empty;
inti=0;
for(i=0;i<Count;++i)
listAsString=String.Format("{0}[{1}] => {2}\r\n",listAsString,i,array[i]);
returnlistAsString;
}
/********************************************************************************/
publicIEnumerator<T>GetEnumerator()
{
return_collection.GetEnumerator()asIEnumerator<T>;
}
System.Collections.IEnumeratorSystem.Collections.IEnumerable.GetEnumerator()
{
returnthis.GetEnumerator();
}
}
}