下面列出了java.nio.DoubleBuffer#allocate ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Creates a new DoubleBuffer with the same contents as the given
* DoubleBuffer. The new DoubleBuffer is seperate from the old one and
* changes are not reflected across. If you want to reflect changes,
* consider using Buffer.duplicate().
*
* @param buf
* the DoubleBuffer to copy
* @return the copy
*/
public static DoubleBuffer clone(DoubleBuffer buf) {
if (buf == null) {
return null;
}
buf.rewind();
DoubleBuffer copy;
if (buf.isDirect()) {
copy = createDoubleBuffer(buf.limit());
} else {
copy = DoubleBuffer.allocate(buf.limit());
}
copy.put(buf);
return copy;
}
void Draw_Body(GL2 gl2) {
gl2.glPushMatrix();
DoubleBuffer m=DoubleBuffer.allocate(16);
m.put( 0,-body.left.x);
m.put( 1,-body.left.y);
m.put( 2,-body.left.z);
m.put( 4,body.forward.x);
m.put( 5,body.forward.y);
m.put( 6,body.forward.z);
m.put( 8,body.up.x);
m.put( 9,body.up.y);
m.put(10,body.up.z);
m.put(15,1);
matBody.render(gl2);
gl2.glTranslated(body.pos.x + 7.5f * body.up.x,
body.pos.y + 7.5f * body.up.y,
body.pos.z + 7.5f * body.up.z );
gl2.glMultMatrixd(m);
gl2.glRotatef(180,0,1,0);
modelBody.render(gl2);
gl2.glPopMatrix();
}
/**
* Creates a new DoubleBuffer with the same contents as the given
* DoubleBuffer. The new DoubleBuffer is separate from the old one and
* changes are not reflected across. If you want to reflect changes,
* consider using Buffer.duplicate().
*
* @param buf
* the DoubleBuffer to copy
* @return the copy
*/
public static DoubleBuffer clone(DoubleBuffer buf) {
if (buf == null) {
return null;
}
buf.rewind();
DoubleBuffer copy;
if (isDirect(buf)) {
copy = createDoubleBuffer(buf.limit());
} else {
copy = DoubleBuffer.allocate(buf.limit());
}
copy.put(buf);
return copy;
}
/**
* Creates a new DoubleBuffer with the same contents as the given
* DoubleBuffer. The new DoubleBuffer is separate from the old one and
* changes are not reflected across. If you want to reflect changes,
* consider using Buffer.duplicate().
*
* @param buf
* the DoubleBuffer to copy
* @return the copy
*/
public static DoubleBuffer clone(DoubleBuffer buf) {
if (buf == null) {
return null;
}
buf.rewind();
DoubleBuffer copy;
if (isDirect(buf)) {
copy = createDoubleBuffer(buf.limit());
} else {
copy = DoubleBuffer.allocate(buf.limit());
}
copy.put(buf);
return copy;
}
/**
* Creates and initializes a 2D {@link NDArray}.
*
* @param data the float array that needs to be set
* @return a new instance of {@link NDArray}
*/
default NDArray create(double[][] data) {
DoubleBuffer buffer = DoubleBuffer.allocate(data.length * data[0].length);
for (double[] d : data) {
buffer.put(d);
}
buffer.rewind();
return create(buffer, new Shape(data.length, data[0].length));
}
@Test
public void baseTest() throws IOException {
UncompressedDoubleDataChunk chunk = new UncompressedDoubleDataChunk(1, new double[] {1d, 2d, 3d});
assertEquals(1, chunk.getOffset());
assertEquals(3, chunk.getLength());
assertArrayEquals(new double[]{1d, 2d, 3d}, chunk.getValues(), 0d);
assertEquals(24, chunk.getEstimatedSize());
assertFalse(chunk.isCompressed());
assertEquals(1d, chunk.getCompressionFactor(), 0d);
DoubleBuffer buffer = DoubleBuffer.allocate(4);
for (int i = 0; i < 4; i++) {
buffer.put(i, Double.NaN);
}
chunk.fillBuffer(buffer, 0);
assertArrayEquals(new double[] {Double.NaN, 1d, 2d, 3d}, buffer.array(), 0d);
String jsonRef = String.join(System.lineSeparator(),
"{",
" \"offset\" : 1,",
" \"values\" : [ 1.0, 2.0, 3.0 ]",
"}");
assertEquals(jsonRef, JsonUtil.toJson(chunk::writeJson));
RegularTimeSeriesIndex index = RegularTimeSeriesIndex.create(Interval.parse("2015-01-01T00:00:00Z/2015-01-01T00:45:00Z"),
Duration.ofMinutes(15));
assertEquals(ImmutableList.of(new DoublePoint(1, Instant.parse("2015-01-01T00:15:00Z").toEpochMilli(), 1d),
new DoublePoint(2, Instant.parse("2015-01-01T00:30:00Z").toEpochMilli(), 2d),
new DoublePoint(3, Instant.parse("2015-01-01T00:45:00Z").toEpochMilli(), 3d)),
chunk.stream(index).collect(Collectors.toList()));
}
static
public double[] toDoubleArray(Tensor tensor){
DoubleBuffer doubleBuffer = DoubleBuffer.allocate(tensor.numElements());
tensor.writeTo(doubleBuffer);
return doubleBuffer.array();
}
private org.tensorflow.Tensor<?> tensorFlowDoubleInputArgument(int d0Size, int d1Size) {
DoubleBuffer fb1 = DoubleBuffer.allocate(d0Size * d1Size);
int i = 0;
for (int d0 = 0; d0 < d0Size; d0++)
for (int d1 = 0; d1 < d1Size; ++d1)
fb1.put(i++, (float)(d1 * 1.0 / d1Size));
return org.tensorflow.Tensor.create(new long[]{ d0Size, d1Size }, fb1);
}
@Override
protected DoubleDataBuffer allocate(long size) {
return new DoubleNioDataBuffer(DoubleBuffer.allocate((int)size));
}
/**
* Create a new {@link JTensor} instance by extracting data from the underlying {@link Tensor} and
* closing it afterwards.
*/
public static JTensor create(final Tensor<?> tensor) {
final JTensor jt;
try {
switch (tensor.dataType()) {
case STRING:
if (tensor.numDimensions() == 0) {
final String value = new String(tensor.bytesValue(), UTF_8);
jt =
new AutoValue_JTensor(
tensor.dataType(), tensor.numDimensions(), tensor.shape(), value);
} else {
final int[] dimensions = toIntExact(tensor.shape());
final Object byteArray =
tensor.copyTo(Array.newInstance(byte[].class, toIntExact(tensor.shape())));
jt =
new AutoValue_JTensor(
tensor.dataType(),
tensor.numDimensions(),
tensor.shape(),
toStringArray(byteArray, tensor.numElements(), dimensions));
}
break;
case INT32:
final IntBuffer intBuf = IntBuffer.allocate(tensor.numElements());
tensor.writeTo(intBuf);
jt =
new AutoValue_JTensor(
tensor.dataType(), tensor.numDimensions(), tensor.shape(), intBuf.array());
break;
case INT64:
final LongBuffer longBuf = LongBuffer.allocate(tensor.numElements());
tensor.writeTo(longBuf);
jt =
new AutoValue_JTensor(
tensor.dataType(), tensor.numDimensions(), tensor.shape(), longBuf.array());
break;
case FLOAT:
final FloatBuffer floatBuf = FloatBuffer.allocate(tensor.numElements());
tensor.writeTo(floatBuf);
jt =
new AutoValue_JTensor(
tensor.dataType(), tensor.numDimensions(), tensor.shape(), floatBuf.array());
break;
case DOUBLE:
final DoubleBuffer doubleBuf = DoubleBuffer.allocate(tensor.numElements());
tensor.writeTo(doubleBuf);
jt =
new AutoValue_JTensor(
tensor.dataType(), tensor.numDimensions(), tensor.shape(), doubleBuf.array());
break;
case BOOL:
final boolean[] array = new boolean[tensor.numElements()];
tensor.copyTo(array);
jt =
new AutoValue_JTensor(
tensor.dataType(), tensor.numDimensions(), tensor.shape(), array);
break;
default:
throw new IllegalStateException("Unsupported data type " + tensor.dataType());
}
} finally {
tensor.close();
}
return jt;
}
@Override
public double[] toArray() {
DoubleBuffer buffer = DoubleBuffer.allocate(metadata.getIndex().getPointCount());
fillBuffer(buffer, 0);
return buffer.array();
}
@Test
public void compressTest() throws IOException {
UncompressedDoubleDataChunk chunk = new UncompressedDoubleDataChunk(1, new double[] {1d, 2d, 2d, 2d, 2d, 3d});
DoubleDataChunk maybeCompressedChunk = chunk.tryToCompress();
assertTrue(maybeCompressedChunk instanceof CompressedDoubleDataChunk);
CompressedDoubleDataChunk compressedChunk = (CompressedDoubleDataChunk) maybeCompressedChunk;
assertEquals(1, compressedChunk.getOffset());
assertEquals(6, compressedChunk.getLength());
assertTrue(compressedChunk.isCompressed());
assertEquals(36, compressedChunk.getEstimatedSize());
assertEquals(36d / 48, compressedChunk.getCompressionFactor(), 0d);
assertArrayEquals(new double[] {1d, 2d, 3d}, compressedChunk.getStepValues(), 0d);
assertArrayEquals(new int[] {1, 4, 1}, compressedChunk.getStepLengths());
DoubleBuffer buffer = DoubleBuffer.allocate(7);
for (int i = 0; i < 7; i++) {
buffer.put(i, Double.NaN);
}
compressedChunk.fillBuffer(buffer, 0);
assertArrayEquals(new double[] {Double.NaN, 1d, 2d, 2d, 2d, 2d, 3d}, buffer.array(), 0d);
// json test
String jsonRef = String.join(System.lineSeparator(),
"{",
" \"offset\" : 1,",
" \"uncompressedLength\" : 6,",
" \"stepValues\" : [ 1.0, 2.0, 3.0 ],",
" \"stepLengths\" : [ 1, 4, 1 ]",
"}");
assertEquals(jsonRef, JsonUtil.toJson(compressedChunk::writeJson));
// test json with object mapper
ObjectMapper objectMapper = JsonUtil.createObjectMapper()
.registerModule(new TimeSeriesJsonModule());
List<DoubleDataChunk> chunks = objectMapper.readValue(objectMapper.writeValueAsString(Arrays.asList(chunk, compressedChunk)),
TypeFactory.defaultInstance().constructCollectionType(List.class, DoubleDataChunk.class));
assertEquals(2, chunks.size());
assertEquals(chunk, chunks.get(0));
assertEquals(compressedChunk, chunks.get(1));
// check base class (DataChunk) deserializer
assertTrue(objectMapper.readValue(objectMapper.writeValueAsString(chunk), DataChunk.class) instanceof DoubleDataChunk);
// stream test
RegularTimeSeriesIndex index = RegularTimeSeriesIndex.create(Interval.parse("2015-01-01T00:00:00Z/2015-01-01T01:30:00Z"),
Duration.ofMinutes(15));
assertEquals(ImmutableList.of(new DoublePoint(1, Instant.parse("2015-01-01T00:15:00Z").toEpochMilli(), 1d),
new DoublePoint(2, Instant.parse("2015-01-01T00:30:00Z").toEpochMilli(), 2d),
new DoublePoint(6, Instant.parse("2015-01-01T01:30:00Z").toEpochMilli(), 3d)),
compressedChunk.stream(index).collect(Collectors.toList()));
}
@Override
public DoubleBuffer makeDouble(int size) {
return DoubleBuffer.allocate(size);
}
@Override
public DoubleBuffer allocateBuffer(long[] shape) {
return DoubleBuffer.allocate(calculateCapacityForShape(shape));
}
/**
* Constructor
* @param size initial size
*/
public BufferArray(final int size) {
buf = DoubleBuffer.allocate(size);
}
/**
* Constructor
* @param size initial size
*/
public BufferArray(final int size) {
buf = DoubleBuffer.allocate(size);
}
/**
* Constructor
* @param size initial size
*/
public BufferArray(final int size) {
buf = DoubleBuffer.allocate(size);
}
/**
* Constructor
* @param size initial size
*/
public BufferArray(final int size) {
buf = DoubleBuffer.allocate(size);
}
/**
* Constructor
* @param size initial size
*/
public BufferArray(final int size) {
buf = DoubleBuffer.allocate(size);
}
/**
* Constructor
* @param size initial size
*/
public BufferArray(final int size) {
buf = DoubleBuffer.allocate(size);
}