From 137ee0647fd5b71556bcf9f02f7cc5e454baabb5 Mon Sep 17 00:00:00 2001 From: Peter Lawrey Date: Thu, 23 Apr 2015 17:44:33 +0100 Subject: [PATCH] Add a DoubleArray example. --- .../net/openhft/lang/values/DoubleArray.java | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100755 src/test/java/net/openhft/lang/values/DoubleArray.java diff --git a/src/test/java/net/openhft/lang/values/DoubleArray.java b/src/test/java/net/openhft/lang/values/DoubleArray.java new file mode 100755 index 000000000..bbc4c6b60 --- /dev/null +++ b/src/test/java/net/openhft/lang/values/DoubleArray.java @@ -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; + } +}