@Override public double[] asDouble() { if (length >= Integer.MAX_VALUE) throw new IllegalArgumentException("Unable to create array of length " + length); double[] ret = new double[(int) length]; for (int i = 0; i < length; i++) ret[i] = getDouble(i); return ret; }
@Override public double[] getDoublesAt(long offset, long inc, int length) { if (offset + length > length()) length -= offset; double[] ret = new double[length]; for (int i = 0; i < length; i++) { ret[i] = getDouble(i + offset); } return ret; }
@Override public byte[] asBytes() { ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bos); if (dataType() == Type.DOUBLE) { for (int i = 0; i < length(); i++) { try { dos.writeDouble(getDouble(i)); } catch (IOException e) { e.printStackTrace(); } } } else { for (int i = 0; i < length(); i++) { try { dos.writeFloat(getFloat(i)); } catch (IOException e) { e.printStackTrace(); } } } return bos.toByteArray(); }
@Override public DataBuffer dup() { DataBuffer ret = create(length); for (int i = 0; i < ret.length(); i++) ret.put(i, getDouble(i)); return ret; }
@Override public boolean equals(Object o) { // FIXME: this is BAD. it takes too long to work, and it breaks general equals contract if (o instanceof DataBuffer) { DataBuffer d = (DataBuffer) o; if (d.length() != length()) return false; for (int i = 0; i < length(); i++) { double eps = Math.abs(getDouble(i) - d.getDouble(i)); if (eps > 1e-12) return false; } } return true; }
@Override public void write(DataOutputStream out) throws IOException { if (length() >= Integer.MAX_VALUE) throw new IllegalArgumentException("Length of data buffer can not be >= Integer.MAX_VALUE on output"); // log.info("Saving dType: {}", dataType().name()); out.writeUTF(allocationMode.name()); out.writeLong(length()); out.writeUTF(dataType().name()); if (dataType() == Type.DOUBLE) { for (int i = 0; i < length(); i++) out.writeDouble(getDouble(i)); } else if (dataType() == Type.LONG) { for (int i = 0; i < length(); i++) out.writeLong(getLong(i)); } else if (dataType() == Type.INT) { for (int i = 0; i < length(); i++) out.writeInt(getInt(i)); } else if (dataType() == Type.HALF) { for (int i = 0; i < length(); i++) { out.writeShort(getShort(i)); } } else { for (int i = 0; i < length(); i++) { out.writeFloat(getFloat(i)); } } }
@Override public Number getNumber(long i) { if (dataType() == Type.DOUBLE) return getDouble(i); else if (dataType() == Type.INT) return getInt(i); else if (dataType() == Type.LONG) return getLong(i); return getFloat(i); }
@Override public double getDouble(long i) { allocator.synchronizeHostData(this); return super.getDouble(i); }
@Override public double getDouble(long i) { allocator.synchronizeHostData(this); return super.getDouble(i); }