2002-09-24 Nick Drochak <ndrochak@gol.com>
authorNick Drochak <nickd@mono-cvs.ximian.com>
Tue, 24 Sep 2002 14:11:07 +0000 (14:11 -0000)
committerNick Drochak <nickd@mono-cvs.ximian.com>
Tue, 24 Sep 2002 14:11:07 +0000 (14:11 -0000)
* ArrayList.cs: Make enumerator throw exception if the ArrayList is
mutated (Insert, Delete, etc.). Also, fix bug in InsertRange() when
this was passed as parameter.

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

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

index 2e06eddeff8eabc3866dff0c287e29a91a7ab488..df419e6cb0e502980e7ea0bb2a5a1953de4319ff 100644 (file)
@@ -684,7 +684,7 @@ namespace System.Collections {
                        if (array.Rank > 1)\r
                                throw new ArgumentException("array cannot be multidimensional");\r
 \r
-                       System.Array.Copy (dataArray, 0, array, 0, this.count);\r
+                       Array.Copy (dataArray, 0, array, 0, this.count);\r
                }\r
 \r
                public virtual void CopyTo (Array array, int arrayIndex) {\r
@@ -697,7 +697,7 @@ namespace System.Collections {
                        if (this.count > array.Length - arrayIndex)\r
                                throw new ArgumentException("this ArrayList has more items than the space available in array from arrayIndex to the end of array");\r
                        \r
-                       System.Array.Copy (dataArray, 0, array, arrayIndex, this.count);\r
+                       Array.Copy (dataArray, 0, array, arrayIndex, this.count);\r
                }\r
 \r
                public virtual void CopyTo (int index, Array array,\r
@@ -719,7 +719,7 @@ namespace System.Collections {
                        if (this.count > array.Length - arrayIndex)\r
                                throw new ArgumentException("this ArrayList has more items than the space available in array from arrayIndex to the end of array");\r
 \r
-                       System.Array.Copy (dataArray, index, array, arrayIndex, count);\r
+                       Array.Copy (dataArray, index, array, arrayIndex, count);\r
                }\r
 \r
                [Serializable]\r
@@ -728,17 +728,21 @@ namespace System.Collections {
                        private int idx;\r
                        private int start;\r
                        private int num;\r
+                       private ArrayList enumeratee;\r
+                       private long version;\r
 \r
-                       internal ArrayListEnumerator(int index, int count, object[] items) {\r
+                       internal ArrayListEnumerator(int index, int count, object[] items, ArrayList al, long ver) {\r
                                data = items;\r
                                start = index;\r
                                num = count;\r
                                idx = start - 1;\r
+                               enumeratee = al;\r
+                               version = ver;\r
                        }\r
 \r
                        public object Clone ()\r
                        {\r
-                               return new ArrayListEnumerator (start, num, data);\r
+                               return new ArrayListEnumerator (start, num, data, enumeratee, version);\r
                        }\r
 \r
                        public virtual object Current {\r
@@ -747,6 +751,8 @@ namespace System.Collections {
                                }\r
                        }\r
                        public virtual bool MoveNext() {\r
+                               if (enumeratee.version != version)\r
+                                       throw new InvalidOperationException();\r
                                if (++idx < start + num)\r
                                        return true;\r
                                return false;\r
@@ -757,7 +763,7 @@ namespace System.Collections {
                }\r
 \r
                public virtual IEnumerator GetEnumerator () {\r
-                       return new ArrayListEnumerator(0, this.Count, dataArray);\r
+                       return new ArrayListEnumerator(0, this.Count, dataArray, this, this.version);\r
                }\r
 \r
                private void ValidateRange(int index, int count) {\r
@@ -777,7 +783,7 @@ namespace System.Collections {
 \r
                public virtual IEnumerator GetEnumerator (int index, int count) {\r
                        ValidateRange(index, count);\r
-                       return new ArrayListEnumerator(index, count, dataArray);\r
+                       return new ArrayListEnumerator(index, count, dataArray, this, this.version);\r
                }\r
 \r
                public virtual ArrayList GetRange (int index, int count) {\r
@@ -845,12 +851,16 @@ namespace System.Collections {
                        if (IsReadOnly || IsFixedSize)\r
                                throw new NotSupportedException ();\r
 \r
+                       // Get a copy of the collection before the shift in case the collection\r
+                       // is this.  Otherwise the enumerator will be confused.\r
+                       Array source = Array.CreateInstance(typeof(object), c.Count);\r
+                       c.CopyTo(source, 0);\r
+\r
                        shiftElements (index, c.Count);\r
                        count += c.Count;\r
 \r
-                       IEnumerator en = c.GetEnumerator();\r
-                       while (en.MoveNext())\r
-                               dataArray[index++] = en.Current;\r
+                       foreach (object o in source)\r
+                               dataArray[index++] = o;\r
 \r
                        version++;\r
                }\r
index 2912a0c2431ea154ea03cc2dbf448c8d9723577c..717d37a27fa492680269c6c6fd147c8e9208ce9c 100644 (file)
@@ -1,3 +1,9 @@
+2002-09-24  Nick Drochak  <ndrochak@gol.com>
+
+       * ArrayList.cs: Make enumerator throw exception if the ArrayList is
+       mutated (Insert, Delete, etc.). Also, fix bug in InsertRange() when
+       this was passed as parameter.
+
 2002-08-29  Gonzalo Paniagua Javier <gonzalo@ximian.com>
 
        * ArrayList.cs: fixed bug #29658.