1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package net.jbeans.j3d.data;
24
25 /* ------------------ Import classes (packages) ------------------- *//package-summary/html">class="comment"> ------------------ Import classes (packages) ------------------- *//package-summary.html">class="comment">/* ------------------ Import classes (packages) ------------------- *//package-summary.html">class="comment"> ------------------ Import classes (packages) ------------------- */
26
27
28
29
30
31 /***
32 * IntArray is an array of (primitive) integers.
33 *
34 * @version $Revision: 1.3 $
35 * @author Masahiro Takatsuka (masa@jbeans.net)
36 * @see DataArray
37 */
38
39 public class IntArray extends DataArray {
40 /***
41 * Construct a new IntArray object.
42 * <PRE>
43 * </PRE>
44 *
45 * @param dim the dimension of the tuple.
46 * @return a new IntArray object.
47 */
48 public IntArray(int dim) {
49 super(dim);
50 }
51
52 public IntArray() {
53 this(1);
54 }
55
56 public IntArray(DataArray da) {
57 this(da.dimension);
58 copy(da);
59 }
60
61 public void copy(DataArray da) {
62 super.copy(da);
63 if (da instanceof IntArray) {
64 IntArray ia = (IntArray) da;
65 System.arraycopy((int[]) ia.array, 0, (int[]) this.array, 0, ((int[]) ia.array).length);
66 }
67 }
68
69 /***
70 * Virtual constructor creates object of same type as this object.
71 */
72 public DataArray makeObject() {
73 return new IntArray(this.dimension);
74 }
75
76 /***
77 * This method lets the user specify data to be held by the array. The
78 * array argument is a pointer to the data. size is the size of
79 * the array supplied by the user. Set save to <code>true</code>
80 * to keep the class from deleting the array when it cleans up or
81 * reallocates memory.
82 * The class uses the actual array provided; it does not copy the data
83 * from the suppled array.
84 *
85 * @param array
86 */
87 public void setArray(int[] array) {
88 this.array = array;
89 this.size = ((int[]) this.array).length;
90 this.maxId = this.size - 1;
91 }
92
93 /***
94 * resize the array.
95 */
96 protected Object resize(int size) {
97 int[] newArray;
98 int newSize;
99
100 if (size > this.size ) {
101 newSize = this.size + size;
102 } else if (size == this.size) {
103 return this.array;
104 } else {
105 newSize = size;
106 }
107
108 if (newSize <= 0) {
109 initialize();
110 return null;
111 }
112
113 newArray = new int[newSize];
114 if (newArray == null ){
115 return null;
116 }
117
118 if (this.array != null) {
119 System.arraycopy((int[]) this.array, 0, newArray, 0, (size < this.size ? size : this.size));
120 }
121
122 this.size = newSize;
123 this.array = newArray;
124
125 return this.array;
126 }
127
128 /***
129 * Allocate memory for this array. Delete old storage only if necessary.
130 */
131 public boolean allocate(int size) {
132
133 this.size = size > 0 ? size : 1;
134 this.array = new int[this.size];
135 if (this.array == null) {
136 return false;
137 }
138
139 this.maxId = -1;
140
141 return true;
142 }
143
144 /***
145 * Specify the number of values for this object to hold. Does an
146 * allocation as well as setting the maxId ivar. Used in conjunction with
147 * setValue() method for fast insertion.
148 */
149 public void setNumberOfValues(int number) {
150 allocate(number);
151 this.maxId = number - 1;
152 }
153
154 /***
155 * Set the number of n-tuples in the array.
156 *
157 * @param number
158 */
159 public void setNumberOfTuples(int number) {
160 setNumberOfValues(number * this.dimension);
161 }
162
163 /***
164 * Get a copy of a tuple at the <code>i</code>th location.
165 *
166 * @param i
167 */
168 public double[] getTuple(int i) {
169 if (this.tupleT.length != this.dimension) {
170 this.tupleT = new double[this.dimension];
171 }
172 int k = i * this.dimension;
173 for (int j = 0; j < this.dimension; j++) {
174 this.tupleT[j] = ((int[]) this.array)[k++];
175 }
176 return this.tupleT;
177 }
178
179 /***
180 * Copy the tuple value into a user-provided array.
181 *
182 * @param i
183 * @param tuple
184 */
185 public void getTuple(int i, double[] tuple) {
186 int k = i * this.dimension;
187 for (int j = 0; j < this.dimension; j++) {
188 tuple[j] = ((int[]) this.array)[k++];
189 }
190 }
191
192 /***
193 * Set the tuple value at the ith location in the array.
194 *
195 * @param i
196 * @param tuple
197 */
198 public void setTuple(int i, double[] tuple) {
199 int k = i * this.dimension;
200 for (int j = 0; j < this.dimension; j++) {
201 ((int[]) this.array)[k++] = (int) tuple[j];
202 }
203 }
204
205 /***
206 * Insert (memory allocation performed) the tuple into the ith location
207 * in the array.
208 *
209 * @param i
210 * @param tuple
211 */
212 public void insertTuple(int i, double[] tuple) {
213 int k = i * this.dimension;
214 ensureSpace(k, this.dimension);
215 for (int j = 0; j < this.dimension; j++) {
216 ((int[]) this.array)[k++] = (int) tuple[j];
217 }
218 }
219
220 /***
221 * Insert (memory allocation performed) the tuple onto the end of
222 * the array.
223 *
224 * @param tuple
225 */
226 public int insertNextTuple(double[] tuple) {
227 int j = this.maxId + 1;
228 ensureSpace(j, this.dimension);
229
230 for (int i = 0; i < this.dimension; i++) {
231 ((int[]) this.array)[j++] = (int) tuple[i];
232 }
233
234 return this.maxId / this.dimension;
235 }
236
237 /***
238 * Get the data type.
239 */
240 public int getDataType() {
241 return DataType.INT;
242 }
243
244 /***
245 * Get the data at a particular index.
246 */
247 public int getValue(int id) {
248 return ((int[]) this.array)[id];
249 }
250
251 /***
252 * Set the data at a particular index. Does not do range checking.
253 * Make sure you use the method SetNumberOfValues() before inserting data.
254 */
255 public void setValue(int id, int value) {
256 ((int[]) this.array)[id] = value;
257 }
258
259 /***
260 * Insert data at a specified position in the array.
261 */
262 public void insertValue(int id, int i) {
263 if (id >= this.size) {
264 resize(id + 1);
265 }
266 ((int[]) this.array)[id] = i;
267 if (id > this.maxId) {
268 this.maxId = id;
269 }
270 }
271
272 /***
273 * Insert data at the end of the array. Return its location in the array.
274 */
275 public int insertNextValue(int i) {
276 insertValue(++this.maxId, i);
277 return this.maxId;
278 }
279
280 /***
281 * Return the data element at the <code>i</code>th tuple and
282 * <code>j</code>th element location.
283 * Note that <code>i</code> is less than number of tuples and
284 * <code>j</code> is less than the dimension.
285 * <PRE>
286 * </PRE>
287 *
288 * @param i the index to the <code>i</code>th tuple.
289 * @param j the index to the <code>j</code>th element of the tuple.
290 * @return element.
291 */
292 public double getElement(int i, int j) throws ArrayIndexOutOfBoundsException {
293 if ((i < 0 || i >= getNumberOfTuples()) ||
294 (j < 0 || j >= this.dimension)) {
295 throw new ArrayIndexOutOfBoundsException();
296 }
297 return (double) getValue(i * this.dimension + j);
298 }
299
300 /***
301 * Set the data element at the <code>i</code>th tuple and
302 * <code>j</code>th element location.
303 * Note that <code>i</code> is less than number of tuples and
304 * <code>j</code> is less than the dimension.
305 * <PRE>
306 * </PRE>
307 *
308 * @param i the index to the <code>i</code>th tuple.
309 * @param j the index to the <code>j</code>th element of the tuple.
310 * @param e the element to be set.
311 * @return void
312 */
313 public void setElement(int i, int j, double e) throws ArrayIndexOutOfBoundsException {
314 if ((i < 0) || (j < 0 || j >= this.dimension)) {
315 throw new ArrayIndexOutOfBoundsException();
316 }
317 setValue(i * this.dimension + j, (int) e);
318 }
319
320 /***
321 * Insert the data element at <code>i</code>th tuple and
322 * <code>j</code>th element location.
323 * <PRE>
324 * </PRE>
325 *
326 * @param (parameter-name) (description)
327 * @return void
328 */
329 public void insertElement(int i, int j, double e) throws ArrayIndexOutOfBoundsException {
330 if ((i < 0) || (j < 0 || j >= this.dimension)) {
331 throw new ArrayIndexOutOfBoundsException();
332 }
333 insertValue(i * this.dimension + j, (int) e);
334 }
335 }
336