Skip to content

Commit

Permalink
Refactoring and use of inline where appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
fmela committed Mar 19, 2017
1 parent 7583dc5 commit 70e054d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
7 changes: 3 additions & 4 deletions src/hashtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,9 @@ hashtable_resize(hashtable* table, unsigned new_size)
memset(ntable, 0, new_size * sizeof(hash_node*));

for (unsigned i = 0; i < table->size; i++) {
hash_node* node = table->table[i];
while (node) {
hash_node* next = node->next;
unsigned mhash = node->hash % new_size;
for (hash_node* node = table->table[i]; node;) {
hash_node* const next = node->next;
const unsigned mhash = node->hash % new_size;

hash_node* search = ntable[mhash];
hash_node* prev = NULL;
Expand Down
12 changes: 6 additions & 6 deletions src/hashtable2.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ hashtable2_free(hashtable2* table, dict_delete_func delete_func)
return count;
}

static dict_insert_result
static inline dict_insert_result
insert(hashtable2* table, void *key, unsigned hash)
{
ASSERT(hash != 0);
Expand All @@ -180,7 +180,7 @@ insert(hashtable2* table, void *key, unsigned hash)
}

static inline unsigned
safe_hash(dict_hash_func hash_func, const void *key)
nonzero_hash(dict_hash_func hash_func, const void *key)
{
const unsigned hash = hash_func(key);
return hash ? hash : ~(unsigned)0;
Expand All @@ -197,7 +197,7 @@ hashtable2_insert(hashtable2* table, void* key)
/* No memory for a bigger table, but let the insert proceed anyway. */
}
}
const unsigned hash = safe_hash(table->hash_func, key);
const unsigned hash = nonzero_hash(table->hash_func, key);
dict_insert_result result = insert(table, key, hash);
if (result.inserted)
table->count++;
Expand All @@ -209,7 +209,7 @@ hashtable2_search(hashtable2* table, const void* key)
{
ASSERT(table != NULL);

const unsigned hash = safe_hash(table->hash_func, key);
const unsigned hash = nonzero_hash(table->hash_func, key);
hash_node* const first = table->table + (hash % table->size);
hash_node* const table_end = table->table + table->size;
hash_node* node = first;
Expand Down Expand Up @@ -276,7 +276,7 @@ hashtable2_remove(hashtable2* table, const void* key)
{
ASSERT(table != NULL);

const unsigned hash = safe_hash(table->hash_func, key);
const unsigned hash = nonzero_hash(table->hash_func, key);
hash_node* const first = table->table + (hash % table->size);
hash_node* const table_end = table->table + table->size;
hash_node* node = first;
Expand Down Expand Up @@ -572,7 +572,7 @@ hashtable2_itor_last(hashtable2_itor* itor)
bool
hashtable2_itor_search(hashtable2_itor* itor, const void* key)
{
const unsigned hash = safe_hash(itor->table->hash_func, key);
const unsigned hash = nonzero_hash(itor->table->hash_func, key);
const unsigned truncated_hash = hash % itor->table->size;
unsigned index = truncated_hash;
do {
Expand Down

0 comments on commit 70e054d

Please sign in to comment.