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

/Java.NET/JavApi Commons collections (Apache Port)/org.apache.commons.collections.FastArrayList.cs

https://github.com/gadfly/nofs
C# | 1646 lines | 1112 code | 208 blank | 326 comment | 89 complexity | 98b71eee4b4493e46daf1c13c268eab5 MD5 | raw file
  1. /*
  2. * Licensed under the Apache License, Version 2.0 (the "License");
  3. * you may not use this file except in compliance with the License.
  4. * You may obtain a copy of the License at
  5. *
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. *
  14. */
  15. using System;
  16. using java = biz.ritter.javapi;
  17. namespace org.apache.commons.collections
  18. {
  19. /**
  20. * <p>A customized implementation of <code>java.util.java.util.ArrayList<Object></code> designed
  21. * to operate in a multithreaded environment where the large majority of
  22. * method calls are read-only, instead of structural changes. When operating
  23. * in "fast" mode, read calls are non-lock and write calls perform the
  24. * following steps:</p>
  25. * <ul>
  26. * <li>Clone the existing collection
  27. * <li>Perform the modification on the clone
  28. * <li>Replace the existing collection with the (modified) clone
  29. * </ul>
  30. * <p>When first created, objects of this class default to "slow" mode, where
  31. * all accesses of any type are lock but no cloning takes place. This
  32. * is appropriate for initially populating the collection, followed by a switch
  33. * to "fast" mode (by calling <code>setFast(true)</code>) after initialization
  34. * is complete.</p>
  35. *
  36. * <p><strong>NOTE</strong>: If you are creating and accessing an
  37. * <code>java.util.ArrayList<Object></code> only within a single thread, you should use
  38. * <code>java.util.java.util.ArrayList<Object></code> directly (with no synchronization), for
  39. * maximum performance.</p>
  40. *
  41. * <p><strong>NOTE</strong>: <i>This class is not cross-platform.
  42. * Using it may cause unexpected failures on some architectures.</i>
  43. * It suffers from the same problems as the double-checked locking idiom.
  44. * In particular, the instruction that clones the internal collection and the
  45. * instruction that sets the internal reference to the clone can be executed
  46. * or perceived out-of-order. This means that any read operation might fail
  47. * unexpectedly, as it may be reading the state of the internal collection
  48. * before the internal collection is fully formed.
  49. * For more information on the double-checked locking idiom, see the
  50. * <a href="http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html">
  51. * Double-Checked Locking Idiom Is Broken Declaration</a>.</p>
  52. *
  53. * @since Commons Collections 1.0
  54. * @version $Revision$ $Date$
  55. *
  56. * @author Craig R. McClanahan
  57. * @author Stephen Colebourne
  58. */
  59. public class FastArrayList : java.util.ArrayList<Object>
  60. {
  61. // ----------------------------------------------------------- Constructors
  62. /**
  63. * Construct a an empty list.
  64. */
  65. public FastArrayList()
  66. : base()
  67. {
  68. this.list = new java.util.ArrayList<Object>();
  69. }
  70. /**
  71. * Construct an empty list with the specified capacity.
  72. *
  73. * @param capacity The initial capacity of the empty list
  74. */
  75. public FastArrayList(int capacity)
  76. : base()
  77. {
  78. this.list = new java.util.ArrayList<Object>(capacity);
  79. }
  80. /**
  81. * Construct a list containing the elements of the specified collection,
  82. * in the order they are returned by the collection's iterator.
  83. *
  84. * @param collection The collection whose elements initialize the contents
  85. * of this list
  86. */
  87. public FastArrayList(java.util.Collection<Object> collection)
  88. : base()
  89. {
  90. this.list = new java.util.ArrayList<Object>(collection);
  91. }
  92. // ----------------------------------------------------- Instance Variables
  93. /**
  94. * The underlying list we are managing.
  95. */
  96. protected java.util.ArrayList<Object> list = null;
  97. // ------------------------------------------------------------- Properties
  98. /**
  99. * Are we operating in "fast" mode?
  100. */
  101. protected bool fast = false;
  102. /**
  103. * Returns true if this list is operating in fast mode.
  104. *
  105. * @return true if this list is operating in fast mode
  106. */
  107. public bool getFast()
  108. {
  109. return (this.fast);
  110. }
  111. /**
  112. * Sets whether this list will operate in fast mode.
  113. *
  114. * @param fast true if the list should operate in fast mode
  115. */
  116. public void setFast(bool fast)
  117. {
  118. this.fast = fast;
  119. }
  120. // --------------------------------------------------------- Public Methods
  121. /**
  122. * Appends the specified element to the end of this list.
  123. *
  124. * @param element The element to be appended
  125. */
  126. public override bool add(Object element)
  127. {
  128. if (fast)
  129. {
  130. lock (this)
  131. {
  132. java.util.ArrayList<Object> temp = (java.util.ArrayList<Object>)list.clone();
  133. bool result = temp.add(element);
  134. list = temp;
  135. return (result);
  136. }
  137. }
  138. else
  139. {
  140. lock (list)
  141. {
  142. return (list.add(element));
  143. }
  144. }
  145. }
  146. /**
  147. * Insert the specified element at the specified position in this list,
  148. * and shift all remaining elements up one position.
  149. *
  150. * @param index Index at which to insert this element
  151. * @param element The element to be inserted
  152. *
  153. * @exception IndexOutOfBoundsException if the index is out of range
  154. */
  155. public override void add(int index, Object element)
  156. {
  157. if (fast)
  158. {
  159. lock (this)
  160. {
  161. java.util.ArrayList<Object> temp = (java.util.ArrayList<Object>)list.clone();
  162. temp.add(index, element);
  163. list = temp;
  164. }
  165. }
  166. else
  167. {
  168. lock (list)
  169. {
  170. list.add(index, element);
  171. }
  172. }
  173. }
  174. /**
  175. * Append all of the elements in the specified Collection to the end
  176. * of this list, in the order that they are returned by the specified
  177. * Collection's Iterator.
  178. *
  179. * @param collection The collection to be appended
  180. */
  181. public override bool addAll(java.util.Collection<Object> collection)
  182. {
  183. if (fast)
  184. {
  185. lock (this)
  186. {
  187. java.util.ArrayList<Object> temp = (java.util.ArrayList<Object>)list.clone();
  188. bool result = temp.addAll(collection);
  189. list = temp;
  190. return (result);
  191. }
  192. }
  193. else
  194. {
  195. lock (list)
  196. {
  197. return (list.addAll(collection));
  198. }
  199. }
  200. }
  201. /**
  202. * Insert all of the elements in the specified Collection at the specified
  203. * position in this list, and shift any previous elements upwards as
  204. * needed.
  205. *
  206. * @param index Index at which insertion takes place
  207. * @param collection The collection to be added
  208. *
  209. * @exception IndexOutOfBoundsException if the index is out of range
  210. */
  211. public override bool addAll(int index, java.util.Collection<Object> collection)
  212. {
  213. if (fast)
  214. {
  215. lock (this)
  216. {
  217. java.util.ArrayList<Object> temp = (java.util.ArrayList<Object>)list.clone();
  218. bool result = temp.addAll(index, collection);
  219. list = temp;
  220. return (result);
  221. }
  222. }
  223. else
  224. {
  225. lock (list)
  226. {
  227. return (list.addAll(index, collection));
  228. }
  229. }
  230. }
  231. /**
  232. * Remove all of the elements from this list. The list will be empty
  233. * after this call returns.
  234. *
  235. * @exception UnsupportedOperationException if <code>clear()</code>
  236. * is not supported by this list
  237. */
  238. public override void clear()
  239. {
  240. if (fast)
  241. {
  242. lock (this)
  243. {
  244. java.util.ArrayList<Object> temp = (java.util.ArrayList<Object>)list.clone();
  245. temp.clear();
  246. list = temp;
  247. }
  248. }
  249. else
  250. {
  251. lock (list)
  252. {
  253. list.clear();
  254. }
  255. }
  256. }
  257. /**
  258. * Return a shallow copy of this <code>FastArrayList</code> instance.
  259. * The elements themselves are not copied.
  260. */
  261. public override Object clone()
  262. {
  263. FastArrayList results = null;
  264. if (fast)
  265. {
  266. results = new FastArrayList(list);
  267. }
  268. else
  269. {
  270. lock (list)
  271. {
  272. results = new FastArrayList(list);
  273. }
  274. }
  275. results.setFast(getFast());
  276. return (results);
  277. }
  278. /**
  279. * Return <code>true</code> if this list contains the specified element.
  280. *
  281. * @param element The element to test for
  282. */
  283. public override bool contains(Object element)
  284. {
  285. if (fast)
  286. {
  287. return (list.contains(element));
  288. }
  289. else
  290. {
  291. lock (list)
  292. {
  293. return (list.contains(element));
  294. }
  295. }
  296. }
  297. /**
  298. * Return <code>true</code> if this list contains all of the elements
  299. * in the specified Collection.
  300. *
  301. * @param collection Collection whose elements are to be checked
  302. */
  303. public override bool containsAll(java.util.Collection<Object> collection)
  304. {
  305. if (fast)
  306. {
  307. return (list.containsAll(collection));
  308. }
  309. else
  310. {
  311. lock (list)
  312. {
  313. return (list.containsAll(collection));
  314. }
  315. }
  316. }
  317. /**
  318. * Increase the capacity of this <code>java.util.ArrayList<Object></code> instance, if
  319. * necessary, to ensure that it can hold at least the number of elements
  320. * specified by the minimum capacity argument.
  321. *
  322. * @param capacity The new minimum capacity
  323. */
  324. public override void ensureCapacity(int capacity)
  325. {
  326. if (fast)
  327. {
  328. lock (this)
  329. {
  330. java.util.ArrayList<Object> temp = (java.util.ArrayList<Object>)list.clone();
  331. temp.ensureCapacity(capacity);
  332. list = temp;
  333. }
  334. }
  335. else
  336. {
  337. lock (list)
  338. {
  339. list.ensureCapacity(capacity);
  340. }
  341. }
  342. }
  343. /**
  344. * Compare the specified object with this list for equality. This
  345. * implementation uses exactly the code that is used to define the
  346. * list equals function in the documentation for the
  347. * <code>List.equals</code> method.
  348. *
  349. * @param o Object to be compared to this list
  350. */
  351. public override bool Equals(Object o)
  352. {
  353. // Simple tests that require no synchronization
  354. if (o == this)
  355. return (true);
  356. else if (!(o is java.util.List<Object>))
  357. return (false);
  358. java.util.List<Object> lo = (java.util.List<Object>)o;
  359. // Compare the sets of elements for equality
  360. if (fast)
  361. {
  362. java.util.ListIterator<Object> li1 = list.listIterator();
  363. java.util.ListIterator<Object> li2 = lo.listIterator();
  364. while (li1.hasNext() && li2.hasNext())
  365. {
  366. Object o1 = li1.next();
  367. Object o2 = li2.next();
  368. if (!(o1 == null ? o2 == null : o1.equals(o2)))
  369. return (false);
  370. }
  371. return (!(li1.hasNext() || li2.hasNext()));
  372. }
  373. else
  374. {
  375. lock (list)
  376. {
  377. java.util.ListIterator<Object> li1 = list.listIterator();
  378. java.util.ListIterator<Object> li2 = lo.listIterator();
  379. while (li1.hasNext() && li2.hasNext())
  380. {
  381. Object o1 = li1.next();
  382. Object o2 = li2.next();
  383. if (!(o1 == null ? o2 == null : o1.equals(o2)))
  384. return (false);
  385. }
  386. return (!(li1.hasNext() || li2.hasNext()));
  387. }
  388. }
  389. }
  390. /**
  391. * Return the element at the specified position in the list.
  392. *
  393. * @param index The index of the element to return
  394. *
  395. * @exception IndexOutOfBoundsException if the index is out of range
  396. */
  397. public override Object get(int index)
  398. {
  399. if (fast)
  400. {
  401. return (list.get(index));
  402. }
  403. else
  404. {
  405. lock (list)
  406. {
  407. return (list.get(index));
  408. }
  409. }
  410. }
  411. /**
  412. * Return the hash code value for this list. This implementation uses
  413. * exactly the code that is used to define the list hash function in the
  414. * documentation for the <code>List.hashCode</code> method.
  415. */
  416. public override int GetHashCode()
  417. {
  418. if (fast)
  419. {
  420. int hashCode = 1;
  421. java.util.Iterator<Object> i = list.iterator();
  422. while (i.hasNext())
  423. {
  424. Object o = i.next();
  425. hashCode = 31 * hashCode + (o == null ? 0 : o.GetHashCode());
  426. }
  427. return (hashCode);
  428. }
  429. else
  430. {
  431. lock (list)
  432. {
  433. int hashCode = 1;
  434. java.util.Iterator<Object> i = list.iterator();
  435. while (i.hasNext())
  436. {
  437. Object o = i.next();
  438. hashCode = 31 * hashCode + (o == null ? 0 : o.GetHashCode());
  439. }
  440. return (hashCode);
  441. }
  442. }
  443. }
  444. /**
  445. * Search for the first occurrence of the given argument, testing
  446. * for equality using the <code>equals()</code> method, and return
  447. * the corresponding index, or -1 if the object is not found.
  448. *
  449. * @param element The element to search for
  450. */
  451. public override int indexOf(Object element)
  452. {
  453. if (fast)
  454. {
  455. return (list.indexOf(element));
  456. }
  457. else
  458. {
  459. lock (list)
  460. {
  461. return (list.indexOf(element));
  462. }
  463. }
  464. }
  465. /**
  466. * Test if this list has no elements.
  467. */
  468. public override bool isEmpty()
  469. {
  470. if (fast)
  471. {
  472. return (list.isEmpty());
  473. }
  474. else
  475. {
  476. lock (list)
  477. {
  478. return (list.isEmpty());
  479. }
  480. }
  481. }
  482. /**
  483. * Return an iterator over the elements in this list in proper sequence.
  484. * <p>
  485. * <b>Thread safety</b><br />
  486. * The iterator returned is thread-safe ONLY in FAST mode.
  487. * In slow mode there is no way to synchronize, or make the iterator thread-safe.
  488. * <p>
  489. * In fast mode iteration and modification may occur in parallel on different threads,
  490. * however there is a restriction. Modification must be EITHER via the Iterator
  491. * interface methods OR the List interface. If a mixture of modification
  492. * methods is used a ConcurrentModificationException is thrown from the iterator
  493. * modification method. If the List modification methods are used the changes are
  494. * NOT visible in the iterator (it shows the list contents at the time the iterator
  495. * was created).
  496. *
  497. * @return the iterator
  498. */
  499. public override java.util.Iterator<Object> iterator()
  500. {
  501. if (fast)
  502. {
  503. return new ListIter(0, this);
  504. }
  505. else
  506. {
  507. return list.iterator();
  508. }
  509. }
  510. /**
  511. * Search for the last occurrence of the given argument, testing
  512. * for equality using the <code>equals()</code> method, and return
  513. * the corresponding index, or -1 if the object is not found.
  514. *
  515. * @param element The element to search for
  516. */
  517. public override int lastIndexOf(Object element)
  518. {
  519. if (fast)
  520. {
  521. return (list.lastIndexOf(element));
  522. }
  523. else
  524. {
  525. lock (list)
  526. {
  527. return (list.lastIndexOf(element));
  528. }
  529. }
  530. }
  531. /**
  532. * Return an iterator of the elements of this list, in proper sequence.
  533. * <p>
  534. * <b>Thread safety</b><br />
  535. * The iterator returned is thread-safe ONLY in FAST mode.
  536. * In slow mode there is no way to synchronize, or make the iterator thread-safe.
  537. * <p>
  538. * In fast mode iteration and modification may occur in parallel on different threads,
  539. * however there is a restriction. Modification must be EITHER via the Iterator
  540. * interface methods OR the List interface. If a mixture of modification
  541. * methods is used a ConcurrentModificationException is thrown from the iterator
  542. * modification method. If the List modification methods are used the changes are
  543. * NOT visible in the iterator (it shows the list contents at the time the iterator
  544. * was created).
  545. *
  546. * @return the list iterator
  547. */
  548. public override java.util.ListIterator<Object> listIterator()
  549. {
  550. if (fast)
  551. {
  552. return new ListIter(0, this);
  553. }
  554. else
  555. {
  556. return list.listIterator();
  557. }
  558. }
  559. /**
  560. * Return an iterator of the elements of this list, in proper sequence,
  561. * starting at the specified position.
  562. * <p>
  563. * <b>Thread safety</b><br />
  564. * The iterator returned is thread-safe ONLY in FAST mode.
  565. * In slow mode there is no way to synchronize, or make the iterator thread-safe.
  566. * <p>
  567. * In fast mode iteration and modification may occur in parallel on different threads,
  568. * however there is a restriction. Modification must be EITHER via the Iterator
  569. * interface methods OR the List interface. If a mixture of modification
  570. * methods is used a ConcurrentModificationException is thrown from the iterator
  571. * modification method. If the List modification methods are used the changes are
  572. * NOT visible in the iterator (it shows the list contents at the time the iterator
  573. * was created).
  574. *
  575. * @param index The starting position of the iterator to return
  576. * @return the list iterator
  577. * @exception IndexOutOfBoundsException if the index is out of range
  578. */
  579. public override java.util.ListIterator<Object> listIterator(int index)
  580. {
  581. if (fast)
  582. {
  583. return new ListIter(index, this);
  584. }
  585. else
  586. {
  587. return list.listIterator(index);
  588. }
  589. }
  590. /**
  591. * Remove the element at the specified position in the list, and shift
  592. * any subsequent elements down one position.
  593. *
  594. * @param index Index of the element to be removed
  595. *
  596. * @exception IndexOutOfBoundsException if the index is out of range
  597. */
  598. public override Object remove(int index)
  599. {
  600. if (fast)
  601. {
  602. lock (this)
  603. {
  604. java.util.ArrayList<Object> temp = (java.util.ArrayList<Object>)list.clone();
  605. Object result = temp.remove(index);
  606. list = temp;
  607. return (result);
  608. }
  609. }
  610. else
  611. {
  612. lock (list)
  613. {
  614. return (list.remove(index));
  615. }
  616. }
  617. }
  618. /**
  619. * Remove the first occurrence of the specified element from the list,
  620. * and shift any subsequent elements down one position.
  621. *
  622. * @param element Element to be removed
  623. */
  624. public override bool remove(Object element)
  625. {
  626. if (fast)
  627. {
  628. lock (this)
  629. {
  630. java.util.ArrayList<Object> temp = (java.util.ArrayList<Object>)list.clone();
  631. bool result = temp.remove(element);
  632. list = temp;
  633. return (result);
  634. }
  635. }
  636. else
  637. {
  638. lock (list)
  639. {
  640. return (list.remove(element));
  641. }
  642. }
  643. }
  644. /**
  645. * Remove from this collection all of its elements that are contained
  646. * in the specified collection.
  647. *
  648. * @param collection Collection containing elements to be removed
  649. *
  650. * @exception UnsupportedOperationException if this optional operation
  651. * is not supported by this list
  652. */
  653. public override bool removeAll(java.util.Collection<Object> collection)
  654. {
  655. if (fast)
  656. {
  657. lock (this)
  658. {
  659. java.util.ArrayList<Object> temp = (java.util.ArrayList<Object>)list.clone();
  660. bool result = temp.removeAll(collection);
  661. list = temp;
  662. return (result);
  663. }
  664. }
  665. else
  666. {
  667. lock (list)
  668. {
  669. return (list.removeAll(collection));
  670. }
  671. }
  672. }
  673. /**
  674. * Remove from this collection all of its elements except those that are
  675. * contained in the specified collection.
  676. *
  677. * @param collection Collection containing elements to be retained
  678. *
  679. * @exception UnsupportedOperationException if this optional operation
  680. * is not supported by this list
  681. */
  682. public override bool retainAll(java.util.Collection<Object> collection)
  683. {
  684. if (fast)
  685. {
  686. lock (this)
  687. {
  688. java.util.ArrayList<Object> temp = (java.util.ArrayList<Object>)list.clone();
  689. bool result = temp.retainAll(collection);
  690. list = temp;
  691. return (result);
  692. }
  693. }
  694. else
  695. {
  696. lock (list)
  697. {
  698. return (list.retainAll(collection));
  699. }
  700. }
  701. }
  702. /**
  703. * Replace the element at the specified position in this list with
  704. * the specified element. Returns the previous object at that position.
  705. * <br><br>
  706. * <strong>IMPLEMENTATION NOTE</strong> - This operation is specifically
  707. * documented to not be a structural change, so it is safe to be performed
  708. * without cloning.
  709. *
  710. * @param index Index of the element to replace
  711. * @param element The new element to be stored
  712. *
  713. * @exception IndexOutOfBoundsException if the index is out of range
  714. */
  715. public override Object set(int index, Object element)
  716. {
  717. if (fast)
  718. {
  719. return (list.set(index, element));
  720. }
  721. else
  722. {
  723. lock (list)
  724. {
  725. return (list.set(index, element));
  726. }
  727. }
  728. }
  729. /**
  730. * Return the number of elements in this list.
  731. */
  732. public override int size()
  733. {
  734. if (fast)
  735. {
  736. return (list.size());
  737. }
  738. else
  739. {
  740. lock (list)
  741. {
  742. return (list.size());
  743. }
  744. }
  745. }
  746. /**
  747. * Return a view of the portion of this list between fromIndex
  748. * (inclusive) and toIndex (exclusive). The returned list is backed
  749. * by this list, so non-structural changes in the returned list are
  750. * reflected in this list. The returned list supports
  751. * all of the optional list operations supported by this list.
  752. *
  753. * @param fromIndex The starting index of the sublist view
  754. * @param toIndex The index after the end of the sublist view
  755. *
  756. * @exception IndexOutOfBoundsException if an index is out of range
  757. */
  758. public override java.util.List<Object> subList(int fromIndex, int toIndex)
  759. {
  760. if (fast)
  761. {
  762. return new SubList(fromIndex, toIndex, this);
  763. }
  764. else
  765. {
  766. return list.subList(fromIndex, toIndex);
  767. }
  768. }
  769. /**
  770. * Return an array containing all of the elements in this list in the
  771. * correct order.
  772. */
  773. public override Object[] toArray()
  774. {
  775. if (fast)
  776. {
  777. return (list.toArray());
  778. }
  779. else
  780. {
  781. lock (list)
  782. {
  783. return (list.toArray());
  784. }
  785. }
  786. }
  787. /**
  788. * Return an array containing all of the elements in this list in the
  789. * correct order. The runtime type of the returned array is that of
  790. * the specified array. If the list fits in the specified array, it is
  791. * returned therein. Otherwise, a new array is allocated with the
  792. * runtime type of the specified array, and the size of this list.
  793. *
  794. * @param array Array defining the element type of the returned list
  795. *
  796. * @exception ArrayStoreException if the runtime type of <code>array</code>
  797. * is not a supertype of the runtime type of every element in this list
  798. */
  799. public override Object[] toArray<Object>(Object[] array)
  800. {
  801. if (fast)
  802. {
  803. return (list.toArray(array));
  804. }
  805. else
  806. {
  807. lock (list)
  808. {
  809. return (list.toArray(array));
  810. }
  811. }
  812. }
  813. /**
  814. * Return a String representation of this object.
  815. */
  816. public override String ToString()
  817. {
  818. java.lang.StringBuffer sb = new java.lang.StringBuffer("FastArrayList[");
  819. sb.append(list.toString());
  820. sb.append("]");
  821. return (sb.toString());
  822. }
  823. /**
  824. * Trim the capacity of this <code>java.util.ArrayList<Object></code> instance to be the
  825. * list's current size. An application can use this operation to minimize
  826. * the storage of an <code>java.util.ArrayList<Object></code> instance.
  827. */
  828. public override void trimToSize()
  829. {
  830. if (fast)
  831. {
  832. lock (this)
  833. {
  834. java.util.ArrayList<Object> temp = (java.util.ArrayList<Object>)list.clone();
  835. temp.trimToSize();
  836. list = temp;
  837. }
  838. }
  839. else
  840. {
  841. lock (list)
  842. {
  843. list.trimToSize();
  844. }
  845. }
  846. }
  847. private class SubList : java.util.List<Object>
  848. {
  849. private int first;
  850. private int last;
  851. private java.util.List<Object> expected;
  852. private FastArrayList root;
  853. public SubList(int first, int last, FastArrayList root)
  854. {
  855. this.first = first;
  856. this.last = last;
  857. this.root = root;
  858. this.expected = root.list;
  859. }
  860. private java.util.List<Object> get(java.util.List<Object> l)
  861. {
  862. if (root.list != expected)
  863. {
  864. throw new java.util.ConcurrentModificationException();
  865. }
  866. return l.subList(first, last);
  867. }
  868. public void clear()
  869. {
  870. if (root.fast)
  871. {
  872. lock (root)
  873. {
  874. java.util.ArrayList<Object> temp = (java.util.ArrayList<Object>)root.list.clone();
  875. get(temp).clear();
  876. last = first;
  877. root.list = temp;
  878. expected = temp;
  879. }
  880. }
  881. else
  882. {
  883. lock (root.list)
  884. {
  885. get(expected).clear();
  886. }
  887. }
  888. }
  889. public bool remove(Object o)
  890. {
  891. if (root.fast)
  892. {
  893. lock (root)
  894. {
  895. java.util.ArrayList<Object> temp = (java.util.ArrayList<Object>)root.list.clone();
  896. bool r = get(temp).remove(o);
  897. if (r) last--;
  898. root.list = temp;
  899. expected = temp;
  900. return r;
  901. }
  902. }
  903. else
  904. {
  905. lock (root.list)
  906. {
  907. return get(expected).remove(o);
  908. }
  909. }
  910. }
  911. public bool removeAll(java.util.Collection<Object> o)
  912. {
  913. if (root.fast)
  914. {
  915. lock (root)
  916. {
  917. java.util.ArrayList<Object> temp = (java.util.ArrayList<Object>)root.list.clone();
  918. java.util.List<Object> sub = get(temp);
  919. bool r = sub.removeAll(o);
  920. if (r) last = first + sub.size();
  921. root.list = temp;
  922. expected = temp;
  923. return r;
  924. }
  925. }
  926. else
  927. {
  928. lock (root.list)
  929. {
  930. return get(expected).removeAll(o);
  931. }
  932. }
  933. }
  934. public bool retainAll(java.util.Collection<Object> o)
  935. {
  936. if (root.fast)
  937. {
  938. lock (root)
  939. {
  940. java.util.ArrayList<Object> temp = (java.util.ArrayList<Object>)root.list.clone();
  941. java.util.List<Object> sub = get(temp);
  942. bool r = sub.retainAll(o);
  943. if (r) last = first + sub.size();
  944. root.list = temp;
  945. expected = temp;
  946. return r;
  947. }
  948. }
  949. else
  950. {
  951. lock (root.list)
  952. {
  953. return get(expected).retainAll(o);
  954. }
  955. }
  956. }
  957. public int size()
  958. {
  959. if (root.fast)
  960. {
  961. return get(expected).size();
  962. }
  963. else
  964. {
  965. lock (root.list)
  966. {
  967. return get(expected).size();
  968. }
  969. }
  970. }
  971. public bool isEmpty()
  972. {
  973. if (root.fast)
  974. {
  975. return get(expected).isEmpty();
  976. }
  977. else
  978. {
  979. lock (root.list)
  980. {
  981. return get(expected).isEmpty();
  982. }
  983. }
  984. }
  985. public bool contains(Object o)
  986. {
  987. if (root.fast)
  988. {
  989. return get(expected).contains(o);
  990. }
  991. else
  992. {
  993. lock (root.list)
  994. {
  995. return get(expected).contains(o);
  996. }
  997. }
  998. }
  999. public bool containsAll(java.util.Collection<Object> o)
  1000. {
  1001. if (root.fast)
  1002. {
  1003. return get(expected).containsAll(o);
  1004. }
  1005. else
  1006. {
  1007. lock (root.list)
  1008. {
  1009. return get(expected).containsAll(o);
  1010. }
  1011. }
  1012. }
  1013. public Object[] toArray<Object>(Object[] o)
  1014. {
  1015. if (root.fast)
  1016. {
  1017. return get(expected).toArray(o);
  1018. }
  1019. else
  1020. {
  1021. lock (root.list)
  1022. {
  1023. return get(expected).toArray(o);
  1024. }
  1025. }
  1026. }
  1027. public Object[] toArray()
  1028. {
  1029. if (root.fast)
  1030. {
  1031. return get(expected).toArray();
  1032. }
  1033. else
  1034. {
  1035. lock (root.list)
  1036. {
  1037. return get(expected).toArray();
  1038. }
  1039. }
  1040. }
  1041. public override bool Equals(Object o)
  1042. {
  1043. if (o == this) return true;
  1044. if (root.fast)
  1045. {
  1046. return get(expected).equals(o);
  1047. }
  1048. else
  1049. {
  1050. lock (root.list)
  1051. {
  1052. return get(expected).equals(o);
  1053. }
  1054. }
  1055. }
  1056. public override int GetHashCode()
  1057. {
  1058. if (root.fast)
  1059. {
  1060. return get(expected).GetHashCode();
  1061. }
  1062. else
  1063. {
  1064. lock (root.list)
  1065. {
  1066. return get(expected).GetHashCode();
  1067. }
  1068. }
  1069. }
  1070. public bool add(Object o)
  1071. {
  1072. if (root.fast)
  1073. {
  1074. lock (root)
  1075. {
  1076. java.util.ArrayList<Object> temp = (java.util.ArrayList<Object>)root.list.clone();
  1077. bool r = get(temp).add(o);
  1078. if (r) last++;
  1079. root.list = temp;
  1080. expected = temp;
  1081. return r;
  1082. }
  1083. }
  1084. else
  1085. {
  1086. lock (root.list)
  1087. {
  1088. return get(expected).add(o);
  1089. }
  1090. }
  1091. }
  1092. public bool addAll(java.util.Collection<Object> o)
  1093. {
  1094. if (root.fast)
  1095. {
  1096. lock (root)
  1097. {
  1098. java.util.ArrayList<Object> temp = (java.util.ArrayList<Object>)root.list.clone();
  1099. bool r = get(temp).addAll(o);
  1100. if (r) last += o.size();
  1101. root.list = temp;
  1102. expected = temp;
  1103. return r;
  1104. }
  1105. }
  1106. else
  1107. {
  1108. lock (root.list)
  1109. {
  1110. return get(expected).addAll(o);
  1111. }
  1112. }
  1113. }
  1114. public void add(int i, Object o)
  1115. {
  1116. if (root.fast)
  1117. {
  1118. lock (root)
  1119. {
  1120. java.util.ArrayList<Object> temp = (java.util.ArrayList<Object>)root.list.clone();
  1121. get(temp).add(i, o);
  1122. last++;
  1123. root.list = temp;
  1124. expected = temp;
  1125. }
  1126. }
  1127. else
  1128. {
  1129. lock (root.list)
  1130. {
  1131. get(expected).add(i, o);
  1132. }
  1133. }
  1134. }
  1135. public bool addAll(int i, java.util.Collection<Object> o)
  1136. {
  1137. if (root.fast)
  1138. {
  1139. lock (root)
  1140. {
  1141. java.util.ArrayList<Object> temp = (java.util.ArrayList<Object>)root.list.clone();
  1142. bool r = get(temp).addAll(i, o);
  1143. root.list = temp;
  1144. if (r) last += o.size();
  1145. expected = temp;
  1146. return r;
  1147. }
  1148. }
  1149. else
  1150. {
  1151. lock (root.list)
  1152. {
  1153. return get(expected).addAll(i, o);
  1154. }
  1155. }
  1156. }
  1157. public Object remove(int i)
  1158. {
  1159. if (root.fast)
  1160. {
  1161. lock (root)
  1162. {
  1163. java.util.ArrayList<Object> temp = (java.util.ArrayList<Object>)root.list.clone();
  1164. Object o = get(temp).remove(i);
  1165. last--;
  1166. root.list = temp;
  1167. expected = temp;
  1168. return o;
  1169. }
  1170. }
  1171. else
  1172. {
  1173. lock (root.list)
  1174. {
  1175. return get(expected).remove(i);
  1176. }
  1177. }
  1178. }
  1179. public Object set(int i, Object a)
  1180. {
  1181. if (root.fast)
  1182. {
  1183. lock (root)
  1184. {
  1185. java.util.ArrayList<Object> temp = (java.util.ArrayList<Object>)root.list.clone();
  1186. Object o = get(temp).set(i, a);
  1187. root.list = temp;
  1188. expected = temp;
  1189. return o;
  1190. }
  1191. }
  1192. else
  1193. {
  1194. lock (root.list)
  1195. {
  1196. return get(expected).set(i, a);
  1197. }
  1198. }
  1199. }
  1200. public java.util.Iterator<Object> iterator()
  1201. {
  1202. return new SubListIter(0, this);
  1203. }
  1204. public java.util.ListIterator<Object> listIterator()
  1205. {
  1206. return new SubListIter(0, this);
  1207. }
  1208. public java.util.ListIterator<Object> listIterator(int i)
  1209. {
  1210. return new SubListIter(i, this);
  1211. }
  1212. public Object get(int i)
  1213. {
  1214. if (root.fast)
  1215. {
  1216. return get(expected).get(i);
  1217. }
  1218. else
  1219. {
  1220. lock (root.list)
  1221. {
  1222. return get(expected).get(i);
  1223. }
  1224. }
  1225. }
  1226. public int indexOf(Object o)
  1227. {
  1228. if (root.fast)
  1229. {
  1230. return get(expected).indexOf(o);
  1231. }
  1232. else
  1233. {
  1234. lock (root.list)
  1235. {
  1236. return get(expected).indexOf(o);
  1237. }
  1238. }
  1239. }
  1240. public int lastIndexOf(Object o)
  1241. {
  1242. if (root.fast)
  1243. {
  1244. return get(expected).lastIndexOf(o);
  1245. }
  1246. else
  1247. {
  1248. lock (root.list)
  1249. {
  1250. return get(expected).lastIndexOf(o);
  1251. }
  1252. }
  1253. }
  1254. public java.util.List<Object> subList(int f, int l)
  1255. {
  1256. if (root.list != expected)
  1257. {
  1258. throw new java.util.ConcurrentModificationException();
  1259. }
  1260. return new SubList(first + f, f + l, root);
  1261. }
  1262. private class SubListIter : java.util.ListIterator<Object>
  1263. {
  1264. private readonly FastArrayList root;
  1265. private readonly SubList rootSubList;
  1266. private java.util.List<Object> expected;
  1267. private java.util.ListIterator<Object> iter;
  1268. private int lastReturnedIndex = -1;
  1269. public SubListIter(int i, SubList root)
  1270. {
  1271. this.rootSubList = root;
  1272. this.root = root.root;
  1273. this.expected = this.root.list;
  1274. this.iter = rootSubList.get(expected).listIterator(i);
  1275. }
  1276. private void checkMod()
  1277. {
  1278. if (root.list != expected)
  1279. {
  1280. throw new java.util.ConcurrentModificationException();
  1281. }
  1282. }
  1283. java.util.List<Object> get()
  1284. {
  1285. return rootSubList.get(expected);
  1286. }
  1287. public bool hasNext()
  1288. {
  1289. checkMod();
  1290. return iter.hasNext();
  1291. }
  1292. public Object next()
  1293. {
  1294. checkMod();
  1295. lastReturnedIndex = iter.nextIndex();
  1296. return iter.next();
  1297. }
  1298. public bool hasPrevious()
  1299. {
  1300. checkMod();
  1301. return iter.hasPrevious();
  1302. }
  1303. public Object previous()
  1304. {
  1305. checkMod();
  1306. lastReturnedIndex = iter.previousIndex();
  1307. return iter.previous();
  1308. }
  1309. public int previousIndex()
  1310. {
  1311. checkMod();
  1312. return iter.previousIndex();
  1313. }
  1314. public int nextIndex()
  1315. {
  1316. checkMod();
  1317. return iter.nextIndex();
  1318. }
  1319. public void remove()
  1320. {
  1321. checkMod();
  1322. if (lastReturnedIndex < 0)
  1323. {
  1324. throw new java.lang.IllegalStateException();
  1325. }
  1326. get().remove(lastReturnedIndex);
  1327. rootSubList.last--;
  1328. expected = root.list;
  1329. iter = get().listIterator(lastReturnedIndex);
  1330. lastReturnedIndex = -1;
  1331. }
  1332. public void set(Object o)
  1333. {
  1334. checkMod();
  1335. if (lastReturnedIndex < 0)
  1336. {
  1337. throw new java.lang.IllegalStateException();
  1338. }
  1339. get().set(lastReturnedIndex, o);
  1340. expected = root.list;
  1341. iter = get().listIterator(previousIndex() + 1);
  1342. }
  1343. public void add(Object o)
  1344. {
  1345. checkMod();
  1346. int i = nextIndex();
  1347. get().add(i, o);
  1348. rootSubList.last++;
  1349. expected = root.list;
  1350. iter = get().listIterator(i + 1);
  1351. lastReturnedIndex = -1;
  1352. }
  1353. }
  1354. }
  1355. private class ListIter : java.util.ListIterator<Object>
  1356. {
  1357. private readonly FastArrayList root;
  1358. private java.util.List<Object> expected;
  1359. private java.util.ListIterator<Object> iter;
  1360. private int lastReturnedIndex = -1;
  1361. public ListIter(int i, FastArrayList root)
  1362. {
  1363. this.root = root;
  1364. this.expected = root.list;
  1365. this.iter = get().listIterator(i);
  1366. }
  1367. private void checkMod()
  1368. {
  1369. if (root.list != expected)
  1370. {
  1371. throw new java.util.ConcurrentModificationException();
  1372. }
  1373. }
  1374. java.util.List<Object> get()
  1375. {
  1376. return expected;
  1377. }
  1378. public bool hasNext()
  1379. {
  1380. return iter.hasNext();
  1381. }
  1382. public Object next()
  1383. {
  1384. lastReturnedIndex = iter.nextIndex();
  1385. return iter.next();
  1386. }
  1387. public bool hasPrevious()
  1388. {
  1389. return iter.hasPrevious();
  1390. }
  1391. public Object previous()
  1392. {
  1393. lastReturnedIndex = iter.previousIndex();
  1394. return iter.previous();
  1395. }
  1396. public int previousIndex()
  1397. {
  1398. return iter.previousIndex();
  1399. }
  1400. public int nextIndex()
  1401. {
  1402. return iter.nextIndex();
  1403. }
  1404. public void remove()
  1405. {
  1406. checkMod();
  1407. if (lastReturnedIndex < 0)
  1408. {
  1409. throw new java.lang.IllegalStateException();
  1410. }
  1411. get().remove(lastReturnedIndex);
  1412. expected = root.list;
  1413. iter = get().listIterator(lastReturnedIndex);
  1414. lastReturnedIndex = -1;
  1415. }
  1416. public void set(Object o)
  1417. {
  1418. checkMod();
  1419. if (lastReturnedIndex < 0)
  1420. {
  1421. throw new java.lang.IllegalStateException();
  1422. }
  1423. get().set(lastReturnedIndex, o);
  1424. expected = root.list;
  1425. iter = get().listIterator(previousIndex() + 1);
  1426. }
  1427. public void add(Object o)
  1428. {
  1429. checkMod();
  1430. int i = nextIndex();
  1431. get().add(i, o);
  1432. expected = root.list;
  1433. iter = get().listIterator(i + 1);
  1434. lastReturnedIndex = -1;
  1435. }
  1436. }
  1437. }
  1438. }