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

The following examples show how to use java.nio.LongBuffer#limit() . 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: TensorBuffers.java    From java with Apache License 2.0 6 votes vote down vote up
/**
 * Maps tensor memory as a buffer of byte sequences, often used to store string values.
 *
 * @param nativeTensor native reference to the tensor
 * @return a string buffer
 */
public static StringTensorBuffer toStrings(TF_Tensor nativeTensor, long numElements) {
  Pointer tensorMemory = tensorMemory(nativeTensor);
  if (TensorRawDataBufferFactory.canBeUsed()) {
    return TensorRawDataBufferFactory.mapTensorToStrings(tensorMemory, numElements);
  }
  if (numElements > Integer.MAX_VALUE) {
    throw new IllegalArgumentException("Cannot map string tensor of " + numElements + " elements");
  }
  ByteBuffer dataBuffer = tensorMemory.asByteBuffer();

  LongBuffer offsetBuffer = dataBuffer.asLongBuffer();
  offsetBuffer.limit((int)numElements);
  LongDataBuffer offsets = DataBuffers.of(offsetBuffer.slice());

  dataBuffer.position((int)numElements * Long.BYTES);
  ByteDataBuffer data = DataBuffers.of(dataBuffer.slice());

  return new StringTensorBuffer(offsets, data);
}
 
Example 2
Source File: ByteBufferViews.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "longViewProvider")
public void testLongGet(String desc, IntFunction<ByteBuffer> fbb,
                        Function<ByteBuffer, LongBuffer> fbi) {
    ByteBuffer bb = allocate(fbb);
    LongBuffer vb = fbi.apply(bb);
    int o = bb.position();

    for (int i = 0; i < vb.limit(); i++) {
        long fromBytes = getLongFromBytes(bb, o + i * 8);
        long fromMethodView = bb.getLong(o + i * 8);
        assertValues(i, fromBytes, fromMethodView, bb);

        long fromBufferView = vb.get(i);
        assertValues(i, fromMethodView, fromBufferView, bb, vb);
    }

    for (int i = 0; i < vb.limit(); i++) {
        long v = getLongFromBytes(bb, o + i * 8);
        long a = bb.getLong();
        assertValues(i, v, a, bb);

        long b = vb.get();
        assertValues(i, a, b, bb, vb);
    }

}
 
Example 3
Source File: BMArrayOpsTest.java    From succinct with Apache License 2.0 6 votes vote down vote up
/**
 * Test method: long getVal(LongBuffer B, int i, int bits)
 *
 * @throws Exception
 */
public void testGetVal() throws Exception {

  BMArray bmArray = new BMArray(1000, 64);
  for (int i = 0; i < 1000; i++) {
    bmArray.setVal(i, i);
  }

  LongBuffer bBuf = bmArray.getLongBuffer();
  FSDataInputStream is = TestUtils.getStream(bBuf);
  LongArrayStream ls = new LongArrayStream(is, 0, bBuf.limit() * 8);
  for (int i = 0; i < 1000; i++) {
    assertEquals(BMArrayOps.getVal(ls, i, 64), i);
  }
  is.close();
}
 
Example 4
Source File: BitMapOpsTest.java    From succinct with Apache License 2.0 6 votes vote down vote up
/**
 * Test method: long getBit(LongBuffer bitmap, int i)
 *
 * @throws Exception
 */
public void testGetBit() throws Exception {

  BitMap instance = new BitMap(1000L);
  ArrayList<Long> test = new ArrayList<Long>();
  for (int i = 0; i < 1000; i++) {
    if ((int) (Math.random() * 2) == 1) {
      instance.setBit(i);
      test.add(1L);
    } else {
      test.add(0L);
    }
  }

  LongBuffer bBuf = instance.getLongBuffer();
  FSDataInputStream is = TestUtils.getStream(bBuf);
  RandomAccessLongStream ls = new RandomAccessLongStream(is, 0, bBuf.limit());
  for (int i = 0; i < 1000; i++) {
    long expResult = test.get(i);
    long result = BitMapOps.getBit(ls, i);
    assertEquals(expResult, result);
  }
  is.close();
}
 
