ArrayList.cs 24 KB

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