in src/main/java/com/twitter/sbf/core/SparseBinaryMatrix.java [119:175]
public void initFromRows(SimpleIterator<String> lines) {
this.reset();
int rowId = 0;
Optional<String> lineOpt;
Int2ObjectMap<IntArrayList> colsMap = new Int2ObjectOpenHashMap<>();
int maxColId = -1;
while (true) {
lineOpt = lines.next();
if (!lineOpt.isPresent()) {
break;
} else {
String line = lineOpt.get().trim();
if (!line.isEmpty()) {
String[] tokens = line.split("\\s+");
int[] row = new int[tokens.length];
for (int i = 0; i < tokens.length; i++) {
int colId = Integer.parseInt(tokens[i]) - 1; // convert to 0-based
if (colId < 0) {
throw new RuntimeException(
String.format(
"Column %d smaller than 1, in line number %d, line:'%s'",
colId + 1, rowId + 1, line
)
);
}
row[i] = colId;
if (!colsMap.containsKey(colId)) {
colsMap.put(colId, new IntArrayList());
}
colsMap.get(colId).add(rowId);
if (colId > maxColId) {
maxColId = colId;
}
}
Arrays.sort(row);
this.rows[rowId] = row;
}
rowId++;
if (rowId > this.numRows) {
throw new RuntimeException(
"More rows in input rows file than the expected " + this.numRows
);
}
}
}
if (maxColId > this.numCols) {
this.numCols = maxColId + 1;
this.cols = new IntSet[this.numCols];
}
for (int colId = 0; colId < this.numCols; colId++) {
if (colsMap.containsKey(colId) && !colsMap.get(colId).isEmpty()) {
this.cols[colId] = new IntOpenHashSet(colsMap.get(colId));
} else {
this.cols[colId] = new IntOpenHashSet();
}
}
}