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

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