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
48 private const int INITIAL_SIZE
= 16;
54 public Stack (int count
)
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
>;
71 _array
= new T
[_size
];
72 col
.CopyTo (_array
, 0);
74 foreach (T t
in collection
)
82 Array
.Clear (_array
, 0, _array
.Length
);
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
)
96 throw new ArgumentNullException ("dest");
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
);
110 throw new InvalidOperationException ();
112 return _array
[_size
- 1];
118 throw new InvalidOperationException ();
121 T popped
= _array
[--_size
];
122 // clear stuff out to make the GC happy
123 _array
[_size
] = default(T
);
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
);
134 _array
[_size
++] = t
;
137 public T
[] ToArray ()
139 T
[] copy
= new T
[_size
];
144 public void TrimExcess ()
146 if (_array
!= null && (_size
< _array
.Length
* 0.9))
147 Array
.Resize
<T
> (ref _array
, _size
);
152 get { return _size; }
155 bool ICollection
.IsSynchronized
{
156 get { return false; }
159 object ICollection
.SyncRoot
{
163 void ICollection
.CopyTo (Array dest
, int idx
)
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 ();
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;
202 internal Enumerator (Stack
<T
> t
)
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
212 public void Dispose ()
217 public bool MoveNext ()
219 if (_version
!= parent
._version
)
220 throw new InvalidOperationException ();
225 return idx
!= FINISHED
&& -- idx
!= FINISHED
;
231 throw new InvalidOperationException ();
233 return parent
._array
[idx
];
237 void IEnumerator
.Reset ()
239 if (_version
!= parent
._version
)
240 throw new InvalidOperationException ();
245 object IEnumerator
.Current
{
246 get { return Current; }