2 // System.Collections.Generic.Stack
5 // Martin Baulig (martin@ximian.com)
6 // Ben Maurer (bmaurer@ximian.com)
8 // (C) 2003, 2004 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
40 [CLSCompliant (false)]
42 public class Stack
<T
> : ICollection
<T
>, IEnumerable
<T
>, ICollection
, IEnumerable
{
52 public Stack (int count
)
55 throw new ArgumentOutOfRangeException ("count");
60 public Stack (IEnumerable
<T
> collection
)
62 if (collection
== null)
63 throw new ArgumentNullException ("collection");
65 ICollection
<T
> col
= collection
as ICollection
<T
>;
74 foreach (T t
in collection
)
83 Array
.Clear (data
, 0, data
.Length
);
89 public bool Contains (T t
)
91 return data
!= null && Array
.IndexOf (data
, t
, 0, size
) != -1;
94 public void CopyTo (T
[] dest
, int idx
)
96 // this gets copied in the order that it is poped
98 data
.CopyTo (dest
, idx
);
99 Array
.Reverse (dest
, idx
, size
);
106 throw new InvalidOperationException ();
110 return data
[size
- 1];
116 throw new InvalidOperationException ();
120 return data
[-- size
];
123 public void Push (T t
)
125 if (size
== 0 || size
== data
.Length
)
126 Array
.Resize
<T
> (ref data
, size
== 0 ? 10 : 2 * size
);
133 public T
[] ToArray ()
135 T
[] copy
= new T
[size
];
140 public void TrimToSize ()
142 // for some broken reason, msft increments the version here
148 Array
.Resize
<T
> (ref data
, size
);
155 bool ICollection
<T
>.IsReadOnly
{
156 get { return false; }
159 bool ICollection
.IsSynchronized
{
160 get { return false; }
163 object ICollection
.SyncRoot
{
167 void ICollection
<T
>.Add (T t
)
172 bool ICollection
<T
>.Remove (T t
)
174 throw new InvalidOperationException ("");
177 void ICollection
.CopyTo (Array dest
, int idx
)
181 data
.CopyTo (dest
, idx
);
182 Array
.Reverse (dest
, idx
, size
);
184 } catch (ArrayTypeMismatchException
) {
185 throw new ArgumentException ();
189 public Enumerator
<T
> GetEnumerator ()
191 return new Enumerator
<T
> (this);
194 IEnumerator
<T
> IEnumerable
<T
>.GetEnumerator ()
196 return GetEnumerator ();
199 IEnumerator IEnumerable
.GetEnumerator ()
201 return GetEnumerator ();
204 public struct Enumerator
<T
> : IEnumerator
<T
>, IEnumerator
, IDisposable
{
205 const int NOT_STARTED
= -2;
207 // this MUST be -1, because we depend on it in move next.
208 // we just decr the size, so, 0 - 1 == FINISHED
209 const int FINISHED
= -1;
215 internal Enumerator (Stack
<T
> t
)
222 // for some fucked up reason, MSFT added a useless dispose to this class
223 // It means that in foreach, we must still do a try/finally. Broken, very
225 public void Dispose ()
230 public bool MoveNext ()
232 if (ver
!= parent
.ver
)
233 throw new InvalidOperationException ();
238 return idx
!= FINISHED
&& -- idx
!= FINISHED
;
244 throw new InvalidOperationException ();
246 return parent
.data
[idx
];
250 void IEnumerator
.Reset ()
252 if (ver
!= parent
.ver
)
253 throw new InvalidOperationException ();
258 object IEnumerator
.Current
{
259 get { return Current; }