#include #include #include #include "hashmap.h" #define DEBUG 1 #if DEBUG #define DEBUG_PRINTF(...) printf(__VA_ARGS__) #else #define DEBUG_PRINTF(...) (void)0 #endif /* computes the hash of a key */ uint32_t hash(K key) { /* Please do not modify this function */ uint32_t ret = 0; for(int i=0; ientries[i] = NULL; } /* destroy a hashmap */ // bareme: 2 points void hashmap_finalize(struct hashmap_t *hashmap) { /* TODO: free all the allocated entries */ for(int i=0; ientries[i]; while(cur) { // 1 point pour parcourt de la liste struct hashmap_entry_t *next = cur->next; free(cur); // 0.5 point pour free cur = next; // 0.5 point pour la gestion correcte des pointeurs } } } /* return the hashmap entry that corresponds to key */ // bareme: 2 points static struct hashmap_entry_t * get(struct hashmap_t *hashmap, K key) { uint32_t h = hash(key); struct hashmap_entry_t *list = hashmap->entries[h]; /* TODO */ while(list) { // 1 pt pour parcourt de la liste if(strcmp(list->key, key) == 0) // 1 pt pour la comparaison return list; list = list->next; } return NULL; } /* insert (key, value) into the hashmap */ // bareme: 3 points void hashmap_put(struct hashmap_t *hashmap, K key, V value) { /* To simplify the implementation, we assume that key is not already in the hashmap */ // appeler hash(key) pour savoir dans quelle case (appelée h) // du tableau entries insérer la nouvelle entrée uint32_t h = hash(key); DEBUG_PRINTF("Putting (%s, %s) in %u\n", key, value, h); // TODO: allouer une nouvelle struct hashmap_entry_t // (appelée new_entry) et l'initialiser. // Pensez à y recopier key et value // 1 point pour malloc struct hashmap_entry_t* new_entry = malloc(sizeof(struct hashmap_entry_t)); // 1 point pour initialisation strncpy(new_entry->value, value, STRING_LEN); strncpy(new_entry->key, key, STRING_LEN); new_entry->hash = h; // TODO: insérer new_entry dans la liste chaînée h // 1 point pour insertion dans la liste new_entry->next = hashmap->entries[h]; hashmap->entries[h] = new_entry; } /* get the value that corresponds to key. * return 1 if the key is found, or 0 otherwise */ int hashmap_get(struct hashmap_t *hashmap, K key, V value) { uint32_t h = hash(key); DEBUG_PRINTF("Looking for %s in %u\n", key, h); /* PLEASE DO NOT MODIFY THIS FUNCTION */ struct hashmap_entry_t *cur_node = get(hashmap, key); if(cur_node) { DEBUG_PRINTF("\tfound %s\n", cur_node->value); strncpy(value, cur_node->value, STRING_LEN); return 1; } DEBUG_PRINTF("\tnot found\n"); return 0; }