hashmap: Add helper to debug hashmap

Add function to print full hashmap for debug purposes.

Signed-off-by: Otavio Pontes <otavio.pontes@intel.com>
This commit is contained in:
Otavio Pontes
2018-12-20 15:20:24 -08:00
parent 58ed52d39e
commit f11bb7ee2b
2 changed files with 38 additions and 0 deletions
+32
View File
@@ -23,6 +23,7 @@
#define _GNU_SOURCE
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -149,3 +150,34 @@ bool hashmap_contains(struct hashmap *hashmap, const void *key)
{
return hashmap_get_internal(hashmap, key, false) != NULL;
}
void hashmap_print(struct hashmap *hashmap, void(print_data)(void *data))
{
struct list *l;
void *out_data;
int i;
size_t count = 0;
if (!hashmap) {
return;
}
printf("Hashmap (bits: %x, size: %d)\n", hashmap->mask_bits,
HASH_SIZE(hashmap->mask_bits));
for (i = 0; i < HASH_SIZE(hashmap->mask_bits); i++) {
printf("map[%d] list(%d) {", i, list_len(hashmap->map[i]));
for (l = hashmap->map[i], out_data = l ? l->data : NULL;
l && (out_data = l->data); l = l->next) {
if (print_data) {
print_data(out_data);
}
count++;
}
printf("}\n");
}
printf("Total elements: %ld\n", count);
return;
}
+6
View File
@@ -69,6 +69,12 @@ void hashmap_free_hash_and_data(struct hashmap *hashmap, free_data_fn_t free_dat
* Hash function helper to calculate a good hash to be used with strings.
*/
size_t hashmap_hash_from_string(const char *key);
/*
* Print a hashmap, for debug purposes.
*/
void hashmap_print(struct hashmap *hashmap, void(print_data)(void *data));
/*
* Loop through all elements in the hashmap.
*