ArrayList.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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 const int defaultCapacity = 16;
  113. private int capacity = defaultCapacity;
  114. private object[] dataArray;
  115. private void copyDataArray (object[] outArray) {
  116. for (int i = 0; i < count; i++) {
  117. outArray[i] = dataArray[i];
  118. }
  119. }
  120. private void setSize (int newSize) {
  121. if (newSize == capacity) {
  122. return;
  123. }
  124. // note that this assumes that we've already sanity-checked
  125. // the new size
  126. object[] newDataArray = new object[newSize];
  127. copyDataArray (newDataArray);
  128. dataArray = newDataArray;
  129. capacity = newSize;
  130. }
  131. // note that this DOES NOT update count
  132. private void shiftElements (int startIndex, int numshift) {
  133. if (numshift == 0) {
  134. return;
  135. }
  136. if (count + numshift > capacity) {
  137. setSize (capacity * 2);
  138. shiftElements (startIndex, numshift);
  139. } else {
  140. if (numshift > 0) {
  141. int numelts = count - startIndex;
  142. for (int i = numelts-1; i >= 0; i--) {
  143. dataArray[startIndex + numshift + i] = dataArray[startIndex + i];
  144. }
  145. for (int i = startIndex; i < startIndex + numshift; i++) {
  146. dataArray[i] = null;
  147. }
  148. } else {
  149. int numelts = count - startIndex + numshift;
  150. for (int i = startIndex; i <= numelts; i++) {
  151. dataArray[i] = dataArray[i - numshift];
  152. }
  153. for (int i = count + numshift; i < count; i++) {
  154. dataArray[i] = null;
  155. }
  156. }
  157. }
  158. }
  159. public virtual int Capacity {
  160. get {
  161. return capacity;
  162. }
  163. set {
  164. if (readOnly) {
  165. throw new NotSupportedException
  166. ("Collection is read-only.");
  167. }
  168. if (value < count) {
  169. throw new ArgumentOutOfRangeException
  170. ("ArrayList Capacity being set to less than Count");
  171. }
  172. if (fixedSize && value != capacity) {
  173. throw new NotSupportedException
  174. ("Collection is fixed size.");
  175. }
  176. setSize (value);
  177. }
  178. }
  179. private void CheckSourceVersion() {
  180. if (null != this.source && this.version != this.source.version) {
  181. throw new InvalidOperationException();
  182. }
  183. }
  184. public virtual int Count {
  185. get {
  186. CheckSourceVersion();
  187. return count;
  188. }
  189. }
  190. public virtual bool IsFixedSize {
  191. get {
  192. return fixedSize;
  193. }
  194. }
  195. public virtual bool IsReadOnly {
  196. get {
  197. return readOnly;
  198. }
  199. }
  200. public virtual bool IsSynchronized {
  201. get {
  202. return synchronized;
  203. }
  204. }
  205. public virtual object this[int index] {
  206. get {
  207. CheckSourceVersion();
  208. if (index < 0) {
  209. throw new ArgumentOutOfRangeException ("index < 0");
  210. }
  211. if (index >= count) {
  212. throw new ArgumentOutOfRangeException ("index out of range");
  213. }
  214. return dataArray[index];
  215. }
  216. set {
  217. if (index < 0) {
  218. throw new ArgumentOutOfRangeException ("index < 0");
  219. }
  220. if (index >= count) {
  221. throw new ArgumentOutOfRangeException ("index out of range");
  222. }
  223. if (readOnly) {
  224. throw new NotSupportedException ("Collection is read-only.");
  225. }
  226. dataArray[index] = value;
  227. version++;
  228. }
  229. }
  230. [MonoTODO]
  231. public virtual object SyncRoot {
  232. get {
  233. throw new NotImplementedException ("System.Collections.ArrayList.SyncRoot.get");
  234. }
  235. }
  236. // methods
  237. public virtual int Add (object value) {
  238. if (readOnly)
  239. throw new NotSupportedException ("ArrayList is read-only.");
  240. if (fixedSize)
  241. throw new NotSupportedException ("ArrayList is fixed size.");
  242. if (count + 1 >= capacity)
  243. setSize (capacity * 2);
  244. dataArray[count] = value;
  245. version++;
  246. return count++;
  247. }
  248. public virtual void AddRange (ICollection c) {
  249. if (null == c)
  250. throw new ArgumentNullException ("c");
  251. if (readOnly || fixedSize)
  252. throw new NotSupportedException ();
  253. int cc = c.Count;
  254. if (count + cc >= capacity)
  255. Capacity = cc < count? count * 2: count + cc + 1;
  256. c.CopyTo (dataArray, count);
  257. count += cc;
  258. version++;
  259. }
  260. public virtual int BinarySearch (object value) {
  261. return BinarySearch (0, count, value, null);
  262. }
  263. public virtual int BinarySearch (object value, IComparer comparer) {
  264. return BinarySearch (0, count, value, comparer);
  265. }
  266. public virtual int BinarySearch (int index, int count,
  267. object value, IComparer comparer) {
  268. return Array.BinarySearch (dataArray, index, count, value, comparer);
  269. }
  270. public virtual void Clear () {
  271. if (readOnly || fixedSize)
  272. throw new NotSupportedException();
  273. count = 0;
  274. setSize(capacity);
  275. version++;
  276. }
  277. public virtual object Clone () {
  278. return new ArrayList (dataArray, count, capacity,
  279. fixedSize, readOnly, synchronized);
  280. }
  281. public virtual bool Contains (object item) {
  282. for (int i = 0; i < count; i++) {
  283. if (Object.Equals (dataArray[i], item)) {
  284. return true;
  285. }
  286. }
  287. return false;
  288. }
  289. public virtual void CopyTo (Array array) {
  290. if (null == array)
  291. throw new ArgumentNullException("array");
  292. if (array.Rank > 1)
  293. throw new ArgumentException("array cannot be multidimensional");
  294. System.Array.Copy (dataArray, 0, array, 0, this.count);
  295. }
  296. public virtual void CopyTo (Array array, int arrayIndex) {
  297. if (null == array)
  298. throw new ArgumentNullException("array");
  299. if (arrayIndex < 0)
  300. throw new ArgumentOutOfRangeException("arrayIndex");
  301. if (array.Rank > 1)
  302. throw new ArgumentException("array cannot be multidimensional");
  303. if (arrayIndex >= array.Length)
  304. throw new ArgumentException("arrayIndex is greater than or equal to array's length");
  305. if (this.count > array.Length - arrayIndex)
  306. throw new ArgumentException("this ArrayList has more items than the space available in array from arrayIndex to the end of array");
  307. System.Array.Copy (dataArray, 0, array, arrayIndex, this.count);
  308. }
  309. public virtual void CopyTo (int index, Array array,
  310. int arrayIndex, int count) {
  311. if (null == array)
  312. throw new ArgumentNullException("array");
  313. if (arrayIndex < 0)
  314. throw new ArgumentOutOfRangeException("arrayIndex");
  315. if (index < 0)
  316. throw new ArgumentOutOfRangeException("index");
  317. if (count < 0)
  318. throw new ArgumentOutOfRangeException("count");
  319. if (index >= this.count)
  320. throw new ArgumentException("index is greater than or equal to the source ArrayList.Count");
  321. if (array.Rank > 1)
  322. throw new ArgumentException("array cannot be multidimensional");
  323. if (arrayIndex >= array.Length)
  324. throw new ArgumentException("arrayIndex is greater than or equal to array's length");
  325. if (this.count > array.Length - arrayIndex)
  326. throw new ArgumentException("this ArrayList has more items than the space available in array from arrayIndex to the end of array");
  327. System.Array.Copy (dataArray, index, array, arrayIndex, count);
  328. }
  329. [Serializable]
  330. private class ArrayListEnumerator : IEnumerator {
  331. private object[] data;
  332. private int idx;
  333. private int start;
  334. private int num;
  335. internal ArrayListEnumerator(int index, int count, object[] items) {
  336. data = items;
  337. start = index;
  338. num = count;
  339. idx = start - 1;
  340. }
  341. public object Clone ()
  342. {
  343. return new ArrayListEnumerator (start, num, data);
  344. }
  345. public virtual object Current {
  346. get {
  347. return data [idx];
  348. }
  349. }
  350. public virtual bool MoveNext() {
  351. if (++idx < start + num)
  352. return true;
  353. return false;
  354. }
  355. public virtual void Reset() {
  356. idx = start - 1;
  357. }
  358. }
  359. public virtual IEnumerator GetEnumerator () {
  360. return new ArrayListEnumerator(0, this.Count, dataArray);
  361. }
  362. private void ValidateRange(int index, int count) {
  363. if (index < 0) {
  364. throw new ArgumentOutOfRangeException("index", index, "Must be equal to or greater than zero");
  365. }
  366. if (count < 0) {
  367. throw new ArgumentOutOfRangeException("count", count, "Must be equal to or greater than zero");
  368. }
  369. if (index > this.count - 1) {
  370. throw new ArgumentException();
  371. }
  372. if (index + count > this.count - 1) {
  373. throw new ArgumentException();
  374. }
  375. }
  376. public virtual IEnumerator GetEnumerator (int index, int count) {
  377. ValidateRange(index, count);
  378. return new ArrayListEnumerator(index, count, dataArray);
  379. }
  380. public virtual ArrayList GetRange (int index, int count) {
  381. ValidateRange(index, count);
  382. ArrayList retVal = new ArrayList(count);
  383. for (int i = index; i < count + index; i++) {
  384. retVal.Add(this[i]);
  385. }
  386. retVal.version = this.version;
  387. retVal.source = this;
  388. return retVal;
  389. }
  390. public virtual int IndexOf (object value) {
  391. return IndexOf (value, 0, count);
  392. }
  393. public virtual int IndexOf (object value, int startIndex) {
  394. return IndexOf (value, startIndex, count - startIndex);
  395. }
  396. public virtual int IndexOf (object value, int startIndex, int count) {
  397. if (startIndex < 0 || startIndex + count > this.count || count < 0) {
  398. throw new ArgumentOutOfRangeException ("IndexOf arguments out of range");
  399. }
  400. for (int i = startIndex; i < (startIndex + count); i++) {
  401. if (Object.Equals (dataArray[i], value)) {
  402. return i;
  403. }
  404. }
  405. return -1;
  406. }
  407. public virtual void Insert (int index, object value) {
  408. if (readOnly) {
  409. throw new NotSupportedException
  410. ("Collection is read-only.");
  411. }
  412. if (fixedSize) {
  413. throw new NotSupportedException
  414. ("Collection is fixed size.");
  415. }
  416. if (index < 0 || index >= capacity) {
  417. throw new ArgumentOutOfRangeException ("index < 0 or index >= capacity");
  418. }
  419. shiftElements (index, 1);
  420. dataArray[index] = value;
  421. count++;
  422. version++;
  423. }
  424. public virtual void InsertRange (int index, ICollection c) {
  425. if (c == null)
  426. throw new ArgumentNullException ();
  427. if (index < 0 || index > count)
  428. throw new ArgumentOutOfRangeException ();
  429. if (IsReadOnly || IsFixedSize)
  430. throw new NotSupportedException ();
  431. shiftElements (index, c.Count);
  432. count += c.Count;
  433. IEnumerator en = c.GetEnumerator();
  434. while (en.MoveNext())
  435. dataArray[index++] = en.Current;
  436. version++;
  437. }
  438. public virtual int LastIndexOf (object value) {
  439. return LastIndexOf (value, count - 1, count);
  440. }
  441. public virtual int LastIndexOf (object value, int startIndex) {
  442. if (startIndex < 0 || startIndex > count - 1) {
  443. throw new ArgumentOutOfRangeException("startIndex", startIndex, "");
  444. }
  445. return LastIndexOf (value, startIndex, startIndex + 1);
  446. }
  447. public virtual int LastIndexOf (object value, int StartIndex,
  448. int count)
  449. {
  450. int EndIndex = StartIndex - count + 1;
  451. for (int i = StartIndex; i >= EndIndex; i--) {
  452. if (Object.Equals (dataArray[i], value)) {
  453. return i;
  454. }
  455. }
  456. return -1;
  457. }
  458. public virtual void Remove (object obj) {
  459. if (IsFixedSize || IsReadOnly)
  460. throw new NotSupportedException ();
  461. int objIndex = IndexOf (obj);
  462. if (objIndex == -1) {
  463. // shouldn't an exception be thrown here??
  464. // the MS docs don't indicate one, and testing
  465. // with the MS .net framework doesn't indicate one
  466. return;
  467. }
  468. RemoveRange (objIndex, 1);
  469. }
  470. public virtual void RemoveAt (int index) {
  471. RemoveRange (index, 1);
  472. }
  473. public virtual void RemoveRange (int index, int count) {
  474. if (readOnly) {
  475. throw new NotSupportedException
  476. ("Collection is read-only.");
  477. }
  478. if (fixedSize) {
  479. throw new NotSupportedException
  480. ("Collection is fixed size.");
  481. }
  482. if (index < 0 || index >= this.count || index + count > this.count) {
  483. throw new ArgumentOutOfRangeException
  484. ("index/count out of range");
  485. }
  486. shiftElements (index, - count);
  487. this.count -= count;
  488. version++;
  489. }
  490. public virtual void Reverse () {
  491. Reverse (0, count);
  492. }
  493. public virtual void Reverse (int index, int count) {
  494. if (readOnly) {
  495. throw new NotSupportedException
  496. ("Collection is read-only.");
  497. }
  498. if (index < 0 || index + count > this.count) {
  499. throw new ArgumentOutOfRangeException
  500. ("index/count out of range");
  501. }
  502. Array.Reverse (dataArray, index, count);
  503. version++;
  504. }
  505. public virtual void SetRange (int index, ICollection c)
  506. {
  507. if (c == null)
  508. throw new ArgumentNullException ();
  509. if (readOnly)
  510. throw new NotSupportedException ();
  511. if (index < 0 || (index + c.Count) > count)
  512. throw new ArgumentOutOfRangeException ();
  513. c.CopyTo(dataArray, index);
  514. }
  515. public virtual void Sort () {
  516. Sort (0, count, null);
  517. }
  518. public virtual void Sort (IComparer comparer) {
  519. Sort (0, count, comparer);
  520. }
  521. public virtual void Sort (int index, int count, IComparer comparer) {
  522. if (readOnly) {
  523. throw new NotSupportedException
  524. ("Collection is read-only.");
  525. }
  526. if (index < 0 || index + count > this.count) {
  527. throw new ArgumentOutOfRangeException
  528. ("index/count out of range");
  529. }
  530. Array.Sort (dataArray, index, count, comparer);
  531. version++;
  532. }
  533. public virtual object[] ToArray() {
  534. object[] outArray = new object[count];
  535. Array.Copy (dataArray, outArray, count);
  536. return outArray;
  537. }
  538. public virtual Array ToArray (Type type) {
  539. Array outArray = Array.CreateInstance (type, count);
  540. Array.Copy (dataArray, outArray, count);
  541. return outArray;
  542. }
  543. public virtual void TrimToSize () {
  544. if (IsReadOnly || IsFixedSize)
  545. throw new NotSupportedException ();
  546. if (count == 0)
  547. setSize(defaultCapacity);
  548. else
  549. setSize(count);
  550. version++;
  551. }
  552. }
  553. }