@Override public DoubleBuffer asNioDouble() { if (wrappedBuffer == null) { if (offset() == 0) { return DoubleBuffer.wrap(doubleData); } else return (DoubleBuffer) DoubleBuffer.wrap(doubleData).position(offset()); } if (offset() == 0) { return wrappedBuffer.asDoubleBuffer(); } else return (DoubleBuffer) wrappedBuffer.asDoubleBuffer().position(offset()); }
@Override public DoubleBuffer asNioDouble() { if (wrappedBuffer == null) { return DoubleBuffer.wrap(doubleData); } return wrappedBuffer.asDoubleBuffer(); }
@Override public double getDouble(int i) { if (doubleData != null) { if (i >= doubleData.length) throw new IllegalStateException("Index out of bounds " + i); dirty.set(false); return doubleData[i]; } else if (floatData != null) { if (i >= floatData.length) throw new IllegalStateException("Index out of bounds " + i); dirty.set(false); return (double) floatData[i]; } else if (intData != null) { dirty.set(false); return (double) intData[i]; } if (dataType() == Type.FLOAT) { dirty.set(false); return wrappedBuffer.asFloatBuffer().get(i); } else if (dataType() == Type.INT) { dirty.set(false); return wrappedBuffer.asIntBuffer().get(i); } else { dirty.set(false); return wrappedBuffer.asDoubleBuffer().get(i); } }
/** * Create a data buffer from the given length * * @param buffer * @param length */ public BaseDataBuffer(ByteBuffer buffer, int length) { allocationMode = Nd4j.alloc; this.length = length; buffer.order(ByteOrder.nativeOrder()); if (allocationMode() == AllocationMode.DIRECT) { this.wrappedBuffer = buffer; } else if (dataType() == Type.INT) { intData = new int[length]; IntBuffer intBuffer = buffer.asIntBuffer(); for (int i = 0; i < length; i++) { intData[i] = intBuffer.get(i); } } else if (dataType() == Type.DOUBLE) { doubleData = new double[length]; DoubleBuffer doubleBuffer = buffer.asDoubleBuffer(); for (int i = 0; i < length; i++) { doubleData[i] = doubleBuffer.get(i); } } else if (dataType() == Type.FLOAT) { floatData = new float[length]; FloatBuffer floatBuffer = buffer.asFloatBuffer(); for (int i = 0; i < length; i++) { floatData[i] = floatBuffer.get(i); } } }
@Override public void put(int i, double element) { if (doubleData != null) doubleData[offset() + i] = element; else if (floatData != null) floatData[offset() + i] = (float) element; else if (intData != null) intData[offset() + i] = (int) element; else { if (dataType() == Type.DOUBLE) { wrappedBuffer.asDoubleBuffer().put(offset() + i, element); } else if (dataType() == Type.INT) { wrappedBuffer.asIntBuffer().put(offset() + i, (int) element); } else { wrappedBuffer.asFloatBuffer().put(offset() + i, (float) element); } } dirty.set(true); }
/** * @param data * @param copy */ public BaseDataBuffer(double[] data, boolean copy) { allocationMode = Nd4j.alloc; if (allocationMode == AllocationMode.HEAP) { if (copy) { doubleData = ArrayUtil.copy(data); } else { this.doubleData = data; } } else { wrappedBuffer = ByteBuffer.allocateDirect(8 * data.length); wrappedBuffer.order(ByteOrder.nativeOrder()); DoubleBuffer buffer = wrappedBuffer.asDoubleBuffer(); for (int i = 0; i < data.length; i++) { buffer.put(i, data[i]); } } length = data.length; }
@Override public void put(int i, double element) { if (i < 0 || i >= length()) throw new IllegalArgumentException("Illegal index " + i); if (doubleData != null) doubleData[i] = element; else if (floatData != null) floatData[i] = (float) element; else if (intData != null) intData[i] = (int) element; else { if (dataType() == Type.DOUBLE) { wrappedBuffer.asDoubleBuffer().put(i, element); } else if (dataType() == Type.INT) { wrappedBuffer.asIntBuffer().put(i, (int) element); } else { wrappedBuffer.asFloatBuffer().put(i, (float) element); } } dirty.set(true); }
@Override public float getFloat(int i) { if (doubleData != null) { if (i >= doubleData.length) throw new IllegalStateException("Index out of bounds " + i); dirty.set(false); return (float) doubleData[i]; } else if (floatData != null) { if (i >= floatData.length) throw new IllegalStateException("Index out of bounds " + i); dirty.set(false); return floatData[i]; } else if (intData != null) { dirty.set(false); return (float) intData[i]; } if (dataType() == Type.DOUBLE) { dirty.set(false); return (float) wrappedBuffer.asDoubleBuffer().get(i); } dirty.getAndSet(true); return wrappedBuffer.asFloatBuffer().get(i); }