KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > utils > ObjectVector


1 /*
2  * Copyright 2002-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$
18  */

19 package com.sun.org.apache.xml.internal.utils;
20
21 /**
22  * A very simple table that stores a list of objects.
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.
29  * @xsl.usage internal
30  */

31 public class ObjectVector implements Cloneable JavaDoc
32 {
33
34   /** Size of blocks to allocate */
35   protected int m_blocksize;
36
37   /** Array of objects */
38   protected Object JavaDoc m_map[];
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 ObjectVector()
51   {
52
53     m_blocksize = 32;
54     m_mapSize = m_blocksize;
55     m_map = new Object JavaDoc[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 ObjectVector(int blocksize)
64   {
65
66     m_blocksize = blocksize;
67     m_mapSize = blocksize;
68     m_map = new Object JavaDoc[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 ObjectVector(int blocksize, int increaseSize)
77   {
78
79     m_blocksize = increaseSize;
80     m_mapSize = blocksize;
81     m_map = new Object JavaDoc[blocksize];
82   }
83
84   /**
85    * Copy constructor for ObjectVector
86    *
87    * @param v Existing ObjectVector to copy
88    */

89   public ObjectVector(ObjectVector v)
90   {
91     m_map = new Object JavaDoc[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 an object onto the vector.
121    *
122    * @param value Object to add to the list
123    */

124   public final void addElement(Object JavaDoc value)
125   {
126
127     if ((m_firstFree + 1) >= m_mapSize)
128     {
129       m_mapSize += m_blocksize;
130
131       Object JavaDoc newMap[] = new Object JavaDoc[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 Object values onto the vector.
145    *
146    * @param value Object to add to the list
147    */

148   public final void addElements(Object JavaDoc value, int numberOfElements)
149   {
150
151     if ((m_firstFree + numberOfElements) >= m_mapSize)
152     {
153       m_mapSize += (m_blocksize+numberOfElements);
154
155       Object JavaDoc newMap[] = new Object JavaDoc[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 numberOfElements number of slots to append
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       Object JavaDoc newMap[] = new Object JavaDoc[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 object 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 Object to insert
199    * @param at Index of where to insert
200    */

201   public final void insertElementAt(Object JavaDoc value, int at)
202   {
203
204     if ((m_firstFree + 1) >= m_mapSize)
205     {
206       m_mapSize += m_blocksize;
207
208       Object JavaDoc newMap[] = new Object JavaDoc[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    * Remove all elements objects from the list.
227    */

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

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

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

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

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

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

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

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

391   public final int lastIndexOf(Object JavaDoc elem)
392   {
393
394     for (int i = (m_firstFree - 1); i >= 0; i--)
395     {
396       if (m_map[i] == elem)
397         return i;
398     }
399
400     return java.lang.Integer.MIN_VALUE;
401   }
402   
403   /*
404    * Reset the array to the supplied size.
405    *
406    * @param size
407    */

408   public final void setToSize(int size) {
409     
410     Object JavaDoc newMap[] = new Object JavaDoc[size];
411     
412     System.arraycopy(m_map, 0, newMap, 0, m_firstFree);
413     m_mapSize = size;
414
415     m_map = newMap;
416     
417   }
418   
419   /**
420    * Returns clone of current ObjectVector
421    *
422    * @return clone of current ObjectVector
423    */

424   public Object JavaDoc clone()
425     throws CloneNotSupportedException JavaDoc
426   {
427     return new ObjectVector(this);
428   }
429 }
430
Popular Tags