diff options
-rw-r--r-- | src/std/hash_map.c | 19 | ||||
-rw-r--r-- | tests/main.c | 1 |
2 files changed, 8 insertions, 12 deletions
diff --git a/src/std/hash_map.c b/src/std/hash_map.c index 7c3d943..9f58f6f 100644 --- a/src/std/hash_map.c +++ b/src/std/hash_map.c @@ -105,13 +105,6 @@ static void hash_map_reorder_nodes(HashMap *self, usize end_index) { BufferView bucket_key; usize bucket_index; - /* Node between current and previous has been removed, chain previous and current */ - if(prev_bucket_node && !bucket_node_get_next(prev_bucket_node)) - bucket_node_set_next(prev_bucket_node, bucket_node); - - if(!prev_bucket_node) - bucket->start = bucket_node; - bucket_key = bucket_node_get_key(bucket_node); bucket_index = self->hash_func((const u8*)bucket_key.data, bucket_key.size) % bucket_size; if(bucket_index != index) { @@ -125,7 +118,9 @@ static void hash_map_reorder_nodes(HashMap *self, usize end_index) { new_bucket->start = moved_node; /* Mark bucket node to be removed bucket by dechaining */ if(prev_bucket_node) - bucket_node_set_next(prev_bucket_node, NULL); + bucket_node_set_next(prev_bucket_node, bucket_node); + else + bucket->start = bucket_node; } else { prev_bucket_node = bucket_node; bucket_node = bucket_node_get_next(bucket_node); @@ -177,13 +172,13 @@ bool hash_map_get(HashMap *self, BufferView key, void *value) { bucket_index = self->hash_func((const u8*)key.data, key.size) % bucket_size; bucket = ((HashMapBucket*)self->buckets.data) + bucket_index; - for(bucket_node = bucket->start; bucket_node; bucket_node_get_next(bucket_node)) { + for(bucket_node = bucket->start; bucket_node; bucket_node = bucket_node_get_next(bucket_node)) { BufferView bucket_key; bucket_key = bucket_node_get_key(bucket_node); - bucket_index = self->hash_func((const u8*)bucket_key.data, bucket_key.size) % bucket_size; - bucket_index %= bucket_size; - if(self->compare_func(value, bucket_node_get_value(bucket_node)) == 0) + if(self->compare_func(&key, &bucket_key) == 0) { + am_memcpy(value, bucket_node_get_value(bucket_node), self->type_size); return bool_true; + } } return bool_false; diff --git a/tests/main.c b/tests/main.c index 11719a8..8e45ba4 100644 --- a/tests/main.c +++ b/tests/main.c @@ -25,6 +25,7 @@ static CHECK_RESULT int test_hash_map() { value = 34; return_if_error(hash_map_insert(&hash_map, create_buffer_view("hello", 5), &value)); + value = 50; for(i = 0; i < 128; ++i) return_if_error(hash_map_insert(&hash_map, create_buffer_view((const char*)&i, 1), &value)); return_if_error(hash_map_insert(&hash_map, create_buffer_view("hellp", 5), &value)); |