map_t map_remove(map_t map, key_t key){ if (map == NULL) printf("The map is already empty!"); else { if (string_less(key,map->key)) map->leftnode = map_remove(map->leftnode, key); else if (string_less(map->key, key)) map->rightnode = map_remove(map->rightnode, key); else { if(map->leftnode == NULL && map->rightnode == NULL){ string_destroy(map->key); string_destroy(map->value); free(map); map = NULL; } else if (map->leftnode == NULL){ map_t mapaux = map; map = map->rightnode; string_destroy(mapaux->key); string_destroy(mapaux->value); free(mapaux); mapaux = NULL; } else if (map->rightnode == NULL){ map_t mapaux = map; map = map->leftnode; string_destroy(mapaux->key); string_destroy(mapaux->value); free(mapaux); mapaux = NULL; } else { map_t mapmin = map_min(map->rightnode); map_t mapaux = map; string_destroy(map->value); string_destroy(map->key); mapmin->leftnode = map->leftnode; map = map->rightnode; free(mapaux); } } } return map; }
bool map_contains(map_t map, key_t key){ bool exist = false; if (map != NULL){ if(string_eq(key, map->key)){ exist = true; } else if (string_less(key, map->key)){ exist = map_contains(map->leftnode, key); } else { exist = map_contains(map->rightnode, key); } } return exist; }
value_t map_get(map_t map, key_t key){ value_t resultado = NULL; if (map != NULL) { if (string_eq(key, map->key)){ resultado = map->value; } else if (string_less(key, map->key)){ resultado = map_get(map->leftnode, key); } else { resultado = map_get(map->rightnode, key); } } return resultado; }
map_t map_put(map_t map, key_t key, value_t value){ if (map == NULL){ map = calloc(1, sizeof(struct _node_t)); map->leftnode = NULL; map->key = key; map->value = value; map->rightnode = NULL; } else if(string_eq(map->key, key)){ string_destroy(map->value); map->value = value; string_destroy(map->key); map->key = key; } else if(string_less(key, map->key)){ map->leftnode = map_put(map->leftnode, key, value); } else { map->rightnode = map_put(map->rightnode, key, value); } return map; }
void _type_less_string(const void* cpv_first, const void* cpv_second, void* pv_output) { assert(cpv_first != NULL && cpv_second != NULL && pv_output != NULL); *(bool_t*)pv_output = string_less((string_t*)cpv_first, (string_t*)cpv_second); }