mscorlib(4.0.0.0) API with additions
Queue.cs
1 using System.Diagnostics;
4 using System.Threading;
5 
6 namespace System.Collections
7 {
10  [DebuggerTypeProxy(typeof(QueueDebugView))]
11  [DebuggerDisplay("Count = {Count}")]
12  [ComVisible(true)]
14  {
15  [Serializable]
16  private class SynchronizedQueue : Queue
17  {
18  private Queue _q;
19 
20  private object root;
21 
22  public override bool IsSynchronized => true;
23 
24  public override object SyncRoot => root;
25 
26  public override int Count
27  {
28  get
29  {
30  lock (root)
31  {
32  return _q.Count;
33  }
34  }
35  }
36 
37  internal SynchronizedQueue(Queue q)
38  {
39  _q = q;
40  root = _q.SyncRoot;
41  }
42 
43  public override void Clear()
44  {
45  lock (root)
46  {
47  _q.Clear();
48  }
49  }
50 
51  public override object Clone()
52  {
53  lock (root)
54  {
55  return new SynchronizedQueue((Queue)_q.Clone());
56  }
57  }
58 
59  public override bool Contains(object obj)
60  {
61  lock (root)
62  {
63  return _q.Contains(obj);
64  }
65  }
66 
67  public override void CopyTo(Array array, int arrayIndex)
68  {
69  lock (root)
70  {
71  _q.CopyTo(array, arrayIndex);
72  }
73  }
74 
75  public override void Enqueue(object value)
76  {
77  lock (root)
78  {
79  _q.Enqueue(value);
80  }
81  }
82 
83  public override object Dequeue()
84  {
85  lock (root)
86  {
87  return _q.Dequeue();
88  }
89  }
90 
91  public override IEnumerator GetEnumerator()
92  {
93  lock (root)
94  {
95  return _q.GetEnumerator();
96  }
97  }
98 
99  public override object Peek()
100  {
101  lock (root)
102  {
103  return _q.Peek();
104  }
105  }
106 
107  public override object[] ToArray()
108  {
109  lock (root)
110  {
111  return _q.ToArray();
112  }
113  }
114 
115  public override void TrimToSize()
116  {
117  lock (root)
118  {
119  _q.TrimToSize();
120  }
121  }
122  }
123 
124  [Serializable]
125  private class QueueEnumerator : IEnumerator, ICloneable
126  {
127  private Queue _q;
128 
129  private int _index;
130 
131  private int _version;
132 
133  private object currentElement;
134 
135  public virtual object Current
136  {
137  get
138  {
139  if (currentElement == _q._array)
140  {
141  if (_index == 0)
142  {
143  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumNotStarted"));
144  }
145  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumEnded"));
146  }
147  return currentElement;
148  }
149  }
150 
151  internal QueueEnumerator(Queue q)
152  {
153  _q = q;
154  _version = _q._version;
155  _index = 0;
156  currentElement = _q._array;
157  if (_q._size == 0)
158  {
159  _index = -1;
160  }
161  }
162 
163  public object Clone()
164  {
165  return MemberwiseClone();
166  }
167 
168  public virtual bool MoveNext()
169  {
170  if (_version != _q._version)
171  {
172  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumFailedVersion"));
173  }
174  if (_index < 0)
175  {
176  currentElement = _q._array;
177  return false;
178  }
179  currentElement = _q.GetElement(_index);
180  _index++;
181  if (_index == _q._size)
182  {
183  _index = -1;
184  }
185  return true;
186  }
187 
188  public virtual void Reset()
189  {
190  if (_version != _q._version)
191  {
192  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumFailedVersion"));
193  }
194  if (_q._size == 0)
195  {
196  _index = -1;
197  }
198  else
199  {
200  _index = 0;
201  }
202  currentElement = _q._array;
203  }
204  }
205 
206  internal class QueueDebugView
207  {
208  private Queue queue;
209 
210  [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
211  public object[] Items
212  {
213  get
214  {
215  return queue.ToArray();
216  }
217  }
218 
219  public QueueDebugView(Queue queue)
220  {
221  if (queue == null)
222  {
223  throw new ArgumentNullException("queue");
224  }
225  this.queue = queue;
226  }
227  }
228 
229  private object[] _array;
230 
231  private int _head;
232 
233  private int _tail;
234 
235  private int _size;
236 
237  private int _growFactor;
238 
239  private int _version;
240 
241  [NonSerialized]
242  private object _syncRoot;
243 
244  private const int _MinimumGrow = 4;
245 
246  private const int _ShrinkThreshold = 32;
247 
250  public virtual int Count => _size;
251 
255  public virtual bool IsSynchronized => false;
256 
259  public virtual object SyncRoot
260  {
261  get
262  {
263  if (_syncRoot == null)
264  {
265  Interlocked.CompareExchange(ref _syncRoot, new object(), null);
266  }
267  return _syncRoot;
268  }
269  }
270 
272  public Queue()
273  : this(32, 2f)
274  {
275  }
276 
281  public Queue(int capacity)
282  : this(capacity, 2f)
283  {
284  }
285 
292  public Queue(int capacity, float growFactor)
293  {
294  if (capacity < 0)
295  {
296  throw new ArgumentOutOfRangeException("capacity", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
297  }
298  if (!((double)growFactor >= 1.0) || !((double)growFactor <= 10.0))
299  {
300  throw new ArgumentOutOfRangeException("growFactor", Environment.GetResourceString("ArgumentOutOfRange_QueueGrowFactor", 1, 10));
301  }
302  _array = new object[capacity];
303  _head = 0;
304  _tail = 0;
305  _size = 0;
306  _growFactor = (int)(growFactor * 100f);
307  }
308 
313  public Queue(ICollection col)
314  : this(col?.Count ?? 32)
315  {
316  if (col == null)
317  {
318  throw new ArgumentNullException("col");
319  }
320  IEnumerator enumerator = col.GetEnumerator();
321  while (enumerator.MoveNext())
322  {
323  Enqueue(enumerator.Current);
324  }
325  }
326 
329  public virtual object Clone()
330  {
331  Queue queue = new Queue(_size);
332  queue._size = _size;
333  int size = _size;
334  int num = (_array.Length - _head < size) ? (_array.Length - _head) : size;
335  Array.Copy(_array, _head, queue._array, 0, num);
336  size -= num;
337  if (size > 0)
338  {
339  Array.Copy(_array, 0, queue._array, _array.Length - _head, size);
340  }
341  queue._version = _version;
342  return queue;
343  }
344 
346  public virtual void Clear()
347  {
348  if (_head < _tail)
349  {
350  Array.Clear(_array, _head, _size);
351  }
352  else
353  {
354  Array.Clear(_array, _head, _array.Length - _head);
355  Array.Clear(_array, 0, _tail);
356  }
357  _head = 0;
358  _tail = 0;
359  _size = 0;
360  _version++;
361  }
362 
373  public virtual void CopyTo(Array array, int index)
374  {
375  if (array == null)
376  {
377  throw new ArgumentNullException("array");
378  }
379  if (array.Rank != 1)
380  {
381  throw new ArgumentException(Environment.GetResourceString("Arg_RankMultiDimNotSupported"));
382  }
383  if (index < 0)
384  {
385  throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index"));
386  }
387  int length = array.Length;
388  if (length - index < _size)
389  {
390  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
391  }
392  int size = _size;
393  if (size != 0)
394  {
395  int num = (_array.Length - _head < size) ? (_array.Length - _head) : size;
396  Array.Copy(_array, _head, array, index, num);
397  size -= num;
398  if (size > 0)
399  {
400  Array.Copy(_array, 0, array, index + _array.Length - _head, size);
401  }
402  }
403  }
404 
407  public virtual void Enqueue(object obj)
408  {
409  if (_size == _array.Length)
410  {
411  int num = (int)((long)_array.Length * (long)_growFactor / 100);
412  if (num < _array.Length + 4)
413  {
414  num = _array.Length + 4;
415  }
416  SetCapacity(num);
417  }
418  _array[_tail] = obj;
419  _tail = (_tail + 1) % _array.Length;
420  _size++;
421  _version++;
422  }
423 
426  public virtual IEnumerator GetEnumerator()
427  {
428  return new QueueEnumerator(this);
429  }
430 
434  public virtual object Dequeue()
435  {
436  if (Count == 0)
437  {
438  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EmptyQueue"));
439  }
440  object result = _array[_head];
441  _array[_head] = null;
442  _head = (_head + 1) % _array.Length;
443  _size--;
444  _version++;
445  return result;
446  }
447 
451  public virtual object Peek()
452  {
453  if (Count == 0)
454  {
455  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EmptyQueue"));
456  }
457  return _array[_head];
458  }
459 
465  [HostProtection(SecurityAction.LinkDemand, Synchronization = true)]
466  public static Queue Synchronized(Queue queue)
467  {
468  if (queue == null)
469  {
470  throw new ArgumentNullException("queue");
471  }
472  return new SynchronizedQueue(queue);
473  }
474 
479  public virtual bool Contains(object obj)
480  {
481  int num = _head;
482  int num2 = _size;
483  while (num2-- > 0)
484  {
485  if (obj == null)
486  {
487  if (_array[num] == null)
488  {
489  return true;
490  }
491  }
492  else if (_array[num] != null && _array[num].Equals(obj))
493  {
494  return true;
495  }
496  num = (num + 1) % _array.Length;
497  }
498  return false;
499  }
500 
501  internal object GetElement(int i)
502  {
503  return _array[(_head + i) % _array.Length];
504  }
505 
508  public virtual object[] ToArray()
509  {
510  object[] array = new object[_size];
511  if (_size == 0)
512  {
513  return array;
514  }
515  if (_head < _tail)
516  {
517  Array.Copy(_array, _head, array, 0, _size);
518  }
519  else
520  {
521  Array.Copy(_array, _head, array, 0, _array.Length - _head);
522  Array.Copy(_array, 0, array, _array.Length - _head, _tail);
523  }
524  return array;
525  }
526 
527  private void SetCapacity(int capacity)
528  {
529  object[] array = new object[capacity];
530  if (_size > 0)
531  {
532  if (_head < _tail)
533  {
534  Array.Copy(_array, _head, array, 0, _size);
535  }
536  else
537  {
538  Array.Copy(_array, _head, array, 0, _array.Length - _head);
539  Array.Copy(_array, 0, array, _array.Length - _head, _tail);
540  }
541  }
542  _array = array;
543  _head = 0;
544  _tail = ((_size != capacity) ? _size : 0);
545  _version++;
546  }
547 
550  public virtual void TrimToSize()
551  {
552  SetCapacity(_size);
553  }
554  }
555 }
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
bool MoveNext()
Advances the enumerator to the next element of the collection.
virtual void Clear()
Removes all objects from the T:System.Collections.Queue.
Definition: Queue.cs:346
static void Clear(Array array, int index, int length)
Sets a range of elements in an array to the default value of each element type.
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
virtual object Clone()
Creates a shallow copy of the T:System.Collections.Queue.
Definition: Queue.cs:329
virtual object Peek()
Returns the object at the beginning of the T:System.Collections.Queue without removing it.
Definition: Queue.cs:451
virtual void Enqueue(object obj)
Adds an object to the end of the T:System.Collections.Queue.
Definition: Queue.cs:407
virtual object [] ToArray()
Copies the T:System.Collections.Queue elements to a new array.
Definition: Queue.cs:508
Queue(int capacity, float growFactor)
Initializes a new instance of the T:System.Collections.Queue class that is empty, has the specified i...
Definition: Queue.cs:292
virtual bool Contains(object obj)
Determines whether an element is in the T:System.Collections.Queue.
Definition: Queue.cs:479
SecurityAction
Specifies the security actions that can be performed using declarative security.
Exposes an enumerator, which supports a simple iteration over a non-generic collection....
Definition: IEnumerable.cs:9
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
static Queue Synchronized(Queue queue)
Returns a new T:System.Collections.Queue that wraps the original queue, and is thread safe.
Definition: Queue.cs:466
virtual int Count
Gets the number of elements contained in the T:System.Collections.Queue.
Definition: Queue.cs:250
static int CompareExchange(ref int location1, int value, int comparand)
Compares two 32-bit signed integers for equality and, if they are equal, replaces the first value.
object Current
Gets the element in the collection at the current position of the enumerator.
Definition: IEnumerator.cs:15
virtual IEnumerator GetEnumerator()
Returns an enumerator that iterates through the T:System.Collections.Queue.
Definition: Queue.cs:426
Supports cloning, which creates a new instance of a class with the same value as an existing instance...
Definition: ICloneable.cs:7
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition: Array.cs:17
Represents a first-in, first-out collection of objects.
Definition: Queue.cs:13
virtual object SyncRoot
Gets an object that can be used to synchronize access to the T:System.Collections....
Definition: Queue.cs:260
IEnumerator GetEnumerator()
Returns an enumerator that iterates through a collection.
The exception that is thrown when one of the arguments provided to a method is not valid.
static void Copy(Array sourceArray, Array destinationArray, int length)
Copies a range of elements from an T:System.Array starting at the first element and pastes them into ...
Definition: Array.cs:1275
virtual void CopyTo(Array array, int index)
Copies the T:System.Collections.Queue elements to an existing one-dimensional T:System....
Definition: Queue.cs:373
virtual bool IsSynchronized
Gets a value indicating whether access to the T:System.Collections.Queue is synchronized (thread safe...
Definition: Queue.cs:255
Specifies that the class can be serialized.
The exception that is thrown when a method call is invalid for the object's current state.
Queue()
Initializes a new instance of the T:System.Collections.Queue class that is empty, has the default ini...
Definition: Queue.cs:272
Defines size, enumerators, and synchronization methods for all nongeneric collections.
Definition: ICollection.cs:8
virtual void TrimToSize()
Sets the capacity to the actual number of elements in the T:System.Collections.Queue.
Definition: Queue.cs:550
virtual object Dequeue()
Removes and returns the object at the beginning of the T:System.Collections.Queue.
Definition: Queue.cs:434
DebuggerBrowsableState
Provides display instructions for the debugger.
Provides atomic operations for variables that are shared by multiple threads.
Definition: Interlocked.cs:10
Supports a simple iteration over a non-generic collection.
Definition: IEnumerator.cs:9
Queue(int capacity)
Initializes a new instance of the T:System.Collections.Queue class that is empty, has the specified i...
Definition: Queue.cs:281