HashFunc hash;
EqualFunc compare;
FreeFunc free;
+ PrintFunc print;
Node** nodes;
};
return (ht->hash(key) % ht->size);
}
+static void hash_table_default_print(void* data) {
+ printf("%p", data);
+}
+
Hashtable* hash_table_create(HashFunc hf, EqualFunc ef) {
- return hash_table_create_full(hf, ef, TABLESIZE, NULL);
+ return hash_table_create_full(hf, ef, TABLESIZE, NULL, NULL);
}
Hashtable* hash_table_create_notify(HashFunc hf, EqualFunc ef, FreeFunc ff) {
- return hash_table_create_full(hf, ef, TABLESIZE, ff);
+ return hash_table_create_full(hf, ef, TABLESIZE, ff, NULL);
}
-Hashtable* hash_table_create_full(HashFunc hf, EqualFunc ef, uint32_t size, FreeFunc ff) {
+Hashtable* hash_table_create_full(HashFunc hf, EqualFunc ef, uint32_t size, FreeFunc ff, PrintFunc pf) {
Hashtable* ht = malloc(sizeof(*ht));
ht->size = size;
ht->hash = hf;
} else {
ht->free = free;
}
+ if (pf) {
+ ht->print = pf;
+ } else {
+ ht->print = hash_table_default_print;
+ }
ht->nodes = calloc(sizeof(Node*), ht->size);
return ht;
}
free(ht);
}
+void hash_table_set_print_func(Hashtable* ht, PrintFunc pf) {
+ ht->print = pf;
+}
+
void hash_table_print(Hashtable* ht) {
printf("Start Table\n");
for (uint32_t i = 0; i < ht->size; i++) {
printf("\t%i\t\n", i);
Node* node = ht->nodes[i];
while (node != NULL) {
- printf("\"%p\" - (%p)", node->key, node->data);
+ printf("\"%p\" - (", node->key);
+ ht->print(node->data);
+ printf(")");
node = node->next;
}
printf("\n");