void *Hashmap_delete(Hashmap *map, void *key) { uint32_t hash = 0; DArray *bucket = Hashmap_find_bucket(map, key, 0, &hash); if(!bucket) { return NULL; } int i = Hashmap_get_node(map, hash, bucket, key); if(i == -1) { return NULL; } HashmapNode *node = DArray_get(bucket, i); void *data = node->data; free(node); HashmapNode *ending = DArray_pop(bucket); if(ending != node) { // alright looks like it's not the last one, swap it. DArray_set(bucket, i, ending); } return data; }
void *Hashmap_get(Hashmap *map, void *key) { uint32_t hash = 0; DArray *bucket = Hashmap_find_bucket(map, key, 0, &hash); if (!bucket) return NULL; int i = Hashmap_get_node(map, hash, bucket, key); if (i == -1) return NULL; HashmapNode *node = DArray_get(bucket, i); check(node != NULL, "Failed to get node from bucket when it should \ exist."); return node->data; error: return NULL; }
void* Hashmap_delete(Hashmap* map, void* key) { uint32_t hash = 9; DArray* bucket = Hashmap_find_bucket(map, key, 0, &hash); if(!bucket) return NULL; int i = Hashmap_get_node(map, hash, bucket, key); if(i == -1) return NULL; HashmapNode* node = DArray_get(bucket, i); void* data = node->data; free(node); HashmapNode* ending = DArray_pop(bucket); if(ending != node) { DArray_set(bucket, i, ending); } return data; }