-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Queue.cs
504 lines (431 loc) · 15.1 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
/*=============================================================================
**
** Class: Queue
**
** Purpose: Represents a first-in, first-out collection of objects.
**
=============================================================================*/
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
namespace System.Collections
{
// A simple Queue of objects. Internally it is implemented as a circular
// buffer, so Enqueue can be O(n). Dequeue is O(1).
[DebuggerTypeProxy(typeof(System.Collections.Queue.QueueDebugView))]
[DebuggerDisplay("Count = {Count}")]
[Serializable]
[System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
public class Queue : ICollection, ICloneable
{
private object?[] _array; // Do not rename (binary serialization)
private int _head; // First valid element in the queue. Do not rename (binary serialization)
private int _tail; // Last valid element in the queue. Do not rename (binary serialization)
private int _size; // Number of elements. Do not rename (binary serialization)
private readonly int _growFactor; // 100 == 1.0, 130 == 1.3, 200 == 2.0. Do not rename (binary serialization)
private int _version; // Do not rename (binary serialization)
private const int MinimumGrow = 4;
// Creates a queue with room for capacity objects. The default initial
// capacity and grow factor are used.
public Queue()
: this(32, (float)2.0)
{
}
// Creates a queue with room for capacity objects. The default grow factor
// is used.
//
public Queue(int capacity)
: this(capacity, (float)2.0)
{
}
// Creates a queue with room for capacity objects. When full, the new
// capacity is set to the old capacity * growFactor.
//
public Queue(int capacity, float growFactor)
{
ArgumentOutOfRangeException.ThrowIfNegative(capacity);
ArgumentOutOfRangeException.ThrowIfLessThan(growFactor, 1.0f);
ArgumentOutOfRangeException.ThrowIfGreaterThan(growFactor, 10.0f);
_array = new object[capacity];
_head = 0;
_tail = 0;
_size = 0;
_growFactor = (int)(growFactor * 100);
}
// Fills a Queue with the elements of an ICollection. Uses the enumerator
// to get each of the elements.
//
public Queue(ICollection col) : this(col?.Count ?? throw new ArgumentNullException(nameof(col)))
{
IEnumerator en = col.GetEnumerator();
while (en.MoveNext())
Enqueue(en.Current);
}
public virtual int Count
{
get { return _size; }
}
public virtual object Clone()
{
Queue q = new Queue(_size);
q._size = _size;
int numToCopy = _size;
int firstPart = (_array.Length - _head < numToCopy) ? _array.Length - _head : numToCopy;
Array.Copy(_array, _head, q._array, 0, firstPart);
numToCopy -= firstPart;
if (numToCopy > 0)
Array.Copy(_array, 0, q._array, _array.Length - _head, numToCopy);
q._version = _version;
return q;
}
public virtual bool IsSynchronized
{
get { return false; }
}
public virtual object SyncRoot => this;
// Removes all Objects from the queue.
public virtual void Clear()
{
if (_size != 0)
{
if (_head < _tail)
Array.Clear(_array, _head, _size);
else
{
Array.Clear(_array, _head, _array.Length - _head);
Array.Clear(_array, 0, _tail);
}
_size = 0;
}
_head = 0;
_tail = 0;
_version++;
}
// CopyTo copies a collection into an Array, starting at a particular
// index into the array.
//
public virtual void CopyTo(Array array, int index)
{
ArgumentNullException.ThrowIfNull(array);
if (array.Rank != 1)
throw new ArgumentException(SR.Arg_RankMultiDimNotSupported, nameof(array));
ArgumentOutOfRangeException.ThrowIfNegative(index);
int arrayLen = array.Length;
if (arrayLen - index < _size)
throw new ArgumentException(SR.Argument_InvalidOffLen);
int numToCopy = _size;
if (numToCopy == 0)
return;
int firstPart = (_array.Length - _head < numToCopy) ? _array.Length - _head : numToCopy;
Array.Copy(_array, _head, array, index, firstPart);
numToCopy -= firstPart;
if (numToCopy > 0)
Array.Copy(_array, 0, array, index + _array.Length - _head, numToCopy);
}
// Adds obj to the tail of the queue.
//
public virtual void Enqueue(object? obj)
{
if (_size == _array.Length)
{
int newcapacity = (int)((long)_array.Length * (long)_growFactor / 100);
if (newcapacity < _array.Length + MinimumGrow)
{
newcapacity = _array.Length + MinimumGrow;
}
SetCapacity(newcapacity);
}
_array[_tail] = obj;
_tail = (_tail + 1) % _array.Length;
_size++;
_version++;
}
// GetEnumerator returns an IEnumerator over this Queue. This
// Enumerator will support removing.
//
public virtual IEnumerator GetEnumerator()
{
return new QueueEnumerator(this);
}
// Removes the object at the head of the queue and returns it. If the queue
// is empty, this method throws an InvalidOperationException.
public virtual object? Dequeue()
{
if (Count == 0)
throw new InvalidOperationException(SR.InvalidOperation_EmptyQueue);
object? removed = _array[_head];
_array[_head] = null;
_head = (_head + 1) % _array.Length;
_size--;
_version++;
return removed;
}
// Returns the object at the head of the queue. The object remains in the
// queue. If the queue is empty, this method throws an
// InvalidOperationException.
public virtual object? Peek()
{
if (Count == 0)
throw new InvalidOperationException(SR.InvalidOperation_EmptyQueue);
return _array[_head];
}
// Returns a synchronized Queue. Returns a synchronized wrapper
// class around the queue - the caller must not use references to the
// original queue.
//
public static Queue Synchronized(Queue queue)
{
ArgumentNullException.ThrowIfNull(queue);
return new SynchronizedQueue(queue);
}
// Returns true if the queue contains at least one object equal to obj.
// Equality is determined using obj.Equals().
//
public virtual bool Contains(object? obj)
{
int index = _head;
int count = _size;
while (count-- > 0)
{
if (obj == null)
{
if (_array[index] == null)
return true;
}
else if (_array[index] != null && _array[index]!.Equals(obj))
{
return true;
}
index = (index + 1) % _array.Length;
}
return false;
}
internal object? GetElement(int i)
{
return _array[(_head + i) % _array.Length];
}
// Iterates over the objects in the queue, returning an array of the
// objects in the Queue, or an empty array if the queue is empty.
// The order of elements in the array is first in to last in, the same
// order produced by successive calls to Dequeue.
public virtual object?[] ToArray()
{
if (_size == 0)
return Array.Empty<object>();
object[] arr = new object[_size];
if (_head < _tail)
{
Array.Copy(_array, _head, arr, 0, _size);
}
else
{
Array.Copy(_array, _head, arr, 0, _array.Length - _head);
Array.Copy(_array, 0, arr, _array.Length - _head, _tail);
}
return arr;
}
// PRIVATE Grows or shrinks the buffer to hold capacity objects. Capacity
// must be >= _size.
private void SetCapacity(int capacity)
{
object[] newarray = new object[capacity];
if (_size > 0)
{
if (_head < _tail)
{
Array.Copy(_array, _head, newarray, 0, _size);
}
else
{
Array.Copy(_array, _head, newarray, 0, _array.Length - _head);
Array.Copy(_array, 0, newarray, _array.Length - _head, _tail);
}
}
_array = newarray;
_head = 0;
_tail = (_size == capacity) ? 0 : _size;
_version++;
}
public virtual void TrimToSize()
{
SetCapacity(_size);
}
// Implements a synchronization wrapper around a queue.
private sealed class SynchronizedQueue : Queue
{
private readonly Queue _q;
private readonly object _root;
internal SynchronizedQueue(Queue q)
{
_q = q;
_root = _q.SyncRoot;
}
public override bool IsSynchronized
{
get { return true; }
}
public override object SyncRoot
{
get
{
return _root;
}
}
public override int Count
{
get
{
lock (_root)
{
return _q.Count;
}
}
}
public override void Clear()
{
lock (_root)
{
_q.Clear();
}
}
public override object Clone()
{
lock (_root)
{
return new SynchronizedQueue((Queue)_q.Clone());
}
}
public override bool Contains(object? obj)
{
lock (_root)
{
return _q.Contains(obj);
}
}
public override void CopyTo(Array array, int arrayIndex)
{
lock (_root)
{
_q.CopyTo(array, arrayIndex);
}
}
public override void Enqueue(object? value)
{
lock (_root)
{
_q.Enqueue(value);
}
}
public override object? Dequeue()
{
lock (_root)
{
return _q.Dequeue();
}
}
public override IEnumerator GetEnumerator()
{
lock (_root)
{
return _q.GetEnumerator();
}
}
public override object? Peek()
{
lock (_root)
{
return _q.Peek();
}
}
public override object?[] ToArray()
{
lock (_root)
{
return _q.ToArray();
}
}
public override void TrimToSize()
{
lock (_root)
{
_q.TrimToSize();
}
}
}
// Implements an enumerator for a Queue. The enumerator uses the
// internal version number of the list to ensure that no modifications are
// made to the list while an enumeration is in progress.
private sealed class QueueEnumerator : IEnumerator, ICloneable
{
private readonly Queue _q;
private int _index;
private readonly int _version;
private object? _currentElement;
internal QueueEnumerator(Queue q)
{
_q = q;
_version = _q._version;
_index = 0;
_currentElement = _q._array;
if (_q._size == 0)
_index = -1;
}
public object Clone() => MemberwiseClone();
public bool MoveNext()
{
if (_version != _q._version) throw new InvalidOperationException(SR.InvalidOperation_EnumFailedVersion);
if (_index < 0)
{
_currentElement = _q._array;
return false;
}
_currentElement = _q.GetElement(_index);
_index++;
if (_index == _q._size)
_index = -1;
return true;
}
public object? Current
{
get
{
if (_currentElement == _q._array)
{
if (_index == 0)
throw new InvalidOperationException(SR.InvalidOperation_EnumNotStarted);
else
throw new InvalidOperationException(SR.InvalidOperation_EnumEnded);
}
return _currentElement;
}
}
public void Reset()
{
if (_version != _q._version) throw new InvalidOperationException(SR.InvalidOperation_EnumFailedVersion);
if (_q._size == 0)
_index = -1;
else
_index = 0;
_currentElement = _q._array;
}
}
internal sealed class QueueDebugView
{
private readonly Queue _queue;
public QueueDebugView(Queue queue)
{
ArgumentNullException.ThrowIfNull(queue);
_queue = queue;
}
[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
public object?[] Items
{
get
{
return _queue.ToArray();
}
}
}
}
}