Java Code Examples for java.nio.LongBuffer#allocate()

The following examples show how to use java.nio.LongBuffer#allocate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DeepLab.java    From cineast with MIT License 6 votes vote down vote up
public synchronized int[][] processImage(Tensor<UInt8> input) {

    Tensor<Long> result = session.runner().feed("ImageTensor", input)
        .fetch("SemanticPredictions").run().get(0).expect(Long.class);

    int len = result.numElements();
    LongBuffer buf = LongBuffer.allocate(len);
    result.writeTo(buf);
    result.close();

    long[] resultShape = result.shape();
    long[] resultArray = buf.array();

    int w = (int) resultShape[2];
    int h = (int) resultShape[1];

    int[][] resultMatrix = new int[w][h];

    for (int i = 0; i < resultArray.length; ++i) {
      resultMatrix[i % w][i / w] = (int) resultArray[i];
    }

    return resultMatrix;
  }
 
Example 2
Source File: TestMappeableBitmapContainer.java    From RoaringBitmap with Apache License 2.0 6 votes vote down vote up
@Test
public void roundtrip() throws Exception {
  LongBuffer buffer = LongBuffer.allocate(MAX_CAPACITY / 64);
  buffer.put(~0L);
  MappeableContainer bc = new MappeableBitmapContainer(buffer.asReadOnlyBuffer(), 64);
  final ByteArrayOutputStream bos = new ByteArrayOutputStream();
  try (ObjectOutputStream oo = new ObjectOutputStream(bos)) {
    bc.writeExternal(oo);
  }
  MappeableContainer bc2 = new MappeableBitmapContainer();
  final ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
  bc2.readExternal(new ObjectInputStream(bis));

  assertEquals(64, bc2.getCardinality());
  for (int i = 0; i < 64; i++) {
    assertTrue(bc2.contains((char) i));
  }
}
 
Example 3
Source File: FloatHistogram.java    From t-digest with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("WeakerAccess")
public long[] getCompressedCounts() {
    LongBuffer buf = LongBuffer.allocate(counts.length);
    Simple64.compress(buf, counts, 0, counts.length);
    long[] r = new long[buf.position()];
    buf.flip();
    buf.get(r);
    return r;
}
 
Example 4
Source File: MappeableBitmapContainer.java    From RoaringBitmap with Apache License 2.0 5 votes vote down vote up
MappeableBitmapContainer(int newCardinality, LongBuffer newBitmap) {
  this.cardinality = newCardinality;
  LongBuffer tmp = newBitmap.duplicate(); // for thread safety
  this.bitmap = LongBuffer.allocate(tmp.limit());
  tmp.rewind();
  this.bitmap.put(tmp);
}
 
Example 5
Source File: TestMappeableBitmapContainer.java    From RoaringBitmap with Apache License 2.0 5 votes vote down vote up
@Test
public void xorBitmap() {
  LongBuffer buffer = LongBuffer.allocate(MAX_CAPACITY / 64);
  buffer.put(~0L);
  MappeableContainer bc = new MappeableBitmapContainer(buffer.asReadOnlyBuffer(), 64);
  MappeableContainer bc2 = new MappeableBitmapContainer();
  bc2 = bc2.add(10, 64);
  bc = bc.xor(bc2);
  assertEquals(10, bc.getCardinality());
  for (char i = 0; i < 10; i++) {
    assertTrue(bc.contains(i));
  }
}
 
Example 6
Source File: TensorUtil.java    From jpmml-tensorflow with GNU Affero General Public License v3.0 5 votes vote down vote up
static
public long[] toLongArray(Tensor tensor){
	LongBuffer longBuffer = LongBuffer.allocate(tensor.numElements());

	tensor.writeTo(longBuffer);

	return longBuffer.array();
}
 
Example 7
Source File: TestMappeableBitmapContainer.java    From RoaringBitmap with Apache License 2.0 5 votes vote down vote up
@Test
public void select() {
  LongBuffer buffer = LongBuffer.allocate(MAX_CAPACITY / 64);
  buffer.put(~0L);
  buffer.put(~0L);
  MappeableContainer bc = new MappeableBitmapContainer(buffer.asReadOnlyBuffer(), 64);
  assertEquals(100, bc.select(100));
}
 
Example 8
Source File: RandomAccessLongStreamTest.java    From succinct with Apache License 2.0 5 votes vote down vote up
public void testPosition() throws Exception {
  LongBuffer buf = LongBuffer.allocate(10);
  for (int i = 0; i < 10; i++) {
    buf.put(i);
  }
  FSDataInputStream is = TestUtils.getStream(buf);
  RandomAccessLongStream bs = new RandomAccessLongStream(is, 0, 80);
  bs.position(3);
  assertEquals(bs.position(), 3);
}
 
Example 9
Source File: LongArrayStreamTest.java    From succinct with Apache License 2.0 5 votes vote down vote up
/**
 * Test method: long get(int i)
 *
 * @throws Exception
 */
