]> git.datanom.net - hashtable.git/commitdiff
Add more functions
authorMichael Rasmussen <mir@datanom.net>
Fri, 31 Mar 2023 21:20:22 +0000 (23:20 +0200)
committerMichael Rasmussen <mir@datanom.net>
Fri, 31 Mar 2023 21:20:22 +0000 (23:20 +0200)
hashtable.c
hashtable.h
test.c

index aa301326d732e42dbfcc85e2bf6f58d409abef69..b6423634464b663ce8c57a2d48a4d175e7c3847b 100644 (file)
@@ -101,7 +101,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 +109,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 +250,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;
 }
index 23c8838fcea51aa5c5123d4acf8cf7c159288c63..b0b08f5c40f8707670d592df4c694bd7e9e4e9d2 100644 (file)
@@ -49,6 +49,9 @@ void hash_table_list_free(List* list);
 void hash_table_foreach(Hashtable* ht, ForeachFunc, void* userdata);
 void hash_table_iter_init(HashtableIter* iter, Hashtable* ht);
 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);
 
 #endif
 
diff --git a/test.c b/test.c
index 8cf65c47d6f8d355bfbe7b806dde811b45d246d0..49a33a9b3543a6bcf8af6fd9e1fc2194eedb6a82 100644 (file)
--- a/test.c
+++ b/test.c
@@ -190,16 +190,30 @@ int main(int argc, char** argv) {
        printf("%u out of %u inserted words in table were in the table\n", good_guesses, numwords);
 
        HashtableIter iter;
-       void* key/* = malloc(sizeof(void*))*/;
-       void* value/* = malloc(sizeof(void*))*/;
+       void* key;
+       void* value;
 
        hash_table_iter_init(&iter, table);
        while (hash_table_iter_next(&iter, &key, &value)) {
                char* s1 = ((Node*) value)->name;
                printf("Key: %p value: %s\n", key, s1);
        }
-       //free(key);
-       //free(value);
+
+       Node* node1 = calloc(sizeof(Node*), 1);
+       node1->name = strdup("Dummy Test Name");
+       if (hash_table_insert(table, node1, node1) == true) {
+               hash_table_iter_init(&iter, table);
+               printf("Hashtable size: %lu. Key found: %d\n", hash_table_size(table), hash_table_contains(table, node1));
+               void* data = hash_table_delete(table, node1);
+               printf("Deleted: [%p] %s\n", node1, ((Node*) data)->name);
+               free(((Node*) data)->name);
+               free(data);
+               printf("Hashtable size: %lu. Key found: %d\n", hash_table_size(table), hash_table_contains(table, node1));
+               Node* node2 = calloc(sizeof(Node*), 1);
+               node2->name = strdup("Test Name");
+               hash_table_replace(table, node2, node2);
+               printf("Hashtable size: %lu. Key found: %d\n", hash_table_size(table), hash_table_contains(table, node2));
+       }
 
        hash_table_destroy(table);
 }
This page took 0.039653 seconds and 5 git commands to generate.