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
41 public class Stack
<T
> : IEnumerable
<T
>, ICollection
, IEnumerable
{
48 private readonly static int INITIAL_SIZE
= 16;
52 defaultCapacity
= INITIAL_SIZE
;
55 public Stack (int count
)
58 throw new ArgumentOutOfRangeException ("count");
60 defaultCapacity
= count
;
64 public Stack (IEnumerable
<T
> collection
)
66 if (collection
== null)
67 throw new ArgumentNullException ("collection");
69 ICollection
<T
> col
= collection
as ICollection
<T
>;
76 foreach (T t
in collection
)
79 defaultCapacity
= size
;
85 Array
.Clear (data
, 0, data
.Length
);
91 public bool Contains (T t
)
93 return data
!= null && Array
.IndexOf (data
, t
, 0, size
) != -1;
96 public void CopyTo (T
[] dest
, int idx
)
98 // this gets copied in the order that it is poped
100 Array
.Copy (data
, 0, dest
, idx
, size
);
101 Array
.Reverse (dest
, idx
, size
);
108 throw new InvalidOperationException ();
112 return data
[size
- 1];
118 throw new InvalidOperationException ();
122 return data
[-- size
];
125 public void Push (T t
)
127 if (size
== 0 || size
== data
.Length
)
128 Array
.Resize
<T
> (ref data
, size
== 0 ? INITIAL_SIZE
: 2 * size
);
135 public T
[] ToArray ()
137 T
[] copy
= new T
[size
];
142 public void TrimExcess ()
144 if (data
!= null && (size
< data
.Length
* 0.9))
145 Array
.Resize
<T
> (ref data
, size
== 0 ? defaultCapacity
: size
);
152 bool ICollection
.IsSynchronized
{
153 get { return false; }
156 object ICollection
.SyncRoot
{
160 void ICollection
.CopyTo (Array dest
, int idx
)
164 data
.CopyTo (dest
, idx
);
165 Array
.Reverse (dest
, idx
, size
);
167 } catch (ArrayTypeMismatchException
) {
168 throw new ArgumentException ();
172 public Enumerator
GetEnumerator ()
174 return new Enumerator (this);
177 IEnumerator
<T
> IEnumerable
<T
>.GetEnumerator ()
179 return GetEnumerator ();
182 IEnumerator IEnumerable
.GetEnumerator ()
184 return GetEnumerator ();
187 public struct Enumerator
: IEnumerator
<T
>, IEnumerator
, IDisposable
{
188 const int NOT_STARTED
= -2;
190 // this MUST be -1, because we depend on it in move next.
191 // we just decr the size, so, 0 - 1 == FINISHED
192 const int FINISHED
= -1;
198 internal Enumerator (Stack
<T
> t
)
205 // for some fucked up reason, MSFT added a useless dispose to this class
206 // It means that in foreach, we must still do a try/finally. Broken, very
208 public void Dispose ()
213 public bool MoveNext ()
215 if (ver
!= parent
.ver
)
216 throw new InvalidOperationException ();
221 return idx
!= FINISHED
&& -- idx
!= FINISHED
;
227 throw new InvalidOperationException ();
229 return parent
.data
[idx
];
233 void IEnumerator
.Reset ()
235 if (ver
!= parent
.ver
)
236 throw new InvalidOperationException ();
241 object IEnumerator
.Current
{
242 get { return Current; }