#include "grid.h"

class Grid<ValueType>

The 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:
   Grid createIdentityMatrix(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.

Constructor detail


Grid();
Grid(int nRows, int nCols);
Initializes a new grid. 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.

Usage:

Grid<ValueType> grid;
Grid<ValueType> grid(nRows, nCols);

Method detail


int numRows();
Returns the number of rows in the grid.

Usage:

int nRows = grid.numRows();

int numCols();
Returns the number of columns in the grid.

Usage:

int nCols = grid.numCols();

void resize(int nRows, int nCols);
Reinitializes the grid to have the specified number of rows and columns. Any previous grid contents are discarded.

Usage:

grid.resize(nRows, nCols);

bool inBounds(int row, int col);
Returns 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);
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. The second form of the call allows the client to pass a data value of any type to the callback function.

Usage:

grid.mapAll(fn);
grid.mapAll(fn, data);

Operator detail


GridRow operator[](int row);
Overloads [] 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]

Macro detail


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.