001: // Copyright (c) 2001, 2002, 2003 Per M.A. Bothner and Brainfood Inc.
002: // This is free software; for terms and warranty disclaimer see ./COPYING.
003:
004: package gnu.lists;
005:
006: import java.io.*;
007:
008: /** Simple adjustable-length vector whose elements are 32-bit floats. */
009:
010: public class F32Vector extends SimpleVector implements Externalizable
011: /* #ifdef JAVA2 */
012: , Comparable
013: /* #endif */
014: {
015: float[] data;
016: protected static float[] empty = new float[0];
017:
018: public F32Vector() {
019: data = empty;
020: }
021:
022: public F32Vector(int size, float value) {
023: float[] array = new float[size];
024: data = array;
025: this .size = size;
026: while (--size >= 0)
027: array[size] = value;
028: }
029:
030: public F32Vector(int size) {
031: this .data = new float[size];
032: this .size = size;
033: }
034:
035: public F32Vector(float[] data) {
036: this .data = data;
037: size = data.length;
038: }
039:
040: public F32Vector(Sequence seq) {
041: data = new float[seq.size()];
042: addAll(seq);
043: }
044:
045: /** Get the allocated length of the data buffer. */
046: public int getBufferLength() {
047: return data.length;
048: }
049:
050: public void setBufferLength(int length) {
051: int oldLength = data.length;
052: if (oldLength != length) {
053: float[] tmp = new float[length];
054: System.arraycopy(data, 0, tmp, 0,
055: oldLength < length ? oldLength : length);
056: data = tmp;
057: }
058: }
059:
060: protected Object getBuffer() {
061: return data;
062: }
063:
064: public final int intAtBuffer(int index) {
065: return (int) data[index];
066: }
067:
068: public final float floatAt(int index) {
069: if (index >= size)
070: throw new ArrayIndexOutOfBoundsException();
071: return data[index];
072: }
073:
074: public final float floatAtBuffer(int index) {
075: return data[index];
076: }
077:
078: public final Object get(int index) {
079: if (index > size)
080: throw new IndexOutOfBoundsException();
081: return Convert.toObject(data[index]);
082: }
083:
084: public final Object getBuffer(int index) {
085: return Convert.toObject(data[index]);
086: }
087:
088: public final void setFloatAt(int index, float value) {
089: if (index > size)
090: throw new IndexOutOfBoundsException();
091: data[index] = value;
092: }
093:
094: public final void setFloatAtBuffer(int index, float value) {
095: data[index] = value;
096: }
097:
098: public final Object setBuffer(int index, Object value) {
099: Object old = Convert.toObject(data[index]);
100: data[index] = Convert.toFloat(value);
101: return old;
102: }
103:
104: protected void clearBuffer(int start, int count) {
105: while (--count >= 0)
106: data[start++] = 0;
107: }
108:
109: public int getElementKind() {
110: return FLOAT_VALUE;
111: }
112:
113: public String getTag() {
114: return "f32";
115: }
116:
117: public boolean consumeNext(int ipos, Consumer out) {
118: int index = ipos >>> 1;
119: if (index >= size)
120: return false;
121: out.writeFloat(data[index]);
122: return true;
123: }
124:
125: public void consumePosRange(int iposStart, int iposEnd, Consumer out) {
126: if (out.ignoring())
127: return;
128: int i = iposStart >>> 1;
129: int end = iposEnd >>> 1;
130: for (; i < end; i++)
131: out.writeFloat(data[i]);
132: }
133:
134: public int compareTo(Object obj) {
135: F32Vector vec2 = (F32Vector) obj;
136: float[] arr1 = data;
137: float[] arr2 = vec2.data;
138: int n1 = size;
139: int n2 = vec2.size;
140: int n = n1 > n2 ? n2 : n1;
141: for (int i = 0; i < n; i++) {
142: float v1 = arr1[i];
143: float v2 = arr2[i];
144: if (v1 != v2)
145: return v1 > v2 ? 1 : -1;
146: }
147: return n1 - n2;
148: }
149:
150: /**
151: * @serialData Write 'size' (using writeInt),
152: * followed by 'size' elements in order (using writeFloat).
153: */
154: public void writeExternal(ObjectOutput out) throws IOException {
155: int size = this .size;
156: out.writeInt(size);
157: for (int i = 0; i < size; i++)
158: out.writeFloat(data[i]);
159: }
160:
161: public void readExternal(ObjectInput in) throws IOException,
162: ClassNotFoundException {
163: int size = in.readInt();
164: float[] data = new float[size];
165: for (int i = 0; i < size; i++)
166: data[i] = in.readFloat();
167: this.data = data;
168: this.size = size;
169: }
170: }
|