2002-05-23 Duncan Mak <duncan@ximian.com>
authorDuncan Mak <duncan@mono-cvs.ximian.com>
Thu, 23 May 2002 20:22:24 +0000 (20:22 -0000)
committerDuncan Mak <duncan@mono-cvs.ximian.com>
Thu, 23 May 2002 20:22:24 +0000 (20:22 -0000)
* ArrayList.cs (Wrapper): Preliminary implementation of
ArrayList.Wrapper (IList).

svn path=/trunk/mcs/; revision=4890

mcs/class/corlib/System.Collections/ArrayList.cs
mcs/class/corlib/System.Collections/ChangeLog

index 9d7181b8c271b3cf370b7a7e131a2b35087675cb..52311afaf1e0d09174c612ee0dcf87ec026d72e5 100644 (file)
@@ -3,8 +3,10 @@
 //\r
 // Author:\r
 //    Vladimir Vukicevic (vladimir@pobox.com)\r
+//    Duncan Mak (duncan@ximian.com)\r
 //\r
 // (C) 2001 Vladimir Vukicevic\r
+// (C) 2002 Ximian, Inc.\r
 //\r
 \r
 using System;\r
@@ -139,10 +141,310 @@ namespace System.Collections {
                        return al;\r
                }\r
 \r
+               [Serializable]\r
+               private class ListWrapper : ArrayList\r
+               {\r
+                       IList list;\r
+\r
+                       public ListWrapper (IList list)\r
+                       {\r
+                               this.list = list;\r
+                               count = ((ICollection) list).Count;\r
+                       }\r
+                       \r
+                       // ArrayList\r
+                       [MonoTODO]\r
+                       public override int Capacity {\r
+                               get { return list.Count; }\r
+                               set { throw new NotSupportedException (); }\r
+                       }\r
+\r
+                       [MonoTODO]\r
+                       public override void AddRange (ICollection collection)\r
+                       {\r
+                               if (collection == null)\r
+                                       throw new ArgumentNullException ("colllection");\r
+                               if (IsFixedSize || IsReadOnly)\r
+                                       throw new NotSupportedException ();\r
+                       }\r
+\r
+                       [MonoTODO]\r
+                       public override int BinarySearch (object value)\r
+                       {\r
+                               throw new NotImplementedException ();\r
+                       }\r
+\r
+                       [MonoTODO]\r
+                       public override int BinarySearch (object value, IComparer comparer)\r
+                       {\r
+                               throw new NotImplementedException ();\r
+                       }\r
+\r
+                       [MonoTODO]\r
+                       public override int BinarySearch (int index, int count, object value,\r
+                                                         IComparer comparer)\r
+                       {\r
+                               throw new NotImplementedException ();\r
+                       }\r
+\r
+                       public override void CopyTo (Array array)\r
+                       {\r
+                               if (null == array)\r
+                                       throw new ArgumentNullException("array");\r
+                               if (array.Rank > 1)\r
+                                       throw new ArgumentException("array cannot be multidimensional");\r
+\r
+                               CopyTo (array, 0);\r
+                       }\r
+\r
+                       [MonoTODO]\r
+                       public override void CopyTo (int index, Array array,\r
+                                                    int arrayIndex, int count)\r
+                       {\r
+                               if (array == null)\r
+                                       throw new ArgumentNullException ();\r
+                               if (index < 0 || arrayIndex < 0 || count < 0)\r
+                                       throw new ArgumentOutOfRangeException ();\r
+                               if (array.Rank > 1 || index >= Count || Count > (array.Length - arrayIndex))\r
+                                       throw new ArgumentException ();\r
+                               // FIXME: handle casting error here\r
+                       }\r
+\r
+                       public override ArrayList GetRange (int index, int count)\r
+                       {\r
+                               if (index < 0 || count < 0)\r
+                                       throw new ArgumentOutOfRangeException ();\r
+                               if (Count < (index + count))\r
+                                       throw new ArgumentException ();\r
+                               \r
+                               ArrayList result = new ArrayList (count);\r
+\r
+                               for (int i = 0; i < count; i++)\r
+                                       result.Add (list [i]);\r
+\r
+                               return result;\r
+                       }\r
+\r
+                       [MonoTODO]\r
+                       public override void InsertRange (int index, ICollection col)\r
+                       {\r
+                               if (col == null)\r
+                                       throw new ArgumentNullException ();\r
+                               if (index < 0 || index > Count)\r
+                                       throw new ArgumentOutOfRangeException ();\r
+                               if (IsReadOnly || IsFixedSize)\r
+                                       throw new NotSupportedException ();\r
+\r
+                               if (index == Count) {\r
+                                       foreach (object element in col)\r
+                                               list.Add (element);\r
+\r
+                               } //else if ((index + count) < Count) {\r
+//                                     for (int i = index; i < (index + count); i++)\r
+//                                             list [i] = col [i];\r
+\r
+//                             } else {\r
+//                                     int added = Count - (index + count);\r
+//                                     for (int i = index; i < Count; i++)\r
+//                                             list [i] = col [i];\r
+//                                     for (int i = 0; i < added; i++)\r
+//                                             list.Add (col [Count +i]);\r
+//                             }\r
+                       }\r
+\r
+                       public override int LastIndexOf (object value)\r
+                       {\r
+                               return LastIndexOf (value, Count, 0);\r
+                       }\r
+\r
+                       public override int LastIndexOf (object value, int startIndex)\r
+                       {\r
+                               return LastIndexOf (value, startIndex, 0);\r
+                       }\r
+\r
+                       public override int LastIndexOf (object value, int startIndex, int count)\r
+                       {\r
+                               if (startIndex > Count || count < 0 || (startIndex + count > Count))\r
+                                       throw new ArgumentOutOfRangeException ();\r
+                               \r
+                               int length = startIndex - count + 1;\r
+\r
+                               for (int i = startIndex; i >= length; i--)\r
+                                       if (list [i] == value)\r
+                                               return i;\r
+                               return -1;\r
+                       }\r
+\r
+                       public override void RemoveRange (int index, int count)\r
+                       {\r
+                               if ((index < 0) || (count < 0))\r
+                                       throw new ArgumentOutOfRangeException ();\r
+                               if ((index > Count) || (index + count) > Count)\r
+                                       throw new ArgumentException ();\r
+                               if (IsReadOnly || IsFixedSize)\r
+                                       throw new NotSupportedException ();\r
+\r
+                               for (int i  = index; i < count; i++)\r
+                                       list.RemoveAt (i);\r
+                       }\r
+\r
+                       public override void Reverse ()\r
+                       {\r
+                               Reverse (0, Count);\r
+                       }\r
+\r
+                       public override void Reverse (int index, int count)\r
+                       {\r
+                               if ((index < 0) || (count < 0))\r
+                                       throw new ArgumentOutOfRangeException ();\r
+                               if ((index > Count) || (index + count) > Count)\r
+                                       throw new ArgumentException ();\r
+                               if (IsReadOnly)\r
+                                       throw new NotSupportedException ();\r
+\r
+                               object tmp = null;\r
+\r
+                               for (int i = index; i < count; i++) {\r
+                                       tmp = list [i];\r
+                                       list [i] = list [count - i];\r
+                                       list [count - i] = tmp;\r
+                               }\r
+                       }\r
+\r
+                       public override void SetRange (int index, ICollection col)\r
+                       {\r
+                               if (index < 0 || (index + col.Count) > Count)\r
+                                       throw new ArgumentOutOfRangeException ();\r
+                               if (col == null)\r
+                                       throw new ArgumentNullException ();\r
+                               if (IsReadOnly)\r
+                                       throw new NotSupportedException ();\r
+\r
+                               for (int i = index; i < col.Count; i++)\r
+                                       foreach (object o in col)\r
+                                               list [i] = o;\r
+                       }\r
+\r
+                       [MonoTODO]\r
+                       public override void Sort ()\r
+                       {\r
+                       }\r
+\r
+                       [MonoTODO]\r
+                       public override void Sort (IComparer comparer)\r
+                       {\r
+                       }\r
+\r
+                       [MonoTODO]\r
+                       public override void Sort (int index, int count, IComparer comparer)\r
+                       {\r
+                       }\r
+\r
+                       public override object [] ToArray ()\r
+                       {\r
+                               return (object []) ToArray (typeof (object));\r
+                       }\r
+\r
+                       public override Array ToArray (Type type)\r
+                       {\r
+                               int count = Count;\r
+                               Array result = Array.CreateInstance (type, count);\r
+\r
+                               for (int i = 0; i < count; i++)\r
+                                       result.SetValue (list [i], i);\r
+\r
+                               return result;\r
+                       }\r
+\r
+                       [MonoTODO]\r
+                       public override void TrimToSize ()\r
+                       {\r
+                       }\r
+\r
+                       // IList\r
+                       public override bool IsFixedSize {\r
+                               get { return list.IsFixedSize; }\r
+                       }\r
+\r
+                       public override bool IsReadOnly {\r
+                               get { return list.IsReadOnly; }\r
+                       }\r
+\r
+                       public override object this [int index] {\r
+                               get { return list [index]; }\r
+                               set { list [index] = value; }\r
+                       }\r
+\r
+                       public override int Add (object value)\r
+                       {\r
+                               return list.Add (value);\r
+                       }\r
+\r
+                       public override void Clear ()\r
+                       {\r
+                               list.Clear ();\r
+                       }\r
+\r
+                       public override bool Contains (object value)\r
+                       {\r
+                               return list.Contains (value);\r
+                       }\r
+\r
+                       public override int IndexOf (object value)\r
+                       {\r
+                               return list.IndexOf (value);\r
+                       }\r
+\r
+                       public override void Insert (int index, object value)\r
+                       {\r
+                               list.Insert (index, value);\r
+                       }\r
+\r
+                       public override void Remove (object value)\r
+                       {\r
+                               list.Remove (value);\r
+                       }\r
+\r
+                       public override void RemoveAt (int index)\r
+                       {\r
+                               list.RemoveAt (index);\r
+                       }\r
+\r
+                       // ICollection                  \r
+                       public override int Count {\r
+                               get { return count; }\r
+                       }\r
+\r
+                       public override bool IsSynchronized {\r
+                               get { return ((ICollection) list).IsSynchronized; }\r
+                       }\r
+\r
+                       public override object SyncRoot {\r
+                               get { return ((ICollection) list).SyncRoot; }\r
+                       }\r
+\r
+                       public override void CopyTo (Array array, int index)\r
+                       {\r
+                               ((ICollection) list).CopyTo (array, index);\r
+                       }\r
+\r
+                       // ICloneable\r
+                       public override object Clone ()\r
+                       {\r
+                               return new ListWrapper (list);\r
+                       }\r
+\r
+                       // IEnumerable\r
+                       public override IEnumerator GetEnumerator ()\r
+                       {\r
+                               return ((IEnumerable) list).GetEnumerator ();\r
+                       }\r
+               }\r
+\r
                [MonoTODO]\r
                public static ArrayList Adapter (IList list)\r
                {\r
-                       throw new NotImplementedException ("System.Collections.ArrayList.Adapter");\r
+                       return new ListWrapper (list);\r
                }\r
 \r
                // properties\r
