2 // System.Collections.Queue
5 // Ricardo Fernández Pascual
7 // (C) 2001 Ricardo Fernández Pascual
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:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
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.
34 using System
.Collections
;
35 using System
.Runtime
.InteropServices
;
37 namespace System
.Collections
{
40 [System
.Diagnostics
.DebuggerDisplay ("Count={Count}")]
41 [System
.Diagnostics
.DebuggerTypeProxy (typeof (CollectionDebuggerView
))]
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
)
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
69 foreach (object o
in col
)
73 public Queue (int capacity
, float growFactor
) {
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);
86 public virtual int Count
{
90 public virtual bool IsSynchronized
{
94 public virtual object SyncRoot
{
98 public virtual void CopyTo (Array array
, int index
)
101 throw new ArgumentNullException ("array");
104 throw new ArgumentOutOfRangeException ("index");
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
);
124 public virtual IEnumerator
GetEnumerator () {
125 return new QueueEnumerator (this);
130 public virtual object Clone () {
133 newQueue
= new Queue (this._array
.Length
);
134 newQueue
._growFactor
= _growFactor
;
136 Array
.Copy (this._array
, 0, newQueue
._array
, 0,
138 newQueue
._head
= this._head
;
139 newQueue
._size
= this._size
;
140 newQueue
._tail
= this._tail
;
145 public virtual void Clear () {
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
;
157 for (int i
= _head
; i
< tail
; i
++) {
158 if (_array
[i
% _array
.Length
] == null)
162 for (int i
= _head
; i
< tail
; i
++) {
163 if (obj
.Equals (_array
[i
% _array
.Length
]))
170 public virtual object Dequeue ()
174 throw new InvalidOperationException ();
175 object result
= _array
[_head
];
176 _array
[_head
] = null;
177 _head
= (_head
+ 1) % _array
.Length
;
182 public virtual void Enqueue (object obj
) {
184 if (_size
== _array
.Length
)
187 _tail
= (_tail
+1) % _array
.Length
;
192 public virtual object Peek () {
194 throw new InvalidOperationException ();
195 return _array
[_head
];
198 public static Queue
Synchronized (Queue queue
) {
200 throw new ArgumentNullException ("queue");
202 return new SyncQueue (queue
);
205 public virtual object[] ToArray () {
206 object[] ret
= new object[_size
];
211 public virtual void TrimToSize() {
213 object[] trimmed
= new object [_size
];
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
;
230 _tail
= _head
+ _size
;
235 private class SyncQueue
: Queue
{
238 internal SyncQueue (Queue queue
) {
242 public override int Count
{
250 public override bool IsSynchronized
{
256 public override object SyncRoot
{
258 return queue
.SyncRoot
;
262 public override void CopyTo (Array array
, int index
) {
264 queue
.CopyTo (array
, index
);
268 public override IEnumerator
GetEnumerator () {
270 return queue
.GetEnumerator ();
274 public override object Clone () {
276 return new SyncQueue((Queue
) queue
.Clone ());
281 public override bool IsReadOnly {
284 return queue.IsReadOnly;
290 public override void Clear () {
296 public override void TrimToSize () {
302 public override bool Contains (object obj
) {
304 return queue
.Contains (obj
);
308 public override object Dequeue () {
310 return queue
.Dequeue ();
314 public override void Enqueue (object obj
) {
320 public override object Peek () {
322 return queue
.Peek ();
326 public override object[] ToArray () {
328 return queue
.ToArray ();
334 private class QueueEnumerator
: IEnumerator
, ICloneable
{
336 private int _version
;
339 internal QueueEnumerator (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
;
353 public virtual object Current
{
355 if (_version
!= queue
._version
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!
377 public virtual void Reset () {
378 if (_version
!= queue
._version
) {
379 throw new InvalidOperationException();