Skip to content
Snippets Groups Projects
Commit 96ad637c authored by dario.genga's avatar dario.genga
Browse files

Add current state of ex

parent a5cd2dd1
Branches
No related tags found
No related merge requests found
......@@ -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;
}
......@@ -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;
}
......@@ -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;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment