]> git.datanom.net - hashtable.git/commitdiff
print function master
authorMichael Rasmussen <mir@datanom.net>
Sun, 30 Apr 2023 00:03:21 +0000 (02:03 +0200)
committerMichael Rasmussen <mir@datanom.net>
Sun, 30 Apr 2023 00:03:21 +0000 (02:03 +0200)
Signed-off-by: Michael Rasmussen <mir@datanom.net>
hashtable.c
hashtable.h
test.c

index b6423634464b663ce8c57a2d48a4d175e7c3847b..8e713dd53bdf49213d3def66bf0a96a1c4c09387 100644 (file)
@@ -14,6 +14,7 @@ struct _Hashtable {
        HashFunc hash;
        EqualFunc compare;
        FreeFunc free;
+       PrintFunc print;
        Node** nodes;
 };
 
@@ -28,15 +29,19 @@ static uint32_t hash_table_index(Hashtable* ht, const void* key) {
        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;
@@ -46,6 +51,11 @@ Hashtable* hash_table_create_full(HashFunc hf, EqualFunc ef, uint32_t size, Free
        } 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;
 }
@@ -64,6 +74,10 @@ void hash_table_destroy(Hashtable* 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++) {
@@ -73,7 +87,9 @@ void hash_table_print(Hashtable* ht) {
                        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");
index b0b08f5c40f8707670d592df4c694bd7e9e4e9d2..edd91f4dffe42ec02a6844ed6a43fd5951802424 100644 (file)
@@ -10,6 +10,7 @@
 typedef uint64_t (*HashFunc)(const void*);
 typedef bool (*EqualFunc)(const void*, const void*);
 typedef void (*FreeFunc)(void*);
+typedef void (*PrintFunc)(void*);
 typedef struct _Hashtable Hashtable;
 typedef void (*ForeachFunc)(void*, void*, void*);
 
@@ -37,7 +38,7 @@ bool ptr_equal(const void*, const void*);
 
 Hashtable* hash_table_create(HashFunc hf, EqualFunc ef);
 Hashtable* hash_table_create_notify(HashFunc hf, EqualFunc ef, FreeFunc ff);
-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);
 void hash_table_destroy(Hashtable* ht);
 void hash_table_print(Hashtable* ht);
 bool hash_table_insert(Hashtable* ht, const void* key, void* obj);
@@ -52,6 +53,7 @@ bool hash_table_iter_next(HashtableIter* iter, void** key, void** value);
 bool hash_table_contains(Hashtable* ht, const void* key);
 uint64_t hash_table_size(Hashtable* ht);
 bool hash_table_replace(Hashtable* ht, void* key, void* value);
+void hash_table_set_print_func(Hashtable* ht, PrintFunc pf);
 
 #endif
 
diff --git a/test.c b/test.c
index 49a33a9b3543a6bcf8af6fd9e1fc2194eedb6a82..a9a08c05256a8d0222bd9ab328eb6faad0e047ea 100644 (file)
--- a/test.c
+++ b/test.c
@@ -40,6 +40,11 @@ void my_ptr_free(void* o) {
        free(node);
 }
 
+void my_print(void* o) {
+       Node* n = (Node *) o;
+       printf("%s", n->name);
+}
+
 bool my_ptr_equal(const void* a, const void* b) {
        const Node* nodea = a;
        const Node* nodeb = b;
@@ -100,6 +105,9 @@ int main(int argc, char** argv) {
        fclose(fp);
 
        printf("Loaded %d words into the table.\n", numwords);
+       hash_table_print(table);
+       hash_table_set_print_func(table, my_print);
+       hash_table_print(table);
 
 /*
        uint32_t good_guesses = 0;
@@ -216,4 +224,26 @@ int main(int argc, char** argv) {
        }
 
        hash_table_destroy(table);
+
+       table = hash_table_create(str_hash, str_equal);
+       char *names[] = { "Adam", "Eva", "Abraham", "Kain", "Abel", NULL };
+       for (int i = 0; names[i]; i++) {
+               void* name = names[i];
+               hash_table_insert(table, name, strdup(name));
+       }
+       hash_table_iter_init(&iter, table);
+       while (hash_table_iter_next(&iter, &key, &value)) {
+               printf("Key: %p value: %s\n", (char*)key, (char*)value);
+       }
+       data = hash_table_delete(table, (void*) names[4]);
+       printf("Deleted: [%p] %s\n", data, (char*) data);
+       free(data);
+       hash_table_replace(table, (void*) names[4], strdup(names[4]));
+       hash_table_replace(table, (void*) names[4], strdup("Johannes"));
+       hash_table_iter_init(&iter, table);
+       while (hash_table_iter_next(&iter, &key, &value)) {
+               printf("Key: %p value: %s\n", (char*)key, (char*)value);
+       }
+
+       hash_table_destroy(table);
 }
This page took 0.039191 seconds and 5 git commands to generate.