Compare commits
No commits in common. "34f7eaea19070f1a141769b94b6ab61c4d1649ad" and "5f539552eeaa0703d0a462233ef8497e1df70ecf" have entirely different histories.
34f7eaea19
...
5f539552ee
22
hashtable.c
22
hashtable.c
@ -4,14 +4,15 @@
|
|||||||
#include "sdbm.h"
|
#include "sdbm.h"
|
||||||
#include "hashtable.h"
|
#include "hashtable.h"
|
||||||
|
|
||||||
bool hashtable_recreate(hashtable* table) {
|
bool hashtable_expand(hashtable* table) {
|
||||||
size_t oldcapacity = table->capacity;
|
size_t oldcapacity = table->capacity;
|
||||||
|
size_t newcapacity = oldcapacity << 1;
|
||||||
hashtable_item* olditems = table->items;
|
hashtable_item* olditems = table->items;
|
||||||
hashtable_item* newitems = calloc(16, sizeof(hashtable_item));
|
hashtable_item* newitems = calloc(newcapacity, sizeof(hashtable_item));
|
||||||
if (newitems == NULL)
|
if (newitems == NULL)
|
||||||
return false;
|
return false;
|
||||||
hashtable temptable;
|
hashtable temptable;
|
||||||
temptable.capacity = 16;
|
temptable.capacity = newcapacity;
|
||||||
temptable.filled = 0;
|
temptable.filled = 0;
|
||||||
temptable.items = newitems;
|
temptable.items = newitems;
|
||||||
for (size_t i = 0; i < oldcapacity; i++) {
|
for (size_t i = 0; i < oldcapacity; i++) {
|
||||||
@ -22,12 +23,11 @@ bool hashtable_recreate(hashtable* table) {
|
|||||||
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);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
newitems[index.sizet].data = olditems[i].data;
|
newitems[index.sizet].data = olditems[i].data;
|
||||||
}
|
}
|
||||||
table->capacity = temptable.capacity;
|
table->capacity = newcapacity;
|
||||||
table->filled = temptable.filled;
|
table->filled = temptable.filled;
|
||||||
table->items = newitems;
|
table->items = newitems;
|
||||||
return true;
|
return true;
|
||||||
@ -59,7 +59,7 @@ size_t_optional hashtable_insert(hashtable* table, char* name) {
|
|||||||
size_t index = sdbm(name) & (table->capacity - 1);
|
size_t 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_expand(table);
|
||||||
}
|
}
|
||||||
while (1) {
|
while (1) {
|
||||||
if (array[index].name == NULL || array[index].name == (void*)1) {
|
if (array[index].name == NULL || array[index].name == (void*)1) {
|
||||||
@ -96,19 +96,17 @@ void hashtable_set(hashtable* table, char* name, void* data) {
|
|||||||
table->items[index.sizet].data = data;
|
table->items[index.sizet].data = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool hashtable_delete(hashtable* table, char* name) {
|
void hashtable_delete(hashtable* table, char* name) {
|
||||||
size_t_optional index = hashtable_key(table, name);
|
size_t_optional index = hashtable_key(table, name);
|
||||||
if (index.exists) {
|
if (index.exists) {
|
||||||
table->items[index.sizet].name = (void*)1;
|
table->items[index.sizet].name = (void*)1;
|
||||||
table->filled--;
|
table->filled--;
|
||||||
return true;
|
}
|
||||||
} else
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void hashtable_forall(hashtable* table, void (*callback)(char*, void*, void*), void* userdata) {
|
void hashtable_forall(hashtable* table, void (*callback)(char*, void*)) {
|
||||||
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].data, userdata);
|
callback(items[i].name, items[i].data);
|
||||||
}
|
}
|
||||||
|
@ -17,8 +17,6 @@ typedef struct {
|
|||||||
hashtable_item* items;
|
hashtable_item* items;
|
||||||
} hashtable;
|
} hashtable;
|
||||||
|
|
||||||
bool hashtable_recreate(hashtable* table);
|
|
||||||
|
|
||||||
hashtable* hashtable_create(void);
|
hashtable* hashtable_create(void);
|
||||||
|
|
||||||
void hashtable_destroy(hashtable*);
|
void hashtable_destroy(hashtable*);
|
||||||
@ -31,6 +29,6 @@ void* hashtable_get(hashtable* table, char* name);
|
|||||||
|
|
||||||
void hashtable_set(hashtable* table, char* name, void* data);
|
void hashtable_set(hashtable* table, char* name, void* data);
|
||||||
|
|
||||||
bool hashtable_delete(hashtable*, char*);
|
void hashtable_delete(hashtable*, char*);
|
||||||
|
|
||||||
void hashtable_forall(hashtable*, void (*)(char*, void*, void*), void*);
|
void hashtable_forall(hashtable*, void (*)(char*, void*));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user