001: /*
002: * $Id: AnyFloatArray.java,v 1.13 2002/09/16 08:05:02 jkl Exp $
003: *
004: * Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
005: *
006: * Use is subject to license terms, as defined in
007: * Anvil Sofware License, Version 1.1. See LICENSE
008: * file, or http://njet.org/license-1.1.txt
009: */
010: package anvil.core.arrays;
011:
012: import anvil.core.Any;
013: import anvil.core.AnyDouble;
014: import anvil.core.AnySequence;
015: import java.util.Comparator;
016: import java.io.Writer;
017:
018: /// @class FloatArray
019:
020: /**
021: * class AnyFloatArray
022: *
023: * @author: Jani Lehtimäki
024: */
025: public class AnyFloatArray extends AnyArray {
026:
027: /// @constructor FloatArray
028: /// @synopsis FloatArray(range size)
029: /// @synopsis FloatArray(object element, ...)
030: public static final Any newInstance(Any[] parameters) {
031: int n = parameters.length;
032: if (n == 0) {
033: return new AnyFloatArray(new float[0]);
034: }
035: if (parameters[0].isRange()) {
036: return new AnyFloatArray(new float[parameters[0].sizeOf()]);
037: }
038: float[] array = new float[n];
039: for (int i = 0; i < n; i++) {
040: array[i] = (float) parameters[i].toDouble();
041: }
042: return new AnyFloatArray(array);
043: }
044:
045: public static final anvil.script.compiler.NativeClass __class__ = new anvil.script.compiler.NativeClass(
046: "FloatArray", AnyFloatArray.class, AnySequence.__class__,
047: //DOC{{
048: "" + " @class FloatArray\n" + " @constructor FloatArray\n"
049: + " @synopsis FloatArray(range size)\n"
050: + " @synopsis FloatArray(object element, ...)\n"
051: //}}DOC
052: );
053: static {
054: ArraysModule.class.getName();
055: }
056:
057: public float[] _array;
058: public int _size;
059:
060: public AnyFloatArray(float[] array) {
061: _array = array;
062: _size = array.length;
063: }
064:
065: public int getSize() {
066: return _size;
067: }
068:
069: public AnySequence setSize(int size) {
070: _size = size;
071: return this ;
072: }
073:
074: public AnySequence clear() {
075: java.util.Arrays.fill(_array, 0, _size, (float) 0);
076: _size = 0;
077: return this ;
078: }
079:
080: public void ensureCapacity(int size) {
081: if (_array.length < size) {
082: float[] array = new float[size + (size / 8)];
083: System.arraycopy(_array, 0, array, 0, _size);
084: _array = array;
085: }
086: }
087:
088: public Any getElement(int index) {
089: return Any.create(_array[index]);
090: }
091:
092: public AnySequence setElement(int index, Any element) {
093: _array[index] = (float) element.toDouble();
094: return this ;
095: }
096:
097: public AnySequence crop(int start, int length) {
098: System.arraycopy(_array, start, _array, 0, length);
099: setSize(length);
100: return this ;
101: }
102:
103: public AnySequence getSlice(int start, int length) {
104: float[] slice = new float[length];
105: System.arraycopy(_array, start, slice, 0, length);
106: return new AnyFloatArray(slice);
107: }
108:
109: public AnySequence deleteSlice(int start, int length) {
110: float[] array = _array;
111: int size = _size;
112: if (start + length < size) {
113: System.arraycopy(array, start + length, array, start, size
114: - (start + length));
115: }
116: setSize(size - length);
117: return this ;
118: }
119:
120: public AnySequence setSlice(int start, int length, Any element) {
121: int size = _size;
122: ensureCapacity(size - length + 1);
123: float[] array = _array;
124: if (start + length < size) {
125: System.arraycopy(array, start + length, array, start + 1,
126: size - (start + length));
127: }
128: array[start] = (float) element.toDouble();
129: setSize(size + 1 - length);
130: return this ;
131: }
132:
133: public AnySequence setSlice(int start, int length,
134: AnySequence sequence) {
135: float[] seq = (float[]) sequence.toObject();
136: int seq_length = sequence.getSize();
137: int size = _size;
138: ensureCapacity(size - length + seq_length);
139: float[] array = _array;
140: if (start + length < size) {
141: System.arraycopy(array, start + length, array, start
142: + seq_length, size - (start + length));
143: }
144: System.arraycopy(seq, 0, array, start, seq_length);
145: setSize(size + seq_length - length);
146: return this ;
147: }
148:
149: public AnySequence append(AnySequence sequence) {
150: float[] seq = (float[]) sequence.toObject();
151: int seq_length = sequence.getSize();
152: int size = _size;
153: ensureCapacity(size + seq_length);
154: float[] array = _array;
155: System.arraycopy(seq, 0, array, size, seq_length);
156: setSize(size + seq_length);
157: return this ;
158: }
159:
160: public AnySequence append(Any element) {
161: int size = _size;
162: ensureCapacity(size + 1);
163: _array[size] = (float) element.toDouble();
164: setSize(size + 1);
165: return this ;
166: }
167:
168: public AnySequence createSequence(Any element) {
169: return new AnyFloatArray(new float[] { (float) element
170: .toDouble() });
171: }
172:
173: public AnySequence createEmptySequence() {
174: return new AnyFloatArray(new float[0]);
175: }
176:
177: public int compareAt(AnySequence sequence, int start, int length) {
178: float[] haystack = _array;
179: float[] needle = (float[]) sequence.toObject();
180: float a;
181: float b;
182: for (int i = 0; i < length; i++) {
183: a = haystack[start + i];
184: b = needle[i];
185: if (a > b) {
186: return -1;
187: } else if (a > b) {
188: return 1;
189: }
190: }
191: return 0;
192: }
193:
194: public int compareAt(Any element, int start) {
195: float a = _array[start];
196: float b = (float) element.toDouble();
197: if (a > b) {
198: return -1;
199: } else if (a > b) {
200: return 1;
201: } else {
202: return 0;
203: }
204: }
205:
206: public AnySequence fill(Any fill, int start, int length) {
207: java.util.Arrays.fill(_array, start, start + length,
208: (float) fill.toDouble());
209: return this ;
210: }
211:
212: public AnySequence sort(int start, int length, Comparator comparator) {
213: java.util.Arrays.sort(_array, start, start + length);
214: return this ;
215: }
216:
217: public int search(Any element, Comparator comparator) {
218: return java.util.Arrays.binarySearch(_array, (float) element
219: .toDouble());
220: }
221:
222: public AnySequence swap(int index1, int index2) {
223: float[] array = _array;
224: float any = array[index1];
225: array[index1] = array[index2];
226: array[index2] = any;
227: return this ;
228: }
229:
230: public final anvil.script.ClassType classOf() {
231: return __class__;
232: }
233:
234: public Object toObject() {
235: return _array;
236: }
237:
238: public Any copy() {
239: int length = _size;
240: float[] array = new float[length];
241: System.arraycopy(_array, 0, array, 0, length);
242: return new AnyFloatArray(array);
243: }
244:
245: public float[] toFloatArray() {
246: if (_size < _array.length) {
247: float[] array = new float[_size];
248: System.arraycopy(_array, 0, array, 0, _size);
249: return array;
250: } else {
251: return _array;
252: }
253: }
254:
255: public Writer toJava(Writer writer) throws java.io.IOException {
256: writer.write("new anvil.core.arrays.AnyFloatArray");
257: writer.write("(new float[] {");
258: float[] array = _array;
259: int n = _size;
260: for (int i = 0; i < n; i++) {
261: if (i > 0) {
262: writer.write(',');
263: writer.write(' ');
264: }
265: writer.write(Float.toString(array[i]));
266: }
267: writer.write('}');
268: writer.write(')');
269: return writer;
270: }
271:
272: public anvil.codec.Code toCode(anvil.codec.Code code) {
273: anvil.codec.ConstantPool pool = code.getPool();
274: int clazz = pool.addClass("anvil/core/arrays/AnyFloatArray");
275: code.anew(clazz);
276: code.dup();
277: float[] array = _array;
278: final int n = _size;
279: code.iconst(n);
280: code.newarray(code.T_FLOAT);
281: for (int i = 0; i < n; i++) {
282: code.dup();
283: code.iconst(i);
284: code.fconst(array[i]);
285: code.fastore();
286: }
287: code.invokespecial(pool.addMethodRef(clazz, "<init>", "([F)V"));
288: return code;
289: }
290:
291: public void serialize(anvil.core.Serializer serializer)
292: throws java.io.IOException {
293: int size = _size;
294: float[] array = _array;
295: serializer.write('A');
296: serializer.write('f');
297: serializer.write(size);
298: serializer.write(';');
299: for (int i = 0; i < size; i++) {
300: serializer.write(array[i]);
301: serializer.write(';');
302: }
303: }
304:
305: public static final Any unserialize(
306: anvil.core.Unserializer unserializer, int size)
307: throws anvil.core.UnserializationException {
308: float[] array = new float[size];
309: for (int i = 0; i < size; i++) {
310: array[i] = unserializer.getFloat();
311: }
312: return new AnyFloatArray(array);
313: }
314:
315: }
|