List of usage examples for java.nio LongBuffer get
public abstract long get();
From source file:Main.java
public static void main(String[] args) { LongBuffer bb = LongBuffer.allocate(10); bb.put(100);/*from w ww . j a v a2 s. co m*/ bb.rewind(); System.out.println(bb.get()); }
From source file:Main.java
public static void main(String[] args) { ByteBuffer bb = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0, 'a' }); bb.rewind();/*from www . jav a2 s .c o m*/ LongBuffer lb = ((ByteBuffer) bb.rewind()).asLongBuffer(); System.out.println("Long Buffer"); while (lb.hasRemaining()) System.out.println(lb.position() + " -> " + lb.get()); }
From source file:Main.java
public static void main(String[] args) { ByteBuffer bb = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0, 'a' }); bb.rewind();/*from www . jav a 2s .co m*/ System.out.println("Byte Buffer"); while (bb.hasRemaining()) System.out.println(bb.position() + " -> " + bb.get()); CharBuffer cb = ((ByteBuffer) bb.rewind()).asCharBuffer(); System.out.println("Char Buffer"); while (cb.hasRemaining()) System.out.println(cb.position() + " -> " + cb.get()); FloatBuffer fb = ((ByteBuffer) bb.rewind()).asFloatBuffer(); System.out.println("Float Buffer"); while (fb.hasRemaining()) System.out.println(fb.position() + " -> " + fb.get()); IntBuffer ib = ((ByteBuffer) bb.rewind()).asIntBuffer(); System.out.println("Int Buffer"); while (ib.hasRemaining()) System.out.println(ib.position() + " -> " + ib.get()); LongBuffer lb = ((ByteBuffer) bb.rewind()).asLongBuffer(); System.out.println("Long Buffer"); while (lb.hasRemaining()) System.out.println(lb.position() + " -> " + lb.get()); ShortBuffer sb = ((ByteBuffer) bb.rewind()).asShortBuffer(); System.out.println("Short Buffer"); while (sb.hasRemaining()) System.out.println(sb.position() + " -> " + sb.get()); DoubleBuffer db = ((ByteBuffer) bb.rewind()).asDoubleBuffer(); System.out.println("Double Buffer"); while (db.hasRemaining()) System.out.println(db.position() + " -> " + db.get()); }
From source file:info.gehrels.flockDBClient.ByteHelper.java
static long[] toLongArray(byte[] ids) { LongBuffer buffy = ByteBuffer.wrap(ids).order(ByteOrder.LITTLE_ENDIAN).asLongBuffer(); long[] result = new long[buffy.capacity()]; int i = 0;//from w w w .j a v a 2 s .c om while (buffy.hasRemaining()) { result[i++] = buffy.get(); } return result; }
From source file:net.pms.util.OpenSubtitle.java
private static long computeHashForChunk(ByteBuffer buffer) { LongBuffer longBuffer = buffer.order(ByteOrder.LITTLE_ENDIAN).asLongBuffer(); long hash = 0; while (longBuffer.hasRemaining()) { hash += longBuffer.get(); }// w ww .j a va2 s. c o m return hash; }
From source file:edu.cmu.graphchi.shards.QueryShard.java
private long findVertexForOff(long qoff, final LongBuffer tmpBuffer) { // Binary search to find the start of the vertex if (tmpBuffer != null) { // non-pinned int n = tmpBuffer.capacity(); int high = n - 1; int low = tmpBuffer.position(); // Check if we are close long cur = tmpBuffer.get(); long curoff = VertexIdTranslate.getAux(cur); // TODO/*from w w w . j av a 2 s . co m*/ if (qoff > curoff && qoff - curoff < 100) { long last = cur; final Timer.Context tmr = findEdgeByOffTimerScan.time(); while (curoff <= qoff) { last = cur; cur = tmpBuffer.get(); curoff = VertexIdTranslate.getAux(cur); } if (tmpBuffer.position() > 0) { tmpBuffer.position(tmpBuffer.position() - 1); // Backtrack one } tmr.stop(); return VertexIdTranslate.getVertexId(last); } if (curoff > qoff) { low = 0; } if (curoff == qoff) { return VertexIdTranslate.getVertexId(cur); } while (low <= high) { int idx = ((high + low) / 2); if (idx == n - 1) idx--; tmpBuffer.position(idx); long x = tmpBuffer.get(); long x_next = tmpBuffer.get(); long off = VertexIdTranslate.getAux(x); long off_next = VertexIdTranslate.getAux(x_next); if (off_next > qoff && off <= qoff) { tmpBuffer.position(idx); return VertexIdTranslate.getVertexId(x); } if (off < qoff) { low = idx + 1; } else { high = idx - 1; } } throw new RuntimeException("Could not find " + qoff); } else { // pinned int idx = gammaSeqOffs.getIndexOfLowerBound(qoff); if (idx == -1) { for (int i = 0; i < gammaSeqOffs.length(); i++) { long x = gammaSeqOffs.get(i); if (x > qoff) break; } throw new RuntimeException("(Gamma-version) Could not find " + qoff); } return gammaSeqVertices.get(idx); } }
From source file:edu.cmu.graphchi.shards.QueryShard.java
private PointerPair findIdxAndPos(long vertexId, ShardIndex.IndexEntry sparseIndexEntry, final LongBuffer tmpPtrBuffer, long[] workarr, boolean nextGreatest) { if (tmpPtrBuffer != null) { assert (sparseIndexEntry.vertex <= vertexId); // Not pinned if (tmpPtrBuffer.capacity() == 0) return new PointerPair(-1, -1); int vertexSeq = sparseIndexEntry.vertexSeq; long curvid = sparseIndexEntry.vertex; if (sparseIndexEntry.nextEntry == null || nextGreatest) { // Linear search tmpPtrBuffer.position(vertexSeq); long ptr = tmpPtrBuffer.get(); while (curvid <= vertexId) { try { curvid = VertexIdTranslate.getVertexId(ptr); if (curvid == vertexId || (curvid >= vertexId && nextGreatest)) { return new PointerPair(VertexIdTranslate.getAux(ptr), VertexIdTranslate.getAux(tmpPtrBuffer.get())); }//ww w . ja v a 2 s . c om ptr = tmpPtrBuffer.get(); } catch (BufferUnderflowException bufe) { return new PointerPair(-1, -1); } } } else { // Binary search int low = sparseIndexEntry.vertexSeq; int high = sparseIndexEntry.nextEntry.vertexSeq; int n = tmpPtrBuffer.capacity(); while (low <= high) { int idx = ((high + low) / 2); long ptr = tmpPtrBuffer.get(idx); curvid = VertexIdTranslate.getVertexId(ptr); if (curvid == vertexId) { return new PointerPair(VertexIdTranslate.getAux(ptr), VertexIdTranslate.getAux(tmpPtrBuffer.get(idx + 1))); } if (curvid < vertexId) { low = idx + 1; } else { high = idx - 1; } } } return new PointerPair(-1, -1); } else { if (gammaSeqVertices == null) { // Empty shard return new PointerPair(-1, -1); } // Pinned int idx = gammaSeqVertices.getIndex(vertexId); if (idx == -1) return new PointerPair(-1, -1); gammaSeqOffs.getTwo(idx, workarr); return new PointerPair(workarr[0], workarr[1]); } }
From source file:edu.cmu.graphchi.shards.QueryShard.java
public Long find(byte edgeType, long src, long dst) { final LongBuffer tmpAdjBuffer = adjBuffer.duplicate(); final LongBuffer tmpPointerBuffer = (pinIndexToMemory ? null : pointerIdxBuffer.duplicate()); ShardIndex.IndexEntry indexEntry = (pinIndexToMemory ? null : index.lookup(src)); PointerPair ptr = findIdxAndPos(src, indexEntry, tmpPointerBuffer, new long[2]); if (ptr.cur != (-1L)) { long nextPtr = ptr.next; int n = (int) (nextPtr - ptr.cur); long adjOffset = ptr.cur; if (n < 32) { // linear search tmpAdjBuffer.position((int) adjOffset); for (int i = 0; i < n; i++) { long e = tmpAdjBuffer.get(); long v = VertexIdTranslate.getVertexId(e); if (v == dst && VertexIdTranslate.getType(e) == edgeType) { return PointerUtil.encodePointer(shardNum, (int) adjOffset + i); } else if (v > dst) { break; }/*from w w w . j a v a 2 s .c o m*/ } } else { // Binary search int low = (int) adjOffset; int high = low + n; while (low <= high) { int idx = ((high + low) / 2); long e = tmpAdjBuffer.get(idx); long v = VertexIdTranslate.getVertexId(e); byte type = VertexIdTranslate.getType(e); if (v == dst && type == edgeType) { // Note: edgeType sorting is not accurate as edge type can be changed (by deleting)! return PointerUtil.encodePointer(shardNum, idx); } if (v < dst || (dst == v && type < edgeType)) { low = idx + 1; } else { high = idx - 1; } } } } return null; }
From source file:edu.cmu.graphchi.shards.QueryShard.java
public EdgeIterator edgeIterator(long fromSrcVertex) { if (!pinIndexToMemory) { final LongBuffer iterBuffer = adjBuffer.duplicate(); final LongBuffer iterPointerBuffer = pointerIdxBuffer.duplicate(); if (fromSrcVertex <= 0) { iterBuffer.position(0);//from ww w . j a va 2 s. co m iterPointerBuffer.position(0); } else { ShardIndex.IndexEntry entry = null; entry = (pinIndexToMemory ? null : index.lookup(fromSrcVertex)); // ugly PointerPair ptr = findIdxAndPos(fromSrcVertex, entry, iterPointerBuffer, new long[2], true); assert (ptr.cur >= 0); iterBuffer.position((int) ptr.cur); iterPointerBuffer.position(iterPointerBuffer.position() - 1); } return new EdgeIterator() { int idx = iterBuffer.position() - 1; long ptr = (iterPointerBuffer.capacity() > 0 ? iterPointerBuffer.get() : -1); long nextPtr = (iterPointerBuffer.capacity() > 0 ? iterPointerBuffer.get() : -1); long nextOff = VertexIdTranslate.getAux(nextPtr); long curSrc = VertexIdTranslate.getVertexId(ptr); long curDst; byte curType; long vertexPacket; @Override public boolean hasNext() { if (idx < numEdges - 1) { vertexPacket = iterBuffer.get(); if (VertexIdTranslate.isEdgeDeleted(vertexPacket)) { next(); // Skip over deleted edges return hasNext(); } else { return true; } } else { return false; } } @Override public void next() { idx++; if (idx == nextOff) { curSrc = VertexIdTranslate.getVertexId(nextPtr); nextPtr = iterPointerBuffer.get(); nextOff = VertexIdTranslate.getAux(nextPtr); } curDst = VertexIdTranslate.getVertexId(vertexPacket); curType = VertexIdTranslate.getType(vertexPacket); } @Override public long getSrc() { return curSrc; } @Override public long getDst() { return curDst; } @Override public byte getType() { return curType; } @Override public int getIdx() { return idx; } }; } else { if (gammaSeqVertices == null) { return new EdgeIterator() { @Override public boolean hasNext() { return false; } @Override public void next() { } @Override public long getSrc() { return 0; } @Override public long getDst() { return 0; } @Override public byte getType() { return 0; } @Override public int getIdx() { return 0; } }; } long startOff = 0; final LongBuffer iterBuffer = adjBuffer.duplicate(); final Iterator<Long> iterPointerVertices = gammaSeqVertices.iterator(fromSrcVertex); if (fromSrcVertex == 0) { iterBuffer.position(0); } else { int startIdx = gammaSeqVertices.getIndex(fromSrcVertex, true); startOff = gammaSeqOffs.get(startIdx); iterBuffer.position((int) startOff); } final Iterator<Long> iterPointerOffs = gammaSeqOffs.iterator(startOff); return new EdgeIterator() { int idx = iterBuffer.position() - 1; long ptr = (iterPointerVertices.hasNext() ? VertexIdTranslate.encodeVertexPacket((byte) 0, iterPointerVertices.next(), iterPointerOffs.next()) : -1); long nextPtr = (iterPointerVertices.hasNext() ? VertexIdTranslate.encodeVertexPacket((byte) 0, iterPointerVertices.next(), iterPointerOffs.next()) : -1); long nextOff = VertexIdTranslate.getAux(nextPtr); long curSrc = VertexIdTranslate.getVertexId(ptr); long curDst; byte curType; long vertexPacket; @Override public boolean hasNext() { if (idx < numEdges - 1) { vertexPacket = iterBuffer.get(); if (VertexIdTranslate.isEdgeDeleted(vertexPacket)) { next(); // Skip over deleted edges return hasNext(); } else { return true; } } else { return false; } } @Override public void next() { idx++; if (idx == nextOff) { curSrc = VertexIdTranslate.getVertexId(nextPtr); if (iterPointerVertices.hasNext()) { nextPtr = VertexIdTranslate.encodeVertexPacket((byte) 0, iterPointerVertices.next(), iterPointerOffs.next()); } else { // System.out.println("Warning: edgeIterator at " + idx + " but no more left..."); } nextOff = VertexIdTranslate.getAux(nextPtr); } curDst = VertexIdTranslate.getVertexId(vertexPacket); curType = VertexIdTranslate.getType(vertexPacket); } @Override public long getSrc() { return curSrc; } @Override public long getDst() { return curDst; } @Override public byte getType() { return curType; } @Override public int getIdx() { return idx; } }; } }
From source file:edu.cmu.graphchi.shards.QueryShard.java
public void queryOut(long vertexId, QueryCallback callback, byte edgeType, boolean ignoreType) { if (isEmpty()) return;//from w w w . j a va2 s .c o m try { final LongBuffer tmpPointerIdxBuffer = (pointerIdxBuffer != null ? pointerIdxBuffer.duplicate() : null); final LongBuffer tmpAdjBuffer = adjBuffer.duplicate(); boolean immediateReceive = callback.immediateReceive(); ShardIndex.IndexEntry entry = null; entry = (pinIndexToMemory ? null : index.lookup(vertexId)); // ugly PointerPair ptr = findIdxAndPos(vertexId, entry, tmpPointerIdxBuffer, new long[2]); long curPtr = ptr.cur; if (ptr.cur != (-1L)) { long nextPtr = ptr.next; int n = (int) (nextPtr - curPtr); ArrayList<Long> res = (immediateReceive ? null : new ArrayList<Long>(n)); ArrayList<Long> resPointers = (immediateReceive ? null : new ArrayList<Long>(n)); ArrayList<Byte> resTypes = (immediateReceive ? null : new ArrayList<Byte>(n)); tmpAdjBuffer.position((int) curPtr); for (int i = 0; i < n && tmpAdjBuffer.position() < tmpAdjBuffer.capacity(); i++) { // The latter condition is to fix some bug with the last vertex? long e = tmpAdjBuffer.get(); byte etype = VertexIdTranslate.getType(e); if (etype == edgeType || ignoreType) { if (!immediateReceive) { res.add(VertexIdTranslate.getVertexId(e)); resPointers.add(PointerUtil.encodePointer(shardNum, (int) curPtr + i)); resTypes.add(etype); } else { callback.receiveEdge(vertexId, VertexIdTranslate.getVertexId(e), etype, PointerUtil.encodePointer(shardNum, (int) curPtr + i)); } } } if (!immediateReceive) callback.receiveOutNeighbors(vertexId, res, resTypes, resPointers); } else { if (!immediateReceive) callback.receiveOutNeighbors(vertexId, new ArrayList<Long>(0), new ArrayList<Byte>(0), new ArrayList<Long>(0)); } } catch (FinishQueryException fqe) { // Used for cases when query was early fulfilled throw fqe; } catch (Exception err) { err.printStackTrace(); throw new RuntimeException(err); } }