Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hashing for coordinate class #102

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
9 changes: 9 additions & 0 deletions include/mcpp/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
14 changes: 14 additions & 0 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Loading