From e172298e1e5e37b60015b8d028ce768a5abc28dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?b=CA=B0edoh=E2=82=82=20sw=C3=A9?= Date: Sat, 29 Jun 2024 03:37:51 +0500 Subject: [PATCH] Initial commit --- LICENSE | 5 +++ hashtable.c | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++ hashtable.h | 30 ++++++++++++++++++ sdbm.c | 8 +++++ sdbm.h | 1 + 5 files changed, 132 insertions(+) create mode 100644 LICENSE create mode 100644 hashtable.c create mode 100644 hashtable.h create mode 100644 sdbm.c create mode 100644 sdbm.h diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..50bb52d --- /dev/null +++ b/LICENSE @@ -0,0 +1,5 @@ +Copyright (C) 2024 by bedohswe bedohswe@firemail.cc + +Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/hashtable.c b/hashtable.c new file mode 100644 index 0000000..ba4356d --- /dev/null +++ b/hashtable.c @@ -0,0 +1,88 @@ +#include +#include + +#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) + continue; + size_t index = hashtable_insert(&temptable, olditems[i].name); + newitems[index].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)); + 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 (!strcmp(name, array[index].name)) + return (size_t_optional) { true, index }; + else + index = (index + 1) & (table->capacity - 1); + } +} + +size_t 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 = malloc(strlen(name) + sizeof(name)); + strcpy(array[index].name, name); + table->filled++; + return index; + } + else + if (!strcmp(array[index].name, name)) + return 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 index = hashtable_insert(table, name); + table->items[index].data = data; +} diff --git a/hashtable.h b/hashtable.h new file mode 100644 index 0000000..0c4c575 --- /dev/null +++ b/hashtable.h @@ -0,0 +1,30 @@ +#include +#include + +typedef struct { + bool exists; + size_t sizet; +} size_t_optional; + +typedef struct { + char* name; + void* data; +} hashtable_item; + +typedef struct { + size_t capacity; + size_t filled; + hashtable_item* items; +} hashtable; + +hashtable* hashtable_create(void); + +void hashtable_destroy(hashtable*); + +size_t_optional hashtable_key(hashtable*, char*); + +size_t hashtable_insert(hashtable*, char*); + +void* hashtable_get(hashtable* table, char* name); + +void hashtable_set(hashtable* table, char* name, void* data); diff --git a/sdbm.c b/sdbm.c new file mode 100644 index 0000000..3715a0f --- /dev/null +++ b/sdbm.c @@ -0,0 +1,8 @@ +unsigned long sdbm(char *str) { + unsigned long hash = 0; + + for (int c = *str; c != 0; c = *str++) + hash = c + (hash << 6) + (hash << 16) - hash; + + return hash; +} diff --git a/sdbm.h b/sdbm.h new file mode 100644 index 0000000..bfe3187 --- /dev/null +++ b/sdbm.h @@ -0,0 +1 @@ +unsigned long sdbm(char *str);