KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > lists > F64Vector


1 // Copyright (c) 2001, 2002, 2003 Per M.A. Bothner and Brainfood Inc.
2
// This is free software; for terms and warranty disclaimer see ./COPYING.
3

4 package gnu.lists;
5 import java.io.*;
6
7 /** Simple adjustable-length vector whose elements are 64-bit floats. */
8
9 public class F64Vector extends SimpleVector
10   implements Externalizable
11   /* #ifdef JAVA2 */
12   , Comparable JavaDoc
13   /* #endif */
14 {
15   double[] data;
16   protected static double[] empty = new double[0];
17
18   public F64Vector ()
19   {
20     data = empty;
21   }
22
23   public F64Vector(int size, double value)
24   {
25     double[] array = new double[size];
26     data = array;
27     this.size = size;
28     while (--size >= 0)
29       array[size] = value;
30   }
31
32   public F64Vector(int size)
33   {
34     this.data = new double[size];
35     this.size = size;
36   }
37
38   public F64Vector (double[] data)
39   {
40     this.data = data;
41     size = data.length;
42   }
43
44   public F64Vector(Sequence seq)
45   {
46     data = new double[seq.size()];
47     addAll(seq);
48   }
49
50   /** Get the allocated length of the data buffer. */
51   public int getBufferLength()
52   {
53     return data.length;
54   }
55
56   public void setBufferLength(int length)
57   {
58     int oldLength = data.length;
59     if (oldLength != length)
60       {
61     double[] tmp = new double[length];
62     System.arraycopy(data, 0, tmp, 0,
63              oldLength < length ? oldLength : length);
64     data = tmp;
65       }
66   }
67
68   protected Object JavaDoc getBuffer() { return data; }
69
70   public final double doubleAt(int index)
71   {
72     if (index >= size)
73       throw new ArrayIndexOutOfBoundsException JavaDoc();
74     return data[index];
75   }
76
77   public final double doubleAtBuffer(int index)
78   {
79     return data[index];
80   }
81
82   public final Object JavaDoc get (int index)
83   {
84     if (index > size)
85       throw new IndexOutOfBoundsException JavaDoc();
86     return Convert.toObject(data[index]);
87   }
88
89   public final Object JavaDoc getBuffer (int index)
90   {
91     return Convert.toObject(data[index]);
92   }
93
94   public final int intAtBuffer(int index)
95   {
96     return (int) data[index];
97   }
98
99   public final void setDoubleAt(int index, double value)
100   {
101     if (index > size)
102       throw new IndexOutOfBoundsException JavaDoc();
103     data[index] = value;
104   }
105
106   public final void setDoubleAtBuffer(int index, double value)
107   {
108     data[index] = value;
109   }
110
111   public final Object JavaDoc setBuffer(int index, Object JavaDoc value)
112   {
113     Object JavaDoc old = Convert.toObject(data[index]);
114     data[index] = Convert.toDouble(value);
115     return old;
116   }
117
118   /*
119   public final void setElementAt(Object value, int index)
120   {
121     data[index] = ((Number) value).doubleValue();
122   }
123   */

124
125   protected void clearBuffer(int start, int count)
126   {
127     while (--count >= 0)
128       data[start++] = 0;
129   }
130
131   public int getElementKind()
132   {
133     return DOUBLE_VALUE;
134   }
135
136   public String JavaDoc getTag() { return "f64"; }
137
138   public boolean consumeNext (int ipos, Consumer out)
139   {
140     int index = ipos >>> 1;
141     if (index >= size)
142       return false;
143     out.writeDouble(data[index]);
144     return true;
145   }
146
147   public void consumePosRange (int iposStart, int iposEnd, Consumer out)
148   {
149     if (out.ignoring())
150       return;
151     int i = iposStart >>> 1;
152     int end = iposEnd >>> 1;
153     for (; i < end; i++)
154       out.writeDouble(data[i]);
155   }
156
157   /*
158   public final void print(int index, java.io.PrintWriter ps)
159   {
160     ps.print(getDouble(index));
161   }
162   */

163
164   public int compareTo(Object JavaDoc obj)
165   {
166     F64Vector vec2 = (F64Vector) obj;
167     double[] arr1 = data;
168     double[] arr2 = vec2.data;
169     int n1 = size;
170     int n2 = vec2.size;
171     int n = n1 > n2 ? n2 : n1;
172     for (int i = 0; i < n; i++)
173       {
174     double v1 = arr1[i];
175     double v2 = arr2[i];
176     if (v1 != v2)
177       return v1 > v2 ? 1 : -1;
178       }
179     return n1 - n2;
180   }
181
182   /**
183    * @serialData Write 'size' (using writeInt),
184    * followed by 'size' elements in order (using writeDouble).
185    */

186   public void writeExternal(ObjectOutput out) throws IOException
187   {
188     int size = this.size;
189     out.writeInt(size);
190     for (int i = 0; i < size; i++)
191       out.writeDouble(data[i]);
192   }
193
194   public void readExternal(ObjectInput in)
195     throws IOException, ClassNotFoundException JavaDoc
196   {
197     int size = in.readInt();
198     double[] data = new double[size];
199     for (int i = 0; i < size; i++)
200       data[i] = in.readDouble();
201     this.data = data;
202     this.size = size;
203   }
204 }
205
Popular Tags