[Mono-list] Array Class

Dwivedi , Ajay Kumar AjayKumar.Dwivedi@dresdner-bank.com
Sat, 9 Mar 2002 18:33:09 -0000


Hi all,
	Here are a few suggestions about the Array class. If you guys like
them, I can create a patch.

1. Array class is fixed size. Wouldn't it be better to store Length and Rank
as cached private fields instead of recalculating them everytime?

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. An
implementation is attached at the bottom. The code is based on Nick's
implementation. The code has been tested for single dimension arrays.

3. All versions of BinarySearch except the most generic one should be spared
of the array==null check.

4. Clear should be implemented as an InternalCall. The loop might be quite
costly, whereas the same can be done using something like bzero in C (BTW I
don't have even slightest idea about how we do internalcall implementations,
so I might be wrong here)

Happy Hacking,
Ajay

//Assumptions:	based on :	internal extern object GetValueImpl (int
pos);
//				The position starts at 0 and max position is
length-1

	internal class SimpleEnumerator : IEnumerator 
	{
		Array enumeratee;
		int currentpos;
		int length;

		public SimpleEnumerator (Array arrayToEnumerate) 
		{
			this.enumeratee = arrayToEnumerate;
			this.currentpos = -1;
			this.length = arrayToEnumerate.Length;
		}
						
		public object Current 
		{
			get 
			{	// Exception messages based on MS
implementation
				if (currentpos < 0 )
				{
					throw new InvalidOperationException
("Enumeration has not started");
				}
				if  (currentpos >= length)
				{
					throw new InvalidOperationException
("Enumeration has already ended");
				}
				// Current should increase the position. So
no ++ over here.
				return enumeratee.GetValueImpl(currentpos);
			}
		}

		public bool MoveNext() 
		{
			//The docs say Current should throw an exception if
last
			//call to MoveNext returned false. This means
currentpos
			//should be set to length when returning false.
			if (currentpos < length)
			{
				currentpos++;
			}
			if(currentpos < length)
				return true;
			else
				return false;
		}

		public void Reset() 
		{
			currentpos= -1;
		}
	}