Example 5
Source File: BitMapOpsTest.java    From succinct with Apache License 2.0 6 votes vote down vote up
/**
 * Test method: long getValPos(LongBuffer bitmap, int pos, int bits)
 *
 * @throws Exception
 */
public void testGetValPos() throws Exception {

  int pos = 60;
  int bits = 10;
  BitMap instance = new BitMap(1000L);
  instance.setValPos(pos, 1000, bits);

  LongBuffer bBuf = instance.getLongBuffer();
  FSDataInputStream is = TestUtils.getStream(bBuf);
  RandomAccessLongStream ls = new RandomAccessLongStream(is, 0, bBuf.limit());

  long expResult = 1000L;
  long result = BitMapOps.getValPos(ls, pos, bits);
  assertEquals(expResult, result);
  is.close();
}
 
Example 6
Source File: BufferUtil.java    From RoaringBitmap with Apache License 2.0 6 votes vote down vote up
protected static void fillArrayXOR(char[] container, LongBuffer bitmap1, LongBuffer bitmap2) {
  int pos = 0;
  if (bitmap1.limit() != bitmap2.limit()) {
    throw new IllegalArgumentException("not supported");
  }
  if (BufferUtil.isBackedBySimpleArray(bitmap1) && BufferUtil.isBackedBySimpleArray(bitmap2)) {
    org.roaringbitmap.Util.fillArrayXOR(container, bitmap1.array(),  bitmap2.array());
  } else {
    int len = bitmap1.limit();
    for (int k = 0; k < len; ++k) {
      long bitset = bitmap1.get(k) ^ bitmap2.get(k);
      while (bitset != 0) {
        container[pos++] = (char) (k * 64 + numberOfTrailingZeros(bitset));
        bitset &= (bitset - 1);
      }
    }
  }
}
 
Example 7
Source File: LongNioDataBuffer.java    From java with Apache License 2.0 5 votes vote down vote up
@Override
public LongDataBuffer slice(long index, long size) {
  Validator.sliceArgs(this, index, size);
  LongBuffer sliceBuf = buf.duplicate();
  sliceBuf.position((int)index);
  sliceBuf.limit((int)index + (int)size);
  return new LongNioDataBuffer(sliceBuf.slice());
}
 
Example 8
Source File: LongDZBP.java    From metrics with Apache License 2.0 5 votes vote down vote up
public void compress(LongBuffer src, LongOutputStream dst) {
    // Output length of original array.  When input array is empty, make
    // empty output for memory efficiency.
    final int srcLen = src.remaining();
    if (srcLen == 0) {
        return;
    }
    dst.write(srcLen);

    // Output first int, and set it as delta's initial context.
    final long first = src.get();
    dst.write(first);
    DZEncodeFilter filter = new DZEncodeFilter(first);

    // Compress intermediate blocks.
    final int chunkSize = this.bitPack.getBlockSize();
    final int chunkRemain = src.remaining() % chunkSize;
    LongBuffer window = src.slice();
    window.limit(window.limit() - chunkRemain);
    this.bitPack.compress(window, dst, filter);
    src.position(src.position() + window.position());

    // Compress last block.
    if (chunkRemain > 0) {
        long[] last = new long[chunkSize];
        src.get(last, 0, chunkRemain);
        // Padding extended area by last value.  It make delta zigzag
        // efficient.
        Arrays.fill(last, chunkRemain, last.length,
                last[chunkRemain - 1]);
        this.bitPack.compress(LongBuffer.wrap(last), dst, filter);
    }
}
 
Example 9
Source File: LongBitPacking.java    From metrics with Apache License 2.0 5 votes vote down vote up
public void compress(
        LongBuffer src,
        LongOutputStream dst,
        LongFilter filter)
{
    int srclen = src.limit() - src.position();
    while (src.remaining() >= this.blockLen * this.blockNum) {
        compressChunk(src, dst, filter);
    }
    return;
}
 
