[Mono-list] Array Class

Nick Drochak ndrochak@gol.com
Mon, 11 Mar 2002 14:59:46 +0900


This is a multi-part message in MIME format.

------=_NextPart_000_001B_01C1C90D.62975FD0
Content-Type: text/plain;
	charset="US-ASCII"
Content-Transfer-Encoding: 7bit

| > 2. Implementing the iterator for all dimensions should be 
| very easy if 
| > we have "internal extern object GetValueImpl (int pos);" 
| implemented. 
| > I suppose this would return an object based on position and not 
| > indices.
| 
| This function already exists as interncall. It takes a 
| position and returns an object.
| 

I changed the Enumerator to use Ajay's version.  This should work fine
with both single and multi-dimensional arrays since it's based on
positions, not indicies.  Thanks Ajay.

Since MS's implementation does not throw an error after a mutation, I
removed all the versioning stuff; much to Paolo's relief, I'm sure :)

I also included the IList interface methods since those are needed in
the Nunit tests. Using Ajay's hint, I utilized GetValueImpl (int pos)
in order to simplify the code for Contains () and IndexOf ().

I've attached the diff for review again.

Thanks,
Nick D.






------=_NextPart_000_001B_01C1C90D.62975FD0
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.26=0A=
diff -u -r1.26 Array.cs=0A=
--- Array.cs	2002/03/08 15:40:29	1.26=0A=
+++ Array.cs	2002/03/11 02:07:29=0A=
@@ -13,8 +13,8 @@=0A=
 namespace System=0A=
 {=0A=
 =0A=
-	[MonoTODO("This should implement IList and IEnumerable too")]=0A=
-	public abstract class Array : ICloneable, ICollection=0A=
+	[MonoTODO("This should implement IEnumerable too")]=0A=
+	public abstract class Array : ICloneable, ICollection, IList=0A=
 	{=0A=
 		// Constructor		=0A=
 		protected Array ()=0A=
@@ -45,6 +45,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=
+		public int Add (object value) {=0A=
+			throw new NotSupportedException ();=0A=
+		}=0A=
+=0A=
+		public void Clear () {=0A=
+			Array.Clear (this, this.GetLowerBound(0), this.Length);=0A=
+		}=0A=
+=0A=
+		public bool 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=
+		public int 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=
+		public void Insert (int index, object value) {=0A=
+			throw new NotSupportedException ();=0A=
+		}=0A=
+=0A=
+		public void Remove (object value) {=0A=
+			throw new NotSupportedException ();=0A=
+		}=0A=
+=0A=
+		public void RemoveAt (int index) {=0A=
+			throw new NotSupportedException ();=0A=
+		}=0A=
+=0A=
 		// InternalCall Methods=0A=
 		=0A=
 		[MethodImplAttribute(MethodImplOptions.InternalCall)]=0A=
@@ -114,8 +174,7 @@=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=
@@ -655,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_001B_01C1C90D.62975FD0--