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.
13 using System.Runtime.InteropServices;
15 namespace System.Collections.Generic
19 public class Queue<T> : ICollection<T>, IEnumerable<T>,
20 ICollection, IEnumerable
23 protected int modified;
34 public void Enqueue (T item)
36 tail = new Node (tail, item);
46 throw new ArgumentException ();
54 throw new ArgumentException ();
65 public bool Contains (T item)
67 for (Node node = head; node != null; node = node.Next)
68 if (node.Item == item)
74 public virtual void CopyTo (T[] array, int start)
76 if (start + count >= array.Length)
77 throw new ArgumentException ();
79 for (Node node = head; node != null; node = node.Next)
80 array [start++] = node.Item;
83 void ICollection.CopyTo (Array array, int start)
85 if (start + count >= array.Length)
86 throw new ArgumentException ();
88 for (Node node = head; node != null; node = node.Next)
89 array.SetValue (node.Item, start++);
95 T[] retval = new T [count];
96 for (Node node = head; node != null; node = node.Next)
97 retval [pos++] = node.Item;
102 public void TrimToSize ()
106 get { return count; }
109 public bool IsSynchronized {
110 get { return false; }
113 public object SyncRoot {
117 public IEnumerator<T> GetEnumerator ()
119 return new Enumerator (this);
122 IEnumerator IEnumerable.GetEnumerator ()
124 return new Enumerator (this);
127 protected sealed class Node
129 public readonly T Item;
130 public readonly Node Next;
132 public Node (Node next, T item)
139 protected class Enumerator : IEnumerator<T>, IEnumerator
145 public Enumerator (Queue<T> queue)
148 this.modified = queue.modified;
149 this.current = queue.head;
154 if (queue.modified != modified)
155 throw new InvalidOperationException ();
157 throw new ArgumentException ();
162 object IEnumerator.Current {
168 public bool MoveNext ()
170 if (queue.modified != modified)
171 throw new InvalidOperationException ();
173 throw new ArgumentException ();
175 current = current.Next;
176 return current != null;
179 public void Reset () {
180 if (queue.modified != modified)
181 throw new InvalidOperationException();
183 current = queue.head;
186 public void Dispose ()