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 using System.Runtime.InteropServices;
35 namespace System.Collections {
41 public class Stack : ICollection, IEnumerable, ICloneable {
44 private object[] contents;
45 private int current = -1;
46 private int count = 0;
48 private int modCount = 0;
50 private void Resize(int ncapacity) {
52 ncapacity = Math.Max (ncapacity, 16);
53 object[] ncontents = new object[ncapacity];
55 Array.Copy(contents, ncontents, count);
61 public Stack () : this (16) {}
63 public Stack(ICollection col) : this (col == null ? 16 : col.Count) {
65 throw new ArgumentNullException("col");
67 // We have to do this because msft seems to call the
68 // enumerator rather than CopyTo. This affects classes
70 foreach (object o in col)
74 public Stack (int initialCapacity) {
75 if (initialCapacity < 0)
76 throw new ArgumentOutOfRangeException ("initialCapacity");
78 capacity = Math.Max (initialCapacity, 16);
79 contents = new object[capacity];
83 private class SyncStack : Stack {
87 internal SyncStack(Stack s) {
91 public override int Count {
100 public override bool IsReadOnly {
103 return stack.IsReadOnly;
109 public override bool IsSynchronized {
113 public override object SyncRoot {
114 get { return stack.SyncRoot; }
117 public override void Clear() {
118 lock(stack) { stack.Clear(); }
121 public override object Clone() {
123 return Stack.Synchronized((Stack)stack.Clone());
127 public override bool Contains(object obj) {
128 lock (stack) { return stack.Contains(obj); }
131 public override void CopyTo(Array array, int index) {
132 lock (stack) { stack.CopyTo(array, index); }
135 public override IEnumerator GetEnumerator() {
137 return new Enumerator(stack);
141 public override object Peek() {
142 lock (stack) { return stack.Peek(); }
145 public override object Pop() {
146 lock (stack) { return stack.Pop(); }
149 public override void Push(object obj) {
150 lock (stack) { stack.Push(obj); }
153 public override object[] ToArray() {
154 lock (stack) { return stack.ToArray(); }
158 public static Stack Synchronized(Stack s) {
160 throw new ArgumentNullException();
163 return new SyncStack(s);
166 public virtual int Count {
167 get { return count; }
171 public virtual bool IsReadOnly {
172 get { return false; }
176 public virtual bool IsSynchronized {
177 get { return false; }
180 public virtual object SyncRoot {
184 public virtual void Clear() {
187 for (int i = 0; i < count; i++) {
195 public virtual object Clone() {
196 Stack stack = new Stack (contents);
197 stack.current = current;
202 public virtual bool Contains(object obj) {
207 for (int i = 0; i < count; i++) {
208 if (contents[i] == null)
212 for (int i = 0; i < count; i++) {
213 if (obj.Equals (contents[i]))
221 public virtual void CopyTo (Array array, int index) {
223 throw new ArgumentNullException("array");
227 throw new ArgumentOutOfRangeException("index");
230 if (array.Rank > 1 ||
231 array.Length > 0 && index >= array.Length ||
232 count > array.Length - index) {
233 throw new ArgumentException();
236 for (int i = current; i != -1; i--) {
237 array.SetValue(contents[i],
238 count - (i + 1) + index);
242 private class Enumerator : IEnumerator, ICloneable {
248 private int modCount;
251 internal Enumerator(Stack s) {
253 modCount = s.modCount;
257 public object Clone ()
259 return MemberwiseClone ();
262 public virtual object Current {
264 if (modCount != stack.modCount
267 || current > stack.count)
268 throw new InvalidOperationException();
269 return stack.contents[current];
273 public virtual bool MoveNext() {
274 if (modCount != stack.modCount)
275 throw new InvalidOperationException();
279 current = stack.current;
280 return current != -1;
287 return current != -1;
291 public virtual void Reset() {
292 if (modCount != stack.modCount) {
293 throw new InvalidOperationException();
300 public virtual IEnumerator GetEnumerator() {
301 return new Enumerator(this);
304 public virtual object Peek() {
306 throw new InvalidOperationException();
308 return contents[current];
312 public virtual object Pop() {
314 throw new InvalidOperationException();
318 object ret = contents[current];
319 contents [current] = null;
324 // if we're down to capacity/4, go back to a
325 // lower array size. this should keep us from
326 // sucking down huge amounts of memory when
327 // putting large numbers of items in the Stack.
328 // if we're lower than 16, don't bother, since
329 // it will be more trouble than it's worth.
330 if (count <= (capacity/4) && count > 16) {
338 public virtual void Push(Object o) {
341 if (capacity == count) {
342 Resize(capacity * 2);
348 contents[current] = o;
351 public virtual object[] ToArray() {
352 object[] ret = new object[count];
354 Array.Copy(contents, ret, count);
356 // ret needs to be in LIFO order