public void testGet_io() throws IOException { assertEquals(-1, ArbitraryInstances.get(InputStream.class).read()); assertEquals(-1, ArbitraryInstances.get(ByteArrayInputStream.class).read()); assertEquals(-1, ArbitraryInstances.get(Readable.class).read(CharBuffer.allocate(1))); assertEquals(-1, ArbitraryInstances.get(Reader.class).read()); assertEquals(-1, ArbitraryInstances.get(StringReader.class).read()); assertEquals(0, ArbitraryInstances.get(Buffer.class).capacity()); assertEquals(0, ArbitraryInstances.get(CharBuffer.class).capacity()); assertEquals(0, ArbitraryInstances.get(ByteBuffer.class).capacity()); assertEquals(0, ArbitraryInstances.get(ShortBuffer.class).capacity()); assertEquals(0, ArbitraryInstances.get(IntBuffer.class).capacity()); assertEquals(0, ArbitraryInstances.get(LongBuffer.class).capacity()); assertEquals(0, ArbitraryInstances.get(FloatBuffer.class).capacity()); assertEquals(0, ArbitraryInstances.get(DoubleBuffer.class).capacity()); ArbitraryInstances.get(PrintStream.class).println("test"); ArbitraryInstances.get(PrintWriter.class).println("test"); assertNotNull(ArbitraryInstances.get(File.class)); assertFreshInstanceReturned( ByteArrayOutputStream.class, OutputStream.class, Writer.class, StringWriter.class, PrintStream.class, PrintWriter.class); assertEquals(ByteSource.empty(), ArbitraryInstances.get(ByteSource.class)); assertEquals(CharSource.empty(), ArbitraryInstances.get(CharSource.class)); assertNotNull(ArbitraryInstances.get(ByteSink.class)); assertNotNull(ArbitraryInstances.get(CharSink.class)); }
public static void main(String[] args) { Random r = new Random(); int entries = 1000; boolean[] src = new boolean[entries * 64]; long[] dest = new long[entries]; long[] result = new long[entries]; for (int c = 0; c < 2000; c++) { for (int i = 0; i < entries; i++) { long l = r.nextLong(); for (int bit = 0; bit < 64; bit++) { src[i * 64 + bit] = (l & (1L << bit)) != 0; } dest[i] = 0; result[i] = l; } test(src, 0, LongBuffer.wrap(dest, 0, dest.length), 0, src.length); for (int i = 0; i < entries; i++) { if (dest[i] != result[i]) { throw new InternalError(i + ": " + Long.toHexString(dest[i]) + " != " + Long.toHexString(result[i])); } } } }
@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); } }
private static boolean isDirect(Buffer buf) { if (buf instanceof FloatBuffer) { return ((FloatBuffer) buf).isDirect(); } if (buf instanceof IntBuffer) { return ((IntBuffer) buf).isDirect(); } if (buf instanceof ShortBuffer) { return ((ShortBuffer) buf).isDirect(); } if (buf instanceof ByteBuffer) { return ((ByteBuffer) buf).isDirect(); } if (buf instanceof DoubleBuffer) { return ((DoubleBuffer) buf).isDirect(); } if (buf instanceof LongBuffer) { return ((LongBuffer) buf).isDirect(); } throw new UnsupportedOperationException(" BufferAux.isDirect was called on " + buf.getClass().getName()); }
@Test public void testCompression() { int n = 1000000; FloatHistogram x = new FloatHistogram(1e-3, 10); Random rand = new Random(); for (int i = 0; i < n; i++) { x.add(rand.nextDouble()); } long[] compressed = x.getCompressedCounts(); System.out.printf("%d\n", compressed.length); long[] uncompressed = new long[x.getCounts().length]; long[] counts = x.getCounts(); int k = Simple64.decompress(LongBuffer.wrap(compressed), uncompressed); assertEquals(k, counts.length); for (int i = 0; i < uncompressed.length; i++) { assertEquals(counts[i], uncompressed[i]); } }
/** * ByteChannelからlong配列を読み込む * @param channel * @return * @throws IOException */ public static long[] readLongArray(@NonNull final ByteChannel channel) throws IOException { final int n = readInt(channel); final ByteBuffer buf = ByteBuffer.allocate(n * 8).order(ByteOrder.BIG_ENDIAN); final int readBytes = channel.read(buf); if (readBytes != n * 8) throw new IOException(); buf.clear(); final LongBuffer result = buf.asLongBuffer(); if (result.hasArray()) { return result.array(); } else { final long[] b = new long[n]; result.get(b); return b; } }
@Benchmark public void readAsLongBuffer() throws IOException { ByteBuffer byteBuffer = ByteBuffer.allocateDirect(64 * 1024); LongBuffer longBuffer = byteBuffer.asLongBuffer(); long readValueCount = 0; try (ReadableByteChannel byteChannel = _testFile.open();) { while (byteChannel.read(byteBuffer) > 0) { byteBuffer.flip(); while (longBuffer.hasRemaining()) { long longValue = longBuffer.get(); assertThat(longValue).isEqualTo(readValueCount); readValueCount++; } longBuffer.rewind(); } } assertThat(readValueCount).isEqualTo(BinaryByteUnit.GIBIBYTES.toBytes(1) / 8); }
private static long[] createFramebuffers(VkDevice device, Swapchain swapchain, long renderPass, int width, int height) { LongBuffer attachments = stackMallocLong(1); VkFramebufferCreateInfo fci = VkFramebufferCreateInfo.callocStack() .sType(VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO) .pAttachments(attachments) .flags(VK_FLAGS_NONE) .height(height) .width(width) .layers(1) .pNext(NULL) .renderPass(renderPass); // Create a framebuffer for each swapchain image long[] framebuffers = new long[swapchain.images.length]; LongBuffer pFramebuffer = stackMallocLong(1); for (int i = 0; i < swapchain.images.length; i++) { attachments.put(0, swapchain.imageViews[i]); int err = vkCreateFramebuffer(device, fci, null, pFramebuffer); long framebuffer = pFramebuffer.get(0); if (err != VK_SUCCESS) { throw new AssertionError("Failed to create framebuffer: " + translateVulkanResult(err)); } framebuffers[i] = framebuffer; } return framebuffers; }
/** * This function sets up the debug callback which the validation layers will use to yell at us when we make mistakes. */ private static long setupDebugging(VkInstance instance, int flags, VkDebugReportCallbackEXT callback) { // Again, a struct to create something, in this case the debug report callback VkDebugReportCallbackCreateInfoEXT dbgCreateInfo = VkDebugReportCallbackCreateInfoEXT.callocStack() .sType(VK_STRUCTURE_TYPE_DEBUG_REPORT_CALLBACK_CREATE_INFO_EXT) // <- the struct type .pNext(NULL) // <- must be NULL .pfnCallback(callback) // <- the actual function pointer (in LWJGL a Closure) .pUserData(NULL) // <- any user data provided to the debug report callback function .flags(flags); // <- indicates which kind of messages we want to receive LongBuffer pCallback = stackMallocLong(1); // <- allocate a LongBuffer (for a non-dispatchable handle) // Actually create the debug report callback int err = vkCreateDebugReportCallbackEXT(instance, dbgCreateInfo, null, pCallback); long callbackHandle = pCallback.get(0); if (err != VK_SUCCESS) { throw new AssertionError("Failed to create VkInstance: " + translateVulkanResult(err)); } return callbackHandle; }
@Override public DoubleImmutableArray.Builder append(double value) { final LongBuffer current; // If the specified outer index is not yet allocated, do that first. if (outer == buffers.size()) { buffers.add(current = ByteBuffer.allocateDirect( BUFFER_SIZE * Long.BYTES ).asLongBuffer()); } else { current = buffers.getLast(); } // Store the value at the specified index. current.put(inner, Double.doubleToLongBits(value)); // If the inner index is about to overflow, reset it and increment outer // index until next time. if (BUFFER_SIZE == ++inner) { inner = 0; outer++; } return this; }
private void forEachThenClear(LongConsumer action) { final long length = length(); final LongBuffer[] bufferArray = bufferArray(); for (long l = 0; l < length; l++) { final int o = outerIndex(l); final int i = innerIndex(l); action.accept(bufferArray[o].get(i)); // If we just consumed the last value in this buffer, clear it. if (i + 1 == BUFFER_SIZE) { MemoryUtil.clear(bufferArray[o]); } } }
private static long computeHashForChunk(ByteBuffer buffer) { LongBuffer longBuffer = buffer.order(ByteOrder.LITTLE_ENDIAN).asLongBuffer(); long hash = 0; while (longBuffer.hasRemaining()) { hash += longBuffer.get(); } return hash; }
public static byte[] longToByteArray(long inLong) { byte[] bArray = new byte[8]; ByteBuffer bBuffer = ByteBuffer.wrap(bArray); LongBuffer lBuffer = bBuffer.asLongBuffer(); lBuffer.put(inLong); return bArray; }
public static byte[] longToByteArrayLittleEndian(long inLong) { byte[] bArray = new byte[8]; ByteBuffer bBuffer = ByteBuffer.wrap(bArray); bBuffer.order(ByteOrder.LITTLE_ENDIAN); LongBuffer lBuffer = bBuffer.asLongBuffer(); lBuffer.put(inLong); return bArray; }
@AndroidIncompatible // problem with equality of Type objects? public void testFreshInstance() { assertFreshInstances( String.class, CharSequence.class, Appendable.class, StringBuffer.class, StringBuilder.class, Pattern.class, MatchResult.class, Number.class, int.class, Integer.class, long.class, Long.class, short.class, Short.class, byte.class, Byte.class, boolean.class, Boolean.class, char.class, Character.class, int[].class, Object[].class, UnsignedInteger.class, UnsignedLong.class, BigInteger.class, BigDecimal.class, Throwable.class, Error.class, Exception.class, RuntimeException.class, Charset.class, Locale.class, Currency.class, List.class, Map.Entry.class, Object.class, Equivalence.class, Predicate.class, Function.class, Comparable.class, Comparator.class, Ordering.class, Class.class, Type.class, TypeToken.class, TimeUnit.class, Ticker.class, Joiner.class, Splitter.class, CharMatcher.class, InputStream.class, ByteArrayInputStream.class, Reader.class, Readable.class, StringReader.class, OutputStream.class, ByteArrayOutputStream.class, Writer.class, StringWriter.class, File.class, Buffer.class, ByteBuffer.class, CharBuffer.class, ShortBuffer.class, IntBuffer.class, LongBuffer.class, FloatBuffer.class, DoubleBuffer.class, String[].class, Object[].class, int[].class); }
private static void writeLongBuffer(LongBuffer buf) { Buffer viewedBuffer = bufferViews.get(buf); if (viewedBuffer != null) { bufferEndiannessWritten.put(viewedBuffer, buf.order()); } else { /* We don't know how that typed view was created. Just assume it is correct. */ } }
public static LongBuffer slice(LongBuffer buf) { LongBuffer buffer = buf.slice(); Buffer viewedBuffer = bufferViews.get(buf); if (viewedBuffer != null) { bufferViews.put(buffer, viewedBuffer); } return buffer; }
public static LongBuffer asLongBuffer(ByteBuffer buf) { LongBuffer buffer = buf.asLongBuffer(); Buffer viewedBuffer = bufferViews.get(buf); if (viewedBuffer != null) { bufferViews.put(buffer, viewedBuffer); } else { bufferViews.put(buffer, buf); } return buffer; }
/** * Read the inflights file and return a * {@link com.google.common.collect.SetMultimap} * of transactionIDs to events that were inflight. * * @return - map of inflight events per txnID. */ public SetMultimap<Long, Long> deserialize() throws IOException, BadCheckpointException { SetMultimap<Long, Long> inflights = HashMultimap.create(); if (!fileChannel.isOpen()) { file = new RandomAccessFile(inflightEventsFile, "rw"); fileChannel = file.getChannel(); } if (file.length() == 0) { return inflights; } file.seek(0); byte[] checksum = new byte[16]; file.read(checksum); ByteBuffer buffer = ByteBuffer.allocate( (int) (file.length() - file.getFilePointer())); fileChannel.read(buffer); byte[] fileChecksum = digest.digest(buffer.array()); if (!Arrays.equals(checksum, fileChecksum)) { throw new BadCheckpointException("Checksum of inflights file differs" + " from the checksum expected."); } buffer.position(0); LongBuffer longBuffer = buffer.asLongBuffer(); try { while (true) { long txnID = longBuffer.get(); int numEvents = (int) (longBuffer.get()); for (int i = 0; i < numEvents; i++) { long val = longBuffer.get(); inflights.put(txnID, val); } } } catch (BufferUnderflowException ex) { LOG.debug("Reached end of inflights buffer. Long buffer position =" + String.valueOf(longBuffer.position())); } return inflights; }
PerfLongArrayCounter(String name, Units u, Variability v, int flags, int vectorLength, LongBuffer lb) { super(name, u, v, flags, vectorLength); this.lb = lb; }
@Test public void testGetLong() { ByteBuffer bb = ByteBuffer.allocate(40); LongBuffer lb = bb.asLongBuffer(); lb.put(0x1110223344556677L); lb.put(0x2220334455667788L); lb.put(0x3330445566778899L); lb.put(0x4440556677889900L); lb.put(0x55506677889900AAL); byte[] bytes = bb.array(); ByteSource bs = createByteSource(bytes); long l = bs.getLong(); assertEquals(0x1110223344556677L, l); assertEquals(8, bs.position()); l = bs.getLong(); assertEquals(0x2220334455667788L, l); assertEquals(16, bs.position()); bs.position(4 * 8); l = bs.getLong(); assertEquals(0x55506677889900AAL, l); assertEquals(40, bs.position()); try { bs.getLong(); fail("expected BufferUnderflowException"); } catch (BufferUnderflowException expected) { } }
@Test public void testGetLongInt() { ByteBuffer bb = ByteBuffer.allocate(40); LongBuffer lb = bb.asLongBuffer(); lb.put(0x1110223344556677L); lb.put(0x2220334455667788L); lb.put(0x3330445566778899L); lb.put(0x4440556677889900L); lb.put(0x55506677889900AAL); byte[] bytes = bb.array(); ByteSource bs = createByteSource(bytes); bs.position(3); long l = bs.getLong(0); assertEquals(0x1110223344556677L, l); assertEquals(3, bs.position()); l = bs.getLong(8); assertEquals(0x2220334455667788L, l); assertEquals(3, bs.position()); l = bs.getLong(4 * 8); assertEquals(0x55506677889900AAL, l); assertEquals(3, bs.position()); try { bs.getLong((4 * 8) + 1); fail("expected IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException expected) { } }
static public long[] toLongArray(Tensor tensor){ LongBuffer longBuffer = LongBuffer.allocate(tensor.numElements()); tensor.writeTo(longBuffer); return longBuffer.array(); }
void asViewGetOne(Buffer v, PrimitiveType t, int index) { switch (t) { case BYTE: ((ByteBuffer) v).get(index); break; case CHAR: ((CharBuffer) v).get(index); break; case SHORT: ((ShortBuffer) v).get(index); break; case INT: ((IntBuffer) v).get(index); break; case LONG: ((LongBuffer) v).get(index); break; case FLOAT: ((FloatBuffer) v).get(index); break; case DOUBLE: ((DoubleBuffer) v).get(index); break; } }
void asViewPutOne(Buffer v, PrimitiveType t, int index) { switch (t) { case BYTE: ((ByteBuffer) v).put(index, (byte)0); break; case CHAR: ((CharBuffer) v).put(index, '0'); break; case SHORT: ((ShortBuffer) v).put(index, (short)0); break; case INT: ((IntBuffer) v).put(index, 0); break; case LONG: ((LongBuffer) v).put(index, 0); break; case FLOAT: ((FloatBuffer) v).put(index, 0); break; case DOUBLE: ((DoubleBuffer) v).put(index, 0); break; } }
@DataProvider public static Object[][] longViewProvider() { List<Map.Entry<String, Function<ByteBuffer, LongBuffer>>> bfs = List.of( Map.entry("bb.asLongBuffer()", bb -> bb.asLongBuffer()), Map.entry("bb.asLongBuffer().slice()", bb -> bb.asLongBuffer().slice()), Map.entry("bb.asLongBuffer().slice().duplicate()", bb -> bb.asLongBuffer().slice().duplicate()) ); return product(BYTE_BUFFER_FUNCTIONS, bfs); }
private static GuardedInvocation linkGetElement(final Object self) { MethodHandle method = null; MethodHandle guard = null; if (self instanceof ByteBuffer) { method = BYTEBUFFER_GET; guard = IS_BYTEBUFFER; } else if (self instanceof CharBuffer) { method = CHARBUFFER_GET; guard = IS_CHARBUFFER; } else if (self instanceof ShortBuffer) { method = SHORTBUFFER_GET; guard = IS_SHORTBUFFER; } else if (self instanceof IntBuffer) { method = INTBUFFER_GET; guard = IS_INTBUFFER; } else if (self instanceof LongBuffer) { method = LONGBUFFER_GET; guard = IS_LONGBUFFER; } else if (self instanceof FloatBuffer) { method = FLOATBUFFER_GET; guard = IS_FLOATBUFFER; } else if (self instanceof DoubleBuffer) { method = DOUBLEBUFFER_GET; guard = IS_DOUBLEBUFFER; } return method != null? new GuardedInvocation(method, guard) : null; }
private static GuardedInvocation linkSetElement(final Object self) { MethodHandle method = null; MethodHandle guard = null; if (self instanceof ByteBuffer) { method = BYTEBUFFER_PUT; guard = IS_BYTEBUFFER; } else if (self instanceof CharBuffer) { method = CHARBUFFER_PUT; guard = IS_CHARBUFFER; } else if (self instanceof ShortBuffer) { method = SHORTBUFFER_PUT; guard = IS_SHORTBUFFER; } else if (self instanceof IntBuffer) { method = INTBUFFER_PUT; guard = IS_INTBUFFER; } else if (self instanceof LongBuffer) { method = LONGBUFFER_PUT; guard = IS_LONGBUFFER; } else if (self instanceof FloatBuffer) { method = FLOATBUFFER_PUT; guard = IS_FLOATBUFFER; } else if (self instanceof DoubleBuffer) { method = DOUBLEBUFFER_PUT; guard = IS_DOUBLEBUFFER; } return method != null? new GuardedInvocation(method, guard) : null; }
private static GuardedInvocation linkGetElement(Object self) { MethodHandle method = null; MethodHandle guard = null; if (self instanceof ByteBuffer) { method = BYTEBUFFER_GET; guard = IS_BYTEBUFFER; } else if (self instanceof CharBuffer) { method = CHARBUFFER_GET; guard = IS_CHARBUFFER; } else if (self instanceof ShortBuffer) { method = SHORTBUFFER_GET; guard = IS_SHORTBUFFER; } else if (self instanceof IntBuffer) { method = INTBUFFER_GET; guard = IS_INTBUFFER; } else if (self instanceof LongBuffer) { method = LONGBUFFER_GET; guard = IS_LONGBUFFER; } else if (self instanceof FloatBuffer) { method = FLOATBUFFER_GET; guard = IS_FLOATBUFFER; } else if (self instanceof DoubleBuffer) { method = DOUBLEBUFFER_GET; guard = IS_DOUBLEBUFFER; } return method != null? new GuardedInvocation(method, guard) : null; }
private static byte[] toByteArray(UUID uuid) { byte[] byteArray = new byte[(Long.SIZE / Byte.SIZE) * 2]; ByteBuffer buffer = ByteBuffer.wrap(byteArray); LongBuffer longBuffer = buffer.asLongBuffer(); longBuffer.put(new long[] { uuid.getMostSignificantBits(),uuid.getLeastSignificantBits() }); return byteArray; }
public static void compress(LongBuffer compressedBuffer, long[] unCompressedData, int offset, int size) { int encoded; while (size > 0) { encoded = compressSingle(unCompressedData, offset, size, compressedBuffer); offset += encoded; size -= encoded; } }
public static int decompress(LongBuffer compressedBuffer, long[] unCompressedData) { int totalOut = 0; compressedBuffer.rewind(); int unComprSize = unCompressedData.length; while (unComprSize > 0) { final int decoded = decompressSingle(compressedBuffer.get(), unCompressedData, totalOut); unComprSize -= decoded; totalOut += decoded; } return totalOut; }
@Override public long[] getCompressedCounts() { LongBuffer buf = LongBuffer.allocate(200); Simple64.compress(buf, counts, 0, counts.length); long[] r = new long[buf.position()]; buf.flip(); buf.get(r); return r; }
@Override 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; }