001: /*
002: * Copyright 2002-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: /*
017: * $Id$
018: */
019: package org.apache.xml.utils;
020:
021: /**
022: * A very simple table that stores a list of objects.
023: *
024: * This version is based on a "realloc" strategy -- a simle array is
025: * used, and when more storage is needed, a larger array is obtained
026: * and all existing data is recopied into it. As a result, read/write
027: * access to existing nodes is O(1) fast but appending may be O(N**2)
028: * slow.
029: * @xsl.usage internal
030: */
031: public class ObjectVector implements Cloneable {
032:
033: /** Size of blocks to allocate */
034: protected int m_blocksize;
035:
036: /** Array of objects */
037: protected Object m_map[];
038:
039: /** Number of ints in array */
040: protected int m_firstFree = 0;
041:
042: /** Size of array */
043: protected int m_mapSize;
044:
045: /**
046: * Default constructor. Note that the default
047: * block size is very small, for small lists.
048: */
049: public ObjectVector() {
050:
051: m_blocksize = 32;
052: m_mapSize = m_blocksize;
053: m_map = new Object[m_blocksize];
054: }
055:
056: /**
057: * Construct a IntVector, using the given block size.
058: *
059: * @param blocksize Size of block to allocate
060: */
061: public ObjectVector(int blocksize) {
062:
063: m_blocksize = blocksize;
064: m_mapSize = blocksize;
065: m_map = new Object[blocksize];
066: }
067:
068: /**
069: * Construct a IntVector, using the given block size.
070: *
071: * @param blocksize Size of block to allocate
072: */
073: public ObjectVector(int blocksize, int increaseSize) {
074:
075: m_blocksize = increaseSize;
076: m_mapSize = blocksize;
077: m_map = new Object[blocksize];
078: }
079:
080: /**
081: * Copy constructor for ObjectVector
082: *
083: * @param v Existing ObjectVector to copy
084: */
085: public ObjectVector(ObjectVector v) {
086: m_map = new Object[v.m_mapSize];
087: m_mapSize = v.m_mapSize;
088: m_firstFree = v.m_firstFree;
089: m_blocksize = v.m_blocksize;
090: System.arraycopy(v.m_map, 0, m_map, 0, m_firstFree);
091: }
092:
093: /**
094: * Get the length of the list.
095: *
096: * @return length of the list
097: */
098: public final int size() {
099: return m_firstFree;
100: }
101:
102: /**
103: * Get the length of the list.
104: *
105: * @return length of the list
106: */
107: public final void setSize(int sz) {
108: m_firstFree = sz;
109: }
110:
111: /**
112: * Append an object onto the vector.
113: *
114: * @param value Object to add to the list
115: */
116: public final void addElement(Object value) {
117:
118: if ((m_firstFree + 1) >= m_mapSize) {
119: m_mapSize += m_blocksize;
120:
121: Object newMap[] = new Object[m_mapSize];
122:
123: System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
124:
125: m_map = newMap;
126: }
127:
128: m_map[m_firstFree] = value;
129:
130: m_firstFree++;
131: }
132:
133: /**
134: * Append several Object values onto the vector.
135: *
136: * @param value Object to add to the list
137: */
138: public final void addElements(Object value, int numberOfElements) {
139:
140: if ((m_firstFree + numberOfElements) >= m_mapSize) {
141: m_mapSize += (m_blocksize + numberOfElements);
142:
143: Object newMap[] = new Object[m_mapSize];
144:
145: System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
146:
147: m_map = newMap;
148: }
149:
150: for (int i = 0; i < numberOfElements; i++) {
151: m_map[m_firstFree] = value;
152: m_firstFree++;
153: }
154: }
155:
156: /**
157: * Append several slots onto the vector, but do not set the values.
158: *
159: * @param numberOfElements number of slots to append
160: */
161: public final void addElements(int numberOfElements) {
162:
163: if ((m_firstFree + numberOfElements) >= m_mapSize) {
164: m_mapSize += (m_blocksize + numberOfElements);
165:
166: Object newMap[] = new Object[m_mapSize];
167:
168: System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
169:
170: m_map = newMap;
171: }
172:
173: m_firstFree += numberOfElements;
174: }
175:
176: /**
177: * Inserts the specified object in this vector at the specified index.
178: * Each component in this vector with an index greater or equal to
179: * the specified index is shifted upward to have an index one greater
180: * than the value it had previously.
181: *
182: * @param value Object to insert
183: * @param at Index of where to insert
184: */
185: public final void insertElementAt(Object value, int at) {
186:
187: if ((m_firstFree + 1) >= m_mapSize) {
188: m_mapSize += m_blocksize;
189:
190: Object newMap[] = new Object[m_mapSize];
191:
192: System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
193:
194: m_map = newMap;
195: }
196:
197: if (at <= (m_firstFree - 1)) {
198: System
199: .arraycopy(m_map, at, m_map, at + 1, m_firstFree
200: - at);
201: }
202:
203: m_map[at] = value;
204:
205: m_firstFree++;
206: }
207:
208: /**
209: * Remove all elements objects from the list.
210: */
211: public final void removeAllElements() {
212:
213: for (int i = 0; i < m_firstFree; i++) {
214: m_map[i] = null;
215: }
216:
217: m_firstFree = 0;
218: }
219:
220: /**
221: * Removes the first occurrence of the argument from this vector.
222: * If the object is found in this vector, each component in the vector
223: * with an index greater or equal to the object's index is shifted
224: * downward to have an index one smaller than the value it had
225: * previously.
226: *
227: * @param s Object to remove from array
228: *
229: * @return True if the object was removed, false if it was not found
230: */
231: public final boolean removeElement(Object s) {
232:
233: for (int i = 0; i < m_firstFree; i++) {
234: if (m_map[i] == s) {
235: if ((i + 1) < m_firstFree)
236: System.arraycopy(m_map, i + 1, m_map, i - 1,
237: m_firstFree - i);
238: else
239: m_map[i] = null;
240:
241: m_firstFree--;
242:
243: return true;
244: }
245: }
246:
247: return false;
248: }
249:
250: /**
251: * Deletes the component at the specified index. Each component in
252: * this vector with an index greater or equal to the specified
253: * index is shifted downward to have an index one smaller than
254: * the value it had previously.
255: *
256: * @param i index of where to remove an object
257: */
258: public final void removeElementAt(int i) {
259:
260: if (i > m_firstFree)
261: System.arraycopy(m_map, i + 1, m_map, i, m_firstFree);
262: else
263: m_map[i] = null;
264:
265: m_firstFree--;
266: }
267:
268: /**
269: * Sets the component at the specified index of this vector to be the
270: * specified object. The previous component at that position is discarded.
271: *
272: * The index must be a value greater than or equal to 0 and less
273: * than the current size of the vector.
274: *
275: * @param value object to set
276: * @param index Index of where to set the object
277: */
278: public final void setElementAt(Object value, int index) {
279: m_map[index] = value;
280: }
281:
282: /**
283: * Get the nth element.
284: *
285: * @param i index of object to get
286: *
287: * @return object at given index
288: */
289: public final Object elementAt(int i) {
290: return m_map[i];
291: }
292:
293: /**
294: * Tell if the table contains the given Object.
295: *
296: * @param s object to look for
297: *
298: * @return true if the object is in the list
299: */
300: public final boolean contains(Object s) {
301:
302: for (int i = 0; i < m_firstFree; i++) {
303: if (m_map[i] == s)
304: return true;
305: }
306:
307: return false;
308: }
309:
310: /**
311: * Searches for the first occurence of the given argument,
312: * beginning the search at index, and testing for equality
313: * using the equals method.
314: *
315: * @param elem object to look for
316: * @param index Index of where to begin search
317: * @return the index of the first occurrence of the object
318: * argument in this vector at position index or later in the
319: * vector; returns -1 if the object is not found.
320: */
321: public final int indexOf(Object elem, int index) {
322:
323: for (int i = index; i < m_firstFree; i++) {
324: if (m_map[i] == elem)
325: return i;
326: }
327:
328: return java.lang.Integer.MIN_VALUE;
329: }
330:
331: /**
332: * Searches for the first occurence of the given argument,
333: * beginning the search at index, and testing for equality
334: * using the equals method.
335: *
336: * @param elem object to look for
337: * @return the index of the first occurrence of the object
338: * argument in this vector at position index or later in the
339: * vector; returns -1 if the object is not found.
340: */
341: public final int indexOf(Object elem) {
342:
343: for (int i = 0; i < m_firstFree; i++) {
344: if (m_map[i] == elem)
345: return i;
346: }
347:
348: return java.lang.Integer.MIN_VALUE;
349: }
350:
351: /**
352: * Searches for the first occurence of the given argument,
353: * beginning the search at index, and testing for equality
354: * using the equals method.
355: *
356: * @param elem Object to look for
357: * @return the index of the first occurrence of the object
358: * argument in this vector at position index or later in the
359: * vector; returns -1 if the object is not found.
360: */
361: public final int lastIndexOf(Object elem) {
362:
363: for (int i = (m_firstFree - 1); i >= 0; i--) {
364: if (m_map[i] == elem)
365: return i;
366: }
367:
368: return java.lang.Integer.MIN_VALUE;
369: }
370:
371: /*
372: * Reset the array to the supplied size.
373: *
374: * @param size
375: */
376: public final void setToSize(int size) {
377:
378: Object newMap[] = new Object[size];
379:
380: System.arraycopy(m_map, 0, newMap, 0, m_firstFree);
381: m_mapSize = size;
382:
383: m_map = newMap;
384:
385: }
386:
387: /**
388: * Returns clone of current ObjectVector
389: *
390: * @return clone of current ObjectVector
391: */
392: public Object clone() throws CloneNotSupportedException {
393: return new ObjectVector(this);
394: }
395: }
|