fix: Fixes in hasntable

This commit is contained in:
2026-06-13 10:38:30 +02:00
parent 827f30908a
commit 497f06e20d
+9 -5
View File
@@ -99,7 +99,7 @@ void *hashtable_set(hashtable h, const char *key, void *value)
} }
// found replace the value // found replace the value
void *old_value = item.found->value; void *old_value = item.found->value;
item.found-> value = value; item.found->value = value;
return old_value; return old_value;
} }
@@ -137,19 +137,20 @@ void hashtable_destroy_bucket(hashtable h, int i)
{ {
struct entry *e = h->table[i]; struct entry *e = h->table[i];
while (e != NULL) { while (e != NULL) {
struct entry *next = e->next;
free((void*)e->key); free((void*)e->key);
e = e->next;
free(e); free(e);
e = next;
} }
} }
void hashtable_destroy(hashtable h) void hashtable_destroy(hashtable h)
{ {
if (h==NULL) return; if (h == NULL) return;
for (int i = 0; i < h->size; i++) { for (int i = 0; i < h->size; i++) {
hashtable_destroy_bucket(h, i); hashtable_destroy_bucket(h, i);
} }
free (h->table); free(h->table);
free(h); free(h);
} }
@@ -171,12 +172,15 @@ bool ht_iter_next(ht_iterator *iter)
} }
hashtable h = iter->h; hashtable h = iter->h;
while(true) { while (true) {
if (iter->index >= h->size) { if (iter->index >= h->size) {
return false; return false;
} }
if (iter->entry == NULL) { if (iter->entry == NULL) {
iter->index++; iter->index++;
if (iter->index >= h->size) {
return false;
}
iter->entry = h->table[iter->index]; iter->entry = h->table[iter->index];
} else { } else {
iter->entry = iter->entry->next; iter->entry = iter->entry->next;