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;
    }
}

@@ -140,16 +140,56 @@
 yteable;
+%0Aimport net.openhft.lang.model.Copyable;
 %0A%0A/**%0A *
@@ -276,16 +276,39 @@
 Byteable
+, Copyable%3CDoubleArray%3E
  %7B%0A    p
@@ -655,32 +655,33 @@
 set = 0;%0A    %7D%0A%0A
+%0A
     @Override%0A  
@@ -2665,11 +2665,247 @@
 h;%0A    %7D
+%0A%0A    @Override%0A    public void copyFrom(DoubleArray doubleArray) %7B%0A        int length = length();%0A        doubleArray.setLength(length);%0A        for (int i = 0; i %3C length; i++)%0A            doubleArray.setDataAt(i, getDataAt(i));%0A    %7D
 %0A%7D%0A

Add a DoubleArray example.