Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions Docs/GridLocation.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ <h1 class=header><code>struct GridLocation</code></h1>
<td class=indexKey><nobr><a href="#Method:toString">toString()</a>&nbsp;</nobr></td><td class=indexSynopsis width=100%>Returns a printable string representation of this <code>GridLocation</code>.</td></tr>
<td class=indexHead colspan=3>Operators</td>
</tr>
<tr>

<td class=indexKey><nobr><a href="#Operator:+"><var>loc1</var> + <var>loc2</var></a>&nbsp;</nobr></td>
<td class=indexSynopsis width=100%>Returns a new <code>GridLocation</code> with row and col added together.</td>
</tr>
<tr>

<td class=indexKey><nobr><a href="#Operator:+="><var>loc1</var> += <var>loc2</var></a>&nbsp;</nobr></td>
<td class=indexSynopsis width=100%>Add <code>loc2.row</code> and <code>loc2.col</code> to <code>loc1</code>.</td>
</tr>
<tr>

<td class=indexKey><nobr><a href="#Operator:=="><var>loc1</var> == <var>loc2</var></a>&nbsp;</nobr></td>
Expand Down Expand Up @@ -101,6 +111,36 @@ <h2>Method detail</h2>
string str = loc.toString();
</pre>

<hr>
<a name="Operator:+"></a>
<pre class=detailCode>
GridLocation operator+(const GridLocation&amp; loc1, const GridLocation&amp; loc2);
</pre>
Returns a new <code>GridLocation</code> where <code>new.row = loc1.row + loc2.row</code> and <code>new.col = loc1.col + loc2.col</code>.

<p>Usage:<br>
</div>
<pre class=usageCode>
GridLocation startLoc(5, 10);
GridLocation offset(1, 1);
GridLocation endLoc = startLoc + offset;
</pre>

<hr>
<a name="Operator:+="></a>
<pre class=detailCode>
GridLocation&amp; operator+=(GridLocation&amp; loc1, const GridLocation&amp; loc2);
</pre>
Equal to <code>loc1 = loc1 + loc2</code>. Returns <code>loc1</code> as a reference.

<p>Usage:<br>
</div>
<pre class=usageCode>
GridLocation pos(5, 10);
GridLocation offset(1, 1);
pos += offset;
</pre>

<hr>
<a name="Operator:<<"></a>
<pre class=detailCode>
Expand Down
10 changes: 10 additions & 0 deletions Library/collections/gridlocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ int hashCode(const GridLocation& loc) {
return hashCode(loc.row, loc.col);
}

GridLocation operator +(const GridLocation& loc1, const GridLocation& loc2) {
return GridLocation(loc1.row + loc2.row, loc1.col + loc2.col);
}

GridLocation& operator +=(GridLocation& loc1, const GridLocation& loc2) {
loc1.row += loc2.row;
loc1.col += loc2.col;
return loc1;
}

bool operator <(const GridLocation& loc1, const GridLocation& loc2) {
return loc1.row < loc2.row ||
(loc1.row == loc2.row && loc1.col < loc2.col);
Expand Down
6 changes: 6 additions & 0 deletions Library/collections/gridlocation.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ struct GridLocation {
*/
int hashCode(const GridLocation& loc);

/*
* Addition operators for grid locations.
*/
GridLocation operator +(const GridLocation& loc1, const GridLocation& loc2);
GridLocation& operator +=(GridLocation& loc1, const GridLocation& loc2);

/*
* Relational operators for comparing grid locations.
*/
Expand Down
11 changes: 11 additions & 0 deletions SPL-unit-tests/test-grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,17 @@ PROVIDED_TEST("GridLocationRange, contains") {
}


PROVIDED_TEST("GridLocation, offset math and behavior") {
GridLocation start(5, 10);
GridLocation offset(1, -8);
EXPECT_EQUAL(start + offset, GridLocation(6, 2));
GridLocation& startReference = start += offset;
EXPECT_EQUAL(start, GridLocation(6, 2));
startReference = GridLocation(0, 0);
EXPECT_EQUAL(start, GridLocation(0, 0));
}


PROVIDED_TEST("GridLocationRange dimensions,size,isEmpty ") {
Grid<int> grid;
GridLocationRange range;
Expand Down