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

>>> READ THIS ALL! IF YOU DON'T WANT TO WASTE HOURS <<< #51

Open
0xdeadc0de opened this issue May 23, 2024 · 0 comments
Open

>>> READ THIS ALL! IF YOU DON'T WANT TO WASTE HOURS <<< #51

0xdeadc0de opened this issue May 23, 2024 · 0 comments

Comments

@0xdeadc0de
Copy link

0xdeadc0de commented May 23, 2024

It's been discussed in several other issue threads. But folks like me doesn't pay attention. Perhaps this will get your attention.

There's a bug in double hash calculation.

Pay attention to the issue #35

There's a case where hash_b will return the (bucket_size - 1) and then the calculation will ignore attemptNumber because of modulus operator.

(hash_a + (attempt * (hash_b + 1))) % num_buckets;

is basically:

(hash_a + (attempt * num_buckets) % num_buckets;

Anything multiplies with num_buckets will get to 0 because of modulus. Therefore it will stuck in infinite loop to get you a negative index.

Fix:

static int ht_hash(const char* s, const int num_buckets, const int attempt) {
    const int hash_a = ht_generic_hash(s, 151, num_buckets);
    int hash_b = ht_generic_hash(s, 163, num_buckets);
	if (hash_b == num_buckets - 1)
	{
		hash_b = 1;
	}
    return (hash_a + (attempt * (hash_b + 1))) % num_buckets;
}

Another fix (possibly more mathematically correct):
Reference: https://en.wikipedia.org/wiki/Double_hashing

We should map the domain of h2 to {1, num_buckets-1}.
One solution could be:

static int ht_hash(const char* s, const int num_buckets, const int attempt) {
    const int hash_a = ht_generic_hash(s, 151, num_buckets);
    const int hash_b = ht_generic_hash(s, 163, num_buckets-1);
    return (hash_a + (attempt * (hash_b + 1))) % num_buckets;
}
@0xdeadc0de 0xdeadc0de changed the title READ THIS ALL! READ THIS ALL! IF YOU DON'T WANT TO WASTE HOURS May 23, 2024
@0xdeadc0de 0xdeadc0de changed the title READ THIS ALL! IF YOU DON'T WANT TO WASTE HOURS >>> READ THIS ALL! IF YOU DON'T WANT TO WASTE HOURS <<< May 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant