class SparseGrid<ValueType>
Grid
, a SparseGrid
is optimized for grids in which most of the cells are empty. Rather than being implemented using an internal array structure, which occupies full memory for all of its rows and columns from the start, a SparseGrid
is implemented internally using nested Map
s and therefore only uses as much memory as the cells that have been assigned to store actual data; empty unassigned cells consume no memory. But if the grid is expected to be mostly full of meaningful data, better performance and better memory usage will be achieved by using Grid
rather than SparseGrid
.
The following code, for example, creates an identity matrix of size n
, in which the elements are 1.0 along the main diagonal and 0.0 everywhere else:
SparseGrid<double> createIdentityMatrix(int n) { SparseGrid<double> matrix(n, n); for (int i = 0; i < n; i++) { matrix[i][i] = 1.0; } return matrix; }
Available since: 2014/07/09 version of C++ library
Constructor | ||
O(1) | Initializes a new empty 0x0 grid. | |
O(N) | Initializes a new grid of the given size. | |
O(N) | Initializes a new grid of the given size, with every cell set to the given value. | |
Methods | ||
O(N) | Returns true if the two grids contain the same elements. |
|
O(N) | Sets every grid element to the given value. | |
O((log N)2) | Returns the element at the specified row /col position in this grid. |
|
O(1) | Returns the grid's height, that is, the number of rows in the grid. | |
O(1) | Returns true if the specified row and column position is inside the bounds of the grid. |
|
O(1) | Returns true if the specified row and column position stores data. |
|
O(1) | Returns true if the grid has 0 rows and/or 0 columns. | |
O(N) | Calls the specified function on each element of the grid. | |
O(1) | Returns the number of columns in the grid. | |
O(1) | Returns the number of rows in the grid. | |
O(N) | Reinitializes the grid to have the specified number of rows and columns. | |
O((log N)2) | Replaces the element at the specified row /col location in this grid with a new value. |
|
O(1) | Returns the total number of elements in the grid. | |
O(N) | Converts the grid to a printable string representation. | |
O(N) | Converts the grid to a printable 2-D string representation. | |
O(1) | Returns the grid's width, that is, the number of columns in the grid. | |
Operator | ||
O((log N)2) | Overloads [] to select elements from this grid. |
|
O(N (log N)2) | Returns true if grid1 and grid2 contain the same elements. |
|
O(N (log N)2) | Returns true if grid1 and grid2 are different. |
|
O(N (log N)2) | Outputs the contents of the grid to the given output stream. | |
O(N (log N)2) | Reads the contents of the given input stream into the grid. |
SparseGrid(); SparseGrid(int nRows, int nCols);
The second form of the constructor is
more common and creates a grid with the specified number of rows
and columns. Each element of the grid is initialized to the
default value for the type. The default constructor creates an
empty grid for which the client must call resize
to
set the dimensions.
The third form also fills every cell of the grid with the given value.
The second and third constructors signal an error if a negative number of rows or columns is passed.
Usage:
SparseGrid<ValueType> grid; SparseGrid<ValueType> grid(nRows, nCols); SparseGrid<ValueType> grid(nRows, nCols, value);
bool equals(const SparseGrid& grid) const;
true
if the two grids are the same size and contain exactly the same element values.
Identical in behavior to the ==
operator.
Usage:
if (grid.equals(grid2)) ...
void fill(ValueType value) const;
SparseGrid
could be considered an anti-pattern.
That is, if you are planning to fill the entire grid, SparseGrid
may be a poor choice for your usage.
Grid
is better than SparseGrid
for full non-sparse data.
Usage:
grid.fill(value);
int numRows() const;
Usage:
int nRows = grid.numRows();
int numCols() const;
Usage:
int nCols = grid.numCols();
int height() const;
numRows
and both are provided for convenience.
Usage:
int nRows = grid.height();
int width() const;
numCols
and both are provided for convenience.
Usage:
int nCols = grid.numCols();
void resize(int nRows, int nCols);
This function signals an error if a negative number of rows or columns is passed.
Usage:
grid.resize(nRows, nCols);
bool inBounds(int row, int col) const;
true
if the specified row and column position
is inside the bounds of the grid.
Usage:
if (grid.inBounds(row, col)) ...
bool isSet(int row, int col) const;
true
if the specified row and column position
stores meaningful data; in other words, if it has had a value placed
there by a previous call to fill
, set
, []
, etc.
Usage:
if (grid.isSet(row, col)) ...
ValueType get(int row, int col); const ValueType& get(int row, int col) const;
row
/col
position in this grid.
If no data was set at the given row/column position, this method returns
a default value for the grid's value type.
This method signals an error if the
row
and col
arguments are outside
the grid boundaries.
Usage:
ValueType value = grid.get(row, col);
void set(int row, int col, ValueType value);
row
/col
location in this grid with a new value. This method signals an error
if the row
and col
arguments are outside
the grid boundaries.
Usage:
grid.set(row, col, value);
int size() const;
Usage:
int sz = grid.size();
bool isEmpty() const;
Usage:
int sz = grid.size();
string toString() const;
"{0:{0:r0c0, 1:r0c1, 2:r0c2}, 1:{0:r1c0, 1:r1c1, 2:r1c2}}"
for a 2x3 grid.
Note that the format is slightly different than the format for a normal Grid
.
Usage:
string str = grid.toString();
string toString2D() const;
"{{r0c0, r0c1, r0c2},\n {r1c0, r1c1, r1c2},\n {r2c0, r2c1, r2c2},\n {r3c0, r3c1, r3c2}}"
Usage:
string str = grid.toString2D();
void mapAll(void (*fn)(ValueType value)) const; void mapAll(void (*fn)(const ValueType& value)) const; void mapAll(FunctorType fn) const;
Usage:
grid.mapAll(fn);
SparseGridRow operator[](int row); const SparseGridRow operator[](int row) const;
[]
to select elements from this grid.
This extension enables the use of traditional array notation to
get or set individual elements.
If no data was set at the given row/column position, this method returns
a default value for the grid's value type.
This method signals an error if
the row
and col
arguments are outside
the grid boundaries.
Usage:
grid[row][col]