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
36 public class ArrayList
37 : IList, ICloneable, ICollection, IEnumerable
41 private sealed class ArrayListEnumerator
42 : IEnumerator, ICloneable
47 private object m_Current;
48 private ArrayList m_List;
49 private int m_ExpectedStateChanges;
51 public ArrayListEnumerator(ArrayList list)
52 : this(list, 0, list.Count)
58 return this.MemberwiseClone();
61 public ArrayListEnumerator(ArrayList list, int index, int count)
68 m_ExpectedStateChanges = list._version;
75 if (m_Pos == m_Index - 1) {
76 throw new InvalidOperationException("Enumerator unusable (Reset pending, or past end of array.");
83 public bool MoveNext()
85 if (m_List._version != m_ExpectedStateChanges)
87 throw new InvalidOperationException("List has changed.");
92 if (m_Pos - m_Index < m_Count)
94 m_Current = m_List[m_Pos];
109 sealed class SimpleEnumerator : IEnumerator, ICloneable
114 object currentElement;
115 static object endFlag = new object ();
117 public SimpleEnumerator (ArrayList list)
121 version = list._version;
122 currentElement = endFlag;
125 public object Clone ()
127 return MemberwiseClone ();
130 public bool MoveNext ()
132 if (version != list._version)
133 throw new InvalidOperationException("List has changed.");
135 if (++index < list.Count) {
136 currentElement = list [index];
139 currentElement = endFlag;
144 public object Current {
146 if (currentElement == endFlag) {
148 throw new InvalidOperationException ("Enumerator not started");
150 throw new InvalidOperationException ("Enumerator ended");
153 return currentElement;
159 if (version != list._version)
160 throw new InvalidOperationException ("List has changed.");
162 currentElement = endFlag;
169 #region ArrayListAdapter
172 /// Adapts various ILists into an ArrayList.
175 private sealed class ArrayListAdapter
178 private sealed class EnumeratorWithRange
179 : IEnumerator, ICloneable
181 private int m_StartIndex;
183 private int m_MaxCount;
184 private IEnumerator m_Enumerator;
186 public EnumeratorWithRange(IEnumerator enumerator, int index, int count)
189 m_StartIndex = index;
191 m_Enumerator = enumerator;
196 public object Clone()
198 return this.MemberwiseClone();
201 public object Current
205 return m_Enumerator.Current;
209 public bool MoveNext()
211 if (m_Count >= m_MaxCount)
218 return m_Enumerator.MoveNext();
224 m_Enumerator.Reset();
226 for (int i = 0; i < m_StartIndex; i++)
228 m_Enumerator.MoveNext();
233 private IList m_Adaptee;
235 public ArrayListAdapter(IList adaptee)
241 public override object this[int index]
245 return m_Adaptee[index];
250 m_Adaptee[index] = value;
254 public override int Count
258 return m_Adaptee.Count;
262 public override int Capacity
266 return m_Adaptee.Count;
271 if (value < m_Adaptee.Count)
273 throw new ArgumentException("capacity");
278 public override bool IsFixedSize
282 return m_Adaptee.IsFixedSize;
286 public override bool IsReadOnly
290 return m_Adaptee.IsReadOnly;
294 public override object SyncRoot
298 return m_Adaptee.SyncRoot;
302 public override int Add(object value)
304 return m_Adaptee.Add(value);
307 public override void Clear()
312 public override bool Contains(object value)
314 return m_Adaptee.Contains(value);
317 public override int IndexOf(object value)
319 return m_Adaptee.IndexOf(value);
322 public override int IndexOf(object value, int startIndex)
324 return IndexOf(value, startIndex, m_Adaptee.Count - startIndex);
327 public override int IndexOf(object value, int startIndex, int count)
329 if (startIndex < 0 || startIndex > m_Adaptee.Count)
331 throw new ArgumentOutOfRangeException("startIndex", startIndex,
332 "Does not specify valid index.");
337 throw new ArgumentOutOfRangeException("count", count,
338 "Can't be less than 0.");
341 // re-ordered to avoid possible integer overflow
342 if (startIndex > m_Adaptee.Count - count) {
343 // LAMESPEC: Every other method throws ArgumentException
344 throw new ArgumentOutOfRangeException("count",
345 "Start index and count do not specify a valid range.");
350 for (int i = startIndex; i < startIndex + count; i++)
352 if (m_Adaptee[i] == null)
360 for (int i = startIndex; i < startIndex + count; i++)
362 if (value.Equals(m_Adaptee[i]))
372 public override int LastIndexOf(object value)
374 return LastIndexOf(value, m_Adaptee.Count - 1);
377 public override int LastIndexOf(object value, int startIndex)
379 return LastIndexOf(value, startIndex, startIndex + 1);
382 public override int LastIndexOf(object value, int startIndex, int count)
386 throw new ArgumentOutOfRangeException("startIndex", startIndex, "< 0");
391 throw new ArgumentOutOfRangeException("count", count, "count is negative.");
394 if (startIndex - count + 1 < 0)
396 throw new ArgumentOutOfRangeException("count", count, "count is too large.");
401 for (int i = startIndex; i > startIndex - count; i--)
403 if (m_Adaptee[i] == null)
411 for (int i = startIndex; i > startIndex - count; i--)
413 if (value.Equals(m_Adaptee[i]))
423 public override void Insert(int index, object value)
425 m_Adaptee.Insert(index, value);
428 public override void InsertRange(int index, ICollection c)
432 throw new ArgumentNullException("c");
435 if (index > m_Adaptee.Count)
437 throw new ArgumentOutOfRangeException("index", index,
438 "Index must be >= 0 and <= Count.");
441 foreach (object value in c)
443 m_Adaptee.Insert(index++, value);
447 public override void Remove(object value)
449 m_Adaptee.Remove(value);
452 public override void RemoveAt(int index)
454 m_Adaptee.RemoveAt(index);
457 public override void RemoveRange(int index, int count)
459 CheckRange(index, count, m_Adaptee.Count);
461 for (int i = 0; i < count; i++)
463 m_Adaptee.RemoveAt(index);
467 public override void Reverse()
469 Reverse(0, m_Adaptee.Count);
472 public override void Reverse(int index, int count)
476 CheckRange(index, count, m_Adaptee.Count);
478 for (int i = 0; i < count / 2; i++)
480 tmp = m_Adaptee[i + index];
481 m_Adaptee[i + index] = m_Adaptee[(index + count) - i + index - 1];
482 m_Adaptee[(index + count) - i + index - 1] = tmp;
486 public override void SetRange(int index, ICollection c)
490 throw new ArgumentNullException("c");
493 if (index < 0 || index + c.Count > m_Adaptee.Count)
495 throw new ArgumentOutOfRangeException("index");
500 foreach (object value in c)
502 m_Adaptee[x++] = value;
506 public override void CopyTo(System.Array array)
508 m_Adaptee.CopyTo(array, 0);
511 public override void CopyTo(System.Array array, int index)
513 m_Adaptee.CopyTo(array, index);
516 public override void CopyTo(int index, System.Array array, int arrayIndex, int count)
520 throw new ArgumentOutOfRangeException("index", index,
521 "Can't be less than zero.");
526 throw new ArgumentOutOfRangeException("arrayIndex", arrayIndex,
527 "Can't be less than zero.");
532 throw new ArgumentOutOfRangeException("index", index,
533 "Can't be less than zero.");
536 if (index >= m_Adaptee.Count)
538 throw new ArgumentException("Can't be more or equal to list count.",
544 throw new ArgumentException("Can't copy into multi-dimensional array.");
547 if (arrayIndex >= array.Length)
549 throw new ArgumentException("arrayIndex can't be greater than array.Length - 1.");
552 if (array.Length - arrayIndex + 1 < count)
554 throw new ArgumentException("Destination array is too small.");
557 // re-ordered to avoid possible integer overflow
558 if (index > m_Adaptee.Count - count) {
559 throw new ArgumentException("Index and count do not denote a valid range of elements.", "index");
562 for (int i = 0; i < count; i++)
564 array.SetValue(m_Adaptee[index + i], arrayIndex + i);
568 public override bool IsSynchronized
572 return m_Adaptee.IsSynchronized;
576 public override IEnumerator GetEnumerator()
578 return m_Adaptee.GetEnumerator();
581 public override IEnumerator GetEnumerator(int index, int count)
583 CheckRange(index, count, m_Adaptee.Count);
585 return new EnumeratorWithRange(m_Adaptee.GetEnumerator(), index, count);
588 public override void AddRange(ICollection c)
590 foreach (object value in c)
592 m_Adaptee.Add(value);
596 public override int BinarySearch(object value)
598 return BinarySearch(value, null);
601 public override int BinarySearch(object value, IComparer comparer)
603 return BinarySearch(0, m_Adaptee.Count, value, comparer);
606 public override int BinarySearch(int index, int count, object value, IComparer comparer)
610 // Doing a direct BinarySearch on the adaptee will perform poorly if the adaptee is a linked-list.
611 // Alternatives include copying the adaptee to a temporary array first.
613 CheckRange(index, count, m_Adaptee.Count);
615 if (comparer == null)
617 comparer = Comparer.Default;
621 y = index + count - 1;
627 r = comparer.Compare(value, m_Adaptee[z]);
646 public override object Clone()
648 return new ArrayList.ArrayListAdapter(m_Adaptee);
651 public override ArrayList GetRange(int index, int count)
653 CheckRange(index, count, m_Adaptee.Count);
655 return new RangedArrayList(this, index, count);
658 public override void TrimToSize()
663 public override void Sort()
665 Sort(Comparer.Default);
668 public override void Sort(IComparer comparer)
670 Sort(0, m_Adaptee.Count, comparer);
673 public override void Sort(int index, int count, IComparer comparer)
675 CheckRange(index, count, m_Adaptee.Count);
677 if (comparer == null)
679 comparer = Comparer.Default;
682 // Doing a direct sort on the adaptee will perform poorly if the adaptee is a linked-list.
683 // Alternatives include copying the adaptee into a temporary array first.
685 QuickSort(m_Adaptee, index, index + count - 1, comparer);
690 /// Swaps two items in a list at the specified indexes.
692 private static void Swap(IList list, int x, int y)
702 /// Quicksort for lists.
705 /// This function acts as both qsort() and partition().
707 internal static void QuickSort(IList list, int left, int right, IComparer comparer)
717 // Pick the pivot using the median-of-three strategy.
719 middle = (left + right) / 2;
721 if (comparer.Compare(list[middle], list[left]) < 0)
723 Swap(list, middle, left);
726 if (comparer.Compare(list[right], list[left]) < 0)
728 Swap(list, right, left);
731 if (comparer.Compare(list[right], list[middle]) < 0)
733 Swap(list, right, middle);
736 if (right - left + 1 <= 3)
741 // Put the pivot in right - 1.
742 Swap(list, right - 1, middle);
744 // List should look like:
746 // [Small] ..Numbers.. [Middle] ..Numbers.. [Pivot][Large]
748 pivot = list[right - 1];
750 // Sort from (left + 1) to (right - 2).
757 while (comparer.Compare(list[++i], pivot) < 0);
758 while (comparer.Compare(list[--j], pivot) > 0);
770 // Put pivot into the right position (real middle).
772 Swap(list, right - 1, i);
774 // Recursively sort the left and right sub lists.
776 QuickSort(list, left, i - 1, comparer);
777 QuickSort(list, i + 1, right, comparer);
780 public override object[] ToArray()
784 retval = new object[m_Adaptee.Count];
786 m_Adaptee.CopyTo(retval, 0);
791 public override Array ToArray(Type elementType)
795 retval = Array.CreateInstance(elementType, m_Adaptee.Count);
797 m_Adaptee.CopyTo(retval, 0);
803 #endregion // ArrayListAdapter
806 // ArrayList wrappers
809 #region ArrayListWrapper
812 /// Base wrapper/decorator for ArrayLists. Simply delegates all methods to
813 /// the underlying wrappee.
816 private class ArrayListWrapper
819 protected ArrayList m_InnerArrayList;
823 public ArrayListWrapper(ArrayList innerArrayList)
825 m_InnerArrayList = innerArrayList;
832 public override object this[int index]
836 return m_InnerArrayList[index];
841 m_InnerArrayList[index] = value;
849 public override int Count
853 return m_InnerArrayList.Count;
857 public override int Capacity
861 return m_InnerArrayList.Capacity;
866 m_InnerArrayList.Capacity = value;
870 public override bool IsFixedSize
874 return m_InnerArrayList.IsFixedSize;
878 public override bool IsReadOnly
882 return m_InnerArrayList.IsReadOnly;
886 public override bool IsSynchronized
890 return m_InnerArrayList.IsSynchronized;
894 public override object SyncRoot
898 return m_InnerArrayList.SyncRoot;
906 public override int Add(object value)
908 return m_InnerArrayList.Add(value);
911 public override void Clear()
913 m_InnerArrayList.Clear();
916 public override bool Contains(object value)
918 return m_InnerArrayList.Contains(value);
921 public override int IndexOf(object value)
923 return m_InnerArrayList.IndexOf(value);
926 public override int IndexOf(object value, int startIndex)
928 return m_InnerArrayList.IndexOf(value, startIndex);
931 public override int IndexOf(object value, int startIndex, int count)
933 return m_InnerArrayList.IndexOf(value, startIndex, count);
936 public override int LastIndexOf(object value)
938 return m_InnerArrayList.LastIndexOf(value);
941 public override int LastIndexOf(object value, int startIndex)
943 return m_InnerArrayList.LastIndexOf(value, startIndex);
946 public override int LastIndexOf(object value, int startIndex, int count)
948 return m_InnerArrayList.LastIndexOf(value, startIndex, count);
951 public override void Insert(int index, object value)
953 m_InnerArrayList.Insert(index, value);
956 public override void InsertRange(int index, ICollection c)
958 m_InnerArrayList.InsertRange(index, c);
961 public override void Remove(object value)
963 m_InnerArrayList.Remove(value);
966 public override void RemoveAt(int index)
968 m_InnerArrayList.RemoveAt(index);
971 public override void RemoveRange(int index, int count)
973 m_InnerArrayList.RemoveRange(index, count);
976 public override void Reverse()
978 m_InnerArrayList.Reverse();
981 public override void Reverse(int index, int count)
983 m_InnerArrayList.Reverse(index, count);
986 public override void SetRange(int index, ICollection c)
988 m_InnerArrayList.SetRange(index, c);
991 public override void CopyTo(System.Array array)
993 m_InnerArrayList.CopyTo(array);
996 public override void CopyTo(System.Array array, int index)
998 m_InnerArrayList.CopyTo(array, index);
1001 public override void CopyTo(int index, System.Array array, int arrayIndex, int count)
1003 m_InnerArrayList.CopyTo(index, array, arrayIndex, count);
1006 public override IEnumerator GetEnumerator()
1008 return m_InnerArrayList.GetEnumerator();
1011 public override IEnumerator GetEnumerator(int index, int count)
1013 return m_InnerArrayList.GetEnumerator(index, count);
1016 public override void AddRange(ICollection c)
1018 m_InnerArrayList.AddRange(c);
1021 public override int BinarySearch(object value)
1023 return m_InnerArrayList.BinarySearch(value);
1026 public override int BinarySearch(object value, IComparer comparer)
1028 return m_InnerArrayList.BinarySearch(value, comparer);
1031 public override int BinarySearch(int index, int count, object value, IComparer comparer)
1033 return m_InnerArrayList.BinarySearch(index, count, value, comparer);
1036 public override object Clone()
1038 return m_InnerArrayList.Clone();
1041 public override ArrayList GetRange(int index, int count)
1043 return m_InnerArrayList.GetRange(index, count);
1046 public override void TrimToSize()
1048 m_InnerArrayList.TrimToSize();
1051 public override void Sort()
1053 m_InnerArrayList.Sort();
1056 public override void Sort(IComparer comparer)
1058 m_InnerArrayList.Sort(comparer);
1061 public override void Sort(int index, int count, IComparer comparer)
1063 m_InnerArrayList.Sort(index, count, comparer);
1066 public override object[] ToArray()
1068 return m_InnerArrayList.ToArray();
1071 public override Array ToArray(Type elementType)
1073 return m_InnerArrayList.ToArray(elementType);
1081 #region SynchronizedArrayListWrapper
1084 /// ArrayListWrapper that synchronizes calls to all methods/properties.
1087 /// Works by just synchronizing all method calls. In the future careful optimisation
1088 /// could give better performance...
1091 private sealed class SynchronizedArrayListWrapper
1094 private object m_SyncRoot;
1096 #region Constructors
1099 /// Creates a new synchronized wrapper for the given <see cref="ArrayList"/>.
1101 /// <param name="innerArrayList"></param>
1102 internal SynchronizedArrayListWrapper(ArrayList innerArrayList)
1103 : base(innerArrayList)
1105 m_SyncRoot = innerArrayList.SyncRoot;
1112 public override object this[int index]
1118 return m_InnerArrayList[index];
1126 m_InnerArrayList[index] = value;
1135 // Some of these properties may be calculated so it's best to synchronize
1136 // them even though it might cause a performance hit.
1137 // Better safe than sorry ;D.
1139 public override int Count
1145 return m_InnerArrayList.Count;
1150 public override int Capacity
1156 return m_InnerArrayList.Capacity;
1164 m_InnerArrayList.Capacity = value;
1169 public override bool IsFixedSize
1175 return m_InnerArrayList.IsFixedSize;
1180 public override bool IsReadOnly
1186 return m_InnerArrayList.IsReadOnly;
1191 public override bool IsSynchronized
1199 public override object SyncRoot
1211 public override int Add(object value)
1215 return m_InnerArrayList.Add(value);
1219 public override void Clear()
1223 m_InnerArrayList.Clear();
1227 public override bool Contains(object value)
1231 return m_InnerArrayList.Contains(value);
1235 public override int IndexOf(object value)
1239 return m_InnerArrayList.IndexOf(value);
1243 public override int IndexOf(object value, int startIndex)
1247 return m_InnerArrayList.IndexOf(value, startIndex);
1251 public override int IndexOf(object value, int startIndex, int count)
1255 return m_InnerArrayList.IndexOf(value, startIndex, count);
1259 public override int LastIndexOf(object value)
1263 return m_InnerArrayList.LastIndexOf(value);
1267 public override int LastIndexOf(object value, int startIndex)
1271 return m_InnerArrayList.LastIndexOf(value, startIndex);
1275 public override int LastIndexOf(object value, int startIndex, int count)
1279 return m_InnerArrayList.LastIndexOf(value, startIndex, count);
1283 public override void Insert(int index, object value)
1287 m_InnerArrayList.Insert(index, value);
1291 public override void InsertRange(int index, ICollection c)
1295 m_InnerArrayList.InsertRange(index, c);
1299 public override void Remove(object value)
1303 m_InnerArrayList.Remove(value);
1307 public override void RemoveAt(int index)
1311 m_InnerArrayList.RemoveAt(index);
1315 public override void RemoveRange(int index, int count)
1319 m_InnerArrayList.RemoveRange(index, count);
1323 public override void Reverse()
1327 m_InnerArrayList.Reverse();
1331 public override void Reverse(int index, int count)
1335 m_InnerArrayList.Reverse(index, count);
1339 public override void CopyTo(System.Array array)
1343 m_InnerArrayList.CopyTo(array);
1347 public override void CopyTo(System.Array array, int index)
1351 m_InnerArrayList.CopyTo(array, index);
1355 public override void CopyTo(int index, System.Array array, int arrayIndex, int count)
1359 m_InnerArrayList.CopyTo(index, array, arrayIndex, count);
1363 public override IEnumerator GetEnumerator()
1367 return m_InnerArrayList.GetEnumerator();
1371 public override IEnumerator GetEnumerator(int index, int count)
1375 return m_InnerArrayList.GetEnumerator(index, count);
1379 public override void AddRange(ICollection c)
1383 m_InnerArrayList.AddRange(c);
1387 public override int BinarySearch(object value)
1391 return m_InnerArrayList.BinarySearch(value);
1395 public override int BinarySearch(object value, IComparer comparer)
1399 return m_InnerArrayList.BinarySearch(value, comparer);
1403 public override int BinarySearch(int index, int count, object value, IComparer comparer)
1407 return m_InnerArrayList.BinarySearch(index, count, value, comparer);
1411 public override object Clone()
1415 return m_InnerArrayList.Clone();
1419 public override ArrayList GetRange(int index, int count)
1423 return m_InnerArrayList.GetRange(index, count);
1427 public override void TrimToSize()
1431 m_InnerArrayList.TrimToSize();
1435 public override void Sort()
1439 m_InnerArrayList.Sort();
1443 public override void Sort(IComparer comparer)
1447 m_InnerArrayList.Sort(comparer);
1451 public override void Sort(int index, int count, IComparer comparer)
1455 m_InnerArrayList.Sort(index, count, comparer);
1459 public override object[] ToArray()
1463 return m_InnerArrayList.ToArray();
1467 public override Array ToArray(Type elementType)
1471 return m_InnerArrayList.ToArray(elementType);
1480 #region FixedSizeArrayListWrapper
1483 private class FixedSizeArrayListWrapper
1486 #region Constructors
1488 public FixedSizeArrayListWrapper(ArrayList innerList)
1498 /// Gets the error message to display when an readonly/fixedsize related exception is
1501 protected virtual string ErrorMessage
1505 return "Can't add or remove from a fixed-size list.";
1509 public override int Capacity
1513 return base.Capacity;
1518 throw new NotSupportedException(this.ErrorMessage);
1522 public override bool IsFixedSize
1534 public override int Add(object value)
1536 throw new NotSupportedException(this.ErrorMessage);
1539 public override void AddRange(ICollection c)
1541 throw new NotSupportedException(this.ErrorMessage);
1544 public override void Clear()
1546 throw new NotSupportedException(this.ErrorMessage);
1549 public override void Insert(int index, object value)
1551 throw new NotSupportedException(this.ErrorMessage);
1554 public override void InsertRange(int index, ICollection c)
1556 throw new NotSupportedException(this.ErrorMessage);
1559 public override void Remove(object value)
1561 throw new NotSupportedException(this.ErrorMessage);
1564 public override void RemoveAt(int index)
1566 throw new NotSupportedException(this.ErrorMessage);
1569 public override void RemoveRange(int index, int count)
1571 throw new NotSupportedException(this.ErrorMessage);
1574 public override void TrimToSize()
1576 throw new NotSupportedException(this.ErrorMessage);
1584 #region ReadOnlyArrayListWrapper
1587 private sealed class ReadOnlyArrayListWrapper
1588 : FixedSizeArrayListWrapper
1590 protected override string ErrorMessage
1594 return "Can't modify a readonly list.";
1598 public override bool IsReadOnly
1606 public ReadOnlyArrayListWrapper(ArrayList innerArrayList)
1607 : base(innerArrayList)
1611 public override object this[int index]
1615 return m_InnerArrayList[index];
1620 throw new NotSupportedException(this.ErrorMessage);
1624 public override void Reverse()
1626 throw new NotSupportedException(this.ErrorMessage);
1629 public override void Reverse(int index, int count)
1631 throw new NotSupportedException(this.ErrorMessage);
1634 public override void SetRange(int index, ICollection c)
1636 throw new NotSupportedException(this.ErrorMessage);
1639 public override void Sort()
1641 throw new NotSupportedException(this.ErrorMessage);
1644 public override void Sort(IComparer comparer)
1646 throw new NotSupportedException(this.ErrorMessage);
1649 public override void Sort(int index, int count, IComparer comparer)
1651 throw new NotSupportedException(this.ErrorMessage);
1657 #region RangedArrayList
1660 private sealed class RangedArrayList
1663 private int m_InnerIndex;
1664 private int m_InnerCount;
1665 private int m_InnerStateChanges;
1667 public RangedArrayList(ArrayList innerList, int index, int count)
1670 m_InnerIndex = index;
1671 m_InnerCount = count;
1672 m_InnerStateChanges = innerList._version;
1677 public override bool IsSynchronized
1685 public override object this[int index]
1689 if (index < 0 || index > m_InnerCount)
1691 throw new ArgumentOutOfRangeException("index");
1694 return m_InnerArrayList[m_InnerIndex + index];
1699 if (index < 0 || index > m_InnerCount)
1701 throw new ArgumentOutOfRangeException("index");
1704 m_InnerArrayList[m_InnerIndex + index] = value;
1712 public override int Count
1716 VerifyStateChanges();
1718 return m_InnerCount;
1722 public override int Capacity
1726 return m_InnerArrayList.Capacity;
1731 if (value < m_InnerCount)
1733 throw new ArgumentOutOfRangeException();
1742 private void VerifyStateChanges()
1744 if (m_InnerStateChanges != m_InnerArrayList._version)
1746 throw new InvalidOperationException
1747 ("ArrayList view is invalid because the underlying ArrayList was modified.");
1751 public override int Add(object value)
1753 VerifyStateChanges();
1755 m_InnerArrayList.Insert(m_InnerIndex + m_InnerCount, value);
1757 m_InnerStateChanges = m_InnerArrayList._version;
1759 return ++m_InnerCount;
1762 public override void Clear()
1764 VerifyStateChanges();
1766 m_InnerArrayList.RemoveRange(m_InnerIndex, m_InnerCount);
1769 m_InnerStateChanges = m_InnerArrayList._version;
1772 public override bool Contains(object value)
1774 return m_InnerArrayList.Contains(value, m_InnerIndex, m_InnerCount);
1777 public override int IndexOf(object value)
1779 return IndexOf(value, 0);
1782 public override int IndexOf(object value, int startIndex)
1784 return IndexOf(value, startIndex, m_InnerCount - startIndex);
1787 public override int IndexOf(object value, int startIndex, int count)
1789 if (startIndex < 0 || startIndex > m_InnerCount)
1791 throw new ArgumentOutOfRangeException("startIndex", startIndex,
1792 "Does not specify valid index.");
1797 throw new ArgumentOutOfRangeException("count", count,
1798 "Can't be less than 0.");
1801 // re-ordered to avoid possible integer overflow
1802 if (startIndex > m_InnerCount - count)
1804 // LAMESPEC: Every other method throws ArgumentException
1806 throw new ArgumentOutOfRangeException("count",
1807 "Start index and count do not specify a valid range.");
1810 int retval = m_InnerArrayList.IndexOf(value, m_InnerIndex + startIndex, count);
1818 return retval - m_InnerIndex;
1822 public override int LastIndexOf(object value)
1824 return LastIndexOf(value, m_InnerCount - 1);
1827 public override int LastIndexOf(object value, int startIndex)
1829 return LastIndexOf(value, startIndex, startIndex + 1);
1832 public override int LastIndexOf(object value, int startIndex, int count)
1836 throw new ArgumentOutOfRangeException("startIndex", startIndex, "< 0");
1841 throw new ArgumentOutOfRangeException("count", count, "count is negative.");
1844 int retval = m_InnerArrayList.LastIndexOf(value, m_InnerIndex + startIndex, count);
1852 return retval - m_InnerIndex;
1856 public override void Insert(int index, object value)
1858 VerifyStateChanges();
1860 if (index < 0 || index > m_InnerCount)
1862 throw new ArgumentOutOfRangeException("index", index,
1863 "Index must be >= 0 and <= Count.");
1866 m_InnerArrayList.Insert(m_InnerIndex + index, value);
1870 m_InnerStateChanges = m_InnerArrayList._version;
1873 public override void InsertRange(int index, ICollection c)
1875 VerifyStateChanges();
1877 if (index < 0 || index > m_InnerCount)
1879 throw new ArgumentOutOfRangeException("index", index,
1880 "Index must be >= 0 and <= Count.");
1883 m_InnerArrayList.InsertRange(m_InnerIndex + index, c);
1885 m_InnerCount += c.Count;
1887 m_InnerStateChanges = m_InnerArrayList._version;
1890 public override void Remove(object value)
1892 VerifyStateChanges();
1894 int x = IndexOf(value);
1901 m_InnerStateChanges = m_InnerArrayList._version;
1904 public override void RemoveAt(int index)
1906 VerifyStateChanges();
1908 if (index < 0 || index > m_InnerCount)
1910 throw new ArgumentOutOfRangeException("index", index,
1911 "Index must be >= 0 and <= Count.");
1914 m_InnerArrayList.RemoveAt(m_InnerIndex + index);
1917 m_InnerStateChanges = m_InnerArrayList._version;
1920 public override void RemoveRange(int index, int count)
1922 VerifyStateChanges();
1924 CheckRange(index, count, m_InnerCount);
1926 m_InnerArrayList.RemoveRange(m_InnerIndex + index, count);
1928 m_InnerCount -= count;
1930 m_InnerStateChanges = m_InnerArrayList._version;
1933 public override void Reverse()
1935 Reverse(0, m_InnerCount);
1938 public override void Reverse(int index, int count)
1940 VerifyStateChanges();
1942 CheckRange(index, count, m_InnerCount);
1944 m_InnerArrayList.Reverse(m_InnerIndex + index, count);
1946 m_InnerStateChanges = m_InnerArrayList._version;
1949 public override void SetRange(int index, ICollection c)
1951 VerifyStateChanges();
1953 if (index < 0 || index > m_InnerCount)
1955 throw new ArgumentOutOfRangeException("index", index,
1956 "Index must be >= 0 and <= Count.");
1959 m_InnerArrayList.SetRange(m_InnerIndex + index, c);
1961 m_InnerStateChanges = m_InnerArrayList._version;
1964 public override void CopyTo(System.Array array)
1969 public override void CopyTo(System.Array array, int index)
1971 CopyTo(0, array, index, m_InnerCount);
1974 public override void CopyTo(int index, System.Array array, int arrayIndex, int count)
1976 CheckRange(index, count, m_InnerCount);
1978 m_InnerArrayList.CopyTo(m_InnerIndex + index, array, arrayIndex, count);
1981 public override IEnumerator GetEnumerator()
1983 return GetEnumerator(0, m_InnerCount);
1986 public override IEnumerator GetEnumerator(int index, int count)
1988 CheckRange(index, count, m_InnerCount);
1990 return m_InnerArrayList.GetEnumerator(m_InnerIndex + index, count);
1993 public override void AddRange(ICollection c)
1995 VerifyStateChanges();
1997 m_InnerArrayList.InsertRange(m_InnerCount, c);
1999 m_InnerCount += c.Count;
2001 m_InnerStateChanges = m_InnerArrayList._version;
2004 public override int BinarySearch(object value)
2006 return BinarySearch(0, m_InnerCount, value, Comparer.Default);
2009 public override int BinarySearch(object value, IComparer comparer)
2011 return BinarySearch(0, m_InnerCount, value, comparer);
2014 public override int BinarySearch(int index, int count, object value, IComparer comparer)
2016 CheckRange(index, count, m_InnerCount);
2018 return m_InnerArrayList.BinarySearch(m_InnerIndex + index, count, value, comparer);
2021 public override object Clone()
2023 return new RangedArrayList((ArrayList)m_InnerArrayList.Clone(), m_InnerIndex, m_InnerCount);
2026 public override ArrayList GetRange(int index, int count)
2028 CheckRange(index, count, m_InnerCount);
2030 return new RangedArrayList(this, index, count);
2033 public override void TrimToSize()
2035 throw new NotSupportedException();
2038 public override void Sort()
2040 Sort(Comparer.Default);
2043 public override void Sort(IComparer comparer)
2045 Sort(0, m_InnerCount, comparer);
2048 public override void Sort(int index, int count, IComparer comparer)
2050 VerifyStateChanges();
2052 CheckRange(index, count, m_InnerCount);
2054 m_InnerArrayList.Sort(m_InnerIndex + index, count, comparer);
2056 m_InnerStateChanges = m_InnerArrayList._version;
2059 public override object[] ToArray()
2063 array = new object[m_InnerCount];
2065 m_InnerArrayList.CopyTo (m_InnerIndex, array, 0, m_InnerCount);
2070 public override Array ToArray(Type elementType)
2074 array = Array.CreateInstance(elementType, m_InnerCount);
2076 m_InnerArrayList.CopyTo(m_InnerIndex, array, 0, m_InnerCount);
2090 #region SynchronizedListWrapper
2093 private sealed class SynchronizedListWrapper
2096 private object m_SyncRoot;
2098 public SynchronizedListWrapper(IList innerList)
2101 m_SyncRoot = innerList.SyncRoot;
2104 public override int Count
2110 return m_InnerList.Count;
2115 public override bool IsSynchronized
2123 public override object SyncRoot
2129 return m_InnerList.SyncRoot;
2134 public override bool IsFixedSize
2140 return m_InnerList.IsFixedSize;
2145 public override bool IsReadOnly
2151 return m_InnerList.IsReadOnly;
2156 public override object this[int index]
2162 return m_InnerList[index];
2170 m_InnerList[index] = value;
2175 public override int Add(object value)
2179 return m_InnerList.Add(value);
2183 public override void Clear()
2187 m_InnerList.Clear();
2191 public override bool Contains(object value)
2195 return m_InnerList.Contains(value);
2199 public override int IndexOf(object value)
2203 return m_InnerList.IndexOf(value);
2207 public override void Insert(int index, object value)
2211 m_InnerList.Insert(index, value);
2215 public override void Remove(object value)
2219 m_InnerList.Remove(value);
2223 public override void RemoveAt(int index)
2227 m_InnerList.RemoveAt(index);
2231 public override void CopyTo(Array array, int index)
2235 m_InnerList.CopyTo(array, index);
2239 public override IEnumerator GetEnumerator()
2243 return m_InnerList.GetEnumerator();
2250 #region FixedSizeListWrapper
2253 private class FixedSizeListWrapper
2256 protected virtual string ErrorMessage
2260 return "List is fixed-size.";
2264 public override bool IsFixedSize
2272 public FixedSizeListWrapper(IList innerList)
2277 public override int Add(object value)
2279 throw new NotSupportedException(this.ErrorMessage);
2282 public override void Clear()
2284 throw new NotSupportedException(this.ErrorMessage);
2287 public override void Insert(int index, object value)
2289 throw new NotSupportedException(this.ErrorMessage);
2292 public override void Remove(object value)
2294 throw new NotSupportedException(this.ErrorMessage);
2297 public override void RemoveAt(int index)
2299 throw new NotSupportedException(this.ErrorMessage);
2305 #region ReadOnlyListWrapper
2308 private sealed class ReadOnlyListWrapper
2309 : FixedSizeListWrapper
2311 protected override string ErrorMessage
2315 return "List is read-only.";
2319 public override bool IsReadOnly
2327 public ReadOnlyListWrapper(IList innerList)
2332 public override object this[int index]
2336 return m_InnerList[index];
2341 throw new NotSupportedException(this.ErrorMessage);
2351 /// Decorates/Wraps any <c>IList</c> implementing object.
2354 private class ListWrapper
2359 protected IList m_InnerList;
2363 #region Constructors
2365 public ListWrapper(IList innerList)
2367 m_InnerList = innerList;
2374 public virtual object this[int index]
2378 return m_InnerList[index];
2383 m_InnerList[index] = value;
2391 public virtual int Count
2395 return m_InnerList.Count;
2399 public virtual bool IsSynchronized
2403 return m_InnerList.IsSynchronized;
2407 public virtual object SyncRoot
2411 return m_InnerList.SyncRoot;
2415 public virtual bool IsFixedSize
2419 return m_InnerList.IsFixedSize;
2423 public virtual bool IsReadOnly
2427 return m_InnerList.IsReadOnly;
2435 public virtual int Add(object value)
2437 return m_InnerList.Add(value);
2440 public virtual void Clear()
2442 m_InnerList.Clear();
2445 public virtual bool Contains(object value)
2447 return m_InnerList.Contains(value);
2450 public virtual int IndexOf(object value)
2452 return m_InnerList.IndexOf(value);
2455 public virtual void Insert(int index, object value)
2457 m_InnerList.Insert(index, value);
2460 public virtual void Remove(object value)
2462 m_InnerList.Remove(value);
2465 public virtual void RemoveAt(int index)
2467 m_InnerList.RemoveAt(index);
2470 public virtual void CopyTo(Array array, int index)
2472 m_InnerList.CopyTo(array, index);
2475 public virtual IEnumerator GetEnumerator()
2477 return m_InnerList.GetEnumerator();
2486 // Start of ArrayList
2492 private const int DefaultInitialCapacity = 4;
2494 private const int DefaultInitialCapacity = 16;
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).
2523 _items = new object[0];
2525 _items = new object[DefaultInitialCapacity];
2530 /// Initializes a new instance of the <see cref="ArrayList"/> class that contains
2531 /// elements copied from the specified collection and that has the same initial capacity
2532 /// as the number of elements copied.
2534 /// <param name="c">
2535 /// The <see cref="ICollection"/> whose elements are copied into the new list.
2537 /// <exception cref="ArgumentNullException">
2538 /// The argument <c>c</c> is a null reference.
2540 public ArrayList(ICollection c)
2546 throw new ArgumentNullException("c");
2551 if (array != null && array.Rank != 1)
2553 throw new RankException();
2556 _items = new object[c.Count];
2562 /// Initializes a new instance of the <see cref="ArrayList"/> class that is empty and
2563 /// has the specified initial capacity.
2565 /// <param name="capacity">
2566 /// The number of elements that hte new list is initially capable of storing.
2568 /// <exception cref="ArgumentOutOfRangeException">
2569 /// The <c>capacity</c> is less than zero.
2571 public ArrayList(int capacity)
2575 throw new ArgumentOutOfRangeException("capacity",
2576 capacity, "The initial capacity can't be smaller than zero.");
2581 capacity = DefaultInitialCapacity;
2583 _items = new object [capacity];
2587 /// Used by ArrayListAdapter to allow creation of an ArrayList with no storage buffer.
2589 private ArrayList(int initialCapacity, bool forceZeroSize)
2597 throw new InvalidOperationException("Use ArrayList(int)");
2602 /// Initializes a new array list that contains a copy of the given array and with the
2605 /// <param name="array"></param>
2606 private ArrayList(object[] array, int index, int count)
2610 _items = new object[DefaultInitialCapacity];
2614 _items = new object[count];
2617 Array.Copy(array, index, _items, 0, count);
2627 /// Gets/Sets an element in the list by index.
2629 /// <exception cref="ArgumentOutOfRangeException">
2630 /// The index is less than 0 or more then or equal to the list count.
2632 public virtual object this[int index]
2636 if (index < 0 || index >= _size)
2638 throw new ArgumentOutOfRangeException("index", index,
2639 "Index is less than 0 or more than or equal to the list count.");
2642 return _items[index];
2647 if (index < 0 || index >= _size)
2649 throw new ArgumentOutOfRangeException("index", index,
2650 "Index is less than 0 or more than or equal to the list count.");
2653 _items[index] = value;
2663 /// Gets the number of elements in the list.
2665 public virtual int Count
2674 /// Gets the number of elements the list can carry without needing to expand.
2677 /// ArrayLists automatically double their capacity when the capacity limit is broken.
2679 /// <exception cref="ArgumentOutOfRangeException">
2680 /// The capacity is less than the count.
2682 public virtual int Capacity
2686 return _items.Length;
2693 throw new ArgumentOutOfRangeException("Capacity", value,
2694 "Must be more than count.");
2699 newArray = new object[value];
2701 Array.Copy(_items, 0, newArray, 0, _size);
2708 /// <see cref="IList.IsFixedSize"/>
2711 public virtual bool IsFixedSize
2720 /// <see cref="IList.IsReadOnly"/>
2722 public virtual bool IsReadOnly
2731 /// <see cref="ICollection.IsSynchronized"/>
2733 public virtual bool IsSynchronized
2742 /// <see cref="ICollection.SyncRoot"/>
2744 public virtual object SyncRoot
2757 /// Ensures that the list has the capacity to contain the given <c>count</c> by
2758 /// automatically expanding the capacity when required.
2760 private void EnsureCapacity(int count)
2762 if (count <= _items.Length)
2770 newLength = _items.Length << 1;
2772 newLength = DefaultInitialCapacity;
2774 while (newLength < count)
2779 newData = new object[newLength];
2781 Array.Copy(_items, 0, newData, 0, _items.Length);
2787 /// Shifts a section of the list.
2789 /// <param name="index">
2790 /// The start of the section to shift (the element at index is included in the shift).
2792 /// <param name="count">
2793 /// The number of positions to shift by (can be negative).
2795 private void Shift(int index, int count)
2799 if (_size + count > _items.Length)
2804 newLength = (_items.Length > 0) ? _items.Length << 1 : 1;
2806 while (newLength < _size + count)
2811 newData = new object[newLength];
2813 Array.Copy(_items, 0, newData, 0, index);
2814 Array.Copy(_items, index, newData, index + count, _size - index);
2820 Array.Copy(_items, index, _items, index + count, _size - index);
2825 // Remember count is negative so this is actually index + (-count)
2827 int x = index - count ;
2829 Array.Copy(_items, x, _items, index, _size - x);
2833 public virtual int Add(object value)
2835 // Do a check here in case EnsureCapacity isn't inlined.
2837 if (_items.Length <= _size /* same as _items.Length < _size + 1) */)
2839 EnsureCapacity(_size + 1);
2842 _items[_size] = value;
2849 public virtual void Clear()
2851 // Keep the array but null all members so they can be garbage collected.
2853 Array.Clear(_items, 0, _size);
2859 public virtual bool Contains(object value)
2861 return IndexOf(value, 0, _size) > -1;
2864 internal virtual bool Contains(object value, int startIndex, int count)
2866 return IndexOf(value, startIndex, count) > -1;
2869 public virtual int IndexOf(object value)
2871 return IndexOf(value, 0);
2874 public virtual int IndexOf(object value, int startIndex)
2876 return IndexOf(value, startIndex, _size - startIndex);
2879 public virtual int IndexOf(object value, int startIndex, int count)
2881 if (startIndex < 0 || startIndex > _size)
2883 throw new ArgumentOutOfRangeException("startIndex", startIndex,
2884 "Does not specify valid index.");
2889 throw new ArgumentOutOfRangeException("count", count,
2890 "Can't be less than 0.");
2893 // re-ordered to avoid integer overflow
2894 if (startIndex > _size - count)
2896 // LAMESPEC: Every other method throws ArgumentException
2898 throw new ArgumentOutOfRangeException("count",
2899 "Start index and count do not specify a valid range.");
2902 return Array.IndexOf(_items, value, startIndex, count);
2905 public virtual int LastIndexOf(object value)
2907 return LastIndexOf(value, _size - 1);
2910 public virtual int LastIndexOf(object value, int startIndex)
2912 return LastIndexOf(value, startIndex, startIndex + 1);
2915 public virtual int LastIndexOf (object value, int startIndex, int count)
2917 // Array will catch the exceptions
2918 return Array.LastIndexOf (_items, value, startIndex, count);
2921 public virtual void Insert(int index, object value)
2923 if (index < 0 || index > _size)
2925 throw new ArgumentOutOfRangeException("index", index,
2926 "Index must be >= 0 and <= Count.");
2931 _items[index] = value;
2936 public virtual void InsertRange(int index, ICollection c)
2942 throw new ArgumentNullException("c");
2945 if (index < 0 || index > _size)
2947 throw new ArgumentOutOfRangeException("index", index,
2948 "Index must be >= 0 and <= Count.");
2953 // Do a check here in case EnsureCapacity isn't inlined.
2955 if (_items.Length < _size + i)
2957 EnsureCapacity(_size + i);
2962 Array.Copy(_items, index, _items, index + i, _size - index);
2965 // Handle inserting a range from a list to itself specially.
2967 if (this == c.SyncRoot)
2969 // Copy range before the insert point.
2971 Array.Copy(_items, 0, _items, index, index);
2973 // Copy range after the insert point.
2975 Array.Copy(_items, index + i, _items, index << 1, _size - index);
2979 c.CopyTo(_items, index);
2986 public virtual void Remove(object value)
3000 public virtual void RemoveAt(int index)
3002 if (index < 0 || index >= _size)
3004 throw new ArgumentOutOfRangeException("index", index,
3005 "Less than 0 or more than list count.");
3013 public virtual void RemoveRange(int index, int count)
3015 ArrayList.CheckRange(index, count, _size);
3017 Shift(index, -count);
3022 public virtual void Reverse()
3024 Array.Reverse(_items, 0, _size);
3028 public virtual void Reverse(int index, int count)
3030 ArrayList.CheckRange(index, count, _size);
3032 Array.Reverse(_items, index, count);
3036 public virtual void CopyTo(System.Array array)
3038 Array.Copy(_items, array, _size);
3041 public virtual void CopyTo(System.Array array, int index)
3043 CopyTo(0, array, index, _size);
3046 public virtual void CopyTo(int index, System.Array array, int arrayIndex, int count)
3050 throw new ArgumentNullException("array");
3053 if (array.Rank != 1)
3056 // This should be a RankException because Array.Copy throws RankException.
3058 throw new ArgumentException("Must have only 1 dimensions.", "array");
3061 Array.Copy(_items, index, array, arrayIndex, count);
3064 public virtual IEnumerator GetEnumerator()
3066 return new SimpleEnumerator(this);
3069 public virtual IEnumerator GetEnumerator(int index, int count)
3071 ArrayList.CheckRange(index, count, _size);
3073 return new ArrayListEnumerator(this, index, count);
3076 public virtual void AddRange(ICollection c)
3078 InsertRange(_size, c);
3081 public virtual int BinarySearch(object value)
3085 return Array.BinarySearch(_items, 0, _size, value);
3087 catch (InvalidOperationException e)
3089 throw new ArgumentException(e.Message);
3093 public virtual int BinarySearch(object value, IComparer comparer)
3097 return Array.BinarySearch(_items, 0, _size, value, comparer);
3099 catch (InvalidOperationException e)
3101 throw new ArgumentException(e.Message);
3105 public virtual int BinarySearch(int index, int count, object value, IComparer comparer)
3109 return Array.BinarySearch(_items, index, count, value, comparer);
3111 catch (InvalidOperationException e)
3113 throw new ArgumentException(e.Message);
3116 public virtual ArrayList GetRange(int index, int count)
3118 ArrayList.CheckRange(index, count, _size);
3120 if (this.IsSynchronized)
3122 return ArrayList.Synchronized(new RangedArrayList(this, index, count));
3126 return new RangedArrayList(this, index, count);
3130 public virtual void SetRange(int index, ICollection c)
3134 throw new ArgumentNullException("c");
3137 if (index < 0 || index + c.Count > _size)
3139 throw new ArgumentOutOfRangeException("index");
3142 c.CopyTo(_items, index);
3147 public virtual void TrimToSize()
3149 if (_items.Length > _size)
3155 newArray = new object[DefaultInitialCapacity];
3159 newArray = new object[_size];
3162 Array.Copy(_items, 0, newArray, 0, _size);
3168 public virtual void Sort()
3170 Array.Sort(_items, 0, _size);
3175 public virtual void Sort(IComparer comparer)
3177 Array.Sort(_items, 0, _size, comparer);
3180 public virtual void Sort(int index, int count, IComparer comparer)
3182 ArrayList.CheckRange(index, count, _size);
3184 Array.Sort(_items, index, count, comparer);
3187 public virtual object[] ToArray()
3191 retval = new object[_size];
3198 public virtual Array ToArray(Type elementType)
3202 retval = Array.CreateInstance(elementType, _size);
3209 public virtual object Clone()
3211 return new ArrayList(this._items, 0, this._size);
3216 #region Static Methods
3219 /// Does a check of the arguments many of the methods in ArrayList use.
3222 /// The choice of exceptions thrown sometimes seem to be arbitrarily chosen so
3223 /// not all methods actually make use of CheckRange.
3225 internal static void CheckRange(int index, int count, int listCount)
3229 throw new ArgumentOutOfRangeException("index", index, "Can't be less than 0.");
3234 throw new ArgumentOutOfRangeException("count", count, "Can't be less than 0.");
3237 // re-ordered to avoid possible integer overflow
3238 if (index > listCount - count)
3240 throw new ArgumentException("Index and count do not denote a valid range of elements.", "index");
3244 public static ArrayList Adapter(IList list)
3246 // LAMESPEC: EWWW. Other lists aren't *Array*Lists.
3250 throw new ArgumentNullException("list");
3253 if (list.IsSynchronized)
3255 return ArrayList.Synchronized(new ArrayListAdapter(list));
3259 return new ArrayListAdapter(list);
3263 public static ArrayList Synchronized(ArrayList arrayList)
3265 if (arrayList == null)
3267 throw new ArgumentNullException("arrayList");
3270 if (arrayList.IsSynchronized)
3275 return new SynchronizedArrayListWrapper(arrayList);
3278 public static IList Synchronized(IList list)
3282 throw new ArgumentNullException("list");
3285 if (list.IsSynchronized)
3290 return new SynchronizedListWrapper(list);
3293 public static ArrayList ReadOnly(ArrayList arrayList)
3295 if (arrayList == null)
3297 throw new ArgumentNullException("arrayList");
3300 if (arrayList.IsReadOnly)
3305 return new ReadOnlyArrayListWrapper(arrayList);
3308 public static IList ReadOnly(IList list)
3312 throw new ArgumentNullException("list");
3315 if (list.IsReadOnly)
3320 return new ReadOnlyListWrapper(list);
3323 public static ArrayList FixedSize(ArrayList arrayList)
3325 if (arrayList == null)
3327 throw new ArgumentNullException("arrayList");
3330 if (arrayList.IsFixedSize)
3335 return new FixedSizeArrayListWrapper(arrayList);
3338 public static IList FixedSize(IList list)
3342 throw new ArgumentNullException("list");
3345 if (list.IsFixedSize)
3350 return new FixedSizeListWrapper(list);
3353 public static ArrayList Repeat(object value, int count)
3355 ArrayList arrayList = new ArrayList(count);
3357 for (int i = 0; i < count; i++)
3359 arrayList.Add(value);