KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > lists > F32Vector


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 32-bit floats. */
8
9 public class F32Vector extends SimpleVector
10   implements Externalizable
11   /* #ifdef JAVA2 */
12   , Comparable JavaDoc
13   /* #endif */
14 {
15   float[] data;
16   protected static float[] empty = new float[0];
17
18   public F32Vector ()
19   {
20     data = empty;
21   }
22
23   public F32Vector(int size, float value)
24   {
25     float[] array = new float[size];
26     data = array;
27     this.size = size;
28     while (--size >= 0)
29       array[size] = value;
30   }
31
32   public F32Vector(int size)
33   {
34     this.data = new float[size];
35     this.size = size;
36   }
37
38   public F32Vector (float[] data)
39   {
40     this.data = data;
41     size = data.length;
42   }
43
44   public F32Vector(Sequence seq)
45   {
46     data = new float[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     float[] tmp = new float[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 int intAtBuffer(int index)
71   {
72     return (int) data[index];
73   }
74
75   public final float floatAt(int index)
76   {
77     if (index >= size)
78       throw new ArrayIndexOutOfBoundsException JavaDoc();
79     return data[index];
80   }
81
82   public final float floatAtBuffer(int index)
83   {
84     return data[index];
85   }
86
87   public final Object JavaDoc get (int index)
88   {
89     if (index > size)
90       throw new IndexOutOfBoundsException JavaDoc();
91     return Convert.toObject(data[index]);
92   }
93
94   public final Object JavaDoc getBuffer (int index)
95   {
96     return Convert.toObject(data[index]);
97   }
98
99   public final void setFloatAt(int index, float value)
100   {
101     if (index > size)
102       throw new IndexOutOfBoundsException JavaDoc();
103     data[index] = value;
104   }
105
106   public final void setFloatAtBuffer(int index, float 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.toFloat(value);
115     return old;
116   }
117
118   protected void clearBuffer(int start, int count)
119   {
120     while (--count >= 0)
121       data[start++] = 0;
122   }
123
124   public int getElementKind()
125   {
126     return FLOAT_VALUE;
127   }
128
129   public String JavaDoc getTag() { return "f32"; }
130
131   public boolean consumeNext (int ipos, Consumer out)
132   {
133     int index = ipos >>> 1;
134     if (index >= size)
135       return false;
136     out.writeFloat(data[index]);
137     return true;
138   }
139
140     public void consumePosRange (int iposStart, int iposEnd, Consumer out)
141   {
142     if (out.ignoring())
143       return;
144     int i = iposStart >>> 1;
145     int end = iposEnd >>> 1;
146     for (; i < end; i++)
147       out.writeFloat(data[i]);
148   }
149
150   public int compareTo(Object JavaDoc obj)
151   {
152     F32Vector vec2 = (F32Vector) obj;
153     float[] arr1 = data;
154     float[] arr2 = vec2.data;
155     int n1 = size;
156     int n2 = vec2.size;
157     int n = n1 > n2 ? n2 : n1;
158     for (int i = 0; i < n; i++)
159       {
160     float v1 = arr1[i];
161     float v2 = arr2[i];
162     if (v1 != v2)
163       return v1 > v2 ? 1 : -1;
164       }
165     return n1 - n2;
166   }
167
168   /**
169    * @serialData Write 'size' (using writeInt),
170    * followed by 'size' elements in order (using writeFloat).
171    */

172   public void writeExternal(ObjectOutput out) throws IOException
173   {
174     int size = this.size;
175     out.writeInt(size);
176     for (int i = 0; i < size; i++)
177       out.writeFloat(data[i]);
178   }
179
180   public void readExternal(ObjectInput in)
181     throws IOException, ClassNotFoundException JavaDoc
182   {
183     int size = in.readInt();
184     float[] data = new float[size];
185     for (int i = 0; i < size; i++)
186       data[i] = in.readFloat();
187     this.data = data;
188     this.size = size;
189   }
190 }
191
Popular Tags