001: /*
002: * Copyright 1999-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: IntVector.java,v 1.9 2005/01/23 00:52:41 mcnamara Exp $
018: */
019: package org.apache.xml.utils;
020:
021: /**
022: * A very simple table that stores a list of int.
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. See also SuballocatedIntVector.
029: * @xsl.usage internal
030: */
031: public class IntVector implements Cloneable {
032:
033: /** Size of blocks to allocate */
034: protected int m_blocksize;
035:
036: /** Array of ints */
037: protected int m_map[]; // IntStack is trying to see this directly
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 IntVector() {
050:
051: m_blocksize = 32;
052: m_mapSize = m_blocksize;
053: m_map = new int[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 IntVector(int blocksize) {
062:
063: m_blocksize = blocksize;
064: m_mapSize = blocksize;
065: m_map = new int[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 IntVector(int blocksize, int increaseSize) {
074:
075: m_blocksize = increaseSize;
076: m_mapSize = blocksize;
077: m_map = new int[blocksize];
078: }
079:
080: /**
081: * Copy constructor for IntVector
082: *
083: * @param v Existing IntVector to copy
084: */
085: public IntVector(IntVector v) {
086: m_map = new int[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 a int onto the vector.
113: *
114: * @param value Int to add to the list
115: */
116: public final void addElement(int value) {
117:
118: if ((m_firstFree + 1) >= m_mapSize) {
119: m_mapSize += m_blocksize;
120:
121: int newMap[] = new int[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 int values onto the vector.
135: *
136: * @param value Int to add to the list
137: */
138: public final void addElements(int value, int numberOfElements) {
139:
140: if ((m_firstFree + numberOfElements) >= m_mapSize) {
141: m_mapSize += (m_blocksize + numberOfElements);
142:
143: int newMap[] = new int[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 Int to add to the list
160: */
161: public final void addElements(int numberOfElements) {
162:
163: if ((m_firstFree + numberOfElements) >= m_mapSize) {
164: m_mapSize += (m_blocksize + numberOfElements);
165:
166: int newMap[] = new int[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 node 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 Int to insert
183: * @param at Index of where to insert
184: */
185: public final void insertElementAt(int value, int at) {
186:
187: if ((m_firstFree + 1) >= m_mapSize) {
188: m_mapSize += m_blocksize;
189:
190: int newMap[] = new int[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: * Inserts the specified node in this vector at the specified index.
210: * Each component in this vector with an index greater or equal to
211: * the specified index is shifted upward to have an index one greater
212: * than the value it had previously.
213: */
214: public final void removeAllElements() {
215:
216: for (int i = 0; i < m_firstFree; i++) {
217: m_map[i] = java.lang.Integer.MIN_VALUE;
218: }
219:
220: m_firstFree = 0;
221: }
222:
223: /**
224: * Removes the first occurrence of the argument from this vector.
225: * If the object is found in this vector, each component in the vector
226: * with an index greater or equal to the object's index is shifted
227: * downward to have an index one smaller than the value it had
228: * previously.
229: *
230: * @param s Int to remove from array
231: *
232: * @return True if the int was removed, false if it was not found
233: */
234: public final boolean removeElement(int s) {
235:
236: for (int i = 0; i < m_firstFree; i++) {
237: if (m_map[i] == s) {
238: if ((i + 1) < m_firstFree)
239: System.arraycopy(m_map, i + 1, m_map, i - 1,
240: m_firstFree - i);
241: else
242: m_map[i] = java.lang.Integer.MIN_VALUE;
243:
244: m_firstFree--;
245:
246: return true;
247: }
248: }
249:
250: return false;
251: }
252:
253: /**
254: * Deletes the component at the specified index. Each component in
255: * this vector with an index greater or equal to the specified
256: * index is shifted downward to have an index one smaller than
257: * the value it had previously.
258: *
259: * @param i index of where to remove and int
260: */
261: public final void removeElementAt(int i) {
262:
263: if (i > m_firstFree)
264: System.arraycopy(m_map, i + 1, m_map, i, m_firstFree);
265: else
266: m_map[i] = java.lang.Integer.MIN_VALUE;
267:
268: m_firstFree--;
269: }
270:
271: /**
272: * Sets the component at the specified index of this vector to be the
273: * specified object. The previous component at that position is discarded.
274: *
275: * The index must be a value greater than or equal to 0 and less
276: * than the current size of the vector.
277: *
278: * @param value object to set
279: * @param index Index of where to set the object
280: */
281: public final void setElementAt(int value, int index) {
282: m_map[index] = value;
283: }
284:
285: /**
286: * Get the nth element.
287: *
288: * @param i index of object to get
289: *
290: * @return object at given index
291: */
292: public final int elementAt(int i) {
293: return m_map[i];
294: }
295:
296: /**
297: * Tell if the table contains the given node.
298: *
299: * @param s object to look for
300: *
301: * @return true if the object is in the list
302: */
303: public final boolean contains(int s) {
304:
305: for (int i = 0; i < m_firstFree; i++) {
306: if (m_map[i] == s)
307: return true;
308: }
309:
310: return false;
311: }
312:
313: /**
314: * Searches for the first occurence of the given argument,
315: * beginning the search at index, and testing for equality
316: * using the equals method.
317: *
318: * @param elem object to look for
319: * @param index Index of where to begin search
320: * @return the index of the first occurrence of the object
321: * argument in this vector at position index or later in the
322: * vector; returns -1 if the object is not found.
323: */
324: public final int indexOf(int elem, int index) {
325:
326: for (int i = index; i < m_firstFree; i++) {
327: if (m_map[i] == elem)
328: return i;
329: }
330:
331: return java.lang.Integer.MIN_VALUE;
332: }
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(int elem) {
345:
346: for (int i = 0; i < m_firstFree; i++) {
347: if (m_map[i] == elem)
348: return i;
349: }
350:
351: return java.lang.Integer.MIN_VALUE;
352: }
353:
354: /**
355: * Searches for the first occurence of the given argument,
356: * beginning the search at index, and testing for equality
357: * using the equals method.
358: *
359: * @param elem Object to look for
360: * @return the index of the first occurrence of the object
361: * argument in this vector at position index or later in the
362: * vector; returns -1 if the object is not found.
363: */
364: public final int lastIndexOf(int elem) {
365:
366: for (int i = (m_firstFree - 1); i >= 0; i--) {
367: if (m_map[i] == elem)
368: return i;
369: }
370:
371: return java.lang.Integer.MIN_VALUE;
372: }
373:
374: /**
375: * Returns clone of current IntVector
376: *
377: * @return clone of current IntVector
378: */
379: public Object clone() throws CloneNotSupportedException {
380: return new IntVector(this);
381: }
382:
383: }
|