PageRenderTime 66ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/corlib/System.Collections/ArrayList.cs

https://bitbucket.org/foobar22/mono
C# | 3378 lines | 2540 code | 639 blank | 199 comment | 196 complexity | 740e609e85613bf21d57b33ab600be54 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0, Unlicense, Apache-2.0, LGPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. // ArrayList.cs
  2. //
  3. // Implementation of the ECMA ArrayList.
  4. //
  5. // Copyright (c) 2003 Thong (Tum) Nguyen [tum@veridicus.com]
  6. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. using System.Runtime.InteropServices;
  28. using System.Diagnostics;
  29. namespace System.Collections
  30. {
  31. [Serializable]
  32. #if INSIDE_CORLIB
  33. [ComVisible(true)]
  34. [DebuggerDisplay ("Count={Count}")]
  35. [DebuggerTypeProxy (typeof (CollectionDebuggerView))]
  36. public class ArrayList : IList, ICloneable, ICollection, IEnumerable {
  37. #else
  38. internal class ArrayList : IList {
  39. #endif
  40. #region Enumerator
  41. private sealed class ArrayListEnumerator
  42. : IEnumerator, ICloneable
  43. {
  44. private object m_Current;
  45. private ArrayList m_List;
  46. private int m_Pos;
  47. private int m_Index;
  48. private int m_Count;
  49. private int m_ExpectedStateChanges;
  50. public ArrayListEnumerator(ArrayList list)
  51. : this(list, 0, list.Count)
  52. {
  53. }
  54. public object Clone()
  55. {
  56. return this.MemberwiseClone();
  57. }
  58. public ArrayListEnumerator(ArrayList list, int index, int count)
  59. {
  60. m_List = list;
  61. m_Index = index;
  62. m_Count = count;
  63. m_Pos = m_Index - 1;
  64. m_Current = null;
  65. m_ExpectedStateChanges = list._version;
  66. }
  67. public object Current
  68. {
  69. get
  70. {
  71. if (m_Pos == m_Index - 1) {
  72. throw new InvalidOperationException("Enumerator unusable (Reset pending, or past end of array.");
  73. }
  74. return m_Current;
  75. }
  76. }
  77. public bool MoveNext()
  78. {
  79. if (m_List._version != m_ExpectedStateChanges)
  80. {
  81. throw new InvalidOperationException("List has changed.");
  82. }
  83. m_Pos++;
  84. if (m_Pos - m_Index < m_Count)
  85. {
  86. m_Current = m_List[m_Pos];
  87. return true;
  88. }
  89. return false;
  90. }
  91. public void Reset()
  92. {
  93. m_Current = null;
  94. m_Pos = m_Index - 1;
  95. }
  96. }
  97. sealed class SimpleEnumerator : IEnumerator, ICloneable
  98. {
  99. ArrayList list;
  100. object currentElement;
  101. int index;
  102. int version;
  103. static readonly object endFlag = new object ();
  104. public SimpleEnumerator (ArrayList list)
  105. {
  106. this.list = list;
  107. index = -1;
  108. version = list._version;
  109. currentElement = endFlag;
  110. }
  111. public object Clone ()
  112. {
  113. return MemberwiseClone ();
  114. }
  115. public bool MoveNext ()
  116. {
  117. if (version != list._version)
  118. throw new InvalidOperationException("List has changed.");
  119. if (++index < list.Count) {
  120. currentElement = list [index];
  121. return true;
  122. } else {
  123. currentElement = endFlag;
  124. return false;
  125. }
  126. }
  127. public object Current {
  128. get {
  129. if (currentElement == endFlag) {
  130. if (index == -1)
  131. throw new InvalidOperationException ("Enumerator not started");
  132. else
  133. throw new InvalidOperationException ("Enumerator ended");
  134. }
  135. return currentElement;
  136. }
  137. }
  138. public void Reset ()
  139. {
  140. if (version != list._version)
  141. throw new InvalidOperationException ("List has changed.");
  142. currentElement = endFlag;
  143. index = -1;
  144. }
  145. }
  146. #endregion
  147. #region ArrayListAdapter
  148. /// <summary>
  149. /// Adapts various ILists into an ArrayList.
  150. /// </summary>
  151. [Serializable]
  152. private sealed class ArrayListAdapter
  153. : ArrayList
  154. {
  155. private sealed class EnumeratorWithRange
  156. : IEnumerator, ICloneable
  157. {
  158. private int m_StartIndex;
  159. private int m_Count;
  160. private int m_MaxCount;
  161. private IEnumerator m_Enumerator;
  162. public EnumeratorWithRange(IEnumerator enumerator, int index, int count)
  163. {
  164. m_Count = 0;
  165. m_StartIndex = index;
  166. m_MaxCount = count;
  167. m_Enumerator = enumerator;
  168. Reset();
  169. }
  170. public object Clone()
  171. {
  172. return this.MemberwiseClone();
  173. }
  174. public object Current
  175. {
  176. get
  177. {
  178. return m_Enumerator.Current;
  179. }
  180. }
  181. public bool MoveNext()
  182. {
  183. if (m_Count >= m_MaxCount)
  184. {
  185. return false;
  186. }
  187. m_Count++;
  188. return m_Enumerator.MoveNext();
  189. }
  190. public void Reset()
  191. {
  192. m_Count = 0;
  193. m_Enumerator.Reset();
  194. for (int i = 0; i < m_StartIndex; i++)
  195. {
  196. m_Enumerator.MoveNext();
  197. }
  198. }
  199. }
  200. private IList m_Adaptee;
  201. public ArrayListAdapter(IList adaptee)
  202. : base(0, true)
  203. {
  204. m_Adaptee = adaptee;
  205. }
  206. public override object this[int index]
  207. {
  208. get
  209. {
  210. return m_Adaptee[index];
  211. }
  212. set
  213. {
  214. m_Adaptee[index] = value;
  215. }
  216. }
  217. public override int Count
  218. {
  219. get
  220. {
  221. return m_Adaptee.Count;
  222. }
  223. }
  224. public override int Capacity
  225. {
  226. get
  227. {
  228. return m_Adaptee.Count;
  229. }
  230. set
  231. {
  232. if (value < m_Adaptee.Count)
  233. {
  234. throw new ArgumentException("capacity");
  235. }
  236. }
  237. }
  238. public override bool IsFixedSize
  239. {
  240. get
  241. {
  242. return m_Adaptee.IsFixedSize;
  243. }
  244. }
  245. public override bool IsReadOnly
  246. {
  247. get
  248. {
  249. return m_Adaptee.IsReadOnly;
  250. }
  251. }
  252. public override object SyncRoot
  253. {
  254. get
  255. {
  256. return m_Adaptee.SyncRoot;
  257. }
  258. }
  259. public override int Add(object value)
  260. {
  261. return m_Adaptee.Add(value);
  262. }
  263. public override void Clear()
  264. {
  265. m_Adaptee.Clear();
  266. }
  267. public override bool Contains(object value)
  268. {
  269. return m_Adaptee.Contains(value);
  270. }
  271. public override int IndexOf(object value)
  272. {
  273. return m_Adaptee.IndexOf(value);
  274. }
  275. public override int IndexOf(object value, int startIndex)
  276. {
  277. return IndexOf(value, startIndex, m_Adaptee.Count - startIndex);
  278. }
  279. public override int IndexOf(object value, int startIndex, int count)
  280. {
  281. if (startIndex < 0 || startIndex > m_Adaptee.Count)
  282. {
  283. ThrowNewArgumentOutOfRangeException ("startIndex", startIndex,
  284. "Does not specify valid index.");
  285. }
  286. if (count < 0)
  287. {
  288. ThrowNewArgumentOutOfRangeException ("count", count,
  289. "Can't be less than 0.");
  290. }
  291. // re-ordered to avoid possible integer overflow
  292. if (startIndex > m_Adaptee.Count - count) {
  293. // LAMESPEC: Every other method throws ArgumentException
  294. throw new ArgumentOutOfRangeException("count",
  295. "Start index and count do not specify a valid range.");
  296. }
  297. if (value == null)
  298. {
  299. for (int i = startIndex; i < startIndex + count; i++)
  300. {
  301. if (m_Adaptee[i] == null)
  302. {
  303. return i;
  304. }
  305. }
  306. }
  307. else
  308. {
  309. for (int i = startIndex; i < startIndex + count; i++)
  310. {
  311. if (value.Equals(m_Adaptee[i]))
  312. {
  313. return i;
  314. }
  315. }
  316. }
  317. return -1;
  318. }
  319. public override int LastIndexOf(object value)
  320. {
  321. return LastIndexOf(value, m_Adaptee.Count - 1);
  322. }
  323. public override int LastIndexOf(object value, int startIndex)
  324. {
  325. return LastIndexOf(value, startIndex, startIndex + 1);
  326. }
  327. public override int LastIndexOf(object value, int startIndex, int count)
  328. {
  329. if (startIndex < 0)
  330. {
  331. ThrowNewArgumentOutOfRangeException ("startIndex", startIndex, "< 0");
  332. }
  333. if (count < 0)
  334. {
  335. ThrowNewArgumentOutOfRangeException ("count", count, "count is negative.");
  336. }
  337. if (startIndex - count + 1 < 0)
  338. {
  339. ThrowNewArgumentOutOfRangeException ("count", count, "count is too large.");
  340. }
  341. if (value == null)
  342. {
  343. for (int i = startIndex; i > startIndex - count; i--)
  344. {
  345. if (m_Adaptee[i] == null)
  346. {
  347. return i;
  348. }
  349. }
  350. }
  351. else
  352. {
  353. for (int i = startIndex; i > startIndex - count; i--)
  354. {
  355. if (value.Equals(m_Adaptee[i]))
  356. {
  357. return i;
  358. }
  359. }
  360. }
  361. return -1;
  362. }
  363. public override void Insert(int index, object value)
  364. {
  365. m_Adaptee.Insert(index, value);
  366. }
  367. public override void InsertRange(int index, ICollection c)
  368. {
  369. if (c == null)
  370. {
  371. throw new ArgumentNullException("c");
  372. }
  373. if (index > m_Adaptee.Count)
  374. {
  375. ThrowNewArgumentOutOfRangeException ("index", index,
  376. "Index must be >= 0 and <= Count.");
  377. }
  378. foreach (object value in c)
  379. {
  380. m_Adaptee.Insert(index++, value);
  381. }
  382. }
  383. public override void Remove(object value)
  384. {
  385. m_Adaptee.Remove(value);
  386. }
  387. public override void RemoveAt(int index)
  388. {
  389. m_Adaptee.RemoveAt(index);
  390. }
  391. public override void RemoveRange(int index, int count)
  392. {
  393. CheckRange(index, count, m_Adaptee.Count);
  394. for (int i = 0; i < count; i++)
  395. {
  396. m_Adaptee.RemoveAt(index);
  397. }
  398. }
  399. public override void Reverse()
  400. {
  401. Reverse(0, m_Adaptee.Count);
  402. }
  403. public override void Reverse(int index, int count)
  404. {
  405. object tmp;
  406. CheckRange(index, count, m_Adaptee.Count);
  407. for (int i = 0; i < count / 2; i++)
  408. {
  409. tmp = m_Adaptee[i + index];
  410. m_Adaptee[i + index] = m_Adaptee[(index + count) - i + index - 1];
  411. m_Adaptee[(index + count) - i + index - 1] = tmp;
  412. }
  413. }
  414. public override void SetRange(int index, ICollection c)
  415. {
  416. if (c == null)
  417. {
  418. throw new ArgumentNullException("c");
  419. }
  420. if (index < 0 || index + c.Count > m_Adaptee.Count)
  421. {
  422. throw new ArgumentOutOfRangeException("index");
  423. }
  424. int x = index;
  425. foreach (object value in c)
  426. {
  427. m_Adaptee[x++] = value;
  428. }
  429. }
  430. public override void CopyTo(System.Array array)
  431. {
  432. m_Adaptee.CopyTo(array, 0);
  433. }
  434. public override void CopyTo(System.Array array, int index)
  435. {
  436. m_Adaptee.CopyTo(array, index);
  437. }
  438. public override void CopyTo(int index, System.Array array, int arrayIndex, int count)
  439. {
  440. if (index < 0)
  441. {
  442. ThrowNewArgumentOutOfRangeException ("index", index,
  443. "Can't be less than zero.");
  444. }
  445. if (arrayIndex < 0)
  446. {
  447. ThrowNewArgumentOutOfRangeException ("arrayIndex", arrayIndex,
  448. "Can't be less than zero.");
  449. }
  450. if (count < 0)
  451. {
  452. ThrowNewArgumentOutOfRangeException ("index", index,
  453. "Can't be less than zero.");
  454. }
  455. if (index >= m_Adaptee.Count)
  456. {
  457. throw new ArgumentException("Can't be more or equal to list count.",
  458. "index");
  459. }
  460. if (array.Rank > 1)
  461. {
  462. throw new ArgumentException("Can't copy into multi-dimensional array.");
  463. }
  464. if (arrayIndex >= array.Length)
  465. {
  466. throw new ArgumentException("arrayIndex can't be greater than array.Length - 1.");
  467. }
  468. if (array.Length - arrayIndex + 1 < count)
  469. {
  470. throw new ArgumentException("Destination array is too small.");
  471. }
  472. // re-ordered to avoid possible integer overflow
  473. if (index > m_Adaptee.Count - count) {
  474. throw new ArgumentException("Index and count do not denote a valid range of elements.", "index");
  475. }
  476. for (int i = 0; i < count; i++)
  477. {
  478. array.SetValue(m_Adaptee[index + i], arrayIndex + i);
  479. }
  480. }
  481. public override bool IsSynchronized
  482. {
  483. get
  484. {
  485. return m_Adaptee.IsSynchronized;
  486. }
  487. }
  488. public override IEnumerator GetEnumerator()
  489. {
  490. return m_Adaptee.GetEnumerator();
  491. }
  492. public override IEnumerator GetEnumerator(int index, int count)
  493. {
  494. CheckRange(index, count, m_Adaptee.Count);
  495. return new EnumeratorWithRange(m_Adaptee.GetEnumerator(), index, count);
  496. }
  497. public override void AddRange(ICollection c)
  498. {
  499. foreach (object value in c)
  500. {
  501. m_Adaptee.Add(value);
  502. }
  503. }
  504. public override int BinarySearch(object value)
  505. {
  506. return BinarySearch(value, null);
  507. }
  508. public override int BinarySearch(object value, IComparer comparer)
  509. {
  510. return BinarySearch(0, m_Adaptee.Count, value, comparer);
  511. }
  512. public override int BinarySearch(int index, int count, object value, IComparer comparer)
  513. {
  514. int r, x, y, z;
  515. // Doing a direct BinarySearch on the adaptee will perform poorly if the adaptee is a linked-list.
  516. // Alternatives include copying the adaptee to a temporary array first.
  517. CheckRange(index, count, m_Adaptee.Count);
  518. if (comparer == null)
  519. {
  520. comparer = Comparer.Default;
  521. }
  522. x = index;
  523. y = index + count - 1;
  524. while (x <= y)
  525. {
  526. // Be careful with overflows
  527. z = x + ((y - x) / 2);
  528. r = comparer.Compare(value, m_Adaptee[z]);
  529. if (r < 0)
  530. {
  531. y = z - 1;
  532. }
  533. else if (r > 0)
  534. {
  535. x = z + 1;
  536. }
  537. else
  538. {
  539. return z;
  540. }
  541. }
  542. return ~x;
  543. }
  544. public override object Clone()
  545. {
  546. return new ArrayList.ArrayListAdapter(m_Adaptee);
  547. }
  548. public override ArrayList GetRange(int index, int count)
  549. {
  550. CheckRange(index, count, m_Adaptee.Count);
  551. return new RangedArrayList(this, index, count);
  552. }
  553. public override void TrimToSize()
  554. {
  555. // N/A
  556. }
  557. public override void Sort()
  558. {
  559. Sort(Comparer.Default);
  560. }
  561. public override void Sort(IComparer comparer)
  562. {
  563. Sort(0, m_Adaptee.Count, comparer);
  564. }
  565. public override void Sort(int index, int count, IComparer comparer)
  566. {
  567. CheckRange(index, count, m_Adaptee.Count);
  568. if (comparer == null)
  569. {
  570. comparer = Comparer.Default;
  571. }
  572. // Doing a direct sort on the adaptee will perform poorly if the adaptee is a linked-list.
  573. // Alternatives include copying the adaptee into a temporary array first.
  574. QuickSort(m_Adaptee, index, index + count - 1, comparer);
  575. }
  576. /// <summary>
  577. /// Swaps two items in a list at the specified indexes.
  578. /// </summary>
  579. private static void Swap(IList list, int x, int y)
  580. {
  581. object tmp;
  582. tmp = list[x];
  583. list[x] = list[y];
  584. list[y] = tmp;
  585. }
  586. /// <summary>
  587. /// Quicksort for lists.
  588. /// </summary>
  589. /// <remarks>
  590. /// This function acts as both qsort() and partition().
  591. /// </remarks>
  592. internal static void QuickSort(IList list, int left, int right, IComparer comparer)
  593. {
  594. int i, j, middle;
  595. object pivot;
  596. if (left >= right)
  597. {
  598. return;
  599. }
  600. // Pick the pivot using the median-of-three strategy.
  601. // Be careful with overflows
  602. middle = left + ((right - left) / 2);
  603. if (comparer.Compare(list[middle], list[left]) < 0)
  604. {
  605. Swap(list, middle, left);
  606. }
  607. if (comparer.Compare(list[right], list[left]) < 0)
  608. {
  609. Swap(list, right, left);
  610. }
  611. if (comparer.Compare(list[right], list[middle]) < 0)
  612. {
  613. Swap(list, right, middle);
  614. }
  615. if (right - left + 1 <= 3)
  616. {
  617. return;
  618. }
  619. // Put the pivot in right - 1.
  620. Swap(list, right - 1, middle);
  621. // List should look like:
  622. //
  623. // [Small] ..Numbers.. [Middle] ..Numbers.. [Pivot][Large]
  624. pivot = list[right - 1];
  625. // Sort from (left + 1) to (right - 2).
  626. i = left;
  627. j = right - 1;
  628. for (;;)
  629. {
  630. while (comparer.Compare(list[++i], pivot) < 0);
  631. while (comparer.Compare(list[--j], pivot) > 0);
  632. if (i < j)
  633. {
  634. Swap(list, i, j);
  635. }
  636. else
  637. {
  638. break;
  639. }
  640. }
  641. // Put pivot into the right position (real middle).
  642. Swap(list, right - 1, i);
  643. // Recursively sort the left and right sub lists.
  644. QuickSort(list, left, i - 1, comparer);
  645. QuickSort(list, i + 1, right, comparer);
  646. }
  647. public override object[] ToArray()
  648. {
  649. object[] retval;
  650. retval = new object[m_Adaptee.Count];
  651. m_Adaptee.CopyTo(retval, 0);
  652. return retval;
  653. }
  654. public override Array ToArray(Type elementType)
  655. {
  656. Array retval;
  657. retval = Array.CreateInstance(elementType, m_Adaptee.Count);
  658. m_Adaptee.CopyTo(retval, 0);
  659. return retval;
  660. }
  661. }
  662. #endregion // ArrayListAdapter
  663. //
  664. // ArrayList wrappers
  665. //
  666. #region ArrayListWrapper
  667. /// <summary>
  668. /// Base wrapper/decorator for ArrayLists. Simply delegates all methods to
  669. /// the underlying wrappee.
  670. /// </summary>
  671. [Serializable]
  672. private class ArrayListWrapper
  673. : ArrayList
  674. {
  675. protected ArrayList m_InnerArrayList;
  676. #region Constructors
  677. public ArrayListWrapper(ArrayList innerArrayList)
  678. {
  679. m_InnerArrayList = innerArrayList;
  680. }
  681. #endregion
  682. #region Indexers
  683. public override object this[int index]
  684. {
  685. get
  686. {
  687. return m_InnerArrayList[index];
  688. }
  689. set
  690. {
  691. m_InnerArrayList[index] = value;
  692. }
  693. }
  694. #endregion
  695. #region Properties
  696. public override int Count
  697. {
  698. get
  699. {
  700. return m_InnerArrayList.Count;
  701. }
  702. }
  703. public override int Capacity
  704. {
  705. get
  706. {
  707. return m_InnerArrayList.Capacity;
  708. }
  709. set
  710. {
  711. m_InnerArrayList.Capacity = value;
  712. }
  713. }
  714. public override bool IsFixedSize
  715. {
  716. get
  717. {
  718. return m_InnerArrayList.IsFixedSize;
  719. }
  720. }
  721. public override bool IsReadOnly
  722. {
  723. get
  724. {
  725. return m_InnerArrayList.IsReadOnly;
  726. }
  727. }
  728. public override bool IsSynchronized
  729. {
  730. get
  731. {
  732. return m_InnerArrayList.IsSynchronized;
  733. }
  734. }
  735. public override object SyncRoot
  736. {
  737. get
  738. {
  739. return m_InnerArrayList.SyncRoot;
  740. }
  741. }
  742. #endregion
  743. #region Methods
  744. public override int Add(object value)
  745. {
  746. return m_InnerArrayList.Add(value);
  747. }
  748. public override void Clear()
  749. {
  750. m_InnerArrayList.Clear();
  751. }
  752. public override bool Contains(object value)
  753. {
  754. return m_InnerArrayList.Contains(value);
  755. }
  756. public override int IndexOf(object value)
  757. {
  758. return m_InnerArrayList.IndexOf(value);
  759. }
  760. public override int IndexOf(object value, int startIndex)
  761. {
  762. return m_InnerArrayList.IndexOf(value, startIndex);
  763. }
  764. public override int IndexOf(object value, int startIndex, int count)
  765. {
  766. return m_InnerArrayList.IndexOf(value, startIndex, count);
  767. }
  768. public override int LastIndexOf(object value)
  769. {
  770. return m_InnerArrayList.LastIndexOf(value);
  771. }
  772. public override int LastIndexOf(object value, int startIndex)
  773. {
  774. return m_InnerArrayList.LastIndexOf(value, startIndex);
  775. }
  776. public override int LastIndexOf(object value, int startIndex, int count)
  777. {
  778. return m_InnerArrayList.LastIndexOf(value, startIndex, count);
  779. }
  780. public override void Insert(int index, object value)
  781. {
  782. m_InnerArrayList.Insert(index, value);
  783. }
  784. public override void InsertRange(int index, ICollection c)
  785. {
  786. m_InnerArrayList.InsertRange(index, c);
  787. }
  788. public override void Remove(object value)
  789. {
  790. m_InnerArrayList.Remove(value);
  791. }
  792. public override void RemoveAt(int index)
  793. {
  794. m_InnerArrayList.RemoveAt(index);
  795. }
  796. public override void RemoveRange(int index, int count)
  797. {
  798. m_InnerArrayList.RemoveRange(index, count);
  799. }
  800. public override void Reverse()
  801. {
  802. m_InnerArrayList.Reverse();
  803. }
  804. public override void Reverse(int index, int count)
  805. {
  806. m_InnerArrayList.Reverse(index, count);
  807. }
  808. public override void SetRange(int index, ICollection c)
  809. {
  810. m_InnerArrayList.SetRange(index, c);
  811. }
  812. public override void CopyTo(System.Array array)
  813. {
  814. m_InnerArrayList.CopyTo(array);
  815. }
  816. public override void CopyTo(System.Array array, int index)
  817. {
  818. m_InnerArrayList.CopyTo(array, index);
  819. }
  820. public override void CopyTo(int index, System.Array array, int arrayIndex, int count)
  821. {
  822. m_InnerArrayList.CopyTo(index, array, arrayIndex, count);
  823. }
  824. public override IEnumerator GetEnumerator()
  825. {
  826. return m_InnerArrayList.GetEnumerator();
  827. }
  828. public override IEnumerator GetEnumerator(int index, int count)
  829. {
  830. return m_InnerArrayList.GetEnumerator(index, count);
  831. }
  832. public override void AddRange(ICollection c)
  833. {
  834. m_InnerArrayList.AddRange(c);
  835. }
  836. public override int BinarySearch(object value)
  837. {
  838. return m_InnerArrayList.BinarySearch(value);
  839. }
  840. public override int BinarySearch(object value, IComparer comparer)
  841. {
  842. return m_InnerArrayList.BinarySearch(value, comparer);
  843. }
  844. public override int BinarySearch(int index, int count, object value, IComparer comparer)
  845. {
  846. return m_InnerArrayList.BinarySearch(index, count, value, comparer);
  847. }
  848. public override object Clone()
  849. {
  850. return m_InnerArrayList.Clone();
  851. }
  852. public override ArrayList GetRange(int index, int count)
  853. {
  854. return m_InnerArrayList.GetRange(index, count);
  855. }
  856. public override void TrimToSize()
  857. {
  858. m_InnerArrayList.TrimToSize();
  859. }
  860. public override void Sort()
  861. {
  862. m_InnerArrayList.Sort();
  863. }
  864. public override void Sort(IComparer comparer)
  865. {
  866. m_InnerArrayList.Sort(comparer);
  867. }
  868. public override void Sort(int index, int count, IComparer comparer)
  869. {
  870. m_InnerArrayList.Sort(index, count, comparer);
  871. }
  872. public override object[] ToArray()
  873. {
  874. return m_InnerArrayList.ToArray();
  875. }
  876. public override Array ToArray(Type elementType)
  877. {
  878. return m_InnerArrayList.ToArray(elementType);
  879. }
  880. #endregion
  881. }
  882. #endregion
  883. #region SynchronizedArrayListWrapper
  884. /// <summary>
  885. /// ArrayListWrapper that synchronizes calls to all methods/properties.
  886. /// </summary>
  887. /// <remarks>
  888. /// Works by just synchronizing all method calls. In the future careful optimisation
  889. /// could give better performance...
  890. /// </remarks>
  891. [Serializable]
  892. private sealed class SynchronizedArrayListWrapper
  893. : ArrayListWrapper
  894. {
  895. private object m_SyncRoot;
  896. #region Constructors
  897. /// <summary>
  898. /// Creates a new synchronized wrapper for the given <see cref="ArrayList"/>.
  899. /// </summary>
  900. /// <param name="innerArrayList"></param>
  901. internal SynchronizedArrayListWrapper(ArrayList innerArrayList)
  902. : base(innerArrayList)
  903. {
  904. m_SyncRoot = innerArrayList.SyncRoot;
  905. }
  906. #endregion
  907. #region Indexers
  908. public override object this[int index]
  909. {
  910. get
  911. {
  912. lock (m_SyncRoot)
  913. {
  914. return m_InnerArrayList[index];
  915. }
  916. }
  917. set
  918. {
  919. lock (m_SyncRoot)
  920. {
  921. m_InnerArrayList[index] = value;
  922. }
  923. }
  924. }
  925. #endregion
  926. #region Properties
  927. // Some of these properties may be calculated so it's best to synchronize
  928. // them even though it might cause a performance hit.
  929. // Better safe than sorry ;D.
  930. public override int Count
  931. {
  932. get
  933. {
  934. lock (m_SyncRoot)
  935. {
  936. return m_InnerArrayList.Count;
  937. }
  938. }
  939. }
  940. public override int Capacity
  941. {
  942. get
  943. {
  944. lock (m_SyncRoot)
  945. {
  946. return m_InnerArrayList.Capacity;
  947. }
  948. }
  949. set
  950. {
  951. lock (m_SyncRoot)
  952. {
  953. m_InnerArrayList.Capacity = value;
  954. }
  955. }
  956. }
  957. public override bool IsFixedSize
  958. {
  959. get
  960. {
  961. lock (m_SyncRoot)
  962. {
  963. return m_InnerArrayList.IsFixedSize;
  964. }
  965. }
  966. }
  967. public override bool IsReadOnly
  968. {
  969. get
  970. {
  971. lock (m_SyncRoot)
  972. {
  973. return m_InnerArrayList.IsReadOnly;
  974. }
  975. }
  976. }
  977. public override bool IsSynchronized
  978. {
  979. get
  980. {
  981. return true;
  982. }
  983. }
  984. public override object SyncRoot
  985. {
  986. get
  987. {
  988. return m_SyncRoot;
  989. }
  990. }
  991. #endregion
  992. #region Methods
  993. public override int Add(object value)
  994. {
  995. lock (m_SyncRoot)
  996. {
  997. return m_InnerArrayList.Add(value);
  998. }
  999. }
  1000. public override void Clear()
  1001. {
  1002. lock (m_SyncRoot)
  1003. {
  1004. m_InnerArrayList.Clear();
  1005. }
  1006. }
  1007. public override bool Contains(object value)
  1008. {
  1009. lock (m_SyncRoot)
  1010. {
  1011. return m_InnerArrayList.Contains(value);
  1012. }
  1013. }
  1014. public override int IndexOf(object value)
  1015. {
  1016. lock (m_SyncRoot)
  1017. {
  1018. return m_InnerArrayList.IndexOf(value);
  1019. }
  1020. }
  1021. public override int IndexOf(object value, int startIndex)
  1022. {
  1023. lock (m_SyncRoot)
  1024. {
  1025. return m_InnerArrayList.IndexOf(value, startIndex);
  1026. }
  1027. }
  1028. public override int IndexOf(object value, int startIndex, int count)
  1029. {
  1030. lock (m_SyncRoot)
  1031. {
  1032. return m_InnerArrayList.IndexOf(value, startIndex, count);
  1033. }
  1034. }
  1035. public override int LastIndexOf(object value)
  1036. {
  1037. lock (m_SyncRoot)
  1038. {
  1039. return m_InnerArrayList.LastIndexOf(value);
  1040. }
  1041. }
  1042. public override int LastIndexOf(object value, int startIndex)
  1043. {
  1044. lock (m_SyncRoot)
  1045. {
  1046. return m_InnerArrayList.LastIndexOf(value, startIndex);
  1047. }
  1048. }
  1049. public override int LastIndexOf(object value, int startIndex, int count)
  1050. {
  1051. lock (m_SyncRoot)
  1052. {
  1053. return m_InnerArrayList.LastIndexOf(value, startIndex, count);
  1054. }
  1055. }
  1056. public override void Insert(int index, object value)
  1057. {
  1058. lock (m_SyncRoot)
  1059. {
  1060. m_InnerArrayList.Insert(index, value);
  1061. }
  1062. }
  1063. public override void InsertRange(int index, ICollection c)
  1064. {
  1065. lock (m_SyncRoot)
  1066. {
  1067. m_InnerArrayList.InsertRange(index, c);
  1068. }
  1069. }
  1070. public override void Remove(object value)
  1071. {
  1072. lock (m_SyncRoot)
  1073. {
  1074. m_InnerArrayList.Remove(value);
  1075. }
  1076. }
  1077. public override void RemoveAt(int index)
  1078. {
  1079. lock (m_SyncRoot)
  1080. {
  1081. m_InnerArrayList.RemoveAt(index);
  1082. }
  1083. }
  1084. public override void RemoveRange(int index, int count)
  1085. {
  1086. lock (m_SyncRoot)
  1087. {
  1088. m_InnerArrayList.RemoveRange(index, count);
  1089. }
  1090. }
  1091. public override void Reverse()
  1092. {
  1093. lock (m_SyncRoot)
  1094. {
  1095. m_InnerArrayList.Reverse();
  1096. }
  1097. }
  1098. public override void Reverse(int index, int count)
  1099. {
  1100. lock (m_SyncRoot)
  1101. {
  1102. m_InnerArrayList.Reverse(index, count);
  1103. }
  1104. }
  1105. public override void CopyTo(System.Array array)
  1106. {
  1107. lock (m_SyncRoot)
  1108. {
  1109. m_InnerArrayList.CopyTo(array);
  1110. }
  1111. }
  1112. public override void CopyTo(System.Array array, int index)
  1113. {
  1114. lock (m_SyncRoot)
  1115. {
  1116. m_InnerArrayList.CopyTo(array, index);
  1117. }
  1118. }
  1119. public override void CopyTo(int index, System.Array array, int arrayIndex, int count)
  1120. {
  1121. lock (m_SyncRoot)
  1122. {
  1123. m_InnerArrayList.CopyTo(index, array, arrayIndex, count);
  1124. }
  1125. }
  1126. public override IEnumerator GetEnumerator()
  1127. {
  1128. lock (m_SyncRoot)
  1129. {
  1130. return m_InnerArrayList.GetEnumerator();
  1131. }
  1132. }
  1133. public override IEnumerator GetEnumerator(int index, int count)
  1134. {
  1135. lock (m_SyncRoot)
  1136. {
  1137. return m_InnerArrayList.GetEnumerator(index, count);
  1138. }
  1139. }
  1140. public override void AddRange(ICollection c)
  1141. {
  1142. lock (m_SyncRoot)
  1143. {
  1144. m_InnerArrayList.AddRange(c);
  1145. }
  1146. }
  1147. public override int BinarySearch(object value)
  1148. {
  1149. lock (m_SyncRoot)
  1150. {
  1151. return m_InnerArrayList.BinarySearch(value);
  1152. }
  1153. }
  1154. public override int BinarySearch(object value, IComparer comparer)
  1155. {
  1156. lock (m_SyncRoot)
  1157. {
  1158. return m_InnerArrayList.BinarySearch(value, comparer);
  1159. }
  1160. }
  1161. public override int BinarySearch(int index, int count, object value, IComparer comparer)
  1162. {
  1163. lock (m_SyncRoot)
  1164. {
  1165. return m_InnerArrayList.BinarySearch(index, count, value, comparer);
  1166. }
  1167. }
  1168. public override object Clone()
  1169. {
  1170. lock (m_SyncRoot)
  1171. {
  1172. return m_InnerArrayList.Clone();
  1173. }
  1174. }
  1175. public override ArrayList GetRange(int index, int count)
  1176. {
  1177. lock (m_SyncRoot)
  1178. {
  1179. return m_InnerArrayList.GetRange(index, count);
  1180. }
  1181. }
  1182. public override void TrimToSize()
  1183. {
  1184. lock (m_SyncRoot)
  1185. {
  1186. m_InnerArrayList.TrimToSize();
  1187. }
  1188. }
  1189. public override void Sort()
  1190. {
  1191. lock (m_SyncRoot)
  1192. {
  1193. m_InnerArrayList.Sort();
  1194. }
  1195. }
  1196. public override void Sort(IComparer comparer)
  1197. {
  1198. lock (m_SyncRoot)
  1199. {
  1200. m_InnerArrayList.Sort(comparer);
  1201. }
  1202. }
  1203. public override void Sort(int index, int count, IComparer comparer)
  1204. {
  1205. lock (m_SyncRoot)
  1206. {
  1207. m_InnerArrayList.Sort(index, count, comparer);
  1208. }
  1209. }
  1210. public override object[] ToArray()
  1211. {
  1212. lock (m_SyncRoot)
  1213. {
  1214. return m_InnerArrayList.ToArray();
  1215. }
  1216. }
  1217. public override Array ToArray(Type elementType)
  1218. {
  1219. lock (m_SyncRoot)
  1220. {
  1221. return m_InnerArrayList.ToArray(elementType);
  1222. }
  1223. }
  1224. #endregion
  1225. }
  1226. #endregion
  1227. #region FixedSizeArrayListWrapper
  1228. [Serializable]
  1229. private class FixedSizeArrayListWrapper
  1230. : ArrayListWrapper
  1231. {
  1232. #region Constructors
  1233. public FixedSizeArrayListWrapper(ArrayList innerList)
  1234. : base(innerList)
  1235. {
  1236. }
  1237. #endregion
  1238. #region Properties
  1239. /// <summary>
  1240. /// Gets the error message to display when an readonly/fixedsize related exception is
  1241. /// thrown.
  1242. /// </summary>
  1243. protected virtual string ErrorMessage
  1244. {
  1245. get
  1246. {
  1247. return "Can't add or remove from a fixed-size list.";
  1248. }
  1249. }
  1250. public override int Capacity
  1251. {
  1252. get
  1253. {
  1254. return base.Capacity;
  1255. }
  1256. set
  1257. {
  1258. throw new NotSupportedException(this.ErrorMessage);
  1259. }
  1260. }
  1261. public override bool IsFixedSize
  1262. {
  1263. get
  1264. {
  1265. return true;
  1266. }
  1267. }
  1268. #endregion
  1269. #region Methods
  1270. public override int Add(object value)
  1271. {
  1272. throw new NotSupportedException(this.ErrorMessage);
  1273. }
  1274. public override void AddRange(ICollection c)
  1275. {
  1276. throw new NotSupportedException(this.ErrorMessage);
  1277. }
  1278. public override void Clear()
  1279. {
  1280. throw new NotSupportedException(this.ErrorMessage);
  1281. }
  1282. public override void Insert(int index, object value)
  1283. {
  1284. throw new NotSupportedException(this.ErrorMessage);
  1285. }
  1286. public override void InsertRange(int index, ICollection c)
  1287. {
  1288. throw new NotSupportedException(this.ErrorMessage);
  1289. }
  1290. public override void Remove(object value)
  1291. {
  1292. throw new NotSupportedException(this.ErrorMessage);
  1293. }
  1294. public override void RemoveAt(int index)
  1295. {
  1296. throw new NotSupportedException(this.ErrorMessage);
  1297. }
  1298. public override void RemoveRange(int index, int count)
  1299. {
  1300. throw new NotSupportedException(this.ErrorMessage);
  1301. }
  1302. public override void TrimToSize()
  1303. {
  1304. throw new NotSupportedException(this.ErrorMessage);
  1305. }
  1306. #endregion
  1307. }
  1308. #endregion
  1309. #region ReadOnlyArrayListWrapper
  1310. [Serializable]
  1311. private sealed class ReadOnlyArrayListWrapper
  1312. : FixedSizeArrayListWrapper
  1313. {
  1314. protected override string ErrorMessage
  1315. {
  1316. get
  1317. {
  1318. return "Can't modify a readonly list.";
  1319. }
  1320. }
  1321. public override bool IsReadOnly
  1322. {
  1323. get
  1324. {
  1325. return true;
  1326. }
  1327. }
  1328. public ReadOnlyArrayListWrapper(ArrayList innerArrayList)
  1329. : base(innerArrayList)
  1330. {
  1331. }
  1332. public override object this[int index]
  1333. {
  1334. get
  1335. {
  1336. return m_InnerArrayList[index];
  1337. }
  1338. set
  1339. {
  1340. throw new NotSupportedException(this.ErrorMessage);
  1341. }
  1342. }
  1343. public override void Reverse()
  1344. {
  1345. throw new NotSupportedException(this.ErrorMessage);
  1346. }
  1347. public override void Reverse(int index, int count)
  1348. {
  1349. throw new NotSupportedException(this.ErrorMessage);
  1350. }
  1351. public override void SetRange(int index, ICollection c)
  1352. {
  1353. throw new NotSupportedException(this.ErrorMessage);
  1354. }
  1355. public override void Sort()
  1356. {
  1357. throw new NotSupportedException(this.ErrorMessage);
  1358. }
  1359. public override void Sort(IComparer comparer)
  1360. {
  1361. throw new NotSupportedException(this.ErrorMessage);
  1362. }
  1363. public override void Sort(int index, int count, IComparer comparer)
  1364. {
  1365. throw new NotSupportedException(this.ErrorMessage);
  1366. }
  1367. }
  1368. #endregion
  1369. #region RangedArrayList
  1370. [Serializable]
  1371. private sealed class RangedArrayList
  1372. : ArrayListWrapper
  1373. {
  1374. private int m_InnerIndex;
  1375. private int m_InnerCount;
  1376. private int m_InnerStateChanges;
  1377. public RangedArrayList(ArrayList innerList, int index, int count)
  1378. : base(innerList)
  1379. {
  1380. m_InnerIndex = index;
  1381. m_InnerCount = count;
  1382. m_InnerStateChanges = innerList._version;
  1383. }
  1384. #region Indexers
  1385. public override bool IsSynchronized
  1386. {
  1387. get
  1388. {
  1389. return false;
  1390. }
  1391. }
  1392. public override object this[int index]
  1393. {
  1394. get
  1395. {
  1396. if (index < 0 || index > m_InnerCount)
  1397. {
  1398. throw new ArgumentOutOfRangeException("index");
  1399. }
  1400. return m_InnerArrayList[m_InnerIndex + index];
  1401. }
  1402. set
  1403. {
  1404. if (index < 0 || index > m_InnerCount)
  1405. {
  1406. throw new ArgumentOutOfRangeException("index");
  1407. }
  1408. m_InnerArrayList[m_InnerIndex + index] = value;
  1409. }
  1410. }
  1411. #endregion
  1412. #region Properties
  1413. public override int Count
  1414. {
  1415. get
  1416. {
  1417. VerifyStateChanges();
  1418. return m_InnerCount;
  1419. }
  1420. }
  1421. public override int Capacity
  1422. {
  1423. get
  1424. {
  1425. return m_InnerArrayList.Capacity;
  1426. }
  1427. set
  1428. {
  1429. if (value < m_InnerCount)
  1430. {
  1431. throw new ArgumentOutOfRangeException();
  1432. }
  1433. }
  1434. }
  1435. #endregion
  1436. #region Methods
  1437. private void VerifyStateChanges()
  1438. {
  1439. if (m_InnerStateChanges != m_InnerArrayList._version)
  1440. {
  1441. throw new InvalidOperationException
  1442. ("ArrayList view is invalid because the underlying ArrayList was modified.");
  1443. }
  1444. }
  1445. public override int Add(object value)
  1446. {
  1447. VerifyStateChanges();
  1448. m_InnerArrayList.Insert(m_InnerIndex + m_InnerCount, value);
  1449. m_InnerStateChanges = m_InnerArrayList._version;
  1450. return ++m_InnerCount;
  1451. }
  1452. public override void Clear()
  1453. {
  1454. VerifyStateChanges();
  1455. m_InnerArrayList.RemoveRange(m_InnerIndex, m_InnerCount);
  1456. m_InnerCount = 0;
  1457. m_InnerStateChanges = m_InnerArrayList._version;
  1458. }
  1459. public override bool Contains(object value)
  1460. {
  1461. return m_InnerArrayList.Contains(value, m_InnerIndex, m_InnerCount);
  1462. }
  1463. public override int IndexOf(object value)
  1464. {
  1465. return IndexOf(value, 0);
  1466. }
  1467. public override int IndexOf(object value, int startIndex)
  1468. {
  1469. return IndexOf(value, startIndex, m_InnerCount - startIndex);
  1470. }
  1471. public override int IndexOf(object value, int startIndex, int count)
  1472. {
  1473. if (startIndex < 0 || startIndex > m_InnerCount)
  1474. {
  1475. ThrowNewArgumentOutOfRangeException ("startIndex", startIndex,
  1476. "Does not specify valid index.");
  1477. }
  1478. if (count < 0)
  1479. {
  1480. ThrowNewArgumentOutOfRangeException ("count", count,
  1481. "Can't be less than 0.");
  1482. }
  1483. // re-ordered to avoid possible integer overflow
  1484. if (startIndex > m_InnerCount - count)
  1485. {
  1486. // LAMESPEC: Every other method throws ArgumentException
  1487. throw new ArgumentOutOfRangeException("count",
  1488. "Start index and count do not specify a valid range.");
  1489. }
  1490. int retval = m_InnerArrayList.IndexOf(value, m_InnerIndex + startIndex, count);
  1491. if (retval == -1)
  1492. {
  1493. return -1;
  1494. }
  1495. else
  1496. {
  1497. return retval - m_InnerIndex;
  1498. }
  1499. }
  1500. public override int LastIndexOf(object value)
  1501. {
  1502. return LastIndexOf(value, m_InnerCount - 1);
  1503. }
  1504. public override int LastIndexOf(object value, int startIndex)
  1505. {
  1506. return LastIndexOf(value, startIndex, startIndex + 1);
  1507. }
  1508. public override int LastIndexOf(object value, int startIndex, int count)
  1509. {
  1510. if (startIndex < 0)
  1511. {
  1512. ThrowNewArgumentOutOfRangeException ("startIndex", startIndex, "< 0");
  1513. }
  1514. if (count < 0)
  1515. {
  1516. ThrowNewArgumentOutOfRangeException ("count", count, "count is negative.");
  1517. }
  1518. int retval = m_InnerArrayList.LastIndexOf(value, m_InnerIndex + startIndex, count);
  1519. if (retval == -1)
  1520. {
  1521. return -1;
  1522. }
  1523. else
  1524. {
  1525. return retval - m_InnerIndex;
  1526. }
  1527. }
  1528. public override void Insert(int index, object value)
  1529. {
  1530. VerifyStateChanges();
  1531. if (index < 0 || index > m_InnerCount)
  1532. {
  1533. ThrowNewArgumentOutOfRangeException ("index", index,
  1534. "Index must be >= 0 and <= Count.");
  1535. }
  1536. m_InnerArrayList.Insert(m_InnerIndex + index, value);
  1537. m_InnerCount++;
  1538. m_InnerStateChanges = m_InnerArrayList._version;
  1539. }
  1540. public override void InsertRange(int index, ICollection c)
  1541. {
  1542. VerifyStateChanges();
  1543. if (index < 0 || index > m_InnerCount)
  1544. {
  1545. ThrowNewArgumentOutOfRangeException ("index", index,
  1546. "Index must be >= 0 and <= Count.");
  1547. }
  1548. m_InnerArrayList.InsertRange(m_InnerIndex + index, c);
  1549. m_InnerCount += c.Count;
  1550. m_InnerStateChanges = m_InnerArrayList._version;
  1551. }
  1552. public override void Remove(object value)
  1553. {
  1554. VerifyStateChanges();
  1555. int x = IndexOf(value);
  1556. if (x > -1)
  1557. {
  1558. RemoveAt(x);
  1559. }
  1560. m_InnerStateChanges = m_InnerArrayList._version;
  1561. }
  1562. public override void RemoveAt(int index)
  1563. {
  1564. VerifyStateChanges();
  1565. if (index < 0 || index > m_InnerCount)
  1566. {
  1567. ThrowNewArgumentOutOfRangeException ("index", index,
  1568. "Index must be >= 0 and <= Count.");
  1569. }
  1570. m_InnerArrayList.RemoveAt(m_InnerIndex + index);
  1571. m_InnerCount--;
  1572. m_InnerStateChanges = m_InnerArrayList._version;
  1573. }
  1574. public override void RemoveRange(int index, int count)
  1575. {
  1576. VerifyStateChanges();
  1577. CheckRange(index, count, m_InnerCount);
  1578. m_InnerArrayList.RemoveRange(m_InnerIndex + index, count);
  1579. m_InnerCount -= count;
  1580. m_InnerStateChanges = m_InnerArrayList._version;
  1581. }
  1582. public override void Reverse()
  1583. {
  1584. Reverse(0, m_InnerCount);
  1585. }
  1586. public override void Reverse(int index, int count)
  1587. {
  1588. VerifyStateChanges();
  1589. CheckRange(index, count, m_InnerCount);
  1590. m_InnerArrayList.Reverse(m_InnerIndex + index, count);
  1591. m_InnerStateChanges = m_InnerArrayList._version;
  1592. }
  1593. public override void SetRange(int index, ICollection c)
  1594. {
  1595. VerifyStateChanges();
  1596. if (index < 0 || index > m_InnerCount)
  1597. {
  1598. ThrowNewArgumentOutOfRangeException ("index", index,
  1599. "Index must be >= 0 and <= Count.");
  1600. }
  1601. m_InnerArrayList.SetRange(m_InnerIndex + index, c);
  1602. m_InnerStateChanges = m_InnerArrayList._version;
  1603. }
  1604. public override void CopyTo(System.Array array)
  1605. {
  1606. CopyTo(array, 0);
  1607. }
  1608. public override void CopyTo(System.Array array, int index)
  1609. {
  1610. CopyTo(0, array, index, m_InnerCount);
  1611. }
  1612. public override void CopyTo(int index, System.Array array, int arrayIndex, int count)
  1613. {
  1614. CheckRange(index, count, m_InnerCount);
  1615. m_InnerArrayList.CopyTo(m_InnerIndex + index, array, arrayIndex, count);
  1616. }
  1617. public override IEnumerator GetEnumerator()
  1618. {
  1619. return GetEnumerator(0, m_InnerCount);
  1620. }
  1621. public override IEnumerator GetEnumerator(int index, int count)
  1622. {
  1623. CheckRange(index, count, m_InnerCount);
  1624. return m_InnerArrayList.GetEnumerator(m_InnerIndex + index, count);
  1625. }
  1626. public override void AddRange(ICollection c)
  1627. {
  1628. VerifyStateChanges();
  1629. m_InnerArrayList.InsertRange(m_InnerCount, c);
  1630. m_InnerCount += c.Count;
  1631. m_InnerStateChanges = m_InnerArrayList._version;
  1632. }
  1633. public override int BinarySearch(object value)
  1634. {
  1635. return BinarySearch(0, m_InnerCount, value, Comparer.Default);
  1636. }
  1637. public override int BinarySearch(object value, IComparer comparer)
  1638. {
  1639. return BinarySearch(0, m_InnerCount, value, comparer);
  1640. }
  1641. public override int BinarySearch(int index, int count, object value, IComparer comparer)
  1642. {
  1643. CheckRange(index, count, m_InnerCount);
  1644. return m_InnerArrayList.BinarySearch(m_InnerIndex + index, count, value, comparer);
  1645. }
  1646. public override object Clone()
  1647. {
  1648. return new RangedArrayList((ArrayList)m_InnerArrayList.Clone(), m_InnerIndex, m_InnerCount);
  1649. }
  1650. public override ArrayList GetRange(int index, int count)
  1651. {
  1652. CheckRange(index, count, m_InnerCount);
  1653. return new RangedArrayList(this, index, count);
  1654. }
  1655. public override void TrimToSize()
  1656. {
  1657. throw new NotSupportedException();
  1658. }
  1659. public override void Sort()
  1660. {
  1661. Sort(Comparer.Default);
  1662. }
  1663. public override void Sort(IComparer comparer)
  1664. {
  1665. Sort(0, m_InnerCount, comparer);
  1666. }
  1667. public override void Sort(int index, int count, IComparer comparer)
  1668. {
  1669. VerifyStateChanges();
  1670. CheckRange(index, count, m_InnerCount);
  1671. m_InnerArrayList.Sort(m_InnerIndex + index, count, comparer);
  1672. m_InnerStateChanges = m_InnerArrayList._version;
  1673. }
  1674. public override object[] ToArray()
  1675. {
  1676. object[] array;
  1677. array = new object[m_InnerCount];
  1678. m_InnerArrayList.CopyTo (m_InnerIndex, array, 0, m_InnerCount);
  1679. return array;
  1680. }
  1681. public override Array ToArray(Type elementType)
  1682. {
  1683. Array array;
  1684. array = Array.CreateInstance(elementType, m_InnerCount);
  1685. m_InnerArrayList.CopyTo(m_InnerIndex, array, 0, m_InnerCount);
  1686. return array;
  1687. }
  1688. #endregion
  1689. }
  1690. #endregion
  1691. //
  1692. // List wrappers
  1693. //
  1694. #region SynchronizedListWrapper
  1695. [Serializable]
  1696. private sealed class SynchronizedListWrapper
  1697. : ListWrapper
  1698. {
  1699. private object m_SyncRoot;
  1700. public SynchronizedListWrapper(IList innerList)
  1701. : base(innerList)
  1702. {
  1703. m_SyncRoot = innerList.SyncRoot;
  1704. }
  1705. public override int Count
  1706. {
  1707. get
  1708. {
  1709. lock (m_SyncRoot)
  1710. {
  1711. return m_InnerList.Count;
  1712. }
  1713. }
  1714. }
  1715. public override bool IsSynchronized
  1716. {
  1717. get
  1718. {
  1719. return true;
  1720. }
  1721. }
  1722. public override object SyncRoot
  1723. {
  1724. get
  1725. {
  1726. lock (m_SyncRoot)
  1727. {
  1728. return m_InnerList.SyncRoot;
  1729. }
  1730. }
  1731. }
  1732. public override bool IsFixedSize
  1733. {
  1734. get
  1735. {
  1736. lock (m_SyncRoot)
  1737. {
  1738. return m_InnerList.IsFixedSize;
  1739. }
  1740. }
  1741. }
  1742. public override bool IsReadOnly
  1743. {
  1744. get
  1745. {
  1746. lock (m_SyncRoot)
  1747. {
  1748. return m_InnerList.IsReadOnly;
  1749. }
  1750. }
  1751. }
  1752. public override object this[int index]
  1753. {
  1754. get
  1755. {
  1756. lock (m_SyncRoot)
  1757. {
  1758. return m_InnerList[index];
  1759. }
  1760. }
  1761. set
  1762. {
  1763. lock (m_SyncRoot)
  1764. {
  1765. m_InnerList[index] = value;
  1766. }
  1767. }
  1768. }
  1769. public override int Add(object value)
  1770. {
  1771. lock (m_SyncRoot)
  1772. {
  1773. return m_InnerList.Add(value);
  1774. }
  1775. }
  1776. public override void Clear()
  1777. {
  1778. lock (m_SyncRoot)
  1779. {
  1780. m_InnerList.Clear();
  1781. }
  1782. }
  1783. public override bool Contains(object value)
  1784. {
  1785. lock (m_SyncRoot)
  1786. {
  1787. return m_InnerList.Contains(value);
  1788. }
  1789. }
  1790. public override int IndexOf(object value)
  1791. {
  1792. lock (m_SyncRoot)
  1793. {
  1794. return m_InnerList.IndexOf(value);
  1795. }
  1796. }
  1797. public override void Insert(int index, object value)
  1798. {
  1799. lock (m_SyncRoot)
  1800. {
  1801. m_InnerList.Insert(index, value);
  1802. }
  1803. }
  1804. public override void Remove(object value)
  1805. {
  1806. lock (m_SyncRoot)
  1807. {
  1808. m_InnerList.Remove(value);
  1809. }
  1810. }
  1811. public override void RemoveAt(int index)
  1812. {
  1813. lock (m_SyncRoot)
  1814. {
  1815. m_InnerList.RemoveAt(index);
  1816. }
  1817. }
  1818. public override void CopyTo(Array array, int index)
  1819. {
  1820. lock (m_SyncRoot)
  1821. {
  1822. m_InnerList.CopyTo(array, index);
  1823. }
  1824. }
  1825. public override IEnumerator GetEnumerator()
  1826. {
  1827. lock (m_SyncRoot)
  1828. {
  1829. return m_InnerList.GetEnumerator();
  1830. }
  1831. }
  1832. }
  1833. #endregion
  1834. #region FixedSizeListWrapper
  1835. [Serializable]
  1836. private class FixedSizeListWrapper
  1837. : ListWrapper
  1838. {
  1839. protected virtual string ErrorMessage
  1840. {
  1841. get
  1842. {
  1843. return "List is fixed-size.";
  1844. }
  1845. }
  1846. public override bool IsFixedSize
  1847. {
  1848. get
  1849. {
  1850. return true;
  1851. }
  1852. }
  1853. public FixedSizeListWrapper(IList innerList)
  1854. : base(innerList)
  1855. {
  1856. }
  1857. public override int Add(object value)
  1858. {
  1859. throw new NotSupportedException(this.ErrorMessage);
  1860. }
  1861. public override void Clear()
  1862. {
  1863. throw new NotSupportedException(this.ErrorMessage);
  1864. }
  1865. public override void Insert(int index, object value)
  1866. {
  1867. throw new NotSupportedException(this.ErrorMessage);
  1868. }
  1869. public override void Remove(object value)
  1870. {
  1871. throw new NotSupportedException(this.ErrorMessage);
  1872. }
  1873. public override void RemoveAt(int index)
  1874. {
  1875. throw new NotSupportedException(this.ErrorMessage);
  1876. }
  1877. }
  1878. #endregion
  1879. #region ReadOnlyListWrapper
  1880. [Serializable]
  1881. private sealed class ReadOnlyListWrapper
  1882. : FixedSizeListWrapper
  1883. {
  1884. protected override string ErrorMessage
  1885. {
  1886. get
  1887. {
  1888. return "List is read-only.";
  1889. }
  1890. }
  1891. public override bool IsReadOnly
  1892. {
  1893. get
  1894. {
  1895. return true;
  1896. }
  1897. }
  1898. public ReadOnlyListWrapper(IList innerList)
  1899. : base(innerList)
  1900. {
  1901. }
  1902. public override object this[int index]
  1903. {
  1904. get
  1905. {
  1906. return m_InnerList[index];
  1907. }
  1908. set
  1909. {
  1910. throw new NotSupportedException(this.ErrorMessage);
  1911. }
  1912. }
  1913. }
  1914. #endregion
  1915. #region ListWrapper
  1916. /// <summary>
  1917. /// Decorates/Wraps any <c>IList</c> implementing object.
  1918. /// </summary>
  1919. [Serializable]
  1920. private class ListWrapper
  1921. : IList
  1922. {
  1923. #region Fields
  1924. protected IList m_InnerList;
  1925. #endregion
  1926. #region Constructors
  1927. public ListWrapper(IList innerList)
  1928. {
  1929. m_InnerList = innerList;
  1930. }
  1931. #endregion
  1932. #region Indexers
  1933. public virtual object this[int index]
  1934. {
  1935. get
  1936. {
  1937. return m_InnerList[index];
  1938. }
  1939. set
  1940. {
  1941. m_InnerList[index] = value;
  1942. }
  1943. }
  1944. #endregion
  1945. #region Properties
  1946. public virtual int Count
  1947. {
  1948. get
  1949. {
  1950. return m_InnerList.Count;
  1951. }
  1952. }
  1953. public virtual bool IsSynchronized
  1954. {
  1955. get
  1956. {
  1957. return m_InnerList.IsSynchronized;
  1958. }
  1959. }
  1960. public virtual object SyncRoot
  1961. {
  1962. get
  1963. {
  1964. return m_InnerList.SyncRoot;
  1965. }
  1966. }
  1967. public virtual bool IsFixedSize
  1968. {
  1969. get
  1970. {
  1971. return m_InnerList.IsFixedSize;
  1972. }
  1973. }
  1974. public virtual bool IsReadOnly
  1975. {
  1976. get
  1977. {
  1978. return m_InnerList.IsReadOnly;
  1979. }
  1980. }
  1981. #endregion
  1982. #region Methods
  1983. public virtual int Add(object value)
  1984. {
  1985. return m_InnerList.Add(value);
  1986. }
  1987. public virtual void Clear()
  1988. {
  1989. m_InnerList.Clear();
  1990. }
  1991. public virtual bool Contains(object value)
  1992. {
  1993. return m_InnerList.Contains(value);
  1994. }
  1995. public virtual int IndexOf(object value)
  1996. {
  1997. return m_InnerList.IndexOf(value);
  1998. }
  1999. public virtual void Insert(int index, object value)
  2000. {
  2001. m_InnerList.Insert(index, value);
  2002. }
  2003. public virtual void Remove(object value)
  2004. {
  2005. m_InnerList.Remove(value);
  2006. }
  2007. public virtual void RemoveAt(int index)
  2008. {
  2009. m_InnerList.RemoveAt(index);
  2010. }
  2011. public virtual void CopyTo(Array array, int index)
  2012. {
  2013. m_InnerList.CopyTo(array, index);
  2014. }
  2015. public virtual IEnumerator GetEnumerator()
  2016. {
  2017. return m_InnerList.GetEnumerator();
  2018. }
  2019. #endregion
  2020. }
  2021. #endregion
  2022. //
  2023. // Start of ArrayList
  2024. //
  2025. #region Fields
  2026. private const int DefaultInitialCapacity = 4;
  2027. /// <summary>
  2028. /// Array to store the items.
  2029. /// </summary>
  2030. private object[] _items;
  2031. /// <summary>
  2032. /// Number of items in the list.
  2033. /// </summary>
  2034. private int _size;
  2035. /// <summary>
  2036. /// Total number of state changes.
  2037. /// </summary>
  2038. private int _version;
  2039. #endregion
  2040. #region Constructors
  2041. /// <summary>
  2042. /// Initializes a new instance of the <see cref="ArrayList"/> class that is empty and
  2043. /// has the default initial capacity (16).
  2044. /// </summary>
  2045. public ArrayList()
  2046. {
  2047. _items = EmptyArray<object>.Value;
  2048. }
  2049. /// <summary>
  2050. /// Initializes a new instance of the <see cref="ArrayList"/> class that contains
  2051. /// elements copied from the specified collection and that has the same initial capacity
  2052. /// as the number of elements copied.
  2053. /// </summary>
  2054. /// <param name="c">
  2055. /// The <see cref="ICollection"/> whose elements are copied into the new list.
  2056. /// </param>
  2057. /// <exception cref="ArgumentNullException">
  2058. /// The argument <c>c</c> is a null reference.
  2059. /// </exception>
  2060. public ArrayList(ICollection c)
  2061. {
  2062. Array array;
  2063. if (c == null)
  2064. {
  2065. throw new ArgumentNullException("c");
  2066. }
  2067. array = c as Array;
  2068. if (array != null && array.Rank != 1)
  2069. {
  2070. throw new RankException();
  2071. }
  2072. _items = new object[c.Count];
  2073. AddRange(c);
  2074. }
  2075. /// <summary>
  2076. /// Initializes a new instance of the <see cref="ArrayList"/> class that is empty and
  2077. /// has the specified initial capacity.
  2078. /// </summary>
  2079. /// <param name="capacity">
  2080. /// The number of elements that hte new list is initially capable of storing.
  2081. /// </param>
  2082. /// <exception cref="ArgumentOutOfRangeException">
  2083. /// The <c>capacity</c> is less than zero.
  2084. /// </exception>
  2085. public ArrayList(int capacity)
  2086. {
  2087. if (capacity < 0)
  2088. {
  2089. ThrowNewArgumentOutOfRangeException ("capacity",
  2090. capacity, "The initial capacity can't be smaller than zero.");
  2091. }
  2092. if (capacity == 0)
  2093. {
  2094. capacity = DefaultInitialCapacity;
  2095. }
  2096. _items = new object [capacity];
  2097. }
  2098. /// <summary>
  2099. /// Used by ArrayListAdapter to allow creation of an ArrayList with no storage buffer.
  2100. /// </summary>
  2101. private ArrayList(int initialCapacity, bool forceZeroSize)
  2102. {
  2103. if (forceZeroSize)
  2104. {
  2105. _items = null;
  2106. }
  2107. else
  2108. {
  2109. throw new InvalidOperationException("Use ArrayList(int)");
  2110. }
  2111. }
  2112. /// <summary>
  2113. /// Initializes a new array list that contains a copy o…

Large files files are truncated, but you can click here to view the full file