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;
53 namespace System.Collections
56 public class ArrayList
57 : IList, ICloneable, ICollection, IEnumerable
61 private sealed class ArrayListEnumerator
62 : IEnumerator, ICloneable
67 private object m_Current;
68 private ArrayList m_List;
69 private int m_ExpectedStateChanges;
71 public ArrayListEnumerator(ArrayList list)
72 : this(list, 0, list.Count)
78 return this.MemberwiseClone();
81 public ArrayListEnumerator(ArrayList list, int index, int count)
88 m_ExpectedStateChanges = list._version;
95 if (m_Pos == m_Index - 1) {
96 throw new InvalidOperationException("Enumerator unusable (Reset pending, or past end of array.");
103 public bool MoveNext()
105 if (m_List._version != m_ExpectedStateChanges)
107 throw new InvalidOperationException("List has changed.");
112 if (m_Pos - m_Index < m_Count)
114 m_Current = m_List[m_Pos];
129 sealed class SimpleEnumerator : IEnumerator, ICloneable
134 object currentElement;
135 static object endFlag = new object ();
137 public SimpleEnumerator (ArrayList list)
141 version = list._version;
142 currentElement = endFlag;
145 public object Clone ()
147 return MemberwiseClone ();
150 public bool MoveNext ()
152 if (version != list._version)
153 throw new InvalidOperationException("List has changed.");
155 if (++index < list.Count) {
156 currentElement = list [index];
159 currentElement = endFlag;
164 public object Current {
166 if (currentElement == endFlag) {
168 throw new InvalidOperationException ("Enumerator not started");
170 throw new InvalidOperationException ("Enumerator ended");
173 return currentElement;
179 if (version != list._version)
180 throw new InvalidOperationException ("List has changed.");
182 currentElement = endFlag;
189 #region ArrayListAdapter
192 /// Adapts various ILists into an ArrayList.
195 private sealed class ArrayListAdapter
198 private sealed class EnumeratorWithRange
199 : IEnumerator, ICloneable
201 private int m_StartIndex;
203 private int m_MaxCount;
204 private IEnumerator m_Enumerator;
206 public EnumeratorWithRange(IEnumerator enumerator, int index, int count)
209 m_StartIndex = index;
211 m_Enumerator = enumerator;
216 public object Clone()
218 return this.MemberwiseClone();
221 public object Current
225 return m_Enumerator.Current;
229 public bool MoveNext()
231 if (m_Count >= m_MaxCount)
238 return m_Enumerator.MoveNext();
244 m_Enumerator.Reset();
246 for (int i = 0; i < m_StartIndex; i++)
248 m_Enumerator.MoveNext();
253 private IList m_Adaptee;
255 public ArrayListAdapter(IList adaptee)
261 public override object this[int index]
265 return m_Adaptee[index];
270 m_Adaptee[index] = value;
274 public override int Count
278 return m_Adaptee.Count;
282 public override int Capacity
286 return m_Adaptee.Count;
291 if (value < m_Adaptee.Count)
293 throw new ArgumentException("capacity");
298 public override bool IsFixedSize
302 return m_Adaptee.IsFixedSize;
306 public override bool IsReadOnly
310 return m_Adaptee.IsReadOnly;
314 public override object SyncRoot
318 return m_Adaptee.SyncRoot;
322 public override int Add(object value)
324 return m_Adaptee.Add(value);
327 public override void Clear()
332 public override bool Contains(object value)
334 return m_Adaptee.Contains(value);
337 public override int IndexOf(object value)
339 return m_Adaptee.IndexOf(value);
342 public override int IndexOf(object value, int startIndex)
344 return IndexOf(value, startIndex, m_Adaptee.Count - startIndex);
347 public override int IndexOf(object value, int startIndex, int count)
349 if (startIndex < 0 || startIndex > m_Adaptee.Count)
351 throw new ArgumentOutOfRangeException("startIndex", startIndex,
352 "Does not specify valid index.");
357 throw new ArgumentOutOfRangeException("count", count,
358 "Can't be less than 0.");
361 // re-ordered to avoid possible integer overflow
362 if (startIndex > m_Adaptee.Count - count) {
363 // LAMESPEC: Every other method throws ArgumentException
364 throw new ArgumentOutOfRangeException("count",
365 "Start index and count do not specify a valid range.");
370 for (int i = startIndex; i < startIndex + count; i++)
372 if (m_Adaptee[i] == null)
380 for (int i = startIndex; i < startIndex + count; i++)
382 if (value.Equals(m_Adaptee[i]))
392 public override int LastIndexOf(object value)
394 return LastIndexOf(value, m_Adaptee.Count - 1);
397 public override int LastIndexOf(object value, int startIndex)
399 return LastIndexOf(value, startIndex, startIndex + 1);
402 public override int LastIndexOf(object value, int startIndex, int count)
406 throw new ArgumentOutOfRangeException("startIndex", startIndex, "< 0");
411 throw new ArgumentOutOfRangeException("count", count, "count is negative.");
414 if (startIndex - count + 1 < 0)
416 throw new ArgumentOutOfRangeException("count", count, "count is too large.");
421 for (int i = startIndex; i > startIndex - count; i--)
423 if (m_Adaptee[i] == null)
431 for (int i = startIndex; i > startIndex - count; i--)
433 if (value.Equals(m_Adaptee[i]))
443 public override void Insert(int index, object value)
445 m_Adaptee.Insert(index, value);
448 public override void InsertRange(int index, ICollection c)
452 throw new ArgumentNullException("c");
455 if (index > m_Adaptee.Count)
457 throw new ArgumentOutOfRangeException("index", index,
458 "Index must be >= 0 and <= Count.");
461 foreach (object value in c)
463 m_Adaptee.Insert(index++, value);
467 public override void Remove(object value)
469 m_Adaptee.Remove(value);
472 public override void RemoveAt(int index)
474 m_Adaptee.RemoveAt(index);
477 public override void RemoveRange(int index, int count)
479 CheckRange(index, count, m_Adaptee.Count);
481 for (int i = 0; i < count; i++)
483 m_Adaptee.RemoveAt(index);
487 public override void Reverse()
489 Reverse(0, m_Adaptee.Count);
492 public override void Reverse(int index, int count)
496 CheckRange(index, count, m_Adaptee.Count);
498 for (int i = 0; i < count / 2; i++)
500 tmp = m_Adaptee[i + index];
501 m_Adaptee[i + index] = m_Adaptee[(index + count) - i + index - 1];
502 m_Adaptee[(index + count) - i + index - 1] = tmp;
506 public override void SetRange(int index, ICollection c)
510 throw new ArgumentNullException("c");
513 if (index < 0 || index + c.Count > m_Adaptee.Count)
515 throw new ArgumentOutOfRangeException("index");
520 foreach (object value in c)
522 m_Adaptee[x++] = value;
526 public override void CopyTo(System.Array array)
528 m_Adaptee.CopyTo(array, 0);
531 public override void CopyTo(System.Array array, int index)
533 m_Adaptee.CopyTo(array, index);
536 public override void CopyTo(int index, System.Array array, int arrayIndex, int count)
540 throw new ArgumentOutOfRangeException("index", index,
541 "Can't be less than zero.");
546 throw new ArgumentOutOfRangeException("arrayIndex", arrayIndex,
547 "Can't be less than zero.");
552 throw new ArgumentOutOfRangeException("index", index,
553 "Can't be less than zero.");
556 if (index >= m_Adaptee.Count)
558 throw new ArgumentException("Can't be more or equal to list count.",
564 throw new ArgumentException("Can't copy into multi-dimensional array.");
567 if (arrayIndex >= array.Length)
569 throw new ArgumentException("arrayIndex can't be greater than array.Length - 1.");
572 if (array.Length - arrayIndex + 1 < count)
574 throw new ArgumentException("Destination array is too small.");
577 // re-ordered to avoid possible integer overflow
578 if (index > m_Adaptee.Count - count) {
579 throw new ArgumentException("Index and count do not denote a valid range of elements.", "index");
582 for (int i = 0; i < count; i++)
584 array.SetValue(m_Adaptee[index + i], arrayIndex + i);
588 public override bool IsSynchronized
592 return m_Adaptee.IsSynchronized;
596 public override IEnumerator GetEnumerator()
598 return m_Adaptee.GetEnumerator();
601 public override IEnumerator GetEnumerator(int index, int count)
603 CheckRange(index, count, m_Adaptee.Count);
605 return new EnumeratorWithRange(m_Adaptee.GetEnumerator(), index, count);
608 public override void AddRange(ICollection c)
610 foreach (object value in c)
612 m_Adaptee.Add(value);
616 public override int BinarySearch(object value)
618 return BinarySearch(value, null);
621 public override int BinarySearch(object value, IComparer comparer)
623 return BinarySearch(0, m_Adaptee.Count, value, comparer);
626 public override int BinarySearch(int index, int count, object value, IComparer comparer)
630 // Doing a direct BinarySearch on the adaptee will perform poorly if the adaptee is a linked-list.
631 // Alternatives include copying the adaptee to a temporary array first.
633 CheckRange(index, count, m_Adaptee.Count);
635 if (comparer == null)
637 comparer = Comparer.Default;
641 y = index + count - 1;
647 r = comparer.Compare(value, m_Adaptee[z]);
666 public override object Clone()
668 return new ArrayList.ArrayListAdapter(m_Adaptee);
671 public override ArrayList GetRange(int index, int count)
673 CheckRange(index, count, m_Adaptee.Count);
675 return new RangedArrayList(this, index, count);
678 public override void TrimToSize()
683 public override void Sort()
685 Sort(Comparer.Default);
688 public override void Sort(IComparer comparer)
690 Sort(0, m_Adaptee.Count, comparer);
693 public override void Sort(int index, int count, IComparer comparer)
695 CheckRange(index, count, m_Adaptee.Count);
697 if (comparer == null)
699 comparer = Comparer.Default;
702 // Doing a direct sort on the adaptee will perform poorly if the adaptee is a linked-list.
703 // Alternatives include copying the adaptee into a temporary array first.
705 QuickSort(m_Adaptee, index, index + count - 1, comparer);
710 /// Swaps two items in a list at the specified indexes.
712 private static void Swap(IList list, int x, int y)
722 /// Quicksort for lists.
725 /// This function acts as both qsort() and partition().
727 internal static void QuickSort(IList list, int left, int right, IComparer comparer)
737 // Pick the pivot using the median-of-three strategy.
739 middle = (left + right) / 2;
741 if (comparer.Compare(list[middle], list[left]) < 0)
743 Swap(list, middle, left);
746 if (comparer.Compare(list[right], list[left]) < 0)
748 Swap(list, right, left);
751 if (comparer.Compare(list[right], list[middle]) < 0)
753 Swap(list, right, middle);
756 if (right - left + 1 <= 3)
761 // Put the pivot in right - 1.
762 Swap(list, right - 1, middle);
764 // List should look like:
766 // [Small] ..Numbers.. [Middle] ..Numbers.. [Pivot][Large]
768 pivot = list[right - 1];
770 // Sort from (left + 1) to (right - 2).
777 while (comparer.Compare(list[++i], pivot) < 0);
778 while (comparer.Compare(list[--j], pivot) > 0);
790 // Put pivot into the right position (real middle).
792 Swap(list, right - 1, i);
794 // Recursively sort the left and right sub lists.
796 QuickSort(list, left, i - 1, comparer);
797 QuickSort(list, i + 1, right, comparer);
800 public override object[] ToArray()
804 retval = new object[m_Adaptee.Count];
806 m_Adaptee.CopyTo(retval, 0);
811 public override Array ToArray(Type elementType)
815 retval = Array.CreateInstance(elementType, m_Adaptee.Count);
817 m_Adaptee.CopyTo(retval, 0);
823 #endregion // ArrayListAdapter
826 // ArrayList wrappers
829 #region ArrayListWrapper
832 /// Base wrapper/decorator for ArrayLists. Simply delegates all methods to
833 /// the underlying wrappee.
836 private class ArrayListWrapper
839 protected ArrayList m_InnerArrayList;
843 public ArrayListWrapper(ArrayList innerArrayList)
845 m_InnerArrayList = innerArrayList;
852 public override object this[int index]
856 return m_InnerArrayList[index];
861 m_InnerArrayList[index] = value;
869 public override int Count
873 return m_InnerArrayList.Count;
877 public override int Capacity
881 return m_InnerArrayList.Capacity;
886 m_InnerArrayList.Capacity = value;
890 public override bool IsFixedSize
894 return m_InnerArrayList.IsFixedSize;
898 public override bool IsReadOnly
902 return m_InnerArrayList.IsReadOnly;
906 public override bool IsSynchronized
910 return m_InnerArrayList.IsSynchronized;
914 public override object SyncRoot
918 return m_InnerArrayList.SyncRoot;
926 public override int Add(object value)
928 return m_InnerArrayList.Add(value);
931 public override void Clear()
933 m_InnerArrayList.Clear();
936 public override bool Contains(object value)
938 return m_InnerArrayList.Contains(value);
941 public override int IndexOf(object value)
943 return m_InnerArrayList.IndexOf(value);
946 public override int IndexOf(object value, int startIndex)
948 return m_InnerArrayList.IndexOf(value, startIndex);
951 public override int IndexOf(object value, int startIndex, int count)
953 return m_InnerArrayList.IndexOf(value, startIndex, count);
956 public override int LastIndexOf(object value)
958 return m_InnerArrayList.LastIndexOf(value);
961 public override int LastIndexOf(object value, int startIndex)
963 return m_InnerArrayList.LastIndexOf(value, startIndex);
966 public override int LastIndexOf(object value, int startIndex, int count)
968 return m_InnerArrayList.LastIndexOf(value, startIndex, count);
971 public override void Insert(int index, object value)
973 m_InnerArrayList.Insert(index, value);
976 public override void InsertRange(int index, ICollection c)
978 m_InnerArrayList.InsertRange(index, c);
981 public override void Remove(object value)
983 m_InnerArrayList.Remove(value);
986 public override void RemoveAt(int index)
988 m_InnerArrayList.RemoveAt(index);
991 public override void RemoveRange(int index, int count)
993 m_InnerArrayList.RemoveRange(index, count);
996 public override void Reverse()
998 m_InnerArrayList.Reverse();
1001 public override void Reverse(int index, int count)
1003 m_InnerArrayList.Reverse(index, count);
1006 public override void SetRange(int index, ICollection c)
1008 m_InnerArrayList.SetRange(index, c);
1011 public override void CopyTo(System.Array array)
1013 m_InnerArrayList.CopyTo(array);
1016 public override void CopyTo(System.Array array, int index)
1018 m_InnerArrayList.CopyTo(array, index);
1021 public override void CopyTo(int index, System.Array array, int arrayIndex, int count)
1023 m_InnerArrayList.CopyTo(index, array, arrayIndex, count);
1026 public override IEnumerator GetEnumerator()
1028 return m_InnerArrayList.GetEnumerator();
1031 public override IEnumerator GetEnumerator(int index, int count)
1033 return m_InnerArrayList.GetEnumerator(index, count);
1036 public override void AddRange(ICollection c)
1038 m_InnerArrayList.AddRange(c);
1041 public override int BinarySearch(object value)
1043 return m_InnerArrayList.BinarySearch(value);
1046 public override int BinarySearch(object value, IComparer comparer)
1048 return m_InnerArrayList.BinarySearch(value, comparer);
1051 public override int BinarySearch(int index, int count, object value, IComparer comparer)
1053 return m_InnerArrayList.BinarySearch(index, count, value, comparer);
1056 public override object Clone()
1058 return m_InnerArrayList.Clone();
1061 public override ArrayList GetRange(int index, int count)
1063 return m_InnerArrayList.GetRange(index, count);
1066 public override void TrimToSize()
1068 m_InnerArrayList.TrimToSize();
1071 public override void Sort()
1073 m_InnerArrayList.Sort();
1076 public override void Sort(IComparer comparer)
1078 m_InnerArrayList.Sort(comparer);
1081 public override void Sort(int index, int count, IComparer comparer)
1083 m_InnerArrayList.Sort(index, count, comparer);
1086 public override object[] ToArray()
1088 return m_InnerArrayList.ToArray();
1091 public override Array ToArray(Type elementType)
1093 return m_InnerArrayList.ToArray(elementType);
1101 #region SynchronizedArrayListWrapper
1104 /// ArrayListWrapper that synchronizes calls to all methods/properties.
1107 /// Works by just synchronizing all method calls. In the future careful optimisation
1108 /// could give better performance...
1111 private sealed class SynchronizedArrayListWrapper
1114 private object m_SyncRoot;
1116 #region Constructors
1119 /// Creates a new synchronized wrapper for the given <see cref="ArrayList"/>.
1121 /// <param name="innerArrayList"></param>
1122 internal SynchronizedArrayListWrapper(ArrayList innerArrayList)
1123 : base(innerArrayList)
1125 m_SyncRoot = innerArrayList.SyncRoot;
1132 public override object this[int index]
1138 return m_InnerArrayList[index];
1146 m_InnerArrayList[index] = value;
1155 // Some of these properties may be calculated so it's best to synchronize
1156 // them even though it might cause a performance hit.
1157 // Better safe than sorry ;D.
1159 public override int Count
1165 return m_InnerArrayList.Count;
1170 public override int Capacity
1176 return m_InnerArrayList.Capacity;
1184 m_InnerArrayList.Capacity = value;
1189 public override bool IsFixedSize
1195 return m_InnerArrayList.IsFixedSize;
1200 public override bool IsReadOnly
1206 return m_InnerArrayList.IsReadOnly;
1211 public override bool IsSynchronized
1219 public override object SyncRoot
1231 public override int Add(object value)
1235 return m_InnerArrayList.Add(value);
1239 public override void Clear()
1243 m_InnerArrayList.Clear();
1247 public override bool Contains(object value)
1251 return m_InnerArrayList.Contains(value);
1255 public override int IndexOf(object value)
1259 return m_InnerArrayList.IndexOf(value);
1263 public override int IndexOf(object value, int startIndex)
1267 return m_InnerArrayList.IndexOf(value, startIndex);
1271 public override int IndexOf(object value, int startIndex, int count)
1275 return m_InnerArrayList.IndexOf(value, startIndex, count);
1279 public override int LastIndexOf(object value)
1283 return m_InnerArrayList.LastIndexOf(value);
1287 public override int LastIndexOf(object value, int startIndex)
1291 return m_InnerArrayList.LastIndexOf(value, startIndex);
1295 public override int LastIndexOf(object value, int startIndex, int count)
1299 return m_InnerArrayList.LastIndexOf(value, startIndex, count);
1303 public override void Insert(int index, object value)
1307 m_InnerArrayList.Insert(index, value);
1311 public override void InsertRange(int index, ICollection c)
1315 m_InnerArrayList.InsertRange(index, c);
1319 public override void Remove(object value)
1323 m_InnerArrayList.Remove(value);
1327 public override void RemoveAt(int index)
1331 m_InnerArrayList.RemoveAt(index);
1335 public override void RemoveRange(int index, int count)
1339 m_InnerArrayList.RemoveRange(index, count);
1343 public override void Reverse()
1347 m_InnerArrayList.Reverse();
1351 public override void Reverse(int index, int count)
1355 m_InnerArrayList.Reverse(index, count);
1359 public override void CopyTo(System.Array array)
1363 m_InnerArrayList.CopyTo(array);
1367 public override void CopyTo(System.Array array, int index)
1371 m_InnerArrayList.CopyTo(array, index);
1375 public override void CopyTo(int index, System.Array array, int arrayIndex, int count)
1379 m_InnerArrayList.CopyTo(index, array, arrayIndex, count);
1383 public override IEnumerator GetEnumerator()
1387 return m_InnerArrayList.GetEnumerator();
1391 public override IEnumerator GetEnumerator(int index, int count)
1395 return m_InnerArrayList.GetEnumerator(index, count);
1399 public override void AddRange(ICollection c)
1403 m_InnerArrayList.AddRange(c);
1407 public override int BinarySearch(object value)
1411 return m_InnerArrayList.BinarySearch(value);
1415 public override int BinarySearch(object value, IComparer comparer)
1419 return m_InnerArrayList.BinarySearch(value, comparer);
1423 public override int BinarySearch(int index, int count, object value, IComparer comparer)
1427 return m_InnerArrayList.BinarySearch(index, count, value, comparer);
1431 public override object Clone()
1435 return m_InnerArrayList.Clone();
1439 public override ArrayList GetRange(int index, int count)
1443 return m_InnerArrayList.GetRange(index, count);
1447 public override void TrimToSize()
1451 m_InnerArrayList.TrimToSize();
1455 public override void Sort()
1459 m_InnerArrayList.Sort();
1463 public override void Sort(IComparer comparer)
1467 m_InnerArrayList.Sort(comparer);
1471 public override void Sort(int index, int count, IComparer comparer)
1475 m_InnerArrayList.Sort(index, count, comparer);
1479 public override object[] ToArray()
1483 return m_InnerArrayList.ToArray();
1487 public override Array ToArray(Type elementType)
1491 return m_InnerArrayList.ToArray(elementType);
1500 #region FixedSizeArrayListWrapper
1503 private class FixedSizeArrayListWrapper
1506 #region Constructors
1508 public FixedSizeArrayListWrapper(ArrayList innerList)
1518 /// Gets the error message to display when an readonly/fixedsize related exception is
1521 protected virtual string ErrorMessage
1525 return "Can't add or remove from a fixed-size list.";
1529 public override int Capacity
1533 return base.Capacity;
1538 throw new NotSupportedException(this.ErrorMessage);
1542 public override bool IsFixedSize
1554 public override int Add(object value)
1556 throw new NotSupportedException(this.ErrorMessage);
1559 public override void AddRange(ICollection c)
1561 throw new NotSupportedException(this.ErrorMessage);
1564 public override void Clear()
1566 throw new NotSupportedException(this.ErrorMessage);
1569 public override void Insert(int index, object value)
1571 throw new NotSupportedException(this.ErrorMessage);
1574 public override void InsertRange(int index, ICollection c)
1576 throw new NotSupportedException(this.ErrorMessage);
1579 public override void Remove(object value)
1581 throw new NotSupportedException(this.ErrorMessage);
1584 public override void RemoveAt(int index)
1586 throw new NotSupportedException(this.ErrorMessage);
1589 public override void RemoveRange(int index, int count)
1591 throw new NotSupportedException(this.ErrorMessage);
1594 public override void TrimToSize()
1596 throw new NotSupportedException(this.ErrorMessage);
1604 #region ReadOnlyArrayListWrapper
1607 private sealed class ReadOnlyArrayListWrapper
1608 : FixedSizeArrayListWrapper
1610 protected override string ErrorMessage
1614 return "Can't modify a readonly list.";
1618 public override bool IsReadOnly
1626 public ReadOnlyArrayListWrapper(ArrayList innerArrayList)
1627 : base(innerArrayList)
1631 public override object this[int index]
1635 return m_InnerArrayList[index];
1640 throw new NotSupportedException(this.ErrorMessage);
1644 public override void Reverse()
1646 throw new NotSupportedException(this.ErrorMessage);
1649 public override void Reverse(int index, int count)
1651 throw new NotSupportedException(this.ErrorMessage);
1654 public override void SetRange(int index, ICollection c)
1656 throw new NotSupportedException(this.ErrorMessage);
1659 public override void Sort()
1661 throw new NotSupportedException(this.ErrorMessage);
1664 public override void Sort(IComparer comparer)
1666 throw new NotSupportedException(this.ErrorMessage);
1669 public override void Sort(int index, int count, IComparer comparer)
1671 throw new NotSupportedException(this.ErrorMessage);
1677 #region RangedArrayList
1680 private sealed class RangedArrayList
1683 private int m_InnerIndex;
1684 private int m_InnerCount;
1685 private int m_InnerStateChanges;
1687 public RangedArrayList(ArrayList innerList, int index, int count)
1690 m_InnerIndex = index;
1691 m_InnerCount = count;
1692 m_InnerStateChanges = innerList._version;
1697 public override bool IsSynchronized
1705 public override object this[int index]
1709 if (index < 0 || index > m_InnerCount)
1711 throw new ArgumentOutOfRangeException("index");
1714 return m_InnerArrayList[m_InnerIndex + index];
1719 if (index < 0 || index > m_InnerCount)
1721 throw new ArgumentOutOfRangeException("index");
1724 m_InnerArrayList[m_InnerIndex + index] = value;
1732 public override int Count
1736 VerifyStateChanges();
1738 return m_InnerCount;
1742 public override int Capacity
1746 return m_InnerArrayList.Capacity;
1751 if (value < m_InnerCount)
1753 throw new ArgumentOutOfRangeException();
1762 private void VerifyStateChanges()
1764 if (m_InnerStateChanges != m_InnerArrayList._version)
1766 throw new InvalidOperationException
1767 ("ArrayList view is invalid because the underlying ArrayList was modified.");
1771 public override int Add(object value)
1773 VerifyStateChanges();
1775 m_InnerArrayList.Insert(m_InnerIndex + m_InnerCount, value);
1777 m_InnerStateChanges = m_InnerArrayList._version;
1779 return ++m_InnerCount;
1782 public override void Clear()
1784 VerifyStateChanges();
1786 m_InnerArrayList.RemoveRange(m_InnerIndex, m_InnerCount);
1789 m_InnerStateChanges = m_InnerArrayList._version;
1792 public override bool Contains(object value)
1794 return m_InnerArrayList.Contains(value, m_InnerIndex, m_InnerCount);
1797 public override int IndexOf(object value)
1799 return IndexOf(value, 0);
1802 public override int IndexOf(object value, int startIndex)
1804 return IndexOf(value, startIndex, m_InnerCount - startIndex);
1807 public override int IndexOf(object value, int startIndex, int count)
1809 if (startIndex < 0 || startIndex > m_InnerCount)
1811 throw new ArgumentOutOfRangeException("startIndex", startIndex,
1812 "Does not specify valid index.");
1817 throw new ArgumentOutOfRangeException("count", count,
1818 "Can't be less than 0.");
1821 // re-ordered to avoid possible integer overflow
1822 if (startIndex > m_InnerCount - count)
1824 // LAMESPEC: Every other method throws ArgumentException
1826 throw new ArgumentOutOfRangeException("count",
1827 "Start index and count do not specify a valid range.");
1830 int retval = m_InnerArrayList.IndexOf(value, m_InnerIndex + startIndex, count);
1838 return retval - m_InnerIndex;
1842 public override int LastIndexOf(object value)
1844 return LastIndexOf(value, m_InnerCount - 1);
1847 public override int LastIndexOf(object value, int startIndex)
1849 return LastIndexOf(value, startIndex, startIndex + 1);
1852 public override int LastIndexOf(object value, int startIndex, int count)
1856 throw new ArgumentOutOfRangeException("startIndex", startIndex, "< 0");
1861 throw new ArgumentOutOfRangeException("count", count, "count is negative.");
1864 int retval = m_InnerArrayList.LastIndexOf(value, m_InnerIndex + startIndex, count);
1872 return retval - m_InnerIndex;
1876 public override void Insert(int index, object value)
1878 VerifyStateChanges();
1880 if (index < 0 || index > m_InnerCount)
1882 throw new ArgumentOutOfRangeException("index", index,
1883 "Index must be >= 0 and <= Count.");
1886 m_InnerArrayList.Insert(m_InnerIndex + index, value);
1890 m_InnerStateChanges = m_InnerArrayList._version;
1893 public override void InsertRange(int index, ICollection c)
1895 VerifyStateChanges();
1897 if (index < 0 || index > m_InnerCount)
1899 throw new ArgumentOutOfRangeException("index", index,
1900 "Index must be >= 0 and <= Count.");
1903 m_InnerArrayList.InsertRange(m_InnerIndex + index, c);
1905 m_InnerCount += c.Count;
1907 m_InnerStateChanges = m_InnerArrayList._version;
1910 public override void Remove(object value)
1912 VerifyStateChanges();
1914 int x = IndexOf(value);
1921 m_InnerStateChanges = m_InnerArrayList._version;
1924 public override void RemoveAt(int index)
1926 VerifyStateChanges();
1928 if (index < 0 || index > m_InnerCount)
1930 throw new ArgumentOutOfRangeException("index", index,
1931 "Index must be >= 0 and <= Count.");
1934 m_InnerArrayList.RemoveAt(m_InnerIndex + index);
1937 m_InnerStateChanges = m_InnerArrayList._version;
1940 public override void RemoveRange(int index, int count)
1942 VerifyStateChanges();
1944 CheckRange(index, count, m_InnerCount);
1946 m_InnerArrayList.RemoveRange(m_InnerIndex + index, count);
1948 m_InnerCount -= count;
1950 m_InnerStateChanges = m_InnerArrayList._version;
1953 public override void Reverse()
1955 Reverse(0, m_InnerCount);
1958 public override void Reverse(int index, int count)
1960 VerifyStateChanges();
1962 CheckRange(index, count, m_InnerCount);
1964 m_InnerArrayList.Reverse(m_InnerIndex + index, count);
1966 m_InnerStateChanges = m_InnerArrayList._version;
1969 public override void SetRange(int index, ICollection c)
1971 VerifyStateChanges();
1973 if (index < 0 || index > m_InnerCount)
1975 throw new ArgumentOutOfRangeException("index", index,
1976 "Index must be >= 0 and <= Count.");
1979 m_InnerArrayList.SetRange(m_InnerIndex + index, c);
1981 m_InnerStateChanges = m_InnerArrayList._version;
1984 public override void CopyTo(System.Array array)
1989 public override void CopyTo(System.Array array, int index)
1991 CopyTo(0, array, index, m_InnerCount);
1994 public override void CopyTo(int index, System.Array array, int arrayIndex, int count)
1996 CheckRange(index, count, m_InnerCount);
1998 m_InnerArrayList.CopyTo(m_InnerIndex + index, array, arrayIndex, count);
2001 public override IEnumerator GetEnumerator()
2003 return GetEnumerator(0, m_InnerCount);
2006 public override IEnumerator GetEnumerator(int index, int count)
2008 CheckRange(index, count, m_InnerCount);
2010 return m_InnerArrayList.GetEnumerator(m_InnerIndex + index, count);
2013 public override void AddRange(ICollection c)
2015 VerifyStateChanges();
2017 m_InnerArrayList.InsertRange(m_InnerCount, c);
2019 m_InnerCount += c.Count;
2021 m_InnerStateChanges = m_InnerArrayList._version;
2024 public override int BinarySearch(object value)
2026 return BinarySearch(0, m_InnerCount, value, Comparer.Default);
2029 public override int BinarySearch(object value, IComparer comparer)
2031 return BinarySearch(0, m_InnerCount, value, comparer);
2034 public override int BinarySearch(int index, int count, object value, IComparer comparer)
2036 CheckRange(index, count, m_InnerCount);
2038 return m_InnerArrayList.BinarySearch(m_InnerIndex + index, count, value, comparer);
2041 public override object Clone()
2043 return new RangedArrayList((ArrayList)m_InnerArrayList.Clone(), m_InnerIndex, m_InnerCount);
2046 public override ArrayList GetRange(int index, int count)
2048 CheckRange(index, count, m_InnerCount);
2050 return new RangedArrayList(this, index, count);
2053 public override void TrimToSize()
2055 throw new NotSupportedException();
2058 public override void Sort()
2060 Sort(Comparer.Default);
2063 public override void Sort(IComparer comparer)
2065 Sort(0, m_InnerCount, comparer);
2068 public override void Sort(int index, int count, IComparer comparer)
2070 VerifyStateChanges();
2072 CheckRange(index, count, m_InnerCount);
2074 m_InnerArrayList.Sort(m_InnerIndex + index, count, comparer);
2076 m_InnerStateChanges = m_InnerArrayList._version;
2079 public override object[] ToArray()
2083 array = new object[m_InnerCount];
2085 m_InnerArrayList.CopyTo(0, array, 0, m_InnerCount);
2090 public override Array ToArray(Type elementType)
2094 array = Array.CreateInstance(elementType, m_InnerCount);
2096 m_InnerArrayList.CopyTo(0, array, 0, m_InnerCount);
2110 #region SynchronizedListWrapper
2113 private sealed class SynchronizedListWrapper
2116 private object m_SyncRoot;
2118 public SynchronizedListWrapper(IList innerList)
2121 m_SyncRoot = innerList.SyncRoot;
2124 public override int Count
2130 return m_InnerList.Count;
2135 public override bool IsSynchronized
2143 public override object SyncRoot
2149 return m_InnerList.SyncRoot;
2154 public override bool IsFixedSize
2160 return m_InnerList.IsFixedSize;
2165 public override bool IsReadOnly
2171 return m_InnerList.IsReadOnly;
2176 public override object this[int index]
2182 return m_InnerList[index];
2190 m_InnerList[index] = value;
2195 public override int Add(object value)
2199 return m_InnerList.Add(value);
2203 public override void Clear()
2207 m_InnerList.Clear();
2211 public override bool Contains(object value)
2215 return m_InnerList.Contains(value);
2219 public override int IndexOf(object value)
2223 return m_InnerList.IndexOf(value);
2227 public override void Insert(int index, object value)
2231 m_InnerList.Insert(index, value);
2235 public override void Remove(object value)
2239 m_InnerList.Remove(value);
2243 public override void RemoveAt(int index)
2247 m_InnerList.RemoveAt(index);
2251 public override void CopyTo(Array array, int index)
2255 m_InnerList.CopyTo(array, index);
2259 public override IEnumerator GetEnumerator()
2263 return m_InnerList.GetEnumerator();
2270 #region FixedSizeListWrapper
2273 private class FixedSizeListWrapper
2276 protected virtual string ErrorMessage
2280 return "List is fixed-size.";
2284 public override bool IsFixedSize
2292 public FixedSizeListWrapper(IList innerList)
2297 public override int Add(object value)
2299 throw new NotSupportedException(this.ErrorMessage);
2302 public override void Clear()
2304 throw new NotSupportedException(this.ErrorMessage);
2307 public override void Insert(int index, object value)
2309 throw new NotSupportedException(this.ErrorMessage);
2312 public override void Remove(object value)
2314 throw new NotSupportedException(this.ErrorMessage);
2317 public override void RemoveAt(int index)
2319 throw new NotSupportedException(this.ErrorMessage);
2325 #region ReadOnlyListWrapper
2328 private sealed class ReadOnlyListWrapper
2329 : FixedSizeListWrapper
2331 protected override string ErrorMessage
2335 return "List is read-only.";
2339 public override bool IsReadOnly
2347 public ReadOnlyListWrapper(IList innerList)
2352 public override object this[int index]
2356 return m_InnerList[index];
2361 throw new NotSupportedException(this.ErrorMessage);
2371 /// Decorates/Wraps any <c>IList</c> implementing object.
2374 private class ListWrapper
2379 protected IList m_InnerList;
2383 #region Constructors
2385 public ListWrapper(IList innerList)
2387 m_InnerList = innerList;
2394 public virtual object this[int index]
2398 return m_InnerList[index];
2403 m_InnerList[index] = value;
2411 public virtual int Count
2415 return m_InnerList.Count;
2419 public virtual bool IsSynchronized
2423 return m_InnerList.IsSynchronized;
2427 public virtual object SyncRoot
2431 return m_InnerList.SyncRoot;
2435 public virtual bool IsFixedSize
2439 return m_InnerList.IsFixedSize;
2443 public virtual bool IsReadOnly
2447 return m_InnerList.IsReadOnly;
2455 public virtual int Add(object value)
2457 return m_InnerList.Add(value);
2460 public virtual void Clear()
2462 m_InnerList.Clear();
2465 public virtual bool Contains(object value)
2467 return m_InnerList.Contains(value);
2470 public virtual int IndexOf(object value)
2472 return m_InnerList.IndexOf(value);
2475 public virtual void Insert(int index, object value)
2477 m_InnerList.Insert(index, value);
2480 public virtual void Remove(object value)
2482 m_InnerList.Remove(value);
2485 public virtual void RemoveAt(int index)
2487 m_InnerList.RemoveAt(index);
2490 public virtual void CopyTo(Array array, int index)
2492 m_InnerList.CopyTo(array, index);
2495 public virtual IEnumerator GetEnumerator()
2497 return m_InnerList.GetEnumerator();
2506 // Start of ArrayList
2511 private const int DefaultInitialCapacity = 0x10;
2514 /// Number of items in the list.
2519 /// Array to store the items.
2521 private object[] _items;
2524 /// Total number of state changes.
2526 private int _version;
2530 #region Constructors
2533 /// Initializes a new instance of the <see cref="ArrayList"/> class that is empty and
2534 /// has the default initial capacity (16).
2538 _items = new object[DefaultInitialCapacity];
2542 /// Initializes a new instance of the <see cref="ArrayList"/> class that contains
2543 /// elements copied from the specified collection and that has the same initial capacity
2544 /// as the number of elements copied.
2546 /// <param name="c">
2547 /// The <see cref="ICollection"/> whose elements are copied into the new list.
2549 /// <exception cref="ArgumentNullException">
2550 /// The argument <c>c</c> is a null reference.
2552 public ArrayList(ICollection c)
2558 throw new ArgumentNullException("c");
2563 if (array != null && array.Rank != 1)
2565 throw new RankException();
2568 _items = new object[c.Count];
2574 /// Initializes a new instance of the <see cref="ArrayList"/> class that is empty and
2575 /// has the specified initial capacity.
2577 /// <param name="initialCapacity">
2578 /// The number of elements that hte new list is initially capable of storing.
2580 /// <exception cref="ArgumentOutOfRangeException">
2581 /// The <c>capacity</c> is less than zero.
2583 public ArrayList(int initialCapacity)
2585 if (initialCapacity < 0)
2587 throw new ArgumentOutOfRangeException("initialCapacity",
2588 initialCapacity, "The initial capacity can't be smaller than zero.");
2591 if (initialCapacity == 0)
2593 initialCapacity = DefaultInitialCapacity;
2595 _items = new object[initialCapacity];
2599 /// Used by ArrayListAdapter to allow creation of an ArrayList with no storage buffer.
2601 private ArrayList(int initialCapacity, bool forceZeroSize)
2609 throw new InvalidOperationException("Use ArrayList(int)");
2614 /// Initializes a new array list that contains a copy of the given array and with the
2617 /// <param name="array"></param>
2618 private ArrayList(object[] array, int index, int count)
2622 _items = new object[DefaultInitialCapacity];
2626 _items = new object[count];
2629 Array.Copy(array, index, _items, 0, count);
2639 /// Gets/Sets an element in the list by index.
2641 /// <exception cref="ArgumentOutOfRangeException">
2642 /// The index is less than 0 or more then or equal to the list count.
2644 public virtual object this[int index]
2648 if (index < 0 || index >= _size)
2650 throw new ArgumentOutOfRangeException("index", index,
2651 "Index is less than 0 or more than or equal to the list count.");
2654 return _items[index];
2659 if (index < 0 || index >= _size)
2661 throw new ArgumentOutOfRangeException("index", index,
2662 "Index is less than 0 or more than or equal to the list count.");
2665 _items[index] = value;
2675 /// Gets the number of elements in the list.
2677 public virtual int Count
2686 /// Gets the number of elements the list can carry without needing to expand.
2689 /// ArrayLists automatically double their capacity when the capacity limit is broken.
2691 /// <exception cref="ArgumentOutOfRangeException">
2692 /// The capacity is less than the count.
2694 public virtual int Capacity
2698 return _items.Length;
2705 throw new ArgumentOutOfRangeException("Capacity", value,
2706 "Must be more than count.");
2711 newArray = new object[value];
2713 Array.Copy(_items, 0, newArray, 0, _size);
2720 /// <see cref="IList.IsFixedSize"/>
2723 public virtual bool IsFixedSize
2732 /// <see cref="IList.IsReadOnly"/>
2734 public virtual bool IsReadOnly
2743 /// <see cref="ICollection.IsSynchronized"/>
2745 public virtual bool IsSynchronized
2754 /// <see cref="ICollection.SyncRoot"/>
2756 public virtual object SyncRoot
2769 /// Ensures that the list has the capacity to contain the given <c>count</c> by
2770 /// automatically expanding the capacity when required.
2772 private void EnsureCapacity(int count)
2774 if (count <= _items.Length)
2782 newLength = _items.Length << 1;
2784 newLength = DefaultInitialCapacity;
2786 while (newLength < count)
2791 newData = new object[newLength];
2793 Array.Copy(_items, 0, newData, 0, _items.Length);
2799 /// Shifts a section of the list.
2801 /// <param name="index">
2802 /// The start of the section to shift (the element at index is included in the shift).
2804 /// <param name="count">
2805 /// The number of positions to shift by (can be negative).
2807 private void Shift(int index, int count)
2811 if (_size + count > _items.Length)
2816 newLength = (_items.Length > 0) ? _items.Length << 1 : 1;
2818 while (newLength < _size + count)
2823 newData = new object[newLength];
2825 Array.Copy(_items, 0, newData, 0, index);
2826 Array.Copy(_items, index, newData, index + count, _size - index);
2832 Array.Copy(_items, index, _items, index + count, _size - index);
2837 // Remember count is negative so this is actually index + (-count)
2839 int x = index - count ;
2841 Array.Copy(_items, x, _items, index, _size - x);
2845 public virtual int Add(object value)
2847 // Do a check here in case EnsureCapacity isn't inlined.
2849 if (_items.Length <= _size /* same as _items.Length < _size + 1) */)
2851 EnsureCapacity(_size + 1);
2854 _items[_size] = value;
2861 public virtual void Clear()
2863 // Keep the array but null all members so they can be garbage collected.
2865 Array.Clear(_items, 0, _size);
2871 public virtual bool Contains(object value)
2873 return IndexOf(value, 0, _size) > -1;
2876 internal virtual bool Contains(object value, int startIndex, int count)
2878 return IndexOf(value, startIndex, count) > -1;
2881 public virtual int IndexOf(object value)
2883 return IndexOf(value, 0);
2886 public virtual int IndexOf(object value, int startIndex)
2888 return IndexOf(value, startIndex, _size - startIndex);
2891 public virtual int IndexOf(object value, int startIndex, int count)
2893 if (startIndex < 0 || startIndex > _size)
2895 throw new ArgumentOutOfRangeException("startIndex", startIndex,
2896 "Does not specify valid index.");
2901 throw new ArgumentOutOfRangeException("count", count,
2902 "Can't be less than 0.");
2905 // re-ordered to avoid integer overflow
2906 if (startIndex > _size - count)
2908 // LAMESPEC: Every other method throws ArgumentException
2910 throw new ArgumentOutOfRangeException("count",
2911 "Start index and count do not specify a valid range.");
2914 return Array.IndexOf(_items, value, startIndex, count);
2917 public virtual int LastIndexOf(object value)
2919 return LastIndexOf(value, _size - 1);
2922 public virtual int LastIndexOf(object value, int startIndex)
2924 return LastIndexOf(value, startIndex, startIndex + 1);
2927 public virtual int LastIndexOf (object value, int startIndex, int count)
2929 // Array will catch the exceptions
2930 return Array.LastIndexOf (_items, value, startIndex, count);
2933 public virtual void Insert(int index, object value)
2935 if (index < 0 || index > _size)
2937 throw new ArgumentOutOfRangeException("index", index,
2938 "Index must be >= 0 and <= Count.");
2943 _items[index] = value;
2948 public virtual void InsertRange(int index, ICollection c)
2954 throw new ArgumentNullException("c");
2957 if (index < 0 || index > _size)
2959 throw new ArgumentOutOfRangeException("index", index,
2960 "Index must be >= 0 and <= Count.");
2965 // Do a check here in case EnsureCapacity isn't inlined.
2967 if (_items.Length < _size + i)
2969 EnsureCapacity(_size + i);
2974 Array.Copy(_items, index, _items, index + i, _size - index);
2977 // Handle inserting a range from a list to itself specially.
2979 if (this == c.SyncRoot)
2981 // Copy range before the insert point.
2983 Array.Copy(_items, 0, _items, index, index);
2985 // Copy range after the insert point.
2987 Array.Copy(_items, index + i, _items, index << 1, _size - index);
2991 c.CopyTo(_items, index);
2998 public virtual void Remove(object value)
3012 public virtual void RemoveAt(int index)
3014 if (index < 0 || index >= _size)
3016 throw new ArgumentOutOfRangeException("index", index,
3017 "Less than 0 or more than list count.");
3025 public virtual void RemoveRange(int index, int count)
3027 ArrayList.CheckRange(index, count, _size);
3029 Shift(index, -count);
3034 public virtual void Reverse()
3036 Array.Reverse(_items, 0, _size);
3040 public virtual void Reverse(int index, int count)
3042 ArrayList.CheckRange(index, count, _size);
3044 Array.Reverse(_items, index, count);
3048 public virtual void CopyTo(System.Array array)
3050 Array.Copy(_items, array, _size);
3053 public virtual void CopyTo(System.Array array, int index)
3055 CopyTo(0, array, index, _size);
3058 public virtual void CopyTo(int index, System.Array array, int arrayIndex, int count)
3062 throw new ArgumentNullException("array");
3065 if (array.Rank != 1)
3068 // This should be a RankException because Array.Copy throws RankException.
3070 throw new ArgumentException("Must have only 1 dimensions.", "array");
3073 Array.Copy(_items, index, array, arrayIndex, count);
3076 public virtual IEnumerator GetEnumerator()
3078 return new SimpleEnumerator(this);
3081 public virtual IEnumerator GetEnumerator(int index, int count)
3083 ArrayList.CheckRange(index, count, _size);
3085 return new ArrayListEnumerator(this, index, count);
3088 public virtual void AddRange(ICollection c)
3090 InsertRange(_size, c);
3093 public virtual int BinarySearch(object value)
3097 return Array.BinarySearch(_items, 0, _size, value);
3099 catch (InvalidOperationException e)
3101 throw new ArgumentException(e.Message);
3105 public virtual int BinarySearch(object value, IComparer comparer)
3109 return Array.BinarySearch(_items, 0, _size, value, comparer);
3111 catch (InvalidOperationException e)
3113 throw new ArgumentException(e.Message);
3117 public virtual int BinarySearch(int index, int count, object value, IComparer comparer)
3121 return Array.BinarySearch(_items, index, count, value, comparer);
3123 catch (InvalidOperationException e)
3125 throw new ArgumentException(e.Message);
3128 public virtual ArrayList GetRange(int index, int count)
3130 ArrayList.CheckRange(index, count, _size);
3132 if (this.IsSynchronized)
3134 return ArrayList.Synchronized(new RangedArrayList(this, index, count));
3138 return new RangedArrayList(this, index, count);
3142 public virtual void SetRange(int index, ICollection c)
3146 throw new ArgumentNullException("c");
3149 if (index < 0 || index + c.Count > _size)
3151 throw new ArgumentOutOfRangeException("index");
3154 c.CopyTo(_items, index);
3159 public virtual void TrimToSize()
3161 if (_items.Length > _size)
3167 newArray = new object[DefaultInitialCapacity];
3171 newArray = new object[_size];
3174 Array.Copy(_items, 0, newArray, 0, _size);
3180 public virtual void Sort()
3182 Array.Sort(_items, 0, _size);
3187 public virtual void Sort(IComparer comparer)
3189 Array.Sort(_items, 0, _size, comparer);
3192 public virtual void Sort(int index, int count, IComparer comparer)
3194 ArrayList.CheckRange(index, count, _size);
3196 Array.Sort(_items, index, count, comparer);
3199 public virtual object[] ToArray()
3203 retval = new object[_size];
3210 public virtual Array ToArray(Type elementType)
3214 retval = Array.CreateInstance(elementType, _size);
3221 public virtual object Clone()
3223 return new ArrayList(this._items, 0, this._size);
3228 #region Static Methods
3231 /// Does a check of the arguments many of the methods in ArrayList use.
3234 /// The choice of exceptions thrown sometimes seem to be arbitrarily chosen so
3235 /// not all methods actually make use of CheckRange.
3237 internal static void CheckRange(int index, int count, int listCount)
3241 throw new ArgumentOutOfRangeException("index", index, "Can't be less than 0.");
3246 throw new ArgumentOutOfRangeException("count", count, "Can't be less than 0.");
3249 // re-ordered to avoid possible integer overflow
3250 if (index > listCount - count)
3252 throw new ArgumentException("Index and count do not denote a valid range of elements.", "index");
3256 public static ArrayList Adapter(IList list)
3258 // LAMESPEC: EWWW. Other lists aren't *Array*Lists.
3262 throw new ArgumentNullException("list");
3265 if (list.IsSynchronized)
3267 return ArrayList.Synchronized(new ArrayListAdapter(list));
3271 return new ArrayListAdapter(list);
3275 public static ArrayList Synchronized(ArrayList arrayList)
3277 if (arrayList == null)
3279 throw new ArgumentNullException("arrayList");
3282 if (arrayList.IsSynchronized)
3287 return new SynchronizedArrayListWrapper(arrayList);
3290 public static IList Synchronized(IList list)
3294 throw new ArgumentNullException("list");
3297 if (list.IsSynchronized)
3302 return new SynchronizedListWrapper(list);
3305 public static ArrayList ReadOnly(ArrayList arrayList)
3307 if (arrayList == null)
3309 throw new ArgumentNullException("arrayList");
3312 if (arrayList.IsReadOnly)
3317 return new ReadOnlyArrayListWrapper(arrayList);
3320 public static IList ReadOnly(IList list)
3324 throw new ArgumentNullException("list");
3327 if (list.IsReadOnly)
3332 return new ReadOnlyListWrapper(list);
3335 public static ArrayList FixedSize(ArrayList arrayList)
3337 if (arrayList == null)
3339 throw new ArgumentNullException("arrayList");
3342 if (arrayList.IsFixedSize)
3347 return new FixedSizeArrayListWrapper(arrayList);
3350 public static IList FixedSize(IList list)
3354 throw new ArgumentNullException("list");
3357 if (list.IsFixedSize)
3362 return new FixedSizeListWrapper(list);
3365 public static ArrayList Repeat(object value, int count)
3367 ArrayList arrayList = new ArrayList(count);
3369 for (int i = 0; i < count; i++)
3371 arrayList.Add(value);