package org.canova.common;

import org.canova.api.io.data.DoubleWritable;
import org.canova.api.io.data.FloatWritable;
import org.canova.api.writable.Writable;
import org.nd4j.linalg.api.buffer.DataBuffer;
import org.nd4j.linalg.api.ndarray.INDArray;

import java.util.ArrayList;
import java.util.Collection;

/**
 * @author Adam Gibson
 */
public class RecordConverter {
    private RecordConverter() {}


    /**
     * Convert an ndarray to a record
     * @param array the array to convert
     * @return the record
     */
    public static Collection<Writable> toRecord(INDArray array) {
        INDArray linear = array.linearView();
        Collection<Writable> writables = new ArrayList<>();
        for(int i = 0; i < linear.length(); i++) {
            writables.add(array.data().dataType() == DataBuffer.DOUBLE ? new DoubleWritable(linear.getDouble(i)) : new FloatWritable(linear.getFloat(i)));
        }
        return writables;
    }

}

@@ -243,16 +243,53 @@
 NDArray;
+%0Aimport org.nd4j.linalg.factory.Nd4j;
 %0A%0Aimport
@@ -338,16 +338,43 @@
 lection;
+%0Aimport java.util.Iterator;
 %0A%0A/**%0A *
@@ -463,16 +463,498 @@
 r() %7B%7D%0A%0A
+    /**%0A     * Convert an ndarray to a record%0A     * @param record the array to convert%0A     * @return the record%0A     */%0A    public static INDArray toRecord(Collection%3CWritable%3E record) %7B%0A        Iterator%3CWritable%3E writables = record.iterator();%0A        INDArray linear = Nd4j.zeros(record.size());%0A%0A        int count = 0;%0A        while(writables.hasNext()) %7B%0A            linear.putScalar(count++,Double.valueOf(writables.next().toString()));%0A        %7D%0A        return linear;%0A    %7D
 %0A    /**

update record converter