Skip to content

Commit 0370a55

Browse files
committed
feat: hash Coordinate2D
1 parent f12d254 commit 0370a55

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

include/mcpp/coordinate.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,15 @@ struct Coordinate2D {
176176
*/
177177
Coordinate2D operator-(const Coordinate2D& obj) const;
178178

179+
/**
180+
* @brief Implements hash algorithm for Coordinate2D object using non-negative
181+
* mapping and weighted coordinate values.
182+
*
183+
* @param obj The Coordinate2D object to hash.
184+
* @return Hash of Coordinate2D object.
185+
*/
186+
std::size_t operator()(const Coordinate2D& obj) const;
187+
179188
/**
180189
* @brief Outputs the Coordinate2D object to an ostream.
181190
*

src/coordinate.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ Coordinate2D Coordinate2D::operator-(const Coordinate2D& obj) const {
7979
return result;
8080
}
8181

82+
std::size_t Coordinate2D::operator()(const mcpp::Coordinate2D& obj) const {
83+
int lower = -3e7, upper = 3e7;
84+
size_t base = upper - lower + 1;
85+
86+
size_t nx = obj.x - lower;
87+
size_t nz = obj.z - lower;
88+
89+
return nx * base + nz;
90+
}
91+
8292
std::string to_string(const Coordinate2D& coord) {
8393
using std::to_string;
8494
return "(" + to_string(coord.x) + "," + to_string(coord.z) + ")";

0 commit comments

Comments
 (0)