]> git.datanom.net - hashtable.git/blobdiff - hashtable.c
print function
[hashtable.git] / hashtable.c
index aa301326d732e42dbfcc85e2bf6f58d409abef69..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");
@@ -101,7 +117,7 @@ bool hash_table_insert(Hashtable* ht, const void* key, void* obj) {
 
 void* hash_table_lookup(Hashtable* ht, const void* key) {
        if (key == NULL || ht == NULL) return false;
-       uint64_t index = hash_table_index(ht, key);
+       uint32_t index = hash_table_index(ht, key);
 
        Node* node = ht->nodes[index];
        while (node != NULL && ht->compare(node->data, key) == false) {
@@ -109,7 +125,7 @@ void* hash_table_lookup(Hashtable* ht, const void* key) {
        }
 
        if (node == NULL) return NULL;
-       printf("find: %p -> found: %p\n", key, node->data);
+       //printf("find: %p -> found: %p\n", key, node->data);
 
        return node->data;
 }
@@ -250,6 +266,53 @@ bool hash_table_iter_next(HashtableIter* iter, void** key, void** value) {
        return false;
 }
 
+bool hash_table_contains(Hashtable* ht, const void* key) {
+       if (ht == NULL || key == NULL) return false;
+
+       if (hash_table_lookup(ht, key) == NULL)
+               return false;
+       return true;
+}
+
+uint64_t hash_table_size(Hashtable *ht) {
+       uint64_t size = 0;
+
+       if (ht == NULL) return size;
+
+       for (uint32_t i = 0; i < ht->size; i++) {
+               if (ht->nodes[i] != NULL) {
+                       Node* node = ht->nodes[i];
+                       while (node != NULL) {
+                               size++;
+                               node = node->next;
+                       }
+               }
+       }
+
+       return size;
+}
+
+bool hash_table_replace(Hashtable *ht, void* key, void* value) {
+       if (ht == NULL || key == NULL || value == NULL) return false;
+
+       uint32_t index = hash_table_index(ht, key);
+
+       Node* node = ht->nodes[index];
+       while (node != NULL && ht->compare(node->data, key) == false) {
+               node = node->next;
+       }
+
+       if (node == NULL)
+               return hash_table_insert(ht, key, value);
+       else {
+               free(node->data);
+               node->key = key;
+               node->data = value;
+       }
+
+       return true;
+}
+
 uint64_t int_hash(const void* key) {
        return *(uint64_t*) key;
 }
This page took 0.034068 seconds and 5 git commands to generate.