Add a DoubleArray example.

This commit is contained in:
Peter Lawrey
2015-04-23 17:44:33 +01:00
parent b8b593b775
commit 137ee0647f
+89
View File
@@ -0,0 +1,89 @@
package net.openhft.lang.values;
import net.openhft.lang.io.Bytes;
import net.openhft.lang.io.DirectStore;
import net.openhft.lang.model.Byteable;
/**
* Created by peter.lawrey on 23/04/2015.
*/
public class DoubleArray implements Byteable {
private static int LENGTH = 0; // assume a 32-bit size.
private static int CAPACITY = LENGTH + 4; // assume a 32-bit size.
private static int BASE = CAPACITY + 4;
private Bytes bytes;
private long offset;
public DoubleArray(int capacity) {
bytes = DirectStore.allocate(BASE + capacity * 8L).bytes();
offset = 0;
}
@Override
public void bytes(Bytes bytes, long offset) {
this.bytes = bytes;
this.offset = offset;
}
@Override
public Bytes bytes() {
return bytes;
}
@Override
public long offset() {
return offset;
}
@Override
public int maxSize() {
return BASE + length() * 8;
}
public int length() {
return bytes.readInt(LENGTH + offset);
}
public int capacity() {
return bytes.readInt(CAPACITY + offset);
}
public double getDataAt(int index) {
if (index < 0 || index >= length()) throw new ArrayIndexOutOfBoundsException();
return bytes.readDouble(BASE + offset + index * 8L);
}
public void setDataAt(int index, double d) {
if (index < 0 || index >= capacity()) throw new ArrayIndexOutOfBoundsException();
if (length() <= index)
setLength(index + 1);
bytes.writeDouble(BASE + offset + index * 8L, d);
}
public void setLength(int length) {
if (length < 0 || length >= capacity()) throw new IllegalArgumentException();
bytes.writeInt(LENGTH + offset, length);
}
public void addData(double d) {
int index = length();
if (index >= capacity()) throw new IllegalStateException();
bytes.writeInt(LENGTH + offset, index + 1);
bytes.writeDouble(BASE + offset + index * 8L, d);
}
public void setData(double[] doubles) {
if (doubles.length > capacity()) throw new IllegalArgumentException();
bytes.writeInt(LENGTH + offset, doubles.length);
for (int index = 0; index < doubles.length; index++)
bytes.writeDouble(BASE + offset + index * 8L, doubles[index]);
}
public int getDataUsing(double[] doubles) {
int length = Math.min(length(), doubles.length);
for (int index = 0; index < length; index++)
doubles[index] = bytes.readDouble(BASE + offset + index * 8L);
return length;
}
}