Example 10
Source File: CodecUtils.java    From metrics with Apache License 2.0 5 votes vote down vote up
public static void encodeBlockPack(
        LongBuffer src,
        LongFilterFactory filterFactory,
        LongBitPacking packer,
        LongOutputStream dst)
{
    // Output length of original array.  When input array is empty, make
    // empty output for memory efficiency.
    final int srcLen = src.remaining();
    if (srcLen == 0) {
        return;
    }
    dst.write(srcLen);

    // Output first int, and set it as delta's initial context.
    final long first = src.get();
    dst.write(first);
    LongFilter filter = filterFactory.newFilter(first);

    // Compress intermediate blocks.
    final int chunkSize = packer.getBlockSize();
    final int chunkRemain = src.remaining() % chunkSize;
    LongBuffer window = src.slice();
    window.limit(window.limit() - chunkRemain);
    packer.compress(window, dst, filter);
    src.position(src.position() + window.position());

    // Compress last block.
    if (chunkRemain > 0) {
        long[] last = new long[chunkSize];
        src.get(last, 0, chunkRemain);
        // Padding extended area by last value.  It make delta zigzag
        // efficient.
        Arrays.fill(last, chunkRemain, last.length,
                last[chunkRemain - 1]);
        packer.compress(LongBuffer.wrap(last), dst, filter);
    }
}
 
Example 11
Source File: BufferUtil.java    From RoaringBitmap with Apache License 2.0 5 votes vote down vote up
/**
 * Intersects the bitmap with the array, returning the cardinality of the result
 * @param bitmap the bitmap, modified
 * @param array the array, not modified
 * @param length how much of the array to consume
 * @return the size of the intersection, i.e. how many bits still set in the bitmap
 */
public static int intersectArrayIntoBitmap(LongBuffer bitmap, CharBuffer array, int length) {
  if (isBackedBySimpleArray(bitmap)) {
    return intersectArrayIntoBitmap(bitmap.array(), array, length);
  }
  int lastWordIndex = 0;
  int wordIndex = 0;
  long word = 0L;
  int cardinality = 0;
  for (int i = 0; i < length; ++i) {
    wordIndex = array.get(i) >>> 6;
    if (wordIndex != lastWordIndex) {
      long lastWord = bitmap.get(lastWordIndex);
      lastWord &= word;
      bitmap.put(lastWordIndex, lastWord);
      cardinality += Long.bitCount(lastWord);
      word = 0L;
      for (int j = lastWordIndex + 1; j < wordIndex; ++j) {
        bitmap.put(j, 0L);
      }
      lastWordIndex = wordIndex;
    }
    word |= 1L << array.get(i);
  }
  if (word != 0L) {
    long currentWord = bitmap.get(wordIndex);
    currentWord &= word;
    bitmap.put(wordIndex, currentWord);
    cardinality += Long.bitCount(currentWord);
  }
  if (wordIndex < bitmap.limit()) {
    for (int j = wordIndex + 1; j < bitmap.limit(); ++j) {
      bitmap.put(j, 0L);
    }
  }
  return cardinality;
}
 
Example 12
Source File: MappeableBitmapContainer.java    From RoaringBitmap with Apache License 2.0 5 votes vote down vote up
/**
 * Construct a new BitmapContainer backed by the provided LongBuffer.
 * 
 * @param array LongBuffer where the data is stored
 * @param initCardinality cardinality (number of values stored)
 */
public MappeableBitmapContainer(final LongBuffer array, final int initCardinality) {
  if (array.limit() != MAX_CAPACITY / 64) {
    throw new RuntimeException("Mismatch between buffer and storage requirements: "
        + array.limit() + " vs. " + MAX_CAPACITY / 64);
  }
  this.cardinality = initCardinality;
  this.bitmap = array;
}
 
Example 13
Source File: BitVectorOps.java    From succinct with Apache License 2.0 2 votes vote down vote up
/**
 * Check if the index being looked up is valid.
 *
 * @param data LongBuffer representation of BitVector's data.
 * @param i The index into the BitVector.
 * @return True if the index is valid, false otherwise.
 */
static boolean checkIndex(LongBuffer data, long i) {
  return (int) (i >>> 6) < data.limit();
}