From 16aaaa19a3ef4220726007d3e644ced0c9e06513 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 9 Sep 2019 01:08:34 +0200 Subject: Allow referencing code in imported file (right now for function calls, allow calling a function in another file) --- src/std/hash_map.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'src/std/hash_map.c') diff --git a/src/std/hash_map.c b/src/std/hash_map.c index 98ebf40..234f3e3 100644 --- a/src/std/hash_map.c +++ b/src/std/hash_map.c @@ -178,6 +178,7 @@ int hash_map_insert(HashMap *self, BufferView key, void *value) { } bool hash_map_get(HashMap *self, BufferView key, void *value) { +#if 0 usize bucket_size; usize bucket_index; usize hash; @@ -199,6 +200,37 @@ bool hash_map_get(HashMap *self, BufferView key, void *value) { } } + return bool_false; +#endif + void *ref; + if(!hash_map_get_ref(self, key, &ref)) + return bool_false; + am_memcpy(value, ref, self->value_type_size); + return bool_true; +} + +bool hash_map_get_ref(HashMap *self, BufferView key, void **value) { + usize bucket_size; + usize bucket_index; + usize hash; + HashMapBucket *bucket; + HashMapBucketNode *bucket_node; + + bucket_size = buffer_get_size(&self->buckets, HashMapBucket); + hash = self->hash_func((const u8*)key.data, key.size); + bucket_index = hash % bucket_size; + + bucket = ((HashMapBucket*)self->buckets.data) + bucket_index; + 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); + if(hash == bucket_node_get_hash(bucket_node) && self->compare_func(&key, &bucket_key) == 0) { + if(value) + *value = bucket_node_get_value(bucket_node); + return bool_true; + } + } + return bool_false; } -- cgit v1.2.3