#include #include "map.h" struct couple { int key; void* val; }; struct map { struct couple* couples; int nb_couples; }; struct map* map_init() { struct map* map = malloc(sizeof(struct map)); map->nb_couples = 0; map->couples = NULL; return map; } void map_free(struct map*map) { if(map->nb_couples) free(map->couples); free(map); } /* return the index of key or -1 if not found */ static int map_find_key(struct map*map, int key) { for(int i=0; inb_couples; i++) { if(map->couples[i].key == key) return i; } return -1; } void map_put(struct map* map, int key, void* val) { int index = map_find_key(map, key); if(index < 0) { /* key was not found. expend the array */ map->nb_couples++; map->couples = realloc(map->couples, map->nb_couples*sizeof(struct couple)); index = map->nb_couples-1; } map->couples[index].key= key; map->couples[index].val = val; } void* map_get(struct map* map, int key) { int index = map_find_key(map, key); if(index >=0) return map->couples[index].val; return NULL; }