`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. | |

nRows, nCols) |
O(N) | Initializes a new grid of the given size. |

nRows, nCols, value) |
O(N) | Initializes a new grid of the given size, with every cell set to the given value. |

Methods | ||

grid) |
O(N) | Returns `true` if the two grids contain the same elements. |

value) |
O(N) | Sets every grid element to the given value. |

row, col) |
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. | |

row, col) |
O(1) | Returns `true` if the specified row and column position is inside the bounds of the grid. |

row, col) |
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. | |

fn) |
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. | |

nRows, nCols) |
O(N) | Reinitializes the grid to have the specified number of rows and columns. |

row, col, value) |
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 | ||

grid[row][col] |
O((log N)^{2}) |
Overloads `[]` to select elements from this grid. |

grid1 == grid1 |
O(N (log N)^{2}) |
Returns `true` if `grid1` and `grid2` contain the same elements. |

grid1 != grid2 |
O(N (log N)^{2}) |
Returns `true` if `grid1` and `grid2` are different. |

ostream << grid |
O(N (log N)^{2}) |
Outputs the contents of the grid to the given output stream. |

istream >> grid |
O(N (log N)^{2}) |
Reads the contents of the given input stream into the grid. |

SparseGrid(); SparseGrid(int nRows, int nCols);

Initializes a new grid.
The first form of the constructor creates an empty grid that
contains zero rows and columns.

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;

Returns

`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;

Sets every grid element to the given value.
The entire contents of the grid are replaced with this value in every cell.
Note that using this method on a

`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;

Returns the number of rows in the grid.

Usage:

int nRows = grid.numRows();

int numCols() const;

Returns the number of columns in the grid.

Usage:

int nCols = grid.numCols();

int height() const;

Returns the grid's height, that is, the number of rows in the grid.
This is equivalent to

`numRows`

and both are provided for convenience.
Usage:

int nRows = grid.height();

int width() const;

Returns the grid's width, that is, the number of columns in the grid.
This is equivalent to

`numCols`

and both are provided for convenience.
Usage:

int nCols = grid.numCols();

void resize(int nRows, int nCols);

Reinitializes the grid to have the specified number of rows
and columns. Each element of the newly resized grid is empty.
Any previous grid contents are discarded.

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;

Returns

`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;

Returns

`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;

Returns the element at the specified

`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);

Replaces the element at the specified

`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;

Returns the total number of elements in the grid,
which is equal to the number of rows times the number of columns.

Usage:

int sz = grid.size();

bool isEmpty() const;

Returns the total number of elements in the grid,
which is equal to the number of rows times the number of columns.

Usage:

int sz = grid.size();

string toString() const;

Converts the grid to a printable string representation, such as

`"{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;

Converts the grid to a printable 2-D string representation, such as the following for a 4x3 grid:

"{{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;

Calls the specified function on each element of the grid. The
elements are processed in *row-major order,* in which
all the elements of row 0 are processed, followed by the elements
in row 1, and so on.

Usage:

grid.mapAll(fn);

SparseGridRow operator[](int row); const SparseGridRow operator[](int row) const;

Overloads

`[]`

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]