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

Conversation

JoshEliades
Copy link

Added hash algorithm for Coordinate object using XOR, bit shifts and prime multiplication

@nhatdongdang
Copy link
Contributor

I think the hash collisions are still high. You can refer to this code
image

#include <bits/stdc++.h>

std::size_t getHash1(int x, int y, int z)
{
    return (std::hash<int>()(x) * 31) ^
           ((std::hash<int>()(y) * 37) << 8) ^
           ((std::hash<int>()(z) * 41) << 16);
}

std::size_t getHash2(int x, int y, int z)
{
    // Minecraft coordinate bounds
    int lower = -3e7, upper = 3e7;
    size_t base = upper - lower + 1;

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

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

int main()
{

    std::set<std::size_t> visited1, visited2;

    int startX = 0, endX = 1000;
    int startY = 0, endY = 20;
    int startZ = 0, endZ = 1000;

    for (int x = startX; x <= endX; x++)
    {
        for (int y = startY; y <= endY; y++)
        {
            for (int z = startZ; z <= endZ; z++)
            {
                visited1.insert(getHash1(x, y, z));
                visited2.insert(getHash2(x, y, z));
            }
        }
    }

    int expectedSize = (endX - startX + 1) * (endY - startY + 1) * (endZ - startZ + 1);
    std::cout << "getHash1 collisions: " << expectedSize - (int)visited1.size() << "\n";
    std::cout << "getHash2 collisions: " << expectedSize - (int)visited2.size() << "\n";
}

@JoshEliades
Copy link
Author

Looks good to me, lets go with that then

@rozukke rozukke added enhancement New feature or request next-release To be released next time breaking changes are allowed labels Oct 30, 2024
@rozukke
Copy link
Owner

rozukke commented Oct 30, 2024

Would be great to get a solution with minimal collisions. Additionally, some sort of testing would be nice to make sure this compiles and runs on all systems. In any case, this is delayed until next release.

@rozukke rozukke marked this pull request as draft October 30, 2024 01:39
@rozukke
Copy link
Owner

rozukke commented Dec 5, 2024

@JoshEliades are you willing to write some tests for this implementation? If not maybe @nhatdongdang can do it

@JoshEliades
Copy link
Author

I can write some tests for the implementation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request next-release To be released next time breaking changes are allowed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants