Initial commit

This commit is contained in:
bʰedoh₂ swé 2024-06-29 03:37:51 +05:00
commit e172298e1e
5 changed files with 132 additions and 0 deletions

5
LICENSE Normal file
View File

@ -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.

88
hashtable.c Normal file
View File

@ -0,0 +1,88 @@
#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)
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;
}

30
hashtable.h Normal file
View File

@ -0,0 +1,30 @@
#include <stdlib.h>
#include <stdbool.h>
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);

8
sdbm.c Normal file
View File

@ -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;
}

1
sdbm.h Normal file
View File

@ -0,0 +1 @@
unsigned long sdbm(char *str);