X-Git-Url: https://git.auder.net/?p=cgds.git;a=blobdiff_plain;f=src%2FHashTable.c;fp=src%2FHashTable.c;h=9e818b07e3236c46933a49f25e95aef723e77bd0;hp=7bd310cc042917e69b0fb62136081e35ff74613c;hb=e45132acdb58c076d5e06849fa51c26de9a7486d;hpb=1ff641f9960fa6c6081817a5641afb22fad91dcd diff --git a/src/HashTable.c b/src/HashTable.c index 7bd310c..9e818b0 100644 --- a/src/HashTable.c +++ b/src/HashTable.c @@ -6,28 +6,28 @@ void _hashtable_init(HashTable* hashTable, size_t dataSize, size_t hashSize) { - hashTable->hashSize = hashSize; + hashTable->hashSize = hashSize; hashTable->dataSize = dataSize; - hashTable->head = safe_malloc(hashSize * sizeof(HashCell*)); - for (UInt i = 0; i < hashSize; i++) + hashTable->head = safe_malloc(hashSize * sizeof(HashCell*)); + for (UInt i = 0; i < hashSize; i++) hashTable->head[i] = NULL; hashTable->size = 0; } HashTable* _hashtable_new(size_t dataSize, size_t hashSize) { - HashTable* hashTable = (HashTable*) safe_malloc(sizeof (HashTable)); - _hashtable_init(hashTable, dataSize, hashSize); - return hashTable; + HashTable* hashTable = (HashTable*) safe_malloc(sizeof (HashTable)); + _hashtable_init(hashTable, dataSize, hashSize); + return hashTable; } HashTable* hashtable_copy(HashTable* hashTable) { - HashTable* hashTableCopy = + HashTable* hashTableCopy = _hashtable_new(hashTable->dataSize, hashTable->hashSize); - hashTableCopy->size = hashTable->size; + hashTableCopy->size = hashTable->size; for (UInt i = 0; i < hashTable->hashSize; i++) - { + { HashCell *cell = hashTable->head[i], *cellCopy = hashTableCopy->head[i], *prev = NULL; @@ -35,28 +35,28 @@ HashTable* hashtable_copy(HashTable* hashTable) { // cellCopy == NULL (from empty list) cellCopy = (HashCell*) safe_malloc(sizeof(HashCell*)); - cellCopy->key = (char*) safe_malloc(strlen(cell->key) + 1); + cellCopy->key = (char*) safe_malloc(strlen(cell->key) + 1); strcpy(cellCopy->key, cell->key); - cellCopy->data = safe_malloc(hashTable->dataSize); - memcpy(cellCopy->data, cell->data, hashTable->dataSize); + cellCopy->data = safe_malloc(hashTable->dataSize); + memcpy(cellCopy->data, cell->data, hashTable->dataSize); if (prev == NULL) hashTableCopy->head[i] = cellCopy; else prev->next = cellCopy; prev = cellCopy; cell = cell->next; } if (cellCopy != NULL) cellCopy->next = NULL; - } - return hashTableCopy; + } + return hashTableCopy; } bool hashtable_empty(HashTable* hashTable) { - return (hashTable->size == 0); + return (hashTable->size == 0); } UInt hashtable_size(HashTable* hashTable) { - return hashTable->size; + return hashTable->size; } // Function (string) key --> (integer) hash [internal usage] @@ -139,7 +139,7 @@ void hashtable_delete(HashTable* hashTable, char* key) void hashtable_clear(HashTable* hashTable) { - for (UInt i = 0; i < hashTable->hashSize; i++) + for (UInt i = 0; i < hashTable->hashSize; i++) { HashCell* cell = hashTable->head[i]; while (cell != NULL) @@ -152,12 +152,12 @@ void hashtable_clear(HashTable* hashTable) } hashTable->head[i] = NULL; } - hashTable->size = 0; + hashTable->size = 0; } void hashtable_destroy(HashTable* hashTable) { - hashtable_clear(hashTable); - safe_free(hashTable->head); + hashtable_clear(hashTable); + safe_free(hashTable->head); safe_free(hashTable); }