/** * Creates a double indexer to access efficiently the data of a pointer. * * @param pointer data to access via a buffer or to copy to an array * @param direct {@code true} to use a direct buffer, see {@link Indexer} for details * @return the new double indexer backed by the raw memory interface, a buffer, or an array */ public static DoubleIndexer create(final DoublePointer pointer, long[] sizes, long[] strides, boolean direct) { if (direct) { return Raw.getInstance() != null ? new DoubleRawIndexer(pointer, sizes, strides) : new DoubleBufferIndexer(pointer.asBuffer(), sizes, strides); } else { final long position = pointer.position(); double[] array = new double[(int)Math.min(pointer.limit() - position, Integer.MAX_VALUE)]; pointer.get(array); return new DoubleArrayIndexer(array, sizes, strides) { @Override public void release() { pointer.position(position).put(array); super.release(); } }; } }
/** @return {@link DPIX#data()} wrapped in a {@link DoubleBuffer} starting at given byte index. */ public DoubleBuffer createBuffer(int index) { int[] w = {0}, h = {0}; dpixGetDimensions((DPIX)this, w, h); int wpl = dpixGetWpl((DPIX)this); DoublePointer data = new DoublePointer(dpixGetData((DPIX)this)).position(index).capacity(h[0] * wpl); return data.asBuffer(); }
/** Returns {@link #tensor_data()} wrapped in a {@link Buffer} of appropriate type starting at given index. */ public <B extends Buffer> B createBuffer(long index) { BytePointer ptr = tensor_data(); long size = TotalBytes(); switch (dtype()) { case DT_COMPLEX64: case DT_FLOAT: return (B)new FloatPointer(ptr).position(index).capacity(size/4).asBuffer(); case DT_DOUBLE: return (B)new DoublePointer(ptr).position(index).capacity(size/8).asBuffer(); case DT_QINT32: case DT_INT32: return (B)new IntPointer(ptr).position(index).capacity(size/4).asBuffer(); case DT_BOOL: case DT_QUINT8: case DT_UINT8: case DT_QINT8: case DT_INT8: return (B)ptr.position(index).capacity(size).asBuffer(); case DT_BFLOAT16: case DT_INT16: return (B)new ShortPointer(ptr).position(index).capacity(size/2).asBuffer(); case DT_INT64: return (B)new LongPointer(ptr).position(index).capacity(size/8).asBuffer(); case DT_STRING: default: assert false; } return null; }