ArrayList.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. //
  2. // System.Collections.ArrayList
  3. //
  4. // Author:
  5. // Vladimir Vukicevic ([email protected])
  6. //
  7. // (C) 2001 Vladimir Vukicevic
  8. //
  9. using System;
  10. namespace System.Collections {
  11. [MonoTODO ("add versioning, changing the arraylist should invalidate all enumerators")]
  12. [Serializable]
  13. public class ArrayList : IList, ICollection, IEnumerable, ICloneable {
  14. // constructors
  15. public ArrayList () {
  16. dataArray = new object[capacity];
  17. }
  18. public ArrayList (ICollection c) {
  19. if (null == c)
  20. throw new ArgumentNullException();
  21. dataArray = new object [c.Count];
  22. this.capacity = c.Count;
  23. foreach (object o in c) {
  24. Add (o);
  25. }
  26. }
  27. public ArrayList (int capacity) {
  28. if (capacity < 0)
  29. throw new ArgumentOutOfRangeException ("capacity", capacity, "Value must be greater than or equal to zero.");
  30. dataArray = new object[capacity];
  31. this.capacity = capacity;
  32. }
  33. private ArrayList (object[] dataArray, int count, int capacity,
  34. bool fixedSize, bool readOnly, bool synchronized)
  35. {
  36. this.dataArray = (object []) dataArray.Clone();
  37. this.count = count;
  38. this.capacity = capacity;
  39. this.fixedSize = fixedSize;
  40. this.readOnly = readOnly;
  41. this.synchronized = synchronized;
  42. }
  43. public static ArrayList ReadOnly (ArrayList list)
  44. {
  45. if (list == null)
  46. throw new ArgumentNullException ();
  47. return new ArrayList (list.ToArray (), list.Count, list.Capacity,
  48. list.IsFixedSize, true, list.IsSynchronized);
  49. }
  50. public static IList ReadOnly (IList list)
  51. {
  52. if (list == null)
  53. throw new ArgumentNullException ();
  54. ArrayList al = new ArrayList ();
  55. foreach (object o in list)
  56. al.Add (o);
  57. return (IList) ArrayList.ReadOnly (al);
  58. }
  59. public static ArrayList Synchronized (ArrayList list)
  60. {
  61. if (list == null)
  62. throw new ArgumentNullException ();
  63. return new ArrayList (list.ToArray (), list.Count, list.Capacity,
  64. list.IsFixedSize, list.IsReadOnly, true);
  65. }
  66. public static IList Synchronized (IList list)
  67. {
  68. if (list == null)
  69. throw new ArgumentNullException ();
  70. ArrayList al = new ArrayList ();
  71. foreach (object o in list)
  72. al.Add (o);
  73. return (IList) ArrayList.Synchronized (al);
  74. }
  75. public static ArrayList FixedSize (ArrayList list)
  76. {
  77. if (list == null)
  78. throw new ArgumentNullException ();
  79. return new ArrayList (list.ToArray (), list.Count, list.Capacity,
  80. true, list.IsReadOnly, list.IsSynchronized);
  81. }
  82. public static IList FixedSize (IList list)
  83. {
  84. if (list == null)
  85. throw new ArgumentNullException ();
  86. ArrayList al = new ArrayList ();
  87. foreach (object o in list)
  88. al.Add (o);
  89. return (IList) ArrayList.FixedSize (al);
  90. }
  91. public static ArrayList Repeat (object value, int count)
  92. {
  93. ArrayList al = new ArrayList (count);
  94. for (int i = 0; i < count; i++) {
  95. al.dataArray[i] = value;
  96. }
  97. al.count = count;
  98. return al;
  99. }
  100. [MonoTODO]
  101. public static ArrayList Adapter (IList list)
  102. {
  103. throw new NotImplementedException ("System.Collections.ArrayList.Adapter");
  104. }
  105. // properties
  106. private bool fixedSize = false;
  107. private bool readOnly = false;
  108. private bool synchronized = false;
  109. private long version = 0;
  110. private ArrayList source = null;
  111. private int count = 0;
  112. private int capacity = 16;
  113. private object[] dataArray;
  114. private void copyDataArray (object[] outArray) {
  115. for (int i = 0; i < count; i++) {
  116. outArray[i] = dataArray[i];
  117. }
  118. }
  119. private void setSize (int newSize) {
  120. if (newSize == capacity) {
  121. return;
  122. }
  123. // note that this assumes that we've already sanity-checked
  124. // the new size
  125. object[] newDataArray = new object[newSize];
  126. copyDataArray (newDataArray);
  127. dataArray = newDataArray;
  128. capacity = newSize;
  129. }
  130. // note that this DOES NOT update count
  131. private void shiftElements (int startIndex, int numshift) {
  132. if (numshift == 0) {
  133. return;
  134. }
  135. if (count + numshift > capacity) {
  136. setSize (capacity * 2);
  137. shiftElements (startIndex, numshift);
  138. } else {
  139. if (numshift > 0) {
  140. int numelts = count - startIndex;
  141. for (int i = numelts-1; i >= 0; i--) {
  142. dataArray[startIndex + numshift + i] = dataArray[startIndex + i];
  143. }
  144. for (int i = startIndex; i < startIndex + numshift; i++) {
  145. dataArray[i] = null;
  146. }
  147. } else {
  148. int numelts = count - startIndex + numshift;
  149. for (int i = startIndex; i <= numelts; i++) {
  150. dataArray[i] = dataArray[i - numshift];
  151. }
  152. for (int i = count + numshift; i < count; i++) {
  153. dataArray[i] = null;
  154. }
  155. }
  156. }
  157. }
  158. public virtual int Capacity {
  159. get {
  160. return capacity;
  161. }
  162. set {
  163. if (readOnly) {
  164. throw new NotSupportedException
  165. ("Collection is read-only.");
  166. }
  167. if (value < count) {
  168. throw new ArgumentOutOfRangeException
  169. ("ArrayList Capacity being set to less than Count");
  170. }
  171. if (fixedSize && value != capacity) {
  172. throw new NotSupportedException
  173. ("Collection is fixed size.");
  174. }
  175. setSize (value);
  176. }
  177. }
  178. private void CheckSourceVersion() {
  179. if (null != this.source && this.version != this.source.version) {
  180. throw new InvalidOperationException();
  181. }
  182. }
  183. public virtual int Count {
  184. get {
  185. CheckSourceVersion();
  186. return count;
  187. }
  188. }
  189. public virtual bool IsFixedSize {
  190. get {
  191. return fixedSize;
  192. }
  193. }
  194. public virtual bool IsReadOnly {
  195. get {
  196. return readOnly;
  197. }
  198. }
  199. public virtual bool IsSynchronized {
  200. get {
  201. return synchronized;
  202. }
  203. }
  204. public virtual object this[int index] {
  205. get {
  206. CheckSourceVersion();
  207. if (index < 0) {
  208. throw new ArgumentOutOfRangeException ("index < 0");
  209. }
  210. if (index >= count) {
  211. throw new ArgumentOutOfRangeException ("index out of range");
  212. }
  213. return dataArray[index];
  214. }
  215. set {
  216. if (index < 0) {
  217. throw new ArgumentOutOfRangeException ("index < 0");
  218. }
  219. if (index >= count) {
  220. throw new ArgumentOutOfRangeException ("index out of range");
  221. }
  222. if (readOnly) {
  223. throw new NotSupportedException ("Collection is read-only.");
  224. }
  225. dataArray[index] = value;
  226. version++;
  227. }
  228. }
  229. [MonoTODO]
  230. public virtual object SyncRoot {
  231. get {
  232. throw new NotImplementedException ("System.Collections.ArrayList.SyncRoot.get");
  233. }
  234. }
  235. // methods
  236. public virtual int Add (object value) {
  237. if (readOnly) {
  238. throw new NotSupportedException ("Collection is read-only.");
  239. }
  240. if (count + 1 >= capacity) {
  241. setSize (capacity * 2);
  242. }
  243. dataArray[count] = value;
  244. version++;
  245. return count++;
  246. }
  247. public virtual void AddRange (ICollection c) {
  248. int cc = c.Count;
  249. if (count + cc >= capacity)
  250. Capacity = cc < count? count * 2: count + cc + 1;
  251. c.CopyTo (dataArray, count);
  252. count += cc;
  253. version++;
  254. }
  255. public virtual int BinarySearch (object value) {
  256. return BinarySearch (0, count, value, null);
  257. }
  258. public virtual int BinarySearch (object value, IComparer comparer) {
  259. return BinarySearch (0, count, value, comparer);
  260. }
  261. public virtual int BinarySearch (int index, int count,
  262. object value, IComparer comparer) {
  263. return Array.BinarySearch (dataArray, index, count, value, comparer);
  264. }
  265. public virtual void Clear () {
  266. count = 0;
  267. setSize(capacity);
  268. version++;
  269. }
  270. public virtual object Clone () {
  271. return new ArrayList (dataArray, count, capacity,
  272. fixedSize, readOnly, synchronized);
  273. }
  274. public virtual bool Contains (object item) {
  275. for (int i = 0; i < count; i++) {
  276. if (Object.Equals (dataArray[i], item)) {
  277. return true;
  278. }
  279. }
  280. return false;
  281. }
  282. public virtual void CopyTo (Array array) {
  283. System.Array.Copy (dataArray, 0, array, 0, count);
  284. }
  285. public virtual void CopyTo (Array array, int arrayIndex) {
  286. System.Array.Copy (dataArray, 0, array, arrayIndex, count);
  287. }
  288. public virtual void CopyTo (int index, Array array,
  289. int arrayIndex, int count) {
  290. System.Array.Copy (dataArray, index, array, arrayIndex, count);
  291. }
  292. [Serializable]
  293. private class ArrayListEnumerator : IEnumerator {
  294. private object[] data;
  295. private int idx;
  296. private int start;
  297. private int num;
  298. internal ArrayListEnumerator(int index, int count, object[] items) {
  299. data = items;
  300. start = index;
  301. num = count;
  302. idx = start - 1;
  303. }
  304. public object Clone ()
  305. {
  306. return new ArrayListEnumerator (start, num, data);
  307. }
  308. public virtual object Current {
  309. get {
  310. return data [idx];
  311. }
  312. }
  313. public virtual bool MoveNext() {
  314. if (++idx < start + num)
  315. return true;
  316. return false;
  317. }
  318. public virtual void Reset() {
  319. idx = start - 1;
  320. }
  321. }
  322. public virtual IEnumerator GetEnumerator () {
  323. return new ArrayListEnumerator(0, this.Count, dataArray);
  324. }
  325. private void ValidateRange(int index, int count) {
  326. if (index < 0) {
  327. throw new ArgumentOutOfRangeException("index", index, "Must be equal to or greater than zero");
  328. }
  329. if (count < 0) {
  330. throw new ArgumentOutOfRangeException("count", count, "Must be equal to or greater than zero");
  331. }
  332. if (index > this.count - 1) {
  333. throw new ArgumentException();
  334. }
  335. if (index + count > this.count - 1) {
  336. throw new ArgumentException();
  337. }
  338. }
  339. public virtual IEnumerator GetEnumerator (int index, int count) {
  340. ValidateRange(index, count);
  341. return new ArrayListEnumerator(index, count, dataArray);
  342. }
  343. public virtual ArrayList GetRange (int index, int count) {
  344. ValidateRange(index, count);
  345. ArrayList retVal = new ArrayList(count);
  346. for (int i = index; i < count + index; i++) {
  347. retVal.Add(this[i]);
  348. }
  349. retVal.version = this.version;
  350. retVal.source = this;
  351. return retVal;
  352. }
  353. public virtual int IndexOf (object value) {
  354. return IndexOf (value, 0, count);
  355. }
  356. public virtual int IndexOf (object value, int startIndex) {
  357. return IndexOf (value, startIndex, count - startIndex);
  358. }
  359. public virtual int IndexOf (object value, int startIndex, int count) {
  360. if (startIndex < 0 || startIndex + count > this.count || count < 0) {
  361. throw new ArgumentOutOfRangeException ("IndexOf arguments out of range");
  362. }
  363. for (int i = startIndex; i < (startIndex + count); i++) {
  364. if (Object.Equals (dataArray[i], value)) {
  365. return i;
  366. }
  367. }
  368. return -1;
  369. }
  370. public virtual void Insert (int index, object value) {
  371. if (readOnly) {
  372. throw new NotSupportedException
  373. ("Collection is read-only.");
  374. }
  375. if (fixedSize) {
  376. throw new NotSupportedException
  377. ("Collection is fixed size.");
  378. }
  379. if (index < 0 || index >= capacity) {
  380. throw new ArgumentOutOfRangeException ("index < 0 or index >= capacity");
  381. }
  382. shiftElements (index, 1);
  383. dataArray[index] = value;
  384. count++;
  385. version++;
  386. }
  387. [MonoTODO]
  388. public virtual void InsertRange (int index, ICollection c) {
  389. if (c == null)
  390. throw new ArgumentNullException ();
  391. if (index < 0 || index > this.Count)
  392. throw new ArgumentOutOfRangeException ();
  393. if (IsReadOnly || IsFixedSize)
  394. throw new NotSupportedException ();
  395. version++;
  396. }
  397. public virtual int LastIndexOf (object value) {
  398. return LastIndexOf (value, count - 1, count);
  399. }
  400. public virtual int LastIndexOf (object value, int startIndex) {
  401. if (startIndex < 0 || startIndex > count - 1) {
  402. throw new ArgumentOutOfRangeException("startIndex", startIndex, "");
  403. }
  404. return LastIndexOf (value, startIndex, startIndex + 1);
  405. }
  406. public virtual int LastIndexOf (object value, int StartIndex,
  407. int count)
  408. {
  409. int EndIndex = StartIndex - count + 1;
  410. for (int i = StartIndex; i >= EndIndex; i--) {
  411. if (Object.Equals (dataArray[i], value)) {
  412. return i;
  413. }
  414. }
  415. return -1;
  416. }
  417. public virtual void Remove (object obj) {
  418. int objIndex = IndexOf (obj);
  419. if (objIndex == -1) {
  420. // shouldn't an exception be thrown here??
  421. // the MS docs don't indicate one, and testing
  422. // with the MS .net framework doesn't indicate one
  423. return;
  424. }
  425. RemoveRange (objIndex, 1);
  426. }
  427. public virtual void RemoveAt (int index) {
  428. RemoveRange (index, 1);
  429. }
  430. public virtual void RemoveRange (int index, int count) {
  431. if (readOnly) {
  432. throw new NotSupportedException
  433. ("Collection is read-only.");
  434. }
  435. if (fixedSize) {
  436. throw new NotSupportedException
  437. ("Collection is fixed size.");
  438. }
  439. if (index < 0 || index >= this.count || index + count > this.count) {
  440. throw new ArgumentOutOfRangeException
  441. ("index/count out of range");
  442. }
  443. shiftElements (index, - count);
  444. this.count -= count;
  445. version++;
  446. }
  447. public virtual void Reverse () {
  448. Reverse (0, count);
  449. }
  450. public virtual void Reverse (int index, int count) {
  451. if (readOnly) {
  452. throw new NotSupportedException
  453. ("Collection is read-only.");
  454. }
  455. if (index < 0 || index + count > this.count) {
  456. throw new ArgumentOutOfRangeException
  457. ("index/count out of range");
  458. }
  459. Array.Reverse (dataArray, index, count);
  460. version++;
  461. }
  462. [MonoTODO]
  463. public virtual void SetRange (int index, ICollection c) {
  464. }
  465. public virtual void Sort () {
  466. Sort (0, count, null);
  467. }
  468. public virtual void Sort (IComparer comparer) {
  469. Sort (0, count, comparer);
  470. }
  471. public virtual void Sort (int index, int count, IComparer comparer) {
  472. if (readOnly) {
  473. throw new NotSupportedException
  474. ("Collection is read-only.");
  475. }
  476. if (index < 0 || index + count > this.count) {
  477. throw new ArgumentOutOfRangeException
  478. ("index/count out of range");
  479. }
  480. Array.Sort (dataArray, index, count, comparer);
  481. version++;
  482. }
  483. public virtual object[] ToArray() {
  484. object[] outArray = new object[count];
  485. Array.Copy (dataArray, outArray, count);
  486. return outArray;
  487. }
  488. public virtual Array ToArray (Type type) {
  489. Array outArray = Array.CreateInstance (type, count);
  490. Array.Copy (dataArray, outArray, count);
  491. return outArray;
  492. }
  493. [MonoTODO]
  494. public virtual void TrimToSize () {
  495. // FIXME: implement this
  496. version++;
  497. }
  498. }
  499. }