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