Decode directly as float
@@ -51,15 +51,22 @@ public class DenseBinaryFormat implements BinaryFormat {
private void encodeCells(GrowableByteBuffer buffer, Tensor tensor) {
switch (serializationValueType) {
- case DOUBLE: encodeCells(tensor, buffer::putDouble); break;
- case FLOAT: encodeCells(tensor, (i) -> buffer.putFloat(i.floatValue())); break;
+ case DOUBLE: encodeDoubleCells(tensor, buffer); break;
+ case FLOAT: encodeFloatCells(tensor, buffer); break;
}
}
- private void encodeCells(Tensor tensor, Consumer<Double> consumer) {
+ private void encodeDoubleCells(Tensor tensor, GrowableByteBuffer buffer) {
Iterator<Double> i = tensor.valueIterator();
while (i.hasNext()) {
- consumer.accept(i.next());
+ buffer.putDouble(i.next());
+ }
+ }
+
+ private void encodeFloatCells(Tensor tensor, GrowableByteBuffer buffer) {
+ Iterator<Double> i = tensor.valueIterator(); // TODO: floatValueIterator
+ while (i.hasNext()) {
+ buffer.putFloat(i.next().floatValue());
}
}
@@ -106,14 +113,19 @@ public class DenseBinaryFormat implements BinaryFormat {
private void decodeCells(DimensionSizes sizes, GrowableByteBuffer buffer, IndexedTensor.BoundBuilder builder) {
switch (serializationValueType) {
- case DOUBLE: decodeCells(sizes, builder, buffer::getDouble); break;
- case FLOAT: decodeCells(sizes, builder, () -> (double)buffer.getFloat()); break;
+ case DOUBLE: decodeDoubleCells(sizes, builder, buffer); break;
+ case FLOAT: decodeFloatCells(sizes, builder, buffer); break;
}
}
- private void decodeCells(DimensionSizes sizes, IndexedTensor.BoundBuilder builder, Supplier<Double> supplier) {
+ private void decodeDoubleCells(DimensionSizes sizes, IndexedTensor.BoundBuilder builder, GrowableByteBuffer buffer) {
+ for (long i = 0; i < sizes.totalSize(); i++)
+ builder.cellByDirectIndex(i, buffer.getDouble());
+ }
+
+ private void decodeFloatCells(DimensionSizes sizes, IndexedTensor.BoundBuilder builder, GrowableByteBuffer buffer) {
for (long i = 0; i < sizes.totalSize(); i++)
- builder.cellByDirectIndex(i, supplier.get());
+ builder.cellByDirectIndex(i, buffer.getFloat());
}
}
