/** Constructor to set the {@link #pointer}, {@link #sizes} and {@link #strides}. */ public DoubleRawIndexer(DoublePointer pointer, long[] sizes, long[] strides) { super(sizes, strides); this.pointer = pointer; base = pointer.address() + pointer.position() * VALUE_BYTES; size = pointer.limit() - pointer.position(); }
/** Calls {@code DoubleRawIndexer(pointer, { pointer.limit() - pointer.position() }, { 1 })}. */ public DoubleRawIndexer(DoublePointer pointer) { this(pointer, new long[] { pointer.limit() - pointer.position() }, ONE_STRIDE); }
@Override public void release() { pointer.position(position).put(array); super.release(); } };
/** Returns {@code create(pointer, { pointer.limit() - pointer.position() }, { 1 }, true)} */ public static DoubleIndexer create(DoublePointer pointer) { return create(pointer, new long[] { pointer.limit() - pointer.position() }, ONE_STRIDE); }
/** * @param doublePointer * @param length * @return */ @Override public DataBuffer create(DoublePointer doublePointer, long length) { doublePointer.capacity(length); doublePointer.limit(length); doublePointer.position(0); return new DoubleBuffer(doublePointer, DoubleIndexer.create(doublePointer), length); }
/** * 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(); } }; } }
/** * For direct buffers, calls {@link Pointer#Pointer(Buffer)}, while for buffers * backed with an array, allocates enough memory for the array and copies it. * * @param buffer the Buffer to reference or copy * @see #put(double[]) */ public DoublePointer(DoubleBuffer buffer) { super(buffer); if (buffer != null && !buffer.isDirect() && buffer.hasArray()) { double[] array = buffer.array(); allocateArray(array.length - buffer.arrayOffset()); put(array, buffer.arrayOffset(), array.length - buffer.arrayOffset()); position(buffer.position()); limit(buffer.limit()); } } /**
/** * Returns the native DoublePointer of this array, pointing to the position 0. * @return pointer to this array. */ /*package visible*/ @DoublePrecisionVersion DoublePointer getPointer() { return pointer.position(0); }
/** * Returns the value at specified index. * <pre> * {@code return nativeArray[i]; } * </pre> * @param i index * @return value at i */ @DoublePrecisionVersion public double get(long i){ if(i >= this.length){ throw new ArrayIndexOutOfBoundsException( String.format("Cannot get value at %d, array is of length %d", i, this.length)); } return pointer.position(0).get(i); }
/** * Sets specified value at specified index. * <pre> * {@code nativeArray[i] = v; } * </pre> * @param i index * @param v value * @return this for chaining */ @DoublePrecisionVersion public NativeRealArray set(long i, double v){ if(i >= this.length){ throw new ArrayIndexOutOfBoundsException( String.format("Cannot set value at %d, array is of length %d", i, this.length)); } pointer.position(0).put(i, v); return this; }
length, i, this.length)); pointer.position(i).get(destination,offset,length); return destination;
/** * Reads <i>length</i> number of elements from the specified * array starting at <i>offset</i> and puts them into this * native array starting at <i>i</i>. * <pre> * {@code for(j=0;j<length;j++) nativeArray[i+j]=values[offset+j]; } * </pre> * @param i offset into this native array * @param length number of values to be copied * @param offset into the specified array * @param values the array from which values will be copied * @return this for chaining * * @throws IllegalArgumentException when specified offset plus length exceeds the specified values array's length */ @DoublePrecisionVersion public NativeRealArray set(long i, int length, int offset, double[] values){ if(offset+length > values.length){ throw new IllegalArgumentException( String.format("Cannot read %d values starting from %d, argument array is only of length %d", length, offset, values.length)); } if(i+length > this.length){ throw new ArrayIndexOutOfBoundsException( String.format("Number of values to be set (%d) to insert at index %d exceed length of this array (%d)", length, i, this.length)); } pointer.position(i).put(values, offset, length); return this; }
/** @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(); }
DoubleBuffer b = (DoubleBuffer)samples[i]; if (samples_in[i] instanceof DoublePointer && samples_in[i].capacity() >= inputSize && b.hasArray()) { ((DoublePointer)samples_in[i]).position(0).put(b.array(), b.position(), inputSize); } else { samples_in[i] = new DoublePointer(b);
DoubleBuffer b = (DoubleBuffer)samples[i]; if (samples_in[i] instanceof DoublePointer && samples_in[i].capacity() >= inputSize && b.hasArray()) { ((DoublePointer)samples_in[i]).position(0).put(b.array(), b.position(), inputSize); } else { samples_in[i] = new DoublePointer(b);
/** 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; }