Skip to content
Snippets Groups Projects
Commit 254dbefd authored by thib's avatar thib
Browse files

everything is working

parent f04b24c6
No related branches found
No related tags found
No related merge requests found
#include "hash.h" #include "hash.h"
// typedef struct _element {
// void *data;
// struct _element *next;
// } element;
typedef struct _entry_t { typedef struct _entry_t {
char *key; char *key;
char *value; char *value;
...@@ -15,7 +10,6 @@ typedef struct _hm_t { ...@@ -15,7 +10,6 @@ typedef struct _hm_t {
int length; int length;
} hm_t; } hm_t;
//
int hash(char *str, int length) { int hash(char *str, int length) {
int val = 0; int val = 0;
int len = strlen(str); int len = strlen(str);
...@@ -59,10 +53,13 @@ void hm_destroy(hm_t **hm) { ...@@ -59,10 +53,13 @@ void hm_destroy(hm_t **hm) {
hm = NULL; hm = NULL;
} }
entry_t *createEntry(char *key, char *value) { entry_t *createEntry(char *key, char *value) {
entry_t *entry = malloc(sizeof(entry_t)); entry_t *entry = malloc(sizeof(entry_t));
entry->key = malloc(sizeof(char) * (strlen(key) + 1)); 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->key, key);
strcpy(entry->value, value); strcpy(entry->value, value);
return entry; return entry;
...@@ -77,14 +74,13 @@ bool isSameKey(char *key, void *x) { ...@@ -77,14 +74,13 @@ bool isSameKey(char *key, void *x) {
hm_t *hm_set(hm_t *hm, char *key, char *value) { hm_t *hm_set(hm_t *hm, char *key, char *value) {
int h = hash(key, hm->length); int h = hash(key, hm->length);
// printf("h(%s) = %d\n", key, h);
element *el = find_element(&hm->elements[h], isSameKey, key); element *el = find_element(&hm->elements[h], isSameKey, key);
if (el != NULL) { // same key -> update val if (el != NULL) { // same key -> update val
// printf("update value\n"); printf("update val\n");
strcpy(((entry_t *)(el->data))->value, value); strcpy(((entry_t *)(el->data))->value, value);
} else { // key is unique -> push it } else { // key is unique -> push it
// printf("push\n");
lst_vector_push(&hm->elements[h], (void *)createEntry(key, value)); lst_vector_push(&hm->elements[h], (void *)createEntry(key, value));
} }
...@@ -99,35 +95,38 @@ char *hm_get(hm_t *hm, char *key) { ...@@ -99,35 +95,38 @@ char *hm_get(hm_t *hm, char *key) {
if (el == NULL) { if (el == NULL) {
return "NULL"; return "NULL";
} }
return ((entry_t *)(el->data))->value; return ((entry_t *)(el->data))->value;
} }
// retire une clé de la hm et la retourne // 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); element *el =remove_element(&hm->elements[hash(key, hm->length)], isSameKey, key);
if (el != NULL) { 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));
printf("%s - %s removed\n",((entry_t *)(el->data))->key,((entry_t *)(el->data))->value); strcpy(value,entry->value);
destroyEntry((entry_t *)(el->data)); destroyEntry(entry);
free(el); free(el);
return;
} return value;
printf("trying to remove non existing entry !\n");
} }
// convertit la hm en chaîne de caractères
// char *hm_to_str(hm_t *hm);
// affiche le contenu de la hm // affiche le contenu de la hm
void hm_print(hm_t *hm) { void hm_print(hm_t *hm) {
for (int i = 0; i < hm->length; ++i) { for (int i = 0; i < hm->length; ++i) {
int len = lst_vector_length(&hm->elements[i]); int len = lst_vector_length(&hm->elements[i]);
printf("h[%d] : ", i); printf("[%d]: ", i);
for (int j = 0; j < len; j++) { for (int j = 0; j < len; j++) {
element *el = lst_vector_get(&hm->elements[i], 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); ((entry_t *)(el->data))->value);
} }
printf("\n"); printf("\n");
......
...@@ -22,8 +22,9 @@ void hm_destroy(hm_t **hm); ...@@ -22,8 +22,9 @@ void hm_destroy(hm_t **hm);
hm_t *hm_set(hm_t *hm, char *key, char *value); hm_t *hm_set(hm_t *hm, char *key, char *value);
// retourne la valeur associé à la clé, key // retourne la valeur associé à la clé, key
char *hm_get(hm_t *hm, char *key); char *hm_get(hm_t *hm, char *key);
// retire une clé de la hm et la retourne // 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 // affiche le contenu de la hm
void hm_print(hm_t *hm); void hm_print(hm_t *hm);
......
...@@ -189,14 +189,14 @@ element *remove_element(lst_vector *v, bool (*f)(char *, void *), char *key) { ...@@ -189,14 +189,14 @@ element *remove_element(lst_vector *v, bool (*f)(char *, void *), char *key) {
if (f(key, current->data)) { if (f(key, current->data)) {
if(loops==0){ if(loops==0){
return lst_vector_pop(v); return lst_vector_pop(v); //first
} }
printf("found it \n");
prev->next=current->next; prev->next=current->next;
return current; return current;
} }
prev=current; prev=current;
current = current->next; current = current->next;
loops++;
} }
return NULL; return NULL;
} }
......
...@@ -5,17 +5,24 @@ ...@@ -5,17 +5,24 @@
int main() { int main() {
hm_t *hm = hm_create(20); hm_t *hm = hm_create(7);
for(int i=0;i<100;i++){ for(int i=0;i<15;i++){
char str1[10]; char str1[80];
char str2[10]; char str2[80];
sprintf(str1,"%d", i); sprintf(str1,"%d", i);
sprintf(str2,"%d", i); sprintf(str2,"%d", i*10);
hm_set(hm,str1,str2); 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_print(hm);
hm_destroy(&hm); hm_destroy(&hm);
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment