KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xml > utils > IntVector


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 org.apache.xml.utils;
20
21 /**
22  * A very simple table that stores a list of int.
23  *
24  * This version is based on a "realloc" strategy -- a simle array is
25  * used, and when more storage is needed, a larger array is obtained
26  * and all existing data is recopied into it. As a result, read/write
27  * access to existing nodes is O(1) fast but appending may be O(N**2)
28  * slow. See also SuballocatedIntVector.
29  * @xsl.usage internal
30  */

31 public class IntVector implements Cloneable JavaDoc
32 {
33
34   /** Size of blocks to allocate */
35   protected int m_blocksize;
36
37   /** Array of ints */
38   protected int m_map[]; // IntStack is trying to see this directly
39

40   /** Number of ints in array */
41   protected int m_firstFree = 0;
42
43   /** Size of array */
44   protected int m_mapSize;
45
46   /**
47    * Default constructor. Note that the default
48    * block size is very small, for small lists.
49    */

50   public IntVector()
51   {
52
53     m_blocksize = 32;
54     m_mapSize = m_blocksize;
55     m_map = new int[m_blocksize];
56   }
57
58   /**
59    * Construct a IntVector, using the given block size.
60    *
61    * @param blocksize Size of block to allocate
62    */

63   public IntVector(int blocksize)
64   {
65
66     m_blocksize = blocksize;
67     m_mapSize = blocksize;
68     m_map = new int[blocksize];
69   }
70   
71   /**
72    * Construct a IntVector, using the given block size.
73    *
74    * @param blocksize Size of block to allocate
75    */

76   public IntVector(int blocksize, int increaseSize)
77   {
78
79     m_blocksize = increaseSize;
80     m_mapSize = blocksize;
81     m_map = new int[blocksize];
82   }
83
84   /**
85    * Copy constructor for IntVector
86    *
87    * @param v Existing IntVector to copy
88    */

89   public IntVector(IntVector v)
90   {
91     m_map = new int[v.m_mapSize];
92     m_mapSize = v.m_mapSize;
93     m_firstFree = v.m_firstFree;
94     m_blocksize = v.m_blocksize;
95     System.arraycopy(v.m_map, 0, m_map, 0, m_firstFree);
96   }
97
98   /**
99    * Get the length of the list.
100    *
101    * @return length of the list
102    */

103   public final int size()
104   {
105     return m_firstFree;
106   }
107   
108   /**
109    * Get the length of the list.
110    *
111    * @return length of the list
112    */

113   public final void setSize(int sz)
114   {
115     m_firstFree = sz;
116   }
117
118
119   /**
120    * Append a int onto the vector.
121    *
122    * @param value Int to add to the list
123    */

124   public final void addElement(int value)
125   {
126
127     if ((m_firstFree + 1) >= m_mapSize)
128     {
129       m_mapSize += m_blocksize;
130
131       int newMap[] = new int[m_mapSize];
132
133       System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
134
135       m_map = newMap;
136     }
137
138     m_map[m_firstFree] = value;
139
140     m_firstFree++;
141   }
142   
143   /**
144    * Append several int values onto the vector.
145    *
146    * @param value Int to add to the list
147    */

148   public final void addElements(int value, int numberOfElements)
149   {
150
151     if ((m_firstFree + numberOfElements) >= m_mapSize)
152     {
153       m_mapSize += (m_blocksize+numberOfElements);
154
155       int newMap[] = new int[m_mapSize];
156
157       System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
158
159       m_map = newMap;
160     }
161
162     for (int i = 0; i < numberOfElements; i++)
163     {
164       m_map[m_firstFree] = value;
165       m_firstFree++;
166     }
167   }
168   
169   /**
170    * Append several slots onto the vector, but do not set the values.
171    *
172    * @param value Int to add to the list
173    */

174   public final void addElements(int numberOfElements)
175   {
176
177     if ((m_firstFree + numberOfElements) >= m_mapSize)
178     {
179       m_mapSize += (m_blocksize+numberOfElements);
180
181       int newMap[] = new int[m_mapSize];
182
183       System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
184
185       m_map = newMap;
186     }
187     
188     m_firstFree += numberOfElements;
189   }
190   
191
192   /**
193    * Inserts the specified node in this vector at the specified index.
194    * Each component in this vector with an index greater or equal to
195    * the specified index is shifted upward to have an index one greater
196    * than the value it had previously.
197    *
198    * @param value Int to insert
199    * @param at Index of where to insert
200    */

201   public final void insertElementAt(int value, int at)
202   {
203
204     if ((m_firstFree + 1) >= m_mapSize)
205     {
206       m_mapSize += m_blocksize;
207
208       int newMap[] = new int[m_mapSize];
209
210       System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
211
212       m_map = newMap;
213     }
214
215     if (at <= (m_firstFree - 1))
216     {
217       System.arraycopy(m_map, at, m_map, at + 1, m_firstFree - at);
218     }
219
220     m_map[at] = value;
221
222     m_firstFree++;
223   }
224
225   /**
226    * Inserts the specified node in this vector at the specified index.
227    * Each component in this vector with an index greater or equal to
228    * the specified index is shifted upward to have an index one greater
229    * than the value it had previously.
230    */

231   public final void removeAllElements()
232   {
233
234     for (int i = 0; i < m_firstFree; i++)
235     {
236       m_map[i] = java.lang.Integer.MIN_VALUE;
237     }
238
239     m_firstFree = 0;
240   }
241
242   /**
243    * Removes the first occurrence of the argument from this vector.
244    * If the object is found in this vector, each component in the vector
245    * with an index greater or equal to the object's index is shifted
246    * downward to have an index one smaller than the value it had
247    * previously.
248    *
249    * @param s Int to remove from array
250    *
251    * @return True if the int was removed, false if it was not found
252    */

253   public final boolean removeElement(int s)
254   {
255
256     for (int i = 0; i < m_firstFree; i++)
257     {
258       if (m_map[i] == s)
259       {
260         if ((i + 1) < m_firstFree)
261           System.arraycopy(m_map, i + 1, m_map, i - 1, m_firstFree - i);
262         else
263           m_map[i] = java.lang.Integer.MIN_VALUE;
264
265         m_firstFree--;
266
267         return true;
268       }
269     }
270
271     return false;
272   }
273
274   /**
275    * Deletes the component at the specified index. Each component in
276    * this vector with an index greater or equal to the specified
277    * index is shifted downward to have an index one smaller than
278    * the value it had previously.
279    *
280    * @param i index of where to remove and int
281    */

282   public final void removeElementAt(int i)
283   {
284
285     if (i > m_firstFree)
286       System.arraycopy(m_map, i + 1, m_map, i, m_firstFree);
287     else
288       m_map[i] = java.lang.Integer.MIN_VALUE;
289
290     m_firstFree--;
291   }
292
293   /**
294    * Sets the component at the specified index of this vector to be the
295    * specified object. The previous component at that position is discarded.
296    *
297    * The index must be a value greater than or equal to 0 and less
298    * than the current size of the vector.
299    *
300    * @param node object to set
301    * @param index Index of where to set the object
302    */

303   public final void setElementAt(int value, int index)
304   {
305     m_map[index] = value;
306   }
307
308   /**
309    * Get the nth element.
310    *
311    * @param i index of object to get
312    *
313    * @return object at given index
314    */

315   public final int elementAt(int i)
316   {
317     return m_map[i];
318   }
319
320   /**
321    * Tell if the table contains the given node.
322    *
323    * @param s object to look for
324    *
325    * @return true if the object is in the list
326    */

327   public final boolean contains(int s)
328   {
329
330     for (int i = 0; i < m_firstFree; i++)
331     {
332       if (m_map[i] == s)
333         return true;
334     }
335
336     return false;
337   }
338
339   /**
340    * Searches for the first occurence of the given argument,
341    * beginning the search at index, and testing for equality
342    * using the equals method.
343    *
344    * @param elem object to look for
345    * @param index Index of where to begin search
346    * @return the index of the first occurrence of the object
347    * argument in this vector at position index or later in the
348    * vector; returns -1 if the object is not found.
349    */

350   public final int indexOf(int elem, int index)
351   {
352
353     for (int i = index; i < m_firstFree; i++)
354     {
355       if (m_map[i] == elem)
356         return i;
357     }
358
359     return java.lang.Integer.MIN_VALUE;
360   }
361
362   /**
363    * Searches for the first occurence of the given argument,
364    * beginning the search at index, and testing for equality
365    * using the equals method.
366    *
367    * @param elem object to look for
368    * @return the index of the first occurrence of the object
369    * argument in this vector at position index or later in the
370    * vector; returns -1 if the object is not found.
371    */

372   public final int indexOf(int elem)
373   {
374
375     for (int i = 0; i < m_firstFree; i++)
376     {
377       if (m_map[i] == elem)
378         return i;
379     }
380
381     return java.lang.Integer.MIN_VALUE;
382   }
383
384   /**
385    * Searches for the first occurence of the given argument,
386    * beginning the search at index, and testing for equality
387    * using the equals method.
388    *
389    * @param elem Object to look for
390    * @return the index of the first occurrence of the object
391    * argument in this vector at position index or later in the
392    * vector; returns -1 if the object is not found.
393    */

394   public final int lastIndexOf(int elem)
395   {
396
397     for (int i = (m_firstFree - 1); i >= 0; i--)
398     {
399       if (m_map[i] == elem)
400         return i;
401     }
402
403     return java.lang.Integer.MIN_VALUE;
404   }
405   
406   /**
407    * Returns clone of current IntVector
408    *
409    * @return clone of current IntVector
410    */

411   public Object JavaDoc clone()
412     throws CloneNotSupportedException JavaDoc
413   {
414     return new IntVector(this);
415   }
416   
417 }
418
Popular Tags