1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: IntVector.java,v 1.8 2004/02/17 04:21:14 minchau Exp $
  18. */
  19. package com.sun.org.apache.xml.internal.utils;
  20. /**
  21. * A very simple table that stores a list of int.
  22. *
  23. * This version is based on a "realloc" strategy -- a simle array is
  24. * used, and when more storage is needed, a larger array is obtained
  25. * and all existing data is recopied into it. As a result, read/write
  26. * access to existing nodes is O(1) fast but appending may be O(N**2)
  27. * slow. See also SuballocatedIntVector.
  28. * @xsl.usage internal
  29. */
  30. public class IntVector implements Cloneable
  31. {
  32. /** Size of blocks to allocate */
  33. protected int m_blocksize;
  34. /** Array of ints */
  35. protected int m_map[]; // IntStack is trying to see this directly
  36. /** Number of ints in array */
  37. protected int m_firstFree = 0;
  38. /** Size of array */
  39. protected int m_mapSize;
  40. /**
  41. * Default constructor. Note that the default
  42. * block size is very small, for small lists.
  43. */
  44. public IntVector()
  45. {
  46. m_blocksize = 32;
  47. m_mapSize = m_blocksize;
  48. m_map = new int[m_blocksize];
  49. }
  50. /**
  51. * Construct a IntVector, using the given block size.
  52. *
  53. * @param blocksize Size of block to allocate
  54. */
  55. public IntVector(int blocksize)
  56. {
  57. m_blocksize = blocksize;
  58. m_mapSize = blocksize;
  59. m_map = new int[blocksize];
  60. }
  61. /**
  62. * Construct a IntVector, using the given block size.
  63. *
  64. * @param blocksize Size of block to allocate
  65. */
  66. public IntVector(int blocksize, int increaseSize)
  67. {
  68. m_blocksize = increaseSize;
  69. m_mapSize = blocksize;
  70. m_map = new int[blocksize];
  71. }
  72. /**
  73. * Copy constructor for IntVector
  74. *
  75. * @param v Existing IntVector to copy
  76. */
  77. public IntVector(IntVector v)
  78. {
  79. m_map = new int[v.m_mapSize];
  80. m_mapSize = v.m_mapSize;
  81. m_firstFree = v.m_firstFree;
  82. m_blocksize = v.m_blocksize;
  83. System.arraycopy(v.m_map, 0, m_map, 0, m_firstFree);
  84. }
  85. /**
  86. * Get the length of the list.
  87. *
  88. * @return length of the list
  89. */
  90. public final int size()
  91. {
  92. return m_firstFree;
  93. }
  94. /**
  95. * Get the length of the list.
  96. *
  97. * @return length of the list
  98. */
  99. public final void setSize(int sz)
  100. {
  101. m_firstFree = sz;
  102. }
  103. /**
  104. * Append a int onto the vector.
  105. *
  106. * @param value Int to add to the list
  107. */
  108. public final void addElement(int value)
  109. {
  110. if ((m_firstFree + 1) >= m_mapSize)
  111. {
  112. m_mapSize += m_blocksize;
  113. int newMap[] = new int[m_mapSize];
  114. System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
  115. m_map = newMap;
  116. }
  117. m_map[m_firstFree] = value;
  118. m_firstFree++;
  119. }
  120. /**
  121. * Append several int values onto the vector.
  122. *
  123. * @param value Int to add to the list
  124. */
  125. public final void addElements(int value, int numberOfElements)
  126. {
  127. if ((m_firstFree + numberOfElements) >= m_mapSize)
  128. {
  129. m_mapSize += (m_blocksize+numberOfElements);
  130. int newMap[] = new int[m_mapSize];
  131. System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
  132. m_map = newMap;
  133. }
  134. for (int i = 0; i < numberOfElements; i++)
  135. {
  136. m_map[m_firstFree] = value;
  137. m_firstFree++;
  138. }
  139. }
  140. /**
  141. * Append several slots onto the vector, but do not set the values.
  142. *
  143. * @param value Int to add to the list
  144. */
  145. public final void addElements(int numberOfElements)
  146. {
  147. if ((m_firstFree + numberOfElements) >= m_mapSize)
  148. {
  149. m_mapSize += (m_blocksize+numberOfElements);
  150. int newMap[] = new int[m_mapSize];
  151. System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
  152. m_map = newMap;
  153. }
  154. m_firstFree += numberOfElements;
  155. }
  156. /**
  157. * Inserts the specified node in this vector at the specified index.
  158. * Each component in this vector with an index greater or equal to
  159. * the specified index is shifted upward to have an index one greater
  160. * than the value it had previously.
  161. *
  162. * @param value Int to insert
  163. * @param at Index of where to insert
  164. */
  165. public final void insertElementAt(int value, int at)
  166. {
  167. if ((m_firstFree + 1) >= m_mapSize)
  168. {
  169. m_mapSize += m_blocksize;
  170. int newMap[] = new int[m_mapSize];
  171. System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
  172. m_map = newMap;
  173. }
  174. if (at <= (m_firstFree - 1))
  175. {
  176. System.arraycopy(m_map, at, m_map, at + 1, m_firstFree - at);
  177. }
  178. m_map[at] = value;
  179. m_firstFree++;
  180. }
  181. /**
  182. * Inserts the specified node in this vector at the specified index.
  183. * Each component in this vector with an index greater or equal to
  184. * the specified index is shifted upward to have an index one greater
  185. * than the value it had previously.
  186. */
  187. public final void removeAllElements()
  188. {
  189. for (int i = 0; i < m_firstFree; i++)
  190. {
  191. m_map[i] = java.lang.Integer.MIN_VALUE;
  192. }
  193. m_firstFree = 0;
  194. }
  195. /**
  196. * Removes the first occurrence of the argument from this vector.
  197. * If the object is found in this vector, each component in the vector
  198. * with an index greater or equal to the object's index is shifted
  199. * downward to have an index one smaller than the value it had
  200. * previously.
  201. *
  202. * @param s Int to remove from array
  203. *
  204. * @return True if the int was removed, false if it was not found
  205. */
  206. public final boolean removeElement(int s)
  207. {
  208. for (int i = 0; i < m_firstFree; i++)
  209. {
  210. if (m_map[i] == s)
  211. {
  212. if ((i + 1) < m_firstFree)
  213. System.arraycopy(m_map, i + 1, m_map, i - 1, m_firstFree - i);
  214. else
  215. m_map[i] = java.lang.Integer.MIN_VALUE;
  216. m_firstFree--;
  217. return true;
  218. }
  219. }
  220. return false;
  221. }
  222. /**
  223. * Deletes the component at the specified index. Each component in
  224. * this vector with an index greater or equal to the specified
  225. * index is shifted downward to have an index one smaller than
  226. * the value it had previously.
  227. *
  228. * @param i index of where to remove and int
  229. */
  230. public final void removeElementAt(int i)
  231. {
  232. if (i > m_firstFree)
  233. System.arraycopy(m_map, i + 1, m_map, i, m_firstFree);
  234. else
  235. m_map[i] = java.lang.Integer.MIN_VALUE;
  236. m_firstFree--;
  237. }
  238. /**
  239. * Sets the component at the specified index of this vector to be the
  240. * specified object. The previous component at that position is discarded.
  241. *
  242. * The index must be a value greater than or equal to 0 and less
  243. * than the current size of the vector.
  244. *
  245. * @param node object to set
  246. * @param index Index of where to set the object
  247. */
  248. public final void setElementAt(int value, int index)
  249. {
  250. m_map[index] = value;
  251. }
  252. /**
  253. * Get the nth element.
  254. *
  255. * @param i index of object to get
  256. *
  257. * @return object at given index
  258. */
  259. public final int elementAt(int i)
  260. {
  261. return m_map[i];
  262. }
  263. /**
  264. * Tell if the table contains the given node.
  265. *
  266. * @param s object to look for
  267. *
  268. * @return true if the object is in the list
  269. */
  270. public final boolean contains(int s)
  271. {
  272. for (int i = 0; i < m_firstFree; i++)
  273. {
  274. if (m_map[i] == s)
  275. return true;
  276. }
  277. return false;
  278. }
  279. /**
  280. * Searches for the first occurence of the given argument,
  281. * beginning the search at index, and testing for equality
  282. * using the equals method.
  283. *
  284. * @param elem object to look for
  285. * @param index Index of where to begin search
  286. * @return the index of the first occurrence of the object
  287. * argument in this vector at position index or later in the
  288. * vector; returns -1 if the object is not found.
  289. */
  290. public final int indexOf(int elem, int index)
  291. {
  292. for (int i = index; i < m_firstFree; i++)
  293. {
  294. if (m_map[i] == elem)
  295. return i;
  296. }
  297. return java.lang.Integer.MIN_VALUE;
  298. }
  299. /**
  300. * Searches for the first occurence of the given argument,
  301. * beginning the search at index, and testing for equality
  302. * using the equals method.
  303. *
  304. * @param elem object to look for
  305. * @return the index of the first occurrence of the object
  306. * argument in this vector at position index or later in the
  307. * vector; returns -1 if the object is not found.
  308. */
  309. public final int indexOf(int elem)
  310. {
  311. for (int i = 0; i < m_firstFree; i++)
  312. {
  313. if (m_map[i] == elem)
  314. return i;
  315. }
  316. return java.lang.Integer.MIN_VALUE;
  317. }
  318. /**
  319. * Searches for the first occurence of the given argument,
  320. * beginning the search at index, and testing for equality
  321. * using the equals method.
  322. *
  323. * @param elem Object to look for
  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 lastIndexOf(int elem)
  329. {
  330. for (int i = (m_firstFree - 1); i >= 0; i--)
  331. {
  332. if (m_map[i] == elem)
  333. return i;
  334. }
  335. return java.lang.Integer.MIN_VALUE;
  336. }
  337. /**
  338. * Returns clone of current IntVector
  339. *
  340. * @return clone of current IntVector
  341. */
  342. public Object clone()
  343. throws CloneNotSupportedException
  344. {
  345. return new IntVector(this);
  346. }
  347. }