package com.intel.daal.examples.datasource;
import java.nio.DoubleBuffer;
import java.nio.IntBuffer;
import com.intel.daal.data_management.data.*;
import com.intel.daal.examples.utils.Service;
import com.intel.daal.services.DaalContext;
class DataStructuresSOA {
private static final long firstReadRow = 0;
private static final long nRead = 3;
private static final long nVectorsSOA = 10;
private static final long nFeaturesSOA = 4;
private static DaalContext context = new DaalContext();
public static void main(String[] args) {
System.out.println("Structure of array (SOA) numeric table example");
long readFeatureIdx;
int[] iDataSOA = { 1, 2, 3, 4, 5, 1, 2, 3, 4, 5 };
double[] dDataSOA = { 1.0, 1.2, 1.4, 1.6, 1.8, 2.0, 2.2, 2.4, 2.6, 2.8 };
float[] fDataSOA = { 3.1f, 3.2f, 3.3f, 3.4f, 3.5f, 3.6f, 3.7f, 3.8f, 3.9f, 4.0f };
long[] lDataSOA = { -10, -20, -30, -40, -50, -60, -70, -80, -90, -100 };
SOANumericTable dataTable = new SOANumericTable(context, nFeaturesSOA, nVectorsSOA);
dataTable.setArray(iDataSOA, 0);
dataTable.setArray(dDataSOA, 1);
dataTable.setArray(fDataSOA, 2);
dataTable.setArray(lDataSOA, 3);
DoubleBuffer dataDouble = DoubleBuffer.allocate((int) (nRead * nFeaturesSOA));
dataDouble = dataTable.getBlockOfRows(firstReadRow, nRead, dataDouble);
printDoubleBuffer(dataDouble, nFeaturesSOA, nRead, "Print SOA data structures as double:");
dataTable.releaseBlockOfRows(firstReadRow, nRead, dataDouble);
IntBuffer dataInt = IntBuffer.allocate((int) nVectorsSOA);
readFeatureIdx = 0;
dataInt = dataTable.getBlockOfColumnValues(readFeatureIdx, firstReadRow, nVectorsSOA, dataInt);
printIntBuffer(dataInt, 1, nVectorsSOA, "Print the first feature of SOA:");
dataTable.releaseBlockOfColumnValues(readFeatureIdx, firstReadRow, nVectorsSOA, dataInt);
DataDictionary dictionary = dataTable.getDictionary();
System.out.println("Number of features in table: " + dictionary.getNumberOfFeatures());
System.out.println("");
System.out.println("Default type in autogenerated dictionary:");
for (int i = 0; i < nFeaturesSOA; i++) {
DataFeature df = dictionary.getFeature(i);
DataFeatureUtils.FeatureType featureType = df.getFeatureType();
System.out.print("Type of feature #" + i + ": ");
System.out.println(featureType.toString());
}
System.out.println("");
DataFeature categoricalFeature = dictionary.getFeature(0);
categoricalFeature.setFeatureType(DataFeatureUtils.FeatureType.DAAL_CATEGORICAL);
System.out.println("Modified type in the dictionary:");
for (int i = 0; i < nFeaturesSOA; i++) {
DataFeature df = dictionary.getFeature(i);
DataFeatureUtils.FeatureType featureType = df.getFeatureType();
System.out.print("Type of feature #" + i + ": ");
System.out.println(featureType.toString());
}
System.out.println("");
context.dispose();
}
private static void printDoubleBuffer(DoubleBuffer buf, long nColumns, long nRows, String message) {
int step = (int) nColumns;
System.out.println(message);
for (int i = 0; i < nRows; i++) {
for (int j = 0; j < nColumns; j++) {
System.out.format("%6.3f ", buf.get(i * step + j));
}
System.out.println("");
}
System.out.println("");
}
private static void printIntBuffer(IntBuffer buf, long nColumns, long nRows, String message) {
int step = (int) nColumns;
System.out.println(message);
for (int i = 0; i < nRows; i++) {
for (int j = 0; j < nColumns; j++) {
System.out.format("%3d ", buf.get(i * step + j));
}
System.out.println("");
}
System.out.println("");
}
}