You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IntList.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.util;
  16. /**
  17. * A List of int's; as full an implementation of the java.util.List
  18. * interface as possible, with an eye toward minimal creation of
  19. * objects
  20. *
  21. * the mimicry of List is as follows:
  22. * <ul>
  23. * <li> if possible, operations designated 'optional' in the List
  24. * interface are attempted
  25. * <li> wherever the List interface refers to an Object, substitute
  26. * int
  27. * <li> wherever the List interface refers to a Collection or List,
  28. * substitute IntList
  29. * </ul>
  30. *
  31. * the mimicry is not perfect, however:
  32. * <ul>
  33. * <li> operations involving Iterators or ListIterators are not
  34. * supported
  35. * <li> remove(Object) becomes removeValue to distinguish it from
  36. * remove(int index)
  37. * <li> subList is not supported
  38. * </ul>
  39. */
  40. public class IntList
  41. {
  42. private int[] _array;
  43. private int _limit;
  44. private static final int _default_size = 128;
  45. /**
  46. * create an IntList of default size
  47. */
  48. public IntList()
  49. {
  50. this(_default_size);
  51. }
  52. public IntList(final int initialCapacity)
  53. {
  54. _array = new int[ initialCapacity ];
  55. _limit = 0;
  56. }
  57. /**
  58. * create a copy of an existing IntList
  59. *
  60. * @param list the existing IntList
  61. */
  62. public IntList(final IntList list)
  63. {
  64. this(list._array.length);
  65. System.arraycopy(list._array, 0, _array, 0, _array.length);
  66. _limit = list._limit;
  67. }
  68. /**
  69. * add the specified value at the specified index
  70. *
  71. * @param index the index where the new value is to be added
  72. * @param value the new value
  73. *
  74. * @throws IndexOutOfBoundsException if the index is out of
  75. * range (index &lt; 0 || index &gt; size()).
  76. */
  77. public void add(final int index, final int value)
  78. {
  79. if (index > _limit)
  80. {
  81. throw new IndexOutOfBoundsException();
  82. }
  83. else if (index == _limit)
  84. {
  85. add(value);
  86. }
  87. else
  88. {
  89. // index < limit -- insert into the middle
  90. if (_limit == _array.length)
  91. {
  92. growArray(_limit * 2);
  93. }
  94. System.arraycopy(_array, index, _array, index + 1,
  95. _limit - index);
  96. _array[ index ] = value;
  97. _limit++;
  98. }
  99. }
  100. /**
  101. * Appends the specified element to the end of this list
  102. *
  103. * @param value element to be appended to this list.
  104. *
  105. * @return true (as per the general contract of the Collection.add
  106. * method).
  107. */
  108. public boolean add(final int value)
  109. {
  110. if (_limit == _array.length)
  111. {
  112. growArray(_limit * 2);
  113. }
  114. _array[ _limit++ ] = value;
  115. return true;
  116. }
  117. /**
  118. * Appends all of the elements in the specified collection to the
  119. * end of this list, in the order that they are returned by the
  120. * specified collection's iterator. The behavior of this
  121. * operation is unspecified if the specified collection is
  122. * modified while the operation is in progress. (Note that this
  123. * will occur if the specified collection is this list, and it's
  124. * nonempty.)
  125. *
  126. * @param c collection whose elements are to be added to this
  127. * list.
  128. *
  129. * @return true if this list changed as a result of the call.
  130. */
  131. public boolean addAll(final IntList c)
  132. {
  133. if (c._limit != 0)
  134. {
  135. if ((_limit + c._limit) > _array.length)
  136. {
  137. growArray(_limit + c._limit);
  138. }
  139. System.arraycopy(c._array, 0, _array, _limit, c._limit);
  140. _limit += c._limit;
  141. }
  142. return true;
  143. }
  144. /**
  145. * Inserts all of the elements in the specified collection into
  146. * this list at the specified position. Shifts the element
  147. * currently at that position (if any) and any subsequent elements
  148. * to the right (increases their indices). The new elements will
  149. * appear in this list in the order that they are returned by the
  150. * specified collection's iterator. The behavior of this
  151. * operation is unspecified if the specified collection is
  152. * modified while the operation is in progress. (Note that this
  153. * will occur if the specified collection is this list, and it's
  154. * nonempty.)
  155. *
  156. * @param index index at which to insert first element from the
  157. * specified collection.
  158. * @param c elements to be inserted into this list.
  159. *
  160. * @return true if this list changed as a result of the call.
  161. *
  162. * @throws IndexOutOfBoundsException if the index is out of
  163. * range (index &lt; 0 || index &gt; size())
  164. */
  165. public boolean addAll(final int index, final IntList c)
  166. {
  167. if (index > _limit)
  168. {
  169. throw new IndexOutOfBoundsException();
  170. }
  171. if (c._limit != 0)
  172. {
  173. if ((_limit + c._limit) > _array.length)
  174. {
  175. growArray(_limit + c._limit);
  176. }
  177. // make a hole
  178. System.arraycopy(_array, index, _array, index + c._limit,
  179. _limit - index);
  180. // fill it in
  181. System.arraycopy(c._array, 0, _array, index, c._limit);
  182. _limit += c._limit;
  183. }
  184. return true;
  185. }
  186. /**
  187. * Removes all of the elements from this list. This list will be
  188. * empty after this call returns (unless it throws an exception).
  189. */
  190. public void clear()
  191. {
  192. _limit = 0;
  193. }
  194. /**
  195. * Returns true if this list contains the specified element. More
  196. * formally, returns true if and only if this list contains at
  197. * least one element e such that o == e
  198. *
  199. * @param o element whose presence in this list is to be tested.
  200. *
  201. * @return true if this list contains the specified element.
  202. */
  203. public boolean contains(final int o)
  204. {
  205. for (int j = 0; j < _limit; j++)
  206. {
  207. if (_array[ j ] == o)
  208. {
  209. return true;
  210. }
  211. }
  212. return false;
  213. }
  214. /**
  215. * Returns true if this list contains all of the elements of the
  216. * specified collection.
  217. *
  218. * @param c collection to be checked for containment in this list.
  219. *
  220. * @return true if this list contains all of the elements of the
  221. * specified collection.
  222. */
  223. public boolean containsAll(final IntList c)
  224. {
  225. if (this != c)
  226. {
  227. for (int j = 0; j < c._limit; j++)
  228. {
  229. if (!contains(c._array[ j ]))
  230. {
  231. return false;
  232. }
  233. }
  234. }
  235. return true;
  236. }
  237. /**
  238. * Compares the specified object with this list for equality.
  239. * Returns true if and only if the specified object is also a
  240. * list, both lists have the same size, and all corresponding
  241. * pairs of elements in the two lists are equal. (Two elements e1
  242. * and e2 are equal if e1 == e2.) In other words, two lists are
  243. * defined to be equal if they contain the same elements in the
  244. * same order. This definition ensures that the equals method
  245. * works properly across different implementations of the List
  246. * interface.
  247. *
  248. * @param o the object to be compared for equality with this list.
  249. *
  250. * @return true if the specified object is equal to this list.
  251. */
  252. public boolean equals(final Object o)
  253. {
  254. if (o == this) {
  255. return true;
  256. }
  257. if (!(o instanceof IntList)) {
  258. return false;
  259. }
  260. IntList other = ( IntList ) o;
  261. if (other._limit != _limit) {
  262. return false;
  263. }
  264. for (int i=0; i< _limit; i++) {
  265. if (other._array[i] != _array[i]) {
  266. return false;
  267. }
  268. }
  269. return true;
  270. }
  271. /**
  272. * Returns the element at the specified position in this list.
  273. *
  274. * @param index index of element to return.
  275. *
  276. * @return the element at the specified position in this list.
  277. *
  278. * @throws IndexOutOfBoundsException if the index is out of
  279. * range (index &lt; 0 || index &gt;= size()).
  280. */
  281. public int get(final int index)
  282. {
  283. if (index >= _limit)
  284. {
  285. throw new IndexOutOfBoundsException(
  286. index + " not accessible in a list of length " + _limit
  287. );
  288. }
  289. return _array[ index ];
  290. }
  291. /**
  292. * Returns the hash code value for this list. The hash code of a
  293. * list is defined to be the result of the following calculation:
  294. *
  295. * <code>
  296. * hashCode = 1;
  297. * Iterator i = list.iterator();
  298. * while (i.hasNext()) {
  299. * Object obj = i.next();
  300. * hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode());
  301. * }
  302. * </code>
  303. *
  304. * This ensures that list1.equals(list2) implies that
  305. * list1.hashCode()==list2.hashCode() for any two lists, list1 and
  306. * list2, as required by the general contract of Object.hashCode.
  307. *
  308. * @return the hash code value for this list.
  309. */
  310. public int hashCode()
  311. {
  312. int hash = 0;
  313. for (int j = 0; j < _limit; j++)
  314. {
  315. hash = (31 * hash) + _array[ j ];
  316. }
  317. return hash;
  318. }
  319. /**
  320. * Returns the index in this list of the first occurrence of the
  321. * specified element, or -1 if this list does not contain this
  322. * element. More formally, returns the lowest index i such that
  323. * (o == get(i)), or -1 if there is no such index.
  324. *
  325. * @param o element to search for.
  326. *
  327. * @return the index in this list of the first occurrence of the
  328. * specified element, or -1 if this list does not contain
  329. * this element.
  330. */
  331. public int indexOf(final int o)
  332. {
  333. for (int i=0; i<_limit; i++) {
  334. if (_array[i] == o) {
  335. return i;
  336. }
  337. }
  338. return -1;
  339. }
  340. /**
  341. * Returns true if this list contains no elements.
  342. *
  343. * @return true if this list contains no elements.
  344. */
  345. public boolean isEmpty()
  346. {
  347. return _limit == 0;
  348. }
  349. /**
  350. * Returns the index in this list of the last occurrence of the
  351. * specified element, or -1 if this list does not contain this
  352. * element. More formally, returns the highest index i such that
  353. * (o == get(i)), or -1 if there is no such index.
  354. *
  355. * @param o element to search for.
  356. *
  357. * @return the index in this list of the last occurrence of the
  358. * specified element, or -1 if this list does not contain
  359. * this element.
  360. */
  361. public int lastIndexOf(final int o)
  362. {
  363. for (int i=_limit-1; i>=0; i--) {
  364. if (_array[i] == o) {
  365. return i;
  366. }
  367. }
  368. return -1;
  369. }
  370. /**
  371. * Removes the element at the specified position in this list.
  372. * Shifts any subsequent elements to the left (subtracts one from
  373. * their indices). Returns the element that was removed from the
  374. * list.
  375. *
  376. * @param index the index of the element to removed.
  377. *
  378. * @return the element previously at the specified position.
  379. *
  380. * @throws IndexOutOfBoundsException if the index is out of
  381. * range (index &lt; 0 || index &gt;= size()).
  382. */
  383. public int remove(final int index)
  384. {
  385. if (index >= _limit)
  386. {
  387. throw new IndexOutOfBoundsException();
  388. }
  389. int rval = _array[ index ];
  390. System.arraycopy(_array, index + 1, _array, index, _limit - index);
  391. _limit--;
  392. return rval;
  393. }
  394. /**
  395. * Removes the first occurrence in this list of the specified
  396. * element (optional operation). If this list does not contain
  397. * the element, it is unchanged. More formally, removes the
  398. * element with the lowest index i such that (o.equals(get(i)))
  399. * (if such an element exists).
  400. *
  401. * @param o element to be removed from this list, if present.
  402. *
  403. * @return true if this list contained the specified element.
  404. */
  405. public boolean removeValue(final int o)
  406. {
  407. for (int j = 0; j < _limit; j++)
  408. {
  409. if (o == _array[ j ])
  410. {
  411. if (j+1 < _limit) {
  412. System.arraycopy(_array, j + 1, _array, j, _limit - j);
  413. }
  414. _limit--;
  415. return true;
  416. }
  417. }
  418. return false;
  419. }
  420. /**
  421. * Removes from this list all the elements that are contained in
  422. * the specified collection
  423. *
  424. * @param c collection that defines which elements will be removed
  425. * from this list.
  426. *
  427. * @return true if this list changed as a result of the call.
  428. */
  429. public boolean removeAll(final IntList c)
  430. {
  431. boolean rval = false;
  432. for (int j = 0; j < c._limit; j++)
  433. {
  434. if (removeValue(c._array[ j ]))
  435. {
  436. rval = true;
  437. }
  438. }
  439. return rval;
  440. }
  441. /**
  442. * Retains only the elements in this list that are contained in
  443. * the specified collection. In other words, removes from this
  444. * list all the elements that are not contained in the specified
  445. * collection.
  446. *
  447. * @param c collection that defines which elements this set will
  448. * retain.
  449. *
  450. * @return true if this list changed as a result of the call.
  451. */
  452. public boolean retainAll(final IntList c)
  453. {
  454. boolean rval = false;
  455. for (int j = 0; j < _limit; )
  456. {
  457. if (!c.contains(_array[ j ]))
  458. {
  459. remove(j);
  460. rval = true;
  461. }
  462. else
  463. {
  464. j++;
  465. }
  466. }
  467. return rval;
  468. }
  469. /**
  470. * Replaces the element at the specified position in this list
  471. * with the specified element
  472. *
  473. * @param index index of element to replace.
  474. * @param element element to be stored at the specified position.
  475. *
  476. * @return the element previously at the specified position.
  477. *
  478. * @throws IndexOutOfBoundsException if the index is out of
  479. * range (index &lt; 0 || index &gt;= size()).
  480. */
  481. public int set(final int index, final int element)
  482. {
  483. if (index >= _limit)
  484. {
  485. throw new IndexOutOfBoundsException();
  486. }
  487. int rval = _array[ index ];
  488. _array[ index ] = element;
  489. return rval;
  490. }
  491. /**
  492. * Returns the number of elements in this list. If this list
  493. * contains more than Integer.MAX_VALUE elements, returns
  494. * Integer.MAX_VALUE.
  495. *
  496. * @return the number of elements in this IntList
  497. */
  498. public int size()
  499. {
  500. return _limit;
  501. }
  502. /**
  503. * Returns an array containing all of the elements in this list in
  504. * proper sequence. Obeys the general contract of the
  505. * Collection.toArray method.
  506. *
  507. * @return an array containing all of the elements in this list in
  508. * proper sequence.
  509. */
  510. public int [] toArray()
  511. {
  512. int[] rval = new int[ _limit ];
  513. System.arraycopy(_array, 0, rval, 0, _limit);
  514. return rval;
  515. }
  516. /**
  517. * Returns an array containing all of the elements in this list in
  518. * proper sequence. Obeys the general contract of the
  519. * Collection.toArray(Object[]) method.
  520. *
  521. * @param a the array into which the elements of this list are to
  522. * be stored, if it is big enough; otherwise, a new array
  523. * is allocated for this purpose.
  524. *
  525. * @return an array containing the elements of this list.
  526. */
  527. public int [] toArray(final int [] a)
  528. {
  529. int[] rval;
  530. if (a.length == _limit)
  531. {
  532. System.arraycopy(_array, 0, a, 0, _limit);
  533. rval = a;
  534. }
  535. else
  536. {
  537. rval = toArray();
  538. }
  539. return rval;
  540. }
  541. private void growArray(final int new_size)
  542. {
  543. int size = (new_size == _array.length) ? new_size + 1
  544. : new_size;
  545. int[] new_array = new int[ size ];
  546. System.arraycopy(_array, 0, new_array, 0, _limit);
  547. _array = new_array;
  548. }
  549. }