KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > DirectIntArray


1 /*
2  * Copyright (c) 2000-2001 Sosnoski Software Solutions, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20  * IN THE SOFTWARE.
21  */

22
23 /**
24  * Growable <code>int</code> array providing the same functionality as Vector
25  * provides for Object subclasses. The underlying array used for storage of
26  * values doubles in size each time more space is required, up to an optional
27  * maximum growth increment specified by the user.<p>
28  *
29  * This test class differs from the
30  * <code>com.sosnoski.util.array.IntArray</code> class in that the subset of
31  * methods it implements are coded inline, rather than using base classes. This
32  * lets us measure the performance difference caused by the use of a base class
33  * hierarchy. It does not include all public methods available from
34  * <code>IntArray</code>.
35  *
36  * @author Dennis M. Sosnoski
37  * @version 1.0
38  */

39
40 public class DirectIntArray
41 {
42
43     /**
44      * The number of values currently present in the array.
45      */

46     protected int countPresent;
47
48     /**
49      * Maximum size increment for growing array.
50      */

51     protected int maximumGrowth;
52
53     /**
54      * The underlying array used for storing the data.
55      */

56     protected int[] baseArray;
57
58     /**
59      * Constructor with full specification.
60      *
61      * @param size number of int values initially allowed in array
62      * @param growth maximum size increment for growing array
63      */

64     
65     public DirectIntArray(int size, int growth) {
66         maximumGrowth = growth;
67         baseArray = new int[size];
68     }
69
70     /**
71      * Constructor with only initial size specified.
72      *
73      * @param size number of int values initially allowed in array
74      */

75     
76     public DirectIntArray(int size) {
77         this(size, Integer.MAX_VALUE);
78     }
79
80     /**
81      * Copy (clone) constructor.
82      *
83      * @param base instance being copied
84      */

85     
86     public DirectIntArray(DirectIntArray base) {
87         this(base.baseArray.length, base.maximumGrowth);
88         System.arraycopy(base.baseArray, 0, baseArray, 0, baseArray.length);
89         countPresent = base.countPresent;
90     }
91
92     /**
93      * Increase the size of the array to at least a specified size. The array
94      * will normally be at least doubled in size, but if a maximum size
95      * increment was specified in the constructor and the value is less than
96      * the current size of the array, the maximum increment will be used
97      * instead. If the requested size requires more than the default growth,
98      * the requested size overrides the normal growth and determines the size
99      * of the replacement array.
100      *
101      * @param required new minimum size required
102      */

103     
104     protected void growArray(int required) {
105         int size = Math.max(required,
106             baseArray.length + Math.min(baseArray.length, maximumGrowth));
107         int[] grown = new int[size];
108         System.arraycopy(baseArray, 0, grown, 0, baseArray.length);
109         baseArray = grown;
110     }
111
112     /**
113      * Add a value to the array, appending it after the current values.
114      *
115      * @param value value to be added
116      * @return index number of added element
117      */

118     
119     public final int add(int value) {
120         int index = countPresent++;
121         if (countPresent > baseArray.length) {
122             growArray(countPresent);
123         }
124         baseArray[index] = value;
125         return index;
126     }
127
128     /**
129      * Add a value at a specified index in the array.
130      *
131      * @param index index position at which to insert element
132      * @param value value to be inserted into array
133      */

134     
135     public void add(int index, int value) {
136         if (index >= 0 && index <= countPresent) {
137             if (++countPresent > baseArray.length) {
138                 growArray(countPresent);
139             }
140             if (index < countPresent) {
141                 System.arraycopy(baseArray, index, baseArray, index + 1,
142                     countPresent - index - 1);
143             }
144             baseArray[index] = value;
145         } else {
146             throw new ArrayIndexOutOfBoundsException JavaDoc("Invalid index value");
147         }
148     }
149
150     /**
151      * Remove a value from the array. All values above the index removed
152      * are moved down one index position.
153      *
154      * @param index index number of value to be removed
155      */

156     
157     public void remove(int index) {
158         if (index >= 0 && index < countPresent) {
159             if (index < --countPresent){
160                 System.arraycopy(baseArray, index + 1, baseArray, index,
161                     countPresent - index);
162                 baseArray[countPresent] = 0;
163             }
164         } else {
165             throw new ArrayIndexOutOfBoundsException JavaDoc("Invalid index value");
166         }
167     }
168
169     /**
170      * Ensure that the array has the capacity for at least the specified
171      * number of values.
172      *
173      * @param min minimum capacity to be guaranteed
174      */

175     
176     public final void ensureCapacity(int min) {
177         if (min > baseArray.length) {
178             growArray(min);
179         }
180     }
181
182     /**
183      * Set the array to the empty state.
184      */

185     
186     public final void clear() {
187         countPresent = 0;
188     }
189
190     /**
191      * Get the number of values currently present in the array.
192      *
193      * @return count of values present
194      */

195     
196     public final int size() {
197         return countPresent;
198     }
199
200     /**
201      * Sets the number of values currently present in the array. If the new
202      * size is greater than the current size, the added values are initialized
203      * to 0.
204      *
205      * @param count number of values to be set
206      */

207     
208     public void setSize(int count) {
209         if (count > baseArray.length) {
210             growArray(count);
211         } else if (count < countPresent) {
212             for (int i = count; i < countPresent; i++) {
213                 baseArray[i] = 0;
214             }
215         }
216         countPresent = count;
217     }
218
219     /**
220      * Retrieve the value present at an index position in the array.
221      *
222      * @param index index position for value to be retrieved
223      * @return value from position in the array
224      */

225     
226     public final int get(int index) {
227         if (index < countPresent) {
228             return baseArray[index];
229         } else {
230             throw new ArrayIndexOutOfBoundsException JavaDoc("Invalid index value");
231         }
232     }
233
234     /**
235      * Set the value at an index position in the array.
236      *
237      * @param index index position to be set
238      * @param value value to be set
239      */

240     
241     public final void set(int index, int value) {
242         if (index < countPresent) {
243             baseArray[index] = value;
244         } else {
245             throw new ArrayIndexOutOfBoundsException JavaDoc("Invalid index value");
246         }
247     }
248
249     /**
250      * Constructs and returns a simple array containing the same data as held
251      * in this growable array.
252      *
253      * @return array containing a copy of the data
254      */

255     
256     public int[] toArray() {
257         int[] copy = new int[countPresent];
258         System.arraycopy(baseArray, 0, copy, 0, countPresent);
259         return copy;
260     }
261
262     /**
263      * Duplicates the object with the generic call.
264      *
265      * @return a copy of the object
266      */

267     
268     public Object JavaDoc clone() {
269         return new DirectIntArray(this);
270     }
271 }
272
Popular Tags