| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462 |
- // -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
- //
- // System.Collections.ArrayList
- //
- // Author:
- // Vladimir Vukicevic ([email protected])
- //
- // (C) 2001 Vladimir Vukicevic
- //
- using System;
- namespace System.Collections {
- public class ArrayList : IList, ICollection, IEnumerable, ICloneable {
- // constructors
- public ArrayList () {
- dataArray = new object[capacity];
- }
- public ArrayList (ICollection c) {
- }
- public ArrayList (int capacity) {
- dataArray = new object[capacity];
- this.capacity = capacity;
- }
- private ArrayList (object[] dataArray, int count, int capacity,
- bool fixedSize, bool readOnly, bool synchronized)
- {
- this.dataArray = dataArray;
- this.count = count;
- this.capacity = capacity;
- this.fixedSize = fixedSize;
- this.readOnly = readOnly;
- this.synchronized = synchronized;
- }
- public static ArrayList ReadOnly (ArrayList list) {
- throw new NotImplementedException ("System.Collections.ArrayList.ReadOnly");
- }
- public static ArrayList ReadOnly (IList list) {
- throw new NotImplementedException ("System.Collections.ArrayList.ReadOnly");
- }
- public static ArrayList Synchronized (ArrayList list) {
- throw new NotImplementedException ("System.Collections.ArrayList.Synchronized");
- }
- public static ArrayList Synchronized (IList list) {
- throw new NotImplementedException ("System.Collections.ArrayList.Synchronized");
- }
- public static ArrayList FixedSize (ArrayList list) {
- throw new NotImplementedException ("System.Collections.ArrayList.FixedSize");
- }
- public static ArrayList FixedSize (IList list) {
- throw new NotImplementedException ("System.Collections.ArrayList.FixedSize");
- }
- 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;
- }
- public static ArrayList Adapter (IList list) {
- throw new NotImplementedException ("System.Collections.ArrayList.Adapter");
- }
- // properties
- private bool fixedSize = false;
- private bool readOnly = false;
- private bool synchronized = false;
- private int count = 0;
- private int capacity = 16;
- private object[] dataArray;
- 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;
- }
- // note that this assumes that we've already sanity-checked
- // the new size
- object[] newDataArray = new object[newSize];
- copyDataArray (newDataArray);
- dataArray = newDataArray;
- capacity = newSize;
- }
- // 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 = startIndex; i < numelts; i++) {
- dataArray[i] = dataArray[i - 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);
- }
- }
- public virtual int Count {
- get {
- 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 {
- 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");
- }
- // FIXME -- should setting an index implicitly extend the array?
- // the docs aren't clear -- I'm assuming not, since the exception
- // is listed for both get and set
- if (index >= count) {
- throw new ArgumentOutOfRangeException ("index out of range");
- }
- if (readOnly) {
- throw new NotSupportedException ("Collection is read-only.");
- }
- dataArray[index] = value;
- }
- }
- public virtual object SyncRoot {
- get {
- throw new NotImplementedException ("System.Collections.ArrayList.SyncRoot.get");
- }
- }
- // methods
- public virtual int Add (object value) {
- if (readOnly) {
- throw new NotSupportedException ("Collection is read-only.");
- }
- if (count + 1 >= capacity) {
- setSize (capacity * 2);
- }
- dataArray[count++] = value;
- return count-1;
- }
- public virtual void AddRange (ICollection c) {
- throw new NotImplementedException ("System.Collections.ArrayList.AddRange");
- }
- public virtual int BinarySearch (object value) {
- throw new NotImplementedException ("System.Collections.ArrayList.BinarySearch");
- }
- public virtual int BinarySearch (object value, IComparer comparer) {
- throw new NotImplementedException ("System.Collections.ArrayList.BinarySearch");
- }
- public virtual int BinarySearch (int index, int count,
- object value, IComparer comparer) {
- throw new NotImplementedException ("System.Collections.ArrayList.BinarySearch");
- }
- public virtual void Clear () {
- count = 0;
- setSize(capacity);
- }
- 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) {
- }
- public virtual void CopyTo (Array array, int arrayIndex) {
- }
- public virtual void CopyTo (int index, Array array,
- int arrayIndex, int count) {
- }
- public virtual IEnumerator GetEnumerator () {
- return null;
- }
- public virtual IEnumerator GetEnumerator (int index, int count) {
- return null;
- }
- public virtual ArrayList GetRange (int index, int count) {
- return null;
- }
- 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 >= count");
- }
- shiftElements (index, 1);
- dataArray[index] = value;
- count++;
- }
- public virtual void InsertRange (int index, ICollection c) {
- }
- public virtual int LastIndexOf (object value) {
- return LastIndexOf (value, 0, count);
- }
- public virtual int LastIndexOf (object value, int startIndex) {
- return LastIndexOf (value, startIndex, count - startIndex);
- }
- public virtual int LastIndexOf (object value, int StartIndex,
- int count)
- {
- for (int i = count - 1; i >= 0; i--) {
- if (Object.Equals (dataArray[i], value)) {
- return i;
- }
- }
- return -1;
- }
- public virtual void Remove (object obj) {
- 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) {
- RemoveRange (index, 1);
- }
- 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 || index >= this.count || index + count > this.count) {
- throw new ArgumentOutOfRangeException
- ("index/count out of range");
- }
- shiftElements (index, - count);
- this.count -= count;
- }
- 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);
- }
- public virtual void SetRange (int index, ICollection c) {
- }
- 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);
- }
- 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 () {
- // FIXME: implement this
- }
- }
- }
|