3 // Implementation of the ECMA ArrayList.
5 // Copyright (c) 2003 Thong (Tum) Nguyen [tum@veridicus.com]
6 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
8 // Permission is hereby granted, free of charge, to any person obtaining
9 // a copy of this software and associated documentation files (the
10 // "Software"), to deal in the Software without restriction, including
11 // without limitation the rights to use, copy, modify, merge, publish,
12 // distribute, sublicense, and/or sell copies of the Software, and to
13 // permit persons to whom the Software is furnished to do so, subject to
14 // the following conditions:
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 using System.Runtime.InteropServices;
30 namespace System.Collections
42 : IList, ICloneable, ICollection, IEnumerable
46 private sealed class ArrayListEnumerator
47 : IEnumerator, ICloneable
52 private object m_Current;
53 private ArrayList m_List;
54 private int m_ExpectedStateChanges;
56 public ArrayListEnumerator(ArrayList list)
57 : this(list, 0, list.Count)
63 return this.MemberwiseClone();
66 public ArrayListEnumerator(ArrayList list, int index, int count)
73 m_ExpectedStateChanges = list._version;
80 if (m_Pos == m_Index - 1) {
81 throw new InvalidOperationException("Enumerator unusable (Reset pending, or past end of array.");
88 public bool MoveNext()
90 if (m_List._version != m_ExpectedStateChanges)
92 throw new InvalidOperationException("List has changed.");
97 if (m_Pos - m_Index < m_Count)
99 m_Current = m_List[m_Pos];
114 sealed class SimpleEnumerator : IEnumerator, ICloneable
119 object currentElement;
120 static object endFlag = new object ();
122 public SimpleEnumerator (ArrayList list)
126 version = list._version;
127 currentElement = endFlag;
130 public object Clone ()
132 return MemberwiseClone ();
135 public bool MoveNext ()
137 if (version != list._version)
138 throw new InvalidOperationException("List has changed.");
140 if (++index < list.Count) {
141 currentElement = list [index];
144 currentElement = endFlag;
149 public object Current {
151 if (currentElement == endFlag) {
153 throw new InvalidOperationException ("Enumerator not started");
155 throw new InvalidOperationException ("Enumerator ended");
158 return currentElement;
164 if (version != list._version)
165 throw new InvalidOperationException ("List has changed.");
167 currentElement = endFlag;
174 #region ArrayListAdapter
177 /// Adapts various ILists into an ArrayList.
180 private sealed class ArrayListAdapter
183 private sealed class EnumeratorWithRange
184 : IEnumerator, ICloneable
186 private int m_StartIndex;
188 private int m_MaxCount;
189 private IEnumerator m_Enumerator;
191 public EnumeratorWithRange(IEnumerator enumerator, int index, int count)
194 m_StartIndex = index;
196 m_Enumerator = enumerator;
201 public object Clone()
203 return this.MemberwiseClone();
206 public object Current
210 return m_Enumerator.Current;
214 public bool MoveNext()
216 if (m_Count >= m_MaxCount)
223 return m_Enumerator.MoveNext();
229 m_Enumerator.Reset();
231 for (int i = 0; i < m_StartIndex; i++)
233 m_Enumerator.MoveNext();
238 private IList m_Adaptee;
240 public ArrayListAdapter(IList adaptee)
246 public override object this[int index]
250 return m_Adaptee[index];
255 m_Adaptee[index] = value;
259 public override int Count
263 return m_Adaptee.Count;
267 public override int Capacity
271 return m_Adaptee.Count;
276 if (value < m_Adaptee.Count)
278 throw new ArgumentException("capacity");
283 public override bool IsFixedSize
287 return m_Adaptee.IsFixedSize;
291 public override bool IsReadOnly
295 return m_Adaptee.IsReadOnly;
299 public override object SyncRoot
303 return m_Adaptee.SyncRoot;
307 public override int Add(object value)
309 return m_Adaptee.Add(value);
312 public override void Clear()
317 public override bool Contains(object value)
319 return m_Adaptee.Contains(value);
322 public override int IndexOf(object value)
324 return m_Adaptee.IndexOf(value);
327 public override int IndexOf(object value, int startIndex)
329 return IndexOf(value, startIndex, m_Adaptee.Count - startIndex);
332 public override int IndexOf(object value, int startIndex, int count)
334 if (startIndex < 0 || startIndex > m_Adaptee.Count)
336 throw new ArgumentOutOfRangeException("startIndex", startIndex,
337 "Does not specify valid index.");
342 throw new ArgumentOutOfRangeException("count", count,
343 "Can't be less than 0.");
346 // re-ordered to avoid possible integer overflow
347 if (startIndex > m_Adaptee.Count - count) {
348 // LAMESPEC: Every other method throws ArgumentException
349 throw new ArgumentOutOfRangeException("count",
350 "Start index and count do not specify a valid range.");
355 for (int i = startIndex; i < startIndex + count; i++)
357 if (m_Adaptee[i] == null)
365 for (int i = startIndex; i < startIndex + count; i++)
367 if (value.Equals(m_Adaptee[i]))
377 public override int LastIndexOf(object value)
379 return LastIndexOf(value, m_Adaptee.Count - 1);
382 public override int LastIndexOf(object value, int startIndex)
384 return LastIndexOf(value, startIndex, startIndex + 1);
387 public override int LastIndexOf(object value, int startIndex, int count)
391 throw new ArgumentOutOfRangeException("startIndex", startIndex, "< 0");
396 throw new ArgumentOutOfRangeException("count", count, "count is negative.");
399 if (startIndex - count + 1 < 0)
401 throw new ArgumentOutOfRangeException("count", count, "count is too large.");
406 for (int i = startIndex; i > startIndex - count; i--)
408 if (m_Adaptee[i] == null)
416 for (int i = startIndex; i > startIndex - count; i--)
418 if (value.Equals(m_Adaptee[i]))
428 public override void Insert(int index, object value)
430 m_Adaptee.Insert(index, value);
433 public override void InsertRange(int index, ICollection c)
437 throw new ArgumentNullException("c");
440 if (index > m_Adaptee.Count)
442 throw new ArgumentOutOfRangeException("index", index,
443 "Index must be >= 0 and <= Count.");
446 foreach (object value in c)
448 m_Adaptee.Insert(index++, value);
452 public override void Remove(object value)
454 m_Adaptee.Remove(value);
457 public override void RemoveAt(int index)
459 m_Adaptee.RemoveAt(index);
462 public override void RemoveRange(int index, int count)
464 CheckRange(index, count, m_Adaptee.Count);
466 for (int i = 0; i < count; i++)
468 m_Adaptee.RemoveAt(index);
472 public override void Reverse()
474 Reverse(0, m_Adaptee.Count);
477 public override void Reverse(int index, int count)
481 CheckRange(index, count, m_Adaptee.Count);
483 for (int i = 0; i < count / 2; i++)
485 tmp = m_Adaptee[i + index];
486 m_Adaptee[i + index] = m_Adaptee[(index + count) - i + index - 1];
487 m_Adaptee[(index + count) - i + index - 1] = tmp;
491 public override void SetRange(int index, ICollection c)
495 throw new ArgumentNullException("c");
498 if (index < 0 || index + c.Count > m_Adaptee.Count)
500 throw new ArgumentOutOfRangeException("index");
505 foreach (object value in c)
507 m_Adaptee[x++] = value;
511 public override void CopyTo(System.Array array)
513 m_Adaptee.CopyTo(array, 0);
516 public override void CopyTo(System.Array array, int index)
518 m_Adaptee.CopyTo(array, index);
521 public override void CopyTo(int index, System.Array array, int arrayIndex, int count)
525 throw new ArgumentOutOfRangeException("index", index,
526 "Can't be less than zero.");
531 throw new ArgumentOutOfRangeException("arrayIndex", arrayIndex,
532 "Can't be less than zero.");
537 throw new ArgumentOutOfRangeException("index", index,
538 "Can't be less than zero.");
541 if (index >= m_Adaptee.Count)
543 throw new ArgumentException("Can't be more or equal to list count.",
549 throw new ArgumentException("Can't copy into multi-dimensional array.");
552 if (arrayIndex >= array.Length)
554 throw new ArgumentException("arrayIndex can't be greater than array.Length - 1.");
557 if (array.Length - arrayIndex + 1 < count)
559 throw new ArgumentException("Destination array is too small.");
562 // re-ordered to avoid possible integer overflow
563 if (index > m_Adaptee.Count - count) {
564 throw new ArgumentException("Index and count do not denote a valid range of elements.", "index");
567 for (int i = 0; i < count; i++)
569 array.SetValue(m_Adaptee[index + i], arrayIndex + i);
573 public override bool IsSynchronized
577 return m_Adaptee.IsSynchronized;
581 public override IEnumerator GetEnumerator()
583 return m_Adaptee.GetEnumerator();
586 public override IEnumerator GetEnumerator(int index, int count)
588 CheckRange(index, count, m_Adaptee.Count);
590 return new EnumeratorWithRange(m_Adaptee.GetEnumerator(), index, count);
593 public override void AddRange(ICollection c)
595 foreach (object value in c)
597 m_Adaptee.Add(value);
601 public override int BinarySearch(object value)
603 return BinarySearch(value, null);
606 public override int BinarySearch(object value, IComparer comparer)
608 return BinarySearch(0, m_Adaptee.Count, value, comparer);
611 public override int BinarySearch(int index, int count, object value, IComparer comparer)
615 // Doing a direct BinarySearch on the adaptee will perform poorly if the adaptee is a linked-list.
616 // Alternatives include copying the adaptee to a temporary array first.
618 CheckRange(index, count, m_Adaptee.Count);
620 if (comparer == null)
622 comparer = Comparer.Default;
626 y = index + count - 1;
630 // Be careful with overflows
631 z = x + ((y - x) / 2);
633 r = comparer.Compare(value, m_Adaptee[z]);
652 public override object Clone()
654 return new ArrayList.ArrayListAdapter(m_Adaptee);
657 public override ArrayList GetRange(int index, int count)
659 CheckRange(index, count, m_Adaptee.Count);
661 return new RangedArrayList(this, index, count);
664 public override void TrimToSize()
669 public override void Sort()
671 Sort(Comparer.Default);
674 public override void Sort(IComparer comparer)
676 Sort(0, m_Adaptee.Count, comparer);
679 public override void Sort(int index, int count, IComparer comparer)
681 CheckRange(index, count, m_Adaptee.Count);
683 if (comparer == null)
685 comparer = Comparer.Default;
688 // Doing a direct sort on the adaptee will perform poorly if the adaptee is a linked-list.
689 // Alternatives include copying the adaptee into a temporary array first.
691 QuickSort(m_Adaptee, index, index + count - 1, comparer);
696 /// Swaps two items in a list at the specified indexes.
698 private static void Swap(IList list, int x, int y)
708 /// Quicksort for lists.
711 /// This function acts as both qsort() and partition().
713 internal static void QuickSort(IList list, int left, int right, IComparer comparer)
723 // Pick the pivot using the median-of-three strategy.
725 // Be careful with overflows
726 middle = left + ((right - left) / 2);
728 if (comparer.Compare(list[middle], list[left]) < 0)
730 Swap(list, middle, left);
733 if (comparer.Compare(list[right], list[left]) < 0)
735 Swap(list, right, left);
738 if (comparer.Compare(list[right], list[middle]) < 0)
740 Swap(list, right, middle);
743 if (right - left + 1 <= 3)
748 // Put the pivot in right - 1.
749 Swap(list, right - 1, middle);
751 // List should look like:
753 // [Small] ..Numbers.. [Middle] ..Numbers.. [Pivot][Large]
755 pivot = list[right - 1];
757 // Sort from (left + 1) to (right - 2).
764 while (comparer.Compare(list[++i], pivot) < 0);
765 while (comparer.Compare(list[--j], pivot) > 0);
777 // Put pivot into the right position (real middle).
779 Swap(list, right - 1, i);
781 // Recursively sort the left and right sub lists.
783 QuickSort(list, left, i - 1, comparer);
784 QuickSort(list, i + 1, right, comparer);
787 public override object[] ToArray()
791 retval = new object[m_Adaptee.Count];
793 m_Adaptee.CopyTo(retval, 0);
798 public override Array ToArray(Type elementType)
802 retval = Array.CreateInstance(elementType, m_Adaptee.Count);
804 m_Adaptee.CopyTo(retval, 0);
810 #endregion // ArrayListAdapter
813 // ArrayList wrappers
816 #region ArrayListWrapper
819 /// Base wrapper/decorator for ArrayLists. Simply delegates all methods to
820 /// the underlying wrappee.
823 private class ArrayListWrapper
826 protected ArrayList m_InnerArrayList;
830 public ArrayListWrapper(ArrayList innerArrayList)
832 m_InnerArrayList = innerArrayList;
839 public override object this[int index]
843 return m_InnerArrayList[index];
848 m_InnerArrayList[index] = value;
856 public override int Count
860 return m_InnerArrayList.Count;
864 public override int Capacity
868 return m_InnerArrayList.Capacity;
873 m_InnerArrayList.Capacity = value;
877 public override bool IsFixedSize
881 return m_InnerArrayList.IsFixedSize;
885 public override bool IsReadOnly
889 return m_InnerArrayList.IsReadOnly;
893 public override bool IsSynchronized
897 return m_InnerArrayList.IsSynchronized;
901 public override object SyncRoot
905 return m_InnerArrayList.SyncRoot;
913 public override int Add(object value)
915 return m_InnerArrayList.Add(value);
918 public override void Clear()
920 m_InnerArrayList.Clear();
923 public override bool Contains(object value)
925 return m_InnerArrayList.Contains(value);
928 public override int IndexOf(object value)
930 return m_InnerArrayList.IndexOf(value);
933 public override int IndexOf(object value, int startIndex)
935 return m_InnerArrayList.IndexOf(value, startIndex);
938 public override int IndexOf(object value, int startIndex, int count)
940 return m_InnerArrayList.IndexOf(value, startIndex, count);
943 public override int LastIndexOf(object value)
945 return m_InnerArrayList.LastIndexOf(value);
948 public override int LastIndexOf(object value, int startIndex)
950 return m_InnerArrayList.LastIndexOf(value, startIndex);
953 public override int LastIndexOf(object value, int startIndex, int count)
955 return m_InnerArrayList.LastIndexOf(value, startIndex, count);
958 public override void Insert(int index, object value)
960 m_InnerArrayList.Insert(index, value);
963 public override void InsertRange(int index, ICollection c)
965 m_InnerArrayList.InsertRange(index, c);
968 public override void Remove(object value)
970 m_InnerArrayList.Remove(value);
973 public override void RemoveAt(int index)
975 m_InnerArrayList.RemoveAt(index);
978 public override void RemoveRange(int index, int count)
980 m_InnerArrayList.RemoveRange(index, count);
983 public override void Reverse()
985 m_InnerArrayList.Reverse();
988 public override void Reverse(int index, int count)
990 m_InnerArrayList.Reverse(index, count);
993 public override void SetRange(int index, ICollection c)
995 m_InnerArrayList.SetRange(index, c);
998 public override void CopyTo(System.Array array)
1000 m_InnerArrayList.CopyTo(array);
1003 public override void CopyTo(System.Array array, int index)
1005 m_InnerArrayList.CopyTo(array, index);
1008 public override void CopyTo(int index, System.Array array, int arrayIndex, int count)
1010 m_InnerArrayList.CopyTo(index, array, arrayIndex, count);
1013 public override IEnumerator GetEnumerator()
1015 return m_InnerArrayList.GetEnumerator();
1018 public override IEnumerator GetEnumerator(int index, int count)
1020 return m_InnerArrayList.GetEnumerator(index, count);
1023 public override void AddRange(ICollection c)
1025 m_InnerArrayList.AddRange(c);
1028 public override int BinarySearch(object value)
1030 return m_InnerArrayList.BinarySearch(value);
1033 public override int BinarySearch(object value, IComparer comparer)
1035 return m_InnerArrayList.BinarySearch(value, comparer);
1038 public override int BinarySearch(int index, int count, object value, IComparer comparer)
1040 return m_InnerArrayList.BinarySearch(index, count, value, comparer);
1043 public override object Clone()
1045 return m_InnerArrayList.Clone();
1048 public override ArrayList GetRange(int index, int count)
1050 return m_InnerArrayList.GetRange(index, count);
1053 public override void TrimToSize()
1055 m_InnerArrayList.TrimToSize();
1058 public override void Sort()
1060 m_InnerArrayList.Sort();
1063 public override void Sort(IComparer comparer)
1065 m_InnerArrayList.Sort(comparer);
1068 public override void Sort(int index, int count, IComparer comparer)
1070 m_InnerArrayList.Sort(index, count, comparer);
1073 public override object[] ToArray()
1075 return m_InnerArrayList.ToArray();
1078 public override Array ToArray(Type elementType)
1080 return m_InnerArrayList.ToArray(elementType);
1088 #region SynchronizedArrayListWrapper
1091 /// ArrayListWrapper that synchronizes calls to all methods/properties.
1094 /// Works by just synchronizing all method calls. In the future careful optimisation
1095 /// could give better performance...
1098 private sealed class SynchronizedArrayListWrapper
1101 private object m_SyncRoot;
1103 #region Constructors
1106 /// Creates a new synchronized wrapper for the given <see cref="ArrayList"/>.
1108 /// <param name="innerArrayList"></param>
1109 internal SynchronizedArrayListWrapper(ArrayList innerArrayList)
1110 : base(innerArrayList)
1112 m_SyncRoot = innerArrayList.SyncRoot;
1119 public override object this[int index]
1125 return m_InnerArrayList[index];
1133 m_InnerArrayList[index] = value;
1142 // Some of these properties may be calculated so it's best to synchronize
1143 // them even though it might cause a performance hit.
1144 // Better safe than sorry ;D.
1146 public override int Count
1152 return m_InnerArrayList.Count;
1157 public override int Capacity
1163 return m_InnerArrayList.Capacity;
1171 m_InnerArrayList.Capacity = value;
1176 public override bool IsFixedSize
1182 return m_InnerArrayList.IsFixedSize;
1187 public override bool IsReadOnly
1193 return m_InnerArrayList.IsReadOnly;
1198 public override bool IsSynchronized
1206 public override object SyncRoot
1218 public override int Add(object value)
1222 return m_InnerArrayList.Add(value);
1226 public override void Clear()
1230 m_InnerArrayList.Clear();
1234 public override bool Contains(object value)
1238 return m_InnerArrayList.Contains(value);
1242 public override int IndexOf(object value)
1246 return m_InnerArrayList.IndexOf(value);
1250 public override int IndexOf(object value, int startIndex)
1254 return m_InnerArrayList.IndexOf(value, startIndex);
1258 public override int IndexOf(object value, int startIndex, int count)
1262 return m_InnerArrayList.IndexOf(value, startIndex, count);
1266 public override int LastIndexOf(object value)
1270 return m_InnerArrayList.LastIndexOf(value);
1274 public override int LastIndexOf(object value, int startIndex)
1278 return m_InnerArrayList.LastIndexOf(value, startIndex);
1282 public override int LastIndexOf(object value, int startIndex, int count)
1286 return m_InnerArrayList.LastIndexOf(value, startIndex, count);
1290 public override void Insert(int index, object value)
1294 m_InnerArrayList.Insert(index, value);
1298 public override void InsertRange(int index, ICollection c)
1302 m_InnerArrayList.InsertRange(index, c);
1306 public override void Remove(object value)
1310 m_InnerArrayList.Remove(value);
1314 public override void RemoveAt(int index)
1318 m_InnerArrayList.RemoveAt(index);
1322 public override void RemoveRange(int index, int count)
1326 m_InnerArrayList.RemoveRange(index, count);
1330 public override void Reverse()
1334 m_InnerArrayList.Reverse();
1338 public override void Reverse(int index, int count)
1342 m_InnerArrayList.Reverse(index, count);
1346 public override void CopyTo(System.Array array)
1350 m_InnerArrayList.CopyTo(array);
1354 public override void CopyTo(System.Array array, int index)
1358 m_InnerArrayList.CopyTo(array, index);
1362 public override void CopyTo(int index, System.Array array, int arrayIndex, int count)
1366 m_InnerArrayList.CopyTo(index, array, arrayIndex, count);
1370 public override IEnumerator GetEnumerator()
1374 return m_InnerArrayList.GetEnumerator();
1378 public override IEnumerator GetEnumerator(int index, int count)
1382 return m_InnerArrayList.GetEnumerator(index, count);
1386 public override void AddRange(ICollection c)
1390 m_InnerArrayList.AddRange(c);
1394 public override int BinarySearch(object value)
1398 return m_InnerArrayList.BinarySearch(value);
1402 public override int BinarySearch(object value, IComparer comparer)
1406 return m_InnerArrayList.BinarySearch(value, comparer);
1410 public override int BinarySearch(int index, int count, object value, IComparer comparer)
1414 return m_InnerArrayList.BinarySearch(index, count, value, comparer);
1418 public override object Clone()
1422 return m_InnerArrayList.Clone();
1426 public override ArrayList GetRange(int index, int count)
1430 return m_InnerArrayList.GetRange(index, count);
1434 public override void TrimToSize()
1438 m_InnerArrayList.TrimToSize();
1442 public override void Sort()
1446 m_InnerArrayList.Sort();
1450 public override void Sort(IComparer comparer)
1454 m_InnerArrayList.Sort(comparer);
1458 public override void Sort(int index, int count, IComparer comparer)
1462 m_InnerArrayList.Sort(index, count, comparer);
1466 public override object[] ToArray()
1470 return m_InnerArrayList.ToArray();
1474 public override Array ToArray(Type elementType)
1478 return m_InnerArrayList.ToArray(elementType);
1487 #region FixedSizeArrayListWrapper
1490 private class FixedSizeArrayListWrapper
1493 #region Constructors
1495 public FixedSizeArrayListWrapper(ArrayList innerList)
1505 /// Gets the error message to display when an readonly/fixedsize related exception is
1508 protected virtual string ErrorMessage
1512 return "Can't add or remove from a fixed-size list.";
1516 public override int Capacity
1520 return base.Capacity;
1525 throw new NotSupportedException(this.ErrorMessage);
1529 public override bool IsFixedSize
1541 public override int Add(object value)
1543 throw new NotSupportedException(this.ErrorMessage);
1546 public override void AddRange(ICollection c)
1548 throw new NotSupportedException(this.ErrorMessage);
1551 public override void Clear()
1553 throw new NotSupportedException(this.ErrorMessage);
1556 public override void Insert(int index, object value)
1558 throw new NotSupportedException(this.ErrorMessage);
1561 public override void InsertRange(int index, ICollection c)
1563 throw new NotSupportedException(this.ErrorMessage);
1566 public override void Remove(object value)
1568 throw new NotSupportedException(this.ErrorMessage);
1571 public override void RemoveAt(int index)
1573 throw new NotSupportedException(this.ErrorMessage);
1576 public override void RemoveRange(int index, int count)
1578 throw new NotSupportedException(this.ErrorMessage);
1581 public override void TrimToSize()
1583 throw new NotSupportedException(this.ErrorMessage);
1591 #region ReadOnlyArrayListWrapper
1594 private sealed class ReadOnlyArrayListWrapper
1595 : FixedSizeArrayListWrapper
1597 protected override string ErrorMessage
1601 return "Can't modify a readonly list.";
1605 public override bool IsReadOnly
1613 public ReadOnlyArrayListWrapper(ArrayList innerArrayList)
1614 : base(innerArrayList)
1618 public override object this[int index]
1622 return m_InnerArrayList[index];
1627 throw new NotSupportedException(this.ErrorMessage);
1631 public override void Reverse()
1633 throw new NotSupportedException(this.ErrorMessage);
1636 public override void Reverse(int index, int count)
1638 throw new NotSupportedException(this.ErrorMessage);
1641 public override void SetRange(int index, ICollection c)
1643 throw new NotSupportedException(this.ErrorMessage);
1646 public override void Sort()
1648 throw new NotSupportedException(this.ErrorMessage);
1651 public override void Sort(IComparer comparer)
1653 throw new NotSupportedException(this.ErrorMessage);
1656 public override void Sort(int index, int count, IComparer comparer)
1658 throw new NotSupportedException(this.ErrorMessage);
1664 #region RangedArrayList
1667 private sealed class RangedArrayList
1670 private int m_InnerIndex;
1671 private int m_InnerCount;
1672 private int m_InnerStateChanges;
1674 public RangedArrayList(ArrayList innerList, int index, int count)
1677 m_InnerIndex = index;
1678 m_InnerCount = count;
1679 m_InnerStateChanges = innerList._version;
1684 public override bool IsSynchronized
1692 public override object this[int index]
1696 if (index < 0 || index > m_InnerCount)
1698 throw new ArgumentOutOfRangeException("index");
1701 return m_InnerArrayList[m_InnerIndex + index];
1706 if (index < 0 || index > m_InnerCount)
1708 throw new ArgumentOutOfRangeException("index");
1711 m_InnerArrayList[m_InnerIndex + index] = value;
1719 public override int Count
1723 VerifyStateChanges();
1725 return m_InnerCount;
1729 public override int Capacity
1733 return m_InnerArrayList.Capacity;
1738 if (value < m_InnerCount)
1740 throw new ArgumentOutOfRangeException();
1749 private void VerifyStateChanges()
1751 if (m_InnerStateChanges != m_InnerArrayList._version)
1753 throw new InvalidOperationException
1754 ("ArrayList view is invalid because the underlying ArrayList was modified.");
1758 public override int Add(object value)
1760 VerifyStateChanges();
1762 m_InnerArrayList.Insert(m_InnerIndex + m_InnerCount, value);
1764 m_InnerStateChanges = m_InnerArrayList._version;
1766 return ++m_InnerCount;
1769 public override void Clear()
1771 VerifyStateChanges();
1773 m_InnerArrayList.RemoveRange(m_InnerIndex, m_InnerCount);
1776 m_InnerStateChanges = m_InnerArrayList._version;
1779 public override bool Contains(object value)
1781 return m_InnerArrayList.Contains(value, m_InnerIndex, m_InnerCount);
1784 public override int IndexOf(object value)
1786 return IndexOf(value, 0);
1789 public override int IndexOf(object value, int startIndex)
1791 return IndexOf(value, startIndex, m_InnerCount - startIndex);
1794 public override int IndexOf(object value, int startIndex, int count)
1796 if (startIndex < 0 || startIndex > m_InnerCount)
1798 throw new ArgumentOutOfRangeException("startIndex", startIndex,
1799 "Does not specify valid index.");
1804 throw new ArgumentOutOfRangeException("count", count,
1805 "Can't be less than 0.");
1808 // re-ordered to avoid possible integer overflow
1809 if (startIndex > m_InnerCount - count)
1811 // LAMESPEC: Every other method throws ArgumentException
1813 throw new ArgumentOutOfRangeException("count",
1814 "Start index and count do not specify a valid range.");
1817 int retval = m_InnerArrayList.IndexOf(value, m_InnerIndex + startIndex, count);
1825 return retval - m_InnerIndex;
1829 public override int LastIndexOf(object value)
1831 return LastIndexOf(value, m_InnerCount - 1);
1834 public override int LastIndexOf(object value, int startIndex)
1836 return LastIndexOf(value, startIndex, startIndex + 1);
1839 public override int LastIndexOf(object value, int startIndex, int count)
1843 throw new ArgumentOutOfRangeException("startIndex", startIndex, "< 0");
1848 throw new ArgumentOutOfRangeException("count", count, "count is negative.");
1851 int retval = m_InnerArrayList.LastIndexOf(value, m_InnerIndex + startIndex, count);
1859 return retval - m_InnerIndex;
1863 public override void Insert(int index, object value)
1865 VerifyStateChanges();
1867 if (index < 0 || index > m_InnerCount)
1869 throw new ArgumentOutOfRangeException("index", index,
1870 "Index must be >= 0 and <= Count.");
1873 m_InnerArrayList.Insert(m_InnerIndex + index, value);
1877 m_InnerStateChanges = m_InnerArrayList._version;
1880 public override void InsertRange(int index, ICollection c)
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.InsertRange(m_InnerIndex + index, c);
1892 m_InnerCount += c.Count;
1894 m_InnerStateChanges = m_InnerArrayList._version;
1897 public override void Remove(object value)
1899 VerifyStateChanges();
1901 int x = IndexOf(value);
1908 m_InnerStateChanges = m_InnerArrayList._version;
1911 public override void RemoveAt(int index)
1913 VerifyStateChanges();
1915 if (index < 0 || index > m_InnerCount)
1917 throw new ArgumentOutOfRangeException("index", index,
1918 "Index must be >= 0 and <= Count.");
1921 m_InnerArrayList.RemoveAt(m_InnerIndex + index);
1924 m_InnerStateChanges = m_InnerArrayList._version;
1927 public override void RemoveRange(int index, int count)
1929 VerifyStateChanges();
1931 CheckRange(index, count, m_InnerCount);
1933 m_InnerArrayList.RemoveRange(m_InnerIndex + index, count);
1935 m_InnerCount -= count;
1937 m_InnerStateChanges = m_InnerArrayList._version;
1940 public override void Reverse()
1942 Reverse(0, m_InnerCount);
1945 public override void Reverse(int index, int count)
1947 VerifyStateChanges();
1949 CheckRange(index, count, m_InnerCount);
1951 m_InnerArrayList.Reverse(m_InnerIndex + index, count);
1953 m_InnerStateChanges = m_InnerArrayList._version;
1956 public override void SetRange(int index, ICollection c)
1958 VerifyStateChanges();
1960 if (index < 0 || index > m_InnerCount)
1962 throw new ArgumentOutOfRangeException("index", index,
1963 "Index must be >= 0 and <= Count.");
1966 m_InnerArrayList.SetRange(m_InnerIndex + index, c);
1968 m_InnerStateChanges = m_InnerArrayList._version;
1971 public override void CopyTo(System.Array array)
1976 public override void CopyTo(System.Array array, int index)
1978 CopyTo(0, array, index, m_InnerCount);
1981 public override void CopyTo(int index, System.Array array, int arrayIndex, int count)
1983 CheckRange(index, count, m_InnerCount);
1985 m_InnerArrayList.CopyTo(m_InnerIndex + index, array, arrayIndex, count);
1988 public override IEnumerator GetEnumerator()
1990 return GetEnumerator(0, m_InnerCount);
1993 public override IEnumerator GetEnumerator(int index, int count)
1995 CheckRange(index, count, m_InnerCount);
1997 return m_InnerArrayList.GetEnumerator(m_InnerIndex + index, count);
2000 public override void AddRange(ICollection c)
2002 VerifyStateChanges();
2004 m_InnerArrayList.InsertRange(m_InnerCount, c);
2006 m_InnerCount += c.Count;
2008 m_InnerStateChanges = m_InnerArrayList._version;
2011 public override int BinarySearch(object value)
2013 return BinarySearch(0, m_InnerCount, value, Comparer.Default);
2016 public override int BinarySearch(object value, IComparer comparer)
2018 return BinarySearch(0, m_InnerCount, value, comparer);
2021 public override int BinarySearch(int index, int count, object value, IComparer comparer)
2023 CheckRange(index, count, m_InnerCount);
2025 return m_InnerArrayList.BinarySearch(m_InnerIndex + index, count, value, comparer);
2028 public override object Clone()
2030 return new RangedArrayList((ArrayList)m_InnerArrayList.Clone(), m_InnerIndex, m_InnerCount);
2033 public override ArrayList GetRange(int index, int count)
2035 CheckRange(index, count, m_InnerCount);
2037 return new RangedArrayList(this, index, count);
2040 public override void TrimToSize()
2042 throw new NotSupportedException();
2045 public override void Sort()
2047 Sort(Comparer.Default);
2050 public override void Sort(IComparer comparer)
2052 Sort(0, m_InnerCount, comparer);
2055 public override void Sort(int index, int count, IComparer comparer)
2057 VerifyStateChanges();
2059 CheckRange(index, count, m_InnerCount);
2061 m_InnerArrayList.Sort(m_InnerIndex + index, count, comparer);
2063 m_InnerStateChanges = m_InnerArrayList._version;
2066 public override object[] ToArray()
2070 array = new object[m_InnerCount];
2072 m_InnerArrayList.CopyTo (m_InnerIndex, array, 0, m_InnerCount);
2077 public override Array ToArray(Type elementType)
2081 array = Array.CreateInstance(elementType, m_InnerCount);
2083 m_InnerArrayList.CopyTo(m_InnerIndex, array, 0, m_InnerCount);
2097 #region SynchronizedListWrapper
2100 private sealed class SynchronizedListWrapper
2103 private object m_SyncRoot;
2105 public SynchronizedListWrapper(IList innerList)
2108 m_SyncRoot = innerList.SyncRoot;
2111 public override int Count
2117 return m_InnerList.Count;
2122 public override bool IsSynchronized
2130 public override object SyncRoot
2136 return m_InnerList.SyncRoot;
2141 public override bool IsFixedSize
2147 return m_InnerList.IsFixedSize;
2152 public override bool IsReadOnly
2158 return m_InnerList.IsReadOnly;
2163 public override object this[int index]
2169 return m_InnerList[index];
2177 m_InnerList[index] = value;
2182 public override int Add(object value)
2186 return m_InnerList.Add(value);
2190 public override void Clear()
2194 m_InnerList.Clear();
2198 public override bool Contains(object value)
2202 return m_InnerList.Contains(value);
2206 public override int IndexOf(object value)
2210 return m_InnerList.IndexOf(value);
2214 public override void Insert(int index, object value)
2218 m_InnerList.Insert(index, value);
2222 public override void Remove(object value)
2226 m_InnerList.Remove(value);
2230 public override void RemoveAt(int index)
2234 m_InnerList.RemoveAt(index);
2238 public override void CopyTo(Array array, int index)
2242 m_InnerList.CopyTo(array, index);
2246 public override IEnumerator GetEnumerator()
2250 return m_InnerList.GetEnumerator();
2257 #region FixedSizeListWrapper
2260 private class FixedSizeListWrapper
2263 protected virtual string ErrorMessage
2267 return "List is fixed-size.";
2271 public override bool IsFixedSize
2279 public FixedSizeListWrapper(IList innerList)
2284 public override int Add(object value)
2286 throw new NotSupportedException(this.ErrorMessage);
2289 public override void Clear()
2291 throw new NotSupportedException(this.ErrorMessage);
2294 public override void Insert(int index, object value)
2296 throw new NotSupportedException(this.ErrorMessage);
2299 public override void Remove(object value)
2301 throw new NotSupportedException(this.ErrorMessage);
2304 public override void RemoveAt(int index)
2306 throw new NotSupportedException(this.ErrorMessage);
2312 #region ReadOnlyListWrapper
2315 private sealed class ReadOnlyListWrapper
2316 : FixedSizeListWrapper
2318 protected override string ErrorMessage
2322 return "List is read-only.";
2326 public override bool IsReadOnly
2334 public ReadOnlyListWrapper(IList innerList)
2339 public override object this[int index]
2343 return m_InnerList[index];
2348 throw new NotSupportedException(this.ErrorMessage);
2358 /// Decorates/Wraps any <c>IList</c> implementing object.
2361 private class ListWrapper
2366 protected IList m_InnerList;
2370 #region Constructors
2372 public ListWrapper(IList innerList)
2374 m_InnerList = innerList;
2381 public virtual object this[int index]
2385 return m_InnerList[index];
2390 m_InnerList[index] = value;
2398 public virtual int Count
2402 return m_InnerList.Count;
2406 public virtual bool IsSynchronized
2410 return m_InnerList.IsSynchronized;
2414 public virtual object SyncRoot
2418 return m_InnerList.SyncRoot;
2422 public virtual bool IsFixedSize
2426 return m_InnerList.IsFixedSize;
2430 public virtual bool IsReadOnly
2434 return m_InnerList.IsReadOnly;
2442 public virtual int Add(object value)
2444 return m_InnerList.Add(value);
2447 public virtual void Clear()
2449 m_InnerList.Clear();
2452 public virtual bool Contains(object value)
2454 return m_InnerList.Contains(value);
2457 public virtual int IndexOf(object value)
2459 return m_InnerList.IndexOf(value);
2462 public virtual void Insert(int index, object value)
2464 m_InnerList.Insert(index, value);
2467 public virtual void Remove(object value)
2469 m_InnerList.Remove(value);
2472 public virtual void RemoveAt(int index)
2474 m_InnerList.RemoveAt(index);
2477 public virtual void CopyTo(Array array, int index)
2479 m_InnerList.CopyTo(array, index);
2482 public virtual IEnumerator GetEnumerator()
2484 return m_InnerList.GetEnumerator();
2493 // Start of ArrayList
2499 private const int DefaultInitialCapacity = 4;
2501 private const int DefaultInitialCapacity = 16;
2505 /// Number of items in the list.
2510 /// Array to store the items.
2512 private object[] _items;
2515 /// Total number of state changes.
2517 private int _version;
2521 #region Constructors
2524 /// Initializes a new instance of the <see cref="ArrayList"/> class that is empty and
2525 /// has the default initial capacity (16).
2530 _items = new object[0];
2532 _items = new object[DefaultInitialCapacity];
2537 /// Initializes a new instance of the <see cref="ArrayList"/> class that contains
2538 /// elements copied from the specified collection and that has the same initial capacity
2539 /// as the number of elements copied.
2541 /// <param name="c">
2542 /// The <see cref="ICollection"/> whose elements are copied into the new list.
2544 /// <exception cref="ArgumentNullException">
2545 /// The argument <c>c</c> is a null reference.
2547 public ArrayList(ICollection c)
2553 throw new ArgumentNullException("c");
2558 if (array != null && array.Rank != 1)
2560 throw new RankException();
2563 _items = new object[c.Count];
2569 /// Initializes a new instance of the <see cref="ArrayList"/> class that is empty and
2570 /// has the specified initial capacity.
2572 /// <param name="capacity">
2573 /// The number of elements that hte new list is initially capable of storing.
2575 /// <exception cref="ArgumentOutOfRangeException">
2576 /// The <c>capacity</c> is less than zero.
2578 public ArrayList(int capacity)
2582 throw new ArgumentOutOfRangeException("capacity",
2583 capacity, "The initial capacity can't be smaller than zero.");
2588 capacity = DefaultInitialCapacity;
2590 _items = new object [capacity];
2594 /// Used by ArrayListAdapter to allow creation of an ArrayList with no storage buffer.
2596 private ArrayList(int initialCapacity, bool forceZeroSize)
2604 throw new InvalidOperationException("Use ArrayList(int)");
2609 /// Initializes a new array list that contains a copy of the given array and with the
2612 /// <param name="array"></param>
2613 private ArrayList(object[] array, int index, int count)
2617 _items = new object[DefaultInitialCapacity];
2621 _items = new object[count];
2624 Array.Copy(array, index, _items, 0, count);
2634 /// Gets/Sets an element in the list by index.
2636 /// <exception cref="ArgumentOutOfRangeException">
2637 /// The index is less than 0 or more then or equal to the list count.
2639 public virtual object this[int index]
2643 if (index < 0 || index >= _size)
2645 throw new ArgumentOutOfRangeException("index", index,
2646 "Index is less than 0 or more than or equal to the list count.");
2649 return _items[index];
2654 if (index < 0 || index >= _size)
2656 throw new ArgumentOutOfRangeException("index", index,
2657 "Index is less than 0 or more than or equal to the list count.");
2660 _items[index] = value;
2670 /// Gets the number of elements in the list.
2672 public virtual int Count
2681 /// Gets the number of elements the list can carry without needing to expand.
2684 /// ArrayLists automatically double their capacity when the capacity limit is broken.
2686 /// <exception cref="ArgumentOutOfRangeException">
2687 /// The capacity is less than the count.
2689 public virtual int Capacity
2693 return _items.Length;
2700 throw new ArgumentOutOfRangeException("Capacity", value,
2701 "Must be more than count.");
2706 newArray = new object[value];
2708 Array.Copy(_items, 0, newArray, 0, _size);
2715 /// <see cref="IList.IsFixedSize"/>
2718 public virtual bool IsFixedSize
2727 /// <see cref="IList.IsReadOnly"/>
2729 public virtual bool IsReadOnly
2738 /// <see cref="ICollection.IsSynchronized"/>
2740 public virtual bool IsSynchronized
2749 /// <see cref="ICollection.SyncRoot"/>
2751 public virtual object SyncRoot
2764 /// Ensures that the list has the capacity to contain the given <c>count</c> by
2765 /// automatically expanding the capacity when required.
2767 private void EnsureCapacity(int count)
2769 if (count <= _items.Length)
2777 newLength = _items.Length << 1;
2779 newLength = DefaultInitialCapacity;
2781 while (newLength < count)
2786 newData = new object[newLength];
2788 Array.Copy(_items, 0, newData, 0, _items.Length);
2794 /// Shifts a section of the list.
2796 /// <param name="index">
2797 /// The start of the section to shift (the element at index is included in the shift).
2799 /// <param name="count">
2800 /// The number of positions to shift by (can be negative).
2802 private void Shift(int index, int count)
2806 if (_size + count > _items.Length)
2811 newLength = (_items.Length > 0) ? _items.Length << 1 : 1;
2813 while (newLength < _size + count)
2818 newData = new object[newLength];
2820 Array.Copy(_items, 0, newData, 0, index);
2821 Array.Copy(_items, index, newData, index + count, _size - index);
2827 Array.Copy(_items, index, _items, index + count, _size - index);
2832 // Remember count is negative so this is actually index + (-count)
2834 int x = index - count ;
2836 Array.Copy(_items, x, _items, index, _size - x);
2837 Array.Clear(_items, _size + count, - count);
2841 public virtual int Add(object value)
2843 // Do a check here in case EnsureCapacity isn't inlined.
2845 if (_items.Length <= _size /* same as _items.Length < _size + 1) */)
2847 EnsureCapacity(_size + 1);
2850 _items[_size] = value;
2857 public virtual void Clear()
2859 // Keep the array but null all members so they can be garbage collected.
2861 Array.Clear(_items, 0, _size);
2867 public virtual bool Contains(object item)
2869 return IndexOf(item, 0, _size) > -1;
2872 internal virtual bool Contains(object value, int startIndex, int count)
2874 return IndexOf(value, startIndex, count) > -1;
2877 public virtual int IndexOf(object value)
2879 return IndexOf(value, 0);
2882 public virtual int IndexOf(object value, int startIndex)
2884 return IndexOf(value, startIndex, _size - startIndex);
2887 public virtual int IndexOf(object value, int startIndex, int count)
2889 if (startIndex < 0 || startIndex > _size)
2891 throw new ArgumentOutOfRangeException("startIndex", startIndex,
2892 "Does not specify valid index.");
2897 throw new ArgumentOutOfRangeException("count", count,
2898 "Can't be less than 0.");
2901 // re-ordered to avoid integer overflow
2902 if (startIndex > _size - count)
2904 // LAMESPEC: Every other method throws ArgumentException
2906 throw new ArgumentOutOfRangeException("count",
2907 "Start index and count do not specify a valid range.");
2910 return Array.IndexOf(_items, value, startIndex, count);
2913 public virtual int LastIndexOf(object value)
2915 return LastIndexOf(value, _size - 1);
2918 public virtual int LastIndexOf(object value, int startIndex)
2920 return LastIndexOf(value, startIndex, startIndex + 1);
2923 public virtual int LastIndexOf (object value, int startIndex, int count)
2925 // Array will catch the exceptions
2926 return Array.LastIndexOf (_items, value, startIndex, count);
2929 public virtual void Insert(int index, object value)
2931 if (index < 0 || index > _size)
2933 throw new ArgumentOutOfRangeException("index", index,
2934 "Index must be >= 0 and <= Count.");
2939 _items[index] = value;
2944 public virtual void InsertRange(int index, ICollection c)
2950 throw new ArgumentNullException("c");
2953 if (index < 0 || index > _size)
2955 throw new ArgumentOutOfRangeException("index", index,
2956 "Index must be >= 0 and <= Count.");
2961 // Do a check here in case EnsureCapacity isn't inlined.
2963 if (_items.Length < _size + i)
2965 EnsureCapacity(_size + i);
2970 Array.Copy(_items, index, _items, index + i, _size - index);
2973 // Handle inserting a range from a list to itself specially.
2975 if (this == c.SyncRoot)
2977 // Copy range before the insert point.
2979 Array.Copy(_items, 0, _items, index, index);
2981 // Copy range after the insert point.
2983 Array.Copy(_items, index + i, _items, index << 1, _size - index);
2987 c.CopyTo(_items, index);
2994 public virtual void Remove(object obj)
3008 public virtual void RemoveAt(int index)
3010 if (index < 0 || index >= _size)
3012 throw new ArgumentOutOfRangeException("index", index,
3013 "Less than 0 or more than list count.");
3021 public virtual void RemoveRange(int index, int count)
3023 ArrayList.CheckRange(index, count, _size);
3025 Shift(index, -count);
3030 public virtual void Reverse()
3032 Array.Reverse(_items, 0, _size);
3036 public virtual void Reverse(int index, int count)
3038 ArrayList.CheckRange(index, count, _size);
3040 Array.Reverse(_items, index, count);
3044 public virtual void CopyTo(System.Array array)
3046 Array.Copy(_items, array, _size);
3049 public virtual void CopyTo(System.Array array, int arrayIndex)
3051 CopyTo(0, array, arrayIndex, _size);
3054 public virtual void CopyTo(int index, System.Array array, int arrayIndex, int count)
3058 throw new ArgumentNullException("array");
3061 if (array.Rank != 1)
3064 // This should be a RankException because Array.Copy throws RankException.
3066 throw new ArgumentException("Must have only 1 dimensions.", "array");
3069 Array.Copy(_items, index, array, arrayIndex, count);
3072 public virtual IEnumerator GetEnumerator()
3074 return new SimpleEnumerator(this);
3077 public virtual IEnumerator GetEnumerator(int index, int count)
3079 ArrayList.CheckRange(index, count, _size);
3081 return new ArrayListEnumerator(this, index, count);
3084 public virtual void AddRange(ICollection c)
3086 InsertRange(_size, c);
3089 public virtual int BinarySearch(object value)
3093 return Array.BinarySearch(_items, 0, _size, value);
3095 catch (InvalidOperationException e)
3097 throw new ArgumentException(e.Message);
3101 public virtual int BinarySearch(object value, IComparer comparer)
3105 return Array.BinarySearch(_items, 0, _size, value, comparer);
3107 catch (InvalidOperationException e)
3109 throw new ArgumentException(e.Message);
3113 public virtual int BinarySearch(int index, int count, object value, IComparer comparer)
3117 return Array.BinarySearch(_items, index, count, value, comparer);
3119 catch (InvalidOperationException e)
3121 throw new ArgumentException(e.Message);
3124 public virtual ArrayList GetRange(int index, int count)
3126 ArrayList.CheckRange(index, count, _size);
3128 if (this.IsSynchronized)
3130 return ArrayList.Synchronized(new RangedArrayList(this, index, count));
3134 return new RangedArrayList(this, index, count);
3138 public virtual void SetRange(int index, ICollection c)
3142 throw new ArgumentNullException("c");
3145 if (index < 0 || index + c.Count > _size)
3147 throw new ArgumentOutOfRangeException("index");
3150 c.CopyTo(_items, index);
3155 public virtual void TrimToSize()
3157 if (_items.Length > _size)
3163 newArray = new object[DefaultInitialCapacity];
3167 newArray = new object[_size];
3170 Array.Copy(_items, 0, newArray, 0, _size);
3176 public virtual void Sort()
3178 Array.Sort(_items, 0, _size);
3183 public virtual void Sort(IComparer comparer)
3185 Array.Sort(_items, 0, _size, comparer);
3188 public virtual void Sort(int index, int count, IComparer comparer)
3190 ArrayList.CheckRange(index, count, _size);
3192 Array.Sort(_items, index, count, comparer);
3195 public virtual object[] ToArray()
3199 retval = new object[_size];
3206 public virtual Array ToArray(Type type)
3210 retval = Array.CreateInstance(type, _size);
3217 public virtual object Clone()
3219 return new ArrayList(this._items, 0, this._size);
3224 #region Static Methods
3227 /// Does a check of the arguments many of the methods in ArrayList use.
3230 /// The choice of exceptions thrown sometimes seem to be arbitrarily chosen so
3231 /// not all methods actually make use of CheckRange.
3233 internal static void CheckRange(int index, int count, int listCount)
3237 throw new ArgumentOutOfRangeException("index", index, "Can't be less than 0.");
3242 throw new ArgumentOutOfRangeException("count", count, "Can't be less than 0.");
3245 // re-ordered to avoid possible integer overflow
3246 if (index > listCount - count)
3248 throw new ArgumentException("Index and count do not denote a valid range of elements.", "index");
3252 public static ArrayList Adapter(IList list)
3254 // LAMESPEC: EWWW. Other lists aren't *Array*Lists.
3258 throw new ArgumentNullException("list");
3261 ArrayList arrayList = list as ArrayList;
3262 if (arrayList != null)
3265 arrayList = new ArrayListAdapter(list);
3267 if (list.IsSynchronized)
3268 return ArrayList.Synchronized(arrayList);
3273 public static ArrayList Synchronized(ArrayList list)
3277 throw new ArgumentNullException("list");
3280 if (list.IsSynchronized)
3285 return new SynchronizedArrayListWrapper(list);
3288 public static IList Synchronized(IList list)
3292 throw new ArgumentNullException("list");
3295 if (list.IsSynchronized)
3300 return new SynchronizedListWrapper(list);
3303 public static ArrayList ReadOnly(ArrayList list)
3307 throw new ArgumentNullException("list");
3310 if (list.IsReadOnly)
3315 return new ReadOnlyArrayListWrapper(list);
3318 public static IList ReadOnly(IList list)
3322 throw new ArgumentNullException("list");
3325 if (list.IsReadOnly)
3330 return new ReadOnlyListWrapper(list);
3333 public static ArrayList FixedSize(ArrayList list)
3337 throw new ArgumentNullException("list");
3340 if (list.IsFixedSize)
3345 return new FixedSizeArrayListWrapper(list);
3348 public static IList FixedSize(IList list)
3352 throw new ArgumentNullException("list");
3355 if (list.IsFixedSize)
3360 return new FixedSizeListWrapper(list);
3363 public static ArrayList Repeat(object value, int count)
3365 ArrayList arrayList = new ArrayList(count);
3367 for (int i = 0; i < count; i++)
3369 arrayList.Add(value);