/** * Returns the row vector, locking the data if {@code shouldLock} is {@code * true}. */ private SparseDoubleVector getRowVector(int row, boolean shouldLock) { int c = cols.get(); if (shouldLock) lockRow(row, c); // Ensure that the column data is up to date while (lastVectorCacheUpdate.get() != modifications.get()) updateVectorCache(); int[] colArr = rowToColsCache[row]; SparseDoubleVector rowVec = new SparseHashDoubleVector(c); for (int column : colArr) rowVec.set(column, matrixEntries.get(new Entry(row, column))); if (shouldLock) unlockRow(row, c); return rowVec; }
/** * {@inheritDoc} */ public void setRow(int row, DoubleVector colValues) { checkIndices(row, colValues.length(), true); int c = cols.get(); lockRow(row, c); boolean modified = false; for (int col = 0; col < c; ++col) { double val = colValues.get(col); Entry e = new Entry(row, col); boolean present = matrixEntries.containsKey(e); if (val != 0) { matrixEntries.put(e, val); // Only invalidate the cache if the number of rows or columns // containing data has changed modified = modified || !present; } else if (present) { matrixEntries.remove(e); modified = true; } } if (modified) modifications.incrementAndGet(); unlockRow(row, c); }
/** * Returns the row vector, locking the data if {@code shouldLock} is {@code * true}. */ private SparseDoubleVector getRowVector(int row, boolean shouldLock) { int c = cols.get(); if (shouldLock) lockRow(row, c); // Ensure that the column data is up to date while (lastVectorCacheUpdate.get() != modifications.get()) updateVectorCache(); int[] colArr = rowToColsCache[row]; SparseDoubleVector rowVec = new SparseHashDoubleVector(c); for (int column : colArr) rowVec.set(column, matrixEntries.get(new Entry(row, column))); if (shouldLock) unlockRow(row, c); return rowVec; }
/** * {@inheritDoc} */ public void setRow(int row, DoubleVector colValues) { checkIndices(row, colValues.length(), true); int c = cols.get(); lockRow(row, c); boolean modified = false; for (int col = 0; col < c; ++col) { double val = colValues.get(col); Entry e = new Entry(row, col); boolean present = matrixEntries.containsKey(e); if (val != 0) { matrixEntries.put(e, val); // Only invalidate the cache if the number of rows or columns // containing data has changed modified = modified || !present; } else if (present) { matrixEntries.remove(e); modified = true; } } if (modified) modifications.incrementAndGet(); unlockRow(row, c); }
/** * {@inheritDoc} */ public double[][] toDenseArray() { int r = rows.get(); int c = cols.get(); for (int i = 0; i < r; ++i) lockRow(i, c); double[][] m = new double[r][0]; for (int i = 0; i < r; ++i) { DoubleVector row = getRowVector(i); // Ensure that we see a consistent length for all the rows if (row.length() != c) row = Vectors.subview(row, 0, c); m[i] = row.toArray(); } for (int i = 0; i < r; ++i) unlockRow(i, c); return m; }
/** * {@inheritDoc} */ public double[][] toDenseArray() { int r = rows.get(); int c = cols.get(); for (int i = 0; i < r; ++i) lockRow(i, c); double[][] m = new double[r][0]; for (int i = 0; i < r; ++i) { DoubleVector row = getRowVector(i); // Ensure that we see a consistent length for all the rows if (row.length() != c) row = Vectors.subview(row, 0, c); m[i] = row.toArray(); } for (int i = 0; i < r; ++i) unlockRow(i, c); return m; }