X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=src%2FHashTable.c;h=468de8139420139d40a9296895a71171f05a1f78;hb=HEAD;hp=9e818b07e3236c46933a49f25e95aef723e77bd0;hpb=e45132acdb58c076d5e06849fa51c26de9a7486d;p=cgds.git diff --git a/src/HashTable.c b/src/HashTable.c index 9e818b0..468de81 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; @@ -84,9 +85,8 @@ void* _hashtable_get(HashTable* hashTable, char* key) void _hashtable_set(HashTable* hashTable, char* key, void* data) { UInt hashIdx = _compute_hash(key, hashTable->hashSize); - HashCell - *cell = hashTable->head[hashIdx], - *prev = NULL; + HashCell *cell = hashTable->head[hashIdx], + *prev = NULL; while (cell != NULL) { if (strcmp(cell->key, key) == 0) @@ -115,9 +115,8 @@ void _hashtable_set(HashTable* hashTable, char* key, void* data) void hashtable_delete(HashTable* hashTable, char* key) { UInt hashIdx = _compute_hash(key, hashTable->hashSize); - HashCell - *cell = hashTable->head[hashIdx], - *prev = NULL; + HashCell *cell = hashTable->head[hashIdx], + *prev = NULL; while (cell != NULL) { if (strcmp(cell->key, key) == 0)