Skip to content
Snippets Groups Projects
Commit 36350a57 authored by Bruce Cowan's avatar Bruce Cowan
Browse files

Change hashtable to use arrays rather than linked lists

parent 35ef2eb4
No related branches found
No related tags found
No related merge requests found
Pipeline #2633 passed
#include "array.h"
#include "hashtable.h"
#include "mem.h"
......@@ -19,10 +20,20 @@ typedef struct
*/
struct _HashTable
{
SList **array;
size_t size;
Array **buckets;
size_t size;
};
static void
key_value_free (void *element)
{
KeyValue *kv = (KeyValue *) element;
free (kv->key);
free (kv->value);
free (kv);
}
/**
* hash_table_new:
* @size: Number of buckets to use.
......@@ -32,23 +43,23 @@ struct _HashTable
HashTable *
hash_table_new (size_t size)
{
HashTable *new = malloc (sizeof (HashTable));
new->array = calloc (size, sizeof (SList *));
new->size = size;
HashTable *new = malloc (sizeof (HashTable));
new->buckets = calloc (size, sizeof (Array *));
new->size = size;
return new;
return new;
}
static unsigned
strhash (const char *string)
{
unsigned h = 5381;
char c;
unsigned h = 5381;
char c;
while ((c = *string++))
h = (h << 5) + h + c;
while ((c = *string++))
h = (h << 5) + h + c;
return h;
return h;
}
/**
......@@ -66,14 +77,17 @@ hash_table_insert (HashTable *table,
const char *key,
const char *value)
{
unsigned index = strhash (key) % table->size;
unsigned index = strhash (key) % table->size;
KeyValue *kv = check_malloc (sizeof (KeyValue));
kv->key = strdup (key);
kv->value = strdup (value);
SList *list = slist_prepend (table->array[index], kv);
table->array[index] = list;
// Only create array if needed
if (!table->buckets[index])
table->buckets[index] = array_new (key_value_free);
array_add (table->buckets[index], kv);
}
/**
......@@ -86,16 +100,19 @@ hash_table_insert (HashTable *table,
void
hash_table_print_all (HashTable *table)
{
for (size_t i = 0; i < table->size; i++)
{
SList *bucket = table->array[i];
if (bucket == NULL)
continue;
for (SList *list = bucket; list; list = list->next)
{
KeyValue *kv = list->data;
printf ("%s:%s\n", kv->key, kv->value);
}
}
for (size_t i = 0; i < table->size; i++)
{
Array *bucket = table->buckets[i];
if (!bucket)
continue;
size_t length = array_get_length (bucket);
for (size_t j = 0; j < length; j++)
{
KeyValue *kv = (KeyValue *) array_get (bucket, j);
printf ("%s:%s\n", kv->key, kv->value);
}
}
}
......@@ -2,8 +2,6 @@
#include <stddef.h>
#include "slist.h"
typedef struct _HashTable HashTable;
HashTable * hash_table_new (size_t size);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment