From 4b2b8d3176e84f76510cc69a627dbfa089c1dd35 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 25 Jan 2020 09:48:56 +0100 Subject: Implement almost all instructions --- src/std_gc/hash_map.c | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) (limited to 'src/std_gc/hash_map.c') diff --git a/src/std_gc/hash_map.c b/src/std_gc/hash_map.c index 1960ffc..4d4c026 100644 --- a/src/std_gc/hash_map.c +++ b/src/std_gc/hash_map.c @@ -1,4 +1,5 @@ #include "../../include/std_gc/hash_map.h" +#include "../../include/value.h" #include #include #include @@ -127,9 +128,6 @@ static int tsl_hash_map_ensure_bucket_capacity_for_one_new_item(TslHashMap *self } } - if(old_capacity == 0) - return 1; - tsl_hash_map_reorder_nodes(self, old_capacity); return 1; } @@ -169,23 +167,26 @@ TslValue* tsl_hash_map_get(TslHashMap *self, const TslValue *key) { TslValue* tsl_hash_map_get_or_create(TslHashMap *self, const TslValue *key) { uint64_t hash; size_t index; - TslHashMapNode **bucket; - TslHashMapNode *node; TslValue *value; - if(self->buckets_capacity == 0) { - if(!tsl_hash_map_ensure_bucket_capacity_for_one_new_item(self)) - return NULL; - } + TslHashMapNode **bucket; hash = tsl_value_hash(key); + if(self->buckets_capacity > 0) { + size_t index = tsl_hash_map_get_index(self, hash); + TslHashMapNode **bucket = (TslHashMapNode**)self->buckets_data + index; + TslHashMapNode *node = *bucket; + while(node) { + if(hash == node->hash && tsl_value_equals(key, &node->key)) + return &node->value; + node = node->next; + } + } + + if(!tsl_hash_map_ensure_bucket_capacity_for_one_new_item(self)) + return NULL; + index = tsl_hash_map_get_index(self, hash); bucket = (TslHashMapNode**)self->buckets_data + index; - node = *bucket; - while(node) { - if(hash == node->hash && tsl_value_equals(key, &node->key)) - return &node->value; - node = node->next; - } if(!tsl_hash_map_append_bucket(bucket, hash, key, NULL, &value)) return NULL; -- cgit v1.2.3