void hash_table_free(HashTable *hash_table) { HashTableEntry *rover; HashTableEntry *next; unsigned long long int i; /* Free all entries in all chains */ for (i=0; i<hash_table->table_size; ++i) { rover = hash_table->table[i]; while (rover != NULL) { next = rover->next; hash_table_free_entry(hash_table, rover); rover = next; } } /* Free the table */ free(hash_table->table); /* Free the hash table structure */ free(hash_table); }
int hash_table_remove(HashTable *hash_table, HashTableKey key) { HashTableEntry **rover; HashTableEntry *entry; unsigned int index; int result; /* Generate the hash of the key and hence the index into the table */ index = hash_table->hash_func(key) % hash_table->table_size; /* Rover points at the pointer which points at the current entry * in the chain being inspected. ie. the entry in the table, or * the "next" pointer of the previous entry in the chain. This * allows us to unlink the entry when we find it. */ result = 0; rover = &hash_table->table[index]; while (*rover != NULL) { if (hash_table->equal_func(key, (*rover)->key) != 0) { /* This is the entry to remove */ entry = *rover; /* Unlink from the list */ *rover = entry->next; /* Destroy the entry structure */ hash_table_free_entry(hash_table, entry); /* Track count of entries */ --hash_table->entries; result = 1; break; } /* Advance to the next entry */ rover = &((*rover)->next); } return result; }
int hash_table_remove(HashTable *hash_table, HashTableKey key, bool resize) { HashTableEntry **rover; HashTableEntry *entry; unsigned long long int index; unsigned long long int result; /* If there are too few items in the table with respect to the table * size, the table is taking up too much space. * Shrink table to improve space efficiency. */ if (resize && (hash_table->entries * 8) / hash_table->table_size <= 0) { /* Table is less than 1/8 full */ if (!hash_table_resize(hash_table)) { /* Failed to enlarge the table */ return 0; } } /* Generate the hash of the key and hence the index into the table */ index = hash_table->hash_func(key) % hash_table->table_size; /* Rover points at the pointer which points at the current entry * in the chain being inspected. ie. the entry in the table, or * the "next" pointer of the previous entry in the chain. This * allows us to unlink the entry when we find it. */ result = 0; rover = &hash_table->table[index]; while (*rover != NULL) { if (hash_table->equal_func(key, (*rover)->key) != 0) { /* This is the entry to remove */ entry = *rover; /* Unlink from the list */ *rover = entry->next; /* Destroy the entry structure */ hash_table_free_entry(hash_table, entry); /* Track count of entries */ --hash_table->entries; result = 1; break; } /* Advance to the next entry */ rover = &((*rover)->next); } return result; }