View Javadoc

1   /* -------------------------------------------------------------------
2    * Java source file for the class BitArray
3    * 
4    * Copyright (c), 2003, Masahiro Takatsuka.
5    * All Rights Researved.
6    * 
7    * Original Author: Masahiro Takatsuka (masa@jbeans.net)
8    * $Author: takatsukam $
9    * 
10   * $Date: 2004/03/03 11:53:05 $
11   * 
12   * $Id: BitArray.java,v 1.3 2004/03/03 11:53:05 takatsukam Exp $
13   * 
14   * Reference:		Document no:
15   * ___				___
16   * 
17   * To Do:
18   * ___
19   * 
20  ------------------------------------------------------------------- */
21  
22  /* --------------------------- Package ---------------------------- */
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  // import foo.*;
27  
28  /*====================================================================
29                     Implementation of class BitArray                   
30  ====================================================================*/
31  /***
32   * BitArray is an array of bits (0/1 data value).  
33   * 
34   * @version $Revision: 1.3 $
35   * @author Masahiro Takatsuka (masa@jbeans.net)
36   * @see DataArray
37   */
38  
39  public class BitArray extends DataArray {
40  	/***
41  	 * Construct a new BitArray object.
42  	 * <PRE>
43  	 * </PRE>
44  	 * 
45  	 * @param dim the dimension of the tuple.
46  	 * @return a new BitArray object.
47  	 */
48  	public BitArray(int dim) {
49  		super(dim);
50  	}
51  	
52  	public BitArray() {
53  		this(1);
54  	}
55  
56  	public BitArray(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 BitArray) {
64  			BitArray ba = (BitArray) da;
65  			System.arraycopy((byte[]) ba.array, 0, (byte[]) this.array, 0, ((byte[]) ba.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 BitArray(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  //  		if (size > this.size ) {
81  			this.size = size > 0 ? size : 1;
82  			this.array = new byte[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  		byte[] 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 byte[newSize];
113 		if (newArray == null ){
114 			return null;
115 		}
116 		
117 		if (this.array != null) {
118 			System.arraycopy((byte[]) 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.BIT;
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] = ((byte[]) this.array)[k++]; // byte to double
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] = ((byte[]) 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 		  setValue(k + j, (int) 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 		for (int j = 0; j < this.dimension; j++) {
184 			insertValue(k + j, (int) tuple[j]);
185 		}
186 	}
187   
188 	/***
189 	 * Insert (memory allocation performed) the tuple onto the end of the
190 	 * array.
191 	 */
192 	public int insertNextTuple(double[] tuple) {
193 		for (int i = 0; i < this.dimension; i++) {
194 			insertNextValue((int) tuple[i]);
195 		}
196 			
197 		return this.maxId / this.dimension;
198 	}
199 	
200 	/***
201 	 * Get the data at a particular index.
202 	 */
203 	public int getValue(int id) {
204 		if ((((byte[]) this.array)[id / 8] != 0) & ((0x80 >> (id % 8)) != 0)) {
205 			return 1;
206 		}
207 		return 0;
208 	}
209 
210 	public boolean getBit(int id) {
211 		return (getValue(id) != 0) ? true : false;
212 	}
213 	
214 	/***
215 	 * Specify the number of values for this object to hold. Does an
216 	 * allocation as well as setting the MaxId ivar. Used in conjunction with
217 	 * SetValue() method for fast insertion.
218 	 */
219 	public void setNumberOfValues(int number) {
220 		allocate(number);
221 		this.maxId = number - 1;
222 	}
223 
224 	/***
225 	 * Set the data at a particular index. Does not do range checking.
226 	 * Make sure you use the method SetNumberOfValues() before inserting data.
227 	 */
228 	public void setValue(int id, int value) {
229 		if (value != 0) {
230 			((byte[]) this.array)[id / 8] |= (0x80 >> id % 8);
231 		} else {
232 			((byte[]) this.array)[id / 8] &= (~(0x80 >> id % 8));
233 		}
234 	}
235 
236 	public void setBit(int id, boolean value) {
237 		setValue(id, (value) ? 1 : 0);
238 	}
239 
240 	/***
241 	 * Insert data at a specified position in the array.
242 	 */
243 	public void insertValue(int id, int i) {
244 		if (id >= this.size) {
245 			resize(id + 1);
246 		}
247 		if (i != 0) {
248 			((byte[]) this.array)[id / 8] |= (0x80 >> id % 8);
249 		} else {
250 			((byte[]) this.array)[id / 8] &= (~(0x80 >> id % 8));
251 		}
252 		if (id > this.maxId) {
253 			this.maxId = id;
254 		}
255 	}
256 
257 	public void insertBit(int id, boolean value) {
258 		insertValue(id, (value) ? 1 : 0);
259 	}
260 	
261 	/***
262 	 * Insert data at the end of the array. Return its location in the array.
263 	 */
264 	public int insertNextValue(int i) {
265 		insertValue(++this.maxId, i);
266 		return this.maxId;
267 	}
268 
269 	public int insertNextBit(boolean value) {
270 		return insertNextValue((value) ? 1 : 0);		
271 	}
272 	
273 	/***
274 	 * This method lets the user specify data to be held by the array.  The 
275 	 * array argument is a pointer to the data.  size is the size of 
276 	 * the array supplied by the user.  Set save to 1 to keep the class
277 	 * from deleting the array when it cleans up or reallocates memory.
278 	 * The class uses the actual array provided; it does not copy the data 
279 	 * from the suppled array.
280 	 */
281 	public void setArray(byte[] array) {
282 		this.array = array;
283 		this.size = ((byte[]) this.array).length;
284 		this.maxId = this.size - 1;
285 	}
286 }