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;
24 protected Node<T> head;
34 public void Enqueue (T item)
36 tail = new Node<T> (tail, item);
46 throw new ArgumentException ();
54 throw new ArgumentException ();
65 public bool Contains (T item)
67 for (Node<T> 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<T> node = head; node != null; node = node.Next)
80 array [start++] = node.Item;
86 T[] retval = new T [count];
87 for (Node<T> node = head; node != null; node = node.Next)
88 retval [pos++] = node.Item;
93 public void TrimToSize ()
100 public IEnumerator<T> GetEnumerator ()
102 return new Enumerator (this);
105 protected sealed class Node<T>
107 public readonly T Item;
108 public readonly Node<T> Next;
110 public Node (Node<T> next, T item)
117 protected class Enumerator : IEnumerator<T>
123 public Enumerator (Queue<T> queue)
126 this.modified = queue.modified;
127 this.current = queue.head;
132 if (queue.modified != modified)
133 throw new InvalidOperationException ();
135 throw new ArgumentException ();
140 public bool MoveNext ()
142 if (queue.modified != modified)
143 throw new InvalidOperationException ();
145 throw new ArgumentException ();
147 current = current.Next;
148 return current != null;
151 public void Dispose ()