Give the struct a better name

This commit is contained in:
bʰedoh₂ swé 2024-08-17 00:27:19 +05:00
parent 3ea9100d78
commit ee3356b7e0
2 changed files with 31 additions and 31 deletions

View File

@ -17,13 +17,13 @@ bool streq(char* str1, size_t len1, char* str2, size_t len2) {
return true;
}
bool hashtable_recreate(hashtable* table) {
bool hashtable_recreate(HashTable* table) {
size_t oldcapacity = table->capacity;
hashtable_item* olditems = table->items;
hashtable_item* newitems = calloc(16, sizeof(hashtable_item));
HashTable_item* olditems = table->items;
HashTable_item* newitems = calloc(16, sizeof(HashTable_item));
if (newitems == NULL)
return false;
hashtable temptable;
HashTable temptable;
temptable.capacity = 16;
temptable.filled = 0;
temptable.items = newitems;
@ -32,7 +32,7 @@ bool hashtable_recreate(hashtable* table) {
continue;
size_t_optional index = hashtable_insert(&temptable, olditems[i].name, olditems[i].size);
if (!index.exists) {
hashtable_item* array = temptable.items;
HashTable_item* array = temptable.items;
for (size_t i = 0; i < temptable.capacity; i++)
free(array[i].name);
free(newitems);
@ -46,22 +46,22 @@ bool hashtable_recreate(hashtable* table) {
return true;
}
hashtable* hashtable_create(void) {
hashtable* table = malloc(sizeof(hashtable));
HashTable* hashtable_create(void) {
HashTable* table = malloc(sizeof(HashTable));
if (table == NULL)
return NULL;
table->capacity = 16;
table->items = calloc(16, sizeof(hashtable_item));
table->items = calloc(16, sizeof(HashTable_item));
return table;
}
size_t_optional hashtable_key(hashtable* table, char* name, size_t size) {
size_t_optional hashtable_key(HashTable* table, char* name, size_t size) {
size_t index;
if (size)
index = sdbm_clean(name, size) & (table->capacity - 1);
else
index = sdbm(name) & (table->capacity - 1);
hashtable_item* array = table->items;
HashTable_item* array = table->items;
while (1) {
if (array[index].name == NULL)
return (size_t_optional) { false, 0 };
@ -72,13 +72,13 @@ size_t_optional hashtable_key(hashtable* table, char* name, size_t size) {
}
}
size_t_optional hashtable_insert(hashtable* table, char* name, size_t size) {
size_t_optional hashtable_insert(HashTable* table, char* name, size_t size) {
size_t index;
if (size)
index = sdbm_clean(name, size) & (table->capacity - 1);
else
index = sdbm(name) & (table->capacity - 1);
hashtable_item* array = table->items;
HashTable_item* array = table->items;
if ((table->capacity >> 2) * 3 < table->filled) {
hashtable_recreate(table);
}
@ -101,27 +101,27 @@ size_t_optional hashtable_insert(hashtable* table, char* name, size_t size) {
}
}
void hashtable_destroy(hashtable* table) {
hashtable_item* array = table->items;
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 size) {
void* hashtable_get(HashTable* table, char* name, size_t size) {
size_t_optional index = hashtable_key(table, name, size);
if (!index.exists)
return NULL;
return table->items[index.sizet].data;
}
void hashtable_set(hashtable* table, char* name, size_t size, void* data) {
void hashtable_set(HashTable* table, char* name, size_t size, void* data) {
size_t_optional index = hashtable_insert(table, name, size);
if (index.exists)
table->items[index.sizet].data = data;
}
bool hashtable_delete(hashtable* table, char* name, size_t size) {
bool hashtable_delete(HashTable* table, char* name, size_t size) {
size_t_optional index = hashtable_key(table, name, size);
if (index.exists) {
table->items[index.sizet].name = (void*)1;
@ -131,9 +131,9 @@ bool hashtable_delete(hashtable* table, char* name, size_t size) {
return false;
}
void hashtable_forall(hashtable* table, void (*callback)(char*, size_t, void*, void*), void* userdata) {
void hashtable_forall(HashTable* table, void (*callback)(char*, size_t, void*, void*), void* userdata) {
size_t capacity = table->capacity;
hashtable_item* items = table->items;
HashTable_item* items = table->items;
for (size_t i = 0; i < capacity; i++)
callback(items[i].name, items[i].size, items[i].data, userdata);
}

View File

@ -13,30 +13,30 @@ typedef struct {
size_t size;
char* name;
void* data;
} hashtable_item;
} HashTable_item;
typedef struct {
size_t capacity;
size_t filled;
hashtable_item* items;
} hashtable;
HashTable_item* items;
} HashTable;
bool hashtable_recreate(hashtable* table);
bool hashtable_recreate(HashTable* table);
hashtable* hashtable_create(void);
HashTable* hashtable_create(void);
void hashtable_destroy(hashtable*);
void hashtable_destroy(HashTable*);
size_t_optional hashtable_key(hashtable*, char*, size_t);
size_t_optional hashtable_key(HashTable*, char*, size_t);
size_t_optional hashtable_insert(hashtable*, char*, size_t);
size_t_optional hashtable_insert(HashTable*, char*, size_t);
void* hashtable_get(hashtable* table, char* name, size_t);
void* hashtable_get(HashTable* table, char* name, size_t);
void hashtable_set(hashtable* table, char* name, size_t, void* data);
void hashtable_set(HashTable* table, char* name, size_t, void* data);
bool hashtable_delete(hashtable*, char*, size_t);
bool hashtable_delete(HashTable*, char*, size_t);
void hashtable_forall(hashtable*, void (*)(char*, size_t, void*, void*), void*);
void hashtable_forall(HashTable*, void (*)(char*, size_t, void*, void*), void*);
#endif