@@ -556,19 +858,22 @@ namespace System.Collections {
                        return LastIndexOf (value, startIndex, startIndex + 1);\r
                }\r
 \r
-               public virtual int LastIndexOf (object value, int StartIndex,\r
+               public virtual int LastIndexOf (object value, int startIndex,\r
                                                int count)\r
-                       {\r
-                               int EndIndex = StartIndex - count + 1;\r
-                               for (int i = StartIndex; i >= EndIndex; i--) {\r
-                                       if (Object.Equals (dataArray[i], value)) {\r
-                                               return i;\r
-                                       }\r
+               {\r
+                       if (startIndex > Count || count < 0 || (startIndex + count > Count))\r
+                               throw new ArgumentOutOfRangeException ();\r
+                       \r
+                       int EndIndex = startIndex - count + 1;\r
+                       for (int i = startIndex; i >= EndIndex; i--) {\r
+                               if (Object.Equals (dataArray[i], value)) {\r
+                                       return i;\r
                                }\r
-\r
-                               return -1;\r
                        }\r
 \r
+                       return -1;\r
+               }\r
+\r
                public virtual void Remove (object obj) {\r
 \r
                        if (IsFixedSize || IsReadOnly)\r
index 46e265fbdac028bdf018a72738dab73eac44acf8..656d00d6f26a254f7ac276939f6aa8050d4cc507 100644 (file)
@@ -1,3 +1,8 @@
+2002-05-23  Duncan Mak  <duncan@ximian.com>
+
+       * ArrayList.cs (Wrapper): Preliminary implementation of
+       ArrayList.Wrapper (IList).
+
 2002-05-22  Martin Baulig  <martin@gnome.org>
 
        * ArrayList.cs: Made count, capacity and dataArray the first three