From 69cf8351edb50cd0a7431a90c8f4a69fdaf57417 Mon Sep 17 00:00:00 2001 From: Quinniboi10 <69563433+Quinniboi10@users.noreply.github.com> Date: Wed, 1 Jul 2026 20:15:43 -0700 Subject: [PATCH] addition operators for GridLocation including associated docs and tests --- Docs/GridLocation.html | 40 ++++++++++++++++++++++++++++ Library/collections/gridlocation.cpp | 10 +++++++ Library/collections/gridlocation.h | 6 +++++ SPL-unit-tests/test-grid.cpp | 11 ++++++++ 4 files changed, 67 insertions(+) diff --git a/Docs/GridLocation.html b/Docs/GridLocation.html index cbfc7794..c795f391 100644 --- a/Docs/GridLocation.html +++ b/Docs/GridLocation.html @@ -39,6 +39,16 @@

struct GridLocation

toString() Returns a printable string representation of this GridLocation. Operators + + + loc1 + loc2  + Returns a new GridLocation with row and col added together. + + + + loc1 += loc2  + Add loc2.row and loc2.col to loc1. + loc1 == loc2  @@ -101,6 +111,36 @@

Method detail

string str = loc.toString(); +
+ +
+GridLocation operator+(const GridLocation& loc1, const GridLocation& loc2);
+
+Returns a new GridLocation where new.row = loc1.row + loc2.row and new.col = loc1.col + loc2.col. + +

Usage:
+ +

+GridLocation startLoc(5, 10);
+GridLocation offset(1, 1);
+GridLocation endLoc = startLoc + offset;
+
+ +
+ +
+GridLocation& operator+=(GridLocation& loc1, const GridLocation& loc2);
+
+Equal to loc1 = loc1 + loc2. Returns loc1 as a reference. + +

Usage:
+ +

+GridLocation pos(5, 10);
+GridLocation offset(1, 1);
+pos += offset;
+
+
diff --git a/Library/collections/gridlocation.cpp b/Library/collections/gridlocation.cpp
index 680fbf3d..7b117947 100644
--- a/Library/collections/gridlocation.cpp
+++ b/Library/collections/gridlocation.cpp
@@ -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);
diff --git a/Library/collections/gridlocation.h b/Library/collections/gridlocation.h
index 3b399031..e26143c7 100644
--- a/Library/collections/gridlocation.h
+++ b/Library/collections/gridlocation.h
@@ -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.
  */
diff --git a/SPL-unit-tests/test-grid.cpp b/SPL-unit-tests/test-grid.cpp
index b303335a..bd240156 100644
--- a/SPL-unit-tests/test-grid.cpp
+++ b/SPL-unit-tests/test-grid.cpp
@@ -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 grid;
     GridLocationRange range;