3 // Implementation of the ECMA ArrayList.
5 // Copyright (c) 2003 Thong (Tum) Nguyen [tum@veridicus.com]
7 // http://www.opensource.org/licenses/mit-license.html
9 // Permission is hereby granted, free of charge, to any person obtaining a copy of this
10 // software and associated documentation files (the "Software"), to deal in the
11 // Software without restriction, including without limitation the rights to use, copy,
12 // modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
13 // and to permit persons to whom the Software is furnished to do so, subject to the
14 // following conditions:
16 // The above copyright notice and this permission notice shall be included in all copies
17 // or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
30 // Permission is hereby granted, free of charge, to any person obtaining
31 // a copy of this software and associated documentation files (the
32 // "Software"), to deal in the Software without restriction, including
33 // without limitation the rights to use, copy, modify, merge, publish,
34 // distribute, sublicense, and/or sell copies of the Software, and to
35 // permit persons to whom the Software is furnished to do so, subject to
36 // the following conditions:
38 // The above copyright notice and this permission notice shall be
39 // included in all copies or substantial portions of the Software.
41 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
42 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
43 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
44 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
45 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
46 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
47 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
51 using System.Collections;
52 using System.Runtime.InteropServices;
54 namespace System.Collections
60 public class ArrayList
61 : IList, ICloneable, ICollection, IEnumerable
65 private sealed class ArrayListEnumerator
66 : IEnumerator, ICloneable
71 private object m_Current;
72 private ArrayList m_List;
73 private int m_ExpectedStateChanges;
75 public ArrayListEnumerator(ArrayList list)
76 : this(list, 0, list.Count)
82 return this.MemberwiseClone();
85 public ArrayListEnumerator(ArrayList list, int index, int count)
92 m_ExpectedStateChanges = list._version;
99 if (m_Pos == m_Index - 1) {
100 throw new InvalidOperationException("Enumerator unusable (Reset pending, or past end of array.");
107 public bool MoveNext()
109 if (m_List._version != m_ExpectedStateChanges)
111 throw new InvalidOperationException("List has changed.");
116 if (m_Pos - m_Index < m_Count)
118 m_Current = m_List[m_Pos];
133 sealed class SimpleEnumerator : IEnumerator, ICloneable
138 object currentElement;
139 static object endFlag = new object ();
141 public SimpleEnumerator (ArrayList list)
145 version = list._version;
146 currentElement = endFlag;
149 public object Clone ()
151 return MemberwiseClone ();
154 public bool MoveNext ()
156 if (version != list._version)
157 throw new InvalidOperationException("List has changed.");
159 if (++index < list.Count) {
160 currentElement = list [index];
163 currentElement = endFlag;
168 public object Current {
170 if (currentElement == endFlag) {
172 throw new InvalidOperationException ("Enumerator not started");
174 throw new InvalidOperationException ("Enumerator ended");
177 return currentElement;
183 if (version != list._version)
184 throw new InvalidOperationException ("List has changed.");
186 currentElement = endFlag;
193 #region ArrayListAdapter
196 /// Adapts various ILists into an ArrayList.
199 private sealed class ArrayListAdapter
202 private sealed class EnumeratorWithRange
203 : IEnumerator, ICloneable
205 private int m_StartIndex;
207 private int m_MaxCount;
208 private IEnumerator m_Enumerator;
210 public EnumeratorWithRange(IEnumerator enumerator, int index, int count)
213 m_StartIndex = index;
215 m_Enumerator = enumerator;
220 public object Clone()
222 return this.MemberwiseClone();
225 public object Current
229 return m_Enumerator.Current;
233 public bool MoveNext()
235 if (m_Count >= m_MaxCount)
242 return m_Enumerator.MoveNext();
248 m_Enumerator.Reset();
250 for (int i = 0; i < m_StartIndex; i++)
252 m_Enumerator.MoveNext();
257 private IList m_Adaptee;
259 public ArrayListAdapter(IList adaptee)
265 public override object this[int index]
269 return m_Adaptee[index];
274 m_Adaptee[index] = value;
278 public override int Count
282 return m_Adaptee.Count;
286 public override int Capacity
290 return m_Adaptee.Count;
295 if (value < m_Adaptee.Count)
297 throw new ArgumentException("capacity");
302 public override bool IsFixedSize
306 return m_Adaptee.IsFixedSize;
310 public override bool IsReadOnly
314 return m_Adaptee.IsReadOnly;
318 public override object SyncRoot
322 return m_Adaptee.SyncRoot;
326 public override int Add(object value)
328 return m_Adaptee.Add(value);
331 public override void Clear()
336 public override bool Contains(object value)
338 return m_Adaptee.Contains(value);
341 public override int IndexOf(object value)
343 return m_Adaptee.IndexOf(value);
346 public override int IndexOf(object value, int startIndex)
348 return IndexOf(value, startIndex, m_Adaptee.Count - startIndex);
351 public override int IndexOf(object value, int startIndex, int count)
353 if (startIndex < 0 || startIndex > m_Adaptee.Count)
355 throw new ArgumentOutOfRangeException("startIndex", startIndex,
356 "Does not specify valid index.");
361 throw new ArgumentOutOfRangeException("count", count,
362 "Can't be less than 0.");
365 // re-ordered to avoid possible integer overflow
366 if (startIndex > m_Adaptee.Count - count) {
367 // LAMESPEC: Every other method throws ArgumentException
368 throw new ArgumentOutOfRangeException("count",
369 "Start index and count do not specify a valid range.");
374 for (int i = startIndex; i < startIndex + count; i++)
376 if (m_Adaptee[i] == null)
384 for (int i = startIndex; i < startIndex + count; i++)
386 if (value.Equals(m_Adaptee[i]))
396 public override int LastIndexOf(object value)
398 return LastIndexOf(value, m_Adaptee.Count - 1);
401 public override int LastIndexOf(object value, int startIndex)
403 return LastIndexOf(value, startIndex, startIndex + 1);
406 public override int LastIndexOf(object value, int startIndex, int count)
410 throw new ArgumentOutOfRangeException("startIndex", startIndex, "< 0");
415 throw new ArgumentOutOfRangeException("count", count, "count is negative.");
418 if (startIndex - count + 1 < 0)
420 throw new ArgumentOutOfRangeException("count", count, "count is too large.");
425 for (int i = startIndex; i > startIndex - count; i--)
427 if (m_Adaptee[i] == null)
435 for (int i = startIndex; i > startIndex - count; i--)
437 if (value.Equals(m_Adaptee[i]))
447 public override void Insert(int index, object value)
449 m_Adaptee.Insert(index, value);
452 public override void InsertRange(int index, ICollection c)
456 throw new ArgumentNullException("c");
459 if (index > m_Adaptee.Count)
461 throw new ArgumentOutOfRangeException("index", index,
462 "Index must be >= 0 and <= Count.");
465 foreach (object value in c)
467 m_Adaptee.Insert(index++, value);
471 public override void Remove(object value)
473 m_Adaptee.Remove(value);
476 public override void RemoveAt(int index)
478 m_Adaptee.RemoveAt(index);
481 public override void RemoveRange(int index, int count)
483 CheckRange(index, count, m_Adaptee.Count);
485 for (int i = 0; i < count; i++)
487 m_Adaptee.RemoveAt(index);
491 public override void Reverse()
493 Reverse(0, m_Adaptee.Count);
496 public override void Reverse(int index, int count)
500 CheckRange(index, count, m_Adaptee.Count);
502 for (int i = 0; i < count / 2; i++)
504 tmp = m_Adaptee[i + index];
505 m_Adaptee[i + index] = m_Adaptee[(index + count) - i + index - 1];
506 m_Adaptee[(index + count) - i + index - 1] = tmp;
510 public override void SetRange(int index, ICollection c)
514 throw new ArgumentNullException("c");
517 if (index < 0 || index + c.Count > m_Adaptee.Count)
519 throw new ArgumentOutOfRangeException("index");
524 foreach (object value in c)
526 m_Adaptee[x++] = value;
530 public override void CopyTo(System.Array array)
532 m_Adaptee.CopyTo(array, 0);
535 public override void CopyTo(System.Array array, int index)
537 m_Adaptee.CopyTo(array, index);
540 public override void CopyTo(int index, System.Array array, int arrayIndex, int count)
544 throw new ArgumentOutOfRangeException("index", index,
545 "Can't be less than zero.");
550 throw new ArgumentOutOfRangeException("arrayIndex", arrayIndex,
551 "Can't be less than zero.");
556 throw new ArgumentOutOfRangeException("index", index,
557 "Can't be less than zero.");
560 if (index >= m_Adaptee.Count)
562 throw new ArgumentException("Can't be more or equal to list count.",
568 throw new ArgumentException("Can't copy into multi-dimensional array.");
571 if (arrayIndex >= array.Length)
573 throw new ArgumentException("arrayIndex can't be greater than array.Length - 1.");
576 if (array.Length - arrayIndex + 1 < count)
578 throw new ArgumentException("Destination array is too small.");
581 // re-ordered to avoid possible integer overflow
582 if (index > m_Adaptee.Count - count) {
583 throw new ArgumentException("Index and count do not denote a valid range of elements.", "index");
586 for (int i = 0; i < count; i++)
588 array.SetValue(m_Adaptee[index + i], arrayIndex + i);
592 public override bool IsSynchronized
596 return m_Adaptee.IsSynchronized;
600 public override IEnumerator GetEnumerator()
602 return m_Adaptee.GetEnumerator();
605 public override IEnumerator GetEnumerator(int index, int count)
607 CheckRange(index, count, m_Adaptee.Count);
609 return new EnumeratorWithRange(m_Adaptee.GetEnumerator(), index, count);
612 public override void AddRange(ICollection c)
614 foreach (object value in c)
616 m_Adaptee.Add(value);
620 public override int BinarySearch(object value)
622 return BinarySearch(value, null);
625 public override int BinarySearch(object value, IComparer comparer)
627 return BinarySearch(0, m_Adaptee.Count, value, comparer);
630 public override int BinarySearch(int index, int count, object value, IComparer comparer)
634 // Doing a direct BinarySearch on the adaptee will perform poorly if the adaptee is a linked-list.
635 // Alternatives include copying the adaptee to a temporary array first.
637 CheckRange(index, count, m_Adaptee.Count);
639 if (comparer == null)
641 comparer = Comparer.Default;
645 y = index + count - 1;
651 r = comparer.Compare(value, m_Adaptee[z]);
670 public override object Clone()
672 return new ArrayList.ArrayListAdapter(m_Adaptee);
675 public override ArrayList GetRange(int index, int count)
677 CheckRange(index, count, m_Adaptee.Count);
679 return new RangedArrayList(this, index, count);
682 public override void TrimToSize()
687 public override void Sort()
689 Sort(Comparer.Default);
692 public override void Sort(IComparer comparer)
694 Sort(0, m_Adaptee.Count, comparer);
697 public override void Sort(int index, int count, IComparer comparer)
699 CheckRange(index, count, m_Adaptee.Count);
701 if (comparer == null)
703 comparer = Comparer.Default;
706 // Doing a direct sort on the adaptee will perform poorly if the adaptee is a linked-list.
707 // Alternatives include copying the adaptee into a temporary array first.
709 QuickSort(m_Adaptee, index, index + count - 1, comparer);
714 /// Swaps two items in a list at the specified indexes.
716 private static void Swap(IList list, int x, int y)
726 /// Quicksort for lists.
729 /// This function acts as both qsort() and partition().
731 internal static void QuickSort(IList list, int left, int right, IComparer comparer)
741 // Pick the pivot using the median-of-three strategy.
743 middle = (left + right) / 2;
745 if (comparer.Compare(list[middle], list[left]) < 0)
747 Swap(list, middle, left);
750 if (comparer.Compare(list[right], list[left]) < 0)
752 Swap(list, right, left);
755 if (comparer.Compare(list[right], list[middle]) < 0)
757 Swap(list, right, middle);
760 if (right - left + 1 <= 3)
765 // Put the pivot in right - 1.
766 Swap(list, right - 1, middle);
768 // List should look like:
770 // [Small] ..Numbers.. [Middle] ..Numbers.. [Pivot][Large]
772 pivot = list[right - 1];
774 // Sort from (left + 1) to (right - 2).
781 while (comparer.Compare(list[++i], pivot) < 0);
782 while (comparer.Compare(list[--j], pivot) > 0);
794 // Put pivot into the right position (real middle).
796 Swap(list, right - 1, i);
798 // Recursively sort the left and right sub lists.
800 QuickSort(list, left, i - 1, comparer);
801 QuickSort(list, i + 1, right, comparer);
804 public override object[] ToArray()
808 retval = new object[m_Adaptee.Count];
810 m_Adaptee.CopyTo(retval, 0);
815 public override Array ToArray(Type elementType)
819 retval = Array.CreateInstance(elementType, m_Adaptee.Count);
821 m_Adaptee.CopyTo(retval, 0);
827 #endregion // ArrayListAdapter
830 // ArrayList wrappers
833 #region ArrayListWrapper
836 /// Base wrapper/decorator for ArrayLists. Simply delegates all methods to
837 /// the underlying wrappee.
840 private class ArrayListWrapper
843 protected ArrayList m_InnerArrayList;
847 public ArrayListWrapper(ArrayList innerArrayList)
849 m_InnerArrayList = innerArrayList;
856 public override object this[int index]
860 return m_InnerArrayList[index];
865 m_InnerArrayList[index] = value;
873 public override int Count
877 return m_InnerArrayList.Count;
881 public override int Capacity
885 return m_InnerArrayList.Capacity;
890 m_InnerArrayList.Capacity = value;
894 public override bool IsFixedSize
898 return m_InnerArrayList.IsFixedSize;
902 public override bool IsReadOnly
906 return m_InnerArrayList.IsReadOnly;
910 public override bool IsSynchronized
914 return m_InnerArrayList.IsSynchronized;
918 public override object SyncRoot
922 return m_InnerArrayList.SyncRoot;
930 public override int Add(object value)
932 return m_InnerArrayList.Add(value);
935 public override void Clear()
937 m_InnerArrayList.Clear();
940 public override bool Contains(object value)
942 return m_InnerArrayList.Contains(value);
945 public override int IndexOf(object value)
947 return m_InnerArrayList.IndexOf(value);
950 public override int IndexOf(object value, int startIndex)
952 return m_InnerArrayList.IndexOf(value, startIndex);
955 public override int IndexOf(object value, int startIndex, int count)
957 return m_InnerArrayList.IndexOf(value, startIndex, count);
960 public override int LastIndexOf(object value)
962 return m_InnerArrayList.LastIndexOf(value);
965 public override int LastIndexOf(object value, int startIndex)
967 return m_InnerArrayList.LastIndexOf(value, startIndex);
970 public override int LastIndexOf(object value, int startIndex, int count)
972 return m_InnerArrayList.LastIndexOf(value, startIndex, count);
975 public override void Insert(int index, object value)
977 m_InnerArrayList.Insert(index, value);
980 public override void InsertRange(int index, ICollection c)
982 m_InnerArrayList.InsertRange(index, c);
985 public override void Remove(object value)
987 m_InnerArrayList.Remove(value);
990 public override void RemoveAt(int index)
992 m_InnerArrayList.RemoveAt(index);
995 public override void RemoveRange(int index, int count)
997 m_InnerArrayList.RemoveRange(index, count);
1000 public override void Reverse()
1002 m_InnerArrayList.Reverse();
1005 public override void Reverse(int index, int count)
1007 m_InnerArrayList.Reverse(index, count);
1010 public override void SetRange(int index, ICollection c)
1012 m_InnerArrayList.SetRange(index, c);
1015 public override void CopyTo(System.Array array)
1017 m_InnerArrayList.CopyTo(array);
1020 public override void CopyTo(System.Array array, int index)
1022 m_InnerArrayList.CopyTo(array, index);
1025 public override void CopyTo(int index, System.Array array, int arrayIndex, int count)
1027 m_InnerArrayList.CopyTo(index, array, arrayIndex, count);
1030 public override IEnumerator GetEnumerator()
1032 return m_InnerArrayList.GetEnumerator();
1035 public override IEnumerator GetEnumerator(int index, int count)
1037 return m_InnerArrayList.GetEnumerator(index, count);
1040 public override void AddRange(ICollection c)
1042 m_InnerArrayList.AddRange(c);
1045 public override int BinarySearch(object value)
1047 return m_InnerArrayList.BinarySearch(value);
1050 public override int BinarySearch(object value, IComparer comparer)
1052 return m_InnerArrayList.BinarySearch(value, comparer);
1055 public override int BinarySearch(int index, int count, object value, IComparer comparer)
1057 return m_InnerArrayList.BinarySearch(index, count, value, comparer);
1060 public override object Clone()
1062 return m_InnerArrayList.Clone();
1065 public override ArrayList GetRange(int index, int count)
1067 return m_InnerArrayList.GetRange(index, count);
1070 public override void TrimToSize()
1072 m_InnerArrayList.TrimToSize();
1075 public override void Sort()
1077 m_InnerArrayList.Sort();
1080 public override void Sort(IComparer comparer)
1082 m_InnerArrayList.Sort(comparer);
1085 public override void Sort(int index, int count, IComparer comparer)
1087 m_InnerArrayList.Sort(index, count, comparer);
1090 public override object[] ToArray()
1092 return m_InnerArrayList.ToArray();
1095 public override Array ToArray(Type elementType)
1097 return m_InnerArrayList.ToArray(elementType);
1105 #region SynchronizedArrayListWrapper
1108 /// ArrayListWrapper that synchronizes calls to all methods/properties.
1111 /// Works by just synchronizing all method calls. In the future careful optimisation
1112 /// could give better performance...
1115 private sealed class SynchronizedArrayListWrapper
1118 private object m_SyncRoot;
1120 #region Constructors
1123 /// Creates a new synchronized wrapper for the given <see cref="ArrayList"/>.
1125 /// <param name="innerArrayList"></param>
1126 internal SynchronizedArrayListWrapper(ArrayList innerArrayList)
1127 : base(innerArrayList)
1129 m_SyncRoot = innerArrayList.SyncRoot;
1136 public override object this[int index]
1142 return m_InnerArrayList[index];
1150 m_InnerArrayList[index] = value;
1159 // Some of these properties may be calculated so it's best to synchronize
1160 // them even though it might cause a performance hit.
1161 // Better safe than sorry ;D.
1163 public override int Count
1169 return m_InnerArrayList.Count;
1174 public override int Capacity
1180 return m_InnerArrayList.Capacity;
1188 m_InnerArrayList.Capacity = value;
1193 public override bool IsFixedSize
1199 return m_InnerArrayList.IsFixedSize;
1204 public override bool IsReadOnly
1210 return m_InnerArrayList.IsReadOnly;
1215 public override bool IsSynchronized
1223 public override object SyncRoot
1235 public override int Add(object value)
1239 return m_InnerArrayList.Add(value);
1243 public override void Clear()
1247 m_InnerArrayList.Clear();
1251 public override bool Contains(object value)
1255 return m_InnerArrayList.Contains(value);
1259 public override int IndexOf(object value)
1263 return m_InnerArrayList.IndexOf(value);
1267 public override int IndexOf(object value, int startIndex)
1271 return m_InnerArrayList.IndexOf(value, startIndex);
1275 public override int IndexOf(object value, int startIndex, int count)
1279 return m_InnerArrayList.IndexOf(value, startIndex, count);
1283 public override int LastIndexOf(object value)
1287 return m_InnerArrayList.LastIndexOf(value);
1291 public override int LastIndexOf(object value, int startIndex)
1295 return m_InnerArrayList.LastIndexOf(value, startIndex);
1299 public override int LastIndexOf(object value, int startIndex, int count)
1303 return m_InnerArrayList.LastIndexOf(value, startIndex, count);
1307 public override void Insert(int index, object value)
1311 m_InnerArrayList.Insert(index, value);
1315 public override void InsertRange(int index, ICollection c)
1319 m_InnerArrayList.InsertRange(index, c);
1323 public override void Remove(object value)
1327 m_InnerArrayList.Remove(value);
1331 public override void RemoveAt(int index)
1335 m_InnerArrayList.RemoveAt(index);
1339 public override void RemoveRange(int index, int count)
1343 m_InnerArrayList.RemoveRange(index, count);
1347 public override void Reverse()
1351 m_InnerArrayList.Reverse();
1355 public override void Reverse(int index, int count)
1359 m_InnerArrayList.Reverse(index, count);
1363 public override void CopyTo(System.Array array)
1367 m_InnerArrayList.CopyTo(array);
1371 public override void CopyTo(System.Array array, int index)
1375 m_InnerArrayList.CopyTo(array, index);
1379 public override void CopyTo(int index, System.Array array, int arrayIndex, int count)
1383 m_InnerArrayList.CopyTo(index, array, arrayIndex, count);
1387 public override IEnumerator GetEnumerator()
1391 return m_InnerArrayList.GetEnumerator();
1395 public override IEnumerator GetEnumerator(int index, int count)
1399 return m_InnerArrayList.GetEnumerator(index, count);
1403 public override void AddRange(ICollection c)
1407 m_InnerArrayList.AddRange(c);
1411 public override int BinarySearch(object value)
1415 return m_InnerArrayList.BinarySearch(value);
1419 public override int BinarySearch(object value, IComparer comparer)
1423 return m_InnerArrayList.BinarySearch(value, comparer);
1427 public override int BinarySearch(int index, int count, object value, IComparer comparer)
1431 return m_InnerArrayList.BinarySearch(index, count, value, comparer);
1435 public override object Clone()
1439 return m_InnerArrayList.Clone();
1443 public override ArrayList GetRange(int index, int count)
1447 return m_InnerArrayList.GetRange(index, count);
1451 public override void TrimToSize()
1455 m_InnerArrayList.TrimToSize();
1459 public override void Sort()
1463 m_InnerArrayList.Sort();
1467 public override void Sort(IComparer comparer)
1471 m_InnerArrayList.Sort(comparer);
1475 public override void Sort(int index, int count, IComparer comparer)
1479 m_InnerArrayList.Sort(index, count, comparer);
1483 public override object[] ToArray()
1487 return m_InnerArrayList.ToArray();
1491 public override Array ToArray(Type elementType)
1495 return m_InnerArrayList.ToArray(elementType);
1504 #region FixedSizeArrayListWrapper
1507 private class FixedSizeArrayListWrapper
1510 #region Constructors
1512 public FixedSizeArrayListWrapper(ArrayList innerList)
1522 /// Gets the error message to display when an readonly/fixedsize related exception is
1525 protected virtual string ErrorMessage
1529 return "Can't add or remove from a fixed-size list.";
1533 public override int Capacity
1537 return base.Capacity;
1542 throw new NotSupportedException(this.ErrorMessage);
1546 public override bool IsFixedSize
1558 public override int Add(object value)
1560 throw new NotSupportedException(this.ErrorMessage);
1563 public override void AddRange(ICollection c)
1565 throw new NotSupportedException(this.ErrorMessage);
1568 public override void Clear()
1570 throw new NotSupportedException(this.ErrorMessage);
1573 public override void Insert(int index, object value)
1575 throw new NotSupportedException(this.ErrorMessage);
1578 public override void InsertRange(int index, ICollection c)
1580 throw new NotSupportedException(this.ErrorMessage);
1583 public override void Remove(object value)
1585 throw new NotSupportedException(this.ErrorMessage);
1588 public override void RemoveAt(int index)
1590 throw new NotSupportedException(this.ErrorMessage);
1593 public override void RemoveRange(int index, int count)
1595 throw new NotSupportedException(this.ErrorMessage);
1598 public override void TrimToSize()
1600 throw new NotSupportedException(this.ErrorMessage);
1608 #region ReadOnlyArrayListWrapper
1611 private sealed class ReadOnlyArrayListWrapper
1612 : FixedSizeArrayListWrapper
1614 protected override string ErrorMessage
1618 return "Can't modify a readonly list.";
1622 public override bool IsReadOnly
1630 public ReadOnlyArrayListWrapper(ArrayList innerArrayList)
1631 : base(innerArrayList)
1635 public override object this[int index]
1639 return m_InnerArrayList[index];
1644 throw new NotSupportedException(this.ErrorMessage);
1648 public override void Reverse()
1650 throw new NotSupportedException(this.ErrorMessage);
1653 public override void Reverse(int index, int count)
1655 throw new NotSupportedException(this.ErrorMessage);
1658 public override void SetRange(int index, ICollection c)
1660 throw new NotSupportedException(this.ErrorMessage);
1663 public override void Sort()
1665 throw new NotSupportedException(this.ErrorMessage);
1668 public override void Sort(IComparer comparer)
1670 throw new NotSupportedException(this.ErrorMessage);
1673 public override void Sort(int index, int count, IComparer comparer)
1675 throw new NotSupportedException(this.ErrorMessage);
1681 #region RangedArrayList
1684 private sealed class RangedArrayList
1687 private int m_InnerIndex;
1688 private int m_InnerCount;
1689 private int m_InnerStateChanges;
1691 public RangedArrayList(ArrayList innerList, int index, int count)
1694 m_InnerIndex = index;
1695 m_InnerCount = count;
1696 m_InnerStateChanges = innerList._version;
1701 public override bool IsSynchronized
1709 public override object this[int index]
1713 if (index < 0 || index > m_InnerCount)
1715 throw new ArgumentOutOfRangeException("index");
1718 return m_InnerArrayList[m_InnerIndex + index];
1723 if (index < 0 || index > m_InnerCount)
1725 throw new ArgumentOutOfRangeException("index");
1728 m_InnerArrayList[m_InnerIndex + index] = value;
1736 public override int Count
1740 VerifyStateChanges();
1742 return m_InnerCount;
1746 public override int Capacity
1750 return m_InnerArrayList.Capacity;
1755 if (value < m_InnerCount)
1757 throw new ArgumentOutOfRangeException();
1766 private void VerifyStateChanges()
1768 if (m_InnerStateChanges != m_InnerArrayList._version)
1770 throw new InvalidOperationException
1771 ("ArrayList view is invalid because the underlying ArrayList was modified.");
1775 public override int Add(object value)
1777 VerifyStateChanges();
1779 m_InnerArrayList.Insert(m_InnerIndex + m_InnerCount, value);
1781 m_InnerStateChanges = m_InnerArrayList._version;
1783 return ++m_InnerCount;
1786 public override void Clear()
1788 VerifyStateChanges();
1790 m_InnerArrayList.RemoveRange(m_InnerIndex, m_InnerCount);
1793 m_InnerStateChanges = m_InnerArrayList._version;
1796 public override bool Contains(object value)
1798 return m_InnerArrayList.Contains(value, m_InnerIndex, m_InnerCount);
1801 public override int IndexOf(object value)
1803 return IndexOf(value, 0);
1806 public override int IndexOf(object value, int startIndex)
1808 return IndexOf(value, startIndex, m_InnerCount - startIndex);
1811 public override int IndexOf(object value, int startIndex, int count)
1813 if (startIndex < 0 || startIndex > m_InnerCount)
1815 throw new ArgumentOutOfRangeException("startIndex", startIndex,
1816 "Does not specify valid index.");
1821 throw new ArgumentOutOfRangeException("count", count,
1822 "Can't be less than 0.");
1825 // re-ordered to avoid possible integer overflow
1826 if (startIndex > m_InnerCount - count)
1828 // LAMESPEC: Every other method throws ArgumentException
1830 throw new ArgumentOutOfRangeException("count",
1831 "Start index and count do not specify a valid range.");
1834 int retval = m_InnerArrayList.IndexOf(value, m_InnerIndex + startIndex, count);
1842 return retval - m_InnerIndex;
1846 public override int LastIndexOf(object value)
1848 return LastIndexOf(value, m_InnerCount - 1);
1851 public override int LastIndexOf(object value, int startIndex)
1853 return LastIndexOf(value, startIndex, startIndex + 1);
1856 public override int LastIndexOf(object value, int startIndex, int count)
1860 throw new ArgumentOutOfRangeException("startIndex", startIndex, "< 0");
1865 throw new ArgumentOutOfRangeException("count", count, "count is negative.");
1868 int retval = m_InnerArrayList.LastIndexOf(value, m_InnerIndex + startIndex, count);
1876 return retval - m_InnerIndex;
1880 public override void Insert(int index, object value)
1882 VerifyStateChanges();
1884 if (index < 0 || index > m_InnerCount)
1886 throw new ArgumentOutOfRangeException("index", index,
1887 "Index must be >= 0 and <= Count.");
1890 m_InnerArrayList.Insert(m_InnerIndex + index, value);
1894 m_InnerStateChanges = m_InnerArrayList._version;
1897 public override void InsertRange(int index, ICollection c)
1899 VerifyStateChanges();
1901 if (index < 0 || index > m_InnerCount)
1903 throw new ArgumentOutOfRangeException("index", index,
1904 "Index must be >= 0 and <= Count.");
1907 m_InnerArrayList.InsertRange(m_InnerIndex + index, c);
1909 m_InnerCount += c.Count;
1911 m_InnerStateChanges = m_InnerArrayList._version;
1914 public override void Remove(object value)
1916 VerifyStateChanges();
1918 int x = IndexOf(value);
1925 m_InnerStateChanges = m_InnerArrayList._version;
1928 public override void RemoveAt(int index)
1930 VerifyStateChanges();
1932 if (index < 0 || index > m_InnerCount)
1934 throw new ArgumentOutOfRangeException("index", index,
1935 "Index must be >= 0 and <= Count.");
1938 m_InnerArrayList.RemoveAt(m_InnerIndex + index);
1941 m_InnerStateChanges = m_InnerArrayList._version;
1944 public override void RemoveRange(int index, int count)
1946 VerifyStateChanges();
1948 CheckRange(index, count, m_InnerCount);
1950 m_InnerArrayList.RemoveRange(m_InnerIndex + index, count);
1952 m_InnerCount -= count;
1954 m_InnerStateChanges = m_InnerArrayList._version;
1957 public override void Reverse()
1959 Reverse(0, m_InnerCount);
1962 public override void Reverse(int index, int count)
1964 VerifyStateChanges();
1966 CheckRange(index, count, m_InnerCount);
1968 m_InnerArrayList.Reverse(m_InnerIndex + index, count);
1970 m_InnerStateChanges = m_InnerArrayList._version;
1973 public override void SetRange(int index, ICollection c)
1975 VerifyStateChanges();
1977 if (index < 0 || index > m_InnerCount)
1979 throw new ArgumentOutOfRangeException("index", index,
1980 "Index must be >= 0 and <= Count.");
1983 m_InnerArrayList.SetRange(m_InnerIndex + index, c);
1985 m_InnerStateChanges = m_InnerArrayList._version;
1988 public override void CopyTo(System.Array array)
1993 public override void CopyTo(System.Array array, int index)
1995 CopyTo(0, array, index, m_InnerCount);
1998 public override void CopyTo(int index, System.Array array, int arrayIndex, int count)
2000 CheckRange(index, count, m_InnerCount);
2002 m_InnerArrayList.CopyTo(m_InnerIndex + index, array, arrayIndex, count);
2005 public override IEnumerator GetEnumerator()
2007 return GetEnumerator(0, m_InnerCount);
2010 public override IEnumerator GetEnumerator(int index, int count)
2012 CheckRange(index, count, m_InnerCount);
2014 return m_InnerArrayList.GetEnumerator(m_InnerIndex + index, count);
2017 public override void AddRange(ICollection c)
2019 VerifyStateChanges();
2021 m_InnerArrayList.InsertRange(m_InnerCount, c);
2023 m_InnerCount += c.Count;
2025 m_InnerStateChanges = m_InnerArrayList._version;
2028 public override int BinarySearch(object value)
2030 return BinarySearch(0, m_InnerCount, value, Comparer.Default);
2033 public override int BinarySearch(object value, IComparer comparer)
2035 return BinarySearch(0, m_InnerCount, value, comparer);
2038 public override int BinarySearch(int index, int count, object value, IComparer comparer)
2040 CheckRange(index, count, m_InnerCount);
2042 return m_InnerArrayList.BinarySearch(m_InnerIndex + index, count, value, comparer);
2045 public override object Clone()
2047 return new RangedArrayList((ArrayList)m_InnerArrayList.Clone(), m_InnerIndex, m_InnerCount);
2050 public override ArrayList GetRange(int index, int count)
2052 CheckRange(index, count, m_InnerCount);
2054 return new RangedArrayList(this, index, count);
2057 public override void TrimToSize()
2059 throw new NotSupportedException();
2062 public override void Sort()
2064 Sort(Comparer.Default);
2067 public override void Sort(IComparer comparer)
2069 Sort(0, m_InnerCount, comparer);
2072 public override void Sort(int index, int count, IComparer comparer)
2074 VerifyStateChanges();
2076 CheckRange(index, count, m_InnerCount);
2078 m_InnerArrayList.Sort(m_InnerIndex + index, count, comparer);
2080 m_InnerStateChanges = m_InnerArrayList._version;
2083 public override object[] ToArray()
2087 array = new object[m_InnerCount];
2089 m_InnerArrayList.CopyTo (m_InnerIndex, array, 0, m_InnerCount);
2094 public override Array ToArray(Type elementType)
2098 array = Array.CreateInstance(elementType, m_InnerCount);
2100 m_InnerArrayList.CopyTo(m_InnerIndex, array, 0, m_InnerCount);
2114 #region SynchronizedListWrapper
2117 private sealed class SynchronizedListWrapper
2120 private object m_SyncRoot;
2122 public SynchronizedListWrapper(IList innerList)
2125 m_SyncRoot = innerList.SyncRoot;
2128 public override int Count
2134 return m_InnerList.Count;
2139 public override bool IsSynchronized
2147 public override object SyncRoot
2153 return m_InnerList.SyncRoot;
2158 public override bool IsFixedSize
2164 return m_InnerList.IsFixedSize;
2169 public override bool IsReadOnly
2175 return m_InnerList.IsReadOnly;
2180 public override object this[int index]
2186 return m_InnerList[index];
2194 m_InnerList[index] = value;
2199 public override int Add(object value)
2203 return m_InnerList.Add(value);
2207 public override void Clear()
2211 m_InnerList.Clear();
2215 public override bool Contains(object value)
2219 return m_InnerList.Contains(value);
2223 public override int IndexOf(object value)
2227 return m_InnerList.IndexOf(value);
2231 public override void Insert(int index, object value)
2235 m_InnerList.Insert(index, value);
2239 public override void Remove(object value)
2243 m_InnerList.Remove(value);
2247 public override void RemoveAt(int index)
2251 m_InnerList.RemoveAt(index);
2255 public override void CopyTo(Array array, int index)
2259 m_InnerList.CopyTo(array, index);
2263 public override IEnumerator GetEnumerator()
2267 return m_InnerList.GetEnumerator();
2274 #region FixedSizeListWrapper
2277 private class FixedSizeListWrapper
2280 protected virtual string ErrorMessage
2284 return "List is fixed-size.";
2288 public override bool IsFixedSize
2296 public FixedSizeListWrapper(IList innerList)
2301 public override int Add(object value)
2303 throw new NotSupportedException(this.ErrorMessage);
2306 public override void Clear()
2308 throw new NotSupportedException(this.ErrorMessage);
2311 public override void Insert(int index, object value)
2313 throw new NotSupportedException(this.ErrorMessage);
2316 public override void Remove(object value)
2318 throw new NotSupportedException(this.ErrorMessage);
2321 public override void RemoveAt(int index)
2323 throw new NotSupportedException(this.ErrorMessage);
2329 #region ReadOnlyListWrapper
2332 private sealed class ReadOnlyListWrapper
2333 : FixedSizeListWrapper
2335 protected override string ErrorMessage
2339 return "List is read-only.";
2343 public override bool IsReadOnly
2351 public ReadOnlyListWrapper(IList innerList)
2356 public override object this[int index]
2360 return m_InnerList[index];
2365 throw new NotSupportedException(this.ErrorMessage);
2375 /// Decorates/Wraps any <c>IList</c> implementing object.
2378 private class ListWrapper
2383 protected IList m_InnerList;
2387 #region Constructors
2389 public ListWrapper(IList innerList)
2391 m_InnerList = innerList;
2398 public virtual object this[int index]
2402 return m_InnerList[index];
2407 m_InnerList[index] = value;
2415 public virtual int Count
2419 return m_InnerList.Count;
2423 public virtual bool IsSynchronized
2427 return m_InnerList.IsSynchronized;
2431 public virtual object SyncRoot
2435 return m_InnerList.SyncRoot;
2439 public virtual bool IsFixedSize
2443 return m_InnerList.IsFixedSize;
2447 public virtual bool IsReadOnly
2451 return m_InnerList.IsReadOnly;
2459 public virtual int Add(object value)
2461 return m_InnerList.Add(value);
2464 public virtual void Clear()
2466 m_InnerList.Clear();
2469 public virtual bool Contains(object value)
2471 return m_InnerList.Contains(value);
2474 public virtual int IndexOf(object value)
2476 return m_InnerList.IndexOf(value);
2479 public virtual void Insert(int index, object value)
2481 m_InnerList.Insert(index, value);
2484 public virtual void Remove(object value)
2486 m_InnerList.Remove(value);
2489 public virtual void RemoveAt(int index)
2491 m_InnerList.RemoveAt(index);
2494 public virtual void CopyTo(Array array, int index)
2496 m_InnerList.CopyTo(array, index);
2499 public virtual IEnumerator GetEnumerator()
2501 return m_InnerList.GetEnumerator();
2510 // Start of ArrayList
2515 private const int DefaultInitialCapacity = 0x10;
2518 /// Number of items in the list.
2523 /// Array to store the items.
2525 private object[] _items;
2528 /// Total number of state changes.
2530 private int _version;
2534 #region Constructors
2537 /// Initializes a new instance of the <see cref="ArrayList"/> class that is empty and
2538 /// has the default initial capacity (16).
2542 _items = new object[DefaultInitialCapacity];
2546 /// Initializes a new instance of the <see cref="ArrayList"/> class that contains
2547 /// elements copied from the specified collection and that has the same initial capacity
2548 /// as the number of elements copied.
2550 /// <param name="c">
2551 /// The <see cref="ICollection"/> whose elements are copied into the new list.
2553 /// <exception cref="ArgumentNullException">
2554 /// The argument <c>c</c> is a null reference.
2556 public ArrayList(ICollection c)
2562 throw new ArgumentNullException("c");
2567 if (array != null && array.Rank != 1)
2569 throw new RankException();
2572 _items = new object[c.Count];
2578 /// Initializes a new instance of the <see cref="ArrayList"/> class that is empty and
2579 /// has the specified initial capacity.
2581 /// <param name="capacity">
2582 /// The number of elements that hte new list is initially capable of storing.
2584 /// <exception cref="ArgumentOutOfRangeException">
2585 /// The <c>capacity</c> is less than zero.
2587 public ArrayList(int capacity)
2591 throw new ArgumentOutOfRangeException("capacity",
2592 capacity, "The initial capacity can't be smaller than zero.");
2597 capacity = DefaultInitialCapacity;
2599 _items = new object [capacity];
2603 /// Used by ArrayListAdapter to allow creation of an ArrayList with no storage buffer.
2605 private ArrayList(int initialCapacity, bool forceZeroSize)
2613 throw new InvalidOperationException("Use ArrayList(int)");
2618 /// Initializes a new array list that contains a copy of the given array and with the
2621 /// <param name="array"></param>
2622 private ArrayList(object[] array, int index, int count)
2626 _items = new object[DefaultInitialCapacity];
2630 _items = new object[count];
2633 Array.Copy(array, index, _items, 0, count);
2643 /// Gets/Sets an element in the list by index.
2645 /// <exception cref="ArgumentOutOfRangeException">
2646 /// The index is less than 0 or more then or equal to the list count.
2648 public virtual object this[int index]
2652 if (index < 0 || index >= _size)
2654 throw new ArgumentOutOfRangeException("index", index,
2655 "Index is less than 0 or more than or equal to the list count.");
2658 return _items[index];
2663 if (index < 0 || index >= _size)
2665 throw new ArgumentOutOfRangeException("index", index,
2666 "Index is less than 0 or more than or equal to the list count.");
2669 _items[index] = value;
2679 /// Gets the number of elements in the list.
2681 public virtual int Count
2690 /// Gets the number of elements the list can carry without needing to expand.
2693 /// ArrayLists automatically double their capacity when the capacity limit is broken.
2695 /// <exception cref="ArgumentOutOfRangeException">
2696 /// The capacity is less than the count.
2698 public virtual int Capacity
2702 return _items.Length;
2709 throw new ArgumentOutOfRangeException("Capacity", value,
2710 "Must be more than count.");
2715 newArray = new object[value];
2717 Array.Copy(_items, 0, newArray, 0, _size);
2724 /// <see cref="IList.IsFixedSize"/>
2727 public virtual bool IsFixedSize
2736 /// <see cref="IList.IsReadOnly"/>
2738 public virtual bool IsReadOnly
2747 /// <see cref="ICollection.IsSynchronized"/>
2749 public virtual bool IsSynchronized
2758 /// <see cref="ICollection.SyncRoot"/>
2760 public virtual object SyncRoot
2773 /// Ensures that the list has the capacity to contain the given <c>count</c> by
2774 /// automatically expanding the capacity when required.
2776 private void EnsureCapacity(int count)
2778 if (count <= _items.Length)
2786 newLength = _items.Length << 1;
2788 newLength = DefaultInitialCapacity;
2790 while (newLength < count)
2795 newData = new object[newLength];
2797 Array.Copy(_items, 0, newData, 0, _items.Length);
2803 /// Shifts a section of the list.
2805 /// <param name="index">
2806 /// The start of the section to shift (the element at index is included in the shift).
2808 /// <param name="count">
2809 /// The number of positions to shift by (can be negative).
2811 private void Shift(int index, int count)
2815 if (_size + count > _items.Length)
2820 newLength = (_items.Length > 0) ? _items.Length << 1 : 1;
2822 while (newLength < _size + count)
2827 newData = new object[newLength];
2829 Array.Copy(_items, 0, newData, 0, index);
2830 Array.Copy(_items, index, newData, index + count, _size - index);
2836 Array.Copy(_items, index, _items, index + count, _size - index);
2841 // Remember count is negative so this is actually index + (-count)
2843 int x = index - count ;
2845 Array.Copy(_items, x, _items, index, _size - x);
2849 public virtual int Add(object value)
2851 // Do a check here in case EnsureCapacity isn't inlined.
2853 if (_items.Length <= _size /* same as _items.Length < _size + 1) */)
2855 EnsureCapacity(_size + 1);
2858 _items[_size] = value;
2865 public virtual void Clear()
2867 // Keep the array but null all members so they can be garbage collected.
2869 Array.Clear(_items, 0, _size);
2875 public virtual bool Contains(object value)
2877 return IndexOf(value, 0, _size) > -1;
2880 internal virtual bool Contains(object value, int startIndex, int count)
2882 return IndexOf(value, startIndex, count) > -1;
2885 public virtual int IndexOf(object value)
2887 return IndexOf(value, 0);
2890 public virtual int IndexOf(object value, int startIndex)
2892 return IndexOf(value, startIndex, _size - startIndex);
2895 public virtual int IndexOf(object value, int startIndex, int count)
2897 if (startIndex < 0 || startIndex > _size)
2899 throw new ArgumentOutOfRangeException("startIndex", startIndex,
2900 "Does not specify valid index.");
2905 throw new ArgumentOutOfRangeException("count", count,
2906 "Can't be less than 0.");
2909 // re-ordered to avoid integer overflow
2910 if (startIndex > _size - count)
2912 // LAMESPEC: Every other method throws ArgumentException
2914 throw new ArgumentOutOfRangeException("count",
2915 "Start index and count do not specify a valid range.");
2918 return Array.IndexOf(_items, value, startIndex, count);
2921 public virtual int LastIndexOf(object value)
2923 return LastIndexOf(value, _size - 1);
2926 public virtual int LastIndexOf(object value, int startIndex)
2928 return LastIndexOf(value, startIndex, startIndex + 1);
2931 public virtual int LastIndexOf (object value, int startIndex, int count)
2933 // Array will catch the exceptions
2934 return Array.LastIndexOf (_items, value, startIndex, count);
2937 public virtual void Insert(int index, object value)
2939 if (index < 0 || index > _size)
2941 throw new ArgumentOutOfRangeException("index", index,
2942 "Index must be >= 0 and <= Count.");
2947 _items[index] = value;
2952 public virtual void InsertRange(int index, ICollection c)
2958 throw new ArgumentNullException("c");
2961 if (index < 0 || index > _size)
2963 throw new ArgumentOutOfRangeException("index", index,
2964 "Index must be >= 0 and <= Count.");
2969 // Do a check here in case EnsureCapacity isn't inlined.
2971 if (_items.Length < _size + i)
2973 EnsureCapacity(_size + i);
2978 Array.Copy(_items, index, _items, index + i, _size - index);
2981 // Handle inserting a range from a list to itself specially.
2983 if (this == c.SyncRoot)
2985 // Copy range before the insert point.
2987 Array.Copy(_items, 0, _items, index, index);
2989 // Copy range after the insert point.
2991 Array.Copy(_items, index + i, _items, index << 1, _size - index);
2995 c.CopyTo(_items, index);
3002 public virtual void Remove(object value)
3016 public virtual void RemoveAt(int index)
3018 if (index < 0 || index >= _size)
3020 throw new ArgumentOutOfRangeException("index", index,
3021 "Less than 0 or more than list count.");
3029 public virtual void RemoveRange(int index, int count)
3031 ArrayList.CheckRange(index, count, _size);
3033 Shift(index, -count);
3038 public virtual void Reverse()
3040 Array.Reverse(_items, 0, _size);
3044 public virtual void Reverse(int index, int count)
3046 ArrayList.CheckRange(index, count, _size);
3048 Array.Reverse(_items, index, count);
3052 public virtual void CopyTo(System.Array array)
3054 Array.Copy(_items, array, _size);
3057 public virtual void CopyTo(System.Array array, int index)
3059 CopyTo(0, array, index, _size);
3062 public virtual void CopyTo(int index, System.Array array, int arrayIndex, int count)
3066 throw new ArgumentNullException("array");
3069 if (array.Rank != 1)
3072 // This should be a RankException because Array.Copy throws RankException.
3074 throw new ArgumentException("Must have only 1 dimensions.", "array");
3077 Array.Copy(_items, index, array, arrayIndex, count);
3080 public virtual IEnumerator GetEnumerator()
3082 return new SimpleEnumerator(this);
3085 public virtual IEnumerator GetEnumerator(int index, int count)
3087 ArrayList.CheckRange(index, count, _size);
3089 return new ArrayListEnumerator(this, index, count);
3092 public virtual void AddRange(ICollection c)
3094 InsertRange(_size, c);
3097 public virtual int BinarySearch(object value)
3101 return Array.BinarySearch(_items, 0, _size, value);
3103 catch (InvalidOperationException e)
3105 throw new ArgumentException(e.Message);
3109 public virtual int BinarySearch(object value, IComparer comparer)
3113 return Array.BinarySearch(_items, 0, _size, value, comparer);
3115 catch (InvalidOperationException e)
3117 throw new ArgumentException(e.Message);
3121 public virtual int BinarySearch(int index, int count, object value, IComparer comparer)
3125 return Array.BinarySearch(_items, index, count, value, comparer);
3127 catch (InvalidOperationException e)
3129 throw new ArgumentException(e.Message);
3132 public virtual ArrayList GetRange(int index, int count)
3134 ArrayList.CheckRange(index, count, _size);
3136 if (this.IsSynchronized)
3138 return ArrayList.Synchronized(new RangedArrayList(this, index, count));
3142 return new RangedArrayList(this, index, count);
3146 public virtual void SetRange(int index, ICollection c)
3150 throw new ArgumentNullException("c");
3153 if (index < 0 || index + c.Count > _size)
3155 throw new ArgumentOutOfRangeException("index");
3158 c.CopyTo(_items, index);
3163 public virtual void TrimToSize()
3165 if (_items.Length > _size)
3171 newArray = new object[DefaultInitialCapacity];
3175 newArray = new object[_size];
3178 Array.Copy(_items, 0, newArray, 0, _size);
3184 public virtual void Sort()
3186 Array.Sort(_items, 0, _size);
3191 public virtual void Sort(IComparer comparer)
3193 Array.Sort(_items, 0, _size, comparer);
3196 public virtual void Sort(int index, int count, IComparer comparer)
3198 ArrayList.CheckRange(index, count, _size);
3200 Array.Sort(_items, index, count, comparer);
3203 public virtual object[] ToArray()
3207 retval = new object[_size];
3214 public virtual Array ToArray(Type elementType)
3218 retval = Array.CreateInstance(elementType, _size);
3225 public virtual object Clone()
3227 return new ArrayList(this._items, 0, this._size);
3232 #region Static Methods
3235 /// Does a check of the arguments many of the methods in ArrayList use.
3238 /// The choice of exceptions thrown sometimes seem to be arbitrarily chosen so
3239 /// not all methods actually make use of CheckRange.
3241 internal static void CheckRange(int index, int count, int listCount)
3245 throw new ArgumentOutOfRangeException("index", index, "Can't be less than 0.");
3250 throw new ArgumentOutOfRangeException("count", count, "Can't be less than 0.");
3253 // re-ordered to avoid possible integer overflow
3254 if (index > listCount - count)
3256 throw new ArgumentException("Index and count do not denote a valid range of elements.", "index");
3260 public static ArrayList Adapter(IList list)
3262 // LAMESPEC: EWWW. Other lists aren't *Array*Lists.
3266 throw new ArgumentNullException("list");
3269 if (list.IsSynchronized)
3271 return ArrayList.Synchronized(new ArrayListAdapter(list));
3275 return new ArrayListAdapter(list);
3279 public static ArrayList Synchronized(ArrayList arrayList)
3281 if (arrayList == null)
3283 throw new ArgumentNullException("arrayList");
3286 if (arrayList.IsSynchronized)
3291 return new SynchronizedArrayListWrapper(arrayList);
3294 public static IList Synchronized(IList list)
3298 throw new ArgumentNullException("list");
3301 if (list.IsSynchronized)
3306 return new SynchronizedListWrapper(list);
3309 public static ArrayList ReadOnly(ArrayList arrayList)
3311 if (arrayList == null)
3313 throw new ArgumentNullException("arrayList");
3316 if (arrayList.IsReadOnly)
3321 return new ReadOnlyArrayListWrapper(arrayList);
3324 public static IList ReadOnly(IList list)
3328 throw new ArgumentNullException("list");
3331 if (list.IsReadOnly)
3336 return new ReadOnlyListWrapper(list);
3339 public static ArrayList FixedSize(ArrayList arrayList)
3341 if (arrayList == null)
3343 throw new ArgumentNullException("arrayList");
3346 if (arrayList.IsFixedSize)
3351 return new FixedSizeArrayListWrapper(arrayList);
3354 public static IList FixedSize(IList list)
3358 throw new ArgumentNullException("list");
3361 if (list.IsFixedSize)
3366 return new FixedSizeListWrapper(list);
3369 public static ArrayList Repeat(object value, int count)
3371 ArrayList arrayList = new ArrayList(count);
3373 for (int i = 0; i < count; i++)
3375 arrayList.Add(value);