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;
50 const int default_capacity = 16;
52 private void Resize(int ncapacity)
55 ncapacity = Math.Max (ncapacity, 16);
56 object[] ncontents = new object[ncapacity];
58 Array.Copy(contents, ncontents, count);
66 contents = new object[default_capacity];
67 capacity = default_capacity;
70 public Stack(ICollection col) : this (col == null ? 16 : col.Count) {
72 throw new ArgumentNullException("col");
74 // We have to do this because msft seems to call the
75 // enumerator rather than CopyTo. This affects classes
77 foreach (object o in col)
81 public Stack (int initialCapacity)
83 if (initialCapacity < 0)
84 throw new ArgumentOutOfRangeException ("initialCapacity");
86 capacity = initialCapacity;
87 contents = new object[capacity];
91 private class SyncStack : Stack {
95 internal SyncStack(Stack s) {
99 public override int Count {
108 public override bool IsReadOnly {
111 return stack.IsReadOnly;
117 public override bool IsSynchronized {
121 public override object SyncRoot {
122 get { return stack.SyncRoot; }
125 public override void Clear() {
126 lock(stack) { stack.Clear(); }
129 public override object Clone() {
131 return Stack.Synchronized((Stack)stack.Clone());
135 public override bool Contains(object obj) {
136 lock (stack) { return stack.Contains(obj); }
139 public override void CopyTo(Array array, int index) {
140 lock (stack) { stack.CopyTo(array, index); }
143 public override IEnumerator GetEnumerator() {
145 return new Enumerator(stack);
149 public override object Peek() {
150 lock (stack) { return stack.Peek(); }
153 public override object Pop() {
154 lock (stack) { return stack.Pop(); }
157 public override void Push(object obj) {
158 lock (stack) { stack.Push(obj); }
161 public override object[] ToArray() {
162 lock (stack) { return stack.ToArray(); }
166 public static Stack Synchronized(Stack s) {
168 throw new ArgumentNullException();
171 return new SyncStack(s);
174 public virtual int Count {
175 get { return count; }
179 public virtual bool IsReadOnly {
180 get { return false; }
184 public virtual bool IsSynchronized {
185 get { return false; }
188 public virtual object SyncRoot {
192 public virtual void Clear() {
195 for (int i = 0; i < count; i++) {
203 public virtual object Clone() {
204 Stack stack = new Stack (contents);
205 stack.current = current;
210 public virtual bool Contains(object obj) {
215 for (int i = 0; i < count; i++) {
216 if (contents[i] == null)
220 for (int i = 0; i < count; i++) {
221 if (obj.Equals (contents[i]))
229 public virtual void CopyTo (Array array, int index) {
231 throw new ArgumentNullException("array");
235 throw new ArgumentOutOfRangeException("index");
238 if (array.Rank > 1 ||
239 array.Length > 0 && index >= array.Length ||
240 count > array.Length - index) {
241 throw new ArgumentException();
244 for (int i = current; i != -1; i--) {
245 array.SetValue(contents[i],
246 count - (i + 1) + index);
250 private class Enumerator : IEnumerator, ICloneable {
256 private int modCount;
259 internal Enumerator(Stack s) {
261 modCount = s.modCount;
265 public object Clone ()
267 return MemberwiseClone ();
270 public virtual object Current {
272 if (modCount != stack.modCount
275 || current > stack.count)
276 throw new InvalidOperationException();
277 return stack.contents[current];
281 public virtual bool MoveNext() {
282 if (modCount != stack.modCount)
283 throw new InvalidOperationException();
287 current = stack.current;
288 return current != -1;
295 return current != -1;
299 public virtual void Reset() {
300 if (modCount != stack.modCount) {
301 throw new InvalidOperationException();
308 public virtual IEnumerator GetEnumerator() {
309 return new Enumerator(this);
312 public virtual object Peek() {
314 throw new InvalidOperationException();
316 return contents[current];
320 public virtual object Pop() {
322 throw new InvalidOperationException();
326 object ret = contents[current];
327 contents [current] = null;
332 // if we're down to capacity/4, go back to a
333 // lower array size. this should keep us from
334 // sucking down huge amounts of memory when
335 // putting large numbers of items in the Stack.
336 // if we're lower than 16, don't bother, since
337 // it will be more trouble than it's worth.
338 if (count <= (capacity/4) && count > 16) {
346 public virtual void Push(Object o) {
349 if (capacity == count) {
350 Resize(capacity * 2);
356 contents[current] = o;
359 public virtual object[] ToArray() {
360 object[] ret = new object[count];
362 Array.Copy(contents, ret, count);
364 // ret needs to be in LIFO order