2009-07-22 Jb Evain <jbevain@novell.com>
[mono-project.git] / mcs / class / System / System.Collections.Generic / Stack.cs
blob6770831a9a37a57de43554843842eb15ad5d0d91
1 //
2 // System.Collections.Generic.Stack
3 //
4 // Authors:
5 // Martin Baulig (martin@ximian.com)
6 // Ben Maurer (bmaurer@ximian.com)
7 //
8 // (C) 2003, 2004 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 [ComVisible (false)]
41 [Serializable]
42 public class Stack <T> : IEnumerable <T>, ICollection, IEnumerable
44 T [] _array;
45 int _size;
46 int _version;
48 private const int INITIAL_SIZE = 16;
50 public Stack ()
54 public Stack (int count)
56 if (count < 0)
57 throw new ArgumentOutOfRangeException ("count");
59 _array = new T [count];
62 public Stack (IEnumerable <T> collection)
64 if (collection == null)
65 throw new ArgumentNullException ("collection");
67 ICollection <T> col = collection as ICollection <T>;
69 if (col != null) {
70 _size = col.Count;
71 _array = new T [_size];
72 col.CopyTo (_array, 0);
73 } else {
74 foreach (T t in collection)
75 Push (t);
79 public void Clear ()
81 if (_array != null)
82 Array.Clear (_array, 0, _array.Length);
84 _size = 0;
85 _version ++;
88 public bool Contains (T t)
90 return _array != null && Array.IndexOf (_array, t, 0, _size) != -1;
93 public void CopyTo (T [] dest, int idx)
95 if (dest == null)
96 throw new ArgumentNullException ("dest");
97 if (idx < 0)
98 throw new ArgumentOutOfRangeException ("idx");
100 // this gets copied in the order that it is poped
101 if (_array != null) {
102 Array.Copy (_array, 0, dest, idx, _size);
103 Array.Reverse (dest, idx, _size);
107 public T Peek ()
109 if (_size == 0)
110 throw new InvalidOperationException ();
112 return _array [_size - 1];
115 public T Pop ()
117 if (_size == 0)
118 throw new InvalidOperationException ();
120 _version ++;
121 T popped = _array [--_size];
122 // clear stuff out to make the GC happy
123 _array [_size] = default(T);
124 return popped;
127 public void Push (T t)
129 if (_array == null || _size == _array.Length)
130 Array.Resize <T> (ref _array, _size == 0 ? INITIAL_SIZE : 2 * _size);
132 _version ++;
134 _array [_size++] = t;
137 public T [] ToArray ()
139 T [] copy = new T [_size];
140 CopyTo (copy, 0);
141 return copy;
144 public void TrimExcess ()
146 if (_array != null && (_size < _array.Length * 0.9))
147 Array.Resize <T> (ref _array, _size);
148 _version ++;
151 public int Count {
152 get { return _size; }
155 bool ICollection.IsSynchronized {
156 get { return false; }
159 object ICollection.SyncRoot {
160 get { return this; }
163 void ICollection.CopyTo (Array dest, int idx)
165 try {
166 if (_array != null) {
167 _array.CopyTo (dest, idx);
168 Array.Reverse (dest, idx, _size);
170 } catch (ArrayTypeMismatchException) {
171 throw new ArgumentException ();
175 public Enumerator GetEnumerator ()
177 return new Enumerator (this);
180 IEnumerator <T> IEnumerable<T>.GetEnumerator ()
182 return GetEnumerator ();
185 IEnumerator IEnumerable.GetEnumerator ()
187 return GetEnumerator ();
190 [Serializable]
191 public struct Enumerator : IEnumerator <T>, IEnumerator, IDisposable {
192 const int NOT_STARTED = -2;
194 // this MUST be -1, because we depend on it in move next.
195 // we just decr the _size, so, 0 - 1 == FINISHED
196 const int FINISHED = -1;
198 Stack <T> parent;
199 int idx;
200 int _version;
202 internal Enumerator (Stack <T> t)
204 parent = t;
205 idx = NOT_STARTED;
206 _version = t._version;
209 // for some fucked up reason, MSFT added a useless dispose to this class
210 // It means that in foreach, we must still do a try/finally. Broken, very
211 // broken.
212 public void Dispose ()
214 idx = NOT_STARTED;
217 public bool MoveNext ()
219 if (_version != parent._version)
220 throw new InvalidOperationException ();
222 if (idx == -2)
223 idx = parent._size;
225 return idx != FINISHED && -- idx != FINISHED;
228 public T Current {
229 get {
230 if (idx < 0)
231 throw new InvalidOperationException ();
233 return parent._array [idx];
237 void IEnumerator.Reset ()
239 if (_version != parent._version)
240 throw new InvalidOperationException ();
242 idx = NOT_STARTED;
245 object IEnumerator.Current {
246 get { return Current; }
252 #endif