1
1
mirror of https://github.com/theoludwig/libcproject.git synced 2024-12-11 21:13:00 +01:00

feat: add hash_map_free

This commit is contained in:
Théo LUDWIG 2023-08-03 23:17:54 +02:00
parent 8b6f06dc6e
commit a0a1310f53
Signed by: theoludwig
GPG Key ID: ADFE5A563D718F3B
5 changed files with 18 additions and 0 deletions

View File

@ -217,3 +217,12 @@ string_t *hash_map_get_keys(struct hash_map *hash_map) {
} }
return keys; return keys;
} }
void hash_map_free(struct hash_map *hash_map) {
for (size_t index = 0; index < hash_map->capacity; index++) {
if (hash_map->items[index] != NULL) {
linked_list_free(hash_map->items[index]);
}
}
free(hash_map);
}

View File

@ -89,4 +89,10 @@ bool hash_map_contains_key(struct hash_map *hash_map, string_t key);
*/ */
string_t *hash_map_get_keys(struct hash_map *hash_map); string_t *hash_map_get_keys(struct hash_map *hash_map);
/**
* @brief Frees the hash map.
* @since v2.1.0
*/
void hash_map_free(struct hash_map *hash_map);
#endif #endif

View File

@ -92,5 +92,6 @@ void linked_list_free(struct linked_list *list) {
node_current = node_current->next; node_current = node_current->next;
free(node_to_remove); free(node_to_remove);
} }
list->head = NULL;
free(list); free(list);
} }

View File

@ -285,6 +285,7 @@ bool string_get_has_unique_characters(const string_t string_value) {
hash_map_add(characters_already_seen, key, (void*)true); hash_map_add(characters_already_seen, key, (void*)true);
} }
} }
hash_map_free(characters_already_seen);
return has_unique; return has_unique;
} }

View File

@ -24,4 +24,5 @@ void hash_map_test() {
hash_map_remove(hash_map, "key5"); hash_map_remove(hash_map, "key5");
assert(hash_map->length == 5); assert(hash_map->length == 5);
assert(!hash_map_contains_key(hash_map, "key5")); assert(!hash_map_contains_key(hash_map, "key5"));
hash_map_free(hash_map);
} }