ArrayList.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  1. //
  2. // System.Collections.ArrayList
  3. //
  4. // Author:
  5. // Vladimir Vukicevic ([email protected])
  6. // Duncan Mak ([email protected])
  7. // Patrik Torstensson ([email protected])
  8. //
  9. // (C) 2001 Vladimir Vukicevic
  10. // (C) 2002 Ximian, Inc.
  11. //
  12. using System;
  13. namespace System.Collections {
  14. [MonoTODO ("add versioning, changing the arraylist should invalidate all enumerators")]
  15. [Serializable]
  16. public class ArrayList : IList, ICollection, IEnumerable, ICloneable {
  17. // Keep these three fields in sync with mono-reflection.h.
  18. private int count = 0;
  19. private int capacity = defaultCapacity;
  20. private object[] dataArray;
  21. // constructors
  22. public ArrayList () {
  23. dataArray = new object[capacity];
  24. }
  25. public ArrayList (ICollection c) {
  26. if (null == c)
  27. throw new ArgumentNullException();
  28. //Emulate MS.NET behavior. Throw RankException when passed a
  29. // multi-dimensional Array.
  30. Array arr = c as Array;
  31. if (null != arr && arr.Rank > 1)
  32. throw new RankException ();
  33. this.capacity = (c.Count == 0) ? defaultCapacity : c.Count;
  34. dataArray = new object [capacity];
  35. foreach (object o in c)
  36. Add (o);
  37. }
  38. public ArrayList (int capacity) {
  39. if (capacity < 0)
  40. throw new ArgumentOutOfRangeException ("capacity", capacity, "Value must be greater than or equal to zero.");
  41. if (capacity > 0)
  42. this.capacity = capacity;
  43. // else if capacity == 0 then use defaultCapacity
  44. dataArray = new object[this.capacity];
  45. }
  46. private ArrayList (object[] dataArray, int count, int capacity,
  47. bool fixedSize, bool readOnly, bool synchronized) {
  48. this.dataArray = new object [capacity];
  49. dataArray.CopyTo (this.dataArray, 0);
  50. this.count = count;
  51. this.capacity = capacity;
  52. this.fixedSize = fixedSize;
  53. this.readOnly = readOnly;
  54. this.synchronized = synchronized;
  55. }
  56. public static ArrayList ReadOnly (ArrayList list) {
  57. if (list == null)
  58. throw new ArgumentNullException ();
  59. if (list.IsSynchronized)
  60. return ArrayList.Synchronized (list);
  61. else
  62. return new ArrayList (list.ToArray (), list.Count, list.Capacity, list.IsFixedSize, true, list.IsSynchronized);
  63. }
  64. public static IList ReadOnly (IList list) {
  65. if (list == null)
  66. throw new ArgumentNullException ();
  67. ArrayList al = new ArrayList ();
  68. foreach (object o in list)
  69. al.Add (o);
  70. return (IList) ArrayList.ReadOnly (al);
  71. }
  72. public static ArrayList Synchronized (ArrayList list) {
  73. if (list == null)
  74. throw new ArgumentNullException ();
  75. return new SyncArrayList(new ArrayList (list.ToArray (), list.Count, list.Capacity, list.IsFixedSize, list.IsReadOnly, true));
  76. }
  77. public static IList Synchronized (IList list) {
  78. if (list == null)
  79. throw new ArgumentNullException ();
  80. ArrayList al = new ArrayList ();
  81. foreach (object o in list)
  82. al.Add (o);
  83. return (IList) ArrayList.Synchronized (al);
  84. }
  85. public static ArrayList FixedSize (ArrayList list) {
  86. if (list == null)
  87. throw new ArgumentNullException ();
  88. if (list.IsSynchronized)
  89. return Synchronized(list);
  90. return new ArrayList (list.ToArray (), list.Count, list.Capacity, true, list.IsReadOnly, list.IsSynchronized);
  91. }
  92. public static IList FixedSize (IList list) {
  93. if (list == null)
  94. throw new ArgumentNullException ();
  95. if (list.IsSynchronized)
  96. return Synchronized(list);
  97. ArrayList al = new ArrayList ();
  98. foreach (object o in list)
  99. al.Add (o);
  100. return (IList) ArrayList.FixedSize (al);
  101. }
  102. public static ArrayList Repeat (object value, int count) {
  103. ArrayList al = new ArrayList (count);
  104. for (int i = 0; i < count; i++) {
  105. al.dataArray[i] = value;
  106. }
  107. al.count = count;
  108. return al;
  109. }
  110. [Serializable]
  111. private class ListWrapper : ArrayList {
  112. IList list;
  113. public ListWrapper (IList list) {
  114. if (null == list)
  115. throw new ArgumentNullException();
  116. this.list = list;
  117. count = ((ICollection) list).Count;
  118. }
  119. // ArrayList
  120. [MonoTODO]
  121. public override int Capacity {
  122. get { return list.Count; }
  123. set { throw new NotSupportedException (); }
  124. }
  125. [MonoTODO]
  126. public override void AddRange (ICollection collection) {
  127. if (collection == null)
  128. throw new ArgumentNullException ("colllection");
  129. if (IsFixedSize || IsReadOnly)
  130. throw new NotSupportedException ();
  131. }
  132. [MonoTODO]
  133. public override int BinarySearch (object value) {
  134. throw new NotImplementedException ();
  135. }
  136. [MonoTODO]
  137. public override int BinarySearch (object value, IComparer comparer) {
  138. throw new NotImplementedException ();
  139. }
  140. [MonoTODO]
  141. public override int BinarySearch (int index, int count, object value,
  142. IComparer comparer) {
  143. throw new NotImplementedException ();
  144. }
  145. public override void CopyTo (Array array) {
  146. if (null == array)
  147. throw new ArgumentNullException("array");
  148. if (array.Rank > 1)
  149. throw new ArgumentException("array cannot be multidimensional");
  150. CopyTo (array, 0);
  151. }
  152. [MonoTODO]
  153. public override void CopyTo (int index, Array array,
  154. int arrayIndex, int count) {
  155. if (array == null)
  156. throw new ArgumentNullException ();
  157. if (index < 0 || arrayIndex < 0 || count < 0)
  158. throw new ArgumentOutOfRangeException ();
  159. if (array.Rank > 1 || index >= Count || Count > (array.Length - arrayIndex))
  160. throw new ArgumentException ();
  161. // FIXME: handle casting error here
  162. }
  163. public override ArrayList GetRange (int index, int count) {
  164. if (index < 0 || count < 0)
  165. throw new ArgumentOutOfRangeException ();
  166. if (Count < (index + count))
  167. throw new ArgumentException ();
  168. ArrayList result = new ArrayList (count);
  169. for (int i = 0; i < count; i++)
  170. result.Add (list [i]);
  171. return result;
  172. }
  173. [MonoTODO]
  174. public override void InsertRange (int index, ICollection col) {
  175. if (col == null)
  176. throw new ArgumentNullException ();
  177. if (index < 0 || index > Count)
  178. throw new ArgumentOutOfRangeException ();
  179. if (IsReadOnly || IsFixedSize)
  180. throw new NotSupportedException ();
  181. if (index == Count) {
  182. foreach (object element in col)
  183. list.Add (element);
  184. } //else if ((index + count) < Count) {
  185. // for (int i = index; i < (index + count); i++)
  186. // list [i] = col [i];
  187. // } else {
  188. // int added = Count - (index + count);
  189. // for (int i = index; i < Count; i++)
  190. // list [i] = col [i];
  191. // for (int i = 0; i < added; i++)
  192. // list.Add (col [Count +i]);
  193. // }
  194. }
  195. public override int LastIndexOf (object value) {
  196. return LastIndexOf (value, Count, 0);
  197. }
  198. public override int LastIndexOf (object value, int startIndex) {
  199. return LastIndexOf (value, startIndex, 0);
  200. }
  201. public override int LastIndexOf (object value, int startIndex, int count) {
  202. if (null == value){
  203. return -1;
  204. }
  205. if (startIndex > Count || count < 0 || (startIndex + count > Count))
  206. throw new ArgumentOutOfRangeException ();
  207. int length = startIndex - count + 1;
  208. for (int i = startIndex; i >= length; i--)
  209. if (list [i] == value)
  210. return i;
  211. return -1;
  212. }
  213. public override void RemoveRange (int index, int count) {
  214. if ((index < 0) || (count < 0))
  215. throw new ArgumentOutOfRangeException ();
  216. if ((index > Count) || (index + count) > Count)
  217. throw new ArgumentException ();
  218. if (IsReadOnly || IsFixedSize)
  219. throw new NotSupportedException ();
  220. for (int i = 0; i < count; i++)
  221. list.RemoveAt (index);
  222. }
  223. public override void Reverse () {
  224. Reverse (0, Count);
  225. }
  226. public override void Reverse (int index, int count) {
  227. if ((index < 0) || (count < 0))
  228. throw new ArgumentOutOfRangeException ();
  229. if ((index > Count) || (index + count) > Count)
  230. throw new ArgumentException ();
  231. if (IsReadOnly)
  232. throw new NotSupportedException ();
  233. object tmp = null;
  234. for (int i = index; i < count; i++) {
  235. tmp = list [i];
  236. list [i] = list [count - i];
  237. list [count - i] = tmp;
  238. }
  239. }
  240. public override void SetRange (int index, ICollection col) {
  241. if (index < 0 || (index + col.Count) > Count)
  242. throw new ArgumentOutOfRangeException ();
  243. if (col == null)
  244. throw new ArgumentNullException ();
  245. if (IsReadOnly)
  246. throw new NotSupportedException ();
  247. for (int i = index; i < col.Count; i++)
  248. foreach (object o in col)
  249. list [i] = o;
  250. }
  251. [MonoTODO]
  252. public override void Sort () {
  253. }
  254. [MonoTODO]
  255. public override void Sort (IComparer comparer) {
  256. }
  257. [MonoTODO]
  258. public override void Sort (int index, int count, IComparer comparer) {
  259. }
  260. public override object [] ToArray () {
  261. return (object []) ToArray (typeof (object));
  262. }
  263. public override Array ToArray (Type type) {
  264. int count = Count;
  265. Array result = Array.CreateInstance (type, count);
  266. for (int i = 0; i < count; i++)
  267. result.SetValue (list [i], i);
  268. return result;
  269. }
  270. [MonoTODO]
  271. public override void TrimToSize () {
  272. }
  273. // IList
  274. public override bool IsFixedSize {
  275. get { return list.IsFixedSize; }
  276. }
  277. public override bool IsReadOnly {
  278. get { return list.IsReadOnly; }
  279. }
  280. public override object this [int index] {
  281. get { return list [index]; }
  282. set { list [index] = value; }
  283. }
  284. public override int Add (object value) {
  285. return list.Add (value);
  286. }
  287. public override void Clear () {
  288. list.Clear ();
  289. }
  290. public override bool Contains (object value) {
  291. return list.Contains (value);
  292. }
  293. public override int IndexOf (object value) {
  294. return list.IndexOf (value);
  295. }
  296. public override void Insert (int index, object value) {
  297. list.Insert (index, value);
  298. }
  299. public override void Remove (object value) {
  300. list.Remove (value);
  301. }
  302. public override void RemoveAt (int index) {
  303. list.RemoveAt (index);
  304. }
  305. // ICollection
  306. public override int Count {
  307. get { return count; }
  308. }
  309. public override bool IsSynchronized {
  310. get { return ((ICollection) list).IsSynchronized; }
  311. }
  312. public override object SyncRoot {
  313. get { return ((ICollection) list).SyncRoot; }
  314. }
  315. public override void CopyTo (Array array, int index) {
  316. ((ICollection) list).CopyTo (array, index);
  317. }
  318. // ICloneable
  319. public override object Clone () {
  320. return new ListWrapper (list);
  321. }
  322. // IEnumerable
  323. public override IEnumerator GetEnumerator () {
  324. return ((IEnumerable) list).GetEnumerator ();
  325. }
  326. }
  327. [MonoTODO]
  328. public static ArrayList Adapter (IList list) {
  329. return new ListWrapper (list);
  330. }
  331. // properties
  332. private bool fixedSize = false;
  333. private bool readOnly = false;
  334. private bool synchronized = false;
  335. private long version = 0;
  336. private ArrayList source = null;
  337. private const int defaultCapacity = 16;
  338. private void copyDataArray (object[] outArray) {
  339. for (int i = 0; i < count; i++) {
  340. outArray[i] = dataArray[i];
  341. }
  342. }
  343. private void setSize (int newSize) {
  344. if (newSize == capacity)
  345. return;
  346. capacity = (newSize == 0) ? defaultCapacity : newSize;
  347. // note that this assumes that we've already sanity-checked
  348. // the new size
  349. object[] newDataArray = new object[capacity];
  350. copyDataArray (newDataArray);
  351. dataArray = newDataArray;
  352. }
  353. // note that this DOES NOT update count
  354. private void shiftElements (int startIndex, int numshift) {
  355. if (numshift == 0) {
  356. return;
  357. }
  358. if (count + numshift > capacity) {
  359. setSize (capacity * 2);
  360. shiftElements (startIndex, numshift);
  361. } else {
  362. if (numshift > 0) {
  363. int numelts = count - startIndex;
  364. for (int i = numelts-1; i >= 0; i--) {
  365. dataArray[startIndex + numshift + i] = dataArray[startIndex + i];
  366. }
  367. for (int i = startIndex; i < startIndex + numshift; i++) {
  368. dataArray[i] = null;
  369. }
  370. } else {
  371. int numelts = count - startIndex + numshift;
  372. for (int i = 0; i < numelts; i++) {
  373. dataArray [i + startIndex] = dataArray [i + startIndex - numshift];
  374. }
  375. for (int i = count + numshift; i < count; i++) {
  376. dataArray[i] = null;
  377. }
  378. }
  379. }
  380. }
  381. public virtual int Capacity {
  382. get {
  383. return capacity;
  384. }
  385. set {
  386. if (readOnly) {
  387. throw new NotSupportedException
  388. ("Collection is read-only.");
  389. }
  390. if (value < count) {
  391. throw new ArgumentOutOfRangeException
  392. ("ArrayList Capacity being set to less than Count");
  393. }
  394. if (fixedSize && value != capacity) {
  395. throw new NotSupportedException
  396. ("Collection is fixed size.");
  397. }
  398. setSize (value);
  399. }
  400. }
  401. private void CheckSourceVersion() {
  402. if (null != this.source && this.version != this.source.version) {
  403. throw new InvalidOperationException();
  404. }
  405. }
  406. public virtual int Count {
  407. get {
  408. CheckSourceVersion();
  409. return count;
  410. }
  411. }
  412. public virtual bool IsFixedSize {
  413. get {
  414. return fixedSize;
  415. }
  416. }
  417. public virtual bool IsReadOnly {
  418. get {
  419. return readOnly;
  420. }
  421. }
  422. public virtual bool IsSynchronized {
  423. get {
  424. return synchronized;
  425. }
  426. }
  427. public virtual object this[int index] {
  428. get {
  429. CheckSourceVersion();
  430. if (index < 0) {
  431. throw new ArgumentOutOfRangeException ("index < 0");
  432. }
  433. if (index >= count) {
  434. throw new ArgumentOutOfRangeException ("index out of range");
  435. }
  436. return dataArray[index];
  437. }
  438. set {
  439. if (index < 0) {
  440. throw new ArgumentOutOfRangeException ("index < 0");
  441. }
  442. if (index >= count) {
  443. throw new ArgumentOutOfRangeException ("index out of range");
  444. }
  445. if (readOnly) {
  446. throw new NotSupportedException ("Collection is read-only.");
  447. }
  448. dataArray[index] = value;
  449. version++;
  450. }
  451. }
  452. public virtual object SyncRoot {
  453. get {
  454. return this;
  455. }
  456. }
  457. // methods
  458. public virtual int Add (object value) {
  459. if (readOnly)
  460. throw new NotSupportedException ("ArrayList is read-only.");
  461. if (fixedSize)
  462. throw new NotSupportedException ("ArrayList is fixed size.");
  463. if (count + 1 >= capacity)
  464. setSize (capacity * 2);
  465. dataArray[count] = value;
  466. version++;
  467. return count++;
  468. }
  469. public virtual void AddRange (ICollection c) {
  470. if (null == c)
  471. throw new ArgumentNullException ("c");
  472. if (readOnly || fixedSize)
  473. throw new NotSupportedException ();
  474. int cc = c.Count;
  475. if (count + cc >= capacity)
  476. Capacity = cc < count? count * 2: count + cc + 1;
  477. c.CopyTo (dataArray, count);
  478. count += cc;
  479. version++;
  480. }
  481. public virtual int BinarySearch (object value) {
  482. return BinarySearch (0, count, value, null);
  483. }
  484. public virtual int BinarySearch (object value, IComparer comparer) {
  485. return BinarySearch (0, count, value, comparer);
  486. }
  487. public virtual int BinarySearch (int index, int count,
  488. object value, IComparer comparer) {
  489. return Array.BinarySearch (dataArray, index, count, value, comparer);
  490. }
  491. public virtual void Clear () {
  492. if (readOnly || fixedSize)
  493. throw new NotSupportedException();
  494. count = 0;
  495. version++;
  496. }
  497. public virtual object Clone () {
  498. return new ArrayList (dataArray, count, capacity,
  499. fixedSize, readOnly, synchronized);
  500. }
  501. public virtual bool Contains (object item) {
  502. for (int i = 0; i < count; i++) {
  503. if (Object.Equals (dataArray[i], item)) {
  504. return true;
  505. }
  506. }
  507. return false;
  508. }
  509. public virtual void CopyTo (Array array) {
  510. if (null == array)
  511. throw new ArgumentNullException("array");
  512. if (array.Rank > 1)
  513. throw new ArgumentException("array cannot be multidimensional");
  514. Array.Copy (dataArray, 0, array, 0, this.count);
  515. }
  516. public virtual void CopyTo (Array array, int arrayIndex) {
  517. if (null == array)
  518. throw new ArgumentNullException("array");
  519. if (arrayIndex < 0)
  520. throw new ArgumentOutOfRangeException("arrayIndex");
  521. if (array.Rank > 1)
  522. throw new ArgumentException("array cannot be multidimensional");
  523. if (this.count > array.Length - arrayIndex)
  524. throw new ArgumentException("this ArrayList has more items than the space available in array from arrayIndex to the end of array");
  525. Array.Copy (dataArray, 0, array, arrayIndex, this.count);
  526. }
  527. public virtual void CopyTo (int index, Array array,
  528. int arrayIndex, int count) {
  529. if (null == array)
  530. throw new ArgumentNullException("array");
  531. if (arrayIndex < 0)
  532. throw new ArgumentOutOfRangeException("arrayIndex");
  533. if (index < 0)
  534. throw new ArgumentOutOfRangeException("index");
  535. if (count < 0)
  536. throw new ArgumentOutOfRangeException("count");
  537. if (index >= this.count)
  538. throw new ArgumentException("index is greater than or equal to the source ArrayList.Count");
  539. if (array.Rank > 1)
  540. throw new ArgumentException("array cannot be multidimensional");
  541. if (arrayIndex >= array.Length)
  542. throw new ArgumentException("arrayIndex is greater than or equal to array's length");
  543. if (this.count > array.Length - arrayIndex)
  544. throw new ArgumentException("this ArrayList has more items than the space available in array from arrayIndex to the end of array");
  545. Array.Copy (dataArray, index, array, arrayIndex, count);
  546. }
  547. [Serializable]
  548. private class ArrayListEnumerator : IEnumerator, ICloneable {
  549. private object[] data;
  550. private int idx;
  551. private int start;
  552. private int num;
  553. private ArrayList enumeratee;
  554. private long version;
  555. internal ArrayListEnumerator(int index, int count, object[] items, ArrayList al, long ver) {
  556. data = items;
  557. start = index;
  558. num = count;
  559. idx = start - 1;
  560. enumeratee = al;
  561. version = ver;
  562. }
  563. public object Clone () {
  564. return new ArrayListEnumerator (start, num, data, enumeratee, version);
  565. }
  566. public virtual object Current {
  567. get {
  568. return data [idx];
  569. }
  570. }
  571. public virtual bool MoveNext() {
  572. if (enumeratee.version != version)
  573. throw new InvalidOperationException();
  574. if (++idx < start + num)
  575. return true;
  576. return false;
  577. }
  578. public virtual void Reset() {
  579. idx = start - 1;
  580. }
  581. }
  582. public virtual IEnumerator GetEnumerator () {
  583. return new ArrayListEnumerator(0, this.Count, dataArray, this, this.version);
  584. }
  585. private void ValidateRange(int index, int count) {
  586. if (index < 0) {
  587. throw new ArgumentOutOfRangeException("index", index, "Must be equal to or greater than zero");
  588. }
  589. if (count < 0) {
  590. throw new ArgumentOutOfRangeException("count", count, "Must be equal to or greater than zero");
  591. }
  592. if (index > this.count - 1) {
  593. throw new ArgumentException();
  594. }
  595. if (index + count > this.count - 1) {
  596. throw new ArgumentException();
  597. }
  598. }
  599. public virtual IEnumerator GetEnumerator (int index, int count) {
  600. ValidateRange(index, count);
  601. return new ArrayListEnumerator(index, count, dataArray, this, this.version);
  602. }
  603. public virtual ArrayList GetRange (int index, int count) {
  604. ValidateRange(index, count);
  605. ArrayList retVal = new ArrayList(count);
  606. for (int i = index; i < count + index; i++) {
  607. retVal.Add(this[i]);
  608. }
  609. retVal.version = this.version;
  610. retVal.source = this;
  611. return retVal;
  612. }
  613. public virtual int IndexOf (object value) {
  614. return IndexOf (value, 0, count);
  615. }
  616. public virtual int IndexOf (object value, int startIndex) {
  617. return IndexOf (value, startIndex, count - startIndex);
  618. }
  619. public virtual int IndexOf (object value, int startIndex, int count) {
  620. if (startIndex < 0 || startIndex + count > this.count || count < 0) {
  621. throw new ArgumentOutOfRangeException ("IndexOf arguments out of range");
  622. }
  623. for (int i = startIndex; i < (startIndex + count); i++) {
  624. if (Object.Equals (dataArray[i], value)) {
  625. return i;
  626. }
  627. }
  628. return -1;
  629. }
  630. public virtual void Insert (int index, object value) {
  631. if (readOnly) {
  632. throw new NotSupportedException
  633. ("Collection is read-only.");
  634. }
  635. if (fixedSize) {
  636. throw new NotSupportedException
  637. ("Collection is fixed size.");
  638. }
  639. if (index < 0 || index > count) {
  640. throw new ArgumentOutOfRangeException ("index < 0 or index >= capacity");
  641. }
  642. shiftElements (index, 1);
  643. dataArray[index] = value;
  644. count++;
  645. version++;
  646. }
  647. public virtual void InsertRange (int index, ICollection c) {
  648. if (c == null)
  649. throw new ArgumentNullException ();
  650. if (index < 0 || index > count)
  651. throw new ArgumentOutOfRangeException ();
  652. if (IsReadOnly || IsFixedSize)
  653. throw new NotSupportedException ();
  654. // Get a copy of the collection before the shift in case the collection
  655. // is this. Otherwise the enumerator will be confused.
  656. Array source = Array.CreateInstance(typeof(object), c.Count);
  657. c.CopyTo(source, 0);
  658. shiftElements (index, c.Count);
  659. count += c.Count;
  660. foreach (object o in source)
  661. dataArray[index++] = o;
  662. version++;
  663. }
  664. public virtual int LastIndexOf (object value) {
  665. return LastIndexOf (value, count - 1, count);
  666. }
  667. public virtual int LastIndexOf (object value, int startIndex) {
  668. if (startIndex < 0 || startIndex > count - 1) {
  669. throw new ArgumentOutOfRangeException("startIndex", startIndex, "");
  670. }
  671. return LastIndexOf (value, startIndex, startIndex + 1);
  672. }
  673. public virtual int LastIndexOf (object value, int startIndex,
  674. int count) {
  675. if (null == value){
  676. return -1;
  677. }
  678. if (startIndex >= this.count)
  679. throw new ArgumentOutOfRangeException ("startIndex >= Count");
  680. if (count < 0)
  681. throw new ArgumentOutOfRangeException ("count < 0");
  682. if (startIndex + 1 < count)
  683. throw new ArgumentOutOfRangeException ("startIndex + 1 < count");
  684. int EndIndex = startIndex - count + 1;
  685. for (int i = startIndex; i >= EndIndex; i--) {
  686. if (Object.Equals (dataArray[i], value)) {
  687. return i;
  688. }
  689. }
  690. return -1;
  691. }
  692. public virtual void Remove (object obj) {
  693. if (IsFixedSize || IsReadOnly)
  694. throw new NotSupportedException ();
  695. int objIndex = IndexOf (obj);
  696. if (objIndex == -1) {
  697. // shouldn't an exception be thrown here??
  698. // the MS docs don't indicate one, and testing
  699. // with the MS .net framework doesn't indicate one
  700. return;
  701. }
  702. RemoveRange (objIndex, 1);
  703. }
  704. public virtual void RemoveAt (int index) {
  705. if (readOnly) {
  706. throw new NotSupportedException
  707. ("Collection is read-only.");
  708. }
  709. if (fixedSize) {
  710. throw new NotSupportedException
  711. ("Collection is fixed size.");
  712. }
  713. if ((index < 0) || (index >= this.count))
  714. throw new ArgumentOutOfRangeException ("index", "Index was out of range. Must be non-negative and less than the size of the collection.");
  715. shiftElements (index, -1);
  716. this.count -= 1;
  717. version ++;
  718. }
  719. public virtual void RemoveRange (int index, int count) {
  720. if (readOnly) {
  721. throw new NotSupportedException
  722. ("Collection is read-only.");
  723. }
  724. if (fixedSize) {
  725. throw new NotSupportedException
  726. ("Collection is fixed size.");
  727. }
  728. if (index < 0)
  729. throw new ArgumentOutOfRangeException ("index", "Non-negative number required.");
  730. if (count < 0)
  731. throw new ArgumentOutOfRangeException ("count", "Non-negative number required.");
  732. if (index + count > this.count)
  733. throw new ArgumentException ("Offset and length were out of bounds for the array " +
  734. "or count is greater than the number of elements from index " +
  735. "to the end of the source collection.");
  736. shiftElements (index, - count);
  737. this.count -= count;
  738. version++;
  739. }
  740. public virtual void Reverse () {
  741. Reverse (0, count);
  742. }
  743. public virtual void Reverse (int index, int count) {
  744. if (readOnly) {
  745. throw new NotSupportedException
  746. ("Collection is read-only.");
  747. }
  748. if (index < 0 || index + count > this.count) {
  749. throw new ArgumentOutOfRangeException
  750. ("index/count out of range");
  751. }
  752. Array.Reverse (dataArray, index, count);
  753. version++;
  754. }
  755. public virtual void SetRange (int index, ICollection c) {
  756. if (c == null)
  757. throw new ArgumentNullException ();
  758. if (readOnly)
  759. throw new NotSupportedException ();
  760. if (index < 0 || (index + c.Count) > count)
  761. throw new ArgumentOutOfRangeException ();
  762. c.CopyTo(dataArray, index);
  763. }
  764. public virtual void Sort () {
  765. Sort (0, count, null);
  766. }
  767. public virtual void Sort (IComparer comparer) {
  768. Sort (0, count, comparer);
  769. }
  770. public virtual void Sort (int index, int count, IComparer comparer) {
  771. if (readOnly) {
  772. throw new NotSupportedException
  773. ("Collection is read-only.");
  774. }
  775. if (index < 0 || index + count > this.count) {
  776. throw new ArgumentOutOfRangeException
  777. ("index/count out of range");
  778. }
  779. Array.Sort (dataArray, index, count, comparer);
  780. version++;
  781. }
  782. public virtual object[] ToArray() {
  783. object[] outArray = new object[count];
  784. Array.Copy (dataArray, outArray, count);
  785. return outArray;
  786. }
  787. public virtual Array ToArray (Type type) {
  788. Array outArray = Array.CreateInstance (type, count);
  789. Array.Copy (dataArray, outArray, count);
  790. return outArray;
  791. }
  792. public virtual void TrimToSize () {
  793. if (IsReadOnly || IsFixedSize)
  794. throw new NotSupportedException ();
  795. setSize(count);
  796. version++;
  797. }
  798. private class SyncArrayList : ArrayList {
  799. private ArrayList _list;
  800. // constructors
  801. public SyncArrayList(ArrayList list) {
  802. _list = list;
  803. }
  804. // properties
  805. public override int Capacity {
  806. get {
  807. lock (_list.SyncRoot) {
  808. return _list.Capacity;
  809. }
  810. }
  811. set {
  812. lock (_list.SyncRoot) {
  813. _list.Capacity = value;
  814. }
  815. }
  816. }
  817. public override int Count {
  818. get {
  819. lock (_list.SyncRoot) {
  820. return _list.Count;
  821. }
  822. }
  823. }
  824. public override bool IsFixedSize {
  825. get {
  826. lock (_list.SyncRoot) {
  827. return _list.IsFixedSize;
  828. }
  829. }
  830. }
  831. public override bool IsReadOnly {
  832. get {
  833. lock (_list.SyncRoot) {
  834. return _list.IsReadOnly;
  835. }
  836. }
  837. }
  838. public override bool IsSynchronized {
  839. get {
  840. lock (_list.SyncRoot) {
  841. return _list.IsSynchronized;
  842. }
  843. }
  844. }
  845. public override object this[int index] {
  846. get {
  847. lock (_list.SyncRoot) {
  848. return _list[index];
  849. }
  850. }
  851. set {
  852. lock (_list.SyncRoot) {
  853. _list[index] = value;
  854. }
  855. }
  856. }
  857. // methods
  858. public override int Add (object value) {
  859. lock (_list.SyncRoot) {
  860. return _list.Add(value);
  861. }
  862. }
  863. public override void AddRange (ICollection c) {
  864. lock (_list.SyncRoot) {
  865. _list.AddRange(c);
  866. }
  867. }
  868. public override int BinarySearch (int index, int count, object value, IComparer comparer) {
  869. lock (_list.SyncRoot) {
  870. return Array.BinarySearch (dataArray, index, count, value, comparer);
  871. }
  872. }
  873. public override void Clear () {
  874. lock (_list.SyncRoot) {
  875. _list.Clear();
  876. }
  877. }
  878. public override object Clone () {
  879. lock (_list.SyncRoot) {
  880. return new SyncArrayList((ArrayList) _list.Clone());
  881. }
  882. }
  883. public override bool Contains (object item) {
  884. lock (_list.SyncRoot) {
  885. return _list.Contains(item);
  886. }
  887. }
  888. public override void CopyTo (Array array) {
  889. lock (_list.SyncRoot) {
  890. _list.CopyTo(array);
  891. }
  892. }
  893. public override void CopyTo (Array array, int arrayIndex) {
  894. lock (_list.SyncRoot) {
  895. _list.CopyTo(array, arrayIndex);
  896. }
  897. }
  898. public override void CopyTo (int index, Array array, int arrayIndex, int count) {
  899. lock (_list.SyncRoot) {
  900. _list.CopyTo(index, array, arrayIndex, count);
  901. }
  902. }
  903. public override IEnumerator GetEnumerator () {
  904. lock (_list.SyncRoot) {
  905. return _list.GetEnumerator();
  906. }
  907. }
  908. public override IEnumerator GetEnumerator (int index, int count) {
  909. lock (_list.SyncRoot) {
  910. return _list.GetEnumerator(index, count);
  911. }
  912. }
  913. public override ArrayList GetRange (int index, int count) {
  914. lock (_list.SyncRoot) {
  915. return new SyncArrayList(_list.GetRange(index, count));
  916. }
  917. }
  918. public override int IndexOf (object value, int startIndex, int count) {
  919. lock (_list.SyncRoot) {
  920. return _list.IndexOf(value, startIndex, count);
  921. }
  922. }
  923. public override void Insert (int index, object value) {
  924. lock (_list.SyncRoot) {
  925. _list.Insert(index, value);
  926. }
  927. }
  928. public override void InsertRange (int index, ICollection c) {
  929. lock (_list.SyncRoot) {
  930. _list.InsertRange(index, c);
  931. }
  932. }
  933. public override int LastIndexOf (object value, int startIndex) {
  934. lock (_list.SyncRoot) {
  935. return _list.LastIndexOf(value, startIndex);
  936. }
  937. }
  938. public override int LastIndexOf (object value, int startIndex, int count) {
  939. lock (_list.SyncRoot) {
  940. return _list.LastIndexOf(value, startIndex, count);
  941. }
  942. }
  943. public override void Remove (object obj) {
  944. lock (_list.SyncRoot) {
  945. _list.Remove(obj);
  946. }
  947. }
  948. public override void RemoveRange (int index, int count) {
  949. lock (_list.SyncRoot) {
  950. _list.RemoveRange(index, count);
  951. }
  952. }
  953. public override void Reverse (int index, int count) {
  954. lock (_list.SyncRoot) {
  955. _list.Reverse(index, count);
  956. }
  957. }
  958. public override void SetRange (int index, ICollection c) {
  959. lock (_list.SyncRoot) {
  960. _list.SetRange(index, c);
  961. }
  962. }
  963. public override void Sort (int index, int count, IComparer comparer) {
  964. lock (_list.SyncRoot) {
  965. _list.Sort(index, count, comparer);
  966. }
  967. }
  968. public override object[] ToArray() {
  969. lock (_list.SyncRoot) {
  970. return _list.ToArray();
  971. }
  972. }
  973. public override Array ToArray (Type type) {
  974. lock (_list.SyncRoot) {
  975. return _list.ToArray(type);
  976. }
  977. }
  978. public override void TrimToSize () {
  979. lock (_list.SyncRoot) {
  980. _list.TrimToSize();
  981. }
  982. }
  983. }
  984. }
  985. }