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 * LongArray is an array of long numbers.
33 *
34 * @version $Revision: 1.3 $
35 * @author Masahiro Takatsuka (masa@jbeans.net)
36 * @see DataArray
37 */
38
39 public class LongArray extends DataArray {
40 /***
41 * Construct a new LongArray object.
42 * <PRE>
43 * </PRE>
44 *
45 * @param dim the dimension of the tuple.
46 * @return a new LongArray object.
47 */
48 public LongArray(int dim) {
49 super(dim);
50 }
51
52 public LongArray() {
53 this(1);
54 }
55
56 public LongArray(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 LongArray) {
64 LongArray la = (LongArray) da;
65 System.arraycopy((long[]) la.array, 0, (long[]) this.array, 0, ((long[]) la.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 LongArray(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(long[] array) {
88 this.array = array;
89 this.size = ((long[]) this.array).length;
90 this.maxId = this.size - 1;
91 }
92
93 /***
94 * resize the array.
95 */
96 protected Object resize(int size) {
97 long[] 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 long[newSize];
114 if (newArray == null ){
115 return null;
116 }
117
118 if (this.array != null) {
119 System.arraycopy((long[]) 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 long[this.size];
135 if (this.array == null) {
136 return false;
137 }
138
139 this.maxId = -1;
140
141 return true;
142 }
143
144 /***
145 * Get the data type.
146 */
147 public int getDataType() {
148 return DataType.LONG;
149 }
150
151 /***
152 * Set the number of n-tuples in the array.
153 *
154 * @param number
155 */
156 public void setNumberOfTuples(int number) {
157 setNumberOfValues(number * this.dimension);
158 }
159
160 /***
161 * Get a copy of a tuple at the <code>i</code>th location.
162 *
163 * @param i
164 */
165 public double[] getTuple(int i) {
166 if (this.tupleT.length != this.dimension) {
167 this.tupleT = new double[this.dimension];
168 }
169 int k = i * this.dimension;
170 for (int j = 0; j < this.dimension; j++) {
171 this.tupleT[j] = ((long[]) this.array)[k++];
172 }
173 return this.tupleT;
174 }
175
176 /***
177 * Copy the tuple value into a user-provided array.
178 *
179 * @param i
180 * @param tuple
181 */
182 public void getTuple(int i, double[] tuple) {
183 int k = i * this.dimension;
184 for (int j = 0; j < this.dimension; j++) {
185 tuple[j] = ((long[]) this.array)[k++];
186 }
187 }
188
189 /***
190 * Set the tuple value at the ith location in the array.
191 *
192 * @param i
193 * @param tuple
194 */
195 public void setTuple(int i, double[] tuple) {
196 int k = i * this.dimension;
197 for (int j = 0; j < this.dimension; j++) {
198 ((long[]) this.array)[k++] = (long) tuple[j];
199 }
200 }
201
202 /***
203 * Insert (memory allocation performed) the tuple into the ith location
204 * in the array.
205 *
206 * @param i
207 * @param tuple
208 */
209 public void insertTuple(int i, double[] tuple) {
210 int k = i * this.dimension;
211 ensureSpace(k, this.dimension);
212 for (int j = 0; j < this.dimension; j++) {
213 ((long[]) this.array)[k++] = (long) tuple[j];
214 }
215 }
216
217 /***
218 * Insert (memory allocation performed) the tuple onto the end of
219 * the array.
220 *
221 * @param tuple
222 */
223 public int insertNextTuple(double[] tuple) {
224 int j = this.maxId + 1;
225 ensureSpace(j, this.dimension);
226
227 for (int i = 0; i < this.dimension; i++) {
228 ((long[]) this.array)[j++] = (long) tuple[i];
229 }
230
231 return this.maxId / this.dimension;
232 }
233
234 /***
235 * Get the data at a particular index.
236 */
237 public long getValue(int id) {
238 return ((long[]) this.array)[id];
239 }
240
241 /***
242 * Set the data at a particular index. Does not do range checking.
243 * Make sure you use the method SetNumberOfValues() before inserting data.
244 */
245 public void setValue(int id, long value) {
246 ((long[]) this.array)[id] = value;
247 }
248
249 /***
250 * Specify the number of values for this object to hold. Does an
251 * allocation as well as setting the maxId ivar. Used in conjunction with
252 * setValue() method for fast insertion.
253 */
254 public void setNumberOfValues(int number) {
255 allocate(number);
256 this.maxId = number - 1;
257 }
258
259
260 /***
261 * Insert data at a specified position in the array.
262 */
263 public void insertValue(int id, long i) {
264 if (id >= this.size) {
265 resize(id + 1);
266 }
267 ((long[]) this.array)[id] = i;
268 if (id > this.maxId) {
269 this.maxId = id;
270 }
271 }
272
273 /***
274 * Insert data at the end of the array. Return its location in the array.
275 */
276 public int insertNextValue(long i) {
277 insertValue(++this.maxId, i);
278 return this.maxId;
279 }
280
281 /***
282 * Return the data element at the <code>i</code>th tuple and
283 * <code>j</code>th element location.
284 * Note that <code>i</code> is less than number of tuples and
285 * <code>j</code> is less than the dimension.
286 * <PRE>
287 * </PRE>
288 *
289 * @param i the index to the <code>i</code>th tuple.
290 * @param j the index to the <code>j</code>th element of the tuple.
291 * @return element.
292 */
293 public double getElement(int i, int j) throws ArrayIndexOutOfBoundsException {
294 if ((i < 0 || i >= getNumberOfTuples()) ||
295 (j < 0 || j >= this.dimension)) {
296 throw new ArrayIndexOutOfBoundsException();
297 }
298 return (double) getValue(i * this.dimension + j);
299 }
300
301 /***
302 * Set the data element at the <code>i</code>th tuple and
303 * <code>j</code>th element location.
304 * Note that <code>i</code> is less than number of tuples and
305 * <code>j</code> is less than the dimension.
306 * <PRE>
307 * </PRE>
308 *
309 * @param i the index to the <code>i</code>th tuple.
310 * @param j the index to the <code>j</code>th element of the tuple.
311 * @param e the element to be set.
312 * @return void
313 */
314 public void setElement(int i, int j, double e) throws ArrayIndexOutOfBoundsException {
315 if ((i < 0) || (j < 0 || j >= this.dimension)) {
316 throw new ArrayIndexOutOfBoundsException();
317 }
318 setValue(i * this.dimension + j, (long) e);
319 }
320
321 /***
322 * Insert the data element at <code>i</code>th tuple and
323 * <code>j</code>th element location.
324 * <PRE>
325 * </PRE>
326 *
327 * @param (parameter-name) (description)
328 * @return void
329 */
330 public void insertElement(int i, int j, double e) throws ArrayIndexOutOfBoundsException {
331 if ((i < 0) || (j < 0 || j >= this.dimension)) {
332 throw new ArrayIndexOutOfBoundsException();
333 }
334 insertValue(i * this.dimension + j, (long) e);
335 }
336 }