fix: hash map keys/values funcs now return lists of pointers to elements

main
Sara 2024-09-24 15:25:51 +02:00
parent af50174b71
commit bb6dcbeabe
1 changed files with 14 additions and 8 deletions

View File

@ -42,17 +42,23 @@ void hash_map_insert(HashMap *self, void *key, void *value) {
} }
List hash_map_keys(HashMap *self) { List hash_map_keys(HashMap *self) {
List keys = list_init(self->key_size); List keys = list_from_type(void*);
for(size_t bucket_index = 0; bucket_index < CUTES_HASH_MAP_BUCKETS; ++bucket_index) for(size_t bucket_index = 0; bucket_index < CUTES_HASH_MAP_BUCKETS; ++bucket_index) {
for(size_t key_index = 0; key_index < self->buckets[bucket_index].len; ++key_index) for(size_t key_index = 0; key_index < self->buckets[bucket_index].len; ++key_index) {
list_add(&keys, list_at(self->buckets + bucket_index, key_index)); void *key = list_at(self->buckets + bucket_index, key_index) + sizeof(uintptr_t);
list_add(&keys, &key);
}
}
return keys; return keys;
} }
List hash_map_values(HashMap *self) { List hash_map_values(HashMap *self) {
List values = list_init(self->value_size); List values = list_from_type(void*);
for(size_t bucket_index = 0; bucket_index < CUTES_HASH_MAP_BUCKETS; ++bucket_index) for(size_t bucket_index = 0; bucket_index < CUTES_HASH_MAP_BUCKETS; ++bucket_index) {
for(size_t value_index = 0; value_index < self->buckets[bucket_index].len; ++value_index) for(size_t value_index = 0; value_index < self->buckets[bucket_index].len; ++value_index) {
list_add(&values, ((char*)list_at(self->buckets + bucket_index, value_index)) + (sizeof(uintptr_t) + self->key_size)); void *value = ((char*)list_at(self->buckets + bucket_index, value_index)) + (sizeof(uintptr_t) + self->key_size);
list_add(&values, &value);
}
}
return values; return values;
} }