sb.append(0).append(",").append(sdv.get(0));
for (int i = 1; i < nz.length; ++i)
sb.append(",").append(i).append(",").append(sdv.get(i));
}
else {
SparseVector sv = (SparseVector)vector;
int[] nz = sv.getNonZeroIndices();
sb = new StringBuilder(nz.length * 4);
// special case the first
sb.append(0).append(",")
.append(sv.getValue(0).doubleValue());
for (int i = 1; i < nz.length; ++i)
sb.append(",").append(i).append(",").
append(sv.getValue(i).doubleValue());
}
}
else {
boolean first = true;
sb = new StringBuilder(vectorLength / 2);
for (int i = 0; i < vector.length(); ++i) {
double d = vector.getValue(i).doubleValue();
if (d != 0d) {
if (first) {
sb.append(i).append(",").append(d);
first = false;
}
else {
sb.append(",").append(i).append(",").append(d);
}
}
}
}
pw.println(sb.toString());
pw.flush();
break;
}
case TEXT: {
PrintWriter pw = new PrintWriter(writer);
pw.println(word + "|" + VectorIO.toString(vector));
pw.flush();
break;
}
case BINARY: {
writer.writeUTF(word);
for (int i = 0; i < vector.length(); ++i) {
writer.writeDouble(vector.getValue(i).doubleValue());
}
break;
}
case SPARSE_BINARY: {
writer.writeUTF(word);
if (vector instanceof SparseVector) {
if (vector instanceof DoubleVector) {
SparseDoubleVector sdv = (SparseDoubleVector)vector;
int[] nz = sdv.getNonZeroIndices();
writer.writeInt(nz.length);
for (int i : nz) {
writer.writeInt(i);
writer.writeDouble(sdv.get(i));
}
}
else {
SparseVector sv = (SparseVector)vector;
int[] nz = sv.getNonZeroIndices();
writer.writeInt(nz.length);
for (int i : nz) {
writer.writeInt(i);
writer.writeDouble(sv.getValue(i).doubleValue());
}
}
}
else {
// count how many are non-zero