| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640 |
- // System.Collections.ArrayList
- //
- // Author:
- // Vladimir Vukicevic ([email protected])
- // Duncan Mak ([email protected])
- // Patrik Torstensson ([email protected])
- // Ben Maurer ([email protected])
- //
- // (C) 2001 Vladimir Vukicevic
- // (C) 2002 Ximian, Inc.
- // (C) 2003 Ben Maurer
- //
- using System;
- namespace System.Collections {
- [MonoTODO ("add versioning, changing the arraylist should invalidate all enumerators")]
- [Serializable]
- public class ArrayList : IList, ICollection, IEnumerable, ICloneable {
- // Keep these three fields in sync with mono-reflection.h.
- private int count = 0;
- private int capacity = defaultCapacity;
- private object[] dataArray;
-
- // constructors
- public ArrayList () {
- dataArray = new object[capacity];
- }
- public ArrayList (ICollection c) {
- if (null == c)
- throw new ArgumentNullException();
- //Emulate MS.NET behavior. Throw RankException when passed a
- // multi-dimensional Array.
- Array arr = c as Array;
- if (null != arr && arr.Rank > 1)
- throw new RankException ();
- this.capacity = (c.Count == 0) ? defaultCapacity : c.Count;
- dataArray = new object [capacity];
- foreach (object o in c)
- Add (o);
- }
- public ArrayList (int capacity) {
- if (capacity < 0)
- throw new ArgumentOutOfRangeException ("capacity", capacity, "Value must be greater than or equal to zero.");
- if (capacity > 0)
- this.capacity = capacity;
- // else if capacity == 0 then use defaultCapacity
- dataArray = new object[this.capacity];
- }
- private ArrayList (object[] dataArray, int count, int capacity,
- bool fixedSize, bool readOnly, bool synchronized) {
- this.dataArray = new object [capacity];
- dataArray.CopyTo (this.dataArray, 0);
- this.count = count;
- this.capacity = capacity;
- this.fixedSize = fixedSize;
- this.readOnly = readOnly;
- this.synchronized = synchronized;
- }
- public static ArrayList ReadOnly (ArrayList list) {
- if (list == null)
- throw new ArgumentNullException ();
- if (list.IsSynchronized)
- return ArrayList.Synchronized (list);
- else
- return new ArrayList (list.ToArray (), list.Count, list.Capacity, list.IsFixedSize, true, list.IsSynchronized);
- }
- public static IList ReadOnly (IList list) {
- if (list == null)
- throw new ArgumentNullException ();
- ArrayList al = new ArrayList ();
- foreach (object o in list)
- al.Add (o);
- return (IList) ArrayList.ReadOnly (al);
- }
- public static ArrayList Synchronized (ArrayList list) {
- if (list == null)
- throw new ArgumentNullException ();
- return new SyncArrayList(new ArrayList (list.ToArray (), list.Count, list.Capacity, list.IsFixedSize, list.IsReadOnly, true));
- }
- public static IList Synchronized (IList list) {
- if (list == null)
- throw new ArgumentNullException ();
- ArrayList al = new ArrayList ();
- foreach (object o in list)
- al.Add (o);
- return (IList) ArrayList.Synchronized (al);
- }
- public static ArrayList FixedSize (ArrayList list) {
- if (list == null)
- throw new ArgumentNullException ();
- if (list.IsSynchronized)
- return Synchronized(list);
-
- return new ArrayList (list.ToArray (), list.Count, list.Capacity, true, list.IsReadOnly, list.IsSynchronized);
- }
- public static IList FixedSize (IList list) {
- if (list == null)
- throw new ArgumentNullException ();
- if (list.IsSynchronized)
- return Synchronized(list);
- ArrayList al = new ArrayList ();
- foreach (object o in list)
- al.Add (o);
- return (IList) ArrayList.FixedSize (al);
- }
- public static ArrayList Repeat (object value, int count) {
- ArrayList al = new ArrayList (count);
- for (int i = 0; i < count; i++) {
- al.dataArray[i] = value;
- }
- al.count = count;
- return al;
- }
- [Serializable]
- private class ListWrapper : ArrayList {
- IList list;
- public ListWrapper (IList list) {
- if (null == list)
- throw new ArgumentNullException();
- this.list = list;
- count = ((ICollection) list).Count;
- }
- // ArrayList
- public override int Capacity {
- get { return list.Count; }
- set {
- // MS seems to do this
- if (value < list.Count) {
- throw new ArgumentOutOfRangeException
- ("ArrayList Capacity being set to less than Count");
- }
- // There is no idiom for Capacity for
- // IList, so we do nothing, as MS does.
- }
- }
- public override void AddRange (ICollection collection) {
- if (collection == null)
- throw new ArgumentNullException ("colllection");
-
- if (IsFixedSize || IsReadOnly)
- throw new NotSupportedException ();
- InsertRange (Count, collection);
-
- }
- public override int BinarySearch (object value) {
- return BinarySearch (value, null);
- }
- public override int BinarySearch (object value, IComparer comparer) {
- return BinarySearch (0, Count, value, comparer);
- }
- public override int BinarySearch (int index, int count, object value,
- IComparer comparer) {
- if ((index < 0) || (count < 0))
- throw new ArgumentOutOfRangeException ();
- if ((index > Count) || (index + count) > Count)
- throw new ArgumentException ();
-
- if (comparer == null)
- comparer = Comparer.Default;
-
- int low = index;
- int hi = index + count - 1;
- int mid;
- while (low <= hi) {
- mid = (low + hi) / 2;
- int r = comparer.Compare (value, list [mid]);
- if (r == 0)
- return mid;
- if (r < 0)
- hi = mid-1;
- else
- low = mid+1;
- }
- return ~low;
- }
-
- public override void CopyTo (Array array) {
- if (null == array)
- throw new ArgumentNullException("array");
- if (array.Rank > 1)
- throw new ArgumentException("array cannot be multidimensional");
- CopyTo (array, 0);
- }
- public override void CopyTo (int index, Array array,
- int arrayIndex, int count) {
- if (array == null)
- throw new ArgumentNullException ();
- if (index < 0 || arrayIndex < 0 || count < 0)
- throw new ArgumentOutOfRangeException ();
- if (array.Rank > 1 || index >= Count || Count > (array.Length - arrayIndex))
- throw new ArgumentException ();
-
- for (int i = index; i < index + count; i++)
- array.SetValue(list [i], arrayIndex++);
- }
- public override ArrayList GetRange (int index, int count) {
- if (index < 0 || count < 0)
- throw new ArgumentOutOfRangeException ();
- if (Count < (index + count))
- throw new ArgumentException ();
-
- return (ArrayList) new Range (this, index, count);
- }
- public override void InsertRange (int index, ICollection col) {
- if (col == null)
- throw new ArgumentNullException ();
- if (index < 0 || index > Count)
- throw new ArgumentOutOfRangeException ();
- if (IsReadOnly || IsFixedSize)
- throw new NotSupportedException ();
- if (index == Count) {
- foreach (object element in col)
- list.Insert (index++, element);
- }
- }
- public override int LastIndexOf (object value) {
- return LastIndexOf (value, Count, 0);
- }
- public override int LastIndexOf (object value, int startIndex) {
- return LastIndexOf (value, startIndex, 0);
- }
- public override int LastIndexOf (object value, int startIndex, int count) {
- if (null == value){
- return -1;
- }
- if (startIndex > Count || count < 0 || (startIndex + count > Count))
- throw new ArgumentOutOfRangeException ();
-
- int length = startIndex - count + 1;
- for (int i = startIndex; i >= length; i--)
- if (list [i] == value)
- return i;
- return -1;
- }
- public override void RemoveRange (int index, int count) {
- if ((index < 0) || (count < 0))
- throw new ArgumentOutOfRangeException ();
- if ((index > Count) || (index + count) > Count)
- throw new ArgumentException ();
- if (IsReadOnly || IsFixedSize)
- throw new NotSupportedException ();
- for (int i = 0; i < count; i++)
- list.RemoveAt (index);
- }
- public override void Reverse () {
- Reverse (0, Count);
- }
- public override void Reverse (int index, int count) {
- if ((index < 0) || (count < 0))
- throw new ArgumentOutOfRangeException ();
- if ((index > Count) || (index + count) > Count)
- throw new ArgumentException ();
- if (IsReadOnly)
- throw new NotSupportedException ();
- object tmp = null;
- for (int i = index; i < count; i++) {
- tmp = list [i];
- list [i] = list [count - i];
- list [count - i] = tmp;
- }
- }
- public override void SetRange (int index, ICollection col) {
- if (index < 0 || (index + col.Count) > Count)
- throw new ArgumentOutOfRangeException ();
- if (col == null)
- throw new ArgumentNullException ();
- if (IsReadOnly)
- throw new NotSupportedException ();
- for (int i = index; i < col.Count; i++)
- foreach (object o in col)
- list [i] = o;
- }
- // Other overloads just call this
- public override void Sort (int index, int count, IComparer comparer) {
- if ((index < 0) || (count < 0))
- throw new ArgumentOutOfRangeException ();
- if ((index > Count) || (index + count) > Count)
- throw new ArgumentException ();
- if (IsReadOnly)
- throw new NotSupportedException ();
-
- // TODO: do some real sorting
- object [] tmpArr = new Object [count];
- CopyTo (index, tmpArr, 0, count);
- Array.Sort (tmpArr, 0, count, comparer);
- for (int i = 0; i < count; i++)
- list [i + index] = tmpArr [i];
- }
- public override object [] ToArray () {
- return (object []) ToArray (typeof (object));
- }
- public override Array ToArray (Type type) {
- int count = Count;
- Array result = Array.CreateInstance (type, count);
- for (int i = 0; i < count; i++)
- result.SetValue (list [i], i);
- return result;
- }
- public override void TrimToSize () {
- // Microsoft does not implement this method
- }
- // IList
- 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; }
- }
- public override int Add (object value) {
- return list.Add (value);
- }
- public override void Clear () {
- list.Clear ();
- }
- public override bool Contains (object value) {
- return list.Contains (value);
- }
- 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);
- }
- // ICollection
- public override int Count {
- get { return count; }
- }
- public override bool IsSynchronized {
- get { return ((ICollection) list).IsSynchronized; }
- }
- public override object SyncRoot {
- get { return ((ICollection) list).SyncRoot; }
- }
- public override void CopyTo (Array array, int index) {
- ((ICollection) list).CopyTo (array, index);
- }
- // ICloneable
- public override object Clone () {
- return new ListWrapper (list);
- }
- // IEnumerable
- public override IEnumerator GetEnumerator () {
- return ((IEnumerable) list).GetEnumerator ();
- }
- }
-
- public static ArrayList Adapter (IList list) {
- return new ListWrapper (list);
- }
- // properties
- private bool fixedSize = false;
- private bool readOnly = false;
- private bool synchronized = false;
- private long version = 0;
- private ArrayList source = null;
- private const int defaultCapacity = 16;
- private void copyDataArray (object[] outArray) {
- for (int i = 0; i < count; i++) {
- outArray[i] = dataArray[i];
- }
- }
- private void setSize (int newSize) {
- if (newSize == capacity)
- return;
-
- capacity = (newSize == 0) ? defaultCapacity : newSize;
- // note that this assumes that we've already sanity-checked
- // the new size
- object[] newDataArray = new object[capacity];
- copyDataArray (newDataArray);
- dataArray = newDataArray;
- }
- // note that this DOES NOT update count
- private void shiftElements (int startIndex, int numshift) {
- if (numshift == 0) {
- return;
- }
- if (count + numshift > capacity) {
- setSize (capacity * 2);
- shiftElements (startIndex, numshift);
- } else {
- if (numshift > 0) {
- int numelts = count - startIndex;
- for (int i = numelts-1; i >= 0; i--) {
- dataArray[startIndex + numshift + i] = dataArray[startIndex + i];
- }
- for (int i = startIndex; i < startIndex + numshift; i++) {
- dataArray[i] = null;
- }
- } else {
- int numelts = count - startIndex + numshift;
- for (int i = 0; i < numelts; i++) {
- dataArray [i + startIndex] = dataArray [i + startIndex - numshift];
- }
- for (int i = count + numshift; i < count; i++) {
- dataArray[i] = null;
- }
- }
- }
- }
- public virtual int Capacity {
- get {
- return capacity;
- }
- set {
- if (readOnly) {
- throw new NotSupportedException
- ("Collection is read-only.");
- }
- if (value < count) {
- throw new ArgumentOutOfRangeException
- ("ArrayList Capacity being set to less than Count");
- }
- if (fixedSize && value != capacity) {
- throw new NotSupportedException
- ("Collection is fixed size.");
- }
- setSize (value);
- }
- }
- private void CheckSourceVersion() {
- if (null != this.source && this.version != this.source.version) {
- throw new InvalidOperationException();
- }
- }
- public virtual int Count {
- get {
- CheckSourceVersion();
- return count;
- }
- }
- public virtual bool IsFixedSize {
- get {
- return fixedSize;
- }
- }
- public virtual bool IsReadOnly {
- get {
- return readOnly;
- }
- }
- public virtual bool IsSynchronized {
- get {
- return synchronized;
- }
- }
- public virtual object this[int index] {
- get {
- CheckSourceVersion();
- if (index < 0) {
- throw new ArgumentOutOfRangeException ("index < 0");
- }
- if (index >= count) {
- throw new ArgumentOutOfRangeException ("index out of range");
- }
- return dataArray[index];
- }
- set {
- if (index < 0) {
- throw new ArgumentOutOfRangeException ("index < 0");
- }
- if (index >= count) {
- throw new ArgumentOutOfRangeException ("index out of range");
- }
- if (readOnly) {
- throw new NotSupportedException ("Collection is read-only.");
- }
- dataArray[index] = value;
- version++;
- }
- }
- public virtual object SyncRoot {
- get {
- return this;
- }
- }
- // methods
- public virtual int Add (object value) {
- if (readOnly)
- throw new NotSupportedException ("ArrayList is read-only.");
- if (fixedSize)
- throw new NotSupportedException ("ArrayList is fixed size.");
- if (count + 1 >= capacity)
- setSize (capacity * 2);
- dataArray[count] = value;
- version++;
- return count++;
- }
- public virtual void AddRange (ICollection c) {
- if (null == c)
- throw new ArgumentNullException ("c");
- if (readOnly || fixedSize)
- throw new NotSupportedException ();
- int cc = c.Count;
- if (count + cc >= capacity)
- Capacity = cc < count? count * 2: count + cc + 1;
- c.CopyTo (dataArray, count);
- count += cc;
- version++;
- }
- public virtual int BinarySearch (object value) {
- return BinarySearch (0, count, value, null);
- }
- public virtual int BinarySearch (object value, IComparer comparer) {
- return BinarySearch (0, count, value, comparer);
- }
- public virtual int BinarySearch (int index, int count,
- object value, IComparer comparer) {
- return Array.BinarySearch (dataArray, index, count, value, comparer);
- }
- public virtual void Clear () {
- if (readOnly || fixedSize)
- throw new NotSupportedException();
- count = 0;
- version++;
- }
- public virtual object Clone () {
- return new ArrayList (dataArray, count, capacity,
- fixedSize, readOnly, synchronized);
- }
- public virtual bool Contains (object item) {
- for (int i = 0; i < count; i++) {
- if (Object.Equals (dataArray[i], item)) {
- return true;
- }
- }
- return false;
- }
- public virtual void CopyTo (Array array) {
- if (null == array)
- throw new ArgumentNullException("array");
- if (array.Rank > 1)
- throw new ArgumentException("array cannot be multidimensional");
- Array.Copy (dataArray, 0, array, 0, this.count);
- }
- public virtual void CopyTo (Array array, int arrayIndex) {
- if (null == array)
- throw new ArgumentNullException("array");
- if (arrayIndex < 0)
- throw new ArgumentOutOfRangeException("arrayIndex");
- if (array.Rank > 1)
- throw new ArgumentException("array cannot be multidimensional");
- if (this.count > array.Length - arrayIndex)
- throw new ArgumentException("this ArrayList has more items than the space available in array from arrayIndex to the end of array");
-
- Array.Copy (dataArray, 0, array, arrayIndex, this.count);
- }
- public virtual void CopyTo (int index, Array array,
- int arrayIndex, int count) {
- if (null == array)
- throw new ArgumentNullException("array");
- if (arrayIndex < 0)
- throw new ArgumentOutOfRangeException("arrayIndex");
- if (index < 0)
- throw new ArgumentOutOfRangeException("index");
- if (count < 0)
- throw new ArgumentOutOfRangeException("count");
- if (index >= this.count)
- throw new ArgumentException("index is greater than or equal to the source ArrayList.Count");
- if (array.Rank > 1)
- throw new ArgumentException("array cannot be multidimensional");
- if (arrayIndex >= array.Length)
- throw new ArgumentException("arrayIndex is greater than or equal to array's length");
- if (this.count > array.Length - arrayIndex)
- throw new ArgumentException("this ArrayList has more items than the space available in array from arrayIndex to the end of array");
- Array.Copy (dataArray, index, array, arrayIndex, count);
- }
- [Serializable]
- private class ArrayListEnumerator : IEnumerator, ICloneable {
- private object[] data;
- private int idx;
- private int start;
- private int num;
- private ArrayList enumeratee;
- private long version;
- internal ArrayListEnumerator(int index, int count, object[] items, ArrayList al, long ver) {
- data = items;
- start = index;
- num = count;
- idx = start - 1;
- enumeratee = al;
- version = ver;
- }
- public object Clone () {
- return new ArrayListEnumerator (start, num, data, enumeratee, version);
- }
- public virtual object Current {
- get {
- return data [idx];
- }
- }
- public virtual bool MoveNext() {
- if (enumeratee.version != version)
- throw new InvalidOperationException();
- if (++idx < start + num)
- return true;
- return false;
- }
- public virtual void Reset() {
- idx = start - 1;
- }
- }
- public virtual IEnumerator GetEnumerator () {
- return new ArrayListEnumerator(0, this.Count, dataArray, this, this.version);
- }
- private void ValidateRange(int index, int count) {
- if (index < 0) {
- throw new ArgumentOutOfRangeException("index", index, "Must be equal to or greater than zero");
- }
- if (count < 0) {
- throw new ArgumentOutOfRangeException("count", count, "Must be equal to or greater than zero");
- }
- if (index > this.count - 1) {
- throw new ArgumentException();
- }
- if (index + count > this.count) {
- throw new ArgumentException();
- }
- }
- public virtual IEnumerator GetEnumerator (int index, int count) {
- ValidateRange(index, count);
- return new ArrayListEnumerator(index, count, dataArray, this, this.version);
- }
- public virtual ArrayList GetRange (int index, int count) {
- ValidateRange(index, count);
- ArrayList retVal = new ArrayList(count);
- for (int i = index; i < count + index; i++) {
- retVal.Add(this[i]);
- }
- retVal.version = this.version;
- retVal.source = this;
- return retVal;
- }
- public virtual int IndexOf (object value) {
- return IndexOf (value, 0, count);
- }
- public virtual int IndexOf (object value, int startIndex) {
- return IndexOf (value, startIndex, count - startIndex);
- }
- public virtual int IndexOf (object value, int startIndex, int count) {
- if (startIndex < 0 || startIndex + count > this.count || count < 0) {
- throw new ArgumentOutOfRangeException ("IndexOf arguments out of range");
- }
- for (int i = startIndex; i < (startIndex + count); i++) {
- if (Object.Equals (dataArray[i], value)) {
- return i;
- }
- }
- return -1;
- }
- public virtual void Insert (int index, object value) {
- if (readOnly) {
- throw new NotSupportedException
- ("Collection is read-only.");
- }
- if (fixedSize) {
- throw new NotSupportedException
- ("Collection is fixed size.");
- }
- if (index < 0 || index > count) {
- throw new ArgumentOutOfRangeException ("index < 0 or index >= capacity");
- }
- shiftElements (index, 1);
- dataArray[index] = value;
- count++;
- version++;
- }
- public virtual void InsertRange (int index, ICollection c) {
- if (c == null)
- throw new ArgumentNullException ();
- if (index < 0 || index > count)
- throw new ArgumentOutOfRangeException ();
- if (IsReadOnly || IsFixedSize)
- throw new NotSupportedException ();
- // Get a copy of the collection before the shift in case the collection
- // is this. Otherwise the enumerator will be confused.
- Array source = Array.CreateInstance(typeof(object), c.Count);
- c.CopyTo(source, 0);
- shiftElements (index, c.Count);
- count += c.Count;
- foreach (object o in source)
- dataArray[index++] = o;
- version++;
- }
- public virtual int LastIndexOf (object value) {
- return LastIndexOf (value, count - 1, count);
- }
- public virtual int LastIndexOf (object value, int startIndex) {
- if (startIndex < 0 || startIndex > count - 1) {
- throw new ArgumentOutOfRangeException("startIndex", startIndex, "");
- }
- return LastIndexOf (value, startIndex, startIndex + 1);
- }
- public virtual int LastIndexOf (object value, int startIndex,
- int count) {
- if (null == value){
- return -1;
- }
- if (startIndex >= this.count)
- throw new ArgumentOutOfRangeException ("startIndex >= Count");
- if (count < 0)
- throw new ArgumentOutOfRangeException ("count < 0");
- if (startIndex + 1 < count)
- throw new ArgumentOutOfRangeException ("startIndex + 1 < count");
-
- int EndIndex = startIndex - count + 1;
- for (int i = startIndex; i >= EndIndex; i--) {
- if (Object.Equals (dataArray[i], value)) {
- return i;
- }
- }
- return -1;
- }
- public virtual void Remove (object obj) {
- if (IsFixedSize || IsReadOnly)
- throw new NotSupportedException ();
-
- int objIndex = IndexOf (obj);
- if (objIndex == -1) {
- // shouldn't an exception be thrown here??
- // the MS docs don't indicate one, and testing
- // with the MS .net framework doesn't indicate one
- return;
- }
- RemoveRange (objIndex, 1);
- }
- public virtual void RemoveAt (int index) {
- if (readOnly) {
- throw new NotSupportedException
- ("Collection is read-only.");
- }
- if (fixedSize) {
- throw new NotSupportedException
- ("Collection is fixed size.");
- }
- if ((index < 0) || (index >= this.count))
- throw new ArgumentOutOfRangeException ("index", "Index was out of range. Must be non-negative and less than the size of the collection.");
- shiftElements (index, -1);
- this.count -= 1;
- version ++;
- }
- public virtual void RemoveRange (int index, int count) {
- if (readOnly) {
- throw new NotSupportedException
- ("Collection is read-only.");
- }
- if (fixedSize) {
- throw new NotSupportedException
- ("Collection is fixed size.");
- }
- if (index < 0)
- throw new ArgumentOutOfRangeException ("index", "Non-negative number required.");
- if (count < 0)
- throw new ArgumentOutOfRangeException ("count", "Non-negative number required.");
- if (index + count > this.count)
- throw new ArgumentException ("Offset and length were out of bounds for the array " +
- "or count is greater than the number of elements from index " +
- "to the end of the source collection.");
-
- shiftElements (index, - count);
- this.count -= count;
- version++;
- }
- public virtual void Reverse () {
- Reverse (0, count);
- }
- public virtual void Reverse (int index, int count) {
- if (readOnly) {
- throw new NotSupportedException
- ("Collection is read-only.");
- }
- if (index < 0 || index + count > this.count) {
- throw new ArgumentOutOfRangeException
- ("index/count out of range");
- }
- Array.Reverse (dataArray, index, count);
- version++;
- }
- public virtual void SetRange (int index, ICollection c) {
- if (c == null)
- throw new ArgumentNullException ();
- if (readOnly)
- throw new NotSupportedException ();
- if (index < 0 || (index + c.Count) > count)
- throw new ArgumentOutOfRangeException ();
- c.CopyTo(dataArray, index);
- }
- public virtual void Sort () {
- Sort (0, count, null);
- }
- public virtual void Sort (IComparer comparer) {
- Sort (0, count, comparer);
- }
- public virtual void Sort (int index, int count, IComparer comparer) {
- if (readOnly) {
- throw new NotSupportedException
- ("Collection is read-only.");
- }
- if (index < 0 || index + count > this.count) {
- throw new ArgumentOutOfRangeException
- ("index/count out of range");
- }
-
- Array.Sort (dataArray, index, count, comparer);
- version++;
- }
- public virtual object[] ToArray() {
- object[] outArray = new object[count];
- Array.Copy (dataArray, outArray, count);
- return outArray;
- }
- public virtual Array ToArray (Type type) {
- Array outArray = Array.CreateInstance (type, count);
- Array.Copy (dataArray, outArray, count);
- return outArray;
- }
- public virtual void TrimToSize () {
- if (IsReadOnly || IsFixedSize)
- throw new NotSupportedException ();
-
- setSize(count);
- version++;
- }
-
- private class Range : ArrayList {
- ArrayList baseList;
- int baseIndex;
- int baseSize;
- long baseVersion;
-
- internal Range (ArrayList list, int index, int count)
- {
- baseList = list;
- baseIndex = index;
- baseSize = count;
- baseVersion = list.version;
- }
-
- int RealIndex (int index)
- {
- return index + baseIndex;
- }
-
- int RealEnd {
- get {return baseIndex + baseSize;}
- }
-
-
- private void CheckVersion ()
- {
- if (baseVersion != this.baseList.version)
- throw new InvalidOperationException ();
- }
-
- public override int Add (object value)
- {
- CheckVersion ();
- baseList.Insert (RealEnd, value);
- baseVersion++;
- return baseSize++;
- }
-
- public override void AddRange (ICollection c)
- {
- CheckVersion ();
- baseList.InsertRange (RealEnd, c);
- baseVersion++;
- baseSize += c.Count;
- }
-
- public override int BinarySearch (int index, int count, object value, IComparer comparer)
- {
- CheckVersion ();
-
- if ((index < 0) || (count < 0))
- throw new ArgumentOutOfRangeException ();
- if ((index > Count) || (index + count) > Count)
- throw new ArgumentException ();
-
- int i = baseList.BinarySearch (RealIndex (index), count, value, comparer);
-
- // Account for how a BinarySearch works
- if (i >= 0) return i - baseIndex;
- return i + baseIndex;
- }
-
- public override int Capacity {
- get {return baseList.Capacity;}
-
- set {
- if (value < Count)
- throw new ArgumentOutOfRangeException("value");
- // From profiling it looks like Microsoft does not do anything
- // This make sense, since we can't very well have a "capacity" for the range.
- }
- }
-
- public override void Clear ()
- {
- CheckVersion ();
- if (baseSize != 0) {
- baseList.RemoveRange (baseIndex, baseSize);
- baseVersion++;
- baseSize = 0;
- }
- }
- public override object Clone ()
- {
- CheckVersion ();
-
- // Debugging Microsoft shows that this is _exactly_ how they do it.
- Range arrayList = new Range (baseList, baseIndex, baseSize);
- arrayList.baseList = (ArrayList)baseList.Clone ();
-
- return arrayList;
- }
- public override bool Contains (object item)
- {
- CheckVersion ();
-
- // Is much faster to check for null than to call Equals.
- if (item == null) {
- for (int i = 0; i < baseSize; i++)
- if (baseList[baseIndex + i] == null)
- return true;
- return false;
- } else {
- for (int i = 0; i < baseSize; i++)
- if (item.Equals (baseList [baseIndex + i]))
- return true;
- return false;
- }
- }
-
-
- public override void CopyTo (Array array, int index)
- {
- CheckVersion ();
- if (null == array)
- throw new ArgumentNullException ("array");
- if (array.Rank > 1)
- throw new ArgumentException ("array cannot be multidimensional");
- if (index < 0)
- throw new ArgumentOutOfRangeException ("index");
- if (array.Length - index < baseSize)
- throw new ArgumentException ();
-
- Array.Copy (baseList.dataArray, baseIndex, array, index, baseSize);
- }
- public override void CopyTo (int index, Array array, int arrayIndex, int count)
- {
- CheckVersion ();
-
- if (null == array)
- throw new ArgumentNullException ("array");
- if (array.Rank > 1)
- throw new ArgumentException ("array cannot be multidimensional");
- if (index < 0)
- throw new ArgumentOutOfRangeException ("index");
- if (count < 0)
- throw new ArgumentOutOfRangeException ("count");
- if (array.Length - index < baseSize)
- throw new ArgumentException ();
- if (baseSize - index < count)
- throw new ArgumentException ();
-
- Array.Copy (baseList.dataArray, RealIndex (index), array, arrayIndex, count);
- }
- public override int Count {
- get {
- CheckVersion ();
- return baseSize;
- }
- }
- public override bool IsReadOnly {
- get { return baseList.IsReadOnly; }
- }
- public override bool IsFixedSize {
- get { return baseList.IsFixedSize; }
- }
- public override bool IsSynchronized {
- get { return baseList.IsSynchronized; }
- }
-
- public override IEnumerator GetEnumerator ()
- {
- return GetEnumerator (0, baseSize);
- }
- public override IEnumerator GetEnumerator (int index, int count)
- {
- CheckVersion ();
- if (index < 0)
- throw new ArgumentOutOfRangeException ("index");
- if (count < 0)
- throw new ArgumentOutOfRangeException ("count");
- if (baseSize - index < count)
- throw new ArgumentException ();
-
- return baseList.GetEnumerator (RealIndex (index), count);
- }
- public override ArrayList GetRange (int index, int count)
- {
- CheckVersion ();
-
- if (index < 0)
- throw new ArgumentOutOfRangeException ("index");
- if (count < 0)
- throw new ArgumentOutOfRangeException ("count");
- if (baseSize - index < count)
- throw new ArgumentException ();
-
- // We have to create a wrapper around a wrapper
- // because if we update the inner most wrapper, the
- // outer ones must still function. If we just wrapped
- // the outer most ArrayList, the others would not update
- // their version.
- return new Range (this, index, count);
- }
- public override object SyncRoot {
- get {return baseList.SyncRoot;}
- }
-
- public override int IndexOf (object value)
- {
- CheckVersion ();
- int i = baseList.IndexOf (value, baseIndex, baseSize);
-
- if (i >= 0) return i - baseIndex;
- else return -1;
- }
- public override int IndexOf (object value, int startIndex)
- {
- CheckVersion ();
- if (startIndex < 0 || startIndex > baseSize)
- throw new ArgumentOutOfRangeException ();
- int i = baseList.IndexOf (value, RealIndex (startIndex), baseSize - startIndex);
- if (i >= 0) return i - baseIndex;
- return -1;
- }
-
- public override int IndexOf (object value, int startIndex, int count)
- {
- CheckVersion ();
- if (startIndex < 0 || startIndex > baseSize)
- throw new ArgumentOutOfRangeException("startIndex");
-
- if (count < 0 || (startIndex > baseSize - count))
- throw new ArgumentOutOfRangeException("count");
- int i = baseList.IndexOf (value, RealIndex (startIndex), count);
- if (i >= 0) return i - baseIndex;
- return -1;
- }
- public override void Insert (int index, object value)
- {
- CheckVersion ();
- if (index < 0 || index > baseSize)
- throw new ArgumentOutOfRangeException("index");
- baseList.Insert ( RealIndex (index), value);
- baseVersion++;
- baseSize++;
- }
- public override void InsertRange (int index, ICollection c)
- {
- CheckVersion ();
- if (index < 0 || index > baseSize)
- throw new ArgumentOutOfRangeException("index");
-
- baseList.InsertRange (RealIndex (index), c);
- baseVersion++;
- baseSize += c.Count;
- }
- public override int LastIndexOf (object value)
- {
- CheckVersion ();
- int i = baseList.LastIndexOf (value, baseIndex, baseSize);
- if (i >= 0) return i - baseIndex;
- return -1;
- }
- public override int LastIndexOf (object value, int startIndex)
- {
- return LastIndexOf (value, startIndex, startIndex + 1);
- }
- public override int LastIndexOf (object value, int startIndex, int count)
- {
- CheckVersion ();
- if (baseSize == 0)
- return -1;
-
- if (startIndex < 0 || startIndex >= baseSize)
- throw new ArgumentOutOfRangeException("startIndex");
-
- int i = baseList.LastIndexOf (value, RealIndex (startIndex), count);
- if (i >= 0) return i - baseIndex;
- return -1;
- }
- // Remove will just call the overrided methods in here
- public override void RemoveAt (int index)
- {
- CheckVersion ();
- if (index < 0 || index >= baseSize)
- throw new ArgumentOutOfRangeException ("index");
- baseList.RemoveAt (RealIndex (index));
- baseVersion++;
- baseSize--;
- }
- public override void RemoveRange (int index, int count)
- {
- CheckVersion ();
- if (index < 0)
- throw new ArgumentOutOfRangeException ("index");
- if (count < 0)
- throw new ArgumentOutOfRangeException ("count");
- if (baseSize - index < count)
- throw new ArgumentException ();
-
- baseList.RemoveRange (RealIndex (index), count);
- baseVersion++;
- baseSize -= count;
- }
- public override void Reverse (int index, int count)
- {
- CheckVersion ();
- if (index < 0)
- throw new ArgumentOutOfRangeException ("index");
- if (count < 0)
- throw new ArgumentOutOfRangeException ("count");
- if (baseSize - index < count)
- throw new ArgumentException ();
-
- baseList.Reverse (RealIndex (index), count);
- baseVersion++;
- }
- public override void SetRange (int index, ICollection c)
- {
- CheckVersion ();
- if (index < 0 || index >= baseSize)
- throw new ArgumentOutOfRangeException("index");
-
- baseList.SetRange (RealIndex (index), c);
- baseVersion++;
- }
-
- // Other overloads just call this
- public override void Sort (int index, int count, IComparer comparer)
- {
- CheckVersion ();
-
- if (index < 0)
- throw new ArgumentOutOfRangeException ("index");
- if (count < 0)
- throw new ArgumentOutOfRangeException ("count");
- if (baseSize - index < count)
- throw new ArgumentException ();
-
- baseList.Sort (RealIndex (index), count, comparer);
- baseVersion++;
- }
- public override object this [int index] {
- get {
- CheckVersion ();
- if (index < 0 || index >= baseSize)
- throw new ArgumentOutOfRangeException("index");
-
- return baseList[baseIndex + index];
- }
- set {
- CheckVersion ();
- if (index < 0 || index >= baseSize)
- throw new ArgumentOutOfRangeException("index");
-
- baseList [baseIndex + index] = value;
- baseVersion++;
- }
- }
- public override object [] ToArray ()
- {
- CheckVersion ();
- object [] array = new object [baseSize];
- Array.Copy (baseList.dataArray, baseIndex, array, 0, baseSize);
- return array;
- }
- public override Array ToArray (Type type)
- {
- CheckVersion ();
- if (type == null)
- throw new ArgumentNullException("type");
-
- Array array = Array.CreateInstance (type, baseSize);
- Array.Copy (baseList.dataArray, baseIndex, array, 0, baseSize);
- return array;
- }
- public override void TrimToSize ()
- {
- throw new NotSupportedException ("Can not trim range");
- }
- }
- private class SyncArrayList : ArrayList {
- private ArrayList _list;
- // constructors
- public SyncArrayList(ArrayList list) {
- _list = list;
- }
- // properties
- public override int Capacity {
- get {
- lock (_list.SyncRoot) {
- return _list.Capacity;
- }
- }
- set {
- lock (_list.SyncRoot) {
- _list.Capacity = value;
- }
- }
- }
- public override int Count {
- get {
- lock (_list.SyncRoot) {
- return _list.Count;
- }
- }
- }
- public override bool IsFixedSize {
- get {
- lock (_list.SyncRoot) {
- return _list.IsFixedSize;
- }
- }
- }
- public override bool IsReadOnly {
- get {
- lock (_list.SyncRoot) {
- return _list.IsReadOnly;
- }
- }
- }
- public override bool IsSynchronized {
- get {
- lock (_list.SyncRoot) {
- return _list.IsSynchronized;
- }
- }
- }
- public override object this[int index] {
- get {
- lock (_list.SyncRoot) {
- return _list[index];
- }
- }
- set {
- lock (_list.SyncRoot) {
- _list[index] = value;
- }
- }
- }
- // methods
- public override int Add (object value) {
- lock (_list.SyncRoot) {
- return _list.Add(value);
- }
- }
- public override void AddRange (ICollection c) {
- lock (_list.SyncRoot) {
- _list.AddRange(c);
- }
- }
- public override int BinarySearch (int index, int count, object value, IComparer comparer) {
- lock (_list.SyncRoot) {
- return Array.BinarySearch (dataArray, index, count, value, comparer);
- }
- }
- public override void Clear () {
- lock (_list.SyncRoot) {
- _list.Clear();
- }
- }
- public override object Clone () {
- lock (_list.SyncRoot) {
- return new SyncArrayList((ArrayList) _list.Clone());
- }
- }
- public override bool Contains (object item) {
- lock (_list.SyncRoot) {
- return _list.Contains(item);
- }
- }
- public override void CopyTo (Array array) {
- lock (_list.SyncRoot) {
- _list.CopyTo(array);
- }
- }
- public override void CopyTo (Array array, int arrayIndex) {
- lock (_list.SyncRoot) {
- _list.CopyTo(array, arrayIndex);
- }
- }
- public override void CopyTo (int index, Array array, int arrayIndex, int count) {
- lock (_list.SyncRoot) {
- _list.CopyTo(index, array, arrayIndex, count);
- }
- }
- public override IEnumerator GetEnumerator () {
- lock (_list.SyncRoot) {
- return _list.GetEnumerator();
- }
- }
- public override IEnumerator GetEnumerator (int index, int count) {
- lock (_list.SyncRoot) {
- return _list.GetEnumerator(index, count);
- }
- }
- public override ArrayList GetRange (int index, int count) {
- lock (_list.SyncRoot) {
- return new SyncArrayList(_list.GetRange(index, count));
- }
- }
- public override int IndexOf (object value, int startIndex, int count) {
- lock (_list.SyncRoot) {
- return _list.IndexOf(value, startIndex, count);
- }
- }
- public override void Insert (int index, object value) {
- lock (_list.SyncRoot) {
- _list.Insert(index, value);
- }
- }
- public override void InsertRange (int index, ICollection c) {
- lock (_list.SyncRoot) {
- _list.InsertRange(index, c);
- }
- }
- public override int LastIndexOf (object value, int startIndex) {
- lock (_list.SyncRoot) {
- return _list.LastIndexOf(value, startIndex);
- }
- }
- public override int LastIndexOf (object value, int startIndex, int count) {
- lock (_list.SyncRoot) {
- return _list.LastIndexOf(value, startIndex, count);
- }
- }
- public override void Remove (object obj) {
- lock (_list.SyncRoot) {
- _list.Remove(obj);
- }
- }
- public override void RemoveRange (int index, int count) {
- lock (_list.SyncRoot) {
- _list.RemoveRange(index, count);
- }
- }
- public override void Reverse (int index, int count) {
- lock (_list.SyncRoot) {
- _list.Reverse(index, count);
- }
- }
- public override void SetRange (int index, ICollection c) {
- lock (_list.SyncRoot) {
- _list.SetRange(index, c);
- }
- }
- public override void Sort (int index, int count, IComparer comparer) {
- lock (_list.SyncRoot) {
- _list.Sort(index, count, comparer);
- }
- }
- public override object[] ToArray() {
- lock (_list.SyncRoot) {
- return _list.ToArray();
- }
- }
- public override Array ToArray (Type type) {
- lock (_list.SyncRoot) {
- return _list.ToArray(type);
- }
- }
- public override void TrimToSize () {
- lock (_list.SyncRoot) {
- _list.TrimToSize();
- }
- }
- }
- }
- }
|