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
35 [DebuggerDisplay ("Count={Count}")]
44 : IList, ICloneable, ICollection, IEnumerable
48 private sealed class ArrayListEnumerator
49 : IEnumerator, ICloneable
54 private object m_Current;
55 private ArrayList m_List;
56 private int m_ExpectedStateChanges;
58 public ArrayListEnumerator(ArrayList list)
59 : this(list, 0, list.Count)
65 return this.MemberwiseClone();
68 public ArrayListEnumerator(ArrayList list, int index, int count)
75 m_ExpectedStateChanges = list._version;
82 if (m_Pos == m_Index - 1) {
83 throw new InvalidOperationException("Enumerator unusable (Reset pending, or past end of array.");
90 public bool MoveNext()
92 if (m_List._version != m_ExpectedStateChanges)
94 throw new InvalidOperationException("List has changed.");
99 if (m_Pos - m_Index < m_Count)
101 m_Current = m_List[m_Pos];
116 sealed class SimpleEnumerator : IEnumerator, ICloneable
121 object currentElement;
122 static object endFlag = new object ();
124 public SimpleEnumerator (ArrayList list)
128 version = list._version;
129 currentElement = endFlag;
132 public object Clone ()
134 return MemberwiseClone ();
137 public bool MoveNext ()
139 if (version != list._version)
140 throw new InvalidOperationException("List has changed.");
142 if (++index < list.Count) {
143 currentElement = list [index];
146 currentElement = endFlag;
151 public object Current {
153 if (currentElement == endFlag) {
155 throw new InvalidOperationException ("Enumerator not started");
157 throw new InvalidOperationException ("Enumerator ended");
160 return currentElement;
166 if (version != list._version)
167 throw new InvalidOperationException ("List has changed.");
169 currentElement = endFlag;
176 #region ArrayListAdapter
179 /// Adapts various ILists into an ArrayList.
182 private sealed class ArrayListAdapter
185 private sealed class EnumeratorWithRange
186 : IEnumerator, ICloneable
188 private int m_StartIndex;
190 private int m_MaxCount;
191 private IEnumerator m_Enumerator;
193 public EnumeratorWithRange(IEnumerator enumerator, int index, int count)
196 m_StartIndex = index;
198 m_Enumerator = enumerator;
203 public object Clone()
205 return this.MemberwiseClone();
208 public object Current
212 return m_Enumerator.Current;
216 public bool MoveNext()
218 if (m_Count >= m_MaxCount)
225 return m_Enumerator.MoveNext();
231 m_Enumerator.Reset();
233 for (int i = 0; i < m_StartIndex; i++)
235 m_Enumerator.MoveNext();
240 private IList m_Adaptee;
242 public ArrayListAdapter(IList adaptee)
248 public override object this[int index]
252 return m_Adaptee[index];
257 m_Adaptee[index] = value;
261 public override int Count
265 return m_Adaptee.Count;
269 public override int Capacity
273 return m_Adaptee.Count;
278 if (value < m_Adaptee.Count)
280 throw new ArgumentException("capacity");
285 public override bool IsFixedSize
289 return m_Adaptee.IsFixedSize;
293 public override bool IsReadOnly
297 return m_Adaptee.IsReadOnly;
301 public override object SyncRoot
305 return m_Adaptee.SyncRoot;
309 public override int Add(object value)
311 return m_Adaptee.Add(value);
314 public override void Clear()
319 public override bool Contains(object value)
321 return m_Adaptee.Contains(value);
324 public override int IndexOf(object value)
326 return m_Adaptee.IndexOf(value);
329 public override int IndexOf(object value, int startIndex)
331 return IndexOf(value, startIndex, m_Adaptee.Count - startIndex);
334 public override int IndexOf(object value, int startIndex, int count)
336 if (startIndex < 0 || startIndex > m_Adaptee.Count)
338 ThrowNewArgumentOutOfRangeException ("startIndex", startIndex,
339 "Does not specify valid index.");
344 ThrowNewArgumentOutOfRangeException ("count", count,
345 "Can't be less than 0.");
348 // re-ordered to avoid possible integer overflow
349 if (startIndex > m_Adaptee.Count - count) {
350 // LAMESPEC: Every other method throws ArgumentException
351 throw new ArgumentOutOfRangeException("count",
352 "Start index and count do not specify a valid range.");
357 for (int i = startIndex; i < startIndex + count; i++)
359 if (m_Adaptee[i] == null)
367 for (int i = startIndex; i < startIndex + count; i++)
369 if (value.Equals(m_Adaptee[i]))
379 public override int LastIndexOf(object value)
381 return LastIndexOf(value, m_Adaptee.Count - 1);
384 public override int LastIndexOf(object value, int startIndex)
386 return LastIndexOf(value, startIndex, startIndex + 1);
389 public override int LastIndexOf(object value, int startIndex, int count)
393 ThrowNewArgumentOutOfRangeException ("startIndex", startIndex, "< 0");
398 ThrowNewArgumentOutOfRangeException ("count", count, "count is negative.");
401 if (startIndex - count + 1 < 0)
403 ThrowNewArgumentOutOfRangeException ("count", count, "count is too large.");
408 for (int i = startIndex; i > startIndex - count; i--)
410 if (m_Adaptee[i] == null)
418 for (int i = startIndex; i > startIndex - count; i--)
420 if (value.Equals(m_Adaptee[i]))
430 public override void Insert(int index, object value)
432 m_Adaptee.Insert(index, value);
435 public override void InsertRange(int index, ICollection c)
439 throw new ArgumentNullException("c");
442 if (index > m_Adaptee.Count)
444 ThrowNewArgumentOutOfRangeException ("index", index,
445 "Index must be >= 0 and <= Count.");
448 foreach (object value in c)
450 m_Adaptee.Insert(index++, value);
454 public override void Remove(object value)
456 m_Adaptee.Remove(value);
459 public override void RemoveAt(int index)
461 m_Adaptee.RemoveAt(index);
464 public override void RemoveRange(int index, int count)
466 CheckRange(index, count, m_Adaptee.Count);
468 for (int i = 0; i < count; i++)
470 m_Adaptee.RemoveAt(index);
474 public override void Reverse()
476 Reverse(0, m_Adaptee.Count);
479 public override void Reverse(int index, int count)
483 CheckRange(index, count, m_Adaptee.Count);
485 for (int i = 0; i < count / 2; i++)
487 tmp = m_Adaptee[i + index];
488 m_Adaptee[i + index] = m_Adaptee[(index + count) - i + index - 1];
489 m_Adaptee[(index + count) - i + index - 1] = tmp;
493 public override void SetRange(int index, ICollection c)
497 throw new ArgumentNullException("c");
500 if (index < 0 || index + c.Count > m_Adaptee.Count)
502 throw new ArgumentOutOfRangeException("index");
507 foreach (object value in c)
509 m_Adaptee[x++] = value;
513 public override void CopyTo(System.Array array)
515 m_Adaptee.CopyTo(array, 0);
518 public override void CopyTo(System.Array array, int index)
520 m_Adaptee.CopyTo(array, index);
523 public override void CopyTo(int index, System.Array array, int arrayIndex, int count)
527 ThrowNewArgumentOutOfRangeException ("index", index,
528 "Can't be less than zero.");
533 ThrowNewArgumentOutOfRangeException ("arrayIndex", arrayIndex,
534 "Can't be less than zero.");
539 ThrowNewArgumentOutOfRangeException ("index", index,
540 "Can't be less than zero.");
543 if (index >= m_Adaptee.Count)
545 throw new ArgumentException("Can't be more or equal to list count.",
551 throw new ArgumentException("Can't copy into multi-dimensional array.");
554 if (arrayIndex >= array.Length)
556 throw new ArgumentException("arrayIndex can't be greater than array.Length - 1.");
559 if (array.Length - arrayIndex + 1 < count)
561 throw new ArgumentException("Destination array is too small.");
564 // re-ordered to avoid possible integer overflow
565 if (index > m_Adaptee.Count - count) {
566 throw new ArgumentException("Index and count do not denote a valid range of elements.", "index");
569 for (int i = 0; i < count; i++)
571 array.SetValue(m_Adaptee[index + i], arrayIndex + i);
575 public override bool IsSynchronized
579 return m_Adaptee.IsSynchronized;
583 public override IEnumerator GetEnumerator()
585 return m_Adaptee.GetEnumerator();
588 public override IEnumerator GetEnumerator(int index, int count)
590 CheckRange(index, count, m_Adaptee.Count);
592 return new EnumeratorWithRange(m_Adaptee.GetEnumerator(), index, count);
595 public override void AddRange(ICollection c)
597 foreach (object value in c)
599 m_Adaptee.Add(value);
603 public override int BinarySearch(object value)
605 return BinarySearch(value, null);
608 public override int BinarySearch(object value, IComparer comparer)
610 return BinarySearch(0, m_Adaptee.Count, value, comparer);
613 public override int BinarySearch(int index, int count, object value, IComparer comparer)
617 // Doing a direct BinarySearch on the adaptee will perform poorly if the adaptee is a linked-list.
618 // Alternatives include copying the adaptee to a temporary array first.
620 CheckRange(index, count, m_Adaptee.Count);
622 if (comparer == null)
624 comparer = Comparer.Default;
628 y = index + count - 1;
632 // Be careful with overflows
633 z = x + ((y - x) / 2);
635 r = comparer.Compare(value, m_Adaptee[z]);
654 public override object Clone()
656 return new ArrayList.ArrayListAdapter(m_Adaptee);
659 public override ArrayList GetRange(int index, int count)
661 CheckRange(index, count, m_Adaptee.Count);
663 return new RangedArrayList(this, index, count);
666 public override void TrimToSize()
671 public override void Sort()
673 Sort(Comparer.Default);
676 public override void Sort(IComparer comparer)
678 Sort(0, m_Adaptee.Count, comparer);
681 public override void Sort(int index, int count, IComparer comparer)
683 CheckRange(index, count, m_Adaptee.Count);
685 if (comparer == null)
687 comparer = Comparer.Default;
690 // Doing a direct sort on the adaptee will perform poorly if the adaptee is a linked-list.
691 // Alternatives include copying the adaptee into a temporary array first.
693 QuickSort(m_Adaptee, index, index + count - 1, comparer);
698 /// Swaps two items in a list at the specified indexes.
700 private static void Swap(IList list, int x, int y)
710 /// Quicksort for lists.
713 /// This function acts as both qsort() and partition().
715 internal static void QuickSort(IList list, int left, int right, IComparer comparer)
725 // Pick the pivot using the median-of-three strategy.
727 // Be careful with overflows
728 middle = left + ((right - left) / 2);
730 if (comparer.Compare(list[middle], list[left]) < 0)
732 Swap(list, middle, left);
735 if (comparer.Compare(list[right], list[left]) < 0)
737 Swap(list, right, left);
740 if (comparer.Compare(list[right], list[middle]) < 0)
742 Swap(list, right, middle);
745 if (right - left + 1 <= 3)
750 // Put the pivot in right - 1.
751 Swap(list, right - 1, middle);
753 // List should look like:
755 // [Small] ..Numbers.. [Middle] ..Numbers.. [Pivot][Large]
757 pivot = list[right - 1];
759 // Sort from (left + 1) to (right - 2).
766 while (comparer.Compare(list[++i], pivot) < 0);
767 while (comparer.Compare(list[--j], pivot) > 0);
779 // Put pivot into the right position (real middle).
781 Swap(list, right - 1, i);
783 // Recursively sort the left and right sub lists.
785 QuickSort(list, left, i - 1, comparer);
786 QuickSort(list, i + 1, right, comparer);
789 public override object[] ToArray()
793 retval = new object[m_Adaptee.Count];
795 m_Adaptee.CopyTo(retval, 0);
800 public override Array ToArray(Type elementType)
804 retval = Array.CreateInstance(elementType, m_Adaptee.Count);
806 m_Adaptee.CopyTo(retval, 0);
812 #endregion // ArrayListAdapter
815 // ArrayList wrappers
818 #region ArrayListWrapper
821 /// Base wrapper/decorator for ArrayLists. Simply delegates all methods to
822 /// the underlying wrappee.
825 private class ArrayListWrapper
828 protected ArrayList m_InnerArrayList;
832 public ArrayListWrapper(ArrayList innerArrayList)
834 m_InnerArrayList = innerArrayList;
841 public override object this[int index]
845 return m_InnerArrayList[index];
850 m_InnerArrayList[index] = value;
858 public override int Count
862 return m_InnerArrayList.Count;
866 public override int Capacity
870 return m_InnerArrayList.Capacity;
875 m_InnerArrayList.Capacity = value;
879 public override bool IsFixedSize
883 return m_InnerArrayList.IsFixedSize;
887 public override bool IsReadOnly
891 return m_InnerArrayList.IsReadOnly;
895 public override bool IsSynchronized
899 return m_InnerArrayList.IsSynchronized;
903 public override object SyncRoot
907 return m_InnerArrayList.SyncRoot;
915 public override int Add(object value)
917 return m_InnerArrayList.Add(value);
920 public override void Clear()
922 m_InnerArrayList.Clear();
925 public override bool Contains(object value)
927 return m_InnerArrayList.Contains(value);
930 public override int IndexOf(object value)
932 return m_InnerArrayList.IndexOf(value);
935 public override int IndexOf(object value, int startIndex)
937 return m_InnerArrayList.IndexOf(value, startIndex);
940 public override int IndexOf(object value, int startIndex, int count)
942 return m_InnerArrayList.IndexOf(value, startIndex, count);
945 public override int LastIndexOf(object value)
947 return m_InnerArrayList.LastIndexOf(value);
950 public override int LastIndexOf(object value, int startIndex)
952 return m_InnerArrayList.LastIndexOf(value, startIndex);
955 public override int LastIndexOf(object value, int startIndex, int count)
957 return m_InnerArrayList.LastIndexOf(value, startIndex, count);
960 public override void Insert(int index, object value)
962 m_InnerArrayList.Insert(index, value);
965 public override void InsertRange(int index, ICollection c)
967 m_InnerArrayList.InsertRange(index, c);
970 public override void Remove(object value)
972 m_InnerArrayList.Remove(value);
975 public override void RemoveAt(int index)
977 m_InnerArrayList.RemoveAt(index);
980 public override void RemoveRange(int index, int count)
982 m_InnerArrayList.RemoveRange(index, count);
985 public override void Reverse()
987 m_InnerArrayList.Reverse();
990 public override void Reverse(int index, int count)
992 m_InnerArrayList.Reverse(index, count);
995 public override void SetRange(int index, ICollection c)
997 m_InnerArrayList.SetRange(index, c);
1000 public override void CopyTo(System.Array array)
1002 m_InnerArrayList.CopyTo(array);
1005 public override void CopyTo(System.Array array, int index)
1007 m_InnerArrayList.CopyTo(array, index);
1010 public override void CopyTo(int index, System.Array array, int arrayIndex, int count)
1012 m_InnerArrayList.CopyTo(index, array, arrayIndex, count);
1015 public override IEnumerator GetEnumerator()
1017 return m_InnerArrayList.GetEnumerator();
1020 public override IEnumerator GetEnumerator(int index, int count)
1022 return m_InnerArrayList.GetEnumerator(index, count);
1025 public override void AddRange(ICollection c)
1027 m_InnerArrayList.AddRange(c);
1030 public override int BinarySearch(object value)
1032 return m_InnerArrayList.BinarySearch(value);
1035 public override int BinarySearch(object value, IComparer comparer)
1037 return m_InnerArrayList.BinarySearch(value, comparer);
1040 public override int BinarySearch(int index, int count, object value, IComparer comparer)
1042 return m_InnerArrayList.BinarySearch(index, count, value, comparer);
1045 public override object Clone()
1047 return m_InnerArrayList.Clone();
1050 public override ArrayList GetRange(int index, int count)
1052 return m_InnerArrayList.GetRange(index, count);
1055 public override void TrimToSize()
1057 m_InnerArrayList.TrimToSize();
1060 public override void Sort()
1062 m_InnerArrayList.Sort();
1065 public override void Sort(IComparer comparer)
1067 m_InnerArrayList.Sort(comparer);
1070 public override void Sort(int index, int count, IComparer comparer)
1072 m_InnerArrayList.Sort(index, count, comparer);
1075 public override object[] ToArray()
1077 return m_InnerArrayList.ToArray();
1080 public override Array ToArray(Type elementType)
1082 return m_InnerArrayList.ToArray(elementType);
1090 #region SynchronizedArrayListWrapper
1093 /// ArrayListWrapper that synchronizes calls to all methods/properties.
1096 /// Works by just synchronizing all method calls. In the future careful optimisation
1097 /// could give better performance...
1100 private sealed class SynchronizedArrayListWrapper
1103 private object m_SyncRoot;
1105 #region Constructors
1108 /// Creates a new synchronized wrapper for the given <see cref="ArrayList"/>.
1110 /// <param name="innerArrayList"></param>
1111 internal SynchronizedArrayListWrapper(ArrayList innerArrayList)
1112 : base(innerArrayList)
1114 m_SyncRoot = innerArrayList.SyncRoot;
1121 public override object this[int index]
1127 return m_InnerArrayList[index];
1135 m_InnerArrayList[index] = value;
1144 // Some of these properties may be calculated so it's best to synchronize
1145 // them even though it might cause a performance hit.
1146 // Better safe than sorry ;D.
1148 public override int Count
1154 return m_InnerArrayList.Count;
1159 public override int Capacity
1165 return m_InnerArrayList.Capacity;
1173 m_InnerArrayList.Capacity = value;
1178 public override bool IsFixedSize
1184 return m_InnerArrayList.IsFixedSize;
1189 public override bool IsReadOnly
1195 return m_InnerArrayList.IsReadOnly;
1200 public override bool IsSynchronized
1208 public override object SyncRoot
1220 public override int Add(object value)
1224 return m_InnerArrayList.Add(value);
1228 public override void Clear()
1232 m_InnerArrayList.Clear();
1236 public override bool Contains(object value)
1240 return m_InnerArrayList.Contains(value);
1244 public override int IndexOf(object value)
1248 return m_InnerArrayList.IndexOf(value);
1252 public override int IndexOf(object value, int startIndex)
1256 return m_InnerArrayList.IndexOf(value, startIndex);
1260 public override int IndexOf(object value, int startIndex, int count)
1264 return m_InnerArrayList.IndexOf(value, startIndex, count);
1268 public override int LastIndexOf(object value)
1272 return m_InnerArrayList.LastIndexOf(value);
1276 public override int LastIndexOf(object value, int startIndex)
1280 return m_InnerArrayList.LastIndexOf(value, startIndex);
1284 public override int LastIndexOf(object value, int startIndex, int count)
1288 return m_InnerArrayList.LastIndexOf(value, startIndex, count);
1292 public override void Insert(int index, object value)
1296 m_InnerArrayList.Insert(index, value);
1300 public override void InsertRange(int index, ICollection c)
1304 m_InnerArrayList.InsertRange(index, c);
1308 public override void Remove(object value)
1312 m_InnerArrayList.Remove(value);
1316 public override void RemoveAt(int index)
1320 m_InnerArrayList.RemoveAt(index);
1324 public override void RemoveRange(int index, int count)
1328 m_InnerArrayList.RemoveRange(index, count);
1332 public override void Reverse()
1336 m_InnerArrayList.Reverse();
1340 public override void Reverse(int index, int count)
1344 m_InnerArrayList.Reverse(index, count);
1348 public override void CopyTo(System.Array array)
1352 m_InnerArrayList.CopyTo(array);
1356 public override void CopyTo(System.Array array, int index)
1360 m_InnerArrayList.CopyTo(array, index);
1364 public override void CopyTo(int index, System.Array array, int arrayIndex, int count)
1368 m_InnerArrayList.CopyTo(index, array, arrayIndex, count);
1372 public override IEnumerator GetEnumerator()
1376 return m_InnerArrayList.GetEnumerator();
1380 public override IEnumerator GetEnumerator(int index, int count)
1384 return m_InnerArrayList.GetEnumerator(index, count);
1388 public override void AddRange(ICollection c)
1392 m_InnerArrayList.AddRange(c);
1396 public override int BinarySearch(object value)
1400 return m_InnerArrayList.BinarySearch(value);
1404 public override int BinarySearch(object value, IComparer comparer)
1408 return m_InnerArrayList.BinarySearch(value, comparer);
1412 public override int BinarySearch(int index, int count, object value, IComparer comparer)
1416 return m_InnerArrayList.BinarySearch(index, count, value, comparer);
1420 public override object Clone()
1424 return m_InnerArrayList.Clone();
1428 public override ArrayList GetRange(int index, int count)
1432 return m_InnerArrayList.GetRange(index, count);
1436 public override void TrimToSize()
1440 m_InnerArrayList.TrimToSize();
1444 public override void Sort()
1448 m_InnerArrayList.Sort();
1452 public override void Sort(IComparer comparer)
1456 m_InnerArrayList.Sort(comparer);
1460 public override void Sort(int index, int count, IComparer comparer)
1464 m_InnerArrayList.Sort(index, count, comparer);
1468 public override object[] ToArray()
1472 return m_InnerArrayList.ToArray();
1476 public override Array ToArray(Type elementType)
1480 return m_InnerArrayList.ToArray(elementType);
1489 #region FixedSizeArrayListWrapper
1492 private class FixedSizeArrayListWrapper
1495 #region Constructors
1497 public FixedSizeArrayListWrapper(ArrayList innerList)
1507 /// Gets the error message to display when an readonly/fixedsize related exception is
1510 protected virtual string ErrorMessage
1514 return "Can't add or remove from a fixed-size list.";
1518 public override int Capacity
1522 return base.Capacity;
1527 throw new NotSupportedException(this.ErrorMessage);
1531 public override bool IsFixedSize
1543 public override int Add(object value)
1545 throw new NotSupportedException(this.ErrorMessage);
1548 public override void AddRange(ICollection c)
1550 throw new NotSupportedException(this.ErrorMessage);
1553 public override void Clear()
1555 throw new NotSupportedException(this.ErrorMessage);
1558 public override void Insert(int index, object value)
1560 throw new NotSupportedException(this.ErrorMessage);
1563 public override void InsertRange(int index, ICollection c)
1565 throw new NotSupportedException(this.ErrorMessage);
1568 public override void Remove(object value)
1570 throw new NotSupportedException(this.ErrorMessage);
1573 public override void RemoveAt(int index)
1575 throw new NotSupportedException(this.ErrorMessage);
1578 public override void RemoveRange(int index, int count)
1580 throw new NotSupportedException(this.ErrorMessage);
1583 public override void TrimToSize()
1585 throw new NotSupportedException(this.ErrorMessage);
1593 #region ReadOnlyArrayListWrapper
1596 private sealed class ReadOnlyArrayListWrapper
1597 : FixedSizeArrayListWrapper
1599 protected override string ErrorMessage
1603 return "Can't modify a readonly list.";
1607 public override bool IsReadOnly
1615 public ReadOnlyArrayListWrapper(ArrayList innerArrayList)
1616 : base(innerArrayList)
1620 public override object this[int index]
1624 return m_InnerArrayList[index];
1629 throw new NotSupportedException(this.ErrorMessage);
1633 public override void Reverse()
1635 throw new NotSupportedException(this.ErrorMessage);
1638 public override void Reverse(int index, int count)
1640 throw new NotSupportedException(this.ErrorMessage);
1643 public override void SetRange(int index, ICollection c)
1645 throw new NotSupportedException(this.ErrorMessage);
1648 public override void Sort()
1650 throw new NotSupportedException(this.ErrorMessage);
1653 public override void Sort(IComparer comparer)
1655 throw new NotSupportedException(this.ErrorMessage);
1658 public override void Sort(int index, int count, IComparer comparer)
1660 throw new NotSupportedException(this.ErrorMessage);
1666 #region RangedArrayList
1669 private sealed class RangedArrayList
1672 private int m_InnerIndex;
1673 private int m_InnerCount;
1674 private int m_InnerStateChanges;
1676 public RangedArrayList(ArrayList innerList, int index, int count)
1679 m_InnerIndex = index;
1680 m_InnerCount = count;
1681 m_InnerStateChanges = innerList._version;
1686 public override bool IsSynchronized
1694 public override object this[int index]
1698 if (index < 0 || index > m_InnerCount)
1700 throw new ArgumentOutOfRangeException("index");
1703 return m_InnerArrayList[m_InnerIndex + index];
1708 if (index < 0 || index > m_InnerCount)
1710 throw new ArgumentOutOfRangeException("index");
1713 m_InnerArrayList[m_InnerIndex + index] = value;
1721 public override int Count
1725 VerifyStateChanges();
1727 return m_InnerCount;
1731 public override int Capacity
1735 return m_InnerArrayList.Capacity;
1740 if (value < m_InnerCount)
1742 throw new ArgumentOutOfRangeException();
1751 private void VerifyStateChanges()
1753 if (m_InnerStateChanges != m_InnerArrayList._version)
1755 throw new InvalidOperationException
1756 ("ArrayList view is invalid because the underlying ArrayList was modified.");
1760 public override int Add(object value)
1762 VerifyStateChanges();
1764 m_InnerArrayList.Insert(m_InnerIndex + m_InnerCount, value);
1766 m_InnerStateChanges = m_InnerArrayList._version;
1768 return ++m_InnerCount;
1771 public override void Clear()
1773 VerifyStateChanges();
1775 m_InnerArrayList.RemoveRange(m_InnerIndex, m_InnerCount);
1778 m_InnerStateChanges = m_InnerArrayList._version;
1781 public override bool Contains(object value)
1783 return m_InnerArrayList.Contains(value, m_InnerIndex, m_InnerCount);
1786 public override int IndexOf(object value)
1788 return IndexOf(value, 0);
1791 public override int IndexOf(object value, int startIndex)
1793 return IndexOf(value, startIndex, m_InnerCount - startIndex);
1796 public override int IndexOf(object value, int startIndex, int count)
1798 if (startIndex < 0 || startIndex > m_InnerCount)
1800 ThrowNewArgumentOutOfRangeException ("startIndex", startIndex,
1801 "Does not specify valid index.");
1806 ThrowNewArgumentOutOfRangeException ("count", count,
1807 "Can't be less than 0.");
1810 // re-ordered to avoid possible integer overflow
1811 if (startIndex > m_InnerCount - count)
1813 // LAMESPEC: Every other method throws ArgumentException
1815 throw new ArgumentOutOfRangeException("count",
1816 "Start index and count do not specify a valid range.");
1819 int retval = m_InnerArrayList.IndexOf(value, m_InnerIndex + startIndex, count);
1827 return retval - m_InnerIndex;
1831 public override int LastIndexOf(object value)
1833 return LastIndexOf(value, m_InnerCount - 1);
1836 public override int LastIndexOf(object value, int startIndex)
1838 return LastIndexOf(value, startIndex, startIndex + 1);
1841 public override int LastIndexOf(object value, int startIndex, int count)
1845 ThrowNewArgumentOutOfRangeException ("startIndex", startIndex, "< 0");
1850 ThrowNewArgumentOutOfRangeException ("count", count, "count is negative.");
1853 int retval = m_InnerArrayList.LastIndexOf(value, m_InnerIndex + startIndex, count);
1861 return retval - m_InnerIndex;
1865 public override void Insert(int index, object value)
1867 VerifyStateChanges();
1869 if (index < 0 || index > m_InnerCount)
1871 ThrowNewArgumentOutOfRangeException ("index", index,
1872 "Index must be >= 0 and <= Count.");
1875 m_InnerArrayList.Insert(m_InnerIndex + index, value);
1879 m_InnerStateChanges = m_InnerArrayList._version;
1882 public override void InsertRange(int index, ICollection c)
1884 VerifyStateChanges();
1886 if (index < 0 || index > m_InnerCount)
1888 ThrowNewArgumentOutOfRangeException ("index", index,
1889 "Index must be >= 0 and <= Count.");
1892 m_InnerArrayList.InsertRange(m_InnerIndex + index, c);
1894 m_InnerCount += c.Count;
1896 m_InnerStateChanges = m_InnerArrayList._version;
1899 public override void Remove(object value)
1901 VerifyStateChanges();
1903 int x = IndexOf(value);
1910 m_InnerStateChanges = m_InnerArrayList._version;
1913 public override void RemoveAt(int index)
1915 VerifyStateChanges();
1917 if (index < 0 || index > m_InnerCount)
1919 ThrowNewArgumentOutOfRangeException ("index", index,
1920 "Index must be >= 0 and <= Count.");
1923 m_InnerArrayList.RemoveAt(m_InnerIndex + index);
1926 m_InnerStateChanges = m_InnerArrayList._version;
1929 public override void RemoveRange(int index, int count)
1931 VerifyStateChanges();
1933 CheckRange(index, count, m_InnerCount);
1935 m_InnerArrayList.RemoveRange(m_InnerIndex + index, count);
1937 m_InnerCount -= count;
1939 m_InnerStateChanges = m_InnerArrayList._version;
1942 public override void Reverse()
1944 Reverse(0, m_InnerCount);
1947 public override void Reverse(int index, int count)
1949 VerifyStateChanges();
1951 CheckRange(index, count, m_InnerCount);
1953 m_InnerArrayList.Reverse(m_InnerIndex + index, count);
1955 m_InnerStateChanges = m_InnerArrayList._version;
1958 public override void SetRange(int index, ICollection c)
1960 VerifyStateChanges();
1962 if (index < 0 || index > m_InnerCount)
1964 ThrowNewArgumentOutOfRangeException ("index", index,
1965 "Index must be >= 0 and <= Count.");
1968 m_InnerArrayList.SetRange(m_InnerIndex + index, c);
1970 m_InnerStateChanges = m_InnerArrayList._version;
1973 public override void CopyTo(System.Array array)
1978 public override void CopyTo(System.Array array, int index)
1980 CopyTo(0, array, index, m_InnerCount);
1983 public override void CopyTo(int index, System.Array array, int arrayIndex, int count)
1985 CheckRange(index, count, m_InnerCount);
1987 m_InnerArrayList.CopyTo(m_InnerIndex + index, array, arrayIndex, count);
1990 public override IEnumerator GetEnumerator()
1992 return GetEnumerator(0, m_InnerCount);
1995 public override IEnumerator GetEnumerator(int index, int count)
1997 CheckRange(index, count, m_InnerCount);
1999 return m_InnerArrayList.GetEnumerator(m_InnerIndex + index, count);
2002 public override void AddRange(ICollection c)
2004 VerifyStateChanges();
2006 m_InnerArrayList.InsertRange(m_InnerCount, c);
2008 m_InnerCount += c.Count;
2010 m_InnerStateChanges = m_InnerArrayList._version;
2013 public override int BinarySearch(object value)
2015 return BinarySearch(0, m_InnerCount, value, Comparer.Default);
2018 public override int BinarySearch(object value, IComparer comparer)
2020 return BinarySearch(0, m_InnerCount, value, comparer);
2023 public override int BinarySearch(int index, int count, object value, IComparer comparer)
2025 CheckRange(index, count, m_InnerCount);
2027 return m_InnerArrayList.BinarySearch(m_InnerIndex + index, count, value, comparer);
2030 public override object Clone()
2032 return new RangedArrayList((ArrayList)m_InnerArrayList.Clone(), m_InnerIndex, m_InnerCount);
2035 public override ArrayList GetRange(int index, int count)
2037 CheckRange(index, count, m_InnerCount);
2039 return new RangedArrayList(this, index, count);
2042 public override void TrimToSize()
2044 throw new NotSupportedException();
2047 public override void Sort()
2049 Sort(Comparer.Default);
2052 public override void Sort(IComparer comparer)
2054 Sort(0, m_InnerCount, comparer);
2057 public override void Sort(int index, int count, IComparer comparer)
2059 VerifyStateChanges();
2061 CheckRange(index, count, m_InnerCount);
2063 m_InnerArrayList.Sort(m_InnerIndex + index, count, comparer);
2065 m_InnerStateChanges = m_InnerArrayList._version;
2068 public override object[] ToArray()
2072 array = new object[m_InnerCount];
2074 m_InnerArrayList.CopyTo (m_InnerIndex, array, 0, m_InnerCount);
2079 public override Array ToArray(Type elementType)
2083 array = Array.CreateInstance(elementType, m_InnerCount);
2085 m_InnerArrayList.CopyTo(m_InnerIndex, array, 0, m_InnerCount);
2099 #region SynchronizedListWrapper
2102 private sealed class SynchronizedListWrapper
2105 private object m_SyncRoot;
2107 public SynchronizedListWrapper(IList innerList)
2110 m_SyncRoot = innerList.SyncRoot;
2113 public override int Count
2119 return m_InnerList.Count;
2124 public override bool IsSynchronized
2132 public override object SyncRoot
2138 return m_InnerList.SyncRoot;
2143 public override bool IsFixedSize
2149 return m_InnerList.IsFixedSize;
2154 public override bool IsReadOnly
2160 return m_InnerList.IsReadOnly;
2165 public override object this[int index]
2171 return m_InnerList[index];
2179 m_InnerList[index] = value;
2184 public override int Add(object value)
2188 return m_InnerList.Add(value);
2192 public override void Clear()
2196 m_InnerList.Clear();
2200 public override bool Contains(object value)
2204 return m_InnerList.Contains(value);
2208 public override int IndexOf(object value)
2212 return m_InnerList.IndexOf(value);
2216 public override void Insert(int index, object value)
2220 m_InnerList.Insert(index, value);
2224 public override void Remove(object value)
2228 m_InnerList.Remove(value);
2232 public override void RemoveAt(int index)
2236 m_InnerList.RemoveAt(index);
2240 public override void CopyTo(Array array, int index)
2244 m_InnerList.CopyTo(array, index);
2248 public override IEnumerator GetEnumerator()
2252 return m_InnerList.GetEnumerator();
2259 #region FixedSizeListWrapper
2262 private class FixedSizeListWrapper
2265 protected virtual string ErrorMessage
2269 return "List is fixed-size.";
2273 public override bool IsFixedSize
2281 public FixedSizeListWrapper(IList innerList)
2286 public override int Add(object value)
2288 throw new NotSupportedException(this.ErrorMessage);
2291 public override void Clear()
2293 throw new NotSupportedException(this.ErrorMessage);
2296 public override void Insert(int index, object value)
2298 throw new NotSupportedException(this.ErrorMessage);
2301 public override void Remove(object value)
2303 throw new NotSupportedException(this.ErrorMessage);
2306 public override void RemoveAt(int index)
2308 throw new NotSupportedException(this.ErrorMessage);
2314 #region ReadOnlyListWrapper
2317 private sealed class ReadOnlyListWrapper
2318 : FixedSizeListWrapper
2320 protected override string ErrorMessage
2324 return "List is read-only.";
2328 public override bool IsReadOnly
2336 public ReadOnlyListWrapper(IList innerList)
2341 public override object this[int index]
2345 return m_InnerList[index];
2350 throw new NotSupportedException(this.ErrorMessage);
2360 /// Decorates/Wraps any <c>IList</c> implementing object.
2363 private class ListWrapper
2368 protected IList m_InnerList;
2372 #region Constructors
2374 public ListWrapper(IList innerList)
2376 m_InnerList = innerList;
2383 public virtual object this[int index]
2387 return m_InnerList[index];
2392 m_InnerList[index] = value;
2400 public virtual int Count
2404 return m_InnerList.Count;
2408 public virtual bool IsSynchronized
2412 return m_InnerList.IsSynchronized;
2416 public virtual object SyncRoot
2420 return m_InnerList.SyncRoot;
2424 public virtual bool IsFixedSize
2428 return m_InnerList.IsFixedSize;
2432 public virtual bool IsReadOnly
2436 return m_InnerList.IsReadOnly;
2444 public virtual int Add(object value)
2446 return m_InnerList.Add(value);
2449 public virtual void Clear()
2451 m_InnerList.Clear();
2454 public virtual bool Contains(object value)
2456 return m_InnerList.Contains(value);
2459 public virtual int IndexOf(object value)
2461 return m_InnerList.IndexOf(value);
2464 public virtual void Insert(int index, object value)
2466 m_InnerList.Insert(index, value);
2469 public virtual void Remove(object value)
2471 m_InnerList.Remove(value);
2474 public virtual void RemoveAt(int index)
2476 m_InnerList.RemoveAt(index);
2479 public virtual void CopyTo(Array array, int index)
2481 m_InnerList.CopyTo(array, index);
2484 public virtual IEnumerator GetEnumerator()
2486 return m_InnerList.GetEnumerator();
2495 // Start of ArrayList
2501 private const int DefaultInitialCapacity = 4;
2503 private const int DefaultInitialCapacity = 16;
2507 /// Number of items in the list.
2512 /// Array to store the items.
2514 private object[] _items;
2517 /// Total number of state changes.
2519 private int _version;
2522 private static readonly object [] EmptyArray = new object [0];
2527 #region Constructors
2530 /// Initializes a new instance of the <see cref="ArrayList"/> class that is empty and
2531 /// has the default initial capacity (16).
2536 _items = EmptyArray;
2538 _items = new object[DefaultInitialCapacity];
2543 /// Initializes a new instance of the <see cref="ArrayList"/> class that contains
2544 /// elements copied from the specified collection and that has the same initial capacity
2545 /// as the number of elements copied.
2547 /// <param name="c">
2548 /// The <see cref="ICollection"/> whose elements are copied into the new list.
2550 /// <exception cref="ArgumentNullException">
2551 /// The argument <c>c</c> is a null reference.
2553 public ArrayList(ICollection c)
2559 throw new ArgumentNullException("c");
2564 if (array != null && array.Rank != 1)
2566 throw new RankException();
2569 _items = new object[c.Count];
2575 /// Initializes a new instance of the <see cref="ArrayList"/> class that is empty and
2576 /// has the specified initial capacity.
2578 /// <param name="capacity">
2579 /// The number of elements that hte new list is initially capable of storing.
2581 /// <exception cref="ArgumentOutOfRangeException">
2582 /// The <c>capacity</c> is less than zero.
2584 public ArrayList(int capacity)
2588 ThrowNewArgumentOutOfRangeException ("capacity",
2589 capacity, "The initial capacity can't be smaller than zero.");
2594 capacity = DefaultInitialCapacity;
2596 _items = new object [capacity];
2600 /// Used by ArrayListAdapter to allow creation of an ArrayList with no storage buffer.
2602 private ArrayList(int initialCapacity, bool forceZeroSize)
2610 throw new InvalidOperationException("Use ArrayList(int)");
2615 /// Initializes a new array list that contains a copy of the given array and with the
2618 /// <param name="array"></param>
2619 private ArrayList(object[] array, int index, int count)
2623 _items = new object[DefaultInitialCapacity];
2627 _items = new object[count];
2630 Array.Copy(array, index, _items, 0, count);
2640 /// Gets/Sets an element in the list by index.
2642 /// <exception cref="ArgumentOutOfRangeException">
2643 /// The index is less than 0 or more then or equal to the list count.
2645 public virtual object this[int index]
2649 if (index < 0 || index >= _size)
2651 ThrowNewArgumentOutOfRangeException ("index", index,
2652 "Index is less than 0 or more than or equal to the list count.");
2655 return _items[index];
2660 if (index < 0 || index >= _size)
2662 ThrowNewArgumentOutOfRangeException ("index", index,
2663 "Index is less than 0 or more than or equal to the list count.");
2666 _items[index] = value;
2676 /// Gets the number of elements in the list.
2678 public virtual int Count
2687 /// Gets the number of elements the list can carry without needing to expand.
2690 /// ArrayLists automatically double their capacity when the capacity limit is broken.
2692 /// <exception cref="ArgumentOutOfRangeException">
2693 /// The capacity is less than the count.
2695 public virtual int Capacity
2699 return _items.Length;
2706 ThrowNewArgumentOutOfRangeException ("Capacity", value,
2707 "Must be more than count.");
2712 newArray = new object[value];
2714 Array.Copy(_items, 0, newArray, 0, _size);
2721 /// <see cref="IList.IsFixedSize"/>
2724 public virtual bool IsFixedSize
2733 /// <see cref="IList.IsReadOnly"/>
2735 public virtual bool IsReadOnly
2744 /// <see cref="ICollection.IsSynchronized"/>
2746 public virtual bool IsSynchronized
2755 /// <see cref="ICollection.SyncRoot"/>
2757 public virtual object SyncRoot
2770 /// Ensures that the list has the capacity to contain the given <c>count</c> by
2771 /// automatically expanding the capacity when required.
2773 private void EnsureCapacity(int count)
2775 if (count <= _items.Length)
2783 newLength = _items.Length << 1;
2785 newLength = DefaultInitialCapacity;
2787 while (newLength < count)
2792 newData = new object[newLength];
2794 Array.Copy(_items, 0, newData, 0, _items.Length);
2800 /// Shifts a section of the list.
2802 /// <param name="index">
2803 /// The start of the section to shift (the element at index is included in the shift).
2805 /// <param name="count">
2806 /// The number of positions to shift by (can be negative).
2808 private void Shift(int index, int count)
2812 if (_size + count > _items.Length)
2817 newLength = (_items.Length > 0) ? _items.Length << 1 : 1;
2819 while (newLength < _size + count)
2824 newData = new object[newLength];
2826 Array.Copy(_items, 0, newData, 0, index);
2827 Array.Copy(_items, index, newData, index + count, _size - index);
2833 Array.Copy(_items, index, _items, index + count, _size - index);
2838 // Remember count is negative so this is actually index + (-count)
2840 int x = index - count ;
2842 Array.Copy(_items, x, _items, index, _size - x);
2843 Array.Clear(_items, _size + count, - count);
2847 public virtual int Add(object value)
2849 // Do a check here in case EnsureCapacity isn't inlined.
2851 if (_items.Length <= _size /* same as _items.Length < _size + 1) */)
2853 EnsureCapacity(_size + 1);
2856 _items[_size] = value;
2863 public virtual void Clear()
2865 // Keep the array but null all members so they can be garbage collected.
2867 Array.Clear(_items, 0, _size);
2873 public virtual bool Contains(object item)
2875 return IndexOf(item, 0, _size) > -1;
2878 internal virtual bool Contains(object value, int startIndex, int count)
2880 return IndexOf(value, startIndex, count) > -1;
2883 public virtual int IndexOf(object value)
2885 return IndexOf(value, 0);
2888 public virtual int IndexOf(object value, int startIndex)
2890 return IndexOf(value, startIndex, _size - startIndex);
2893 public virtual int IndexOf(object value, int startIndex, int count)
2895 if (startIndex < 0 || startIndex > _size)
2897 ThrowNewArgumentOutOfRangeException ("startIndex", startIndex,
2898 "Does not specify valid index.");
2903 ThrowNewArgumentOutOfRangeException ("count", count,
2904 "Can't be less than 0.");
2907 // re-ordered to avoid integer overflow
2908 if (startIndex > _size - count)
2910 // LAMESPEC: Every other method throws ArgumentException
2912 throw new ArgumentOutOfRangeException("count",
2913 "Start index and count do not specify a valid range.");
2916 return Array.IndexOf(_items, value, startIndex, count);
2919 public virtual int LastIndexOf(object value)
2921 return LastIndexOf(value, _size - 1);
2924 public virtual int LastIndexOf(object value, int startIndex)
2926 return LastIndexOf(value, startIndex, startIndex + 1);
2929 public virtual int LastIndexOf (object value, int startIndex, int count)
2931 // Array will catch the exceptions
2932 return Array.LastIndexOf (_items, value, startIndex, count);
2935 public virtual void Insert(int index, object value)
2937 if (index < 0 || index > _size)
2939 ThrowNewArgumentOutOfRangeException ("index", index,
2940 "Index must be >= 0 and <= Count.");
2945 _items[index] = value;
2950 public virtual void InsertRange(int index, ICollection c)
2956 throw new ArgumentNullException("c");
2959 if (index < 0 || index > _size)
2961 ThrowNewArgumentOutOfRangeException ("index", index,
2962 "Index must be >= 0 and <= Count.");
2967 // Do a check here in case EnsureCapacity isn't inlined.
2969 if (_items.Length < _size + i)
2971 EnsureCapacity(_size + i);
2976 Array.Copy(_items, index, _items, index + i, _size - index);
2979 // Handle inserting a range from a list to itself specially.
2981 if (this == c.SyncRoot)
2983 // Copy range before the insert point.
2985 Array.Copy(_items, 0, _items, index, index);
2987 // Copy range after the insert point.
2989 Array.Copy(_items, index + i, _items, index << 1, _size - index);
2993 c.CopyTo(_items, index);
3000 public virtual void Remove(object obj)
3014 public virtual void RemoveAt(int index)
3016 if (index < 0 || index >= _size)
3018 ThrowNewArgumentOutOfRangeException ("index", index,
3019 "Less than 0 or more than list count.");
3027 public virtual void RemoveRange(int index, int count)
3029 ArrayList.CheckRange(index, count, _size);
3031 Shift(index, -count);
3036 public virtual void Reverse()
3038 Array.Reverse(_items, 0, _size);
3042 public virtual void Reverse(int index, int count)
3044 ArrayList.CheckRange(index, count, _size);
3046 Array.Reverse(_items, index, count);
3050 public virtual void CopyTo(System.Array array)
3052 Array.Copy(_items, array, _size);
3055 public virtual void CopyTo(System.Array array, int arrayIndex)
3057 CopyTo(0, array, arrayIndex, _size);
3060 public virtual void CopyTo(int index, System.Array array, int arrayIndex, int count)
3064 throw new ArgumentNullException("array");
3067 if (array.Rank != 1)
3070 // This should be a RankException because Array.Copy throws RankException.
3072 throw new ArgumentException("Must have only 1 dimensions.", "array");
3075 Array.Copy(_items, index, array, arrayIndex, count);
3078 public virtual IEnumerator GetEnumerator()
3080 return new SimpleEnumerator(this);
3083 public virtual IEnumerator GetEnumerator(int index, int count)
3085 ArrayList.CheckRange(index, count, _size);
3087 return new ArrayListEnumerator(this, index, count);
3090 public virtual void AddRange(ICollection c)
3092 InsertRange(_size, c);
3095 public virtual int BinarySearch(object value)
3099 return Array.BinarySearch(_items, 0, _size, value);
3101 catch (InvalidOperationException e)
3103 throw new ArgumentException(e.Message);
3107 public virtual int BinarySearch(object value, IComparer comparer)
3111 return Array.BinarySearch(_items, 0, _size, value, comparer);
3113 catch (InvalidOperationException e)
3115 throw new ArgumentException(e.Message);
3119 public virtual int BinarySearch(int index, int count, object value, IComparer comparer)
3123 return Array.BinarySearch(_items, index, count, value, comparer);
3125 catch (InvalidOperationException e)
3127 throw new ArgumentException(e.Message);
3130 public virtual ArrayList GetRange(int index, int count)
3132 ArrayList.CheckRange(index, count, _size);
3134 if (this.IsSynchronized)
3136 return ArrayList.Synchronized(new RangedArrayList(this, index, count));
3140 return new RangedArrayList(this, index, count);
3144 public virtual void SetRange(int index, ICollection c)
3148 throw new ArgumentNullException("c");
3151 if (index < 0 || index + c.Count > _size)
3153 throw new ArgumentOutOfRangeException("index");
3156 c.CopyTo(_items, index);
3161 public virtual void TrimToSize()
3163 if (_items.Length > _size)
3169 newArray = new object[DefaultInitialCapacity];
3173 newArray = new object[_size];
3176 Array.Copy(_items, 0, newArray, 0, _size);
3182 public virtual void Sort()
3184 Array.Sort(_items, 0, _size);
3189 public virtual void Sort(IComparer comparer)
3191 Array.Sort(_items, 0, _size, comparer);
3194 public virtual void Sort(int index, int count, IComparer comparer)
3196 ArrayList.CheckRange(index, count, _size);
3198 Array.Sort(_items, index, count, comparer);
3201 public virtual object[] ToArray()
3205 retval = new object[_size];
3212 public virtual Array ToArray(Type type)
3216 retval = Array.CreateInstance(type, _size);
3223 public virtual object Clone()
3225 return new ArrayList(this._items, 0, this._size);
3230 #region Static Methods
3233 /// Does a check of the arguments many of the methods in ArrayList use.
3236 /// The choice of exceptions thrown sometimes seem to be arbitrarily chosen so
3237 /// not all methods actually make use of CheckRange.
3239 internal static void CheckRange(int index, int count, int listCount)
3243 ThrowNewArgumentOutOfRangeException ("index", index, "Can't be less than 0.");
3248 ThrowNewArgumentOutOfRangeException ("count", count, "Can't be less than 0.");
3251 // re-ordered to avoid possible integer overflow
3252 if (index > listCount - count)
3254 throw new ArgumentException("Index and count do not denote a valid range of elements.", "index");
3258 internal static void ThrowNewArgumentOutOfRangeException (string name, object actual, string message)
3260 throw new ArgumentOutOfRangeException (
3261 #if !INSIDE_CORLIB && NET_2_1
3264 name, actual, message
3269 public static ArrayList Adapter(IList list)
3271 // LAMESPEC: EWWW. Other lists aren't *Array*Lists.
3275 throw new ArgumentNullException("list");
3278 ArrayList arrayList = list as ArrayList;
3279 if (arrayList != null)
3282 arrayList = new ArrayListAdapter(list);
3284 if (list.IsSynchronized)
3285 return ArrayList.Synchronized(arrayList);
3290 public static ArrayList Synchronized(ArrayList list)
3294 throw new ArgumentNullException("list");
3297 if (list.IsSynchronized)
3302 return new SynchronizedArrayListWrapper(list);
3305 public static IList Synchronized(IList list)
3309 throw new ArgumentNullException("list");
3312 if (list.IsSynchronized)
3317 return new SynchronizedListWrapper(list);
3320 public static ArrayList ReadOnly(ArrayList list)
3324 throw new ArgumentNullException("list");
3327 if (list.IsReadOnly)
3332 return new ReadOnlyArrayListWrapper(list);
3335 public static IList ReadOnly(IList list)
3339 throw new ArgumentNullException("list");
3342 if (list.IsReadOnly)
3347 return new ReadOnlyListWrapper(list);
3350 public static ArrayList FixedSize(ArrayList list)
3354 throw new ArgumentNullException("list");
3357 if (list.IsFixedSize)
3362 return new FixedSizeArrayListWrapper(list);
3365 public static IList FixedSize(IList list)
3369 throw new ArgumentNullException("list");
3372 if (list.IsFixedSize)
3377 return new FixedSizeListWrapper(list);
3380 public static ArrayList Repeat(object value, int count)
3382 ArrayList arrayList = new ArrayList(count);
3384 for (int i = 0; i < count; i++)
3386 arrayList.Add(value);