1 4 package gnu.lists; 5 import java.io.*; 6 7 8 9 public class F32Vector extends SimpleVector 10 implements Externalizable 11 12 , Comparable 13 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 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 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 (); 79 return data[index]; 80 } 81 82 public final float floatAtBuffer(int index) 83 { 84 return data[index]; 85 } 86 87 public final Object get (int index) 88 { 89 if (index > size) 90 throw new IndexOutOfBoundsException (); 91 return Convert.toObject(data[index]); 92 } 93 94 public final Object 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 (); 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 setBuffer(int index, Object value) 112 { 113 Object 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 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 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 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 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 |