public class AtomicGrowingSparseHashMatrix extends Object implements AtomicMatrix, SparseMatrix
SparseMatrix
class that is
optimized for operations only access one value of the matrix at a time. This
class allows multiple threads to operate on the same matrix where all methods
are concurrent to the fullest extent possible.
This class offers different performance trade-offs than the AtomicGrowingSparseMatrix
. Specifically, this class supports concurrent
write access to the same row. Atomicity is localized to the specific matrix
entry, which ensures high concurrency for workloads consisting primarily of
get
, addAndGet
, getAndAdd
and set
. This
performance comes at a cost for the full row- or column-related operations.
Each time these operations are used after a modification to the matrix, a
full O(m * n)
computation must be done to determine the structure of
the matrix. An subsequent calls until the next modification will not incur
this penalty and will operate in O(k)
where k
is the number
of non-zero entries in the row or column. It is therefore highly recommended
that vector method be used only during periods when the matrix is not likely
to be modified.
This class also provides support for access the matrix data without ensuring
data coherency (i.e. non-atomic) through the getRowVectorUnsafe(int)
and getColumnVectorUnsafe(int)
methods. These methods are designed
to be used only in circumstances where the matrix is guaranteed to only be
accessed by one thread. In such circumstances, the overhead of locking row
or column data can be avoided with no side-effects. Use these methods with
great caution.
AtomicGrowingSparseMatrix
,
Matrices.synchronizedMatrix(Matrix)
Matrix.Type
Constructor and Description |
---|
AtomicGrowingSparseHashMatrix()
Create an
AtomicGrowingSparseMatrix with 0 rows and 0 columns. |
Modifier and Type | Method and Description |
---|---|
double |
addAndGet(int row,
int col,
double delta)
Return the value stored at (
row , col ) plus delta ,
and then increment the specified cell by delta . |
int |
columns()
Returns the number of columns in this
Matrix . |
double |
get(int row,
int col)
Returns the value of the
Matrix at the provided row and column. |
double |
getAndAdd(int row,
int col,
double delta)
Return the value stored at (
row , col ), and add delta to the same index. |
double[] |
getColumn(int column)
Returns the entire column.
|
SparseDoubleVector |
getColumnVector(int column)
Returns the column as a vector.
|
SparseDoubleVector |
getColumnVectorUnsafe(int column)
Provides non-atomic access to the data at the specified column, which may
present an inconsistent view of the data if this matrix is being
concurrently modified.
|
double[] |
getRow(int row)
Returns the entire row.
|
SparseDoubleVector |
getRowVector(int row)
Returns a
DoubleVector for an entire row. |
SparseDoubleVector |
getRowVectorUnsafe(int row)
Provides non-atomic access to the data at the specified row, which may
present an inconsistent view of the data if this matrix is being
concurrently modified.
|
int |
rows()
Returns the number of rows in this
Matrix . |
void |
set(int row,
int col,
double val)
Sets the location at the row and column to the provided value.
|
void |
setColumn(int column,
double[] values)
Sets the values for the column of this
Matrix using the provided
array. |
void |
setColumn(int column,
DoubleVector rowValues)
Sets the values for the column of this
Matrix using the provided
DoubleVector . |
void |
setRow(int row,
double[] columns)
Sets the values for the row of this
Matrix using the provided
array. |
void |
setRow(int row,
DoubleVector colValues)
Sets the values for the row of this
Matrix using the provided
DoubleVector . |
double[][] |
toDenseArray()
Converts the
Matrix to a two dimensional array. |
public AtomicGrowingSparseHashMatrix()
AtomicGrowingSparseMatrix
with 0 rows and 0 columns.public double addAndGet(int row, int col, double delta)
row
, col
) plus delta
,
and then increment the specified cell by delta
.addAndGet
in interface AtomicMatrix
row
- The row of the cell to modify and return.col
- The column of the cell to modify and return.delta
- The amount to change the specified cell.row
, col
) after
adding delta
.public int columns()
Matrix
.public double get(int row, int col)
Matrix
at the provided row and column.public double getAndAdd(int row, int col, double delta)
row
, col
), and add delta
to the same index.getAndAdd
in interface AtomicMatrix
row
- The row of the cell to return and modify.col
- The column of the cell to return and modify.delta
- The amount to change the specified cell.row
, col
) prior to
adding delta
.public double[] getColumn(int column)
rows()
public SparseDoubleVector getColumnVector(int column)
SparseVector
instance. The length of the returned row vector reflects the size of
matrix at the time of the call, which may be different from earlier calls
to rows()
getColumnVector
in interface Matrix
getColumnVector
in interface SparseMatrix
column
- The column to return a DoubleVector
for.DoubleVector
representing the column at column
.public SparseDoubleVector getColumnVectorUnsafe(int column)
public double[] getRow(int row)
columns()
.public SparseDoubleVector getRowVector(int row)
DoubleVector
for an entire row. Implementations should
return an approriately typed DoubleVector. Whether updates to the vector
are written through to the backing matrix is left open to the specific
implementation. Implementations that maintain their data in a sparse
format are encouraged to return a SparseVector
instance. The length of the returned row vector reflects the size of
matrix at the time of the call, which may be different from earlier calls
to columns()
.getRowVector
in interface Matrix
getRowVector
in interface SparseMatrix
row
- the index of the row to return.DoubleVector
representing the row at row
.public SparseDoubleVector getRowVectorUnsafe(int row)
public int rows()
Matrix
.public void set(int row, int col, double val)
public void setColumn(int column, double[] values)
Matrix
using the provided
array. Note that the array itself is not made internal to the instance
itself.public void setColumn(int column, DoubleVector rowValues)
Matrix
using the provided
DoubleVector
. Note that the DoubleVector
itself is not
made internal to the instance itself.public void setRow(int row, double[] columns)
Matrix
using the provided
array. Note that the array itself is not made internal to the instance
itself.public void setRow(int row, DoubleVector colValues)
Matrix
using the provided
DoubleVector
. Note that the DoubleVector
itself is not
made internal to the instance itself.public double[][] toDenseArray()
Matrix
to a two dimensional array. Note that for
large matrices, this may exhaust all available memory.toDenseArray
in interface Matrix
Matrix
.Copyright © 2012. All Rights Reserved.