PageRenderTime 169ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/components/forks/poi/src/loci/poi/util/IntList.java

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