// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
/*============================================================
**
** Class: ArrayList
**
** <OWNER>Microsoft</OWNER>
**
**
** Purpose: Implements a dynamically sized List as an array,
** and provides many convenience methods for treating
** an array as an IList.
**
**
===========================================================*/
namespace System.Collections {
using System;
using System.Runtime;
using System.Security;
using System.Security.Permissions;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
// Implements a variable-size List that uses an array of objects to store the
// elements. A ArrayList has a capacity, which is the allocated length
// of the internal array. As elements are added to a ArrayList, the capacity
// of the ArrayList is automatically increased as required by reallocating the
// internal array.
//
#if FEATURE_CORECLR
[FriendAccessAllowed]
#endif
[DebuggerTypeProxy(typeof(System.Collections.ArrayList.ArrayListDebugView))]
[DebuggerDisplay("Count = {Count}")]
[Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class ArrayList : IList, ICloneable
{
private Object[] _items;
[ContractPublicPropertyName("Count")]
private int _size;
private int _version;
[NonSerialized]
private Object _syncRoot;
private const int _defaultCapacity = 4;
private static readonly Object[] emptyArray = EmptyArray<Object>.Value;
// Note: this constructor is a bogus constructor that does nothing
// and is for use only with SyncArrayList.
internal ArrayList( bool trash )
{
}
// Constructs a ArrayList. The list is initially empty and has a capacity
// of zero. Upon adding the first element to the list the capacity is
// increased to _defaultCapacity, and then increased in multiples of two as required.
public ArrayList() {
_items = emptyArray;
}
// Constructs a ArrayList with a given initial capacity. The list is
// initially empty, but will have room for the given number of elements
// before any reallocations are required.
//
public ArrayList(int capacity) {
if (capacity < 0) throw new ArgumentOutOfRangeException("capacity", Environment.GetResourceString("ArgumentOutOfRange_MustBeNonNegNum", "capacity"));
Contract.EndContractBlock();
if (capacity == 0)
_items = emptyArray;
else
_items = new Object[capacity];
}
// Constructs a ArrayList, copying the contents of the given collection. The
// size and capacity of the new list will both be equal to the size of the
// given collection.
//
public ArrayList(ICollection c) {
if (c==null)
throw new ArgumentNullException("c", Environment.GetResourceString("ArgumentNull_Collection"));
Contract.EndContractBlock();
int count = c.Count;
if (count == 0)
{
_items = emptyArray;
}
else {
_items = new Object[count];
AddRange(c);
}
}
// Gets and sets the capacity of this list. The capacity is the size of
// the internal array used to hold items. When set, the internal
// array of the list is reallocated to the given capacity.
//
public virtual int Capacity {
get {
Contract.Ensures(Contract.Result<int>() >= Count);
return _items.Length;
}
set {
if (value < _size) {
throw new ArgumentOutOfRangeException("value", Environment.GetResourceString("ArgumentOutOfRange_SmallCapacity"));
}
Contract.Ensures(Capacity >= 0);
Contract.EndContractBlock();
// We don't want to update the version number when we change the capacity.
// Some existing applications have dependency on this.
if (value != _items.Length) {
if (value > 0) {
Object[] newItems = new Object[value];
if (_size > 0) {
Array.Copy(_items, 0, newItems, 0, _size);
}
_items = newItems;
}
else {
_items = new Object[_defaultCapacity];
}
}
}
}
// Read-only property describing how many elements are in the List.
public virtual int Count {
get {
Contract.Ensures(Contract.Result<int>() >= 0);
return _size;
}
}
public virtual bool IsFixedSize {
get { return false; }
}
// Is this ArrayList read-only?
public virtual bool IsReadOnly {
get { return false; }
}
// Is this ArrayList synchronized (thread-safe)?
public virtual bool IsSynchronized {
get { return false; }
}
// Synchronization root for this object.
public virtual Object SyncRoot {
get {
if( _syncRoot == null) {
System.Threading.Interlocked.CompareExchange<Object>(ref _syncRoot, new Object(), null);
}
return _syncRoot;
}
}
// Sets or Gets the element at the given index.
//
public virtual Object this[int index] {
get {
if (index < 0 || index >= _size) throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index"));
Contract.EndContractBlock();
return _items[index];
}
set {
if (index < 0 || index >= _size) throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_Index"));
Contract.EndContractBlock();
_items[index] = value;
_version++;
}
}
// Creates a ArrayList wrapper for a particular IList. This does not
// copy the contents of the IList, but only wraps the ILIst. So any
// changes to the underlying list will affect the ArrayList. This would
// be useful if you want to Reverse a subrange of an IList, or want to
// use a generic BinarySearch or Sort method without implementing one yourself.
// However, since these methods are generic, the performance may not be
// nearly as good for some operations as they would be on the IList itself.
//
public static ArrayList Adapter(IList list) {
if (list==null)
throw new ArgumentNullException("list");
Contract.Ensures(Contract.Result<ArrayList>() != null);
Contract.EndContractBlock();
return new IListWrapper(list);
}
// Adds the given object to the end of this list. The size of the list is
// increased by one. If required, the capacity of the list is doubled
// before adding the new element.
//
public virtual int Add(Object value) {
Contract.Ensures(Contract.Result<int>() >= 0);
if (_size == _items.Length) EnsureCapacity(_size + 1);
_items[_size] = value;
_version++;
return _size++;
}
// Adds the elements of the given collection to the end of this list. If
// required, the capacity of the list is increased to twice the previous
// capacity or the new size, whichever is larger.
//
public virtual void AddRange(ICollection c) {
InsertRange(_size, c);
}
// Searches a section of the list for a given element using a binary search
// algorithm. Elements of the list are compared to the search value using
// the given IComparer interface. If comparer is null, elements of
// the list are compared to the search value using the IComparable
// interface, which in that case must be implemented by all elements of the
// list and the given search value. This method assumes that the given
// section of the list is already sorted; if this is not the case, the
// result will be incorrect.
//
// The method returns the index of the given value in the list. If the
// list does not contain the given value, the method returns a negative
// integer. The bitwise complement operator (~) can be applied to a
// negative result to produce the index of the first element (if any) that
// is larger than the given search value. This is also the index at which
// the search value should be inserted into the list in order for the list
// to remain sorted.
//
// The method uses the Array.BinarySearch method to perform the
// search.
//
public virtual int BinarySearch(int index, int count, Object value, IComparer comparer) {
if (index < 0)
throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
if (count < 0)
throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
if (_size - index < count)
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
Contract.Ensures(Contract.Result<int>() < Count);
Contract.Ensures(Contract.Result<int>() < index + count);
Contract.EndContractBlock();
return Array.BinarySearch((Array)_items, index, count, value, comparer);
}
public virtual int BinarySearch(Object value)
{
Contract.Ensures(Contract.Result<int>() < Count);
return BinarySearch(0, Count, value, null);
}
public virtual int BinarySearch(Object value, IComparer comparer)
{
Contract.Ensures(Contract.Result<int>() < Count);
return BinarySearch(0, Count, value, comparer);
}
// Clears the contents of ArrayList.
public virtual void Clear() {
if (_size > 0)
{
Array.Clear(_items, 0, _size); // Don't need to doc this but we clear the elements so that the gc can reclaim the references.
_size = 0;
}
_version++;
}
// Clones this ArrayList, doing a shallow copy. (A copy is made of all
// Object references in the ArrayList, but the Objects pointed to
// are not cloned).
public virtual Object Clone()
{
Contract.Ensures(Contract.Result<Object>() != null);
ArrayList la = new ArrayList(_size);
la._size = _size;
la._version = _version;
Array.Copy(_items, 0, la._items, 0, _size);
return la;
}
// Contains returns true if the specified element is in the ArrayList.
// It does a linear, O(n) search. Equality is determined by calling
// item.Equals().
//
public virtual bool Contains(Object item) {
if (item==null) {
for(int i=0; i<_size; i++)
if (_items[i]==null)
return true;
return false;
}
else {
for(int i=0; i<_size; i++)
if ( (_items[i] != null) && (_items[i].Equals(item)) )
return true;
return false;
}
}
// Copies this ArrayList into array, which must be of a
// compatible array type.
//
public virtual void CopyTo(Array array) {
CopyTo(array, 0);
}
// Copies this ArrayList into array, which must be of a
// compatible array type.
//
public virtual void CopyTo(Array array, int arrayIndex) {
if ((array != null) && (array.Rank != 1))
throw new ArgumentException(Environment.GetResourceString("Arg_RankMultiDimNotSupported"));
Contract.EndContractBlock();
// Delegate rest of error checking to Array.Copy.
Array.Copy(_items, 0, array, arrayIndex, _size);
}
// Copies a section of this list to the given array at the given index.
//
// The method uses the Array.Copy method to copy the elements.
//
public virtual void CopyTo(int index, Array array, int arrayIndex, int count) {
if (_size - index < count)
throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
if ((array != null) && (array.Rank != 1))
throw new ArgumentException(Environment.GetResourceString("Arg_RankMultiDimNotSupported"));
Contract.EndContractBlock();
// Delegate rest of error checking to Array.Copy.
Array.Copy(_items, index, array, arrayIndex, count);
}
// Ensures that the capacity of this list is at least the given minimum
// value. If the currect capacity of the list is less than min, the
// capacity is increased to twice the current capacity or to min,
// whichever is larger.
private void EnsureCapacity(int min) {
if (_items.Length < min) {
int newCapacity = _items.Length == 0? _defaultCapacity: _items.Length * 2;
// Allow the list to grow to maximum possible capacity (~2G elements) before encountering overflow.
// Note that this check works even when _items.Length overflowed thanks to the (uint) cast
if ((uint)newCapacity > Array.MaxArrayLength) newCapacity = Array.MaxArrayLength;
if (newCapacity < min) newCapacity = min;
Capacity = newCapacity;
}
}
// Returns a list wrapper that is fixed at the current size. Operations
// that add or remove items will fail, however, replacing items is allowed.
//
public static IList FixedSize(IList list) {
if (list==null)
throw new ArgumentNullException("list");
Contract.Ensures(Contract.Result<IList>() != null);
Contract.EndContractBlock();
return new FixedSizeList(list);
}
// Returns a list wrapper that is fixed at the current size. Operations
// that add or remove items will fail, however, replacing items is allowed.
//
public static ArrayList FixedSize(ArrayList list) {
if (list==null)
throw new ArgumentNullException("list");
Contract.Ensures(Contract.Result<ArrayList>() != null);
Contract.EndContractBlock();
return new FixedSizeArrayList(list);
}
// Returns an enumerator for this list with the given
// permission for removal of elements. If modifications made to the list
// while an enumeration is in progress, the MoveNext and
// GetObject methods of the enumerator will throw an exception.
//
public virtual IEnumerator GetEnumerator() {
Contract.Ensures(Contract.Result<IEnumerator>() != null);
return new ArrayListEnumeratorSimple(this);
}
// Returns an enumerator for a section of this list with the given
// permission for removal of elements. If modifications made to the list
// while an enumeration is in progress, the MoveNext and
|