Skip to content

Commit

Permalink
hash: just keep a count rather than enumerating to calc it
Browse files Browse the repository at this point in the history
  • Loading branch information
elliefm committed Jul 30, 2021
1 parent d6a393c commit e92952e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
24 changes: 9 additions & 15 deletions lib/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ EXPORTED hash_table *construct_hash_table(hash_table *table, size_t size, int us
assert(size);

table->size = size;
table->count = 0;
table->seed = rand(); /* might be zero, that's okay */

/* Allocate the table -- different for using memory pools and not */
Expand Down Expand Up @@ -94,12 +95,13 @@ EXPORTED void *hash_insert(const char *key, void *data, hash_table *table)
}
(table->table)[val] -> next = NULL;
(table->table)[val] -> data = data;
table->count++;
return (table->table)[val] -> data;
}

/*
** This spot in the table is already in use. See if the current string
** has already been inserted, and if so, increment its count.
** has already been inserted, and if so, replace its data
*/
for (prev = &((table->table)[val]), ptr=(table->table)[val];
ptr;
Expand All @@ -125,6 +127,7 @@ EXPORTED void *hash_insert(const char *key, void *data, hash_table *table)
newptr->data = data;
newptr->next = ptr;
*prev = newptr;
table->count++;
return data;
}
}
Expand All @@ -143,10 +146,10 @@ EXPORTED void *hash_insert(const char *key, void *data, hash_table *table)
newptr->data = data;
newptr->next = NULL;
*prev = newptr;
table->count++;
return data;
}


/*
** Look up a key and return the associated data. Returns NULL if
** the key is not in the table.
Expand Down Expand Up @@ -227,6 +230,7 @@ EXPORTED void *hash_del(const char *key, hash_table *table)
free(ptr->key);
free(ptr);
}
table->count--;
return data;
}
if (cmpresult < 0) {
Expand Down Expand Up @@ -287,6 +291,7 @@ EXPORTED void free_hash_table(hash_table *table, void (*func)(void *))
}
table->table = NULL;
table->size = 0;
table->count = 0;
}

/*
Expand Down Expand Up @@ -335,19 +340,8 @@ EXPORTED strarray_t *hash_keys(hash_table *table)

EXPORTED int hash_numrecords(hash_table *table)
{
unsigned i;
bucket *temp;
int count = 0;

for (i = 0; i < table->size; i++) {
temp = (table->table)[i];
while (temp) {
count++;
temp = temp->next;
}
}

return count;
/* XXX macro or inline this if we keep the count field long term */
return table->count;
}

EXPORTED void hash_enumerate_sorted(hash_table *table, void (*func)(const char *, void *, void *),
Expand Down
3 changes: 2 additions & 1 deletion lib/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "mpool.h"
#include "strarray.h"

#define HASH_TABLE_INITIALIZER {0, 0, NULL, NULL}
#define HASH_TABLE_INITIALIZER {0, 0, 0, NULL, NULL}

/*
** A hash table consists of an array of these buckets. Each bucket
Expand All @@ -33,6 +33,7 @@ typedef struct bucket {

typedef struct hash_table {
size_t size;
size_t count;
uint32_t seed;
bucket **table;
struct mpool *pool;
Expand Down

0 comments on commit e92952e

Please sign in to comment.