#include "gridlocation.h"

class GridLocationRange

This class represents a two-dimensional rectangular ange of grid locations that can be looped over.

Common usage patterns:
GridLocationRange range(0, 0, 10, 5);
for (GridLocation loc : range) {
    cout << loc << endl; // print location itself
}
The locations() method of the Grid class returns a GridLocationRange for the entire grid.
for (GridLocation loc : grid.locations()) {
    cout << grid[loc] << endl; // access grid element at each location and print it
}
Constructor
GridLocationRange(startLocationendLocation)
GridLocationRange(startRowstartColendRowendCol) 
Creates a GridLocationRange with the specified boundaries.
Methods
contains(loc) Returns true if loc is contained in this range.
endCol() Returns the last column in this range, inclusive.
endLocation() Returns the last row/column location in this range, inclusive.
endRow() Returns the last row in this range, inclusive.
isEmpty() Returns true if this range contains no rows or columns.
numCols() Returns the count of columns in this range.
numRows() Returns the count of rows in this range.
size() Returns the total number of locations in this range.
startCol() Returns the first column in this range, inclusive.
startLocation() Returns the first row/column location in this range, inclusive.
startRow() Returns the first row in this range, inclusive.
toString() Returns a printable string representation of this range.
Operators
ostream << range Outputs a range to the given output stream.
istream >> range Reads a range from the given input stream.

Constructor detail


GridLocationRange(int startRow, int startCol, int endRow, int endCol);
GridLocationRange(GridLocation startLocation, GridLocation endLocation);
Creates a GridLocationRange object with the specified boundaries. The range of locations extends from the corner at (startRow, startCol) to the corner at (endRow, endCol) inclusive.

Usage:

GridLocationRange row(0, 0, 0, 10);
GridLocationRange box(2, 3, 7, 9);
GridLocationRange byCorners(GridLocation(2, 3), GridLocation(7, 9));

GridLocationRange range;  // no arguments initializes to default 0,0,0,0
range = GridLocationRange(0, 0, 5, 5); // set range to have new boundaries

Method detail


bool contains(GridLocation loc) const;
Returns true if the specified location is contained in this range.

Usage:

if (range.contains(loc)) ...

bool isEmpty() const;
Returns true if this range contains no rows or columns.

Usage:

if (range.isEmpty()) ...

int endCol() const;
Returns the last column in this range.

Usage:

int last = range.endCol();

GridLocation endLocation() const;
Returns the last row/column location in this range.

Usage:

GridLocation last = range.endLocation();

int endRow() const;
Returns the last row in this range.

Usage:

int last = range.endRow();

int numCols() const;
Returns the count of columns in this range.

Usage:

int n = range.numCols();

int numRows() const;
Returns the count of rows in this range.

Usage:

int n = range.numRows();

int size() const;
Returns the total number of locations in this range.

Usage:

int n = range.size();

int startCol() const;
Returns the first column in this range.

Usage:

int first = range.startCol();

GridLocation startLocation() const;
Returns the first row/column location in this range.

Usage:

GridLocation first = range.startLocation();

int startRow() const;
Returns the first row in this range.

Usage:

int first = range.startRow();

string toString() const;
Returns a printable string for this GridLocationRange in the form "r5c8...r6c9".

Usage:

string str = range.toString();

ostream& operator<<(const GridLocationRange& range);
Outputs the GridLocationRange to the given output stream. The output is in the form r5c8...r6c9.

Usage:

cout << range << endl;

istream& operator>>(GridLocationRange& range);
Reads a GridLocationRange from the given input stream. The input is expected to be in the form r5c8...r6c9. If unable to read a proper range from the stream, the operation results in a stream fail state.

Usage:

if (infile >> range) ...