ArrayList.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. if (index >= count) {
  168. throw new ArgumentOutOfRangeException ("index out of range");
  169. }
  170. if (readOnly) {
  171. throw new NotSupportedException ("Collection is read-only.");
  172. }
  173. dataArray[index] = value;
  174. }
  175. }
  176. public virtual object SyncRoot {
  177. get {
  178. throw new NotImplementedException ("System.Collections.ArrayList.SyncRoot.get");
  179. }
  180. }
  181. // methods
  182. public virtual int Add (object value) {
  183. if (readOnly) {
  184. throw new NotSupportedException ("Collection is read-only.");
  185. }
  186. if (count + 1 >= capacity) {
  187. setSize (capacity * 2);
  188. }
  189. dataArray[count++] = value;
  190. return count-1;
  191. }
  192. public virtual void AddRange (ICollection c) {
  193. int cc = c.Count;
  194. if (count + cc >= capacity)
  195. Capacity = cc < count? count * 2: count + cc + 1;
  196. c.CopyTo (dataArray, count);
  197. count += cc;
  198. }
  199. public virtual int BinarySearch (object value) {
  200. throw new NotImplementedException ("System.Collections.ArrayList.BinarySearch");
  201. }
  202. public virtual int BinarySearch (object value, IComparer comparer) {
  203. throw new NotImplementedException ("System.Collections.ArrayList.BinarySearch");
  204. }
  205. public virtual int BinarySearch (int index, int count,
  206. object value, IComparer comparer) {
  207. throw new NotImplementedException ("System.Collections.ArrayList.BinarySearch");
  208. }
  209. public virtual void Clear () {
  210. count = 0;
  211. setSize(capacity);
  212. }
  213. public virtual object Clone () {
  214. return new ArrayList (dataArray, count, capacity,
  215. fixedSize, readOnly, synchronized);
  216. }
  217. public virtual bool Contains (object item) {
  218. for (int i = 0; i < count; i++) {
  219. if (Object.Equals (dataArray[i], item)) {
  220. return true;
  221. }
  222. }
  223. return false;
  224. }
  225. public virtual void CopyTo (Array array) {
  226. CopyTo (array, 0);
  227. }
  228. public virtual void CopyTo (Array array, int arrayIndex) {
  229. System.Array.Copy (dataArray, 0, array, arrayIndex, count);
  230. }
  231. [MonoTODO]
  232. public virtual void CopyTo (int index, Array array,
  233. int arrayIndex, int count) {
  234. // FIXME: check count...
  235. System.Array.Copy (dataArray, index, array, arrayIndex, count);
  236. }
  237. private class ArrayListEnumerator : IEnumerator {
  238. private object[] data;
  239. private int idx;
  240. private int start;
  241. private int num;
  242. public ArrayListEnumerator(int index, int count, object[] items) {
  243. data = items;
  244. start = index;
  245. num = count;
  246. idx = start - 1;
  247. }
  248. public object Current {
  249. get {
  250. return data [idx];
  251. }
  252. }
  253. public bool MoveNext() {
  254. if (++idx < start + num)
  255. return true;
  256. return false;
  257. }
  258. public void Reset() {
  259. idx = start - 1;
  260. }
  261. }
  262. public virtual IEnumerator GetEnumerator () {
  263. return new ArrayListEnumerator(0, this.Count, dataArray);
  264. }
  265. public virtual IEnumerator GetEnumerator (int index, int count) {
  266. return new ArrayListEnumerator(index, count, dataArray);
  267. }
  268. public virtual ArrayList GetRange (int index, int count) {
  269. return null;
  270. }
  271. public virtual int IndexOf (object value) {
  272. return IndexOf (value, 0, count);
  273. }
  274. public virtual int IndexOf (object value, int startIndex) {
  275. return IndexOf (value, startIndex, count - startIndex);
  276. }
  277. public virtual int IndexOf (object value, int startIndex, int count) {
  278. if (startIndex < 0 || startIndex + count > this.count || count < 0) {
  279. throw new ArgumentOutOfRangeException ("IndexOf arguments out of range");
  280. }
  281. for (int i = startIndex; i < (startIndex + count); i++) {
  282. if (Object.Equals (dataArray[i], value)) {
  283. return i;
  284. }
  285. }
  286. return -1;
  287. }
  288. public virtual void Insert (int index, object value) {
  289. if (readOnly) {
  290. throw new NotSupportedException
  291. ("Collection is read-only.");
  292. }
  293. if (fixedSize) {
  294. throw new NotSupportedException
  295. ("Collection is fixed size.");
  296. }
  297. if (index < 0 || index >= capacity) {
  298. throw new ArgumentOutOfRangeException ("index < 0 or index >= capacity");
  299. }
  300. shiftElements (index, 1);
  301. dataArray[index] = value;
  302. count++;
  303. }
  304. public virtual void InsertRange (int index, ICollection c) {
  305. }
  306. public virtual int LastIndexOf (object value) {
  307. return LastIndexOf (value, 0, count);
  308. }
  309. public virtual int LastIndexOf (object value, int startIndex) {
  310. return LastIndexOf (value, startIndex, count - startIndex);
  311. }
  312. public virtual int LastIndexOf (object value, int StartIndex,
  313. int count)
  314. {
  315. for (int i = count - 1; i >= 0; i--) {
  316. if (Object.Equals (dataArray[i], value)) {
  317. return i;
  318. }
  319. }
  320. return -1;
  321. }
  322. public virtual void Remove (object obj) {
  323. int objIndex = IndexOf (obj);
  324. if (objIndex == -1) {
  325. // shouldn't an exception be thrown here??
  326. // the MS docs don't indicate one, and testing
  327. // with the MS .net framework doesn't indicate one
  328. return;
  329. }
  330. RemoveRange (objIndex, 1);
  331. }
  332. public virtual void RemoveAt (int index) {
  333. RemoveRange (index, 1);
  334. }
  335. public virtual void RemoveRange (int index, int count) {
  336. if (readOnly) {
  337. throw new NotSupportedException
  338. ("Collection is read-only.");
  339. }
  340. if (fixedSize) {
  341. throw new NotSupportedException
  342. ("Collection is fixed size.");
  343. }
  344. if (index < 0 || index >= this.count || index + count > this.count) {
  345. throw new ArgumentOutOfRangeException
  346. ("index/count out of range");
  347. }
  348. shiftElements (index, - count);
  349. this.count -= count;
  350. }
  351. public virtual void Reverse () {
  352. Reverse (0, count);
  353. }
  354. public virtual void Reverse (int index, int count) {
  355. if (readOnly) {
  356. throw new NotSupportedException
  357. ("Collection is read-only.");
  358. }
  359. if (index < 0 || index + count > this.count) {
  360. throw new ArgumentOutOfRangeException
  361. ("index/count out of range");
  362. }
  363. Array.Reverse (dataArray, index, count);
  364. }
  365. public virtual void SetRange (int index, ICollection c) {
  366. }
  367. public virtual void Sort () {
  368. Sort (0, count, null);
  369. }
  370. public virtual void Sort (IComparer comparer) {
  371. Sort (0, count, comparer);
  372. }
  373. public virtual void Sort (int index, int count, IComparer comparer) {
  374. if (readOnly) {
  375. throw new NotSupportedException
  376. ("Collection is read-only.");
  377. }
  378. if (index < 0 || index + count > this.count) {
  379. throw new ArgumentOutOfRangeException
  380. ("index/count out of range");
  381. }
  382. Array.Sort (dataArray, index, count, comparer);
  383. }
  384. public virtual object[] ToArray() {
  385. object[] outArray = new object[count];
  386. Array.Copy (dataArray, outArray, count);
  387. return outArray;
  388. }
  389. public virtual Array ToArray (Type type) {
  390. Array outArray = Array.CreateInstance (type, count);
  391. Array.Copy (dataArray, outArray, count);
  392. return outArray;
  393. }
  394. [MonoTODO]
  395. public virtual void TrimToSize () {
  396. // FIXME: implement this
  397. }
  398. }
  399. }