2003-06-07 Ben Maurer <bmaurer@users.sourceforge.net>
authorBen Maurer <benm@mono-cvs.ximian.com>
Sat, 7 Jun 2003 18:55:38 +0000 (18:55 -0000)
committerBen Maurer <benm@mono-cvs.ximian.com>
Sat, 7 Jun 2003 18:55:38 +0000 (18:55 -0000)
* Stack.cs: Contains (null) works correctly. We never have the
array sized less than 16, so that the doubling logic works no
matter what. The enumerator is IClonable, like in ms. The
Enumerator correctly throws an exception if Current is called
before the enumerator is started. We now pass all the Rotor tests
for this file!

svn path=/trunk/mcs/; revision=15179

mcs/class/corlib/System.Collections/ChangeLog
mcs/class/corlib/System.Collections/Stack.cs

index 432416d974bdd6e02fa2d879718b98a4e8d7c1a7..17155dfd81391426aa9a4cfde5f1806c32fcbf0e 100644 (file)
@@ -1,5 +1,10 @@
 2003-06-07  Ben Maurer <bmaurer@users.sourceforge.net>
-       * Stack.cs: Contains (null) works correctly.
+       * Stack.cs: Contains (null) works correctly. We never have the
+       array sized less than 16, so that the doubling logic works no
+       matter what. The enumerator is IClonable, like in ms. The
+       Enumerator correctly throws an exception if Current is called
+       before the enumerator is started. We now pass all the Rotor tests
+       for this file!
 
 2003-06-07  Gonzalo Paniagua Javier <gonzalo@ximian.com>
 
index a462e6fb03ddfadaab2376f59ceb087117a8e40c..0a65a818cf76e1ee349d22e183382cca94c0ffee 100644 (file)
@@ -16,10 +16,12 @@ namespace System.Collections {
                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
@@ -28,21 +30,23 @@ namespace System.Collections {
                        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
@@ -205,26 +209,31 @@ namespace System.Collections {
                        }\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
@@ -232,17 +241,20 @@ namespace System.Collections {
                        }\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
@@ -251,9 +263,7 @@ namespace System.Collections {
                                        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