The SimpleCxxLib package


#include "grid.h"

class Grid<ValueType>

This class stores an indexed, two-dimensional array. 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:
   Grid<double> createIdentityMatrix(int n) {
      Grid<double> 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
get(row, col) Returns the element at the specified row/col position in this grid.
inBounds(row, col) Returns true if the specified row and column position is inside the bounds of the grid.
mapAll(fn) Calls the specified function on each element of the grid.
numCols() Returns the number of columns in the grid.
numRows() Returns the number of rows in the grid.
resize(nRows, nCols) Reinitializes the grid to have the specified number of rows and columns.
set(row, col, value) Replaces the element at the specified row/col location in this grid with a new value.
toString() Converts the grid to a printable string representation.
Operator
grid[row][col] Overloads [] to select elements from this grid.

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);
Grid<ValueType> grid = {{ value1, value2, value3 }, { value4, value5, value6 }};

Method detail


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

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) const;
Returns true if the specified row and column position is inside the bounds of the grid.

Usage:

if (grid.inBounds(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. 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);

string toString();
Converts the grid to a printable string representation.

Usage:

string str = grid.toString();

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

Operator detail


GridRow operator[](int row);
const GridRow 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. This method signals an error if the row and col arguments are outside the grid boundaries.

Usage:

grid[row][col]