-
Notifications
You must be signed in to change notification settings - Fork 326
Open
Description
The pseduo-code
hash = 0
string_len = length(string)
for i = 0, 1, ..., string_len:
hash += (a ** (string_len - (i+1))) * char_code(string[i])
hash = hash % num_buckets
return hash
takes the modulus of the hash value and the number of buckets once, immediately before returning it. The actual code
static int ht_hash(const char* s, const int a, const int m) {
long hash = 0;
const int len_s = strlen(s);
for (int i = 0; i < len_s; i++) {
hash += (long)pow(a, len_s - (i+1)) * s[i];
hash = hash % m;
}
return (int)hash;
}
takes the modulus of the hash value and the number of buckets for each letter in the input string.
Metadata
Metadata
Assignees
Labels
No labels