class Grid<ValueType>
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 | ||
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 element set to the specified value. | |
Methods | ||
O(1) | Removes all elements from this grid. | |
O(N) | Returns true if the two grids contain the same elements. |
|
O(N) | Sets every element in this grid to the given value. | |
O(1) | Returns the element at the specified row /col or gridLocation in this grid. |
|
O(1) | Returns true if the specified row /col or gridLocation is inside the bounds of this grid. |
|
O(1) | Returns true if this grid has 0 rows and/or 0 columns. | |
O(1) | Returns a range containing all GridLocations for this grid. | |
O(N) | Calls the specified function on each element of this grid. | |
O(1) | Returns the number of columns in this grid. | |
O(1) | Returns the number of rows in this grid. | |
O(N) | Reinitializes this grid to have the specified number of rows and columns. | |
O(1) | Replaces the element at the specified row /col or gridLocation in this grid with a new value. |
|
O(1) | Returns the total number of elements in this grid. | |
O(N) | Returns a printable single-line string of this grid. | |
O(N) | Returns a printable 2-D string representation of this grid. | |
Operator | ||
O(N) | Iterates through the elements in a grid in row-major order. | |
grid[gridLocation] |
O(1) | Overloads [] to select elements from this grid by row/col or GridLocation. |
O(N) | Returns true if grid1 and grid2 contain the same elements. |
|
O(N) | Returns true if grid1 and grid2 are different. |
|
O(N) | Outputs the contents of the grid to the given output stream. | |
O(N) | Reads the contents of the given input stream into the grid. |
Grid(); Grid(int nRows, int nCols); Grid(int nRows, int nCols, const ValueType& value);
resize
to set the dimensions.
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 third form also fills every location 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:
Grid<ValueType> grid; Grid<ValueType> grid(nRows, nCols); Grid<ValueType> grid(nRows, nCols, value);
void clear();
Usage:
grid.clear();
bool equals(const Grid& 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(const ValueType& value) const;
Usage:
grid.fill(value);
ValueType get(int row, int col); ValueType get(GridLocation loc); const ValueType& get(int row, int col) const; const ValueType& get(GridLocation loc) const;
row
/col
location or GridLocation
in this grid. This method signals an error if the
specified location is outside the grid boundaries.
Usage:
ValueType value = grid.get(row, col); ValueType value = grid.get(gridLocation);
bool inBounds(int row, int col) const; bool inBounds(GridLocation loc) const;
true
if the specified row
/col
location or GridLocation
is inside the bounds of the grid.
Usage:
if (grid.inBounds(row, col)) ... if (grid.inBounds(gridLocation)) ...
bool isEmpty() const;
true
if the grid does not contain any rows or columns (size 0x0).
Usage:
if (grid.isEmpty()) ...
GridLocationRange locations() const;
Usage:
for (GridLocation loc: grid.locations() { ValueType val = grid[loc]; }
void mapAll(std::function<void (const ValueType&)> fn) const;
Usage:
grid.mapAll(fn);
int numCols() const;
Usage:
int nCols = grid.numCols();
int numRows() const;
Usage:
int nRows = grid.numRows();
void resize(int nRows, int nCols, bool retain = false);
retain
argument is true, it retains whatever previous grid contents can
be. If retain
is false or not given, all previous contents are discarded.
This function signals an error if a negative number of rows or columns is passed.
Usage:
grid.resize(nRows, nCols);
void set(int row, int col, const ValueType& value); void set(GridLocation loc, const ValueType& value);
row
/col
location or GridLocation
in this grid with a new value. This method signals an error
if the specified location is outside
the grid boundaries.
Usage:
grid.set(row, col, value); grid.set(gridLocation, value);
int size() const;
Usage:
int sz = grid.size();
string toString() const;
"{{r0c0, r0c1, r0c2}, {r1c0, r1c1, r1c2}}"
for a 2x3 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();
ValueType operator[];
[]
to select elements from this grid. Can select elements
by single argument of GridLocation or pair of brackets to select by row, then column.
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] grid[gridLocation]
for (ValueType elem : grid) for (ValueType& elem : grid)
Usage:
for (ValueType elem : grid) { cout << elem << endl; } for (ValueType& elem : grid) { // if reference type, elements are mutable elem *= 2; }
ostream& operator<<(const Grid& grid);Outputs the contents of
grid
to the given output stream.
The output is in the form { {r0c0, r0c1, r0c2}, {r1c0, r1c1, r1c2} }
where elements are listed in row-major order.
Usage:
cout << grid << endl;
istream& operator>>(Grid& grid);Reads the contents of the given input stream into
grid
. Any previous
contents of the grid are replaced.
The input is expected to be in the form { {r0c0, r0c1, r0c2}, {r1c0, r1c1, r1c2} }
where elements are listed in row-major order. If unable to read a proper
grid from the stream, the operation results in a stream fail state.
Usage:
if (infile >> grid) ...