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
42 public class Stack <T> : IEnumerable <T>, ICollection, IEnumerable {
49 private readonly static int INITIAL_SIZE = 16;
53 #if TARGET_JVM //to work around resize (null) bug
54 data = new T [INITIAL_SIZE];
56 defaultCapacity = INITIAL_SIZE;
59 public Stack (int count)
62 throw new ArgumentOutOfRangeException ("count");
64 defaultCapacity = count;
68 public Stack (IEnumerable <T> collection)
70 if (collection == null)
71 throw new ArgumentNullException ("collection");
73 ICollection <T> col = collection as ICollection <T>;
80 foreach (T t in collection)
83 defaultCapacity = size;
89 Array.Clear (data, 0, data.Length);
95 public bool Contains (T t)
97 return data != null && Array.IndexOf (data, t, 0, size) != -1;
100 public void CopyTo (T [] dest, int idx)
102 // this gets copied in the order that it is poped
104 Array.Copy (data, 0, dest, idx, size);
105 Array.Reverse (dest, idx, size);
112 throw new InvalidOperationException ();
116 return data [size - 1];
122 throw new InvalidOperationException ();
126 return data [-- size];
129 public void Push (T t)
131 if (size == 0 || size == data.Length)
132 Array.Resize <T> (ref data, size == 0 ? INITIAL_SIZE : 2 * size);
139 public T [] ToArray ()
141 T [] copy = new T [size];
146 public void TrimExcess ()
148 if (data != null && (size < data.Length * 0.9))
149 Array.Resize <T> (ref data, size == 0 ? defaultCapacity : size);
156 bool ICollection.IsSynchronized {
157 get { return false; }
160 object ICollection.SyncRoot {
164 void ICollection.CopyTo (Array dest, int idx)
168 data.CopyTo (dest, idx);
169 Array.Reverse (dest, idx, size);
171 } catch (ArrayTypeMismatchException) {
172 throw new ArgumentException ();
176 public Enumerator GetEnumerator ()
178 return new Enumerator (this);
181 IEnumerator <T> IEnumerable<T>.GetEnumerator ()
183 return GetEnumerator ();
186 IEnumerator IEnumerable.GetEnumerator ()
188 return GetEnumerator ();
192 public struct Enumerator : IEnumerator <T>, IEnumerator, IDisposable {
193 const int NOT_STARTED = -2;
195 // this MUST be -1, because we depend on it in move next.
196 // we just decr the size, so, 0 - 1 == FINISHED
197 const int FINISHED = -1;
203 internal Enumerator (Stack <T> t)
210 // for some fucked up reason, MSFT added a useless dispose to this class
211 // It means that in foreach, we must still do a try/finally. Broken, very
213 public void Dispose ()
218 public bool MoveNext ()
220 if (ver != parent.ver)
221 throw new InvalidOperationException ();
226 return idx != FINISHED && -- idx != FINISHED;
232 throw new InvalidOperationException ();
234 return parent.data [idx];
238 void IEnumerator.Reset ()
240 if (ver != parent.ver)
241 throw new InvalidOperationException ();
246 object IEnumerator.Current {
247 get { return Current; }