X-Git-Url: https://git.auder.net/?p=cgds.git;a=blobdiff_plain;f=src%2FHashTable.c;h=d54934bcc8416586ebfcb42b936485f6cde68f99;hp=9e818b07e3236c46933a49f25e95aef723e77bd0;hb=eed1b5d2fca21abae2540b500f4a4ed94a809403;hpb=e45132acdb58c076d5e06849fa51c26de9a7486d diff --git a/src/HashTable.c b/src/HashTable.c index 9e818b0..d54934b 100644 --- a/src/HashTable.c +++ b/src/HashTable.c @@ -16,7 +16,7 @@ void _hashtable_init(HashTable* hashTable, size_t dataSize, size_t hashSize) HashTable* _hashtable_new(size_t dataSize, size_t hashSize) { - HashTable* hashTable = (HashTable*) safe_malloc(sizeof (HashTable)); + HashTable* hashTable = (HashTable*) safe_malloc(sizeof(HashTable)); _hashtable_init(hashTable, dataSize, hashSize); return hashTable; } @@ -34,7 +34,7 @@ HashTable* hashtable_copy(HashTable* hashTable) while (cell != NULL) { // cellCopy == NULL (from empty list) - cellCopy = (HashCell*) safe_malloc(sizeof(HashCell*)); + cellCopy = (HashCell*) safe_malloc(sizeof(HashCell)); cellCopy->key = (char*) safe_malloc(strlen(cell->key) + 1); strcpy(cellCopy->key, cell->key); cellCopy->data = safe_malloc(hashTable->dataSize); @@ -65,8 +65,8 @@ UInt _compute_hash(char* key, size_t hashSize) UInt res = 0; for (unsigned char* s = key; *s != '\0'; s++) // NOTE: '31' from here https://stackoverflow.com/a/4384446 - res += *s + 31 * res; - return res % hashSize; + res = (*s + 31 * res) % hashSize; + return res; } void* _hashtable_get(HashTable* hashTable, char* key) @@ -75,7 +75,8 @@ void* _hashtable_get(HashTable* hashTable, char* key) HashCell* cell = hashTable->head[hashIdx]; while (cell != NULL) { - if (strcmp(cell->key, key) == 0) return cell->data; + if (strcmp(cell->key, key) == 0) + return cell->data; cell = cell->next; } return NULL;