1 // -*- Mode: csharp; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3 // System.Collections.Generic.Queue
6 // Martin Baulig (martin@ximian.com)
8 // (C) 2003 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
42 public class Queue<T> : ICollection<T>, IEnumerable<T>,
43 ICollection, IEnumerable
46 protected int modified;
57 public void Enqueue (T item)
59 tail = new Node (tail, item);
69 throw new ArgumentException ();
77 throw new ArgumentException ();
88 public bool Contains (T item)
90 for (Node node = head; node != null; node = node.Next)
91 if (node.Item == item)
97 public virtual void CopyTo (T[] array, int start)
99 // re-ordered to avoid possible integer overflow
100 if (start >= array.Length - count)
101 throw new ArgumentException ();
103 for (Node node = head; node != null; node = node.Next)
104 array [start++] = node.Item;
107 void ICollection.CopyTo (Array array, int start)
109 // re-ordered to avoid possible integer overflow
110 if (start >= array.Length - count)
111 throw new ArgumentException ();
113 for (Node node = head; node != null; node = node.Next)
114 array.SetValue (node.Item, start++);
117 public T[] ToArray ()
120 T[] retval = new T [count];
121 for (Node node = head; node != null; node = node.Next)
122 retval [pos++] = node.Item;
127 public void TrimToSize ()
131 get { return count; }
134 public bool IsSynchronized {
135 get { return false; }
138 public object SyncRoot {
142 public IEnumerator<T> GetEnumerator ()
144 return new Enumerator (this);
147 IEnumerator IEnumerable.GetEnumerator ()
149 return new Enumerator (this);
152 protected sealed class Node
154 public readonly T Item;
155 public readonly Node Next;
157 public Node (Node next, T item)
164 protected class Enumerator : IEnumerator<T>, IEnumerator
170 public Enumerator (Queue<T> queue)
173 this.modified = queue.modified;
174 this.current = queue.head;
179 if (queue.modified != modified)
180 throw new InvalidOperationException ();
182 throw new ArgumentException ();
187 object IEnumerator.Current {
193 public bool MoveNext ()
195 if (queue.modified != modified)
196 throw new InvalidOperationException ();
198 throw new ArgumentException ();
200 current = current.Next;
201 return current != null;
204 public void Reset () {
205 if (queue.modified != modified)
206 throw new InvalidOperationException();
208 current = queue.head;
211 public void Dispose ()