Skip to content

Commit

Permalink
Reduced hash collisions using code from @nhatdongdang
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshEliades committed Dec 4, 2024
1 parent 9a6135c commit 08a37b9
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,14 @@ Coordinate Coordinate::operator-(const Coordinate& obj) const {
}

std::size_t Coordinate::operator()(const mcpp::Coordinate& obj) const {
return (std::hash<int>()(obj.x) * 31) ^
((std::hash<int>()(obj.y) * 37) << 8) ^
((std::hash<int>()(obj.z) * 41) << 16);
int lower = -3e7, upper = 3e7;
size_t base = upper - lower + 1;

// Make x,y,z non negative
size_t nx = obj.x - lower, ny = obj.y - lower, nz = obj.z - lower;

// Use overflow instead of modding
return nx * base * base + ny * base + nz;
}

Coordinate Coordinate::clone() const {
Expand Down

0 comments on commit 08a37b9

Please sign in to comment.