2010-04-07 Jb Evain <jbevain@novell.com>
[mcs.git] / class / corlib / System.Collections / Queue.cs
blob5e359e821f4d5f1bf85e94360afa20587d2f0422
1 //
2 // System.Collections.Queue
3 //
4 // Author:
5 // Ricardo Fernández Pascual
6 //
7 // (C) 2001 Ricardo Fernández Pascual
8 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System;
34 using System.Collections;
35 using System.Runtime.InteropServices;
37 namespace System.Collections {
39 [ComVisible(true)]
40 [System.Diagnostics.DebuggerDisplay ("Count={Count}")]
41 [System.Diagnostics.DebuggerTypeProxy (typeof (CollectionDebuggerView))]
42 [Serializable]
43 #if INSIDE_CORLIB
44 public
45 #else
46 internal
47 #endif
48 class Queue : ICollection, IEnumerable, ICloneable {
50 private object[] _array;
51 private int _head = 0; // points to the first used slot
52 private int _size = 0;
53 private int _tail = 0;
54 private int _growFactor;
55 private int _version = 0;
57 public Queue () : this (32, 2.0F) {}
59 public Queue (int capacity) : this (capacity, 2.0F) {}
61 public Queue(ICollection col) : this (col == null ? 32 : col.Count)
63 if (col == null)
64 throw new ArgumentNullException ("col");
66 // We have to do this because msft seems to call the
67 // enumerator rather than CopyTo. This affects classes
68 // like bitarray.
69 foreach (object o in col)
70 Enqueue (o);
73 public Queue (int capacity, float growFactor) {
74 if (capacity < 0)
75 throw new ArgumentOutOfRangeException("capacity", "Needs a non-negative number");
76 if (!(growFactor >= 1.0F && growFactor <= 10.0F))
77 throw new ArgumentOutOfRangeException("growFactor", "Queue growth factor must be between 1.0 and 10.0, inclusive");
79 _array = new object[capacity];
81 this._growFactor = (int)(growFactor * 100);
84 // from ICollection
86 public virtual int Count {
87 get { return _size; }
90 public virtual bool IsSynchronized {
91 get { return false; }
94 public virtual object SyncRoot {
95 get { return this; }
98 public virtual void CopyTo (Array array, int index)
100 if (array == null)
101 throw new ArgumentNullException ("array");
103 if (index < 0)
104 throw new ArgumentOutOfRangeException ("index");
106 if (array.Rank > 1
107 || (index != 0 && index >= array.Length)
108 || _size > array.Length - index)
109 throw new ArgumentException ();
111 int contents_length = _array.Length;
112 int length_from_head = contents_length - _head;
113 // copy the _array of the circular array
114 Array.Copy (_array, _head, array, index,
115 Math.Min (_size, length_from_head));
116 if (_size > length_from_head)
117 Array.Copy (_array, 0, array,
118 index + length_from_head,
119 _size - length_from_head);
122 // from IEnumerable
124 public virtual IEnumerator GetEnumerator () {
125 return new QueueEnumerator (this);
128 // from ICloneable
130 public virtual object Clone () {
131 Queue newQueue;
133 newQueue = new Queue (this._array.Length);
134 newQueue._growFactor = _growFactor;
136 Array.Copy (this._array, 0, newQueue._array, 0,
137 this._array.Length);
138 newQueue._head = this._head;
139 newQueue._size = this._size;
140 newQueue._tail = this._tail;
142 return newQueue;
145 public virtual void Clear () {
146 _version++;
147 _head = 0;
148 _size = 0;
149 _tail = 0;
150 for (int length = _array.Length - 1; length >= 0; length--)
151 _array [length] = null;
154 public virtual bool Contains (object obj) {
155 int tail = _head + _size;
156 if (obj == null) {
157 for (int i = _head; i < tail; i++) {
158 if (_array[i % _array.Length] == null)
159 return true;
161 } else {
162 for (int i = _head; i < tail; i++) {
163 if (obj.Equals (_array[i % _array.Length]))
164 return true;
167 return false;
170 public virtual object Dequeue ()
172 _version++;
173 if (_size < 1)
174 throw new InvalidOperationException ();
175 object result = _array[_head];
176 _array [_head] = null;
177 _head = (_head + 1) % _array.Length;
178 _size--;
179 return result;
182 public virtual void Enqueue (object obj) {
183 _version++;
184 if (_size == _array.Length)
185 grow ();
186 _array[_tail] = obj;
187 _tail = (_tail+1) % _array.Length;
188 _size++;
192 public virtual object Peek () {
193 if (_size < 1)
194 throw new InvalidOperationException ();
195 return _array[_head];
198 public static Queue Synchronized (Queue queue) {
199 if (queue == null) {
200 throw new ArgumentNullException ("queue");
202 return new SyncQueue (queue);
205 public virtual object[] ToArray () {
206 object[] ret = new object[_size];
207 CopyTo (ret, 0);
208 return ret;
211 public virtual void TrimToSize() {
212 _version++;
213 object[] trimmed = new object [_size];
214 CopyTo (trimmed, 0);
215 _array = trimmed;
216 _head = 0;
217 _tail = 0;
220 // private methods
222 private void grow () {
223 int newCapacity = (_array.Length * _growFactor) / 100;
224 if (newCapacity < _array.Length + 1)
225 newCapacity = _array.Length + 1;
226 object[] newContents = new object[newCapacity];
227 CopyTo (newContents, 0);
228 _array = newContents;
229 _head = 0;
230 _tail = _head + _size;
233 // private classes
235 private class SyncQueue : Queue {
236 Queue queue;
238 internal SyncQueue (Queue queue) {
239 this.queue = queue;
242 public override int Count {
243 get {
244 lock (queue) {
245 return queue.Count;
250 public override bool IsSynchronized {
251 get {
252 return true;
256 public override object SyncRoot {
257 get {
258 return queue.SyncRoot;
262 public override void CopyTo (Array array, int index) {
263 lock (queue) {
264 queue.CopyTo (array, index);
268 public override IEnumerator GetEnumerator () {
269 lock (queue) {
270 return queue.GetEnumerator ();
274 public override object Clone () {
275 lock (queue) {
276 return new SyncQueue((Queue) queue.Clone ());
281 public override bool IsReadOnly {
282 get {
283 lock (queue) {
284 return queue.IsReadOnly;
290 public override void Clear () {
291 lock (queue) {
292 queue.Clear ();
296 public override void TrimToSize () {
297 lock (queue) {
298 queue.TrimToSize ();
302 public override bool Contains (object obj) {
303 lock (queue) {
304 return queue.Contains (obj);
308 public override object Dequeue () {
309 lock (queue) {
310 return queue.Dequeue ();
314 public override void Enqueue (object obj) {
315 lock (queue) {
316 queue.Enqueue (obj);
320 public override object Peek () {
321 lock (queue) {
322 return queue.Peek ();
326 public override object[] ToArray () {
327 lock (queue) {
328 return queue.ToArray ();
333 [Serializable]
334 private class QueueEnumerator : IEnumerator, ICloneable {
335 Queue queue;
336 private int _version;
337 private int current;
339 internal QueueEnumerator (Queue q) {
340 queue = q;
341 _version = q._version;
342 current = -1; // one element before the _head
345 public object Clone ()
347 QueueEnumerator q = new QueueEnumerator (queue);
348 q._version = _version;
349 q.current = current;
350 return q;
353 public virtual object Current {
354 get {
355 if (_version != queue._version
356 || current < 0
357 || current >= queue._size)
358 throw new InvalidOperationException ();
359 return queue._array[(queue._head + current) % queue._array.Length];
363 public virtual bool MoveNext () {
364 if (_version != queue._version) {
365 throw new InvalidOperationException ();
368 if (current >= queue._size - 1) {
369 current = Int32.MaxValue; // to late!
370 return false;
371 } else {
372 current++;
373 return true;
377 public virtual void Reset () {
378 if (_version != queue._version) {
379 throw new InvalidOperationException();
381 current = -1;