private object[] contents;\r
private int current = -1;\r
private int count = 0;\r
- private int capacity = 16;\r
+ private int capacity;\r
private int modCount = 0;\r
\r
- private void Resize(int ncapacity) {\r
+ private void Resize(int ncapacity) {
+
+ ncapacity = Math.Max (ncapacity, 16);\r
object[] ncontents = new object[ncapacity];\r
\r
Array.Copy(contents, ncontents, count);\r
contents = ncontents;\r
}\r
\r
- public Stack() {\r
- contents = new object[capacity];\r
- }\r
+ public Stack () : this (16) {}\r
\r
- public Stack(ICollection collection) {\r
- capacity = collection.Count;\r
- contents = new object[capacity];\r
- current = capacity - 1;\r
- count = capacity;\r
+ public Stack(ICollection collection) : this (collection == null ? 16 : collection.Count) {
+ if (collection == null)
+ throw new ArgumentNullException("collection");
+ \r
+ current = collection.Count - 1;\r
+ count = collection.Count;\r
\r
collection.CopyTo(contents, 0);\r
}\r
\r
- public Stack(int c) {\r
- capacity = c;\r
+ public Stack (int c) {
+ if (c < 0)
+ throw new ArgumentOutOfRangeException ("c");
+ \r
+ capacity = Math.Max (c, 16);\r
contents = new object[capacity];\r
}\r
\r
}\r
}\r
\r
- private class Enumerator : IEnumerator {\r
+ private class Enumerator : IEnumerator, ICloneable {
+
+ const int EOF = -1;
+ const int BOF = -2;\r
\r
Stack stack;\r
private int modCount;\r
private int current;\r
\r
internal Enumerator(Stack s) {\r
- // this is odd. it seems that you need to \r
- // start one further ahead than current, since \r
- // MoveNext() gets called first when using an \r
- // Enumeration...\r
stack = s;\r
- modCount = s.modCount;\r
- current = s.current + 1;\r
+ modCount = s.modCount;
+ current = BOF;\r
+ }
+
+ public object Clone ()
+ {
+ return MemberwiseClone ();
}\r
\r
public virtual object Current {\r
get {\r
if (modCount != stack.modCount \r
- || current == -1 \r
+ || current == BOF
+ || current == EOF\r
|| current > stack.count)\r
throw new InvalidOperationException();\r
return stack.contents[current];\r
}\r
\r
public virtual bool MoveNext() {\r
- if (modCount != stack.modCount \r
- || current == -1) {\r
- throw new InvalidOperationException();\r
- }\r
-\r
- current--;\r
-\r
- if (current == -1) {\r
- return false;\r
- } else {\r
- return true;\r
+ if (modCount != stack.modCount)\r
+ throw new InvalidOperationException();
+
+ switch (current) {
+ case BOF:
+ current = stack.current;
+ return current != -1;
+
+ case EOF:
+ return false;
+
+ default:
+ current--;
+ return current != -1;
}\r
}\r
\r
throw new InvalidOperationException();\r
}\r
\r
- // start one ahead of stack.current, so the \r
- // first MoveNext() will put us at the top\r
- current = stack.current + 1;\r
+ current = BOF;\r
}\r
}\r
\r