public void testGet() throws Exception {
  LongBuffer buf = LongBuffer.allocate(10);
  for (int i = 0; i < 10; i++) {
    buf.put(i);
  }
  FSDataInputStream is = TestUtils.getStream(buf);
  LongArrayStream bs = new LongArrayStream(is, 0, 80);
  for (int i = 0; i < 10; i++) {
    assertEquals(i, bs.get(i));
  }
}
 
Example 10
Source File: TestMappeableBitmapContainer.java    From RoaringBitmap with Apache License 2.0 5 votes vote down vote up
@Test
public void numberOfRuns2() {
  LongBuffer buffer = LongBuffer.allocate(MAX_CAPACITY / 64);
  buffer.put(~8L);
  MappeableContainer bc = new MappeableBitmapContainer(buffer.asReadOnlyBuffer(), 64);
  assertEquals(2, bc.numberOfRuns());
}
 
Example 11
Source File: RandomAccessByteStreamTest.java    From succinct with Apache License 2.0 5 votes vote down vote up
public void testGetLong1() throws Exception {
  LongBuffer buf = LongBuffer.allocate(10);
  for (int i = 0; i < 10; i++) {
    buf.put(i);
  }
  FSDataInputStream is = TestUtils.getStream(buf);
  RandomAccessByteStream bs = new RandomAccessByteStream(is, 0, 80);
  for (int i = 0; i < 10; i++) {
    assertEquals(i, bs.getLong(i * 8));
  }
}
 
Example 12
Source File: TestMappeableBitmapContainer.java    From RoaringBitmap with Apache License 2.0 5 votes vote down vote up
@Test
public void xorBitmap2() {
  LongBuffer buffer = LongBuffer.allocate(MAX_CAPACITY / 64);
  for (int i = 0; i < 128; i++) {
    buffer.put(~0L);
  }
  MappeableContainer bc = new MappeableBitmapContainer(buffer.asReadOnlyBuffer(), 8192);
  MappeableContainer bc2 = new MappeableBitmapContainer();
  bc2 = bc2.add(5000, 8192);
  bc = bc.xor(bc2);
  assertEquals(5000, bc.getCardinality());
  for (char i = 0; i < 5000; i++) {
    assertTrue(bc.contains(i));
  }
}
 
Example 13
Source File: RandomAccessLongStreamTest.java    From succinct with Apache License 2.0 5 votes vote down vote up
public void testGet() throws Exception {
  LongBuffer buf = LongBuffer.allocate(10);
  for (int i = 0; i < 10; i++) {
    buf.put(i);
  }
  FSDataInputStream is = TestUtils.getStream(buf);
  RandomAccessLongStream bs = new RandomAccessLongStream(is, 0, 80);
  for (int i = 0; i < 10; i++) {
    assertEquals(i, bs.get());
  }
}
 
Example 14
Source File: TestMappeableBitmapContainer.java    From RoaringBitmap with Apache License 2.0 5 votes vote down vote up
@Test
public void andNotBitmap() {
  LongBuffer buffer = LongBuffer.allocate(MAX_CAPACITY / 64);
  buffer.put(~0L);
  MappeableContainer bc = new MappeableBitmapContainer(buffer.asReadOnlyBuffer(), 64);
  MappeableContainer bc2 = new MappeableBitmapContainer();
  bc2 = bc2.add(32, 64);
  bc = bc.andNot(bc2);
  assertEquals(32, bc.getCardinality());
  for (char i = 0; i < 32; i++) {
    assertTrue(bc.contains(i));
  }
}
 
Example 15
Source File: BufferParallelAggregation.java    From RoaringBitmap with Apache License 2.0 4 votes vote down vote up
@Override
public Supplier<MappeableContainer> supplier() {
  return () -> new MappeableBitmapContainer(LongBuffer.allocate(1 << 10), -1);
}
 
Example 16
Source File: LongTensorTypeSupport.java    From datacollector with Apache License 2.0 4 votes vote down vote up
@Override
public LongBuffer allocateBuffer(long[] shape) {
  return LongBuffer.allocate(calculateCapacityForShape(shape));
}
 
Example 17
Source File: BufferTools.java    From OSPREY3 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public LongBuffer makeLong(int size) {
	return LongBuffer.allocate(size);
}
 
Example 18
Source File: JTensor.java    From zoltar with Apache License 2.0 4 votes vote down vote up
/**
 * 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;
}
 
Example 19
Source File: LongNioDataBufferTest.java    From java with Apache License 2.0 4 votes vote down vote up
@Override
protected LongDataBuffer allocate(long size) {
  return new LongNioDataBuffer(LongBuffer.allocate((int)size));
}
 
Example 20
Source File: BitmapContainer.java    From RoaringBitmap with Apache License 2.0 2 votes vote down vote up
/**
 * Return the content of this container as a LongBuffer. This creates a copy and might be
 * relatively slow.
 *
 * @return the LongBuffer
 */
public LongBuffer toLongBuffer() {
  LongBuffer lb = LongBuffer.allocate(bitmap.length);
  lb.put(bitmap);
  return lb;
}