2 // System.Collections.Generic.Queue
5 // Martin Baulig (martin@ximian.com)
6 // Ben Maurer (bmaurer@ximian.com)
8 // (C) 2003, 2004 Novell, Inc.
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using System.Runtime.InteropServices;
38 namespace System.Collections.Generic
41 public class Queue<T> : ICollection<T>, ICollection
53 public Queue (int count)
56 throw new ArgumentOutOfRangeException ("count");
61 public Queue (IEnumerable <T> collection)
63 if (collection == null)
64 throw new ArgumentNullException ("collection");
66 foreach (T t in collection)
73 Array.Clear (data, 0, data.Length);
75 head = tail = size = 0;
78 public bool Contains (T item)
93 public void CopyTo (T [] array, int idx)
96 throw new ArgumentNullException ();
98 if ((uint) idx > (uint) array.Length)
99 throw new ArgumentOutOfRangeException ();
101 if (array.Length - idx < size)
102 throw new ArgumentOutOfRangeException ();
107 int contents_length = data.Length;
108 int length_from_head = contents_length - head;
110 Array.Copy (data, head, array, idx, Math.Min (size, length_from_head));
111 if (size > length_from_head)
112 Array.Copy (data, 0, array,
113 idx + length_from_head,
114 size - length_from_head);
118 void ICollection.CopyTo (Array array, int idx)
121 throw new ArgumentNullException ();
123 if ((uint) idx < (uint) array.Length)
124 throw new ArgumentOutOfRangeException ();
126 if (array.Length - idx < size)
127 throw new ArgumentOutOfRangeException ();
133 int contents_length = data.Length;
134 int length_from_head = contents_length - head;
136 Array.Copy (data, head, array, idx, Math.Min (size, length_from_head));
137 if (size > length_from_head)
138 Array.Copy (data, 0, array,
139 idx + length_from_head,
140 size - length_from_head);
141 } catch (ArrayTypeMismatchException) {
142 throw new ArgumentException ();
150 // clear stuff out to make the GC happy
151 data [head] = default (T);
153 if (++head == data.Length)
164 throw new InvalidOperationException ();
169 public void Enqueue (T item)
171 if (data == null || size == data.Length)
172 SetCapacity (Math.Max (size * 2, 4));
176 if (++tail == data.Length)
183 public T [] ToArray ()
185 T [] t = new T [size];
190 public void TrimToSize ()
195 void SetCapacity (int new_size)
197 if (data != null && new_size == data.Length)
201 throw new InvalidOperationException ("shouldnt happen");
203 T [] new_data = new T [new_size];
205 CopyTo (new_data, 0);
218 bool ICollection <T>.IsReadOnly {
219 get { return false; }
222 bool ICollection.IsSynchronized {
223 get { return false; }
226 object ICollection.SyncRoot {
230 void ICollection <T>.Add (T t)
235 bool ICollection <T>.Remove (T t)
237 throw new InvalidOperationException ("");
241 public Enumerator GetEnumerator ()
243 return new Enumerator (this);
246 IEnumerator <T> IEnumerable<T>.GetEnumerator ()
248 return GetEnumerator ();
251 IEnumerator IEnumerable.GetEnumerator ()
253 return GetEnumerator ();
256 public struct Enumerator : IEnumerator <T>, IEnumerator, IDisposable {
257 const int NOT_STARTED = -2;
259 // this MUST be -1, because we depend on it in move next.
260 // we just decr the size, so, 0 - 1 == FINISHED
261 const int FINISHED = -1;
267 internal Enumerator (Queue <T> q)
274 // for some fucked up reason, MSFT added a useless dispose to this class
275 // It means that in foreach, we must still do a try/finally. Broken, very
277 public void Dispose ()
282 public bool MoveNext ()
284 if (ver != q.version)
285 throw new InvalidOperationException ();
287 if (idx == NOT_STARTED)
290 return idx != FINISHED && -- idx != FINISHED;
296 throw new InvalidOperationException ();
298 return q.data [(q.size - 1 - idx + q.head) % q.data.Length];
302 void IEnumerator.Reset ()
304 if (ver != q.version)
305 throw new InvalidOperationException ();
310 object IEnumerator.Current {
311 get { return Current; }