Util: Rebalance non-hashed Tables too

This commit is contained in:
Vicki Pfau 2022-05-28 19:03:27 -07:00
parent 21e2ccb025
commit c8978fd425
1 changed files with 18 additions and 0 deletions

View File

@ -161,6 +161,24 @@ void* TableLookup(const struct Table* table, uint32_t key) {
void TableInsert(struct Table* table, uint32_t key, void* value) {
struct TableList* list = _getList(table, key);
if (table->size >= table->tableSize * REBALANCE_THRESHOLD) {
struct Table newTable;
TableInit(&newTable, table->tableSize * REBALANCE_THRESHOLD, NULL);
memcpy(&newTable.fn, &table->fn, sizeof(newTable.fn));
size_t i;
for (i = 0; i < table->tableSize; ++i) {
struct TableList* list = &table->table[i];
size_t j;
for (j = 0; j < list->nEntries; ++j) {
TableInsert(&newTable, list->list[j].key, list->list[j].value);
}
free(list->list);
}
free(table->table);
table->tableSize = newTable.tableSize;
table->table = newTable.table;
list = _getList(table, key);
}
TABLE_LOOKUP_START(TABLE_COMPARATOR, list) {
if (value != lookupResult->value) {
if (table->fn.deinitializer) {