Skip to content

Commit

Permalink
Add hashing for coordinate class
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshEliades committed Oct 29, 2024
1 parent bc94f6b commit 102c2a9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
8 changes: 8 additions & 0 deletions include/mcpp/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ struct Coordinate {
*/
Coordinate operator-(const Coordinate& obj) const;

/**
* @brief Implements hash algorithm for Coordinate object using XOR.
*
* @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
5 changes: 5 additions & 0 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ Coordinate Coordinate::operator-(const Coordinate& obj) const {
return result;
}

std::size_t Coordinate::operator()(const Coordinate& obj) const {
return std::hash<int>()(obj.x) ^ std::hash<int>()(obj.y) ^
std::hash<int>()(obj.z);
}

Coordinate Coordinate::clone() const {
return Coordinate(this->x, this->y, this->z);
}
Expand Down

0 comments on commit 102c2a9

Please sign in to comment.