1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 2002 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 objects.
  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.
  67. */
  68. public class ObjectVector implements Cloneable
  69. {
  70. /** Size of blocks to allocate */
  71. protected int m_blocksize;
  72. /** Array of objects */
  73. protected Object m_map[];
  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 ObjectVector()
  83. {
  84. m_blocksize = 32;
  85. m_mapSize = m_blocksize;
  86. m_map = new Object[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 ObjectVector(int blocksize)
  94. {
  95. m_blocksize = blocksize;
  96. m_mapSize = blocksize;
  97. m_map = new Object[blocksize];
  98. }
  99. /**
  100. * Construct a IntVector, using the given block size.
  101. *
  102. * @param blocksize Size of block to allocate
  103. */
  104. public ObjectVector(int blocksize, int increaseSize)
  105. {
  106. m_blocksize = increaseSize;
  107. m_mapSize = blocksize;
  108. m_map = new Object[blocksize];
  109. }
  110. /**
  111. * Copy constructor for ObjectVector
  112. *
  113. * @param v Existing ObjectVector to copy
  114. */
  115. public ObjectVector(ObjectVector v)
  116. {
  117. m_map = new Object[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 an object onto the vector.
  143. *
  144. * @param value Object to add to the list
  145. */
  146. public final void addElement(Object value)
  147. {
  148. if ((m_firstFree + 1) >= m_mapSize)
  149. {
  150. m_mapSize += m_blocksize;
  151. Object newMap[] = new Object[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 Object values onto the vector.
  160. *
  161. * @param value Object to add to the list
  162. */
  163. public final void addElements(Object value, int numberOfElements)
  164. {
  165. if ((m_firstFree + numberOfElements) >= m_mapSize)
  166. {
  167. m_mapSize += (m_blocksize+numberOfElements);
  168. Object newMap[] = new Object[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 numberOfElements number of slots to append
  182. */
  183. public final void addElements(int numberOfElements)
  184. {
  185. if ((m_firstFree + numberOfElements) >= m_mapSize)
  186. {
  187. m_mapSize += (m_blocksize+numberOfElements);
  188. Object newMap[] = new Object[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 object 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 Object to insert
  201. * @param at Index of where to insert
  202. */
  203. public final void insertElementAt(Object value, int at)
  204. {
  205. if ((m_firstFree + 1) >= m_mapSize)
  206. {
  207. m_mapSize += m_blocksize;
  208. Object newMap[] = new Object[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. * Remove all elements objects from the list.
  221. */
  222. public final void removeAllElements()
  223. {
  224. for (int i = 0; i < m_firstFree; i++)
  225. {
  226. m_map[i] = null;
  227. }
  228. m_firstFree = 0;
  229. }
  230. /**
  231. * Removes the first occurrence of the argument from this vector.
  232. * If the object is found in this vector, each component in the vector
  233. * with an index greater or equal to the object's index is shifted
  234. * downward to have an index one smaller than the value it had
  235. * previously.
  236. *
  237. * @param s Object to remove from array
  238. *
  239. * @return True if the object was removed, false if it was not found
  240. */
  241. public final boolean removeElement(Object s)
  242. {
  243. for (int i = 0; i < m_firstFree; i++)
  244. {
  245. if (m_map[i] == s)
  246. {
  247. if ((i + 1) < m_firstFree)
  248. System.arraycopy(m_map, i + 1, m_map, i - 1, m_firstFree - i);
  249. else
  250. m_map[i] = null;
  251. m_firstFree--;
  252. return true;
  253. }
  254. }
  255. return false;
  256. }
  257. /**
  258. * Deletes the component at the specified index. Each component in
  259. * this vector with an index greater or equal to the specified
  260. * index is shifted downward to have an index one smaller than
  261. * the value it had previously.
  262. *
  263. * @param i index of where to remove an object
  264. */
  265. public final void removeElementAt(int i)
  266. {
  267. if (i > m_firstFree)
  268. System.arraycopy(m_map, i + 1, m_map, i, m_firstFree);
  269. else
  270. m_map[i] = null;
  271. m_firstFree--;
  272. }
  273. /**
  274. * Sets the component at the specified index of this vector to be the
  275. * specified object. The previous component at that position is discarded.
  276. *
  277. * The index must be a value greater than or equal to 0 and less
  278. * than the current size of the vector.
  279. *
  280. * @param value object to set
  281. * @param index Index of where to set the object
  282. */
  283. public final void setElementAt(Object value, int index)
  284. {
  285. m_map[index] = value;
  286. }
  287. /**
  288. * Get the nth element.
  289. *
  290. * @param i index of object to get
  291. *
  292. * @return object at given index
  293. */
  294. public final Object elementAt(int i)
  295. {
  296. return m_map[i];
  297. }
  298. /**
  299. * Tell if the table contains the given Object.
  300. *
  301. * @param s object to look for
  302. *
  303. * @return true if the object is in the list
  304. */
  305. public final boolean contains(Object s)
  306. {
  307. for (int i = 0; i < m_firstFree; i++)
  308. {
  309. if (m_map[i] == s)
  310. return true;
  311. }
  312. return false;
  313. }
  314. /**
  315. * Searches for the first occurence of the given argument,
  316. * beginning the search at index, and testing for equality
  317. * using the equals method.
  318. *
  319. * @param elem object to look for
  320. * @param index Index of where to begin search
  321. * @return the index of the first occurrence of the object
  322. * argument in this vector at position index or later in the
  323. * vector; returns -1 if the object is not found.
  324. */
  325. public final int indexOf(Object elem, int index)
  326. {
  327. for (int i = index; i < m_firstFree; i++)
  328. {
  329. if (m_map[i] == elem)
  330. return i;
  331. }
  332. return java.lang.Integer.MIN_VALUE;
  333. }
  334. /**
  335. * Searches for the first occurence of the given argument,
  336. * beginning the search at index, and testing for equality
  337. * using the equals method.
  338. *
  339. * @param elem object to look for
  340. * @return the index of the first occurrence of the object
  341. * argument in this vector at position index or later in the
  342. * vector; returns -1 if the object is not found.
  343. */
  344. public final int indexOf(Object elem)
  345. {
  346. for (int i = 0; i < m_firstFree; i++)
  347. {
  348. if (m_map[i] == elem)
  349. return i;
  350. }
  351. return java.lang.Integer.MIN_VALUE;
  352. }
  353. /**
  354. * Searches for the first occurence of the given argument,
  355. * beginning the search at index, and testing for equality
  356. * using the equals method.
  357. *
  358. * @param elem Object to look for
  359. * @return the index of the first occurrence of the object
  360. * argument in this vector at position index or later in the
  361. * vector; returns -1 if the object is not found.
  362. */
  363. public final int lastIndexOf(Object elem)
  364. {
  365. for (int i = (m_firstFree - 1); i >= 0; i--)
  366. {
  367. if (m_map[i] == elem)
  368. return i;
  369. }
  370. return java.lang.Integer.MIN_VALUE;
  371. }
  372. /*
  373. * Reset the array to the supplied size.
  374. *
  375. * @param size
  376. */
  377. public final void setToSize(int size) {
  378. Object newMap[] = new Object[size];
  379. System.arraycopy(m_map, 0, newMap, 0, m_firstFree);
  380. m_mapSize = size;
  381. m_map = newMap;
  382. }
  383. /**
  384. * Returns clone of current ObjectVector
  385. *
  386. * @return clone of current ObjectVector
  387. */
  388. public Object clone()
  389. throws CloneNotSupportedException
  390. {
  391. return new ObjectVector(this);
  392. }
  393. }