2 // System.Collections.Stack
5 // Garrett Rooney (rooneg@electricjellyfish.net)
7 // (C) 2001 Garrett Rooney
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 namespace System.Collections {
35 [MonoTODO ("Fix serialization compatibility with MS.NET")]
36 public class Stack : ICollection, IEnumerable, ICloneable {
39 private object[] contents;
40 private int current = -1;
41 private int count = 0;
43 private int modCount = 0;
45 private void Resize(int ncapacity) {
47 ncapacity = Math.Max (ncapacity, 16);
48 object[] ncontents = new object[ncapacity];
50 Array.Copy(contents, ncontents, count);
56 public Stack () : this (16) {}
58 public Stack(ICollection col) : this (col == null ? 16 : col.Count) {
60 throw new ArgumentNullException("col");
62 // We have to do this because msft seems to call the
63 // enumerator rather than CopyTo. This affects classes
65 foreach (object o in col)
69 public Stack (int initialCapacity) {
70 if (initialCapacity < 0)
71 throw new ArgumentOutOfRangeException ("initialCapacity");
73 capacity = Math.Max (initialCapacity, 16);
74 contents = new object[capacity];
78 private class SyncStack : Stack {
82 internal SyncStack(Stack s) {
86 public override int Count {
95 public override bool IsReadOnly {
98 return stack.IsReadOnly;
104 public override bool IsSynchronized {
108 public override object SyncRoot {
109 get { return stack.SyncRoot; }
112 public override void Clear() {
113 lock(stack) { stack.Clear(); }
116 public override object Clone() {
118 return Stack.Synchronized((Stack)stack.Clone());
122 public override bool Contains(object obj) {
123 lock (stack) { return stack.Contains(obj); }
126 public override void CopyTo(Array array, int index) {
127 lock (stack) { stack.CopyTo(array, index); }
130 public override IEnumerator GetEnumerator() {
132 return new Enumerator(stack);
136 public override object Peek() {
137 lock (stack) { return stack.Peek(); }
140 public override object Pop() {
141 lock (stack) { return stack.Pop(); }
144 public override void Push(object obj) {
145 lock (stack) { stack.Push(obj); }
148 public override object[] ToArray() {
149 lock (stack) { return stack.ToArray(); }
153 public static Stack Synchronized(Stack s) {
155 throw new ArgumentNullException();
158 return new SyncStack(s);
161 public virtual int Count {
162 get { return count; }
166 public virtual bool IsReadOnly {
167 get { return false; }
171 public virtual bool IsSynchronized {
172 get { return false; }
175 public virtual object SyncRoot {
179 public virtual void Clear() {
182 for (int i = 0; i < count; i++) {
190 public virtual object Clone() {
191 Stack stack = new Stack (contents);
192 stack.current = current;
197 public virtual bool Contains(object obj) {
202 for (int i = 0; i < count; i++) {
203 if (contents[i] == null)
207 for (int i = 0; i < count; i++) {
208 if (obj.Equals (contents[i]))
216 public virtual void CopyTo (Array array, int index) {
218 throw new ArgumentNullException("array");
222 throw new ArgumentOutOfRangeException("index");
225 if (array.Rank > 1 ||
226 array.Length > 0 && index >= array.Length ||
227 count > array.Length - index) {
228 throw new ArgumentException();
231 for (int i = current; i != -1; i--) {
232 array.SetValue(contents[i],
233 count - (i + 1) + index);
237 private class Enumerator : IEnumerator, ICloneable {
243 private int modCount;
246 internal Enumerator(Stack s) {
248 modCount = s.modCount;
252 public object Clone ()
254 return MemberwiseClone ();
257 public virtual object Current {
259 if (modCount != stack.modCount
262 || current > stack.count)
263 throw new InvalidOperationException();
264 return stack.contents[current];
268 public virtual bool MoveNext() {
269 if (modCount != stack.modCount)
270 throw new InvalidOperationException();
274 current = stack.current;
275 return current != -1;
282 return current != -1;
286 public virtual void Reset() {
287 if (modCount != stack.modCount) {
288 throw new InvalidOperationException();
295 public virtual IEnumerator GetEnumerator() {
296 return new Enumerator(this);
299 public virtual object Peek() {
301 throw new InvalidOperationException();
303 return contents[current];
307 public virtual object Pop() {
309 throw new InvalidOperationException();
313 object ret = contents[current];
314 contents [current] = null;
319 // if we're down to capacity/4, go back to a
320 // lower array size. this should keep us from
321 // sucking down huge amounts of memory when
322 // putting large numbers of items in the Stack.
323 // if we're lower than 16, don't bother, since
324 // it will be more trouble than it's worth.
325 if (count <= (capacity/4) && count > 16) {
333 public virtual void Push(Object o) {
336 if (capacity == count) {
337 Resize(capacity * 2);
343 contents[current] = o;
346 public virtual object[] ToArray() {
347 object[] ret = new object[count];
349 Array.Copy(contents, ret, count);
351 // ret needs to be in LIFO order