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;
29 using System.Diagnostics;
31 namespace System.Collections
36 [DebuggerDisplay ("Count={Count}")]
37 [DebuggerTypeProxy (typeof (CollectionDebuggerView))]
38 public class ArrayList : IList, ICloneable, ICollection, IEnumerable {
40 internal class ArrayList : IList {
44 private sealed class ArrayListEnumerator
45 : IEnumerator, ICloneable
47 private object m_Current;
48 private ArrayList m_List;
52 private int m_ExpectedStateChanges;
54 public ArrayListEnumerator(ArrayList list)
55 : this(list, 0, list.Count)
61 return this.MemberwiseClone();
64 public ArrayListEnumerator(ArrayList list, int index, int count)
71 m_ExpectedStateChanges = list._version;
78 if (m_Pos == m_Index - 1) {
79 throw new InvalidOperationException("Enumerator unusable (Reset pending, or past end of array.");
86 public bool MoveNext()
88 if (m_List._version != m_ExpectedStateChanges)
90 throw new InvalidOperationException("List has changed.");
95 if (m_Pos - m_Index < m_Count)
97 m_Current = m_List[m_Pos];
112 sealed class SimpleEnumerator : IEnumerator, ICloneable
115 object currentElement;
118 static readonly object endFlag = new object ();
120 public SimpleEnumerator (ArrayList list)
124 version = list._version;
125 currentElement = endFlag;
128 public object Clone ()
130 return MemberwiseClone ();
133 public bool MoveNext ()
135 if (version != list._version)
136 throw new InvalidOperationException("List has changed.");
138 if (++index < list.Count) {
139 currentElement = list [index];
142 currentElement = endFlag;
147 public object Current {
149 if (currentElement == endFlag) {
151 throw new InvalidOperationException ("Enumerator not started");
153 throw new InvalidOperationException ("Enumerator ended");
156 return currentElement;
162 if (version != list._version)
163 throw new InvalidOperationException ("List has changed.");
165 currentElement = endFlag;
172 #region ArrayListAdapter
175 /// Adapts various ILists into an ArrayList.
178 private sealed class ArrayListAdapter
181 private sealed class EnumeratorWithRange
182 : IEnumerator, ICloneable
184 private int m_StartIndex;
186 private int m_MaxCount;
187 private IEnumerator m_Enumerator;
189 public EnumeratorWithRange(IEnumerator enumerator, int index, int count)
192 m_StartIndex = index;
194 m_Enumerator = enumerator;
199 public object Clone()
201 return this.MemberwiseClone();
204 public object Current
208 return m_Enumerator.Current;
212 public bool MoveNext()
214 if (m_Count >= m_MaxCount)
221 return m_Enumerator.MoveNext();
227 m_Enumerator.Reset();
229 for (int i = 0; i < m_StartIndex; i++)
231 m_Enumerator.MoveNext();
236 private IList m_Adaptee;
238 public ArrayListAdapter(IList adaptee)
244 public override object this[int index]
248 return m_Adaptee[index];
253 m_Adaptee[index] = value;
257 public override int Count
261 return m_Adaptee.Count;
265 public override int Capacity
269 return m_Adaptee.Count;
274 if (value < m_Adaptee.Count)
276 throw new ArgumentException("capacity");
281 public override bool IsFixedSize
285 return m_Adaptee.IsFixedSize;
289 public override bool IsReadOnly
293 return m_Adaptee.IsReadOnly;
297 public override object SyncRoot
301 return m_Adaptee.SyncRoot;
305 public override int Add(object value)
307 return m_Adaptee.Add(value);
310 public override void Clear()
315 public override bool Contains(object value)
317 return m_Adaptee.Contains(value);
320 public override int IndexOf(object value)
322 return m_Adaptee.IndexOf(value);
325 public override int IndexOf(object value, int startIndex)
327 return IndexOf(value, startIndex, m_Adaptee.Count - startIndex);
330 public override int IndexOf(object value, int startIndex, int count)
332 if (startIndex < 0 || startIndex > m_Adaptee.Count)
334 ThrowNewArgumentOutOfRangeException ("startIndex", startIndex,
335 "Does not specify valid index.");
340 ThrowNewArgumentOutOfRangeException ("count", count,
341 "Can't be less than 0.");
344 // re-ordered to avoid possible integer overflow
345 if (startIndex > m_Adaptee.Count - count) {
346 // LAMESPEC: Every other method throws ArgumentException
347 throw new ArgumentOutOfRangeException("count",
348 "Start index and count do not specify a valid range.");
353 for (int i = startIndex; i < startIndex + count; i++)
355 if (m_Adaptee[i] == null)
363 for (int i = startIndex; i < startIndex + count; i++)
365 if (value.Equals(m_Adaptee[i]))
375 public override int LastIndexOf(object value)
377 return LastIndexOf(value, m_Adaptee.Count - 1);
380 public override int LastIndexOf(object value, int startIndex)
382 return LastIndexOf(value, startIndex, startIndex + 1);
385 public override int LastIndexOf(object value, int startIndex, int count)
389 ThrowNewArgumentOutOfRangeException ("startIndex", startIndex, "< 0");
394 ThrowNewArgumentOutOfRangeException ("count", count, "count is negative.");
397 if (startIndex - count + 1 < 0)
399 ThrowNewArgumentOutOfRangeException ("count", count, "count is too large.");
404 for (int i = startIndex; i > startIndex - count; i--)
406 if (m_Adaptee[i] == null)
414 for (int i = startIndex; i > startIndex - count; i--)
416 if (value.Equals(m_Adaptee[i]))
426 public override void Insert(int index, object value)
428 m_Adaptee.Insert(index, value);
431 public override void InsertRange(int index, ICollection c)
435 throw new ArgumentNullException("c");
438 if (index > m_Adaptee.Count)
440 ThrowNewArgumentOutOfRangeException ("index", index,
441 "Index must be >= 0 and <= Count.");
444 foreach (object value in c)
446 m_Adaptee.Insert(index++, value);
450 public override void Remove(object value)
452 m_Adaptee.Remove(value);
455 public override void RemoveAt(int index)
457 m_Adaptee.RemoveAt(index);
460 public override void RemoveRange(int index, int count)
462 CheckRange(index, count, m_Adaptee.Count);
464 for (int i = 0; i < count; i++)
466 m_Adaptee.RemoveAt(index);
470 public override void Reverse()
472 Reverse(0, m_Adaptee.Count);
475 public override void Reverse(int index, int count)
479 CheckRange(index, count, m_Adaptee.Count);
481 for (int i = 0; i < count / 2; i++)
483 tmp = m_Adaptee[i + index];
484 m_Adaptee[i + index] = m_Adaptee[(index + count) - i + index - 1];
485 m_Adaptee[(index + count) - i + index - 1] = tmp;
489 public override void SetRange(int index, ICollection c)
493 throw new ArgumentNullException("c");
496 if (index < 0 || index + c.Count > m_Adaptee.Count)
498 throw new ArgumentOutOfRangeException("index");
503 foreach (object value in c)
505 m_Adaptee[x++] = value;
509 public override void CopyTo(System.Array array)
511 m_Adaptee.CopyTo(array, 0);
514 public override void CopyTo(System.Array array, int index)
516 m_Adaptee.CopyTo(array, index);
519 public override void CopyTo(int index, System.Array array, int arrayIndex, int count)
523 ThrowNewArgumentOutOfRangeException ("index", index,
524 "Can't be less than zero.");
529 ThrowNewArgumentOutOfRangeException ("arrayIndex", arrayIndex,
530 "Can't be less than zero.");
535 ThrowNewArgumentOutOfRangeException ("index", index,
536 "Can't be less than zero.");
539 if (index >= m_Adaptee.Count)
541 throw new ArgumentException("Can't be more or equal to list count.",
547 throw new ArgumentException("Can't copy into multi-dimensional array.");
550 if (arrayIndex >= array.Length)
552 throw new ArgumentException("arrayIndex can't be greater than array.Length - 1.");
555 if (array.Length - arrayIndex + 1 < count)
557 throw new ArgumentException("Destination array is too small.");
560 // re-ordered to avoid possible integer overflow
561 if (index > m_Adaptee.Count - count) {
562 throw new ArgumentException("Index and count do not denote a valid range of elements.", "index");
565 for (int i = 0; i < count; i++)
567 array.SetValue(m_Adaptee[index + i], arrayIndex + i);
571 public override bool IsSynchronized
575 return m_Adaptee.IsSynchronized;
579 public override IEnumerator GetEnumerator()
581 return m_Adaptee.GetEnumerator();
584 public override IEnumerator GetEnumerator(int index, int count)
586 CheckRange(index, count, m_Adaptee.Count);
588 return new EnumeratorWithRange(m_Adaptee.GetEnumerator(), index, count);
591 public override void AddRange(ICollection c)
593 foreach (object value in c)
595 m_Adaptee.Add(value);
599 public override int BinarySearch(object value)
601 return BinarySearch(value, null);
604 public override int BinarySearch(object value, IComparer comparer)
606 return BinarySearch(0, m_Adaptee.Count, value, comparer);
609 public override int BinarySearch(int index, int count, object value, IComparer comparer)
613 // Doing a direct BinarySearch on the adaptee will perform poorly if the adaptee is a linked-list.
614 // Alternatives include copying the adaptee to a temporary array first.
616 CheckRange(index, count, m_Adaptee.Count);
618 if (comparer == null)
620 comparer = Comparer.Default;
624 y = index + count - 1;
628 // Be careful with overflows
629 z = x + ((y - x) / 2);
631 r = comparer.Compare(value, m_Adaptee[z]);
650 public override object Clone()
652 return new ArrayList.ArrayListAdapter(m_Adaptee);
655 public override ArrayList GetRange(int index, int count)
657 CheckRange(index, count, m_Adaptee.Count);
659 return new RangedArrayList(this, index, count);
662 public override void TrimToSize()
667 public override void Sort()
669 Sort(Comparer.Default);
672 public override void Sort(IComparer comparer)
674 Sort(0, m_Adaptee.Count, comparer);
677 public override void Sort(int index, int count, IComparer comparer)
679 CheckRange(index, count, m_Adaptee.Count);
681 if (comparer == null)
683 comparer = Comparer.Default;
686 // Doing a direct sort on the adaptee will perform poorly if the adaptee is a linked-list.
687 // Alternatives include copying the adaptee into a temporary array first.
689 QuickSort(m_Adaptee, index, index + count - 1, comparer);
694 /// Swaps two items in a list at the specified indexes.
696 private static void Swap(IList list, int x, int y)
706 /// Quicksort for lists.
709 /// This function acts as both qsort() and partition().
711 internal static void QuickSort(IList list, int left, int right, IComparer comparer)
721 // Pick the pivot using the median-of-three strategy.
723 // Be careful with overflows
724 middle = left + ((right - left) / 2);
726 if (comparer.Compare(list[middle], list[left]) < 0)
728 Swap(list, middle, left);
731 if (comparer.Compare(list[right], list[left]) < 0)
733 Swap(list, right, left);
736 if (comparer.Compare(list[right], list[middle]) < 0)
738 Swap(list, right, middle);
741 if (right - left + 1 <= 3)
746 // Put the pivot in right - 1.
747 Swap(list, right - 1, middle);
749 // List should look like:
751 // [Small] ..Numbers.. [Middle] ..Numbers.. [Pivot][Large]
753 pivot = list[right - 1];
755 // Sort from (left + 1) to (right - 2).
762 while (comparer.Compare(list[++i], pivot) < 0);
763 while (comparer.Compare(list[--j], pivot) > 0);
775 // Put pivot into the right position (real middle).
777 Swap(list, right - 1, i);
779 // Recursively sort the left and right sub lists.
781 QuickSort(list, left, i - 1, comparer);
782 QuickSort(list, i + 1, right, comparer);
785 public override object[] ToArray()
789 retval = new object[m_Adaptee.Count];
791 m_Adaptee.CopyTo(retval, 0);
796 public override Array ToArray(Type elementType)
800 retval = Array.CreateInstance(elementType, m_Adaptee.Count);
802 m_Adaptee.CopyTo(retval, 0);
808 #endregion // ArrayListAdapter
811 // ArrayList wrappers
814 #region ArrayListWrapper
817 /// Base wrapper/decorator for ArrayLists. Simply delegates all methods to
818 /// the underlying wrappee.
821 private class ArrayListWrapper
824 protected ArrayList m_InnerArrayList;
828 public ArrayListWrapper(ArrayList innerArrayList)
830 m_InnerArrayList = innerArrayList;
837 public override object this[int index]
841 return m_InnerArrayList[index];
846 m_InnerArrayList[index] = value;
854 public override int Count
858 return m_InnerArrayList.Count;
862 public override int Capacity
866 return m_InnerArrayList.Capacity;
871 m_InnerArrayList.Capacity = value;
875 public override bool IsFixedSize
879 return m_InnerArrayList.IsFixedSize;
883 public override bool IsReadOnly
887 return m_InnerArrayList.IsReadOnly;
891 public override bool IsSynchronized
895 return m_InnerArrayList.IsSynchronized;
899 public override object SyncRoot
903 return m_InnerArrayList.SyncRoot;
911 public override int Add(object value)
913 return m_InnerArrayList.Add(value);
916 public override void Clear()
918 m_InnerArrayList.Clear();
921 public override bool Contains(object value)
923 return m_InnerArrayList.Contains(value);
926 public override int IndexOf(object value)
928 return m_InnerArrayList.IndexOf(value);
931 public override int IndexOf(object value, int startIndex)
933 return m_InnerArrayList.IndexOf(value, startIndex);
936 public override int IndexOf(object value, int startIndex, int count)
938 return m_InnerArrayList.IndexOf(value, startIndex, count);
941 public override int LastIndexOf(object value)
943 return m_InnerArrayList.LastIndexOf(value);
946 public override int LastIndexOf(object value, int startIndex)
948 return m_InnerArrayList.LastIndexOf(value, startIndex);
951 public override int LastIndexOf(object value, int startIndex, int count)
953 return m_InnerArrayList.LastIndexOf(value, startIndex, count);
956 public override void Insert(int index, object value)
958 m_InnerArrayList.Insert(index, value);
961 public override void InsertRange(int index, ICollection c)
963 m_InnerArrayList.InsertRange(index, c);
966 public override void Remove(object value)
968 m_InnerArrayList.Remove(value);
971 public override void RemoveAt(int index)
973 m_InnerArrayList.RemoveAt(index);
976 public override void RemoveRange(int index, int count)
978 m_InnerArrayList.RemoveRange(index, count);
981 public override void Reverse()
983 m_InnerArrayList.Reverse();
986 public override void Reverse(int index, int count)
988 m_InnerArrayList.Reverse(index, count);
991 public override void SetRange(int index, ICollection c)
993 m_InnerArrayList.SetRange(index, c);
996 public override void CopyTo(System.Array array)
998 m_InnerArrayList.CopyTo(array);
1001 public override void CopyTo(System.Array array, int index)
1003 m_InnerArrayList.CopyTo(array, index);
1006 public override void CopyTo(int index, System.Array array, int arrayIndex, int count)
1008 m_InnerArrayList.CopyTo(index, array, arrayIndex, count);
1011 public override IEnumerator GetEnumerator()
1013 return m_InnerArrayList.GetEnumerator();
1016 public override IEnumerator GetEnumerator(int index, int count)
1018 return m_InnerArrayList.GetEnumerator(index, count);
1021 public override void AddRange(ICollection c)
1023 m_InnerArrayList.AddRange(c);
1026 public override int BinarySearch(object value)
1028 return m_InnerArrayList.BinarySearch(value);
1031 public override int BinarySearch(object value, IComparer comparer)
1033 return m_InnerArrayList.BinarySearch(value, comparer);
1036 public override int BinarySearch(int index, int count, object value, IComparer comparer)
1038 return m_InnerArrayList.BinarySearch(index, count, value, comparer);
1041 public override object Clone()
1043 return m_InnerArrayList.Clone();
1046 public override ArrayList GetRange(int index, int count)
1048 return m_InnerArrayList.GetRange(index, count);
1051 public override void TrimToSize()
1053 m_InnerArrayList.TrimToSize();
1056 public override void Sort()
1058 m_InnerArrayList.Sort();
1061 public override void Sort(IComparer comparer)
1063 m_InnerArrayList.Sort(comparer);
1066 public override void Sort(int index, int count, IComparer comparer)
1068 m_InnerArrayList.Sort(index, count, comparer);
1071 public override object[] ToArray()
1073 return m_InnerArrayList.ToArray();
1076 public override Array ToArray(Type elementType)
1078 return m_InnerArrayList.ToArray(elementType);
1086 #region SynchronizedArrayListWrapper
1089 /// ArrayListWrapper that synchronizes calls to all methods/properties.
1092 /// Works by just synchronizing all method calls. In the future careful optimisation
1093 /// could give better performance...
1096 private sealed class SynchronizedArrayListWrapper
1099 private object m_SyncRoot;
1101 #region Constructors
1104 /// Creates a new synchronized wrapper for the given <see cref="ArrayList"/>.
1106 /// <param name="innerArrayList"></param>
1107 internal SynchronizedArrayListWrapper(ArrayList innerArrayList)
1108 : base(innerArrayList)
1110 m_SyncRoot = innerArrayList.SyncRoot;
1117 public override object this[int index]
1123 return m_InnerArrayList[index];
1131 m_InnerArrayList[index] = value;
1140 // Some of these properties may be calculated so it's best to synchronize
1141 // them even though it might cause a performance hit.
1142 // Better safe than sorry ;D.
1144 public override int Count
1150 return m_InnerArrayList.Count;
1155 public override int Capacity
1161 return m_InnerArrayList.Capacity;
1169 m_InnerArrayList.Capacity = value;
1174 public override bool IsFixedSize
1180 return m_InnerArrayList.IsFixedSize;
1185 public override bool IsReadOnly
1191 return m_InnerArrayList.IsReadOnly;
1196 public override bool IsSynchronized
1204 public override object SyncRoot
1216 public override int Add(object value)
1220 return m_InnerArrayList.Add(value);
1224 public override void Clear()
1228 m_InnerArrayList.Clear();
1232 public override bool Contains(object value)
1236 return m_InnerArrayList.Contains(value);
1240 public override int IndexOf(object value)
1244 return m_InnerArrayList.IndexOf(value);
1248 public override int IndexOf(object value, int startIndex)
1252 return m_InnerArrayList.IndexOf(value, startIndex);
1256 public override int IndexOf(object value, int startIndex, int count)
1260 return m_InnerArrayList.IndexOf(value, startIndex, count);
1264 public override int LastIndexOf(object value)
1268 return m_InnerArrayList.LastIndexOf(value);
1272 public override int LastIndexOf(object value, int startIndex)
1276 return m_InnerArrayList.LastIndexOf(value, startIndex);
1280 public override int LastIndexOf(object value, int startIndex, int count)
1284 return m_InnerArrayList.LastIndexOf(value, startIndex, count);
1288 public override void Insert(int index, object value)
1292 m_InnerArrayList.Insert(index, value);
1296 public override void InsertRange(int index, ICollection c)
1300 m_InnerArrayList.InsertRange(index, c);
1304 public override void Remove(object value)
1308 m_InnerArrayList.Remove(value);
1312 public override void RemoveAt(int index)
1316 m_InnerArrayList.RemoveAt(index);
1320 public override void RemoveRange(int index, int count)
1324 m_InnerArrayList.RemoveRange(index, count);
1328 public override void Reverse()
1332 m_InnerArrayList.Reverse();
1336 public override void Reverse(int index, int count)
1340 m_InnerArrayList.Reverse(index, count);
1344 public override void CopyTo(System.Array array)
1348 m_InnerArrayList.CopyTo(array);
1352 public override void CopyTo(System.Array array, int index)
1356 m_InnerArrayList.CopyTo(array, index);
1360 public override void CopyTo(int index, System.Array array, int arrayIndex, int count)
1364 m_InnerArrayList.CopyTo(index, array, arrayIndex, count);
1368 public override IEnumerator GetEnumerator()
1372 return m_InnerArrayList.GetEnumerator();
1376 public override IEnumerator GetEnumerator(int index, int count)
1380 return m_InnerArrayList.GetEnumerator(index, count);
1384 public override void AddRange(ICollection c)
1388 m_InnerArrayList.AddRange(c);
1392 public override int BinarySearch(object value)
1396 return m_InnerArrayList.BinarySearch(value);
1400 public override int BinarySearch(object value, IComparer comparer)
1404 return m_InnerArrayList.BinarySearch(value, comparer);
1408 public override int BinarySearch(int index, int count, object value, IComparer comparer)
1412 return m_InnerArrayList.BinarySearch(index, count, value, comparer);
1416 public override object Clone()
1420 return m_InnerArrayList.Clone();
1424 public override ArrayList GetRange(int index, int count)
1428 return m_InnerArrayList.GetRange(index, count);
1432 public override void TrimToSize()
1436 m_InnerArrayList.TrimToSize();
1440 public override void Sort()
1444 m_InnerArrayList.Sort();
1448 public override void Sort(IComparer comparer)
1452 m_InnerArrayList.Sort(comparer);
1456 public override void Sort(int index, int count, IComparer comparer)
1460 m_InnerArrayList.Sort(index, count, comparer);
1464 public override object[] ToArray()
1468 return m_InnerArrayList.ToArray();
1472 public override Array ToArray(Type elementType)
1476 return m_InnerArrayList.ToArray(elementType);
1485 #region FixedSizeArrayListWrapper
1488 private class FixedSizeArrayListWrapper
1491 #region Constructors
1493 public FixedSizeArrayListWrapper(ArrayList innerList)
1503 /// Gets the error message to display when an readonly/fixedsize related exception is
1506 protected virtual string ErrorMessage
1510 return "Can't add or remove from a fixed-size list.";
1514 public override int Capacity
1518 return base.Capacity;
1523 throw new NotSupportedException(this.ErrorMessage);
1527 public override bool IsFixedSize
1539 public override int Add(object value)
1541 throw new NotSupportedException(this.ErrorMessage);
1544 public override void AddRange(ICollection c)
1546 throw new NotSupportedException(this.ErrorMessage);
1549 public override void Clear()
1551 throw new NotSupportedException(this.ErrorMessage);
1554 public override void Insert(int index, object value)
1556 throw new NotSupportedException(this.ErrorMessage);
1559 public override void InsertRange(int index, ICollection c)
1561 throw new NotSupportedException(this.ErrorMessage);
1564 public override void Remove(object value)
1566 throw new NotSupportedException(this.ErrorMessage);
1569 public override void RemoveAt(int index)
1571 throw new NotSupportedException(this.ErrorMessage);
1574 public override void RemoveRange(int index, int count)
1576 throw new NotSupportedException(this.ErrorMessage);
1579 public override void TrimToSize()
1581 throw new NotSupportedException(this.ErrorMessage);
1589 #region ReadOnlyArrayListWrapper
1592 private sealed class ReadOnlyArrayListWrapper
1593 : FixedSizeArrayListWrapper
1595 protected override string ErrorMessage
1599 return "Can't modify a readonly list.";
1603 public override bool IsReadOnly
1611 public ReadOnlyArrayListWrapper(ArrayList innerArrayList)
1612 : base(innerArrayList)
1616 public override object this[int index]
1620 return m_InnerArrayList[index];
1625 throw new NotSupportedException(this.ErrorMessage);
1629 public override void Reverse()
1631 throw new NotSupportedException(this.ErrorMessage);
1634 public override void Reverse(int index, int count)
1636 throw new NotSupportedException(this.ErrorMessage);
1639 public override void SetRange(int index, ICollection c)
1641 throw new NotSupportedException(this.ErrorMessage);
1644 public override void Sort()
1646 throw new NotSupportedException(this.ErrorMessage);
1649 public override void Sort(IComparer comparer)
1651 throw new NotSupportedException(this.ErrorMessage);
1654 public override void Sort(int index, int count, IComparer comparer)
1656 throw new NotSupportedException(this.ErrorMessage);
1662 #region RangedArrayList
1665 private sealed class RangedArrayList
1668 private int m_InnerIndex;
1669 private int m_InnerCount;
1670 private int m_InnerStateChanges;
1672 public RangedArrayList(ArrayList innerList, int index, int count)
1675 m_InnerIndex = index;
1676 m_InnerCount = count;
1677 m_InnerStateChanges = innerList._version;
1682 public override bool IsSynchronized
1690 public override object this[int index]
1694 if (index < 0 || index > m_InnerCount)
1696 throw new ArgumentOutOfRangeException("index");
1699 return m_InnerArrayList[m_InnerIndex + index];
1704 if (index < 0 || index > m_InnerCount)
1706 throw new ArgumentOutOfRangeException("index");
1709 m_InnerArrayList[m_InnerIndex + index] = value;
1717 public override int Count
1721 VerifyStateChanges();
1723 return m_InnerCount;
1727 public override int Capacity
1731 return m_InnerArrayList.Capacity;
1736 if (value < m_InnerCount)
1738 throw new ArgumentOutOfRangeException();
1747 private void VerifyStateChanges()
1749 if (m_InnerStateChanges != m_InnerArrayList._version)
1751 throw new InvalidOperationException
1752 ("ArrayList view is invalid because the underlying ArrayList was modified.");
1756 public override int Add(object value)
1758 VerifyStateChanges();
1760 m_InnerArrayList.Insert(m_InnerIndex + m_InnerCount, value);
1762 m_InnerStateChanges = m_InnerArrayList._version;
1764 return ++m_InnerCount;
1767 public override void Clear()
1769 VerifyStateChanges();
1771 m_InnerArrayList.RemoveRange(m_InnerIndex, m_InnerCount);
1774 m_InnerStateChanges = m_InnerArrayList._version;
1777 public override bool Contains(object value)
1779 return m_InnerArrayList.Contains(value, m_InnerIndex, m_InnerCount);
1782 public override int IndexOf(object value)
1784 return IndexOf(value, 0);
1787 public override int IndexOf(object value, int startIndex)
1789 return IndexOf(value, startIndex, m_InnerCount - startIndex);
1792 public override int IndexOf(object value, int startIndex, int count)
1794 if (startIndex < 0 || startIndex > m_InnerCount)
1796 ThrowNewArgumentOutOfRangeException ("startIndex", startIndex,
1797 "Does not specify valid index.");
1802 ThrowNewArgumentOutOfRangeException ("count", count,
1803 "Can't be less than 0.");
1806 // re-ordered to avoid possible integer overflow
1807 if (startIndex > m_InnerCount - count)
1809 // LAMESPEC: Every other method throws ArgumentException
1811 throw new ArgumentOutOfRangeException("count",
1812 "Start index and count do not specify a valid range.");
1815 int retval = m_InnerArrayList.IndexOf(value, m_InnerIndex + startIndex, count);
1823 return retval - m_InnerIndex;
1827 public override int LastIndexOf(object value)
1829 return LastIndexOf(value, m_InnerCount - 1);
1832 public override int LastIndexOf(object value, int startIndex)
1834 return LastIndexOf(value, startIndex, startIndex + 1);
1837 public override int LastIndexOf(object value, int startIndex, int count)
1841 ThrowNewArgumentOutOfRangeException ("startIndex", startIndex, "< 0");
1846 ThrowNewArgumentOutOfRangeException ("count", count, "count is negative.");
1849 int retval = m_InnerArrayList.LastIndexOf(value, m_InnerIndex + startIndex, count);
1857 return retval - m_InnerIndex;
1861 public override void Insert(int index, object value)
1863 VerifyStateChanges();
1865 if (index < 0 || index > m_InnerCount)
1867 ThrowNewArgumentOutOfRangeException ("index", index,
1868 "Index must be >= 0 and <= Count.");
1871 m_InnerArrayList.Insert(m_InnerIndex + index, value);
1875 m_InnerStateChanges = m_InnerArrayList._version;
1878 public override void InsertRange(int index, ICollection c)
1880 VerifyStateChanges();
1882 if (index < 0 || index > m_InnerCount)
1884 ThrowNewArgumentOutOfRangeException ("index", index,
1885 "Index must be >= 0 and <= Count.");
1888 m_InnerArrayList.InsertRange(m_InnerIndex + index, c);
1890 m_InnerCount += c.Count;
1892 m_InnerStateChanges = m_InnerArrayList._version;
1895 public override void Remove(object value)
1897 VerifyStateChanges();
1899 int x = IndexOf(value);
1906 m_InnerStateChanges = m_InnerArrayList._version;
1909 public override void RemoveAt(int index)
1911 VerifyStateChanges();
1913 if (index < 0 || index > m_InnerCount)
1915 ThrowNewArgumentOutOfRangeException ("index", index,
1916 "Index must be >= 0 and <= Count.");
1919 m_InnerArrayList.RemoveAt(m_InnerIndex + index);
1922 m_InnerStateChanges = m_InnerArrayList._version;
1925 public override void RemoveRange(int index, int count)
1927 VerifyStateChanges();
1929 CheckRange(index, count, m_InnerCount);
1931 m_InnerArrayList.RemoveRange(m_InnerIndex + index, count);
1933 m_InnerCount -= count;
1935 m_InnerStateChanges = m_InnerArrayList._version;
1938 public override void Reverse()
1940 Reverse(0, m_InnerCount);
1943 public override void Reverse(int index, int count)
1945 VerifyStateChanges();
1947 CheckRange(index, count, m_InnerCount);
1949 m_InnerArrayList.Reverse(m_InnerIndex + index, count);
1951 m_InnerStateChanges = m_InnerArrayList._version;
1954 public override void SetRange(int index, ICollection c)
1956 VerifyStateChanges();
1958 if (index < 0 || index > m_InnerCount)
1960 ThrowNewArgumentOutOfRangeException ("index", index,
1961 "Index must be >= 0 and <= Count.");
1964 m_InnerArrayList.SetRange(m_InnerIndex + index, c);
1966 m_InnerStateChanges = m_InnerArrayList._version;
1969 public override void CopyTo(System.Array array)
1974 public override void CopyTo(System.Array array, int index)
1976 CopyTo(0, array, index, m_InnerCount);
1979 public override void CopyTo(int index, System.Array array, int arrayIndex, int count)
1981 CheckRange(index, count, m_InnerCount);
1983 m_InnerArrayList.CopyTo(m_InnerIndex + index, array, arrayIndex, count);
1986 public override IEnumerator GetEnumerator()
1988 return GetEnumerator(0, m_InnerCount);
1991 public override IEnumerator GetEnumerator(int index, int count)
1993 CheckRange(index, count, m_InnerCount);
1995 return m_InnerArrayList.GetEnumerator(m_InnerIndex + index, count);
1998 public override void AddRange(ICollection c)
2000 VerifyStateChanges();
2002 m_InnerArrayList.InsertRange(m_InnerCount, c);
2004 m_InnerCount += c.Count;
2006 m_InnerStateChanges = m_InnerArrayList._version;
2009 public override int BinarySearch(object value)
2011 return BinarySearch(0, m_InnerCount, value, Comparer.Default);
2014 public override int BinarySearch(object value, IComparer comparer)
2016 return BinarySearch(0, m_InnerCount, value, comparer);
2019 public override int BinarySearch(int index, int count, object value, IComparer comparer)
2021 CheckRange(index, count, m_InnerCount);
2023 return m_InnerArrayList.BinarySearch(m_InnerIndex + index, count, value, comparer);
2026 public override object Clone()
2028 return new RangedArrayList((ArrayList)m_InnerArrayList.Clone(), m_InnerIndex, m_InnerCount);
2031 public override ArrayList GetRange(int index, int count)
2033 CheckRange(index, count, m_InnerCount);
2035 return new RangedArrayList(this, index, count);
2038 public override void TrimToSize()
2040 throw new NotSupportedException();
2043 public override void Sort()
2045 Sort(Comparer.Default);
2048 public override void Sort(IComparer comparer)
2050 Sort(0, m_InnerCount, comparer);
2053 public override void Sort(int index, int count, IComparer comparer)
2055 VerifyStateChanges();
2057 CheckRange(index, count, m_InnerCount);
2059 m_InnerArrayList.Sort(m_InnerIndex + index, count, comparer);
2061 m_InnerStateChanges = m_InnerArrayList._version;
2064 public override object[] ToArray()
2068 array = new object[m_InnerCount];
2070 m_InnerArrayList.CopyTo (m_InnerIndex, array, 0, m_InnerCount);
2075 public override Array ToArray(Type elementType)
2079 array = Array.CreateInstance(elementType, m_InnerCount);
2081 m_InnerArrayList.CopyTo(m_InnerIndex, array, 0, m_InnerCount);
2095 #region SynchronizedListWrapper
2098 private sealed class SynchronizedListWrapper
2101 private object m_SyncRoot;
2103 public SynchronizedListWrapper(IList innerList)
2106 m_SyncRoot = innerList.SyncRoot;
2109 public override int Count
2115 return m_InnerList.Count;
2120 public override bool IsSynchronized
2128 public override object SyncRoot
2134 return m_InnerList.SyncRoot;
2139 public override bool IsFixedSize
2145 return m_InnerList.IsFixedSize;
2150 public override bool IsReadOnly
2156 return m_InnerList.IsReadOnly;
2161 public override object this[int index]
2167 return m_InnerList[index];
2175 m_InnerList[index] = value;
2180 public override int Add(object value)
2184 return m_InnerList.Add(value);
2188 public override void Clear()
2192 m_InnerList.Clear();
2196 public override bool Contains(object value)
2200 return m_InnerList.Contains(value);
2204 public override int IndexOf(object value)
2208 return m_InnerList.IndexOf(value);
2212 public override void Insert(int index, object value)
2216 m_InnerList.Insert(index, value);
2220 public override void Remove(object value)
2224 m_InnerList.Remove(value);
2228 public override void RemoveAt(int index)
2232 m_InnerList.RemoveAt(index);
2236 public override void CopyTo(Array array, int index)
2240 m_InnerList.CopyTo(array, index);
2244 public override IEnumerator GetEnumerator()
2248 return m_InnerList.GetEnumerator();
2255 #region FixedSizeListWrapper
2258 private class FixedSizeListWrapper
2261 protected virtual string ErrorMessage
2265 return "List is fixed-size.";
2269 public override bool IsFixedSize
2277 public FixedSizeListWrapper(IList innerList)
2282 public override int Add(object value)
2284 throw new NotSupportedException(this.ErrorMessage);
2287 public override void Clear()
2289 throw new NotSupportedException(this.ErrorMessage);
2292 public override void Insert(int index, object value)
2294 throw new NotSupportedException(this.ErrorMessage);
2297 public override void Remove(object value)
2299 throw new NotSupportedException(this.ErrorMessage);
2302 public override void RemoveAt(int index)
2304 throw new NotSupportedException(this.ErrorMessage);
2310 #region ReadOnlyListWrapper
2313 private sealed class ReadOnlyListWrapper
2314 : FixedSizeListWrapper
2316 protected override string ErrorMessage
2320 return "List is read-only.";
2324 public override bool IsReadOnly
2332 public ReadOnlyListWrapper(IList innerList)
2337 public override object this[int index]
2341 return m_InnerList[index];
2346 throw new NotSupportedException(this.ErrorMessage);
2356 /// Decorates/Wraps any <c>IList</c> implementing object.
2359 private class ListWrapper
2364 protected IList m_InnerList;
2368 #region Constructors
2370 public ListWrapper(IList innerList)
2372 m_InnerList = innerList;
2379 public virtual object this[int index]
2383 return m_InnerList[index];
2388 m_InnerList[index] = value;
2396 public virtual int Count
2400 return m_InnerList.Count;
2404 public virtual bool IsSynchronized
2408 return m_InnerList.IsSynchronized;
2412 public virtual object SyncRoot
2416 return m_InnerList.SyncRoot;
2420 public virtual bool IsFixedSize
2424 return m_InnerList.IsFixedSize;
2428 public virtual bool IsReadOnly
2432 return m_InnerList.IsReadOnly;
2440 public virtual int Add(object value)
2442 return m_InnerList.Add(value);
2445 public virtual void Clear()
2447 m_InnerList.Clear();
2450 public virtual bool Contains(object value)
2452 return m_InnerList.Contains(value);
2455 public virtual int IndexOf(object value)
2457 return m_InnerList.IndexOf(value);
2460 public virtual void Insert(int index, object value)
2462 m_InnerList.Insert(index, value);
2465 public virtual void Remove(object value)
2467 m_InnerList.Remove(value);
2470 public virtual void RemoveAt(int index)
2472 m_InnerList.RemoveAt(index);
2475 public virtual void CopyTo(Array array, int index)
2477 m_InnerList.CopyTo(array, index);
2480 public virtual IEnumerator GetEnumerator()
2482 return m_InnerList.GetEnumerator();
2491 // Start of ArrayList
2496 private const int DefaultInitialCapacity = 4;
2499 /// Array to store the items.
2501 private object[] _items;
2504 /// Number of items in the list.
2509 /// Total number of state changes.
2511 private int _version;
2515 #region Constructors
2518 /// Initializes a new instance of the <see cref="ArrayList"/> class that is empty and
2519 /// has the default initial capacity (16).
2523 _items = EmptyArray<object>.Value;
2527 /// Initializes a new instance of the <see cref="ArrayList"/> class that contains
2528 /// elements copied from the specified collection and that has the same initial capacity
2529 /// as the number of elements copied.
2531 /// <param name="c">
2532 /// The <see cref="ICollection"/> whose elements are copied into the new list.
2534 /// <exception cref="ArgumentNullException">
2535 /// The argument <c>c</c> is a null reference.
2537 public ArrayList(ICollection c)
2543 throw new ArgumentNullException("c");
2548 if (array != null && array.Rank != 1)
2550 throw new RankException();
2553 _items = new object[c.Count];
2559 /// Initializes a new instance of the <see cref="ArrayList"/> class that is empty and
2560 /// has the specified initial capacity.
2562 /// <param name="capacity">
2563 /// The number of elements that hte new list is initially capable of storing.
2565 /// <exception cref="ArgumentOutOfRangeException">
2566 /// The <c>capacity</c> is less than zero.
2568 public ArrayList(int capacity)
2572 ThrowNewArgumentOutOfRangeException ("capacity",
2573 capacity, "The initial capacity can't be smaller than zero.");
2578 capacity = DefaultInitialCapacity;
2580 _items = new object [capacity];
2584 /// Used by ArrayListAdapter to allow creation of an ArrayList with no storage buffer.
2586 private ArrayList(int initialCapacity, bool forceZeroSize)
2594 throw new InvalidOperationException("Use ArrayList(int)");
2599 /// Initializes a new array list that contains a copy of the given array and with the
2602 /// <param name="array"></param>
2603 private ArrayList(object[] array, int index, int count)
2607 _items = new object[DefaultInitialCapacity];
2611 _items = new object[count];
2614 Array.Copy(array, index, _items, 0, count);
2624 /// Gets/Sets an element in the list by index.
2626 /// <exception cref="ArgumentOutOfRangeException">
2627 /// The index is less than 0 or more then or equal to the list count.
2629 public virtual object this[int index]
2633 if (index < 0 || index >= _size)
2635 ThrowNewArgumentOutOfRangeException ("index", index,
2636 "Index is less than 0 or more than or equal to the list count.");
2639 return _items[index];
2644 if (index < 0 || index >= _size)
2646 ThrowNewArgumentOutOfRangeException ("index", index,
2647 "Index is less than 0 or more than or equal to the list count.");
2650 _items[index] = value;
2660 /// Gets the number of elements in the list.
2662 public virtual int Count
2671 /// Gets the number of elements the list can carry without needing to expand.
2674 /// ArrayLists automatically double their capacity when the capacity limit is broken.
2676 /// <exception cref="ArgumentOutOfRangeException">
2677 /// The capacity is less than the count.
2679 public virtual int Capacity
2683 return _items.Length;
2690 ThrowNewArgumentOutOfRangeException ("Capacity", value,
2691 "Must be more than count.");
2696 newArray = new object[value];
2698 Array.Copy(_items, 0, newArray, 0, _size);
2705 /// <see cref="IList.IsFixedSize"/>
2708 public virtual bool IsFixedSize
2717 /// <see cref="IList.IsReadOnly"/>
2719 public virtual bool IsReadOnly
2728 /// <see cref="ICollection.IsSynchronized"/>
2730 public virtual bool IsSynchronized
2739 /// <see cref="ICollection.SyncRoot"/>
2741 public virtual object SyncRoot
2754 /// Ensures that the list has the capacity to contain the given <c>count</c> by
2755 /// automatically expanding the capacity when required.
2757 private void EnsureCapacity(int count)
2759 if (count <= _items.Length)
2767 newLength = _items.Length << 1;
2769 newLength = DefaultInitialCapacity;
2771 while (newLength < count)
2776 newData = new object[newLength];
2778 Array.Copy(_items, 0, newData, 0, _items.Length);
2784 /// Shifts a section of the list.
2786 /// <param name="index">
2787 /// The start of the section to shift (the element at index is included in the shift).
2789 /// <param name="count">
2790 /// The number of positions to shift by (can be negative).
2792 private void Shift(int index, int count)
2796 if (_size + count > _items.Length)
2801 newLength = (_items.Length > 0) ? _items.Length << 1 : 1;
2803 while (newLength < _size + count)
2808 newData = new object[newLength];
2810 Array.Copy(_items, 0, newData, 0, index);
2811 Array.Copy(_items, index, newData, index + count, _size - index);
2817 Array.Copy(_items, index, _items, index + count, _size - index);
2822 // Remember count is negative so this is actually index + (-count)
2824 int x = index - count ;
2826 Array.Copy(_items, x, _items, index, _size - x);
2827 Array.Clear(_items, _size + count, - count);
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 item)
2859 return IndexOf(item, 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 ThrowNewArgumentOutOfRangeException ("startIndex", startIndex,
2882 "Does not specify valid index.");
2887 ThrowNewArgumentOutOfRangeException ("count", count,
2888 "Can't be less than 0.");
2891 // re-ordered to avoid integer overflow
2892 if (startIndex > _size - count)
2894 // LAMESPEC: Every other method throws ArgumentException
2896 throw new ArgumentOutOfRangeException("count",
2897 "Start index and count do not specify a valid range.");
2900 return Array.IndexOf(_items, value, startIndex, count);
2903 public virtual int LastIndexOf(object value)
2905 return LastIndexOf(value, _size - 1);
2908 public virtual int LastIndexOf(object value, int startIndex)
2910 return LastIndexOf(value, startIndex, startIndex + 1);
2913 public virtual int LastIndexOf (object value, int startIndex, int count)
2915 // Array will catch the exceptions
2916 return Array.LastIndexOf (_items, value, startIndex, count);
2919 public virtual void Insert(int index, object value)
2921 if (index < 0 || index > _size)
2923 ThrowNewArgumentOutOfRangeException ("index", index,
2924 "Index must be >= 0 and <= Count.");
2929 _items[index] = value;
2934 public virtual void InsertRange(int index, ICollection c)
2940 throw new ArgumentNullException("c");
2943 if (index < 0 || index > _size)
2945 ThrowNewArgumentOutOfRangeException ("index", index,
2946 "Index must be >= 0 and <= Count.");
2951 // Do a check here in case EnsureCapacity isn't inlined.
2953 if (_items.Length < _size + i)
2955 EnsureCapacity(_size + i);
2960 Array.Copy(_items, index, _items, index + i, _size - index);
2963 // Handle inserting a range from a list to itself specially.
2965 if (this == c.SyncRoot)
2967 // Copy range before the insert point.
2969 Array.Copy(_items, 0, _items, index, index);
2971 // Copy range after the insert point.
2973 Array.Copy(_items, index + i, _items, index << 1, _size - index);
2977 c.CopyTo(_items, index);
2984 public virtual void Remove(object obj)
2998 public virtual void RemoveAt(int index)
3000 if (index < 0 || index >= _size)
3002 ThrowNewArgumentOutOfRangeException ("index", index,
3003 "Less than 0 or more than list count.");
3011 public virtual void RemoveRange(int index, int count)
3013 ArrayList.CheckRange(index, count, _size);
3015 Shift(index, -count);
3020 public virtual void Reverse()
3022 Array.Reverse(_items, 0, _size);
3026 public virtual void Reverse(int index, int count)
3028 ArrayList.CheckRange(index, count, _size);
3030 Array.Reverse(_items, index, count);
3034 public virtual void CopyTo(System.Array array)
3036 Array.Copy(_items, array, _size);
3039 public virtual void CopyTo(System.Array array, int arrayIndex)
3041 CopyTo(0, array, arrayIndex, _size);
3044 public virtual void CopyTo(int index, System.Array array, int arrayIndex, int count)
3048 throw new ArgumentNullException("array");
3051 if (array.Rank != 1)
3054 // This should be a RankException because Array.Copy throws RankException.
3056 throw new ArgumentException("Must have only 1 dimensions.", "array");
3059 Array.Copy(_items, index, array, arrayIndex, count);
3062 public virtual IEnumerator GetEnumerator()
3064 return new SimpleEnumerator(this);
3067 public virtual IEnumerator GetEnumerator(int index, int count)
3069 ArrayList.CheckRange(index, count, _size);
3071 return new ArrayListEnumerator(this, index, count);
3074 public virtual void AddRange(ICollection c)
3076 InsertRange(_size, c);
3079 public virtual int BinarySearch(object value)
3083 return Array.BinarySearch(_items, 0, _size, value);
3085 catch (InvalidOperationException e)
3087 throw new ArgumentException(e.Message);
3091 public virtual int BinarySearch(object value, IComparer comparer)
3095 return Array.BinarySearch(_items, 0, _size, value, comparer);
3097 catch (InvalidOperationException e)
3099 throw new ArgumentException(e.Message);
3103 public virtual int BinarySearch(int index, int count, object value, IComparer comparer)
3107 return Array.BinarySearch(_items, index, count, value, comparer);
3109 catch (InvalidOperationException e)
3111 throw new ArgumentException(e.Message);
3114 public virtual ArrayList GetRange(int index, int count)
3116 ArrayList.CheckRange(index, count, _size);
3118 if (this.IsSynchronized)
3120 return ArrayList.Synchronized(new RangedArrayList(this, index, count));
3124 return new RangedArrayList(this, index, count);
3128 public virtual void SetRange(int index, ICollection c)
3132 throw new ArgumentNullException("c");
3135 if (index < 0 || index + c.Count > _size)
3137 throw new ArgumentOutOfRangeException("index");
3140 c.CopyTo(_items, index);
3145 public virtual void TrimToSize()
3147 if (_items.Length > _size)
3153 newArray = new object[DefaultInitialCapacity];
3157 newArray = new object[_size];
3160 Array.Copy(_items, 0, newArray, 0, _size);
3166 public virtual void Sort()
3168 Array.Sort(_items, 0, _size);
3173 public virtual void Sort(IComparer comparer)
3175 Array.Sort(_items, 0, _size, comparer);
3178 public virtual void Sort(int index, int count, IComparer comparer)
3180 ArrayList.CheckRange(index, count, _size);
3182 Array.Sort(_items, index, count, comparer);
3185 public virtual object[] ToArray()
3189 retval = new object[_size];
3196 public virtual Array ToArray(Type type)
3200 retval = Array.CreateInstance(type, _size);
3207 public virtual object Clone()
3209 return new ArrayList(this._items, 0, this._size);
3214 #region Static Methods
3217 /// Does a check of the arguments many of the methods in ArrayList use.
3220 /// The choice of exceptions thrown sometimes seem to be arbitrarily chosen so
3221 /// not all methods actually make use of CheckRange.
3223 internal static void CheckRange(int index, int count, int listCount)
3227 ThrowNewArgumentOutOfRangeException ("index", index, "Can't be less than 0.");
3232 ThrowNewArgumentOutOfRangeException ("count", count, "Can't be less than 0.");
3235 // re-ordered to avoid possible integer overflow
3236 if (index > listCount - count)
3238 throw new ArgumentException("Index and count do not denote a valid range of elements.", "index");
3242 internal static void ThrowNewArgumentOutOfRangeException (string name, object actual, string message)
3244 throw new ArgumentOutOfRangeException (
3245 #if !INSIDE_CORLIB && NET_2_1
3248 name, actual, message
3253 public static ArrayList Adapter(IList list)
3255 // LAMESPEC: EWWW. Other lists aren't *Array*Lists.
3259 throw new ArgumentNullException("list");
3262 ArrayList arrayList = list as ArrayList;
3263 if (arrayList != null)
3266 arrayList = new ArrayListAdapter(list);
3268 if (list.IsSynchronized)
3269 return ArrayList.Synchronized(arrayList);
3274 public static ArrayList Synchronized(ArrayList list)
3278 throw new ArgumentNullException("list");
3281 if (list.IsSynchronized)
3286 return new SynchronizedArrayListWrapper(list);
3289 public static IList Synchronized(IList list)
3293 throw new ArgumentNullException("list");
3296 if (list.IsSynchronized)
3301 return new SynchronizedListWrapper(list);
3304 public static ArrayList ReadOnly(ArrayList list)
3308 throw new ArgumentNullException("list");
3311 if (list.IsReadOnly)
3316 return new ReadOnlyArrayListWrapper(list);
3319 public static IList ReadOnly(IList list)
3323 throw new ArgumentNullException("list");
3326 if (list.IsReadOnly)
3331 return new ReadOnlyListWrapper(list);
3334 public static ArrayList FixedSize(ArrayList list)
3338 throw new ArgumentNullException("list");
3341 if (list.IsFixedSize)
3346 return new FixedSizeArrayListWrapper(list);
3349 public static IList FixedSize(IList list)
3353 throw new ArgumentNullException("list");
3356 if (list.IsFixedSize)
3361 return new FixedSizeListWrapper(list);
3364 public static ArrayList Repeat(object value, int count)
3366 ArrayList arrayList = new ArrayList(count);
3368 for (int i = 0; i < count; i++)
3370 arrayList.Add(value);