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 using System.Collections;
30 namespace System.Collections
33 public class ArrayList
34 : IList, ICloneable, ICollection, IEnumerable
38 private sealed class ArrayListEnumerator
39 : IEnumerator, ICloneable
44 private object m_Current;
45 private ArrayList m_List;
46 private int m_ExpectedStateChanges;
48 public ArrayListEnumerator(ArrayList list)
49 : this(list, 0, list.Count)
55 return this.MemberwiseClone();
58 public ArrayListEnumerator(ArrayList list, int index, int count)
65 m_ExpectedStateChanges = list._version;
72 if (m_Pos == m_Index - 1) {
73 throw new InvalidOperationException("Enumerator unusable (Reset pending, or past end of array.");
80 public bool MoveNext()
82 if (m_List._version != m_ExpectedStateChanges)
84 throw new InvalidOperationException("List has changed.");
89 if (m_Pos - m_Index < m_Count)
91 m_Current = m_List[m_Pos];
106 sealed class SimpleEnumerator : IEnumerator, ICloneable
111 object currentElement;
112 static object endFlag = new object ();
114 public SimpleEnumerator (ArrayList list)
118 version = list._version;
119 currentElement = endFlag;
122 public object Clone ()
124 return MemberwiseClone ();
127 public bool MoveNext ()
129 if (version != list._version)
130 throw new InvalidOperationException("List has changed.");
132 if (++index < list.Count) {
133 currentElement = list [index];
136 currentElement = endFlag;
141 public object Current {
143 if (currentElement == endFlag) {
145 throw new InvalidOperationException ("Enumerator not started");
147 throw new InvalidOperationException ("Enumerator ended");
150 return currentElement;
156 if (version != list._version)
157 throw new InvalidOperationException ("List has changed.");
159 currentElement = endFlag;
166 #region ArrayListAdapter
169 /// Adapts various ILists into an ArrayList.
172 private sealed class ArrayListAdapter
175 private sealed class EnumeratorWithRange
176 : IEnumerator, ICloneable
178 private int m_StartIndex;
180 private int m_MaxCount;
181 private IEnumerator m_Enumerator;
183 public EnumeratorWithRange(IEnumerator enumerator, int index, int count)
186 m_StartIndex = index;
188 m_Enumerator = enumerator;
193 public object Clone()
195 return this.MemberwiseClone();
198 public object Current
202 return m_Enumerator.Current;
206 public bool MoveNext()
208 if (m_Count >= m_MaxCount)
215 return m_Enumerator.MoveNext();
221 m_Enumerator.Reset();
223 for (int i = 0; i < m_StartIndex; i++)
225 m_Enumerator.MoveNext();
230 private IList m_Adaptee;
232 public ArrayListAdapter(IList adaptee)
238 public override object this[int index]
242 return m_Adaptee[index];
247 m_Adaptee[index] = value;
251 public override int Count
255 return m_Adaptee.Count;
259 public override int Capacity
263 return m_Adaptee.Count;
268 if (value < m_Adaptee.Count)
270 throw new ArgumentException("capacity");
275 public override bool IsFixedSize
279 return m_Adaptee.IsFixedSize;
283 public override bool IsReadOnly
287 return m_Adaptee.IsReadOnly;
291 public override object SyncRoot
295 return m_Adaptee.SyncRoot;
299 public override int Add(object value)
301 return m_Adaptee.Add(value);
304 public override void Clear()
309 public override bool Contains(object value)
311 return m_Adaptee.Contains(value);
314 public override int IndexOf(object value)
316 return m_Adaptee.IndexOf(value);
319 public override int IndexOf(object value, int startIndex)
321 return IndexOf(value, startIndex, m_Adaptee.Count - startIndex);
324 public override int IndexOf(object value, int startIndex, int count)
326 if (startIndex < 0 || startIndex > m_Adaptee.Count)
328 throw new ArgumentOutOfRangeException("startIndex", startIndex,
329 "Does not specify valid index.");
334 throw new ArgumentOutOfRangeException("count", count,
335 "Can't be less than 0.");
338 if (startIndex + count > m_Adaptee.Count)
340 // LAMESPEC: Every other method throws ArgumentException
342 throw new ArgumentOutOfRangeException("count",
343 "Start index and count do not specify a valid range.");
348 for (int i = startIndex; i < startIndex + count; i++)
350 if (m_Adaptee[i] == null)
358 for (int i = startIndex; i < startIndex + count; i++)
360 if (value.Equals(m_Adaptee[i]))
370 public override int LastIndexOf(object value)
372 return LastIndexOf(value, m_Adaptee.Count - 1);
375 public override int LastIndexOf(object value, int startIndex)
377 return LastIndexOf(value, startIndex, startIndex + 1);
380 public override int LastIndexOf(object value, int startIndex, int count)
382 if (startIndex < 0 || startIndex > m_Adaptee.Count - 1)
384 throw new ArgumentOutOfRangeException("startIndex", startIndex,
385 "startIndex must be within the list.");
390 throw new ArgumentOutOfRangeException("count", count, "count is negative.");
393 if (startIndex - count + 1 < 0)
395 throw new ArgumentOutOfRangeException("count", count, "count is too large.");
400 for (int i = startIndex; i > startIndex - count; i--)
402 if (m_Adaptee[i] == null)
410 for (int i = startIndex; i > startIndex - count; i--)
412 if (value.Equals(m_Adaptee[i]))
422 public override void Insert(int index, object value)
424 m_Adaptee.Insert(index, value);
427 public override void InsertRange(int index, ICollection c)
431 throw new ArgumentNullException("c");
434 if (index > m_Adaptee.Count)
436 throw new ArgumentOutOfRangeException("index", index,
437 "Index must be >= 0 and <= Count.");
440 foreach (object value in c)
442 m_Adaptee.Insert(index++, value);
446 public override void Remove(object value)
448 m_Adaptee.Remove(value);
451 public override void RemoveAt(int index)
453 m_Adaptee.RemoveAt(index);
456 public override void RemoveRange(int index, int count)
458 CheckRange(index, count, m_Adaptee.Count);
460 for (int i = 0; i < count; i++)
462 m_Adaptee.RemoveAt(index);
466 public override void Reverse()
468 Reverse(0, m_Adaptee.Count);
471 public override void Reverse(int index, int count)
475 CheckRange(index, count, m_Adaptee.Count);
477 for (int i = 0; i < count / 2; i++)
479 tmp = m_Adaptee[i + index];
480 m_Adaptee[i + index] = m_Adaptee[(index + count) - i + index - 1];
481 m_Adaptee[(index + count) - i + index - 1] = tmp;
485 public override void SetRange(int index, ICollection c)
489 throw new ArgumentNullException("c");
492 if (index < 0 || index + c.Count > m_Adaptee.Count)
494 throw new ArgumentOutOfRangeException("index");
499 foreach (object value in c)
501 m_Adaptee[x++] = value;
505 public override void CopyTo(System.Array array)
507 m_Adaptee.CopyTo(array, 0);
510 public override void CopyTo(System.Array array, int index)
512 m_Adaptee.CopyTo(array, index);
515 public override void CopyTo(int index, System.Array array, int arrayIndex, int count)
519 throw new ArgumentOutOfRangeException("index", index,
520 "Can't be less than zero.");
525 throw new ArgumentOutOfRangeException("arrayIndex", arrayIndex,
526 "Can't be less than zero.");
531 throw new ArgumentOutOfRangeException("index", index,
532 "Can't be less than zero.");
535 if (index >= m_Adaptee.Count)
537 throw new ArgumentException("Can't be more or equal to list count.",
543 throw new ArgumentException("Can't copy into multi-dimensional array.");
546 if (arrayIndex >= array.Length)
548 throw new ArgumentException("arrayIndex can't be greater than array.Length - 1.");
551 if (array.Length - arrayIndex + 1 < count)
553 throw new ArgumentException("Destination array is too small.");
556 if (index + count > m_Adaptee.Count)
558 throw new ArgumentException("Index and count do not denote a valid range of elements.", "index");
561 for (int i = 0; i < count; i++)
563 array.SetValue(m_Adaptee[index + i], arrayIndex + i);
567 public override bool IsSynchronized
571 return m_Adaptee.IsSynchronized;
575 public override IEnumerator GetEnumerator()
577 return m_Adaptee.GetEnumerator();
580 public override IEnumerator GetEnumerator(int index, int count)
582 CheckRange(index, count, m_Adaptee.Count);
584 return new EnumeratorWithRange(m_Adaptee.GetEnumerator(), index, count);
587 public override void AddRange(ICollection c)
589 foreach (object value in c)
591 m_Adaptee.Add(value);
595 public override int BinarySearch(object value)
597 return BinarySearch(value, null);
600 public override int BinarySearch(object value, IComparer comparer)
602 return BinarySearch(0, m_Adaptee.Count, value, comparer);
605 public override int BinarySearch(int index, int count, object value, IComparer comparer)
609 // Doing a direct BinarySearch on the adaptee will perform poorly if the adaptee is a linked-list.
610 // Alternatives include copying the adaptee to a temporary array first.
612 CheckRange(index, count, m_Adaptee.Count);
614 if (comparer == null)
616 comparer = Comparer.Default;
620 y = index + count - 1;
626 r = comparer.Compare(value, m_Adaptee[z]);
645 public override object Clone()
647 return new ArrayList.ArrayListAdapter(m_Adaptee);
650 public override ArrayList GetRange(int index, int count)
652 CheckRange(index, count, m_Adaptee.Count);
654 return new RangedArrayList(this, index, count);
657 public override void TrimToSize()
662 public override void Sort()
664 Sort(Comparer.Default);
667 public override void Sort(IComparer comparer)
669 Sort(0, m_Adaptee.Count, comparer);
672 public override void Sort(int index, int count, IComparer comparer)
674 CheckRange(index, count, m_Adaptee.Count);
676 if (comparer == null)
678 comparer = Comparer.Default;
681 // Doing a direct sort on the adaptee will perform poorly if the adaptee is a linked-list.
682 // Alternatives include copying the adaptee into a temporary array first.
684 QuickSort(m_Adaptee, index, index + count - 1, comparer);
689 /// Swaps two items in a list at the specified indexes.
691 private static void Swap(IList list, int x, int y)
701 /// Quicksort for lists.
704 /// This function acts as both qsort() and partition().
706 internal static void QuickSort(IList list, int left, int right, IComparer comparer)
716 // Pick the pivot using the median-of-three strategy.
718 middle = (left + right) / 2;
720 if (comparer.Compare(list[middle], list[left]) < 0)
722 Swap(list, middle, left);
725 if (comparer.Compare(list[right], list[left]) < 0)
727 Swap(list, right, left);
730 if (comparer.Compare(list[right], list[middle]) < 0)
732 Swap(list, right, middle);
735 if (right - left + 1 <= 3)
740 // Put the pivot in right - 1.
741 Swap(list, right - 1, middle);
743 // List should look like:
745 // [Small] ..Numbers.. [Middle] ..Numbers.. [Pivot][Large]
747 pivot = list[right - 1];
749 // Sort from (left + 1) to (right - 2).
756 while (comparer.Compare(list[++i], pivot) < 0);
757 while (comparer.Compare(list[--j], pivot) > 0);
769 // Put pivot into the right position (real middle).
771 Swap(list, right - 1, i);
773 // Recursively sort the left and right sub lists.
775 QuickSort(list, left, i - 1, comparer);
776 QuickSort(list, i + 1, right, comparer);
779 public override object[] ToArray()
783 retval = new object[m_Adaptee.Count];
785 m_Adaptee.CopyTo(retval, 0);
790 public override Array ToArray(Type elementType)
794 retval = Array.CreateInstance(elementType, m_Adaptee.Count);
796 m_Adaptee.CopyTo(retval, 0);
802 #endregion // ArrayListAdapter
805 // ArrayList wrappers
808 #region ArrayListWrapper
811 /// Base wrapper/decorator for ArrayLists. Simply delegates all methods to
812 /// the underlying wrappee.
815 private class ArrayListWrapper
818 protected ArrayList m_InnerArrayList;
822 public ArrayListWrapper(ArrayList innerArrayList)
824 m_InnerArrayList = innerArrayList;
831 public override object this[int index]
835 return m_InnerArrayList[index];
840 m_InnerArrayList[index] = value;
848 public override int Count
852 return m_InnerArrayList.Count;
856 public override int Capacity
860 return m_InnerArrayList.Capacity;
865 m_InnerArrayList.Capacity = value;
869 public override bool IsFixedSize
873 return m_InnerArrayList.IsFixedSize;
877 public override bool IsReadOnly
881 return m_InnerArrayList.IsReadOnly;
885 public override bool IsSynchronized
889 return m_InnerArrayList.IsSynchronized;
893 public override object SyncRoot
897 return m_InnerArrayList.SyncRoot;
905 public override int Add(object value)
907 return m_InnerArrayList.Add(value);
910 public override void Clear()
912 m_InnerArrayList.Clear();
915 public override bool Contains(object value)
917 return m_InnerArrayList.Contains(value);
920 public override int IndexOf(object value)
922 return m_InnerArrayList.IndexOf(value);
925 public override int IndexOf(object value, int startIndex)
927 return m_InnerArrayList.IndexOf(value, startIndex);
930 public override int IndexOf(object value, int startIndex, int count)
932 return m_InnerArrayList.IndexOf(value, startIndex, count);
935 public override int LastIndexOf(object value)
937 return m_InnerArrayList.LastIndexOf(value);
940 public override int LastIndexOf(object value, int startIndex)
942 return m_InnerArrayList.LastIndexOf(value, startIndex);
945 public override int LastIndexOf(object value, int startIndex, int count)
947 return m_InnerArrayList.LastIndexOf(value, startIndex, count);
950 public override void Insert(int index, object value)
952 m_InnerArrayList.Insert(index, value);
955 public override void InsertRange(int index, ICollection c)
957 m_InnerArrayList.InsertRange(index, c);
960 public override void Remove(object value)
962 m_InnerArrayList.Remove(value);
965 public override void RemoveAt(int index)
967 m_InnerArrayList.RemoveAt(index);
970 public override void RemoveRange(int index, int count)
972 m_InnerArrayList.RemoveRange(index, count);
975 public override void Reverse()
977 m_InnerArrayList.Reverse();
980 public override void Reverse(int index, int count)
982 m_InnerArrayList.Reverse(index, count);
985 public override void SetRange(int index, ICollection c)
987 m_InnerArrayList.SetRange(index, c);
990 public override void CopyTo(System.Array array)
992 m_InnerArrayList.CopyTo(array);
995 public override void CopyTo(System.Array array, int index)
997 m_InnerArrayList.CopyTo(array, index);
1000 public override void CopyTo(int index, System.Array array, int arrayIndex, int count)
1002 m_InnerArrayList.CopyTo(index, array, arrayIndex, count);
1005 public override IEnumerator GetEnumerator()
1007 return m_InnerArrayList.GetEnumerator();
1010 public override IEnumerator GetEnumerator(int index, int count)
1012 return m_InnerArrayList.GetEnumerator(index, count);
1015 public override void AddRange(ICollection c)
1017 m_InnerArrayList.AddRange(c);
1020 public override int BinarySearch(object value)
1022 return m_InnerArrayList.BinarySearch(value);
1025 public override int BinarySearch(object value, IComparer comparer)
1027 return m_InnerArrayList.BinarySearch(value, comparer);
1030 public override int BinarySearch(int index, int count, object value, IComparer comparer)
1032 return m_InnerArrayList.BinarySearch(index, count, value, comparer);
1035 public override object Clone()
1037 return m_InnerArrayList.Clone();
1040 public override ArrayList GetRange(int index, int count)
1042 return m_InnerArrayList.GetRange(index, count);
1045 public override void TrimToSize()
1047 m_InnerArrayList.TrimToSize();
1050 public override void Sort()
1052 m_InnerArrayList.Sort();
1055 public override void Sort(IComparer comparer)
1057 m_InnerArrayList.Sort(comparer);
1060 public override void Sort(int index, int count, IComparer comparer)
1062 m_InnerArrayList.Sort(index, count, comparer);
1065 public override object[] ToArray()
1067 return m_InnerArrayList.ToArray();
1070 public override Array ToArray(Type elementType)
1072 return m_InnerArrayList.ToArray(elementType);
1080 #region SynchronizedArrayListWrapper
1083 /// ArrayListWrapper that synchronizes calls to all methods/properties.
1086 /// Works by just synchronizing all method calls. In the future careful optimisation
1087 /// could give better performance...
1090 private sealed class SynchronizedArrayListWrapper
1093 private object m_SyncRoot;
1095 #region Constructors
1098 /// Creates a new synchronized wrapper for the given <see cref="ArrayList"/>.
1100 /// <param name="innerArrayList"></param>
1101 internal SynchronizedArrayListWrapper(ArrayList innerArrayList)
1102 : base(innerArrayList)
1104 m_SyncRoot = innerArrayList.SyncRoot;
1111 public override object this[int index]
1117 return m_InnerArrayList[index];
1125 m_InnerArrayList[index] = value;
1134 // Some of these properties may be calculated so it's best to synchronize
1135 // them even though it might cause a performance hit.
1136 // Better safe than sorry ;D.
1138 public override int Count
1144 return m_InnerArrayList.Count;
1149 public override int Capacity
1155 return m_InnerArrayList.Capacity;
1163 m_InnerArrayList.Capacity = value;
1168 public override bool IsFixedSize
1174 return m_InnerArrayList.IsFixedSize;
1179 public override bool IsReadOnly
1185 return m_InnerArrayList.IsReadOnly;
1190 public override bool IsSynchronized
1198 public override object SyncRoot
1210 public override int Add(object value)
1214 return m_InnerArrayList.Add(value);
1218 public override void Clear()
1222 m_InnerArrayList.Clear();
1226 public override bool Contains(object value)
1230 return m_InnerArrayList.Contains(value);
1234 public override int IndexOf(object value)
1238 return m_InnerArrayList.IndexOf(value);
1242 public override int IndexOf(object value, int startIndex)
1246 return m_InnerArrayList.IndexOf(value, startIndex);
1250 public override int IndexOf(object value, int startIndex, int count)
1254 return m_InnerArrayList.IndexOf(value, startIndex, count);
1258 public override int LastIndexOf(object value)
1262 return m_InnerArrayList.LastIndexOf(value);
1266 public override int LastIndexOf(object value, int startIndex)
1270 return m_InnerArrayList.LastIndexOf(value, startIndex);
1274 public override int LastIndexOf(object value, int startIndex, int count)
1278 return m_InnerArrayList.LastIndexOf(value, startIndex, count);
1282 public override void Insert(int index, object value)
1286 m_InnerArrayList.Insert(index, value);
1290 public override void InsertRange(int index, ICollection c)
1294 m_InnerArrayList.InsertRange(index, c);
1298 public override void Remove(object value)
1302 m_InnerArrayList.Remove(value);
1306 public override void RemoveAt(int index)
1310 m_InnerArrayList.RemoveAt(index);
1314 public override void RemoveRange(int index, int count)
1318 m_InnerArrayList.RemoveRange(index, count);
1322 public override void Reverse()
1326 m_InnerArrayList.Reverse();
1330 public override void Reverse(int index, int count)
1334 m_InnerArrayList.Reverse(index, count);
1338 public override void CopyTo(System.Array array)
1342 m_InnerArrayList.CopyTo(array);
1346 public override void CopyTo(System.Array array, int index)
1350 m_InnerArrayList.CopyTo(array, index);
1354 public override void CopyTo(int index, System.Array array, int arrayIndex, int count)
1358 m_InnerArrayList.CopyTo(index, array, arrayIndex, count);
1362 public override IEnumerator GetEnumerator()
1366 return m_InnerArrayList.GetEnumerator();
1370 public override IEnumerator GetEnumerator(int index, int count)
1374 return m_InnerArrayList.GetEnumerator(index, count);
1378 public override void AddRange(ICollection c)
1382 m_InnerArrayList.AddRange(c);
1386 public override int BinarySearch(object value)
1390 return m_InnerArrayList.BinarySearch(value);
1394 public override int BinarySearch(object value, IComparer comparer)
1398 return m_InnerArrayList.BinarySearch(value, comparer);
1402 public override int BinarySearch(int index, int count, object value, IComparer comparer)
1406 return m_InnerArrayList.BinarySearch(index, count, value, comparer);
1410 public override object Clone()
1414 return m_InnerArrayList.Clone();
1418 public override ArrayList GetRange(int index, int count)
1422 return m_InnerArrayList.GetRange(index, count);
1426 public override void TrimToSize()
1430 m_InnerArrayList.TrimToSize();
1434 public override void Sort()
1438 m_InnerArrayList.Sort();
1442 public override void Sort(IComparer comparer)
1446 m_InnerArrayList.Sort(comparer);
1450 public override void Sort(int index, int count, IComparer comparer)
1454 m_InnerArrayList.Sort(index, count, comparer);
1458 public override object[] ToArray()
1462 return m_InnerArrayList.ToArray();
1466 public override Array ToArray(Type elementType)
1470 return m_InnerArrayList.ToArray(elementType);
1479 #region FixedSizeArrayListWrapper
1482 private class FixedSizeArrayListWrapper
1485 #region Constructors
1487 public FixedSizeArrayListWrapper(ArrayList innerList)
1497 /// Gets the error message to display when an readonly/fixedsize related exception is
1500 protected virtual string ErrorMessage
1504 return "Can't add or remove from a fixed-size list.";
1508 public override int Capacity
1512 return base.Capacity;
1517 throw new NotSupportedException(this.ErrorMessage);
1521 public override bool IsFixedSize
1533 public override int Add(object value)
1535 throw new NotSupportedException(this.ErrorMessage);
1538 public override void AddRange(ICollection c)
1540 throw new NotSupportedException(this.ErrorMessage);
1543 public override void Clear()
1545 throw new NotSupportedException(this.ErrorMessage);
1548 public override void Insert(int index, object value)
1550 throw new NotSupportedException(this.ErrorMessage);
1553 public override void InsertRange(int index, ICollection c)
1555 throw new NotSupportedException(this.ErrorMessage);
1558 public override void Remove(object value)
1560 throw new NotSupportedException(this.ErrorMessage);
1563 public override void RemoveAt(int index)
1565 throw new NotSupportedException(this.ErrorMessage);
1568 public override void RemoveRange(int index, int count)
1570 throw new NotSupportedException(this.ErrorMessage);
1573 public override void TrimToSize()
1575 throw new NotSupportedException(this.ErrorMessage);
1583 #region ReadOnlyArrayListWrapper
1586 private sealed class ReadOnlyArrayListWrapper
1587 : FixedSizeArrayListWrapper
1589 protected override string ErrorMessage
1593 return "Can't modify a readonly list.";
1597 public override bool IsReadOnly
1605 public ReadOnlyArrayListWrapper(ArrayList innerArrayList)
1606 : base(innerArrayList)
1610 public override object this[int index]
1614 return m_InnerArrayList[index];
1619 throw new NotSupportedException(this.ErrorMessage);
1623 public override void Reverse()
1625 throw new NotSupportedException(this.ErrorMessage);
1628 public override void Reverse(int index, int count)
1630 throw new NotSupportedException(this.ErrorMessage);
1633 public override void SetRange(int index, ICollection c)
1635 throw new NotSupportedException(this.ErrorMessage);
1638 public override void Sort()
1640 throw new NotSupportedException(this.ErrorMessage);
1643 public override void Sort(IComparer comparer)
1645 throw new NotSupportedException(this.ErrorMessage);
1648 public override void Sort(int index, int count, IComparer comparer)
1650 throw new NotSupportedException(this.ErrorMessage);
1656 #region RangedArrayList
1659 private sealed class RangedArrayList
1662 private int m_InnerIndex;
1663 private int m_InnerCount;
1664 private int m_InnerStateChanges;
1666 public RangedArrayList(ArrayList innerList, int index, int count)
1669 m_InnerIndex = index;
1670 m_InnerCount = count;
1671 m_InnerStateChanges = innerList._version;
1676 public override bool IsSynchronized
1684 public override object this[int index]
1688 if (index < 0 || index > m_InnerCount)
1690 throw new ArgumentOutOfRangeException("index");
1693 return m_InnerArrayList[m_InnerIndex + index];
1698 if (index < 0 || index > m_InnerCount)
1700 throw new ArgumentOutOfRangeException("index");
1703 m_InnerArrayList[m_InnerIndex + index] = value;
1711 public override int Count
1715 VerifyStateChanges();
1717 return m_InnerCount;
1721 public override int Capacity
1725 return m_InnerArrayList.Capacity;
1730 if (value < m_InnerCount)
1732 throw new ArgumentOutOfRangeException();
1741 private void VerifyStateChanges()
1743 if (m_InnerStateChanges != m_InnerArrayList._version)
1745 throw new InvalidOperationException
1746 ("ArrayList view is invalid because the underlying ArrayList was modified.");
1750 public override int Add(object value)
1752 VerifyStateChanges();
1754 m_InnerArrayList.Insert(m_InnerIndex + m_InnerCount, value);
1756 m_InnerStateChanges = m_InnerArrayList._version;
1758 return ++m_InnerCount;
1761 public override void Clear()
1763 VerifyStateChanges();
1765 m_InnerArrayList.RemoveRange(m_InnerIndex, m_InnerCount);
1768 m_InnerStateChanges = m_InnerArrayList._version;
1771 public override bool Contains(object value)
1773 return m_InnerArrayList.Contains(value, m_InnerIndex, m_InnerCount);
1776 public override int IndexOf(object value)
1778 return IndexOf(value, 0);
1781 public override int IndexOf(object value, int startIndex)
1783 return IndexOf(value, startIndex, m_InnerCount - startIndex);
1786 public override int IndexOf(object value, int startIndex, int count)
1788 if (startIndex < 0 || startIndex > m_InnerCount)
1790 throw new ArgumentOutOfRangeException("startIndex", startIndex,
1791 "Does not specify valid index.");
1796 throw new ArgumentOutOfRangeException("count", count,
1797 "Can't be less than 0.");
1800 if (startIndex + count > m_InnerCount)
1802 // LAMESPEC: Every other method throws ArgumentException
1804 throw new ArgumentOutOfRangeException("count",
1805 "Start index and count do not specify a valid range.");
1808 int retval = m_InnerArrayList.IndexOf(value, m_InnerIndex + startIndex, count);
1816 return retval - m_InnerIndex;
1820 public override int LastIndexOf(object value)
1822 return LastIndexOf(value, m_InnerCount - 1);
1825 public override int LastIndexOf(object value, int startIndex)
1827 return LastIndexOf(value, startIndex, startIndex + 1);
1830 public override int LastIndexOf(object value, int startIndex, int count)
1832 if (startIndex < 0 || startIndex > m_InnerCount - 1)
1834 throw new ArgumentOutOfRangeException("startIndex", startIndex,
1835 "index must be within the list.");
1840 throw new ArgumentOutOfRangeException("count", count, "count is negative.");
1843 if (startIndex - count + 1 < 0)
1845 throw new ArgumentOutOfRangeException("count", count, "count too large.");
1848 int retval = m_InnerArrayList.LastIndexOf(value, m_InnerIndex + startIndex, count);
1856 return retval - m_InnerIndex;
1860 public override void Insert(int index, object value)
1862 VerifyStateChanges();
1864 if (index < 0 || index > m_InnerCount)
1866 throw new ArgumentOutOfRangeException("index", index,
1867 "Index must be >= 0 and <= Count.");
1870 m_InnerArrayList.Insert(m_InnerIndex + index, value);
1874 m_InnerStateChanges = m_InnerArrayList._version;
1877 public override void InsertRange(int index, ICollection c)
1879 VerifyStateChanges();
1881 if (index < 0 || index > m_InnerCount)
1883 throw new ArgumentOutOfRangeException("index", index,
1884 "Index must be >= 0 and <= Count.");
1887 m_InnerArrayList.InsertRange(m_InnerIndex + index, c);
1889 m_InnerCount += c.Count;
1891 m_InnerStateChanges = m_InnerArrayList._version;
1894 public override void Remove(object value)
1896 VerifyStateChanges();
1898 int x = IndexOf(value);
1905 m_InnerStateChanges = m_InnerArrayList._version;
1908 public override void RemoveAt(int index)
1910 VerifyStateChanges();
1912 if (index < 0 || index > m_InnerCount)
1914 throw new ArgumentOutOfRangeException("index", index,
1915 "Index must be >= 0 and <= Count.");
1918 m_InnerArrayList.RemoveAt(m_InnerIndex + index);
1921 m_InnerStateChanges = m_InnerArrayList._version;
1924 public override void RemoveRange(int index, int count)
1926 VerifyStateChanges();
1928 CheckRange(index, count, m_InnerCount);
1930 m_InnerArrayList.RemoveRange(m_InnerIndex + index, count);
1932 m_InnerCount -= count;
1934 m_InnerStateChanges = m_InnerArrayList._version;
1937 public override void Reverse()
1939 Reverse(0, m_InnerCount);
1942 public override void Reverse(int index, int count)
1944 VerifyStateChanges();
1946 CheckRange(index, count, m_InnerCount);
1948 m_InnerArrayList.Reverse(m_InnerIndex + index, count);
1950 m_InnerStateChanges = m_InnerArrayList._version;
1953 public override void SetRange(int index, ICollection c)
1955 VerifyStateChanges();
1957 if (index < 0 || index > m_InnerCount)
1959 throw new ArgumentOutOfRangeException("index", index,
1960 "Index must be >= 0 and <= Count.");
1963 m_InnerArrayList.SetRange(m_InnerIndex + index, c);
1965 m_InnerStateChanges = m_InnerArrayList._version;
1968 public override void CopyTo(System.Array array)
1973 public override void CopyTo(System.Array array, int index)
1975 CopyTo(0, array, index, m_InnerCount);
1978 public override void CopyTo(int index, System.Array array, int arrayIndex, int count)
1980 CheckRange(index, count, m_InnerCount);
1982 m_InnerArrayList.CopyTo(m_InnerIndex + index, array, arrayIndex, count);
1985 public override IEnumerator GetEnumerator()
1987 return GetEnumerator(0, m_InnerCount);
1990 public override IEnumerator GetEnumerator(int index, int count)
1992 CheckRange(index, count, m_InnerCount);
1994 return m_InnerArrayList.GetEnumerator(m_InnerIndex + index, count);
1997 public override void AddRange(ICollection c)
1999 VerifyStateChanges();
2001 m_InnerArrayList.InsertRange(m_InnerCount, c);
2003 m_InnerCount += c.Count;
2005 m_InnerStateChanges = m_InnerArrayList._version;
2008 public override int BinarySearch(object value)
2010 return BinarySearch(0, m_InnerCount, value, Comparer.Default);
2013 public override int BinarySearch(object value, IComparer comparer)
2015 return BinarySearch(0, m_InnerCount, value, comparer);
2018 public override int BinarySearch(int index, int count, object value, IComparer comparer)
2020 CheckRange(index, count, m_InnerCount);
2022 return m_InnerArrayList.BinarySearch(m_InnerIndex + index, count, value, comparer);
2025 public override object Clone()
2027 return new RangedArrayList((ArrayList)m_InnerArrayList.Clone(), m_InnerIndex, m_InnerCount);
2030 public override ArrayList GetRange(int index, int count)
2032 CheckRange(index, count, m_InnerCount);
2034 return new RangedArrayList(this, index, count);
2037 public override void TrimToSize()
2039 throw new NotSupportedException();
2042 public override void Sort()
2044 Sort(Comparer.Default);
2047 public override void Sort(IComparer comparer)
2049 Sort(0, m_InnerCount, comparer);
2052 public override void Sort(int index, int count, IComparer comparer)
2054 VerifyStateChanges();
2056 CheckRange(index, count, m_InnerCount);
2058 m_InnerArrayList.Sort(m_InnerIndex + index, count, comparer);
2060 m_InnerStateChanges = m_InnerArrayList._version;
2063 public override object[] ToArray()
2067 array = new object[m_InnerCount];
2069 m_InnerArrayList.CopyTo(0, array, 0, m_InnerCount);
2074 public override Array ToArray(Type elementType)
2078 array = Array.CreateInstance(elementType, m_InnerCount);
2080 m_InnerArrayList.CopyTo(0, array, 0, m_InnerCount);
2094 #region SynchronizedListWrapper
2097 private sealed class SynchronizedListWrapper
2100 private object m_SyncRoot;
2102 public SynchronizedListWrapper(IList innerList)
2105 m_SyncRoot = innerList.SyncRoot;
2108 public override int Count
2114 return m_InnerList.Count;
2119 public override bool IsSynchronized
2127 public override object SyncRoot
2133 return m_InnerList.SyncRoot;
2138 public override bool IsFixedSize
2144 return m_InnerList.IsFixedSize;
2149 public override bool IsReadOnly
2155 return m_InnerList.IsReadOnly;
2160 public override object this[int index]
2166 return m_InnerList[index];
2174 m_InnerList[index] = value;
2179 public override int Add(object value)
2183 return m_InnerList.Add(value);
2187 public override void Clear()
2191 m_InnerList.Clear();
2195 public override bool Contains(object value)
2199 return m_InnerList.Contains(value);
2203 public override int IndexOf(object value)
2207 return m_InnerList.IndexOf(value);
2211 public override void Insert(int index, object value)
2215 m_InnerList.Insert(index, value);
2219 public override void Remove(object value)
2223 m_InnerList.Remove(value);
2227 public override void RemoveAt(int index)
2231 m_InnerList.RemoveAt(index);
2235 public override void CopyTo(Array array, int index)
2239 m_InnerList.CopyTo(array, index);
2243 public override IEnumerator GetEnumerator()
2247 return m_InnerList.GetEnumerator();
2254 #region FixedSizeListWrapper
2257 private class FixedSizeListWrapper
2260 protected virtual string ErrorMessage
2264 return "List is fixed-size.";
2268 public override bool IsFixedSize
2276 public FixedSizeListWrapper(IList innerList)
2281 public override int Add(object value)
2283 throw new NotSupportedException(this.ErrorMessage);
2286 public override void Clear()
2288 throw new NotSupportedException(this.ErrorMessage);
2291 public override void Insert(int index, object value)
2293 throw new NotSupportedException(this.ErrorMessage);
2296 public override void Remove(object value)
2298 throw new NotSupportedException(this.ErrorMessage);
2301 public override void RemoveAt(int index)
2303 throw new NotSupportedException(this.ErrorMessage);
2309 #region ReadOnlyListWrapper
2312 private sealed class ReadOnlyListWrapper
2313 : FixedSizeListWrapper
2315 protected override string ErrorMessage
2319 return "List is read-only.";
2323 public override bool IsReadOnly
2331 public ReadOnlyListWrapper(IList innerList)
2336 public override object this[int index]
2340 return m_InnerList[index];
2345 throw new NotSupportedException(this.ErrorMessage);
2355 /// Decorates/Wraps any <c>IList</c> implementing object.
2358 private class ListWrapper
2363 protected IList m_InnerList;
2367 #region Constructors
2369 public ListWrapper(IList innerList)
2371 m_InnerList = innerList;
2378 public virtual object this[int index]
2382 return m_InnerList[index];
2387 m_InnerList[index] = value;
2395 public virtual int Count
2399 return m_InnerList.Count;
2403 public virtual bool IsSynchronized
2407 return m_InnerList.IsSynchronized;
2411 public virtual object SyncRoot
2415 return m_InnerList.SyncRoot;
2419 public virtual bool IsFixedSize
2423 return m_InnerList.IsFixedSize;
2427 public virtual bool IsReadOnly
2431 return m_InnerList.IsReadOnly;
2439 public virtual int Add(object value)
2441 return m_InnerList.Add(value);
2444 public virtual void Clear()
2446 m_InnerList.Clear();
2449 public virtual bool Contains(object value)
2451 return m_InnerList.Contains(value);
2454 public virtual int IndexOf(object value)
2456 return m_InnerList.IndexOf(value);
2459 public virtual void Insert(int index, object value)
2461 m_InnerList.Insert(index, value);
2464 public virtual void Remove(object value)
2466 m_InnerList.Remove(value);
2469 public virtual void RemoveAt(int index)
2471 m_InnerList.RemoveAt(index);
2474 public virtual void CopyTo(Array array, int index)
2476 m_InnerList.CopyTo(array, index);
2479 public virtual IEnumerator GetEnumerator()
2481 return m_InnerList.GetEnumerator();
2490 // Start of ArrayList
2495 private const int DefaultInitialCapacity = 0x10;
2498 /// Number of items in the list.
2503 /// Array to store the items.
2505 private object[] _items;
2508 /// Total number of state changes.
2510 private int _version;
2514 #region Constructors
2517 /// Initializes a new instance of the <see cref="ArrayList"/> class that is empty and
2518 /// has the default initial capacity (16).
2522 _items = new object[DefaultInitialCapacity];
2526 /// Initializes a new instance of the <see cref="ArrayList"/> class that contains
2527 /// elements copied from the specified collection and that has the same initial capacity
2528 /// as the number of elements copied.
2530 /// <param name="c">
2531 /// The <see cref="ICollection"/> whose elements are copied into the new list.
2533 /// <exception cref="ArgumentNullException">
2534 /// The argument <c>c</c> is a null reference.
2536 public ArrayList(ICollection c)
2542 throw new ArgumentNullException("c");
2547 if (array != null && array.Rank != 1)
2549 throw new RankException();
2552 _items = new object[c.Count];
2558 /// Initializes a new instance of the <see cref="ArrayList"/> class that is empty and
2559 /// has the specified initial capacity.
2561 /// <param name="initialCapacity">
2562 /// The number of elements that hte new list is initially capable of storing.
2564 /// <exception cref="ArgumentOutOfRangeException">
2565 /// The <c>capacity</c> is less than zero.
2567 public ArrayList(int initialCapacity)
2569 if (initialCapacity < 0)
2571 throw new ArgumentOutOfRangeException("initialCapacity",
2572 initialCapacity, "The initial capacity can't be smaller than zero.");
2575 if (initialCapacity == 0)
2577 initialCapacity = DefaultInitialCapacity;
2579 _items = new object[initialCapacity];
2583 /// Used by ArrayListAdapter to allow creation of an ArrayList with no storage buffer.
2585 private ArrayList(int initialCapacity, bool forceZeroSize)
2593 throw new InvalidOperationException("Use ArrayList(int)");
2598 /// Initializes a new array list that contains a copy of the given array and with the
2601 /// <param name="array"></param>
2602 private ArrayList(object[] array, int index, int count)
2604 _items = new object[count];
2608 _items = new object[DefaultInitialCapacity];
2612 _items = new object[count];
2615 Array.Copy(array, index, _items, 0, count);
2625 /// Gets/Sets an element in the list by index.
2627 /// <exception cref="ArgumentOutOfRangeException">
2628 /// The index is less than 0 or more then or equal to the list count.
2630 public virtual object this[int index]
2634 if (index < 0 || index >= _size)
2636 throw new ArgumentOutOfRangeException("index", index,
2637 "Index is less than 0 or more than or equal to the list count.");
2640 return _items[index];
2645 if (index < 0 || index >= _size)
2647 throw new ArgumentOutOfRangeException("index", index,
2648 "Index is less than 0 or more than or equal to the list count.");
2651 _items[index] = value;
2661 /// Gets the number of elements in the list.
2663 public virtual int Count
2672 /// Gets the number of elements the list can carry without needing to expand.
2675 /// ArrayLists automatically double their capacity when the capacity limit is broken.
2677 /// <exception cref="ArgumentOutOfRangeException">
2678 /// The capacity is less than the count.
2680 public virtual int Capacity
2684 return _items.Length;
2691 throw new ArgumentOutOfRangeException("Capacity", value,
2692 "Must be more than count.");
2697 newArray = new object[value];
2699 Array.Copy(_items, 0, newArray, 0, _size);
2706 /// <see cref="IList.IsFixedSize"/>
2709 public virtual bool IsFixedSize
2718 /// <see cref="IList.IsReadOnly"/>
2720 public virtual bool IsReadOnly
2729 /// <see cref="ICollection.IsSynchronized"/>
2731 public virtual bool IsSynchronized
2740 /// <see cref="ICollection.SyncRoot"/>
2742 public virtual object SyncRoot
2755 /// Ensures that the list has the capacity to contain the given <c>count</c> by
2756 /// automatically expanding the capacity when required.
2758 private void EnsureCapacity(int count)
2760 if (count <= _items.Length)
2768 newLength = _items.Length << 1;
2770 newLength = DefaultInitialCapacity;
2772 while (newLength < count)
2777 newData = new object[newLength];
2779 Array.Copy(_items, 0, newData, 0, _items.Length);
2785 /// Shifts a section of the list.
2787 /// <param name="index">
2788 /// The start of the section to shift (the element at index is included in the shift).
2790 /// <param name="count">
2791 /// The number of positions to shift by (can be negative).
2793 private void Shift(int index, int count)
2797 if (_size + count > _items.Length)
2802 newLength = _items.Length << 1;
2804 while (newLength < _size + count)
2809 newData = new object[newLength];
2811 Array.Copy(_items, 0, newData, 0, index);
2812 Array.Copy(_items, index, newData, index + count, _size - index);
2818 Array.Copy(_items, index, _items, index + count, _size - index);
2823 // Remember count is negative so this is actually index + (-count)
2825 int x = index - count ;
2827 Array.Copy(_items, x, _items, index, _size - x);
2831 public virtual int Add(object value)
2833 // Do a check here in case EnsureCapacity isn't inlined.
2835 if (_items.Length <= _size /* same as _items.Length < _size + 1) */)
2837 EnsureCapacity(_size + 1);
2840 _items[_size] = value;
2847 public virtual void Clear()
2849 // Keep the array but null all members so they can be garbage collected.
2851 Array.Clear(_items, 0, _size);
2857 public virtual bool Contains(object value)
2859 return IndexOf(value, 0, _size) > -1;
2862 internal virtual bool Contains(object value, int startIndex, int count)
2864 return IndexOf(value, startIndex, count) > -1;
2867 public virtual int IndexOf(object value)
2869 return IndexOf(value, 0);
2872 public virtual int IndexOf(object value, int startIndex)
2874 return IndexOf(value, startIndex, _size - startIndex);
2877 public virtual int IndexOf(object value, int startIndex, int count)
2879 if (startIndex < 0 || startIndex > _size)
2881 throw new ArgumentOutOfRangeException("startIndex", startIndex,
2882 "Does not specify valid index.");
2887 throw new ArgumentOutOfRangeException("count", count,
2888 "Can't be less than 0.");
2891 if (startIndex + count > _size)
2893 // LAMESPEC: Every other method throws ArgumentException
2895 throw new ArgumentOutOfRangeException("count",
2896 "Start index and count do not specify a valid range.");
2899 return Array.IndexOf(_items, value, startIndex, count);
2902 public virtual int LastIndexOf(object value)
2904 return LastIndexOf(value, _size - 1);
2907 public virtual int LastIndexOf(object value, int startIndex)
2909 return LastIndexOf(value, startIndex, startIndex + 1);
2912 public virtual int LastIndexOf(object value, int startIndex, int count)
2914 if (startIndex < 0 || startIndex > _size - 1)
2916 throw new ArgumentOutOfRangeException("startIndex", startIndex,
2917 "index must be within the list.");
2922 throw new ArgumentOutOfRangeException("count", count, "count is negative.");
2925 if (startIndex - count + 1 < 0)
2927 throw new ArgumentOutOfRangeException("count", count, "count too large.");
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)
3148 throw new ArgumentNullException("c");
3151 if (index < 0 || index + c.Count > _size)
3153 throw new ArgumentOutOfRangeException("index");
3156 c.CopyTo(_items, index);
3161 public virtual void TrimToSize()
3163 if (_items.Length > _size)
3169 newArray = new object[DefaultInitialCapacity];
3173 newArray = new object[_size];
3176 Array.Copy(_items, 0, newArray, 0, _size);
3182 public virtual void Sort()
3184 Array.Sort(_items, 0, _size);
3189 public virtual void Sort(IComparer comparer)
3191 Array.Sort(_items, 0, _size, comparer);
3194 public virtual void Sort(int index, int count, IComparer comparer)
3196 ArrayList.CheckRange(index, count, _size);
3198 Array.Sort(_items, index, count, comparer);
3201 public virtual object[] ToArray()
3205 retval = new object[_size];
3212 public virtual Array ToArray(Type elementType)
3216 retval = Array.CreateInstance(elementType, _size);
3223 public virtual object Clone()
3225 return new ArrayList(this._items, 0, this._size);
3230 #region Static Methods
3233 /// Does a check of the arguments many of the methods in ArrayList use.
3236 /// The choice of exceptions thrown sometimes seem to be arbitrarily chosen so
3237 /// not all methods actually make use of CheckRange.
3239 internal static void CheckRange(int index, int count, int listCount)
3243 throw new ArgumentOutOfRangeException("index", index, "Can't be less than 0.");
3248 throw new ArgumentOutOfRangeException("count", count, "Can't be less than 0.");
3251 if (index + count > listCount)
3253 throw new ArgumentException("Index and count do not denote a valid range of elements.", "index");
3257 public static ArrayList Adapter(IList list)
3259 // LAMESPEC: EWWW. Other lists aren't *Array*Lists.
3263 throw new ArgumentNullException("list");
3266 if (list.IsSynchronized)
3268 return ArrayList.Synchronized(new ArrayListAdapter(list));
3272 return new ArrayListAdapter(list);
3276 public static ArrayList Synchronized(ArrayList arrayList)
3278 if (arrayList == null)
3280 throw new ArgumentNullException("arrayList");
3283 if (arrayList.IsSynchronized)
3288 return new SynchronizedArrayListWrapper(arrayList);
3291 public static IList Synchronized(IList list)
3295 throw new ArgumentNullException("list");
3298 if (list.IsSynchronized)
3303 return new SynchronizedListWrapper(list);
3306 public static ArrayList ReadOnly(ArrayList arrayList)
3308 if (arrayList == null)
3310 throw new ArgumentNullException("arrayList");
3313 if (arrayList.IsReadOnly)
3318 return new ReadOnlyArrayListWrapper(arrayList);
3321 public static IList ReadOnly(IList list)
3325 throw new ArgumentNullException("list");
3328 if (list.IsReadOnly)
3333 return new ReadOnlyListWrapper(list);
3336 public static ArrayList FixedSize(ArrayList arrayList)
3338 if (arrayList == null)
3340 throw new ArgumentNullException("arrayList");
3343 if (arrayList.IsFixedSize)
3348 return new FixedSizeArrayListWrapper(arrayList);
3351 public static IList FixedSize(IList list)
3355 throw new ArgumentNullException("list");
3358 if (list.IsFixedSize)
3363 return new FixedSizeListWrapper(list);
3366 public static ArrayList Repeat(object value, int count)
3368 ArrayList arrayList = new ArrayList(count);
3370 for (int i = 0; i < count; i++)
3372 arrayList.Add(value);