Skip to content

Commit

Permalink
feat(hash_table): support iterating only occupied buckets
Browse files Browse the repository at this point in the history
  • Loading branch information
exbotanical committed Dec 29, 2024
1 parent 0543dc1 commit b3f79ab
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
3 changes: 2 additions & 1 deletion clib.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "libhash",
"version": "1.0.0",
"version": "1.1.0",
"author": "Matthew Zito",
"repo": "exbotanical/libhash",
"license": "MIT",
Expand All @@ -13,6 +13,7 @@
"src/hash.h",
"src/prime.c",
"src/prime.h",
"src/list.h",
"include/libhash.h"
],
"dependencies": {
Expand Down
9 changes: 5 additions & 4 deletions src/hash_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ static void ht_resize(hash_table *ht, int base_capacity) {

const int tmp_capacity = ht->capacity;
ht->capacity = new_ht->capacity;
new_ht->capacity = tmp_capacity;

// Cannot free values - we may still be using them.
ht_entry **tmp_entries = ht->entries;
ht->entries = new_ht->entries;
new_ht->entries = tmp_entries;
free(tmp_entries);

list_free(ht->occupied_buckets);
ht->occupied_buckets = new_ht->occupied_buckets;

// Cannot free values - we may still be using them.
free(new_ht->entries);
free(new_ht);
}

Expand Down Expand Up @@ -183,6 +183,7 @@ static void __ht_delete_table(hash_table *ht) {
}
}

// list_free(ht->occupied_buckets);
free(ht->entries);
free(ht);
}
Expand Down
4 changes: 2 additions & 2 deletions src/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ static void list_remove(node_t **head, int value) {
}

// TODO: Use and test
static void free_list(node_t *head) {
static void list_free(node_t *head) {
node_t *tmp;
while (head) {
while (head != &LIST_SENTINEL_NODE) {
tmp = head;
head = head->next;
free(tmp);
Expand Down

0 comments on commit b3f79ab

Please sign in to comment.