From 96ad637c666e24fd51027b48752a404d795b3f7b Mon Sep 17 00:00:00 2001 From: "dario.genga" <dario.genga@etu.hesge.ch> Date: Tue, 3 May 2022 15:49:45 +0200 Subject: [PATCH] Add current state of ex --- ex2/ex2.c | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ ex4/ex4.c | 33 +++++++++++++++++ ex5/ex5.c | 14 ++++++++ 3 files changed, 151 insertions(+) diff --git a/ex2/ex2.c b/ex2/ex2.c index 938dcdf..878e30c 100644 --- a/ex2/ex2.c +++ b/ex2/ex2.c @@ -8,6 +8,110 @@ #include <string.h> #include <stdbool.h> +typedef struct hm_t { + struct entry_t **entries; + int length; +} hm; + +typedef struct entry_t { + char *key; + double value; + struct entry_t *next; +} entry; + +int hash(char *key, int length) { + int index = 0; + for (int i = 0; i < 4; ++i) { + index = (43 * index + key[i]) % length; + } + return index; +} + +entry *entry_create_empty() { + entry *e = malloc(sizeof(entry)); + e->key = NULL; + e->value = -1; + e->next = NULL; + return e; +} + +hm *hm_create(unsigned int length) { + hm *hashmap = malloc(sizeof(hm)); + hashmap->length = length; + hashmap->entries = malloc(sizeof(entry) * length); + for (size_t i = 0; i < length; i++) { + hashmap->entries[i] = entry_create_empty(); + } + + return hashmap; +} + +void hm_destroy(hm **hm) { + for (int i = 0; i < (*hm)->length; i++) { + entry *e = (*hm)->entries[i]; + while (e != NULL) { + entry *to_free = e; + e = e->next; + free(to_free); + } + } + free((*hm)->entries); + free((*hm)); + (*hm) = NULL; +} + +entry *get_entry_by_key(entry *e, char *key) { + if (e->key == key) { + return e; + } else if (e->next != NULL) { + return get_entry_by_key(e->next, key); + } else { + return e; + } +} + +hm *hm_set(hm *hm, const char *const key, double value) { + // Hash the key and get the corresponding entry + int key_index = hash((char *)key, hm->length); + entry *e = get_entry_by_key(hm->entries[key_index], (char *)key); + + // Set the key and create the next entry if the key is not existing + if (e->key != key) { + e->key = (char *)key; + if (e->next == NULL) { + e->next = entry_create_empty(); + } + } + + // Set the entry value and return the hashmap + e->value = value; + return hm; +} + + int main() { + int length = 10; + hm* hashmap = hm_create(length); + hm_set(hashmap, "ajku", 1.1); + hm_set(hashmap, "ajkv", 2.2); + hm_set(hashmap, "ajkw", 3.1); + hm_set(hashmap, "ajkx", 4.2); + hm_set(hashmap, "ajky", 5.7); + hm_set(hashmap, "ajkz", 6.1); + hm_set(hashmap, "ajka", 1.8); + hm_set(hashmap, "ajkb", 2.7); + hm_set(hashmap, "ajkc", 3.6); + hm_set(hashmap, "bjkd", 1.9); + + // print + + + hm_set(hashmap, "ajkt", 0.1); + hm_set(hashmap, "ajku", 9.1); + hm_set(hashmap, "ajkx", 2.9); + + // print + + hm_destroy(&hashmap); return EXIT_SUCCESS; } diff --git a/ex4/ex4.c b/ex4/ex4.c index 0d3ec90..49c8d8e 100644 --- a/ex4/ex4.c +++ b/ex4/ex4.c @@ -7,6 +7,7 @@ #include <stdlib.h> #include <string.h> #include <stdbool.h> +#include <math.h> #define QUADTREE_SIZE 16 @@ -74,6 +75,25 @@ node *create_tree_from_matrix(int nb_row, int nb_col, int matrix[nb_row][nb_col] return qt; } +node *copy(node *original) { + node *c = original; + return c; +} + +void destroy(node *tree) { + if (tree == NULL) { + return; + } + if (is_leaf(tree)) { + free(tree); + return; + } + + for (int i = 0; i < 4; i++) { + destroy(tree->child[i]); + } +} + int main() { int row = QUADTREE_SIZE; int col = QUADTREE_SIZE; @@ -96,5 +116,18 @@ int main() { {0, 3, 3, 3, 3, 0, 0, 7, 7, 7, 7, 0, 0, 11, 11, 11} }; + node* qTree = create_tree_from_matrix(row, col, matrix, 4); + node* cpy = copy(qTree); + + for (int r = 0; r < row; r++) { + for (int c = 0; c < col; c++) { + printf("%d ", matrix[r][c]); + } + printf("\n"); + } + + destroy(cpy); + destroy(qTree); + return EXIT_SUCCESS; } diff --git a/ex5/ex5.c b/ex5/ex5.c index 9fbb0a9..6aa5b1c 100644 --- a/ex5/ex5.c +++ b/ex5/ex5.c @@ -90,6 +90,20 @@ void transform(node *qt, int size, int indices[size]) { } } +void destroy(node *tree) { + if (tree == NULL) { + return; + } + if (is_leaf(tree)) { + free(tree); + return; + } + + for (int i = 0; i < 4; i++) { + destroy(tree->child[i]); + } +} + int main() { int row = QUADTREE_SIZE; int col = QUADTREE_SIZE; -- GitLab