tweak: formatting of hash map
parent
e99e391ffa
commit
faf0463e37
14
hash_map.c
14
hash_map.c
|
@ -1,5 +1,6 @@
|
||||||
#include "hash_map.h"
|
#include "hash_map.h"
|
||||||
#include "string.h"
|
#include "string.h"
|
||||||
|
#include "debug.h"
|
||||||
|
|
||||||
HashMap hash_map_from_sizes(size_t key, size_t value, HashFunc hasher) {
|
HashMap hash_map_from_sizes(size_t key, size_t value, HashFunc hasher) {
|
||||||
HashMap self = {
|
HashMap self = {
|
||||||
|
@ -23,9 +24,10 @@ void *hash_map_get_raw(HashMap *self, void *key) {
|
||||||
List bucket = self->buckets[hash % CUTES_HASH_MAP_BUCKETS]; // get the bucket to search
|
List bucket = self->buckets[hash % CUTES_HASH_MAP_BUCKETS]; // get the bucket to search
|
||||||
// linear search through the bucket to find the element
|
// linear search through the bucket to find the element
|
||||||
for(size_t i = 0; i < bucket.len; ++i) {
|
for(size_t i = 0; i < bucket.len; ++i) {
|
||||||
uintptr_t *key_at = list_at(&bucket, i);
|
char *key_at = ((char*)bucket.data) + (bucket.element_size * i);
|
||||||
if(hash == *key_at)
|
if(memcmp(&hash, key_at, sizeof(uintptr_t)) == 0) {
|
||||||
return ((char*)key_at) + sizeof(uintptr_t) + self->key_size;
|
return key_at + sizeof(uintptr_t) + self->key_size;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -34,11 +36,11 @@ void hash_map_insert(HashMap *self, void *key, void *value) {
|
||||||
uintptr_t hash = self->hasher(key);
|
uintptr_t hash = self->hasher(key);
|
||||||
// stage key-value-pair data
|
// stage key-value-pair data
|
||||||
char data[self->buckets[0].element_size];
|
char data[self->buckets[0].element_size];
|
||||||
memcpy(data, &hash, sizeof(uintptr_t)); // copy key hash into start of data
|
memcpy(data, &hash, sizeof(uintptr_t)); // copy key hash into start of data
|
||||||
memcpy(data + sizeof(uintptr_t), key, self->key_size); // copy key after hash
|
memcpy(data + sizeof(uintptr_t), key, self->key_size); // copy key after hash
|
||||||
memcpy(data + sizeof(uintptr_t) + self->key_size, value, self->value_size); // copy value into end of data
|
memcpy(data + sizeof(uintptr_t) + self->key_size, value, self->value_size); // copy value into end of data
|
||||||
// insert staged data into list
|
// insert staged data into list
|
||||||
list_add(self->buckets + (hash % CUTES_HASH_MAP_BUCKETS), data);
|
list_add(&self->buckets[hash % CUTES_HASH_MAP_BUCKETS], data);
|
||||||
}
|
}
|
||||||
|
|
||||||
List hash_map_keys(HashMap *self) {
|
List hash_map_keys(HashMap *self) {
|
||||||
|
|
Loading…
Reference in New Issue