diff --git a/include/mcpp/util.h b/include/mcpp/util.h index e503b43..191af2f 100644 --- a/include/mcpp/util.h +++ b/include/mcpp/util.h @@ -68,6 +68,15 @@ struct Coordinate { */ Coordinate operator-(const Coordinate& obj) const; + /** + * @brief Implements hash algorithm for Coordinate object using non-negative + * mapping and weighted coordinate values. + * + * @param obj The Coordinate object to hash. + * @return Hash of Coordinate object. + */ + std::size_t operator()(const Coordinate& obj) const; + /** * @brief Creates a copy of the Coordinate object. * diff --git a/src/util.cpp b/src/util.cpp index f52aa55..aa7c4e3 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -41,6 +41,20 @@ Coordinate Coordinate::operator-(const Coordinate& obj) const { return result; } +std::size_t Coordinate::operator()(const mcpp::Coordinate& obj) const { + // Minecraft coordinate bounds + int lower = -3e7, upper = 3e7; + size_t base = upper - lower + 1; + + // Convert coordinate attributes to non-negative values + size_t nx = obj.x - lower; + size_t ny = obj.y - lower; + size_t nz = obj.z - lower; + + // Combine and weight coordinate values using the boundary range + return nx * base * base + ny * base + nz; +} + Coordinate Coordinate::clone() const { return Coordinate(this->x, this->y, this->z); }