3 // Implementation of the ECMA ArrayList.
5 // Copyright (c) 2003 Thong (Tum) Nguyen [tum@veridicus.com]
6 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
8 // Permission is hereby granted, free of charge, to any person obtaining
9 // a copy of this software and associated documentation files (the
10 // "Software"), to deal in the Software without restriction, including
11 // without limitation the rights to use, copy, modify, merge, publish,
12 // distribute, sublicense, and/or sell copies of the Software, and to
13 // permit persons to whom the Software is furnished to do so, subject to
14 // the following conditions:
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 using System.Runtime.InteropServices;
30 namespace System.Collections
36 public class ArrayList
37 : IList, ICloneable, ICollection, IEnumerable
41 private sealed class ArrayListEnumerator
42 : IEnumerator, ICloneable
47 private object m_Current;
48 private ArrayList m_List;
49 private int m_ExpectedStateChanges;
51 public ArrayListEnumerator(ArrayList list)
52 : this(list, 0, list.Count)
58 return this.MemberwiseClone();
61 public ArrayListEnumerator(ArrayList list, int index, int count)
68 m_ExpectedStateChanges = list._version;
75 if (m_Pos == m_Index - 1) {
76 throw new InvalidOperationException("Enumerator unusable (Reset pending, or past end of array.");
83 public bool MoveNext()
85 if (m_List._version != m_ExpectedStateChanges)
87 throw new InvalidOperationException("List has changed.");
92 if (m_Pos - m_Index < m_Count)
94 m_Current = m_List[m_Pos];
109 sealed class SimpleEnumerator : IEnumerator, ICloneable
114 object currentElement;
115 static object endFlag = new object ();
117 public SimpleEnumerator (ArrayList list)
121 version = list._version;
122 currentElement = endFlag;
125 public object Clone ()
127 return MemberwiseClone ();
130 public bool MoveNext ()
132 if (version != list._version)
133 throw new InvalidOperationException("List has changed.");
135 if (++index < list.Count) {
136 currentElement = list [index];
139 currentElement = endFlag;
144 public object Current {
146 if (currentElement == endFlag) {
148 throw new InvalidOperationException ("Enumerator not started");
150 throw new InvalidOperationException ("Enumerator ended");
153 return currentElement;
159 if (version != list._version)
160 throw new InvalidOperationException ("List has changed.");
162 currentElement = endFlag;
169 #region ArrayListAdapter
172 /// Adapts various ILists into an ArrayList.
175 private sealed class ArrayListAdapter
178 private sealed class EnumeratorWithRange
179 : IEnumerator, ICloneable
181 private int m_StartIndex;
183 private int m_MaxCount;
184 private IEnumerator m_Enumerator;
186 public EnumeratorWithRange(IEnumerator enumerator, int index, int count)
189 m_StartIndex = index;
191 m_Enumerator = enumerator;
196 public object Clone()
198 return this.MemberwiseClone();
201 public object Current
205 return m_Enumerator.Current;
209 public bool MoveNext()
211 if (m_Count >= m_MaxCount)
218 return m_Enumerator.MoveNext();
224 m_Enumerator.Reset();
226 for (int i = 0; i < m_StartIndex; i++)
228 m_Enumerator.MoveNext();
233 private IList m_Adaptee;
235 public ArrayListAdapter(IList adaptee)
241 public override object this[int index]
245 return m_Adaptee[index];
250 m_Adaptee[index] = value;
254 public override int Count
258 return m_Adaptee.Count;
262 public override int Capacity
266 return m_Adaptee.Count;
271 if (value < m_Adaptee.Count)
273 throw new ArgumentException("capacity");
278 public override bool IsFixedSize
282 return m_Adaptee.IsFixedSize;
286 public override bool IsReadOnly
290 return m_Adaptee.IsReadOnly;
294 public override object SyncRoot
298 return m_Adaptee.SyncRoot;
302 public override int Add(object value)
304 return m_Adaptee.Add(value);
307 public override void Clear()
312 public override bool Contains(object value)
314 return m_Adaptee.Contains(value);
317 public override int IndexOf(object value)
319 return m_Adaptee.IndexOf(value);
322 public override int IndexOf(object value, int startIndex)
324 return IndexOf(value, startIndex, m_Adaptee.Count - startIndex);
327 public override int IndexOf(object value, int startIndex, int count)
329 if (startIndex < 0 || startIndex > m_Adaptee.Count)
331 throw new ArgumentOutOfRangeException("startIndex", startIndex,
332 "Does not specify valid index.");
337 throw new ArgumentOutOfRangeException("count", count,
338 "Can't be less than 0.");
341 // re-ordered to avoid possible integer overflow
342 if (startIndex > m_Adaptee.Count - count) {
343 // LAMESPEC: Every other method throws ArgumentException
344 throw new ArgumentOutOfRangeException("count",
345 "Start index and count do not specify a valid range.");
350 for (int i = startIndex; i < startIndex + count; i++)
352 if (m_Adaptee[i] == null)
360 for (int i = startIndex; i < startIndex + count; i++)
362 if (value.Equals(m_Adaptee[i]))
372 public override int LastIndexOf(object value)
374 return LastIndexOf(value, m_Adaptee.Count - 1);
377 public override int LastIndexOf(object value, int startIndex)
379 return LastIndexOf(value, startIndex, startIndex + 1);
382 public override int LastIndexOf(object value, int startIndex, int count)
386 throw new ArgumentOutOfRangeException("startIndex", startIndex, "< 0");
391 throw new ArgumentOutOfRangeException("count", count, "count is negative.");
394 if (startIndex - count + 1 < 0)
396 throw new ArgumentOutOfRangeException("count", count, "count is too large.");
401 for (int i = startIndex; i > startIndex - count; i--)
403 if (m_Adaptee[i] == null)
411 for (int i = startIndex; i > startIndex - count; i--)
413 if (value.Equals(m_Adaptee[i]))
423 public override void Insert(int index, object value)
425 m_Adaptee.Insert(index, value);
428 public override void InsertRange(int index, ICollection c)
432 throw new ArgumentNullException("c");
435 if (index > m_Adaptee.Count)
437 throw new ArgumentOutOfRangeException("index", index,
438 "Index must be >= 0 and <= Count.");
441 foreach (object value in c)
443 m_Adaptee.Insert(index++, value);
447 public override void Remove(object value)
449 m_Adaptee.Remove(value);
452 public override void RemoveAt(int index)
454 m_Adaptee.RemoveAt(index);
457 public override void RemoveRange(int index, int count)
459 CheckRange(index, count, m_Adaptee.Count);
461 for (int i = 0; i < count; i++)
463 m_Adaptee.RemoveAt(index);
467 public override void Reverse()
469 Reverse(0, m_Adaptee.Count);
472 public override void Reverse(int index, int count)
476 CheckRange(index, count, m_Adaptee.Count);
478 for (int i = 0; i < count / 2; i++)
480 tmp = m_Adaptee[i + index];
481 m_Adaptee[i + index] = m_Adaptee[(index + count) - i + index - 1];
482 m_Adaptee[(index + count) - i + index - 1] = tmp;
486 public override void SetRange(int index, ICollection c)
490 throw new ArgumentNullException("c");
493 if (index < 0 || index + c.Count > m_Adaptee.Count)
495 throw new ArgumentOutOfRangeException("index");
500 foreach (object value in c)
502 m_Adaptee[x++] = value;
506 public override void CopyTo(System.Array array)
508 m_Adaptee.CopyTo(array, 0);
511 public override void CopyTo(System.Array array, int index)
513 m_Adaptee.CopyTo(array, index);
516 public override void CopyTo(int index, System.Array array, int arrayIndex, int count)
520 throw new ArgumentOutOfRangeException("index", index,
521 "Can't be less than zero.");
526 throw new ArgumentOutOfRangeException("arrayIndex", arrayIndex,
527 "Can't be less than zero.");
532 throw new ArgumentOutOfRangeException("index", index,
533 "Can't be less than zero.");
536 if (index >= m_Adaptee.Count)
538 throw new ArgumentException("Can't be more or equal to list count.",
544 throw new ArgumentException("Can't copy into multi-dimensional array.");
547 if (arrayIndex >= array.Length)
549 throw new ArgumentException("arrayIndex can't be greater than array.Length - 1.");
552 if (array.Length - arrayIndex + 1 < count)
554 throw new ArgumentException("Destination array is too small.");
557 // re-ordered to avoid possible integer overflow
558 if (index > m_Adaptee.Count - count) {
559 throw new ArgumentException("Index and count do not denote a valid range of elements.", "index");
562 for (int i = 0; i < count; i++)
564 array.SetValue(m_Adaptee[index + i], arrayIndex + i);
568 public override bool IsSynchronized
572 return m_Adaptee.IsSynchronized;
576 public override IEnumerator GetEnumerator()
578 return m_Adaptee.GetEnumerator();
581 public override IEnumerator GetEnumerator(int index, int count)
583 CheckRange(index, count, m_Adaptee.Count);
585 return new EnumeratorWithRange(m_Adaptee.GetEnumerator(), index, count);
588 public override void AddRange(ICollection c)
590 foreach (object value in c)
592 m_Adaptee.Add(value);
596 public override int BinarySearch(object value)
598 return BinarySearch(value, null);
601 public override int BinarySearch(object value, IComparer comparer)
603 return BinarySearch(0, m_Adaptee.Count, value, comparer);
606 public override int BinarySearch(int index, int count, object value, IComparer comparer)
610 // Doing a direct BinarySearch on the adaptee will perform poorly if the adaptee is a linked-list.
611 // Alternatives include copying the adaptee to a temporary array first.
613 CheckRange(index, count, m_Adaptee.Count);
615 if (comparer == null)
617 comparer = Comparer.Default;
621 y = index + count - 1;
625 // Be careful with overflows
626 z = x + ((y - x) / 2);
628 r = comparer.Compare(value, m_Adaptee[z]);
647 public override object Clone()
649 return new ArrayList.ArrayListAdapter(m_Adaptee);
652 public override ArrayList GetRange(int index, int count)
654 CheckRange(index, count, m_Adaptee.Count);
656 return new RangedArrayList(this, index, count);
659 public override void TrimToSize()
664 public override void Sort()
666 Sort(Comparer.Default);
669 public override void Sort(IComparer comparer)
671 Sort(0, m_Adaptee.Count, comparer);
674 public override void Sort(int index, int count, IComparer comparer)
676 CheckRange(index, count, m_Adaptee.Count);
678 if (comparer == null)
680 comparer = Comparer.Default;
683 // Doing a direct sort on the adaptee will perform poorly if the adaptee is a linked-list.
684 // Alternatives include copying the adaptee into a temporary array first.
686 QuickSort(m_Adaptee, index, index + count - 1, comparer);
691 /// Swaps two items in a list at the specified indexes.
693 private static void Swap(IList list, int x, int y)
703 /// Quicksort for lists.
706 /// This function acts as both qsort() and partition().
708 internal static void QuickSort(IList list, int left, int right, IComparer comparer)
718 // Pick the pivot using the median-of-three strategy.
720 // Be careful with overflows
721 middle = left + ((right - left) / 2);
723 if (comparer.Compare(list[middle], list[left]) < 0)
725 Swap(list, middle, left);
728 if (comparer.Compare(list[right], list[left]) < 0)
730 Swap(list, right, left);
733 if (comparer.Compare(list[right], list[middle]) < 0)
735 Swap(list, right, middle);
738 if (right - left + 1 <= 3)
743 // Put the pivot in right - 1.
744 Swap(list, right - 1, middle);
746 // List should look like:
748 // [Small] ..Numbers.. [Middle] ..Numbers.. [Pivot][Large]
750 pivot = list[right - 1];
752 // Sort from (left + 1) to (right - 2).
759 while (comparer.Compare(list[++i], pivot) < 0);
760 while (comparer.Compare(list[--j], pivot) > 0);
772 // Put pivot into the right position (real middle).
774 Swap(list, right - 1, i);
776 // Recursively sort the left and right sub lists.
778 QuickSort(list, left, i - 1, comparer);
779 QuickSort(list, i + 1, right, comparer);
782 public override object[] ToArray()
786 retval = new object[m_Adaptee.Count];
788 m_Adaptee.CopyTo(retval, 0);
793 public override Array ToArray(Type elementType)
797 retval = Array.CreateInstance(elementType, m_Adaptee.Count);
799 m_Adaptee.CopyTo(retval, 0);
805 #endregion // ArrayListAdapter
808 // ArrayList wrappers
811 #region ArrayListWrapper
814 /// Base wrapper/decorator for ArrayLists. Simply delegates all methods to
815 /// the underlying wrappee.
818 private class ArrayListWrapper
821 protected ArrayList m_InnerArrayList;
825 public ArrayListWrapper(ArrayList innerArrayList)
827 m_InnerArrayList = innerArrayList;
834 public override object this[int index]
838 return m_InnerArrayList[index];
843 m_InnerArrayList[index] = value;
851 public override int Count
855 return m_InnerArrayList.Count;
859 public override int Capacity
863 return m_InnerArrayList.Capacity;
868 m_InnerArrayList.Capacity = value;
872 public override bool IsFixedSize
876 return m_InnerArrayList.IsFixedSize;
880 public override bool IsReadOnly
884 return m_InnerArrayList.IsReadOnly;
888 public override bool IsSynchronized
892 return m_InnerArrayList.IsSynchronized;
896 public override object SyncRoot
900 return m_InnerArrayList.SyncRoot;
908 public override int Add(object value)
910 return m_InnerArrayList.Add(value);
913 public override void Clear()
915 m_InnerArrayList.Clear();
918 public override bool Contains(object value)
920 return m_InnerArrayList.Contains(value);
923 public override int IndexOf(object value)
925 return m_InnerArrayList.IndexOf(value);
928 public override int IndexOf(object value, int startIndex)
930 return m_InnerArrayList.IndexOf(value, startIndex);
933 public override int IndexOf(object value, int startIndex, int count)
935 return m_InnerArrayList.IndexOf(value, startIndex, count);
938 public override int LastIndexOf(object value)
940 return m_InnerArrayList.LastIndexOf(value);
943 public override int LastIndexOf(object value, int startIndex)
945 return m_InnerArrayList.LastIndexOf(value, startIndex);
948 public override int LastIndexOf(object value, int startIndex, int count)
950 return m_InnerArrayList.LastIndexOf(value, startIndex, count);
953 public override void Insert(int index, object value)
955 m_InnerArrayList.Insert(index, value);
958 public override void InsertRange(int index, ICollection c)
960 m_InnerArrayList.InsertRange(index, c);
963 public override void Remove(object value)
965 m_InnerArrayList.Remove(value);
968 public override void RemoveAt(int index)
970 m_InnerArrayList.RemoveAt(index);
973 public override void RemoveRange(int index, int count)
975 m_InnerArrayList.RemoveRange(index, count);
978 public override void Reverse()
980 m_InnerArrayList.Reverse();
983 public override void Reverse(int index, int count)
985 m_InnerArrayList.Reverse(index, count);
988 public override void SetRange(int index, ICollection c)
990 m_InnerArrayList.SetRange(index, c);
993 public override void CopyTo(System.Array array)
995 m_InnerArrayList.CopyTo(array);
998 public override void CopyTo(System.Array array, int index)
1000 m_InnerArrayList.CopyTo(array, index);
1003 public override void CopyTo(int index, System.Array array, int arrayIndex, int count)
1005 m_InnerArrayList.CopyTo(index, array, arrayIndex, count);
1008 public override IEnumerator GetEnumerator()
1010 return m_InnerArrayList.GetEnumerator();
1013 public override IEnumerator GetEnumerator(int index, int count)
1015 return m_InnerArrayList.GetEnumerator(index, count);
1018 public override void AddRange(ICollection c)
1020 m_InnerArrayList.AddRange(c);
1023 public override int BinarySearch(object value)
1025 return m_InnerArrayList.BinarySearch(value);
1028 public override int BinarySearch(object value, IComparer comparer)
1030 return m_InnerArrayList.BinarySearch(value, comparer);
1033 public override int BinarySearch(int index, int count, object value, IComparer comparer)
1035 return m_InnerArrayList.BinarySearch(index, count, value, comparer);
1038 public override object Clone()
1040 return m_InnerArrayList.Clone();
1043 public override ArrayList GetRange(int index, int count)
1045 return m_InnerArrayList.GetRange(index, count);
1048 public override void TrimToSize()
1050 m_InnerArrayList.TrimToSize();
1053 public override void Sort()
1055 m_InnerArrayList.Sort();
1058 public override void Sort(IComparer comparer)
1060 m_InnerArrayList.Sort(comparer);
1063 public override void Sort(int index, int count, IComparer comparer)
1065 m_InnerArrayList.Sort(index, count, comparer);
1068 public override object[] ToArray()
1070 return m_InnerArrayList.ToArray();
1073 public override Array ToArray(Type elementType)
1075 return m_InnerArrayList.ToArray(elementType);
1083 #region SynchronizedArrayListWrapper
1086 /// ArrayListWrapper that synchronizes calls to all methods/properties.
1089 /// Works by just synchronizing all method calls. In the future careful optimisation
1090 /// could give better performance...
1093 private sealed class SynchronizedArrayListWrapper
1096 private object m_SyncRoot;
1098 #region Constructors
1101 /// Creates a new synchronized wrapper for the given <see cref="ArrayList"/>.
1103 /// <param name="innerArrayList"></param>
1104 internal SynchronizedArrayListWrapper(ArrayList innerArrayList)
1105 : base(innerArrayList)
1107 m_SyncRoot = innerArrayList.SyncRoot;
1114 public override object this[int index]
1120 return m_InnerArrayList[index];
1128 m_InnerArrayList[index] = value;
1137 // Some of these properties may be calculated so it's best to synchronize
1138 // them even though it might cause a performance hit.
1139 // Better safe than sorry ;D.
1141 public override int Count
1147 return m_InnerArrayList.Count;
1152 public override int Capacity
1158 return m_InnerArrayList.Capacity;
1166 m_InnerArrayList.Capacity = value;
1171 public override bool IsFixedSize
1177 return m_InnerArrayList.IsFixedSize;
1182 public override bool IsReadOnly
1188 return m_InnerArrayList.IsReadOnly;
1193 public override bool IsSynchronized
1201 public override object SyncRoot
1213 public override int Add(object value)
1217 return m_InnerArrayList.Add(value);
1221 public override void Clear()
1225 m_InnerArrayList.Clear();
1229 public override bool Contains(object value)
1233 return m_InnerArrayList.Contains(value);
1237 public override int IndexOf(object value)
1241 return m_InnerArrayList.IndexOf(value);
1245 public override int IndexOf(object value, int startIndex)
1249 return m_InnerArrayList.IndexOf(value, startIndex);
1253 public override int IndexOf(object value, int startIndex, int count)
1257 return m_InnerArrayList.IndexOf(value, startIndex, count);
1261 public override int LastIndexOf(object value)
1265 return m_InnerArrayList.LastIndexOf(value);
1269 public override int LastIndexOf(object value, int startIndex)
1273 return m_InnerArrayList.LastIndexOf(value, startIndex);
1277 public override int LastIndexOf(object value, int startIndex, int count)
1281 return m_InnerArrayList.LastIndexOf(value, startIndex, count);
1285 public override void Insert(int index, object value)
1289 m_InnerArrayList.Insert(index, value);
1293 public override void InsertRange(int index, ICollection c)
1297 m_InnerArrayList.InsertRange(index, c);
1301 public override void Remove(object value)
1305 m_InnerArrayList.Remove(value);
1309 public override void RemoveAt(int index)
1313 m_InnerArrayList.RemoveAt(index);
1317 public override void RemoveRange(int index, int count)
1321 m_InnerArrayList.RemoveRange(index, count);
1325 public override void Reverse()
1329 m_InnerArrayList.Reverse();
1333 public override void Reverse(int index, int count)
1337 m_InnerArrayList.Reverse(index, count);
1341 public override void CopyTo(System.Array array)
1345 m_InnerArrayList.CopyTo(array);
1349 public override void CopyTo(System.Array array, int index)
1353 m_InnerArrayList.CopyTo(array, index);
1357 public override void CopyTo(int index, System.Array array, int arrayIndex, int count)
1361 m_InnerArrayList.CopyTo(index, array, arrayIndex, count);
1365 public override IEnumerator GetEnumerator()
1369 return m_InnerArrayList.GetEnumerator();
1373 public override IEnumerator GetEnumerator(int index, int count)
1377 return m_InnerArrayList.GetEnumerator(index, count);
1381 public override void AddRange(ICollection c)
1385 m_InnerArrayList.AddRange(c);
1389 public override int BinarySearch(object value)
1393 return m_InnerArrayList.BinarySearch(value);
1397 public override int BinarySearch(object value, IComparer comparer)
1401 return m_InnerArrayList.BinarySearch(value, comparer);
1405 public override int BinarySearch(int index, int count, object value, IComparer comparer)
1409 return m_InnerArrayList.BinarySearch(index, count, value, comparer);
1413 public override object Clone()
1417 return m_InnerArrayList.Clone();
1421 public override ArrayList GetRange(int index, int count)
1425 return m_InnerArrayList.GetRange(index, count);
1429 public override void TrimToSize()
1433 m_InnerArrayList.TrimToSize();
1437 public override void Sort()
1441 m_InnerArrayList.Sort();
1445 public override void Sort(IComparer comparer)
1449 m_InnerArrayList.Sort(comparer);
1453 public override void Sort(int index, int count, IComparer comparer)
1457 m_InnerArrayList.Sort(index, count, comparer);
1461 public override object[] ToArray()
1465 return m_InnerArrayList.ToArray();
1469 public override Array ToArray(Type elementType)
1473 return m_InnerArrayList.ToArray(elementType);
1482 #region FixedSizeArrayListWrapper
1485 private class FixedSizeArrayListWrapper
1488 #region Constructors
1490 public FixedSizeArrayListWrapper(ArrayList innerList)
1500 /// Gets the error message to display when an readonly/fixedsize related exception is
1503 protected virtual string ErrorMessage
1507 return "Can't add or remove from a fixed-size list.";
1511 public override int Capacity
1515 return base.Capacity;
1520 throw new NotSupportedException(this.ErrorMessage);
1524 public override bool IsFixedSize
1536 public override int Add(object value)
1538 throw new NotSupportedException(this.ErrorMessage);
1541 public override void AddRange(ICollection c)
1543 throw new NotSupportedException(this.ErrorMessage);
1546 public override void Clear()
1548 throw new NotSupportedException(this.ErrorMessage);
1551 public override void Insert(int index, object value)
1553 throw new NotSupportedException(this.ErrorMessage);
1556 public override void InsertRange(int index, ICollection c)
1558 throw new NotSupportedException(this.ErrorMessage);
1561 public override void Remove(object value)
1563 throw new NotSupportedException(this.ErrorMessage);
1566 public override void RemoveAt(int index)
1568 throw new NotSupportedException(this.ErrorMessage);
1571 public override void RemoveRange(int index, int count)
1573 throw new NotSupportedException(this.ErrorMessage);
1576 public override void TrimToSize()
1578 throw new NotSupportedException(this.ErrorMessage);
1586 #region ReadOnlyArrayListWrapper
1589 private sealed class ReadOnlyArrayListWrapper
1590 : FixedSizeArrayListWrapper
1592 protected override string ErrorMessage
1596 return "Can't modify a readonly list.";
1600 public override bool IsReadOnly
1608 public ReadOnlyArrayListWrapper(ArrayList innerArrayList)
1609 : base(innerArrayList)
1613 public override object this[int index]
1617 return m_InnerArrayList[index];
1622 throw new NotSupportedException(this.ErrorMessage);
1626 public override void Reverse()
1628 throw new NotSupportedException(this.ErrorMessage);
1631 public override void Reverse(int index, int count)
1633 throw new NotSupportedException(this.ErrorMessage);
1636 public override void SetRange(int index, ICollection c)
1638 throw new NotSupportedException(this.ErrorMessage);
1641 public override void Sort()
1643 throw new NotSupportedException(this.ErrorMessage);
1646 public override void Sort(IComparer comparer)
1648 throw new NotSupportedException(this.ErrorMessage);
1651 public override void Sort(int index, int count, IComparer comparer)
1653 throw new NotSupportedException(this.ErrorMessage);
1659 #region RangedArrayList
1662 private sealed class RangedArrayList
1665 private int m_InnerIndex;
1666 private int m_InnerCount;
1667 private int m_InnerStateChanges;
1669 public RangedArrayList(ArrayList innerList, int index, int count)
1672 m_InnerIndex = index;
1673 m_InnerCount = count;
1674 m_InnerStateChanges = innerList._version;
1679 public override bool IsSynchronized
1687 public override object this[int index]
1691 if (index < 0 || index > m_InnerCount)
1693 throw new ArgumentOutOfRangeException("index");
1696 return m_InnerArrayList[m_InnerIndex + index];
1701 if (index < 0 || index > m_InnerCount)
1703 throw new ArgumentOutOfRangeException("index");
1706 m_InnerArrayList[m_InnerIndex + index] = value;
1714 public override int Count
1718 VerifyStateChanges();
1720 return m_InnerCount;
1724 public override int Capacity
1728 return m_InnerArrayList.Capacity;
1733 if (value < m_InnerCount)
1735 throw new ArgumentOutOfRangeException();
1744 private void VerifyStateChanges()
1746 if (m_InnerStateChanges != m_InnerArrayList._version)
1748 throw new InvalidOperationException
1749 ("ArrayList view is invalid because the underlying ArrayList was modified.");
1753 public override int Add(object value)
1755 VerifyStateChanges();
1757 m_InnerArrayList.Insert(m_InnerIndex + m_InnerCount, value);
1759 m_InnerStateChanges = m_InnerArrayList._version;
1761 return ++m_InnerCount;
1764 public override void Clear()
1766 VerifyStateChanges();
1768 m_InnerArrayList.RemoveRange(m_InnerIndex, m_InnerCount);
1771 m_InnerStateChanges = m_InnerArrayList._version;
1774 public override bool Contains(object value)
1776 return m_InnerArrayList.Contains(value, m_InnerIndex, m_InnerCount);
1779 public override int IndexOf(object value)
1781 return IndexOf(value, 0);
1784 public override int IndexOf(object value, int startIndex)
1786 return IndexOf(value, startIndex, m_InnerCount - startIndex);
1789 public override int IndexOf(object value, int startIndex, int count)
1791 if (startIndex < 0 || startIndex > m_InnerCount)
1793 throw new ArgumentOutOfRangeException("startIndex", startIndex,
1794 "Does not specify valid index.");
1799 throw new ArgumentOutOfRangeException("count", count,
1800 "Can't be less than 0.");
1803 // re-ordered to avoid possible integer overflow
1804 if (startIndex > m_InnerCount - count)
1806 // LAMESPEC: Every other method throws ArgumentException
1808 throw new ArgumentOutOfRangeException("count",
1809 "Start index and count do not specify a valid range.");
1812 int retval = m_InnerArrayList.IndexOf(value, m_InnerIndex + startIndex, count);
1820 return retval - m_InnerIndex;
1824 public override int LastIndexOf(object value)
1826 return LastIndexOf(value, m_InnerCount - 1);
1829 public override int LastIndexOf(object value, int startIndex)
1831 return LastIndexOf(value, startIndex, startIndex + 1);
1834 public override int LastIndexOf(object value, int startIndex, int count)
1838 throw new ArgumentOutOfRangeException("startIndex", startIndex, "< 0");
1843 throw new ArgumentOutOfRangeException("count", count, "count is negative.");
1846 int retval = m_InnerArrayList.LastIndexOf(value, m_InnerIndex + startIndex, count);
1854 return retval - m_InnerIndex;
1858 public override void Insert(int index, object value)
1860 VerifyStateChanges();
1862 if (index < 0 || index > m_InnerCount)
1864 throw new ArgumentOutOfRangeException("index", index,
1865 "Index must be >= 0 and <= Count.");
1868 m_InnerArrayList.Insert(m_InnerIndex + index, value);
1872 m_InnerStateChanges = m_InnerArrayList._version;
1875 public override void InsertRange(int index, ICollection c)
1877 VerifyStateChanges();
1879 if (index < 0 || index > m_InnerCount)
1881 throw new ArgumentOutOfRangeException("index", index,
1882 "Index must be >= 0 and <= Count.");
1885 m_InnerArrayList.InsertRange(m_InnerIndex + index, c);
1887 m_InnerCount += c.Count;
1889 m_InnerStateChanges = m_InnerArrayList._version;
1892 public override void Remove(object value)
1894 VerifyStateChanges();
1896 int x = IndexOf(value);
1903 m_InnerStateChanges = m_InnerArrayList._version;
1906 public override void RemoveAt(int index)
1908 VerifyStateChanges();
1910 if (index < 0 || index > m_InnerCount)
1912 throw new ArgumentOutOfRangeException("index", index,
1913 "Index must be >= 0 and <= Count.");
1916 m_InnerArrayList.RemoveAt(m_InnerIndex + index);
1919 m_InnerStateChanges = m_InnerArrayList._version;
1922 public override void RemoveRange(int index, int count)
1924 VerifyStateChanges();
1926 CheckRange(index, count, m_InnerCount);
1928 m_InnerArrayList.RemoveRange(m_InnerIndex + index, count);
1930 m_InnerCount -= count;
1932 m_InnerStateChanges = m_InnerArrayList._version;
1935 public override void Reverse()
1937 Reverse(0, m_InnerCount);
1940 public override void Reverse(int index, int count)
1942 VerifyStateChanges();
1944 CheckRange(index, count, m_InnerCount);
1946 m_InnerArrayList.Reverse(m_InnerIndex + index, count);
1948 m_InnerStateChanges = m_InnerArrayList._version;
1951 public override void SetRange(int index, ICollection c)
1953 VerifyStateChanges();
1955 if (index < 0 || index > m_InnerCount)
1957 throw new ArgumentOutOfRangeException("index", index,
1958 "Index must be >= 0 and <= Count.");
1961 m_InnerArrayList.SetRange(m_InnerIndex + index, c);
1963 m_InnerStateChanges = m_InnerArrayList._version;
1966 public override void CopyTo(System.Array array)
1971 public override void CopyTo(System.Array array, int index)
1973 CopyTo(0, array, index, m_InnerCount);
1976 public override void CopyTo(int index, System.Array array, int arrayIndex, int count)
1978 CheckRange(index, count, m_InnerCount);
1980 m_InnerArrayList.CopyTo(m_InnerIndex + index, array, arrayIndex, count);
1983 public override IEnumerator GetEnumerator()
1985 return GetEnumerator(0, m_InnerCount);
1988 public override IEnumerator GetEnumerator(int index, int count)
1990 CheckRange(index, count, m_InnerCount);
1992 return m_InnerArrayList.GetEnumerator(m_InnerIndex + index, count);
1995 public override void AddRange(ICollection c)
1997 VerifyStateChanges();
1999 m_InnerArrayList.InsertRange(m_InnerCount, c);
2001 m_InnerCount += c.Count;
2003 m_InnerStateChanges = m_InnerArrayList._version;
2006 public override int BinarySearch(object value)
2008 return BinarySearch(0, m_InnerCount, value, Comparer.Default);
2011 public override int BinarySearch(object value, IComparer comparer)
2013 return BinarySearch(0, m_InnerCount, value, comparer);
2016 public override int BinarySearch(int index, int count, object value, IComparer comparer)
2018 CheckRange(index, count, m_InnerCount);
2020 return m_InnerArrayList.BinarySearch(m_InnerIndex + index, count, value, comparer);
2023 public override object Clone()
2025 return new RangedArrayList((ArrayList)m_InnerArrayList.Clone(), m_InnerIndex, m_InnerCount);
2028 public override ArrayList GetRange(int index, int count)
2030 CheckRange(index, count, m_InnerCount);
2032 return new RangedArrayList(this, index, count);
2035 public override void TrimToSize()
2037 throw new NotSupportedException();
2040 public override void Sort()
2042 Sort(Comparer.Default);
2045 public override void Sort(IComparer comparer)
2047 Sort(0, m_InnerCount, comparer);
2050 public override void Sort(int index, int count, IComparer comparer)
2052 VerifyStateChanges();
2054 CheckRange(index, count, m_InnerCount);
2056 m_InnerArrayList.Sort(m_InnerIndex + index, count, comparer);
2058 m_InnerStateChanges = m_InnerArrayList._version;
2061 public override object[] ToArray()
2065 array = new object[m_InnerCount];
2067 m_InnerArrayList.CopyTo (m_InnerIndex, array, 0, m_InnerCount);
2072 public override Array ToArray(Type elementType)
2076 array = Array.CreateInstance(elementType, m_InnerCount);
2078 m_InnerArrayList.CopyTo(m_InnerIndex, array, 0, m_InnerCount);
2092 #region SynchronizedListWrapper
2095 private sealed class SynchronizedListWrapper
2098 private object m_SyncRoot;
2100 public SynchronizedListWrapper(IList innerList)
2103 m_SyncRoot = innerList.SyncRoot;
2106 public override int Count
2112 return m_InnerList.Count;
2117 public override bool IsSynchronized
2125 public override object SyncRoot
2131 return m_InnerList.SyncRoot;
2136 public override bool IsFixedSize
2142 return m_InnerList.IsFixedSize;
2147 public override bool IsReadOnly
2153 return m_InnerList.IsReadOnly;
2158 public override object this[int index]
2164 return m_InnerList[index];
2172 m_InnerList[index] = value;
2177 public override int Add(object value)
2181 return m_InnerList.Add(value);
2185 public override void Clear()
2189 m_InnerList.Clear();
2193 public override bool Contains(object value)
2197 return m_InnerList.Contains(value);
2201 public override int IndexOf(object value)
2205 return m_InnerList.IndexOf(value);
2209 public override void Insert(int index, object value)
2213 m_InnerList.Insert(index, value);
2217 public override void Remove(object value)
2221 m_InnerList.Remove(value);
2225 public override void RemoveAt(int index)
2229 m_InnerList.RemoveAt(index);
2233 public override void CopyTo(Array array, int index)
2237 m_InnerList.CopyTo(array, index);
2241 public override IEnumerator GetEnumerator()
2245 return m_InnerList.GetEnumerator();
2252 #region FixedSizeListWrapper
2255 private class FixedSizeListWrapper
2258 protected virtual string ErrorMessage
2262 return "List is fixed-size.";
2266 public override bool IsFixedSize
2274 public FixedSizeListWrapper(IList innerList)
2279 public override int Add(object value)
2281 throw new NotSupportedException(this.ErrorMessage);
2284 public override void Clear()
2286 throw new NotSupportedException(this.ErrorMessage);
2289 public override void Insert(int index, object value)
2291 throw new NotSupportedException(this.ErrorMessage);
2294 public override void Remove(object value)
2296 throw new NotSupportedException(this.ErrorMessage);
2299 public override void RemoveAt(int index)
2301 throw new NotSupportedException(this.ErrorMessage);
2307 #region ReadOnlyListWrapper
2310 private sealed class ReadOnlyListWrapper
2311 : FixedSizeListWrapper
2313 protected override string ErrorMessage
2317 return "List is read-only.";
2321 public override bool IsReadOnly
2329 public ReadOnlyListWrapper(IList innerList)
2334 public override object this[int index]
2338 return m_InnerList[index];
2343 throw new NotSupportedException(this.ErrorMessage);
2353 /// Decorates/Wraps any <c>IList</c> implementing object.
2356 private class ListWrapper
2361 protected IList m_InnerList;
2365 #region Constructors
2367 public ListWrapper(IList innerList)
2369 m_InnerList = innerList;
2376 public virtual object this[int index]
2380 return m_InnerList[index];
2385 m_InnerList[index] = value;
2393 public virtual int Count
2397 return m_InnerList.Count;
2401 public virtual bool IsSynchronized
2405 return m_InnerList.IsSynchronized;
2409 public virtual object SyncRoot
2413 return m_InnerList.SyncRoot;
2417 public virtual bool IsFixedSize
2421 return m_InnerList.IsFixedSize;
2425 public virtual bool IsReadOnly
2429 return m_InnerList.IsReadOnly;
2437 public virtual int Add(object value)
2439 return m_InnerList.Add(value);
2442 public virtual void Clear()
2444 m_InnerList.Clear();
2447 public virtual bool Contains(object value)
2449 return m_InnerList.Contains(value);
2452 public virtual int IndexOf(object value)
2454 return m_InnerList.IndexOf(value);
2457 public virtual void Insert(int index, object value)
2459 m_InnerList.Insert(index, value);
2462 public virtual void Remove(object value)
2464 m_InnerList.Remove(value);
2467 public virtual void RemoveAt(int index)
2469 m_InnerList.RemoveAt(index);
2472 public virtual void CopyTo(Array array, int index)
2474 m_InnerList.CopyTo(array, index);
2477 public virtual IEnumerator GetEnumerator()
2479 return m_InnerList.GetEnumerator();
2488 // Start of ArrayList
2494 private const int DefaultInitialCapacity = 4;
2496 private const int DefaultInitialCapacity = 16;
2500 /// Number of items in the list.
2505 /// Array to store the items.
2507 private object[] _items;
2510 /// Total number of state changes.
2512 private int _version;
2516 #region Constructors
2519 /// Initializes a new instance of the <see cref="ArrayList"/> class that is empty and
2520 /// has the default initial capacity (16).
2525 _items = new object[0];
2527 _items = new object[DefaultInitialCapacity];
2532 /// Initializes a new instance of the <see cref="ArrayList"/> class that contains
2533 /// elements copied from the specified collection and that has the same initial capacity
2534 /// as the number of elements copied.
2536 /// <param name="c">
2537 /// The <see cref="ICollection"/> whose elements are copied into the new list.
2539 /// <exception cref="ArgumentNullException">
2540 /// The argument <c>c</c> is a null reference.
2542 public ArrayList(ICollection c)
2548 throw new ArgumentNullException("c");
2553 if (array != null && array.Rank != 1)
2555 throw new RankException();
2558 _items = new object[c.Count];
2564 /// Initializes a new instance of the <see cref="ArrayList"/> class that is empty and
2565 /// has the specified initial capacity.
2567 /// <param name="capacity">
2568 /// The number of elements that hte new list is initially capable of storing.
2570 /// <exception cref="ArgumentOutOfRangeException">
2571 /// The <c>capacity</c> is less than zero.
2573 public ArrayList(int capacity)
2577 throw new ArgumentOutOfRangeException("capacity",
2578 capacity, "The initial capacity can't be smaller than zero.");
2583 capacity = DefaultInitialCapacity;
2585 _items = new object [capacity];
2589 /// Used by ArrayListAdapter to allow creation of an ArrayList with no storage buffer.
2591 private ArrayList(int initialCapacity, bool forceZeroSize)
2599 throw new InvalidOperationException("Use ArrayList(int)");
2604 /// Initializes a new array list that contains a copy of the given array and with the
2607 /// <param name="array"></param>
2608 private ArrayList(object[] array, int index, int count)
2612 _items = new object[DefaultInitialCapacity];
2616 _items = new object[count];
2619 Array.Copy(array, index, _items, 0, count);
2629 /// Gets/Sets an element in the list by index.
2631 /// <exception cref="ArgumentOutOfRangeException">
2632 /// The index is less than 0 or more then or equal to the list count.
2634 public virtual object this[int index]
2638 if (index < 0 || index >= _size)
2640 throw new ArgumentOutOfRangeException("index", index,
2641 "Index is less than 0 or more than or equal to the list count.");
2644 return _items[index];
2649 if (index < 0 || index >= _size)
2651 throw new ArgumentOutOfRangeException("index", index,
2652 "Index is less than 0 or more than or equal to the list count.");
2655 _items[index] = value;
2665 /// Gets the number of elements in the list.
2667 public virtual int Count
2676 /// Gets the number of elements the list can carry without needing to expand.
2679 /// ArrayLists automatically double their capacity when the capacity limit is broken.
2681 /// <exception cref="ArgumentOutOfRangeException">
2682 /// The capacity is less than the count.
2684 public virtual int Capacity
2688 return _items.Length;
2695 throw new ArgumentOutOfRangeException("Capacity", value,
2696 "Must be more than count.");
2701 newArray = new object[value];
2703 Array.Copy(_items, 0, newArray, 0, _size);
2710 /// <see cref="IList.IsFixedSize"/>
2713 public virtual bool IsFixedSize
2722 /// <see cref="IList.IsReadOnly"/>
2724 public virtual bool IsReadOnly
2733 /// <see cref="ICollection.IsSynchronized"/>
2735 public virtual bool IsSynchronized
2744 /// <see cref="ICollection.SyncRoot"/>
2746 public virtual object SyncRoot
2759 /// Ensures that the list has the capacity to contain the given <c>count</c> by
2760 /// automatically expanding the capacity when required.
2762 private void EnsureCapacity(int count)
2764 if (count <= _items.Length)
2772 newLength = _items.Length << 1;
2774 newLength = DefaultInitialCapacity;
2776 while (newLength < count)
2781 newData = new object[newLength];
2783 Array.Copy(_items, 0, newData, 0, _items.Length);
2789 /// Shifts a section of the list.
2791 /// <param name="index">
2792 /// The start of the section to shift (the element at index is included in the shift).
2794 /// <param name="count">
2795 /// The number of positions to shift by (can be negative).
2797 private void Shift(int index, int count)
2801 if (_size + count > _items.Length)
2806 newLength = (_items.Length > 0) ? _items.Length << 1 : 1;
2808 while (newLength < _size + count)
2813 newData = new object[newLength];
2815 Array.Copy(_items, 0, newData, 0, index);
2816 Array.Copy(_items, index, newData, index + count, _size - index);
2822 Array.Copy(_items, index, _items, index + count, _size - index);
2827 // Remember count is negative so this is actually index + (-count)
2829 int x = index - count ;
2831 Array.Copy(_items, x, _items, index, _size - x);
2832 Array.Clear(_items, _size + count, - count);
2836 public virtual int Add(object value)
2838 // Do a check here in case EnsureCapacity isn't inlined.
2840 if (_items.Length <= _size /* same as _items.Length < _size + 1) */)
2842 EnsureCapacity(_size + 1);
2845 _items[_size] = value;
2852 public virtual void Clear()
2854 // Keep the array but null all members so they can be garbage collected.
2856 Array.Clear(_items, 0, _size);
2862 public virtual bool Contains(object value)
2864 return IndexOf(value, 0, _size) > -1;
2867 internal virtual bool Contains(object value, int startIndex, int count)
2869 return IndexOf(value, startIndex, count) > -1;
2872 public virtual int IndexOf(object value)
2874 return IndexOf(value, 0);
2877 public virtual int IndexOf(object value, int startIndex)
2879 return IndexOf(value, startIndex, _size - startIndex);
2882 public virtual int IndexOf(object value, int startIndex, int count)
2884 if (startIndex < 0 || startIndex > _size)
2886 throw new ArgumentOutOfRangeException("startIndex", startIndex,
2887 "Does not specify valid index.");
2892 throw new ArgumentOutOfRangeException("count", count,
2893 "Can't be less than 0.");
2896 // re-ordered to avoid integer overflow
2897 if (startIndex > _size - count)
2899 // LAMESPEC: Every other method throws ArgumentException
2901 throw new ArgumentOutOfRangeException("count",
2902 "Start index and count do not specify a valid range.");
2905 return Array.IndexOf(_items, value, startIndex, count);
2908 public virtual int LastIndexOf(object value)
2910 return LastIndexOf(value, _size - 1);
2913 public virtual int LastIndexOf(object value, int startIndex)
2915 return LastIndexOf(value, startIndex, startIndex + 1);
2918 public virtual int LastIndexOf (object value, int startIndex, int count)
2920 // Array will catch the exceptions
2921 return Array.LastIndexOf (_items, value, startIndex, count);
2924 public virtual void Insert(int index, object value)
2926 if (index < 0 || index > _size)
2928 throw new ArgumentOutOfRangeException("index", index,
2929 "Index must be >= 0 and <= Count.");
2934 _items[index] = value;
2939 public virtual void InsertRange(int index, ICollection c)
2945 throw new ArgumentNullException("c");
2948 if (index < 0 || index > _size)
2950 throw new ArgumentOutOfRangeException("index", index,
2951 "Index must be >= 0 and <= Count.");
2956 // Do a check here in case EnsureCapacity isn't inlined.
2958 if (_items.Length < _size + i)
2960 EnsureCapacity(_size + i);
2965 Array.Copy(_items, index, _items, index + i, _size - index);
2968 // Handle inserting a range from a list to itself specially.
2970 if (this == c.SyncRoot)
2972 // Copy range before the insert point.
2974 Array.Copy(_items, 0, _items, index, index);
2976 // Copy range after the insert point.
2978 Array.Copy(_items, index + i, _items, index << 1, _size - index);
2982 c.CopyTo(_items, index);
2989 public virtual void Remove(object value)
3003 public virtual void RemoveAt(int index)
3005 if (index < 0 || index >= _size)
3007 throw new ArgumentOutOfRangeException("index", index,
3008 "Less than 0 or more than list count.");
3016 public virtual void RemoveRange(int index, int count)
3018 ArrayList.CheckRange(index, count, _size);
3020 Shift(index, -count);
3025 public virtual void Reverse()
3027 Array.Reverse(_items, 0, _size);
3031 public virtual void Reverse(int index, int count)
3033 ArrayList.CheckRange(index, count, _size);
3035 Array.Reverse(_items, index, count);
3039 public virtual void CopyTo(System.Array array)
3041 Array.Copy(_items, array, _size);
3044 public virtual void CopyTo(System.Array array, int index)
3046 CopyTo(0, array, index, _size);
3049 public virtual void CopyTo(int index, System.Array array, int arrayIndex, int count)
3053 throw new ArgumentNullException("array");
3056 if (array.Rank != 1)
3059 // This should be a RankException because Array.Copy throws RankException.
3061 throw new ArgumentException("Must have only 1 dimensions.", "array");
3064 Array.Copy(_items, index, array, arrayIndex, count);
3067 public virtual IEnumerator GetEnumerator()
3069 return new SimpleEnumerator(this);
3072 public virtual IEnumerator GetEnumerator(int index, int count)
3074 ArrayList.CheckRange(index, count, _size);
3076 return new ArrayListEnumerator(this, index, count);
3079 public virtual void AddRange(ICollection c)
3081 InsertRange(_size, c);
3084 public virtual int BinarySearch(object value)
3088 return Array.BinarySearch(_items, 0, _size, value);
3090 catch (InvalidOperationException e)
3092 throw new ArgumentException(e.Message);
3096 public virtual int BinarySearch(object value, IComparer comparer)
3100 return Array.BinarySearch(_items, 0, _size, value, comparer);
3102 catch (InvalidOperationException e)
3104 throw new ArgumentException(e.Message);
3108 public virtual int BinarySearch(int index, int count, object value, IComparer comparer)
3112 return Array.BinarySearch(_items, index, count, value, comparer);
3114 catch (InvalidOperationException e)
3116 throw new ArgumentException(e.Message);
3119 public virtual ArrayList GetRange(int index, int count)
3121 ArrayList.CheckRange(index, count, _size);
3123 if (this.IsSynchronized)
3125 return ArrayList.Synchronized(new RangedArrayList(this, index, count));
3129 return new RangedArrayList(this, index, count);
3133 public virtual void SetRange(int index, ICollection c)
3137 throw new ArgumentNullException("c");
3140 if (index < 0 || index + c.Count > _size)
3142 throw new ArgumentOutOfRangeException("index");
3145 c.CopyTo(_items, index);
3150 public virtual void TrimToSize()
3152 if (_items.Length > _size)
3158 newArray = new object[DefaultInitialCapacity];
3162 newArray = new object[_size];
3165 Array.Copy(_items, 0, newArray, 0, _size);
3171 public virtual void Sort()
3173 Array.Sort(_items, 0, _size);
3178 public virtual void Sort(IComparer comparer)
3180 Array.Sort(_items, 0, _size, comparer);
3183 public virtual void Sort(int index, int count, IComparer comparer)
3185 ArrayList.CheckRange(index, count, _size);
3187 Array.Sort(_items, index, count, comparer);
3190 public virtual object[] ToArray()
3194 retval = new object[_size];
3201 public virtual Array ToArray(Type elementType)
3205 retval = Array.CreateInstance(elementType, _size);
3212 public virtual object Clone()
3214 return new ArrayList(this._items, 0, this._size);
3219 #region Static Methods
3222 /// Does a check of the arguments many of the methods in ArrayList use.
3225 /// The choice of exceptions thrown sometimes seem to be arbitrarily chosen so
3226 /// not all methods actually make use of CheckRange.
3228 internal static void CheckRange(int index, int count, int listCount)
3232 throw new ArgumentOutOfRangeException("index", index, "Can't be less than 0.");
3237 throw new ArgumentOutOfRangeException("count", count, "Can't be less than 0.");
3240 // re-ordered to avoid possible integer overflow
3241 if (index > listCount - count)
3243 throw new ArgumentException("Index and count do not denote a valid range of elements.", "index");
3247 public static ArrayList Adapter(IList list)
3249 // LAMESPEC: EWWW. Other lists aren't *Array*Lists.
3253 throw new ArgumentNullException("list");
3256 ArrayList arrayList = list as ArrayList;
3257 if (arrayList != null)
3260 arrayList = new ArrayListAdapter(list);
3262 if (list.IsSynchronized)
3263 return ArrayList.Synchronized(arrayList);
3268 public static ArrayList Synchronized(ArrayList arrayList)
3270 if (arrayList == null)
3272 throw new ArgumentNullException("arrayList");
3275 if (arrayList.IsSynchronized)
3280 return new SynchronizedArrayListWrapper(arrayList);
3283 public static IList Synchronized(IList list)
3287 throw new ArgumentNullException("list");
3290 if (list.IsSynchronized)
3295 return new SynchronizedListWrapper(list);
3298 public static ArrayList ReadOnly(ArrayList arrayList)
3300 if (arrayList == null)
3302 throw new ArgumentNullException("arrayList");
3305 if (arrayList.IsReadOnly)
3310 return new ReadOnlyArrayListWrapper(arrayList);
3313 public static IList ReadOnly(IList list)
3317 throw new ArgumentNullException("list");
3320 if (list.IsReadOnly)
3325 return new ReadOnlyListWrapper(list);
3328 public static ArrayList FixedSize(ArrayList arrayList)
3330 if (arrayList == null)
3332 throw new ArgumentNullException("arrayList");
3335 if (arrayList.IsFixedSize)
3340 return new FixedSizeArrayListWrapper(arrayList);
3343 public static IList FixedSize(IList list)
3347 throw new ArgumentNullException("list");
3350 if (list.IsFixedSize)
3355 return new FixedSizeListWrapper(list);
3358 public static ArrayList Repeat(object value, int count)
3360 ArrayList arrayList = new ArrayList(count);
3362 for (int i = 0; i < count; i++)
3364 arrayList.Add(value);