ArrayList.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. // -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
  2. //
  3. // System.Collections.ArrayList
  4. //
  5. // Author:
  6. // Vladimir Vukicevic ([email protected])
  7. //
  8. // (C) 2001 Vladimir Vukicevic
  9. //
  10. using System;
  11. namespace System.Collections {
  12. public class ArrayList : IList, ICollection, IEnumerable, ICloneable {
  13. // constructors
  14. public ArrayList () {
  15. dataArray = new object[capacity];
  16. }
  17. public ArrayList (ICollection c) {
  18. }
  19. public ArrayList (int capacity) {
  20. dataArray = new object[capacity];
  21. this.capacity = capacity;
  22. }
  23. private ArrayList (object[] dataArray, int count, int capacity,
  24. bool fixedSize, bool readOnly, bool synchronized)
  25. {
  26. this.dataArray = dataArray;
  27. this.count = count;
  28. this.capacity = capacity;
  29. this.fixedSize = fixedSize;
  30. this.readOnly = readOnly;
  31. this.synchronized = synchronized;
  32. }
  33. public static ArrayList ReadOnly (ArrayList list) {
  34. throw new NotImplementedException ("System.Collections.ArrayList.ReadOnly");
  35. }
  36. public static ArrayList ReadOnly (IList list) {
  37. throw new NotImplementedException ("System.Collections.ArrayList.ReadOnly");
  38. }
  39. public static ArrayList Synchronized (ArrayList list) {
  40. throw new NotImplementedException ("System.Collections.ArrayList.Synchronized");
  41. }
  42. public static ArrayList Synchronized (IList list) {
  43. throw new NotImplementedException ("System.Collections.ArrayList.Synchronized");
  44. }
  45. public static ArrayList FixedSize (ArrayList list) {
  46. throw new NotImplementedException ("System.Collections.ArrayList.FixedSize");
  47. }
  48. public static ArrayList FixedSize (IList list) {
  49. throw new NotImplementedException ("System.Collections.ArrayList.FixedSize");
  50. }
  51. public static ArrayList Repeat (object value, int count) {
  52. ArrayList al = new ArrayList (count);
  53. for (int i = 0; i < count; i++) {
  54. al.dataArray[i] = value;
  55. }
  56. al.count = count;
  57. return al;
  58. }
  59. public static ArrayList Adapter (IList list) {
  60. throw new NotImplementedException ("System.Collections.ArrayList.Adapter");
  61. }
  62. // properties
  63. private bool fixedSize = false;
  64. private bool readOnly = false;
  65. private bool synchronized = false;
  66. private int count = 0;
  67. private int capacity = 16;
  68. private object[] dataArray;
  69. private void copyDataArray (object[] outArray) {
  70. for (int i = 0; i < count; i++) {
  71. outArray[i] = dataArray[i];
  72. }
  73. }
  74. private void setSize (int newSize) {
  75. if (newSize == capacity) {
  76. return;
  77. }
  78. // note that this assumes that we've already sanity-checked
  79. // the new size
  80. object[] newDataArray = new object[newSize];
  81. copyDataArray (newDataArray);
  82. dataArray = newDataArray;
  83. capacity = newSize;
  84. }
  85. // note that this DOES NOT update count
  86. private void shiftElements (int startIndex, int numshift) {
  87. if (numshift == 0) {
  88. return;
  89. }
  90. if (count + numshift > capacity) {
  91. setSize (capacity * 2);
  92. shiftElements (startIndex, numshift);
  93. } else {
  94. if (numshift > 0) {
  95. int numelts = count - startIndex;
  96. for (int i = numelts-1; i >= 0; i--) {
  97. dataArray[startIndex + numshift + i] = dataArray[startIndex + i];
  98. }
  99. for (int i = startIndex; i < startIndex + numshift; i++) {
  100. dataArray[i] = null;
  101. }
  102. } else {
  103. int numelts = count - startIndex + numshift;
  104. for (int i = startIndex; i < numelts; i++) {
  105. dataArray[i] = dataArray[i - numshift];
  106. }
  107. for (int i = count + numshift; i < count; i++) {
  108. dataArray[i] = null;
  109. }
  110. }
  111. }
  112. }
  113. public virtual int Capacity {
  114. get {
  115. return capacity;
  116. }
  117. set {
  118. if (readOnly) {
  119. throw new NotSupportedException
  120. ("Collection is read-only.");
  121. }
  122. if (value < count) {
  123. throw new ArgumentOutOfRangeException
  124. ("ArrayList Capacity being set to less than Count");
  125. }
  126. if (fixedSize && value != capacity) {
  127. throw new NotSupportedException
  128. ("Collection is fixed size.");
  129. }
  130. setSize (value);
  131. }
  132. }
  133. public virtual int Count {
  134. get {
  135. return count;
  136. }
  137. }
  138. public virtual bool IsFixedSize {
  139. get {
  140. return fixedSize;
  141. }
  142. }
  143. public virtual bool IsReadOnly {
  144. get {
  145. return readOnly;
  146. }
  147. }
  148. public virtual bool IsSynchronized {
  149. get {
  150. return synchronized;
  151. }
  152. }
  153. public virtual object this[int index] {
  154. get {
  155. if (index < 0) {
  156. throw new ArgumentOutOfRangeException ("index < 0");
  157. }
  158. if (index >= count) {
  159. throw new ArgumentOutOfRangeException ("index out of range");
  160. }
  161. return dataArray[index];
  162. }
  163. set {
  164. if (index < 0) {
  165. throw new ArgumentOutOfRangeException ("index < 0");
  166. }
  167. // FIXME -- should setting an index implicitly extend the array?
  168. // the docs aren't clear -- I'm assuming not, since the exception
  169. // is listed for both get and set
  170. if (index >= count) {
  171. throw new ArgumentOutOfRangeException ("index out of range");
  172. }
  173. if (readOnly) {
  174. throw new NotSupportedException ("Collection is read-only.");
  175. }
  176. dataArray[index] = value;
  177. }
  178. }
  179. public virtual object SyncRoot {
  180. get {
  181. throw new NotImplementedException ("System.Collections.ArrayList.SyncRoot.get");
  182. }
  183. }
  184. // methods
  185. public virtual int Add (object value) {
  186. if (readOnly) {
  187. throw new NotSupportedException ("Collection is read-only.");
  188. }
  189. if (count + 1 >= capacity) {
  190. setSize (capacity * 2);
  191. }
  192. dataArray[count++] = value;
  193. return count-1;
  194. }
  195. public virtual void AddRange (ICollection c) {
  196. throw new NotImplementedException ("System.Collections.ArrayList.AddRange");
  197. }
  198. public virtual int BinarySearch (object value) {
  199. throw new NotImplementedException ("System.Collections.ArrayList.BinarySearch");
  200. }
  201. public virtual int BinarySearch (object value, IComparer comparer) {
  202. throw new NotImplementedException ("System.Collections.ArrayList.BinarySearch");
  203. }
  204. public virtual int BinarySearch (int index, int count,
  205. object value, IComparer comparer) {
  206. throw new NotImplementedException ("System.Collections.ArrayList.BinarySearch");
  207. }
  208. public virtual void Clear () {
  209. count = 0;
  210. setSize(capacity);
  211. }
  212. public virtual object Clone () {
  213. return new ArrayList (dataArray, count, capacity,
  214. fixedSize, readOnly, synchronized);
  215. }
  216. public virtual bool Contains (object item) {
  217. for (int i = 0; i < count; i++) {
  218. if (Object.Equals (dataArray[i], item)) {
  219. return true;
  220. }
  221. }
  222. return false;
  223. }
  224. public virtual void CopyTo (Array array) {
  225. }
  226. public virtual void CopyTo (Array array, int arrayIndex) {
  227. }
  228. public virtual void CopyTo (int index, Array array,
  229. int arrayIndex, int count) {
  230. }
  231. public virtual IEnumerator GetEnumerator () {
  232. return null;
  233. }
  234. public virtual IEnumerator GetEnumerator (int index, int count) {
  235. return null;
  236. }
  237. public virtual ArrayList GetRange (int index, int count) {
  238. return null;
  239. }
  240. public virtual int IndexOf (object value) {
  241. return IndexOf (value, 0, count);
  242. }
  243. public virtual int IndexOf (object value, int startIndex) {
  244. return IndexOf (value, startIndex, count - startIndex);
  245. }
  246. public virtual int IndexOf (object value, int startIndex, int count) {
  247. if (startIndex < 0 || startIndex + count > this.count || count < 0) {
  248. throw new ArgumentOutOfRangeException ("IndexOf arguments out of range");
  249. }
  250. for (int i = startIndex; i < (startIndex + count); i++) {
  251. if (Object.Equals (dataArray[i], value)) {
  252. return i;
  253. }
  254. }
  255. return -1;
  256. }
  257. public virtual void Insert (int index, object value) {
  258. if (readOnly) {
  259. throw new NotSupportedException
  260. ("Collection is read-only.");
  261. }
  262. if (fixedSize) {
  263. throw new NotSupportedException
  264. ("Collection is fixed size.");
  265. }
  266. if (index < 0 || index >= count) {
  267. throw new ArgumentOutOfRangeException ("index < 0 or index >= count");
  268. }
  269. shiftElements (index, 1);
  270. dataArray[index] = value;
  271. count++;
  272. }
  273. public virtual void InsertRange (int index, ICollection c) {
  274. }
  275. public virtual int LastIndexOf (object value) {
  276. return LastIndexOf (value, 0, count);
  277. }
  278. public virtual int LastIndexOf (object value, int startIndex) {
  279. return LastIndexOf (value, startIndex, count - startIndex);
  280. }
  281. public virtual int LastIndexOf (object value, int StartIndex,
  282. int count)
  283. {
  284. for (int i = count - 1; i >= 0; i--) {
  285. if (Object.Equals (dataArray[i], value)) {
  286. return i;
  287. }
  288. }
  289. return -1;
  290. }
  291. public virtual void Remove (object obj) {
  292. int objIndex = IndexOf (obj);
  293. if (objIndex == -1) {
  294. // shouldn't an exception be thrown here??
  295. // the MS docs don't indicate one, and testing
  296. // with the MS .net framework doesn't indicate one
  297. return;
  298. }
  299. RemoveRange (objIndex, 1);
  300. }
  301. public virtual void RemoveAt (int index) {
  302. RemoveRange (index, 1);
  303. }
  304. public virtual void RemoveRange (int index, int count) {
  305. if (readOnly) {
  306. throw new NotSupportedException
  307. ("Collection is read-only.");
  308. }
  309. if (fixedSize) {
  310. throw new NotSupportedException
  311. ("Collection is fixed size.");
  312. }
  313. if (index < 0 || index >= this.count || index + count > this.count) {
  314. throw new ArgumentOutOfRangeException
  315. ("index/count out of range");
  316. }
  317. shiftElements (index, - count);
  318. this.count -= count;
  319. }
  320. public virtual void Reverse () {
  321. Reverse (0, count);
  322. }
  323. public virtual void Reverse (int index, int count) {
  324. if (readOnly) {
  325. throw new NotSupportedException
  326. ("Collection is read-only.");
  327. }
  328. if (index < 0 || index + count > this.count) {
  329. throw new ArgumentOutOfRangeException
  330. ("index/count out of range");
  331. }
  332. Array.Reverse (dataArray, index, count);
  333. }
  334. public virtual void SetRange (int index, ICollection c) {
  335. }
  336. public virtual void Sort () {
  337. Sort (0, count, null);
  338. }
  339. public virtual void Sort (IComparer comparer) {
  340. Sort (0, count, comparer);
  341. }
  342. public virtual void Sort (int index, int count, IComparer comparer) {
  343. if (readOnly) {
  344. throw new NotSupportedException
  345. ("Collection is read-only.");
  346. }
  347. if (index < 0 || index + count > this.count) {
  348. throw new ArgumentOutOfRangeException
  349. ("index/count out of range");
  350. }
  351. Array.Sort (dataArray, index, count, comparer);
  352. }
  353. public virtual object[] ToArray() {
  354. object[] outArray = new object[count];
  355. Array.Copy (dataArray, outArray, count);
  356. return outArray;
  357. }
  358. public virtual Array ToArray (Type type) {
  359. Array outArray = Array.CreateInstance (type, count);
  360. Array.Copy (dataArray, outArray, count);
  361. return outArray;
  362. }
  363. public virtual void TrimToSize () {
  364. // FIXME: implement this
  365. }
  366. }
  367. }