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 bool IsReadOnly
{
143 get { return false; }
146 public void Add (T 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
)
178 protected class Enumerator
: IEnumerator
<T
>, IEnumerator
184 public Enumerator (Queue
<T
> queue
)
187 this.modified
= queue
.modified
;
188 this.current
= queue
.head
;
193 if (queue
.modified
!= modified
)
194 throw new InvalidOperationException ();
196 throw new ArgumentException ();
201 object IEnumerator
.Current
{
207 public bool MoveNext ()
209 if (queue
.modified
!= modified
)
210 throw new InvalidOperationException ();
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 ()