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 * CharArray is an array of character values.
33 *
34 * @version $Revision: 1.3 $
35 * @author Masahiro Takatsuka (masa@jbeans.net)
36 * @see DataArray
37 */
38
39 public class CharArray extends DataArray {
40 /***
41 * Construct a new CharArray object.
42 * <PRE>
43 * </PRE>
44 *
45 * @param dim the dimension of the tuple.
46 * @return a new CharArray object.
47 */
48 public CharArray(int dim) {
49 super(dim);
50 }
51
52 public CharArray() {
53 this(1);
54 }
55
56 public CharArray(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 CharArray) {
64 CharArray ca = (CharArray) da;
65 System.arraycopy((char[]) ca.array, 0, (char[]) this.array, 0, ((char[]) ca.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 CharArray(this.dimension);
74 }
75
76 /***
77 * Allocate memory for this array. Delete old storage only if necessary.
78 */
79 public boolean allocate(int size) {
80
81 this.size = size > 0 ? size : 1;
82 this.array = new char[this.size];
83 if (this.array == null) {
84 return false;
85 }
86
87 this.maxId = -1;
88
89 return true;
90 }
91
92 /***
93 * resize the array.
94 */
95 protected Object resize(int size) {
96 char[] newArray;
97 int newSize;
98
99 if (size > this.size ) {
100 newSize = this.size + size;
101 } else if (size == this.size) {
102 return this.array;
103 } else {
104 newSize = size;
105 }
106
107 if (newSize <= 0) {
108 initialize();
109 return null;
110 }
111
112 newArray = new char[newSize];
113 if (newArray == null ){
114 return null;
115 }
116
117 if (this.array != null) {
118 System.arraycopy((char[]) this.array, 0, newArray, 0, (size < this.size ? size : this.size));
119 }
120
121 this.size = newSize;
122 this.array = newArray;
123
124 return this.array;
125 }
126
127 /***
128 * Get the data type.
129 */
130 public int getDataType() {
131 return DataType.CHAR;
132 }
133
134 /***
135 * Set the number of tuples in the array. Note that this allocates space
136 * depending on the tuple dimension.
137 */
138 public void setNumberOfTuples(int number) {
139 setNumberOfValues(number * this.dimension);
140 }
141
142 /***
143 * Get a pointer to a tuple at the ith location. This is a dangerous method
144 * (it is not thread safe since a pointer is returned).
145 */
146 public double[] getTuple(int i) {
147 if (this.tupleT.length != this.dimension) {
148 this.tupleT = new double[this.dimension];
149 }
150 int k = i * this.dimension;
151 for (int j = 0; j < this.dimension; j++) {
152 this.tupleT[j] = ((char[]) this.array)[k++];
153 }
154 return this.tupleT;
155 }
156
157 /***
158 * Copy the tuple value into a user-provided array.
159 */
160 public void getTuple(int i, double[] tuple) {
161 int k = i * this.dimension;
162 for (int j = 0; j < this.dimension; j++) {
163 tuple[j] = ((char[]) this.array)[k++];
164 }
165 }
166
167 /***
168 * Set the tuple value at the ith location in the array.
169 */
170 public void setTuple(int i, double[] tuple) {
171 int k = i * this.dimension;
172 for (int j = 0; j < this.dimension; j++) {
173 ((char[]) this.array)[k++] = (char) tuple[j];
174 }
175 }
176
177 /***
178 * Insert (memory allocation performed) the tuple into the ith location
179 * in the array.
180 */
181 public void insertTuple(int i, double[] tuple) {
182 int k = i * this.dimension;
183 ensureSpace(k, this.dimension);
184 for (int j = 0; j < this.dimension; j++) {
185 ((char[]) this.array)[k++] = (char) tuple[j];
186 }
187 }
188
189 /***
190 * Insert (memory allocation performed) the tuple onto the end of the
191 * array.
192 */
193 public int insertNextTuple(double[] tuple) {
194 int j = this.maxId + 1;
195 ensureSpace(j, this.dimension);
196
197 for (int i = 0; i < this.dimension; i++) {
198 ((char[]) this.array)[j++] = (char) tuple[i];
199 }
200
201 return this.maxId / this.dimension;
202 }
203
204 /***
205 * Return the data component at the ith tuple and jth component location.
206 * Note that i is less than NumberOfTuples and j is less than
207 * NumberOfComponents.
208 */
209 public double getElement(int i, int j) {
210 return (double) getValue(i * this.dimension + j);
211 }
212
213 /***
214 * Set the data component at the ith tuple and jth component location.
215 * Note that i is less than NumberOfTuples and j is less than
216 * NumberOfComponents. Make sure enough memory has been allocated
217 * (use SetNumberOfTuples() and SetNumberOfComponents()).
218 */
219 public void setElement(int i, int j, double c) {
220 setValue(i * this.dimension + j, (char)c);
221 }
222
223 /***
224 * Insert the data component at ith tuple and jth component location.
225 * Note that memory allocation is performed as necessary to hold the data.
226 */
227 public void insertElement(int i, int j, double c) {
228 insertValue(i * this.dimension + j, (char)c);
229 }
230
231 /***
232 * Get the data at a particular index.
233 */
234 public char getValue(int id) {
235 return ((char[]) this.array)[id];
236 }
237
238 /***
239 * Set the data at a particular index. Does not do range checking.
240 * Make sure you use the method SetNumberOfValues() before inserting data.
241 */
242 public void setValue(int id, char value) {
243 ((char[]) this.array)[id] = value;
244 }
245
246 /***
247 * Specify the number of values for this object to hold. Does an
248 * allocation as well as setting the MaxId ivar. Used in conjunction with
249 * SetValue() method for fast insertion.
250 */
251 public void setNumberOfValues(int number) {
252 allocate(number);
253 this.maxId = number - 1;
254 }
255
256 /***
257 * Insert data at a specified position in the array.
258 */
259 public void insertValue(int id, char c) {
260 if (id >= this.size) {
261 resize(id + 1);
262 }
263 ((char[]) this.array)[id] = c;
264 if (id > this.maxId) {
265 this.maxId = id;
266 }
267 }
268
269 /***
270 * Insert data at the end of the array. Return its location in the array.
271 */
272 public int insertNextValue(char c) {
273 insertValue(++this.maxId, c);
274 return this.maxId;
275 }
276
277 /***
278 * This method lets the user specify data to be held by the array. The
279 * array argument is a pointer to the data. size is the size of
280 * the array supplied by the user. Set save to 1 to keep the class
281 * from deleting the array when it cleans up or reallocates memory.
282 * The class uses the actual array provided; it does not copy the data
283 * from the suppled array.
284 */
285 public void setArray(char[] array) {
286 this.array = array;
287 this.size = ((char[]) this.array).length;
288 this.maxId = this.size - 1;
289 }
290 }
291