Give the struct a better name
This commit is contained in:
parent
3ea9100d78
commit
ee3356b7e0
38
hashtable.c
38
hashtable.c
@ -17,13 +17,13 @@ bool streq(char* str1, size_t len1, char* str2, size_t len2) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool hashtable_recreate(hashtable* table) {
|
bool hashtable_recreate(HashTable* table) {
|
||||||
size_t oldcapacity = table->capacity;
|
size_t oldcapacity = table->capacity;
|
||||||
hashtable_item* olditems = table->items;
|
HashTable_item* olditems = table->items;
|
||||||
hashtable_item* newitems = calloc(16, sizeof(hashtable_item));
|
HashTable_item* newitems = calloc(16, sizeof(HashTable_item));
|
||||||
if (newitems == NULL)
|
if (newitems == NULL)
|
||||||
return false;
|
return false;
|
||||||
hashtable temptable;
|
HashTable temptable;
|
||||||
temptable.capacity = 16;
|
temptable.capacity = 16;
|
||||||
temptable.filled = 0;
|
temptable.filled = 0;
|
||||||
temptable.items = newitems;
|
temptable.items = newitems;
|
||||||
@ -32,7 +32,7 @@ bool hashtable_recreate(hashtable* table) {
|
|||||||
continue;
|
continue;
|
||||||
size_t_optional index = hashtable_insert(&temptable, olditems[i].name, olditems[i].size);
|
size_t_optional index = hashtable_insert(&temptable, olditems[i].name, olditems[i].size);
|
||||||
if (!index.exists) {
|
if (!index.exists) {
|
||||||
hashtable_item* array = temptable.items;
|
HashTable_item* array = temptable.items;
|
||||||
for (size_t i = 0; i < temptable.capacity; i++)
|
for (size_t i = 0; i < temptable.capacity; i++)
|
||||||
free(array[i].name);
|
free(array[i].name);
|
||||||
free(newitems);
|
free(newitems);
|
||||||
@ -46,22 +46,22 @@ bool hashtable_recreate(hashtable* table) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
hashtable* hashtable_create(void) {
|
HashTable* hashtable_create(void) {
|
||||||
hashtable* table = malloc(sizeof(hashtable));
|
HashTable* table = malloc(sizeof(HashTable));
|
||||||
if (table == NULL)
|
if (table == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
table->capacity = 16;
|
table->capacity = 16;
|
||||||
table->items = calloc(16, sizeof(hashtable_item));
|
table->items = calloc(16, sizeof(HashTable_item));
|
||||||
return table;
|
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;
|
size_t index;
|
||||||
if (size)
|
if (size)
|
||||||
index = sdbm_clean(name, size) & (table->capacity - 1);
|
index = sdbm_clean(name, size) & (table->capacity - 1);
|
||||||
else
|
else
|
||||||
index = sdbm(name) & (table->capacity - 1);
|
index = sdbm(name) & (table->capacity - 1);
|
||||||
hashtable_item* array = table->items;
|
HashTable_item* array = table->items;
|
||||||
while (1) {
|
while (1) {
|
||||||
if (array[index].name == NULL)
|
if (array[index].name == NULL)
|
||||||
return (size_t_optional) { false, 0 };
|
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;
|
size_t index;
|
||||||
if (size)
|
if (size)
|
||||||
index = sdbm_clean(name, size) & (table->capacity - 1);
|
index = sdbm_clean(name, size) & (table->capacity - 1);
|
||||||
else
|
else
|
||||||
index = sdbm(name) & (table->capacity - 1);
|
index = sdbm(name) & (table->capacity - 1);
|
||||||
hashtable_item* array = table->items;
|
HashTable_item* array = table->items;
|
||||||
if ((table->capacity >> 2) * 3 < table->filled) {
|
if ((table->capacity >> 2) * 3 < table->filled) {
|
||||||
hashtable_recreate(table);
|
hashtable_recreate(table);
|
||||||
}
|
}
|
||||||
@ -101,27 +101,27 @@ size_t_optional hashtable_insert(hashtable* table, char* name, size_t size) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void hashtable_destroy(hashtable* table) {
|
void hashtable_destroy(HashTable* table) {
|
||||||
hashtable_item* array = table->items;
|
HashTable_item* array = table->items;
|
||||||
for (size_t i = 0; i < table->capacity; i++)
|
for (size_t i = 0; i < table->capacity; i++)
|
||||||
free(array[i].name);
|
free(array[i].name);
|
||||||
free(table);
|
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);
|
size_t_optional index = hashtable_key(table, name, size);
|
||||||
if (!index.exists)
|
if (!index.exists)
|
||||||
return NULL;
|
return NULL;
|
||||||
return table->items[index.sizet].data;
|
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);
|
size_t_optional index = hashtable_insert(table, name, size);
|
||||||
if (index.exists)
|
if (index.exists)
|
||||||
table->items[index.sizet].data = data;
|
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);
|
size_t_optional index = hashtable_key(table, name, size);
|
||||||
if (index.exists) {
|
if (index.exists) {
|
||||||
table->items[index.sizet].name = (void*)1;
|
table->items[index.sizet].name = (void*)1;
|
||||||
@ -131,9 +131,9 @@ bool hashtable_delete(hashtable* table, char* name, size_t size) {
|
|||||||
return false;
|
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;
|
size_t capacity = table->capacity;
|
||||||
hashtable_item* items = table->items;
|
HashTable_item* items = table->items;
|
||||||
for (size_t i = 0; i < capacity; i++)
|
for (size_t i = 0; i < capacity; i++)
|
||||||
callback(items[i].name, items[i].size, items[i].data, userdata);
|
callback(items[i].name, items[i].size, items[i].data, userdata);
|
||||||
}
|
}
|
||||||
|
24
hashtable.h
24
hashtable.h
@ -13,30 +13,30 @@ typedef struct {
|
|||||||
size_t size;
|
size_t size;
|
||||||
char* name;
|
char* name;
|
||||||
void* data;
|
void* data;
|
||||||
} hashtable_item;
|
} HashTable_item;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
size_t capacity;
|
size_t capacity;
|
||||||
size_t filled;
|
size_t filled;
|
||||||
hashtable_item* items;
|
HashTable_item* items;
|
||||||
} hashtable;
|
} 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
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user