class Grid<ValueType>Grid class stores an indexed, two-dimensional
array. The following function, 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:
GridcreateIdentityMatrix(int n) { Grid matrix(n, n); for (int i = 0; i < n; i++) { matrix[i][i] = 1.0; } return matrix; }
| Constructor | |
| Grid() Grid(nRows, nCols) | Initializes a new grid. |
| Methods | |
| numRows() | Returns the number of rows in the grid. |
| numCols() | Returns the number of columns in the grid. |
| resize(nRows, nCols) | Reinitializes the grid to have the specified number of rows and columns. |
| inBounds(row, col) | Returns true if the specified row and column position is inside the bounds of the grid. |
| mapAll(fn) mapAll(fn, data) | Calls the specified function on each element of the grid. |
| Operator | |
| grid[row][col] | Overloads [] to select elements from this grid. |
| Macro | |
| foreach(ValueType value in grid) | Iterates over the elements of the grid in row-major order, in which all the elements of row 0 are processed, followed by the elements in row 1, and so on. |
Grid(); Grid(int nRows, int nCols);
resize to
set the dimensions.
Usage:
Grid<ValueType> grid; Grid<ValueType> grid(nRows, nCols);
int numRows();
Usage:
int nRows = grid.numRows();
int numCols();
Usage:
int nCols = grid.numCols();
void resize(int nRows, int nCols);
Usage:
grid.resize(nRows, nCols);
bool inBounds(int row, int col);
true if the specified row and column position
is inside the bounds of the grid.
Usage:
if (grid.inBounds(row, col)) . . .
void mapAll(void (*fn)(ValueType value));
void mapAll(void (*fn)(ValueType value, ClientDataType & data),
ClientDataType & data);
Usage:
grid.mapAll(fn); grid.mapAll(fn, data);
GridRow operator[](int row);
[] to select elements from this grid.
This extension enables the use of traditional array notation to
get or set individual elements. This method signals an error if
the row and col arguments are outside
the grid boundaries.
Usage:
grid[row][col]
foreach (ValueType value in grid) . . .