diff --git a/hashmap/hash.c b/hashmap/hash.c index c45dcb53abb8c7789f031445eec11488bec0cf4c..aacd5ad4cf200efec427c8fc17bc581e417db17c 100644 --- a/hashmap/hash.c +++ b/hashmap/hash.c @@ -1,10 +1,5 @@ #include "hash.h" -// typedef struct _element { -// void *data; -// struct _element *next; -// } element; - typedef struct _entry_t { char *key; char *value; @@ -15,7 +10,6 @@ typedef struct _hm_t { int length; } hm_t; -// int hash(char *str, int length) { int val = 0; int len = strlen(str); @@ -59,10 +53,13 @@ void hm_destroy(hm_t **hm) { hm = NULL; } + entry_t *createEntry(char *key, char *value) { entry_t *entry = malloc(sizeof(entry_t)); entry->key = malloc(sizeof(char) * (strlen(key) + 1)); - entry->value = malloc(sizeof(char) * (strlen(key) + 1)); + assert(entry->key!=NULL); + entry->value = malloc((strlen(value) + 1)); + assert(entry->value!=NULL); strcpy(entry->key, key); strcpy(entry->value, value); return entry; @@ -77,14 +74,13 @@ bool isSameKey(char *key, void *x) { hm_t *hm_set(hm_t *hm, char *key, char *value) { int h = hash(key, hm->length); - // printf("h(%s) = %d\n", key, h); element *el = find_element(&hm->elements[h], isSameKey, key); if (el != NULL) { // same key -> update val - // printf("update value\n"); + printf("update val\n"); strcpy(((entry_t *)(el->data))->value, value); } else { // key is unique -> push it - // printf("push\n"); + lst_vector_push(&hm->elements[h], (void *)createEntry(key, value)); } @@ -99,35 +95,38 @@ char *hm_get(hm_t *hm, char *key) { if (el == NULL) { return "NULL"; } + return ((entry_t *)(el->data))->value; } // retire une clé de la hm et la retourne -void hm_rm(hm_t *hm, char *key) { - +char* hm_rm(hm_t *hm, char *key){ element *el =remove_element(&hm->elements[hash(key, hm->length)], isSameKey, key); - if (el != NULL) { - - printf("%s - %s removed\n",((entry_t *)(el->data))->key,((entry_t *)(el->data))->value); - destroyEntry((entry_t *)(el->data)); - free(el); - return; - - } + if(el==NULL){ printf("trying to remove non existing entry !\n"); + return "NULL"; + } + entry_t *entry=(entry_t *)(el->data); + char* value=malloc(sizeof(char) * (strlen(entry->value) + 1)); + + strcpy(value,entry->value); + destroyEntry(entry); + free(el); + + return value; + } -// convertit la hm en chaîne de caractères -// char *hm_to_str(hm_t *hm); + // affiche le contenu de la hm void hm_print(hm_t *hm) { for (int i = 0; i < hm->length; ++i) { int len = lst_vector_length(&hm->elements[i]); - printf("h[%d] : ", i); + printf("[%d]: ", i); for (int j = 0; j < len; j++) { element *el = lst_vector_get(&hm->elements[i], j); - printf(" %s - %s ", ((entry_t *)(el->data))->key, + printf("(%s - %s) ", ((entry_t *)(el->data))->key, ((entry_t *)(el->data))->value); } printf("\n"); diff --git a/hashmap/hash.h b/hashmap/hash.h index 548a5ea7ebd77fa16edf99973c092c421473433e..94302d9bb1df93556892496098cb85f95c858a3b 100644 --- a/hashmap/hash.h +++ b/hashmap/hash.h @@ -22,8 +22,9 @@ void hm_destroy(hm_t **hm); hm_t *hm_set(hm_t *hm, char *key, char *value); // retourne la valeur associé à la clé, key char *hm_get(hm_t *hm, char *key); + // retire une clé de la hm et la retourne -void hm_rm(hm_t *hm, char *key); +char* hm_rm(hm_t *hm, char *key); // affiche le contenu de la hm void hm_print(hm_t *hm); diff --git a/hashmap/lst_vec.c b/hashmap/lst_vec.c index 1023429136bf19164667f5d53770038e940c646b..9f13d5ec3f0c459873b95eeac793780dfda631b3 100644 --- a/hashmap/lst_vec.c +++ b/hashmap/lst_vec.c @@ -189,14 +189,14 @@ element *remove_element(lst_vector *v, bool (*f)(char *, void *), char *key) { if (f(key, current->data)) { if(loops==0){ - return lst_vector_pop(v); + return lst_vector_pop(v); //first } - printf("found it \n"); prev->next=current->next; return current; } prev=current; current = current->next; + loops++; } return NULL; } diff --git a/hashmap/main.c b/hashmap/main.c index 41082bebbe9ca7e6984c77f8b1a534118a54e74a..5a8a86acf7e1876e94559d051e934459bcd3d3dd 100644 --- a/hashmap/main.c +++ b/hashmap/main.c @@ -5,17 +5,24 @@ int main() { - hm_t *hm = hm_create(20); + hm_t *hm = hm_create(7); - for(int i=0;i<100;i++){ - char str1[10]; - char str2[10]; + for(int i=0;i<15;i++){ + char str1[80]; + char str2[80]; sprintf(str1,"%d", i); - sprintf(str2,"%d", i); + sprintf(str2,"%d", i*10); hm_set(hm,str1,str2); - // hm_rm(hm,str1); } + hm_print(hm); + printf("\nget (1 - %s)\n",hm_get(hm,"1")); + printf("get (8 - %s)\n\n",hm_get(hm,"8")); + + char* value=hm_rm(hm,"9"); + printf("9 - %s removed\n",value); + free(value); + hm_print(hm); hm_destroy(&hm); }