[Mono-list] System.Collections.ArrayList Patch
Ben Maurer
bmaurer@users.sourceforge.net
02 May 2003 17:51:14 -0400
--=-Z9ARJ62W1i9VaNAZnR/I
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
Hi guys,
As per Miguel's suggestion, I am cleaning up the System.Collections
namespace.
I got the wrappers in the ArrayList working.
Could someone do a sanity check on this. I will commit after that is
done.
--Ben Maurer
--=-Z9ARJ62W1i9VaNAZnR/I
Content-Disposition: attachment; filename=ArrayList.cs.patch
Content-Type: text/x-patch; name=ArrayList.cs.patch; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Index: ArrayList.cs
=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
RCS file: /cvs/public/mcs/class/corlib/System.Collections/ArrayList.cs,v
retrieving revision 1.43
diff -u -r1.43 ArrayList.cs
--- ArrayList.cs 22 Apr 2003 00:19:52 -0000 1.43
+++ ArrayList.cs 2 May 2003 21:54:38 -0000
@@ -4,10 +4,12 @@
// Author:
// Vladimir Vukicevic (vladimir@pobox.com)
// Duncan Mak (duncan@ximian.com)
-// Patrik Torstensson (totte@crepundia.net)
+// Patrik Torstensson (totte@crepundia.net)
+// Ben Maurer (bmaurer@users.sf.net)
//
// (C) 2001 Vladimir Vukicevic
// (C) 2002 Ximian, Inc.
+// (C) 2003 Ben Maurer
//
=20
using System;
@@ -153,38 +155,66 @@
this.list =3D list;
count =3D ((ICollection) list).Count;
}
- =09
+
// ArrayList
- [MonoTODO]
public override int Capacity {
get { return list.Count; }
- set { throw new NotSupportedException (); }
+ set {
+ // MS seems to do this
+ if (value < list.Count) {
+ throw new ArgumentOutOfRangeException
+ ("ArrayList Capacity being set to less than Count");
+ }
+ }
}
=20
- [MonoTODO]
public override void AddRange (ICollection collection) {
if (collection =3D=3D null)
throw new ArgumentNullException ("colllection");
+ =09
if (IsFixedSize || IsReadOnly)
throw new NotSupportedException ();
+
+ InsertRange (Count, collection);
+ =09
}
=20
- [MonoTODO]
public override int BinarySearch (object value) {
- throw new NotImplementedException ();
+ return BinarySearch (value, null);
}
=20
- [MonoTODO]
public override int BinarySearch (object value, IComparer comparer) {
- throw new NotImplementedException ();
+ return BinarySearch (0, Count, value, comparer);
}
=20
- [MonoTODO]
public override int BinarySearch (int index, int count, object value,
IComparer comparer) {
- throw new NotImplementedException ();
- }
=20
+ if ((index < 0) || (count < 0))
+ throw new ArgumentOutOfRangeException ();
+ if ((index > Count) || (index + count) > Count)
+ throw new ArgumentException ();
+ =09
+ if (comparer =3D=3D null)
+ comparer =3D Comparer.Default;
+ =20
+ int low =3D index;
+ int hi =3D index + count - 1;
+ int mid;
+ while (low <=3D hi) {
+ mid =3D (low + hi) / 2;
+ int r =3D comparer.Compare (value, list [mid]);
+ if (r =3D=3D 0)
+ return mid;
+ if (r < 0)
+ hi =3D mid-1;
+ else=20
+ low =3D mid+1;
+ }
+
+ return ~low;
+ }
+ =09
public override void CopyTo (Array array) {
if (null =3D=3D array)
throw new ArgumentNullException("array");
@@ -194,7 +224,6 @@
CopyTo (array, 0);
}
=20
- [MonoTODO]
public override void CopyTo (int index, Array array,
int arrayIndex, int count) {
if (array =3D=3D null)
@@ -203,7 +232,9 @@
throw new ArgumentOutOfRangeException ();
if (array.Rank > 1 || index >=3D Count || Count > (array.Length - arra=
yIndex))
throw new ArgumentException ();
- // FIXME: handle casting error here
+ =09
+ for (int i =3D index; i < index + count; i++)
+ array.SetValue(list [i], arrayIndex++);
}
=20
public override ArrayList GetRange (int index, int count) {
@@ -220,7 +251,6 @@
return result;
}
=20
- [MonoTODO]
public override void InsertRange (int index, ICollection col) {
if (col =3D=3D null)
throw new ArgumentNullException ();
@@ -231,19 +261,8 @@
=20
if (index =3D=3D Count) {
foreach (object element in col)
- list.Add (element);
-
- } //else if ((index + count) < Count) {
- // for (int i =3D index; i < (index + count); i++)
- // list [i] =3D col [i];
-
- // } else {
- // int added =3D Count - (index + count);
- // for (int i =3D index; i < Count; i++)
- // list [i] =3D col [i];
- // for (int i =3D 0; i < added; i++)
- // list.Add (col [Count +i]);
- // }
+ list.Insert (index++, element);
+ }
}
=20
public override int LastIndexOf (object value) {
@@ -316,16 +335,28 @@
list [i] =3D o;
}
=20
- [MonoTODO]
public override void Sort () {
+ Sort (null);
}
=20
- [MonoTODO]
public override void Sort (IComparer comparer) {
+ Sort (0, Count, comparer);
}
=20
- [MonoTODO]
public override void Sort (int index, int count, IComparer comparer) {
+ if ((index < 0) || (count < 0))
+ throw new ArgumentOutOfRangeException ();
+ if ((index > Count) || (index + count) > Count)
+ throw new ArgumentException ();
+ if (IsReadOnly)
+ throw new NotSupportedException ();
+ =09
+ // TODO: do some real sorting
+ object [] tmpArr =3D new Object [count];
+ CopyTo (index, tmpArr, 0, count);
+ Array.Sort(tmpArr, 0, count, comparer);
+ for (int i =3D 0; i < count; i++)
+ list [i + index] =3D tmpArr [i];
}
=20
public override object [] ToArray () {
@@ -342,8 +373,8 @@
return result;
}
=20
- [MonoTODO]
public override void TrimToSize () {
+ // Microsoft does not implement this method
}
=20
// IList
@@ -415,8 +446,7 @@
return ((IEnumerable) list).GetEnumerator ();
}
}
-
- [MonoTODO]
+ =09
public static ArrayList Adapter (IList list) {
return new ListWrapper (list);
}
--=-Z9ARJ62W1i9VaNAZnR/I--