improved hash code function

I improved the hash code function
This commit is contained in:
Christian Bender 2017-12-24 23:42:07 +01:00 committed by GitHub
parent df5d9d63c3
commit c26124199f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -43,6 +43,12 @@ int get_hash(char s[])
hash_code = s[counter] + (hash_code << 6) + (hash_code << 16) - hash_code;
}
/*
make sure hash_code is always positive.
because we want uses it as index
*/
hash_code = (hash_code < 0) ? hash_code * -1 : hash_code;
/* % modulo is for fitting the index in array. */
return hash_code % MAXELEMENTS;
}