diff options
author | dec05eba <dec05eba@protonmail.com> | 2019-09-09 01:08:34 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-07-25 14:36:46 +0200 |
commit | 16aaaa19a3ef4220726007d3e644ced0c9e06513 (patch) | |
tree | 6a7c2fc91bc362559b079afbb10dc247d7bcbae0 /src/std/hash_map.c | |
parent | de48503aef098d855754ab6f85558402130188d7 (diff) |
Allow referencing code in imported file (right now for function calls, allow calling a function in another file)
Diffstat (limited to 'src/std/hash_map.c')
-rw-r--r-- | src/std/hash_map.c | 32 |
1 files changed, 32 insertions, 0 deletions
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; @@ -200,6 +201,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; } bool hash_map_contains(HashMap *self, BufferView key) { |