hash/hashtable.c

113 lines
3.1 KiB
C

#include <string.h>
#include <stdio.h>
#include "sdbm.h"
#include "hashtable.h"
bool hashtable_expand(hashtable* table) {
size_t oldcapacity = table->capacity;
size_t newcapacity = oldcapacity << 1;
hashtable_item* olditems = table->items;
hashtable_item* newitems = calloc(newcapacity, sizeof(hashtable_item));
if (newitems == NULL)
return false;
hashtable temptable;
temptable.capacity = newcapacity;
temptable.filled = 0;
temptable.items = newitems;
for (size_t i = 0; i < oldcapacity; i++) {
if (olditems[i].name == NULL || olditems[i].name == (void*)1)
continue;
size_t_optional index = hashtable_insert(&temptable, olditems[i].name);
if (!index.exists) {
hashtable_item* array = temptable.items;
for (size_t i = 0; i < temptable.capacity; i++)
free(array[i].name);
return false;
}
newitems[index.sizet].data = olditems[i].data;
}
table->capacity = newcapacity;
table->filled = temptable.filled;
table->items = newitems;
return true;
}
hashtable* hashtable_create(void) {
hashtable* table = malloc(sizeof(hashtable));
if (table == NULL)
return NULL;
table->capacity = 16;
table->items = calloc(16, sizeof(hashtable_item));
return table;
}
size_t_optional hashtable_key(hashtable* table, char* name) {
size_t index = sdbm(name) & (table->capacity - 1);
hashtable_item* array = table->items;
while (1) {
if (array[index].name == NULL)
return (size_t_optional) { false, 0 };
if (array[index].name != (void*)(1))
if (!strcmp(name, array[index].name))
return (size_t_optional) { true, index };
index = (index + 1) & (table->capacity - 1);
}
}
size_t_optional hashtable_insert(hashtable* table, char* name) {
size_t index = sdbm(name) & (table->capacity - 1);
hashtable_item* array = table->items;
if ((table->capacity >> 2) * 3 < table->filled) {
hashtable_expand(table);
}
while (1) {
if (array[index].name == NULL || array[index].name == (void*)1) {
array[index].name = malloc(strlen(name) + sizeof(name));
strcpy(array[index].name, name);
table->filled++;
return (size_t_optional) {true, index};
}
else
if (!strcmp(array[index].name, name))
return (size_t_optional) {true, index};
else
index = (index + 1) & (table->capacity - 1);
}
}
void hashtable_destroy(hashtable* table) {
hashtable_item* array = table->items;
for (size_t i = 0; i < table->capacity; i++)
free(array[i].name);
free(table);
}
void* hashtable_get(hashtable* table, char* name) {
size_t_optional index = hashtable_key(table, name);
if (!index.exists)
return NULL;
return table->items[index.sizet].data;
}
void hashtable_set(hashtable* table, char* name, void* data) {
size_t_optional index = hashtable_insert(table, name);
if (index.exists)
table->items[index.sizet].data = data;
}
void hashtable_delete(hashtable* table, char* name) {
size_t_optional index = hashtable_key(table, name);
if (index.exists) {
table->items[index.sizet].name = (void*)1;
table->filled--;
}
}
void hashtable_forall(hashtable* table, void (*callback)(char*, void*)) {
size_t capacity = table->capacity;
hashtable_item* items = table->items;
for (size_t i = 0; i < capacity; i++)
callback(items[i].name, items[i].data);
}