[Mono-list] IList and IEnumerable for Array

Nick Drochak ndrochak@gol.com
Tue, 12 Mar 2002 23:17:55 +0900


This is a multi-part message in MIME format.

------=_NextPart_000_0027_01C1CA1C.2411B5C0
Content-Type: text/plain;
	charset="us-ascii"
Content-Transfer-Encoding: 7bit

All,

Here's my patch for Array.  It adds IEnumerable (thanks Ajay) and IList
to the class.

Initial testing on Linux with RunTests.corlib.exe have it working under
mono, but crashing mint.  Mint runs for a second or so and I see the
memory bar climbing up in the CPU/Mem usage applet, then it crashes with
a segfault.  It doesn't report anything other than the fault.

I could have sworn I tried this about 12 hours ago and it worked in both
(since then I've updated from cvs at about 10pm Tokyo time).


Nick D.

------=_NextPart_000_0027_01C1CA1C.2411B5C0
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.27=0A=
diff -u -r1.27 Array.cs=0A=
--- Array.cs	2002/03/11 14:39:34	1.27=0A=
+++ Array.cs	2002/03/12 10:22:20=0A=
@@ -15,8 +15,7 @@=0A=
 namespace System=0A=
 {=0A=
 =0A=
-	[MonoTODO("This should implement IList and IEnumerable too")]=0A=
-	public abstract class Array : ICloneable, ICollection=0A=
+	public abstract class Array : ICloneable, ICollection, IList, =
IEnumerable=0A=
 	{=0A=
 		// Constructor		=0A=
 		protected Array ()=0A=
@@ -47,6 +46,66 @@=0A=
 			}=0A=
 		}=0A=
 =0A=
+		// IList interface=0A=
+		public object this [int index] {=0A=
+			get {=0A=
+				return GetValueImpl (index);=0A=
+			} =0A=
+			set {=0A=
+				SetValueImpl (value, index);=0A=
+			}=0A=
+		}=0A=
+=0A=
+		int IList.Add (object value) {=0A=
+			throw new NotSupportedException ();=0A=
+		}=0A=
+=0A=
+		void IList.Clear () {=0A=
+			Array.Clear (this, this.GetLowerBound(0), this.Length);=0A=
+		}=0A=
+=0A=
+		bool IList.Contains (object value) {=0A=
+			int length =3D this.Length;=0A=
+			for (int i =3D 0; i < length; i++) {=0A=
+				if (value.Equals (this.GetValueImpl (i)))=0A=
+					return true;=0A=
+			}=0A=
+			return false;=0A=
+		}=0A=
+=0A=
+		int IList.IndexOf (object value) {=0A=
+			if (this.Rank > 1)=0A=
+				throw new RankException ();=0A=
+=0A=
+			int length =3D this.Length;=0A=
+			for (int i =3D 0; i < length; i++) {=0A=
+				if (value.Equals (this.GetValueImpl (i)))=0A=
+					// array index may not be zero-based.=0A=
+					// use lower bound=0A=
+					return i + this.GetLowerBound (0);=0A=
+			}=0A=
+=0A=
+			int retVal;=0A=
+			unchecked {=0A=
+				// lower bound may be MinValue=0A=
+				retVal =3D this.GetLowerBound (0) - 1;=0A=
+			}=0A=
+=0A=
+			return retVal;=0A=
+		}=0A=
+=0A=
+		void IList.Insert (int index, object value) {=0A=
+			throw new NotSupportedException ();=0A=
+		}=0A=
+=0A=
+		void IList.Remove (object value) {=0A=
+			throw new NotSupportedException ();=0A=
+		}=0A=
+=0A=
+		void IList.RemoveAt (int index) {=0A=
+			throw new NotSupportedException ();=0A=
+		}=0A=
+=0A=
 		// InternalCall Methods=0A=
 		=0A=
 		[MethodImplAttribute(MethodImplOptions.InternalCall)]=0A=
@@ -113,11 +172,9 @@=0A=
 			}=0A=
 		}=0A=
 =0A=
-		[MonoTODO]=0A=
 		public virtual IEnumerator GetEnumerator ()=0A=
 		{=0A=
-			// FIXME=0A=
-			return null;=0A=
+			return new SimpleEnumerator(this);=0A=
 		}=0A=
 =0A=
 		public int GetUpperBound (int dimension)=0A=
@@ -657,6 +714,51 @@=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=
+			int currentpos;=0A=
+			int length;=0A=
+=0A=
+			public SimpleEnumerator (Array arrayToEnumerate) {=0A=
+				this.enumeratee =3D arrayToEnumerate;=0A=
+				this.currentpos =3D -1;=0A=
+				this.length =3D arrayToEnumerate.Length;=0A=
+			}=0A=
+=0A=
+			public object Current {=0A=
+				get {=0A=
+			 		// Exception messages based on MS implementation=0A=
+					if (currentpos < 0 ) {=0A=
+						throw new InvalidOperationException=0A=
+							("Enumeration has not started");=0A=
+					}=0A=
+					if  (currentpos >=3D length) {=0A=
+						throw new InvalidOperationException=0A=
+							("Enumeration has already ended");=0A=
+					}=0A=
+					// Current should not increase the position. So no ++ over here.=0A=
+					return enumeratee.GetValueImpl(currentpos);=0A=
+				}=0A=
+			}=0A=
+=0A=
+			public bool MoveNext() {=0A=
+				//The docs say Current should throw an exception if last=0A=
+				//call to MoveNext returned false. This means currentpos=0A=
+				//should be set to length when returning false.=0A=
+					if (currentpos < length) {=0A=
+						currentpos++;=0A=
+					}=0A=
+				if(currentpos < length)=0A=
+					return true;=0A=
+				else=0A=
+					return false;=0A=
+			}=0A=
+=0A=
+			public void Reset() {=0A=
+				currentpos=3D -1;=0A=
+			}=0A=
 		}=0A=
 	}=0A=
 }=0A=

------=_NextPart_000_0027_01C1CA1C.2411B5C0--