util/khash: Add hash_hash_string_part()

This commit is contained in:
Ingo Weinhold 2013-05-11 23:17:55 +02:00
parent 1e7416d9b3
commit 1848fdc329
2 changed files with 19 additions and 1 deletions

View File

@ -60,7 +60,8 @@ void hash_dump_table(struct hash_table* table);
* the key, returning 0 if equal, other if not
*/
uint32 hash_hash_string(const char *str);
uint32 hash_hash_string(const char *string);
uint32 hash_hash_string_part(const char *string, size_t maxLength);
#ifdef __cplusplus
}

View File

@ -398,6 +398,23 @@ hash_hash_string(const char *string)
}
uint32
hash_hash_string_part(const char *string, size_t maxLength)
{
uint32 hash = 0;
char c;
// we assume hash to be at least 32 bits
while (maxLength-- > 0 && (c = *string++) != 0) {
hash ^= hash >> 28;
hash <<= 4;
hash ^= c;
}
return hash;
}
uint32
hash_count_elements(struct hash_table *table)
{