void hashmap_dispose(hashmap *hm) { for (int i = 1; i <= hm->num_buckets; ++i) vector_dispose(hm->pairs + i); vector_dispose(hm->used_buckets); free(hm->pairs); free(hm->used_buckets); hm->pairs = NULL; hm->used_buckets = NULL; }
void test_int_hashmap() { hashmap hm; hashmap_new(&hm, sizeof(char*), 20, hashmap_int_hash_fun, hashmap_int_compare_fun, NULL, Number); char* words[] = { "casa", "borde", "leche", "vaca", "mesa", "pila", "solo", "pared", "luz", "television", "dvd", "pata", "plato", "vinilo", "amplificador"}; int numbers[15]; for (int i = 0; i < 15; ++i) { int n = get_random(uniforme, 0, 150); numbers[i] = n; hashmap_enter(&hm, &numbers[i], words[i]); } printf("\n"); int k = 88; char* word = (char*) hashmap_get_value(&hm, &k); printf("word with key 88 is %s\n", word); vector* res = hashmap_get_values(&hm); for (int i = 0; i < res->len; ++i) { char* w = (char*) res->elements + i*hm.value_size; printf("%s\n", w); } hashmap_map(&hm, hashmap_int_print_pair, NULL); vector_dispose(res); free(res); res = NULL; hashmap_dispose(&hm); }
int main(void) { vector v; int num = 23; vector_new(&v, sizeof(int *), NULL, 2); vector_append(&v, &num); vector_append(&v, &num); vector_append(&v, &num); vector_append(&v, &num); vector_dispose(&v); return 0; }