(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / corlib / System.Collections.Generic / Queue.cs
blobcddcf8290bae0672bae09ec4e6feb20ed18900e6
1 // -*- Mode: csharp; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 //
3 // System.Collections.Generic.Queue
4 //
5 // Author:
6 // Martin Baulig (martin@ximian.com)
7 //
8 // (C) 2003 Novell, Inc.
9 //
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:
21 //
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 //
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.
34 #if NET_2_0
35 using System;
36 using System.Runtime.InteropServices;
38 namespace System.Collections.Generic
40 [CLSCompliant(false)]
41 [ComVisible(false)]
42 public class Queue<T> : ICollection<T>, IEnumerable<T>,
43 ICollection, IEnumerable
45 int count;
46 protected int modified;
47 protected Node head;
48 Node tail;
50 public void Clear ()
52 head = tail = null;
53 count = 0;
54 modified++;
57 public void Enqueue (T item)
59 tail = new Node (tail, item);
60 if (head == null)
61 head = tail;
62 count++;
63 modified++;
66 public T Peek ()
68 if (head == null)
69 throw new ArgumentException ();
71 return head.Item;
74 public T Dequeue ()
76 if (head == null)
77 throw new ArgumentException ();
79 T retval = head.Item;
80 head = head.Next;
81 if (head == null)
82 tail = null;
83 count--;
84 modified++;
85 return retval;
88 public bool Contains (T item)
90 for (Node node = head; node != null; node = node.Next)
91 if (node.Item == item)
92 return true;
94 return false;
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 ()
119 int pos = 0;
120 T[] retval = new T [count];
121 for (Node node = head; node != null; node = node.Next)
122 retval [pos++] = node.Item;
124 return retval;
127 public void TrimToSize ()
130 public int Count {
131 get { return count; }
134 public bool IsSynchronized {
135 get { return false; }
138 public object SyncRoot {
139 get { return this; }
142 public bool IsReadOnly {
143 get { return false; }
146 public void Add (T item)
148 Enqueue (item);
151 public bool Remove (T item)
153 throw new NotImplementedException ();
156 public IEnumerator<T> GetEnumerator ()
158 return new Enumerator (this);
161 IEnumerator IEnumerable.GetEnumerator ()
163 return new Enumerator (this);
166 protected sealed class Node
168 public readonly T Item;
169 public readonly Node Next;
171 public Node (Node next, T item)
173 this.Next = next;
174 this.Item = item;
178 protected class Enumerator : IEnumerator<T>, IEnumerator
180 Queue<T> queue;
181 int modified;
182 Node current;
184 public Enumerator (Queue<T> queue)
186 this.queue = queue;
187 this.modified = queue.modified;
188 this.current = queue.head;
191 public T Current {
192 get {
193 if (queue.modified != modified)
194 throw new InvalidOperationException ();
195 if (current == null)
196 throw new ArgumentException ();
197 return current.Item;
201 object IEnumerator.Current {
202 get {
203 return Current;
207 public bool MoveNext ()
209 if (queue.modified != modified)
210 throw new InvalidOperationException ();
211 if (current == null)
212 throw new ArgumentException ();
214 current = current.Next;
215 return current != null;
218 public void Reset () {
219 if (queue.modified != modified)
220 throw new InvalidOperationException();
222 current = queue.head;
225 public void Dispose ()
227 modified = -1;
232 #endif