[Mono-list] Array.GetEnumerator()

Nick Drochak ndrochak@gol.com
Fri, 8 Mar 2002 23:52:34 +0900


This is a multi-part message in MIME format.

------=_NextPart_000_000C_01C1C6FC.548C1790
Content-Type: text/plain;
	charset="us-ascii"
Content-Transfer-Encoding: 7bit

Here's my patch to implement Array.GetEnumerator() for single rank
arrays.

I think that multi-dimensional arrays are different enough to warrant a
separate internal enumerator class.  Besides, it was pretty simple
without worrying about multiple dimensions and I'm sleepy :)

I'll write some tests for this tomorrow, but wanted to get some feedback
sooner rather than later.

Notice I had to put in a version count in all the mutators. I hope this
doesn't mess up anything anyone else is working on.

Regards,
Nick D.

------=_NextPart_000_000C_01C1C6FC.548C1790
Content-Type: application/octet-stream;
	name="Array.diff"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="Array.diff"

Index: Array.cs=0A=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=0A=
RCS file: /cvs/public/mcs/class/corlib/System/Array.cs,v=0A=
retrieving revision 1.25=0A=
diff -u -r1.25 Array.cs=0A=
--- Array.cs	2002/03/07 09:19:29	1.25=0A=
+++ Array.cs	2002/03/08 10:53:52=0A=
@@ -22,6 +22,8 @@=0A=
 			/* empty */=0A=
 		}=0A=
 		=0A=
+		int version =3D 0;=0A=
+=0A=
 		// Properties=0A=
 		public int Length =0A=
 		{=0A=
@@ -114,8 +116,11 @@=0A=
 		[MonoTODO]=0A=
 		public virtual IEnumerator GetEnumerator ()=0A=
 		{=0A=
-			// FIXME=0A=
-			return null;=0A=
+			if (Rank =3D=3D 1)=0A=
+				return new SimpleEnumerator(this);=0A=
+			else=0A=
+				//return new MultiRankEnumerator(this);=0A=
+				return null;=0A=
 		}=0A=
 =0A=
 		public int GetUpperBound (int dimension)=0A=
@@ -185,6 +190,8 @@=0A=
 			ind [0] =3D idx;=0A=
 =0A=
 			SetValue (value, ind);=0A=
+=0A=
+			version++;=0A=
 		}=0A=
 		=0A=
 		public void SetValue (object value, int idx1, int idx2)=0A=
@@ -195,6 +202,8 @@=0A=
 			ind [1] =3D idx2;=0A=
 =0A=
 			SetValue (value, ind);=0A=
+=0A=
+			version++;=0A=
 		}=0A=
 =0A=
 		public void SetValue (object value, int idx1, int idx2, int idx3)=0A=
@@ -206,6 +215,8 @@=0A=
 			ind [2] =3D idx3;=0A=
 =0A=
 			SetValue (value, ind);=0A=
+=0A=
+			version++;=0A=
 		}=0A=
 =0A=
 		public static Array CreateInstance(Type elementType, int length)=0A=
@@ -346,6 +357,8 @@=0A=
 			{=0A=
 				array.SetValue(null, index + i);=0A=
 			}=0A=
+=0A=
+			array.version++;=0A=
 		}=0A=
 =0A=
 		[MethodImplAttribute(MethodImplOptions.InternalCall)]=0A=
@@ -517,6 +530,8 @@=0A=
 				array.SetValue(array.GetValue (index + length - i - 1), index + i);=0A=
 				array.SetValue(tmp, index + length - i - 1);=0A=
 			}=0A=
+=0A=
+			array.version++;=0A=
 		}		=0A=
 		=0A=
 		public static void Sort (Array array)=0A=
@@ -572,6 +587,9 @@=0A=
 			int high0 =3D index + length - 1;=0A=
 =0A=
 			qsort (keys, items, index, index + length - 1, comparer);=0A=
+			keys.version++;=0A=
+			if (items !=3D null)=0A=
+				items.version++;=0A=
 		}=0A=
 =0A=
 		private static void qsort (Array keys, Array items, int low0, int =
high0, IComparer comparer)=0A=
@@ -658,6 +676,50 @@=0A=
 				throw new ArgumentOutOfRangeException ();=0A=
 =0A=
 			Copy (this, this.GetLowerBound(0), array, index, this.GetLength (0));=0A=
+		}=0A=
+=0A=
+		internal class SimpleEnumerator : IEnumerator {=0A=
+			Array enumeratee;=0A=
+=0A=
+			int currentIndex;=0A=
+			int firstIndex;=0A=
+			int lastIndex;=0A=
+=0A=
+			int version;=0A=
+=0A=
+			public SimpleEnumerator (Array arrayToEnumerate) {=0A=
+				enumeratee =3D arrayToEnumerate;=0A=
+				firstIndex =3D arrayToEnumerate.GetLowerBound(0);=0A=
+				currentIndex =3D firstIndex - 1;=0A=
+				lastIndex =3D firstIndex + arrayToEnumerate.Length - 1;=0A=
+				version =3D arrayToEnumerate.version;=0A=
+			}=0A=
+			=0A=
+			public object Current {=0A=
+				get {=0A=
+					if (currentIndex < firstIndex || =0A=
+						currentIndex > lastIndex){=0A=
+						throw new InvalidOperationException ();=0A=
+					}=0A=
+=0A=
+					return enumeratee.GetValue(++currentIndex);=0A=
+				}=0A=
+			}=0A=
+=0A=
+			public bool MoveNext() {=0A=
+				if (version !=3D enumeratee.version)=0A=
+					throw new InvalidOperationException();=0A=
+=0A=
+				if (currentIndex <=3D lastIndex)=0A=
+					currentIndex++;=0A=
+=0A=
+				return (currentIndex <=3D lastIndex);=0A=
+			}=0A=
+=0A=
+			public void Reset() {=0A=
+				currentIndex =3D firstIndex - 1;=0A=
+			}=0A=
+=0A=
 		}=0A=
 	}=0A=
 }=0A=

------=_NextPart_000_000C_01C1C6FC.548C1790--