1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xalan" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, Lotus
  53. * Development Corporation., http://www.lotus.com. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package org.apache.xml.utils;
  58. /**
  59. * <meta name="usage" content="internal"/>
  60. * A very simple table that stores a list of int.
  61. *
  62. * This version is based on a "realloc" strategy -- a simle array is
  63. * used, and when more storage is needed, a larger array is obtained
  64. * and all existing data is recopied into it. As a result, read/write
  65. * access to existing nodes is O(1) fast but appending may be O(N**2)
  66. * slow. See also SuballocatedIntVector.
  67. */
  68. public class IntVector implements Cloneable
  69. {
  70. /** Size of blocks to allocate */
  71. protected int m_blocksize;
  72. /** Array of ints */
  73. protected int m_map[]; // IntStack is trying to see this directly
  74. /** Number of ints in array */
  75. protected int m_firstFree = 0;
  76. /** Size of array */
  77. protected int m_mapSize;
  78. /**
  79. * Default constructor. Note that the default
  80. * block size is very small, for small lists.
  81. */
  82. public IntVector()
  83. {
  84. m_blocksize = 32;
  85. m_mapSize = m_blocksize;
  86. m_map = new int[m_blocksize];
  87. }
  88. /**
  89. * Construct a IntVector, using the given block size.
  90. *
  91. * @param blocksize Size of block to allocate
  92. */
  93. public IntVector(int blocksize)
  94. {
  95. m_blocksize = blocksize;
  96. m_mapSize = blocksize;
  97. m_map = new int[blocksize];
  98. }
  99. /**
  100. * Construct a IntVector, using the given block size.
  101. *
  102. * @param blocksize Size of block to allocate
  103. */
  104. public IntVector(int blocksize, int increaseSize)
  105. {
  106. m_blocksize = increaseSize;
  107. m_mapSize = blocksize;
  108. m_map = new int[blocksize];
  109. }
  110. /**
  111. * Copy constructor for IntVector
  112. *
  113. * @param v Existing IntVector to copy
  114. */
  115. public IntVector(IntVector v)
  116. {
  117. m_map = new int[v.m_mapSize];
  118. m_mapSize = v.m_mapSize;
  119. m_firstFree = v.m_firstFree;
  120. m_blocksize = v.m_blocksize;
  121. System.arraycopy(v.m_map, 0, m_map, 0, m_firstFree);
  122. }
  123. /**
  124. * Get the length of the list.
  125. *
  126. * @return length of the list
  127. */
  128. public final int size()
  129. {
  130. return m_firstFree;
  131. }
  132. /**
  133. * Get the length of the list.
  134. *
  135. * @return length of the list
  136. */
  137. public final void setSize(int sz)
  138. {
  139. m_firstFree = sz;
  140. }
  141. /**
  142. * Append a int onto the vector.
  143. *
  144. * @param value Int to add to the list
  145. */
  146. public final void addElement(int value)
  147. {
  148. if ((m_firstFree + 1) >= m_mapSize)
  149. {
  150. m_mapSize += m_blocksize;
  151. int newMap[] = new int[m_mapSize];
  152. System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
  153. m_map = newMap;
  154. }
  155. m_map[m_firstFree] = value;
  156. m_firstFree++;
  157. }
  158. /**
  159. * Append several int values onto the vector.
  160. *
  161. * @param value Int to add to the list
  162. */
  163. public final void addElements(int value, int numberOfElements)
  164. {
  165. if ((m_firstFree + numberOfElements) >= m_mapSize)
  166. {
  167. m_mapSize += (m_blocksize+numberOfElements);
  168. int newMap[] = new int[m_mapSize];
  169. System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
  170. m_map = newMap;
  171. }
  172. for (int i = 0; i < numberOfElements; i++)
  173. {
  174. m_map[m_firstFree] = value;
  175. m_firstFree++;
  176. }
  177. }
  178. /**
  179. * Append several slots onto the vector, but do not set the values.
  180. *
  181. * @param value Int to add to the list
  182. */
  183. public final void addElements(int numberOfElements)
  184. {
  185. if ((m_firstFree + numberOfElements) >= m_mapSize)
  186. {
  187. m_mapSize += (m_blocksize+numberOfElements);
  188. int newMap[] = new int[m_mapSize];
  189. System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
  190. m_map = newMap;
  191. }
  192. m_firstFree += numberOfElements;
  193. }
  194. /**
  195. * Inserts the specified node in this vector at the specified index.
  196. * Each component in this vector with an index greater or equal to
  197. * the specified index is shifted upward to have an index one greater
  198. * than the value it had previously.
  199. *
  200. * @param value Int to insert
  201. * @param at Index of where to insert
  202. */
  203. public final void insertElementAt(int value, int at)
  204. {
  205. if ((m_firstFree + 1) >= m_mapSize)
  206. {
  207. m_mapSize += m_blocksize;
  208. int newMap[] = new int[m_mapSize];
  209. System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
  210. m_map = newMap;
  211. }
  212. if (at <= (m_firstFree - 1))
  213. {
  214. System.arraycopy(m_map, at, m_map, at + 1, m_firstFree - at);
  215. }
  216. m_map[at] = value;
  217. m_firstFree++;
  218. }
  219. /**
  220. * Inserts the specified node in this vector at the specified index.
  221. * Each component in this vector with an index greater or equal to
  222. * the specified index is shifted upward to have an index one greater
  223. * than the value it had previously.
  224. */
  225. public final void removeAllElements()
  226. {
  227. for (int i = 0; i < m_firstFree; i++)
  228. {
  229. m_map[i] = java.lang.Integer.MIN_VALUE;
  230. }
  231. m_firstFree = 0;
  232. }
  233. /**
  234. * Removes the first occurrence of the argument from this vector.
  235. * If the object is found in this vector, each component in the vector
  236. * with an index greater or equal to the object's index is shifted
  237. * downward to have an index one smaller than the value it had
  238. * previously.
  239. *
  240. * @param s Int to remove from array
  241. *
  242. * @return True if the int was removed, false if it was not found
  243. */
  244. public final boolean removeElement(int s)
  245. {
  246. for (int i = 0; i < m_firstFree; i++)
  247. {
  248. if (m_map[i] == s)
  249. {
  250. if ((i + 1) < m_firstFree)
  251. System.arraycopy(m_map, i + 1, m_map, i - 1, m_firstFree - i);
  252. else
  253. m_map[i] = java.lang.Integer.MIN_VALUE;
  254. m_firstFree--;
  255. return true;
  256. }
  257. }
  258. return false;
  259. }
  260. /**
  261. * Deletes the component at the specified index. Each component in
  262. * this vector with an index greater or equal to the specified
  263. * index is shifted downward to have an index one smaller than
  264. * the value it had previously.
  265. *
  266. * @param i index of where to remove and int
  267. */
  268. public final void removeElementAt(int i)
  269. {
  270. if (i > m_firstFree)
  271. System.arraycopy(m_map, i + 1, m_map, i, m_firstFree);
  272. else
  273. m_map[i] = java.lang.Integer.MIN_VALUE;
  274. m_firstFree--;
  275. }
  276. /**
  277. * Sets the component at the specified index of this vector to be the
  278. * specified object. The previous component at that position is discarded.
  279. *
  280. * The index must be a value greater than or equal to 0 and less
  281. * than the current size of the vector.
  282. *
  283. * @param node object to set
  284. * @param index Index of where to set the object
  285. */
  286. public final void setElementAt(int value, int index)
  287. {
  288. m_map[index] = value;
  289. }
  290. /**
  291. * Get the nth element.
  292. *
  293. * @param i index of object to get
  294. *
  295. * @return object at given index
  296. */
  297. public final int elementAt(int i)
  298. {
  299. return m_map[i];
  300. }
  301. /**
  302. * Tell if the table contains the given node.
  303. *
  304. * @param s object to look for
  305. *
  306. * @return true if the object is in the list
  307. */
  308. public final boolean contains(int s)
  309. {
  310. for (int i = 0; i < m_firstFree; i++)
  311. {
  312. if (m_map[i] == s)
  313. return true;
  314. }
  315. return false;
  316. }
  317. /**
  318. * Searches for the first occurence of the given argument,
  319. * beginning the search at index, and testing for equality
  320. * using the equals method.
  321. *
  322. * @param elem object to look for
  323. * @param index Index of where to begin search
  324. * @return the index of the first occurrence of the object
  325. * argument in this vector at position index or later in the
  326. * vector; returns -1 if the object is not found.
  327. */
  328. public final int indexOf(int elem, int index)
  329. {
  330. for (int i = index; i < m_firstFree; i++)
  331. {
  332. if (m_map[i] == elem)
  333. return i;
  334. }
  335. return java.lang.Integer.MIN_VALUE;
  336. }
  337. /**
  338. * Searches for the first occurence of the given argument,
  339. * beginning the search at index, and testing for equality
  340. * using the equals method.
  341. *
  342. * @param elem object to look for
  343. * @return the index of the first occurrence of the object
  344. * argument in this vector at position index or later in the
  345. * vector; returns -1 if the object is not found.
  346. */
  347. public final int indexOf(int elem)
  348. {
  349. for (int i = 0; i < m_firstFree; i++)
  350. {
  351. if (m_map[i] == elem)
  352. return i;
  353. }
  354. return java.lang.Integer.MIN_VALUE;
  355. }
  356. /**
  357. * Searches for the first occurence of the given argument,
  358. * beginning the search at index, and testing for equality
  359. * using the equals method.
  360. *
  361. * @param elem Object to look for
  362. * @return the index of the first occurrence of the object
  363. * argument in this vector at position index or later in the
  364. * vector; returns -1 if the object is not found.
  365. */
  366. public final int lastIndexOf(int elem)
  367. {
  368. for (int i = (m_firstFree - 1); i >= 0; i--)
  369. {
  370. if (m_map[i] == elem)
  371. return i;
  372. }
  373. return java.lang.Integer.MIN_VALUE;
  374. }
  375. /**
  376. * Returns clone of current IntVector
  377. *
  378. * @return clone of current IntVector
  379. */
  380. public Object clone()
  381. throws CloneNotSupportedException
  382. {
  383. return new IntVector(this);
  384. }
  385. }