(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / corlib / System.Collections / Queue.cs
blob480f950f51a3e53b7b515db06a18385788f955a0
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;
36 namespace System.Collections {
38 [Serializable]
39 public class Queue : ICollection, IEnumerable, ICloneable {
41 private object[] _array;
42 private int _head = 0; // points to the first used slot
43 private int _size = 0;
44 private int _tail = 0;
45 private int _growFactor;
46 private int _version = 0;
48 public Queue () : this (32, 2.0F) {}
49 public Queue (int initialCapacity) : this (initialCapacity, 2.0F) {}
50 public Queue(ICollection col) : this (col == null ? 32 : col.Count)
52 if (col == null)
53 throw new ArgumentNullException ("col");
55 _size = _array.Length;
56 _tail = _size;
57 col.CopyTo (_array, 0);
60 public Queue (int initialCapacity, float growFactor) {
61 if (initialCapacity < 0)
62 throw new ArgumentOutOfRangeException("capacity", "Needs a non-negative number");
63 if (!(growFactor >= 1.0F && growFactor <= 10.0F))
64 throw new ArgumentOutOfRangeException("growFactor", "Queue growth factor must be between 1.0 and 10.0, inclusive");
66 _array = new object[initialCapacity];
68 this._growFactor = (int)(growFactor * 100);
71 // from ICollection
73 public virtual int Count {
74 get { return _size; }
77 public virtual bool IsSynchronized {
78 get { return false; }
81 public virtual object SyncRoot {
82 get { return this; }
85 public virtual void CopyTo (Array array, int index)
87 if (array == null)
88 throw new ArgumentNullException ("array");
90 if (index < 0)
91 throw new ArgumentOutOfRangeException ("index");
93 if (array.Rank > 1
94 || (index != 0 && index >= array.Length)
95 || _size > array.Length - index)
96 throw new ArgumentException ();
98 int contents_length = _array.Length;
99 int length_from_head = contents_length - _head;
100 // copy the _array of the circular array
101 Array.Copy (_array, _head, array, index,
102 Math.Min (_size, length_from_head));
103 if (_size > length_from_head)
104 Array.Copy (_array, 0, array,
105 index + length_from_head,
106 _size - length_from_head);
109 // from IEnumerable
111 public virtual IEnumerator GetEnumerator () {
112 return new QueueEnumerator (this);
115 // from ICloneable
117 public virtual object Clone () {
118 Queue newQueue;
120 newQueue = new Queue (this._array.Length);
121 newQueue._growFactor = _growFactor;
123 Array.Copy (this._array, 0, newQueue._array, 0,
124 this._array.Length);
125 newQueue._head = this._head;
126 newQueue._size = this._size;
127 newQueue._tail = this._tail;
129 return newQueue;
132 public virtual void Clear () {
133 _version++;
134 _head = 0;
135 _size = 0;
136 _tail = 0;
137 for (int length = _array.Length - 1; length >= 0; length--)
138 _array [length] = null;
141 public virtual bool Contains (object obj) {
142 int tail = _head + _size;
143 if (obj == null) {
144 for (int i = _head; i < tail; i++) {
145 if (_array[i % _array.Length] == null)
146 return true;
148 } else {
149 for (int i = _head; i < tail; i++) {
150 if (obj.Equals (_array[i % _array.Length]))
151 return true;
154 return false;
157 public virtual object Dequeue ()
159 _version++;
160 if (_size < 1)
161 throw new InvalidOperationException ();
162 object result = _array[_head];
163 _array [_head] = null;
164 _head = (_head + 1) % _array.Length;
165 _size--;
166 return result;
169 public virtual void Enqueue (object obj) {
170 _version++;
171 if (_size == _array.Length)
172 grow ();
173 _array[_tail] = obj;
174 _tail = (_tail+1) % _array.Length;
175 _size++;
179 public virtual object Peek () {
180 if (_size < 1)
181 throw new InvalidOperationException ();
182 return _array[_head];
185 public static Queue Synchronized (Queue queue) {
186 if (queue == null) {
187 throw new ArgumentNullException ("queue");
189 return new SyncQueue (queue);
192 public virtual object[] ToArray () {
193 object[] ret = new object[_size];
194 CopyTo (ret, 0);
195 return ret;
198 public virtual void TrimToSize() {
199 _version++;
200 object[] trimmed = new object [_size];
201 CopyTo (trimmed, 0);
202 _array = trimmed;
203 _head = 0;
204 _tail = _head + _size;
207 // private methods
209 private void grow () {
210 int newCapacity = (_array.Length * _growFactor) / 100;
211 object[] newContents = new object[newCapacity];
212 CopyTo (newContents, 0);
213 _array = newContents;
214 _head = 0;
215 _tail = _head + _size;
218 // private classes
220 private class SyncQueue : Queue {
221 Queue queue;
223 internal SyncQueue (Queue queue) {
224 this.queue = queue;
227 public override int Count {
228 get {
229 lock (queue) {
230 return queue.Count;
235 public override bool IsSynchronized {
236 get {
237 return true;
241 public override object SyncRoot {
242 get {
243 return queue.SyncRoot;
247 public override void CopyTo (Array array, int index) {
248 lock (queue) {
249 queue.CopyTo (array, index);
253 public override IEnumerator GetEnumerator () {
254 lock (queue) {
255 return queue.GetEnumerator ();
259 public override object Clone () {
260 lock (queue) {
261 return new SyncQueue((Queue) queue.Clone ());
266 public override bool IsReadOnly {
267 get {
268 lock (queue) {
269 return queue.IsReadOnly;
275 public override void Clear () {
276 lock (queue) {
277 queue.Clear ();
281 public override void TrimToSize () {
282 lock (queue) {
283 queue.TrimToSize ();
287 public override bool Contains (object obj) {
288 lock (queue) {
289 return queue.Contains (obj);
293 public override object Dequeue () {
294 lock (queue) {
295 return queue.Dequeue ();
299 public override void Enqueue (object obj) {
300 lock (queue) {
301 queue.Enqueue (obj);
305 public override object Peek () {
306 lock (queue) {
307 return queue.Peek ();
311 public override object[] ToArray () {
312 lock (queue) {
313 return queue.ToArray ();
318 [Serializable]
319 private class QueueEnumerator : IEnumerator, ICloneable {
320 Queue queue;
321 private int _version;
322 private int current;
324 internal QueueEnumerator (Queue q) {
325 queue = q;
326 _version = q._version;
327 current = -1; // one element before the _head
330 public object Clone ()
332 QueueEnumerator q = new QueueEnumerator (queue);
333 q._version = _version;
334 q.current = current;
335 return q;
338 public virtual object Current {
339 get {
340 if (_version != queue._version
341 || current < 0
342 || current >= queue._size)
343 throw new InvalidOperationException ();
344 return queue._array[(queue._head + current) % queue._array.Length];
348 public virtual bool MoveNext () {
349 if (_version != queue._version) {
350 throw new InvalidOperationException ();
353 if (current >= queue._size - 1) {
354 current = Int32.MaxValue; // to late!
355 return false;
356 } else {
357 current++;
358 return true;
362 public virtual void Reset () {
363 if (_version != queue._version) {
364 throw new InvalidOperationException();
366 current = -1;