27 #ifndef _gridlocation_h 28 #define _gridlocation_h 36 class GridLocationRange;
45 GridLocation(
int row,
int col);
55 string toString()
const;
65 string to_string(
const GridLocation& value);
70 bool operator <(
const GridLocation& loc1,
const GridLocation& loc2);
71 bool operator <=(
const GridLocation& loc1,
const GridLocation& loc2);
72 bool operator ==(
const GridLocation& loc1,
const GridLocation& loc2);
73 bool operator !=(
const GridLocation& loc1,
const GridLocation& loc2);
74 bool operator >(
const GridLocation& loc1,
const GridLocation& loc2);
75 bool operator >=(
const GridLocation& loc1,
const GridLocation& loc2);
80 std::ostream& operator <<(std::ostream& out,
const GridLocation& loc);
81 std::istream& operator >>(std::istream& input, GridLocation& loc);
100 class GridLocationRange {
105 class GridLocationRangeIterator :
public std::iterator<std::input_iterator_tag, GridLocation> {
107 const GridLocationRange* glr;
111 GridLocationRangeIterator(
const GridLocationRange* glr,
bool end)
114 loc.row = glr->endRow() + 1;
115 loc.col = glr->endCol() + 1;
117 loc = glr->startLocation();
121 GridLocationRangeIterator(
const GridLocationRangeIterator& itr)
127 GridLocationRangeIterator& operator ++() {
128 if (glr->isRowMajor()) {
130 if (loc.col > glr->endCol()) {
131 loc.col = glr->startCol();
136 if (loc.row > glr->endRow()) {
137 loc.row = glr->startRow();
141 if (!glr->contains(loc)) {
142 loc.row = glr->endRow() + 1;
143 loc.col = glr->endCol() + 1;
148 GridLocationRangeIterator operator ++(
int) {
149 GridLocationRangeIterator copy(*
this);
154 GridLocationRangeIterator& operator --() {
155 if (glr->isRowMajor()) {
157 if (loc.col < glr->startCol()) {
158 loc.col = glr->endCol();
163 if (loc.row < glr->startRow()) {
164 loc.row = glr->endRow();
171 GridLocationRangeIterator operator --(
int) {
172 GridLocationRangeIterator copy(*
this);
177 bool operator ==(
const GridLocationRangeIterator& rhs)
const {
178 return loc == rhs.loc;
181 bool operator !=(
const GridLocationRangeIterator& rhs)
const {
182 return !(*
this == rhs);
185 bool operator <(
const GridLocationRangeIterator& rhs)
const {
186 if (glr != rhs.glr) {
187 throw std::runtime_error(
"GridLocationRange Iterator::operator <: Iterators are in different ranges");
189 return loc < rhs.loc;
192 bool operator <=(
const GridLocationRangeIterator& rhs)
const {
193 if (glr != rhs.glr) {
194 throw std::runtime_error(
"GridLocationRange Iterator::operator <=: Iterators are in different ranges");
196 return loc <= rhs.loc;
199 bool operator >(
const GridLocationRangeIterator& rhs)
const {
200 if (glr != rhs.glr) {
201 throw std::runtime_error(
"GridLocationRange Iterator::operator >: Iterators are in different ranges");
203 return loc > rhs.loc;
206 bool operator >=(
const GridLocationRangeIterator& rhs)
const {
207 if (glr != rhs.glr) {
208 throw std::runtime_error(
"GridLocationRange Iterator::operator >=: Iterators are in different ranges");
210 return loc >= rhs.loc;
213 const GridLocation& operator *()
const {
217 const GridLocation* operator ->()
const {
232 GridLocationRange(
int startRow = 0,
int startCol = 0,
int endRow = 0,
int endCol = 0,
bool isRowMajor =
true);
239 GridLocationRange(
const GridLocation& startLoc,
const GridLocation& endLoc,
bool isRowMajor =
true);
244 GridLocationRangeIterator begin()
const;
249 bool contains(
const GridLocation& loc)
const;
254 GridLocationRangeIterator end()
const;
264 const GridLocation& endLocation()
const;
274 bool isEmpty()
const;
280 bool isRowMajor()
const;
285 int startCol()
const;
290 const GridLocation& startLocation()
const;
295 int startRow()
const;
301 string toString()
const;
307 std::ostream& operator <<(std::ostream& out,
const GridLocationRange& range);
309 #endif // _gridlocation_h