/** * {@inheritDoc} */ public double addAndGet(int row, int col, double delta) { checkIndices(row, col, true); Entry e = new Entry(row, col); // Spin waiting for the entry to be unlocked while (lockedEntries.putIfAbsent(e, new Object()) != null) ; Double val = matrixEntries.get(e); double newVal = (val == null) ? delta : delta + val; if (newVal != 0) { matrixEntries.put(e, newVal); // Only invalidate the cache if the number of rows or columns // containing data has changed if (val == null) modifications.incrementAndGet(); } else { matrixEntries.remove(e); modifications.incrementAndGet(); } lockedEntries.remove(e); return newVal; }
/** * {@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 add(int row, int col, double delta) { checkIndices(row, col, true); Entry e = new Entry(row, col); // Spin waiting for the entry to be unlocked while (lockedEntries.putIfAbsent(e, new Object()) != null) ; Double val = matrixEntries.get(e); double newVal = (val == null) ? delta : delta + val; if (newVal != 0) { matrixEntries.put(e, newVal); // Only invalidate the cache if the number of rows or columns // containing data has changed if (val == null) modifications.incrementAndGet(); } else { matrixEntries.remove(e); modifications.incrementAndGet(); } lockedEntries.remove(e); return (val == null) ? 0 : val; }
/** * {@inheritDoc} */ public double getAndAdd(int row, int col, double delta) { checkIndices(row, col, true); Entry e = new Entry(row, col); // Spin waiting for the entry to be unlocked while (lockedEntries.putIfAbsent(e, new Object()) != null) ; Double val = matrixEntries.get(e); double newVal = (val == null) ? delta : delta + val; if (newVal != 0) { matrixEntries.put(e, newVal); // Only invalidate the cache if the number of rows or columns // containing data has changed if (val == null) modifications.incrementAndGet(); } else { matrixEntries.remove(e); modifications.incrementAndGet(); } lockedEntries.remove(e); return (val == null) ? 0 : val; }
/** * {@inheritDoc} */ public double addAndGet(int row, int col, double delta) { checkIndices(row, col, true); Entry e = new Entry(row, col); // Spin waiting for the entry to be unlocked while (lockedEntries.putIfAbsent(e, new Object()) != null) ; Double val = matrixEntries.get(e); double newVal = (val == null) ? delta : delta + val; if (newVal != 0) { matrixEntries.put(e, newVal); // Only invalidate the cache if the number of rows or columns // containing data has changed if (val == null) modifications.incrementAndGet(); } else { matrixEntries.remove(e); modifications.incrementAndGet(); } lockedEntries.remove(e); return newVal; }
/** * {@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 void setColumn(int column, DoubleVector rowValues) { checkIndices(rowValues.length(), column, true); int r = rows.get(); lockColumn(column, r); boolean modified = false; for (int row = 0; row < r; ++row) { double val = rowValues.get(row); Entry e = new Entry(row, column); 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(); unlockColumn(column, r); }
/** * {@inheritDoc} */ public void setColumn(int column, DoubleVector rowValues) { checkIndices(rowValues.length(), column, true); int r = rows.get(); lockColumn(column, r); boolean modified = false; for (int row = 0; row < r; ++row) { double val = rowValues.get(row); Entry e = new Entry(row, column); 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(); unlockColumn(column, r); }
/** * {@inheritDoc} */ public void set(int row, int col, double val) { checkIndices(row, col, true); Entry e = new Entry(row, col); // Spin waiting for the entry to be unlocked while (lockedEntries.putIfAbsent(e, new Object()) != null) ; 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 if (!present) modifications.incrementAndGet(); } else if (present) { matrixEntries.remove(e); modifications.incrementAndGet(); } lockedEntries.remove(e); }
/** * {@inheritDoc} */ public void set(int row, int col, double val) { checkIndices(row, col, true); Entry e = new Entry(row, col); // Spin waiting for the entry to be unlocked while (lockedEntries.putIfAbsent(e, new Object()) != null) ; 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 if (!present) modifications.incrementAndGet(); } else if (present) { matrixEntries.remove(e); modifications.incrementAndGet(); } lockedEntries.remove(e); }
/** * Locks all the column entries for this row, thereby preventing write or * read access to the values. Note that the number of columns to lock * <b>must</b> be the same value used with {@link #unlockRow(int,int)}, * otherwise the unlock may potentially unlock matrix entries associated * with this lock call. * * @param row the row to lock * @param colsToLock the number of rows to lock. This value should be the * number of {@link #cols} at the time of the call. */ private void lockRow(int row, int colsToLock) { // Put in an entry for all the row's columns for (int col = 0; col < colsToLock; ++col) { Entry e = new Entry(row, col); // Spin waiting for the entry to be unlocked while (lockedEntries.putIfAbsent(e, new Object()) != null) ; } }
/** * Locks all the row entries for this column, thereby preventing write or * read access to the values. Note that the number of rows to lock * <b>must</b> be the same value used with {@link #unlockColumn(int,int)}, * otherwise the unlock may potentially unlock matrix entries associated * with this lock call. * * @param col the column to lock * @param rowsToLock the number of rows to lock. This value should be the * number of {@link #rows} at the time of the call. */ private void lockColumn(int col, int rowsToLock) { // Put in an entry for all the columns's rows for (int row = 0; row < rowsToLock; ++row) { Entry e = new Entry(row, col); // Spin waiting for the entry to be unlocked while (lockedEntries.putIfAbsent(e, new Object()) != null) ; } }
/** * Locks all the column entries for this row, thereby preventing write or * read access to the values. Note that the number of columns to lock * <b>must</b> be the same value used with {@link #unlockRow(int,int)}, * otherwise the unlock may potentially unlock matrix entries associated * with this lock call. * * @param row the row to lock * @param colsToLock the number of rows to lock. This value should be the * number of {@link #cols} at the time of the call. */ private void lockRow(int row, int colsToLock) { // Put in an entry for all the row's columns for (int col = 0; col < colsToLock; ++col) { Entry e = new Entry(row, col); // Spin waiting for the entry to be unlocked while (lockedEntries.putIfAbsent(e, new Object()) != null) ; } }
/** * 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; }
/** * Locks all the row entries for this column, thereby preventing write or * read access to the values. Note that the number of rows to lock * <b>must</b> be the same value used with {@link #unlockColumn(int,int)}, * otherwise the unlock may potentially unlock matrix entries associated * with this lock call. * * @param col the column to lock * @param rowsToLock the number of rows to lock. This value should be the * number of {@link #rows} at the time of the call. */ private void lockColumn(int col, int rowsToLock) { // Put in an entry for all the columns's rows for (int row = 0; row < rowsToLock; ++row) { Entry e = new Entry(row, col); // Spin waiting for the entry to be unlocked while (lockedEntries.putIfAbsent(e, new Object()) != null) ; } }
/** * 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; }
/** * Returns the column vector, locking the data if {@code shouldLock} is * {@code true}. */ private SparseDoubleVector getColumnVector(int column, boolean shouldLock) { int r = rows.get(); if (shouldLock) lockColumn(column, r); // Ensure that the column data is up to date while (lastVectorCacheUpdate.get() != modifications.get()) updateVectorCache(); int[] rowArr = colToRowsCache[column]; SparseDoubleVector colVec = new SparseHashDoubleVector(r); for (int row : rowArr) colVec.set(row, matrixEntries.get(new Entry(row, column))); if (shouldLock) unlockColumn(column, r); return colVec; }
/** * Returns the column vector, locking the data if {@code shouldLock} is * {@code true}. */ private SparseDoubleVector getColumnVector(int column, boolean shouldLock) { int r = rows.get(); if (shouldLock) lockColumn(column, r); // Ensure that the column data is up to date while (lastVectorCacheUpdate.get() != modifications.get()) updateVectorCache(); int[] rowArr = colToRowsCache[column]; SparseDoubleVector colVec = new SparseHashDoubleVector(r); for (int row : rowArr) colVec.set(row, matrixEntries.get(new Entry(row, column))); if (shouldLock) unlockColumn(column, r); return colVec; }
/** * Unlocks the column for write access based on the number of rows that were * initially locked * * @param col the column to unlock * @param rowsToUnlock the number of rows locked by the previous {@link * #lockColumn(int, int) call} */ private void unlockColumn(int col, int rowsToUnlock) { // Remove the locks on all of the columns's columns for (int row = 0; row < rowsToUnlock; ++row) lockedEntries.remove(new Entry(row, col)); }
/** * Unlocks the column for write access based on the number of rows that were * initially locked * * @param col the column to unlock * @param rowsToUnlock the number of rows locked by the previous {@link * #lockColumn(int, int) call} */ private void unlockColumn(int col, int rowsToUnlock) { // Remove the locks on all of the columns's columns for (int row = 0; row < rowsToUnlock; ++row) lockedEntries.remove(new Entry(row, col)); }