aboutsummaryrefslogtreecommitdiff
path: root/src/std/hash_map.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/std/hash_map.c')
-rw-r--r--src/std/hash_map.c32
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) {