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 64-bit floats. */
009:
010: public class F64Vector extends SimpleVector implements Externalizable
011: /* #ifdef JAVA2 */
012: , Comparable
013: /* #endif */
014: {
015: double[] data;
016: protected static double[] empty = new double[0];
017:
018: public F64Vector() {
019: data = empty;
020: }
021:
022: public F64Vector(int size, double value) {
023: double[] array = new double[size];
024: data = array;
025: this .size = size;
026: while (--size >= 0)
027: array[size] = value;
028: }
029:
030: public F64Vector(int size) {
031: this .data = new double[size];
032: this .size = size;
033: }
034:
035: public F64Vector(double[] data) {
036: this .data = data;
037: size = data.length;
038: }
039:
040: public F64Vector(Sequence seq) {
041: data = new double[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: double[] tmp = new double[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 double doubleAt(int index) {
065: if (index >= size)
066: throw new ArrayIndexOutOfBoundsException();
067: return data[index];
068: }
069:
070: public final double doubleAtBuffer(int index) {
071: return data[index];
072: }
073:
074: public final Object get(int index) {
075: if (index > size)
076: throw new IndexOutOfBoundsException();
077: return Convert.toObject(data[index]);
078: }
079:
080: public final Object getBuffer(int index) {
081: return Convert.toObject(data[index]);
082: }
083:
084: public final int intAtBuffer(int index) {
085: return (int) data[index];
086: }
087:
088: public final void setDoubleAt(int index, double value) {
089: if (index > size)
090: throw new IndexOutOfBoundsException();
091: data[index] = value;
092: }
093:
094: public final void setDoubleAtBuffer(int index, double 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.toDouble(value);
101: return old;
102: }
103:
104: /*
105: public final void setElementAt(Object value, int index)
106: {
107: data[index] = ((Number) value).doubleValue();
108: }
109: */
110:
111: protected void clearBuffer(int start, int count) {
112: while (--count >= 0)
113: data[start++] = 0;
114: }
115:
116: public int getElementKind() {
117: return DOUBLE_VALUE;
118: }
119:
120: public String getTag() {
121: return "f64";
122: }
123:
124: public boolean consumeNext(int ipos, Consumer out) {
125: int index = ipos >>> 1;
126: if (index >= size)
127: return false;
128: out.writeDouble(data[index]);
129: return true;
130: }
131:
132: public void consumePosRange(int iposStart, int iposEnd, Consumer out) {
133: if (out.ignoring())
134: return;
135: int i = iposStart >>> 1;
136: int end = iposEnd >>> 1;
137: for (; i < end; i++)
138: out.writeDouble(data[i]);
139: }
140:
141: /*
142: public final void print(int index, java.io.PrintWriter ps)
143: {
144: ps.print(getDouble(index));
145: }
146: */
147:
148: public int compareTo(Object obj) {
149: F64Vector vec2 = (F64Vector) obj;
150: double[] arr1 = data;
151: double[] arr2 = vec2.data;
152: int n1 = size;
153: int n2 = vec2.size;
154: int n = n1 > n2 ? n2 : n1;
155: for (int i = 0; i < n; i++) {
156: double v1 = arr1[i];
157: double v2 = arr2[i];
158: if (v1 != v2)
159: return v1 > v2 ? 1 : -1;
160: }
161: return n1 - n2;
162: }
163:
164: /**
165: * @serialData Write 'size' (using writeInt),
166: * followed by 'size' elements in order (using writeDouble).
167: */
168: public void writeExternal(ObjectOutput out) throws IOException {
169: int size = this .size;
170: out.writeInt(size);
171: for (int i = 0; i < size; i++)
172: out.writeDouble(data[i]);
173: }
174:
175: public void readExternal(ObjectInput in) throws IOException,
176: ClassNotFoundException {
177: int size = in.readInt();
178: double[] data = new double[size];
179: for (int i = 0; i < size; i++)
180: data[i] = in.readDouble();
181: this.data = data;
182: this.size = size;
183: }
184: }
|