arraylist.cs

来自「没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没」· CS 代码 · 共 2,722 行 · 第 1/4 页

CS
2,722
字号
						// Find two values to be swapped.						while(comparer.Compare(this[i], pivot) < 0)						{							++i;						}						while(comparer.Compare(this[j], pivot) > 0)						{							--j;						}						if(i > j)						{							break;						}								// Swap the values.						if(i < j)						{							temp = this[i];							this[i] = this[j];							this[j] = temp;						}						++i;						--j;					}					while(i <= j);							// Sort the partitions.					if((j - lower) <= (upper - i))					{						if(lower < j)						{							InnerSort(lower, j, comparer);						}						lower = i;					}					else					{						if(i < upper)						{							InnerSort(i, upper, comparer);						}						upper = j;					}				}				while(lower < upper);			}	// Sort the contents of this array list.	public virtual void Sort()			{				InnerSort(0, Count - 1, Comparer.Default);			}	public virtual void Sort(IComparer comparer)			{				if(comparer == null)				{					comparer = Comparer.Default;				}				InnerSort(0, Count - 1, comparer);			}	public virtual void Sort(int index, int count, IComparer comparer)			{				if(index < 0)				{					throw new ArgumentOutOfRangeException						("index", _("ArgRange_Array"));				}				if(count < 0)				{					throw new ArgumentOutOfRangeException						("count", _("ArgRange_Array"));				}				if((Count - index) < count)				{					throw new ArgumentException(_("Arg_InvalidArrayRange"));				}				if(comparer == null)				{					comparer = Comparer.Default;				}				InnerSort(index, index + count - 1, comparer);			}	// Create an array that contains the elements of this array list.	public virtual Object[] ToArray()			{				int count = Count;				Object[] array = new Object[count];				int index;				for(index = 0; index < count; ++index)				{					array[index] = this[index];				}				return array;			}	public virtual Array ToArray(Type type)			{				int count = Count;				Array array = Array.CreateInstance(type, count);				int index;				for(index = 0; index < count; ++index)				{					array.SetValue(this[index], index);				}				return array;			}	// Trim the array list to its actual size.	public virtual void TrimToSize()			{				if(count != 0)				{					if(count != store.Length)					{						Object[] newStore = new Object[count];						int index;						for(index = 0; index < count; ++index)						{							newStore[index] = store[index];						}						store = newStore;					}				}				else if(store.Length != 16)				{					store = new Object[16];				}				++generation;			}	// Get or set the current capacity of the array list.	public virtual int Capacity			{				get				{					return store.Length;				}				set				{					if(value <= 0)					{						value = 16;					}					if(value < count)					{						throw new ArgumentOutOfRangeException							("value", _("Arg_CannotReduceCapacity"));					}					if(value != store.Length)					{						Object[] newStore = new Object[value];						int index;						for(index = 0; index < count; ++index)						{							newStore[index] = store[index];						}						store = newStore;					}					++generation;				}			}	// Get an enumerator for this array list.	public virtual IEnumerator GetEnumerator()			{				return new ArrayListEnumerator(this, 0, Count);			}	public virtual IEnumerator GetEnumerator(int index, int count)			{				if(index < 0)				{					throw new ArgumentOutOfRangeException						("index", _("ArgRange_Array"));				}				if(count < 0)				{					throw new ArgumentOutOfRangeException						("count", _("ArgRange_Array"));				}				if((Count - index) < count)				{					throw new ArgumentException(_("Arg_InvalidArrayRange"));				}				return new ArrayListEnumerator(this, index, index + count);			}	// Array list enumerator class.	private class ArrayListEnumerator : IEnumerator	{		private ArrayList list;		private int start;		private int finish;		private int position;		private int generation;		public ArrayListEnumerator(ArrayList list, int start, int finish)				{					this.list = list;					this.start = start;					this.finish = finish;					position = start - 1;					generation = list.generation;				}		public bool MoveNext()				{					if(generation != list.generation)					{						throw new InvalidOperationException							(_("Invalid_CollectionModified"));					}					++position;					return (position < finish);				}		public void Reset()				{					position = start - 1;				}		public Object Current				{					get					{						if(generation != list.generation)						{							throw new InvalidOperationException								(_("Invalid_CollectionModified"));						}						else if(position < start || position >= finish)						{							throw new InvalidOperationException								(_("Invalid_BadEnumeratorPosition"));						}						return list[position];					}				}	}; // class ArrayListEnumerator	// Adapt an IList to appear to look like an ArrayList.	public static ArrayList Adapter(IList list)		{			if(list == null)			{				throw new ArgumentNullException("list");			}			else if(list is ArrayList)			{				return (ArrayList)list;			}			else			{				return new IListWrapper(list);			}		}	// Wrapper class for IList.	private class IListWrapper : ArrayList	{		// Internal state.		private IList list;		// Constructor		public IListWrapper(IList list)				{					this.list = list;				}		// Implement the IList interface.		public override int Add(Object value)				{					return list.Add(value);				}		public override void Clear()				{					list.Clear();				}		public override bool Contains(Object item)				{					return list.Contains(item);				}		public override int IndexOf(Object value)				{					return list.IndexOf(value);				}		public override void Insert(int index, Object value)				{					list.Insert(index, value);				}		public override void Remove(Object value)				{					list.Remove(value);				}		public override void RemoveAt(int index)				{					list.RemoveAt(index);				}		public override bool IsFixedSize				{					get					{						return list.IsFixedSize;					}				}		public override bool IsReadOnly				{					get					{						return list.IsReadOnly;					}				}		public override Object this[int index]				{					get					{						return list[index];					}					set					{						list[index] = value;					}				}		// Implement the ICloneable interface.		public override Object Clone()				{					return new IListWrapper(list);				}		// Range-related methods.		public override void AddRange(ICollection c)				{					if(c == null)					{						throw new ArgumentNullException("c");					}					IEnumerator enumerator = c.GetEnumerator();					while(enumerator.MoveNext())					{						list.Add(enumerator.Current);					}				}		public override void InsertRange(int index, ICollection c)				{					if(c == null)					{						throw new ArgumentNullException("c");					}					IEnumerator enumerator = c.GetEnumerator();					while(enumerator.MoveNext())					{						list.Insert(index++, enumerator.Current);					}				}		public override void RemoveRange(int index, int count)				{					if(index < 0 || index >= list.Count)					{						throw new ArgumentOutOfRangeException							("index", _("ArgRange_Array"));					}					if(count < 0)					{						throw new ArgumentOutOfRangeException							("count", _("ArgRange_Array"));					}					while(count > 0)					{						list.RemoveAt(index);						--count;					}				}		public override void SetRange(int index, ICollection c)				{					if(c == null)					{						throw new ArgumentNullException("c");					}					IEnumerator enumerator = c.GetEnumerator();					while(enumerator.MoveNext())					{						list[index++] = enumerator.Current;					}				}		// Implement the ICollection interface.		public override void CopyTo(Array array, int arrayIndex)				{					list.CopyTo(array, arrayIndex);				}		public override int Count				{					get					{						return list.Count;					}				}		public override bool IsSynchronized				{					get					{						return list.IsSynchronized;					}				}		public override Object SyncRoot				{					get					{						return list.SyncRoot;					}				}		// Copy from this array list to another array.		public override void CopyTo(Array array)				{					list.CopyTo(array, 0);				}		// Reverse the contents of this array list.		public override void Reverse()				{					Reverse(0, Count);				}		public override void Reverse(int index, int count)				{					// Validate the parameters.					if(index < 0)					{						throw new ArgumentOutOfRangeException							("index", _("ArgRange_Array"));					}					if(count < 0)					{						throw new ArgumentOutOfRangeException							("count", _("ArgRange_Array"));					}					if((Count - index) < count)					{						throw new ArgumentException(_("Arg_InvalidArrayRange"));					}					// Perform the reversal.					Object temp;					int lower = index;					int upper = index + count - 1;					while(lower < upper)					{						temp = list[lower];						list[lower] = list[upper];						list[upper] = temp;						++lower;						--upper;					}				}		// Trim the array list to its actual size.		public override void TrimToSize()				{					// Nothing can be done here.				}		// Get or set the current capacity of the array list.		public override int Capacity				{					get					{						// Return the IList's count as the capacity.						return list.Count;					}					set					{						// IList does not have a capacity, so just validate.						if(value < list.Count)						{							throw new ArgumentOutOfRangeException								("value", _("Arg_CannotReduceCapacity"));						}					}				}	}; // class IListWrapper	// Pass-through wrapper class that encapsulates another array list.	private class PassThroughWrapper : ArrayList	{		protected ArrayList list;		public PassThroughWrapper(ArrayList list)				{					this.list = list;				}		// Implement the IList interface.		public override int Add(Object value)				{					return list.Add(value);				}		public override void Clear()				{					list.Clear();				}		public override bool Contains(Object item)				{					return list.Contains(item);				}		public override int IndexOf(Object value)				{					return list.IndexOf(value);				}		public override void Insert(int index, Object value)				{					list.Insert(index, value);				}		public override void Remove(Object value)				{					list.Remove(value);				}		public override void RemoveAt(int index)				{					list.RemoveAt(index);				}		public override bool IsFixedSize				{					get					{						return list.IsFixedSize;					}				}		public override bool IsReadOnly				{					get					{						return list.IsReadOnly;					}				}		public override Object this[int index]				{					get					{						return list[index];					}					set					{						list[index] = value;					}				}		// Range-related methods.		public override void AddRange(ICollection c)				{					list.AddRange(c);				}		public override void InsertRange(int index, ICollection c)				{					list.InsertRange(index, c);				}		public override void RemoveRange(int index, int count)				{					list.RemoveRange(index, count);				}		public override void SetRange(int index, ICollection c)				{					list.SetRange(index, c);				}		// Searching methods.		public override int BinarySearch(Object value)				{					return list.BinarySearch(value);				}		public override int BinarySearch(Object value, IComparer comparer)				{					return list.BinarySearch(value, comparer);				}		public override int BinarySearch(int index, int count,									    Object value, IComparer comparer)				{					return list.BinarySearch(index, count, value, comparer);				}		public override int IndexOf(Object value, int startIndex)				{					return list.IndexOf(value, startIndex);				}		public override int IndexOf(Object value, int startIndex, int count)				{					return list.IndexOf(value, startIndex, count);				}		public override int LastIndexOf(Object value)				{					return list.LastIndexOf(value);				}		public override int LastIndexOf(Object value, int startIndex)				{					return list.LastIndexOf(value, startIndex);				}		public override int LastIndexOf(Object value, int startIndex, int count)				{					return list.LastIndexOf(value, startIndex, count);				}		// Implement the ICollection interface.		public override void CopyTo(Array array, int arrayIndex)				{					list.CopyTo(array, arrayIndex);				}		public override int Count				{					get					{						return list.count;					}				}		public override bool IsSynchronized				{					get					{						return list.IsSynchronized;					}				}		public override Object SyncRoot				{					get					{						return list.SyncRoot;					}				}		// Copy from this array list to another array.		public override void CopyTo(Array array)				{					list.CopyTo(array);				}		public override void CopyTo(int index, Array array,							   	   int arrayIndex, int count)				{					list.CopyTo(index, array, arrayIndex, count);				}		// Reverse the contents of this array list.		public override void Reverse()				{					list.Reverse();				}		public override void Reverse(int index, int count)				{					list.Reverse(index, count);				}		// Sort the contents of this array list.		public override void Sort()				{					list.Sort();				}		public override void Sort(IComparer comparer)				{					list.Sort(comparer);				}		public override void Sort(int index, int count, IComparer comparer)				{					list.Sort(index, count, comparer);				}		// Create an array that contains the elements of this array list.		public override Object[] ToArray()				{					return list.ToArray();				}		public override Array ToArray(Type type)				{					return list.ToArray(type);				}		// Trim the array list to its actual size.		public override void TrimToSize()				{					list.TrimToSize();				}		// Get or set the current capacity of the array list.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?