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