From bb988bbef106ec4fd8c22911bb50aabe5567ffd8 Mon Sep 17 00:00:00 2001 From: MrGeorgen Date: Fri, 7 Aug 2020 12:48:41 +0200 Subject: [PATCH] hashmap_get --- include/acl/hashmap.h | 5 +++-- src/hashmap.c | 24 ++++++++++++++++-------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/include/acl/hashmap.h b/include/acl/hashmap.h index 58aed5b..f5dcbc3 100644 --- a/include/acl/hashmap.h +++ b/include/acl/hashmap.h @@ -2,7 +2,7 @@ #define _acl_hashmap_h #include #include -#define acl_hashmap_create(type, keyType, bucketCount) acl_hashmap_init(bucketCount, sizeof(struct{type c; type d;}), offsetof(struct{type c; type d;}, d), sizeof(keyType)); +#define acl_hashmap_create(type, keyType, bucketCount) acl_hashmap_init(bucketCount, sizeof(struct{keyType c; type d;}), offsetof(struct{keyType c; type d;}, d), sizeof(keyType)) union acl_hashmap_meta { void *dummy_ptr; struct { @@ -13,5 +13,6 @@ union acl_hashmap_meta { }; }; union acl_hashmap_meta* acl_hashmap_init(size_t bucketCount, size_t sizeOneElement, size_t offset, size_t keySize); -void* acl_hashmap_put(union acl_hashmap_meta *hashmap_meta, void *key); +void* acl_hashmap_declare(union acl_hashmap_meta *hashmap_meta, void *key); +void* acl_hashmap_get(union acl_hashmap_meta *hashmap_meta, void *key); #endif diff --git a/src/hashmap.c b/src/hashmap.c index b9448bc..4d11271 100644 --- a/src/hashmap.c +++ b/src/hashmap.c @@ -21,11 +21,14 @@ *hash_char ^= *i;\ }\ } -inline static void* acl_hashmap_elementFromBucketAndKey(void *bucket, void *key, union acl_hashmap_meta *hashmap_meta) { - char *i = bucket; - for(char *dest = bucket + acl_arraylist_len(bucket); i < dest; i += hashmap_meta->sizeOneElement) { +static size_t acl_hash(void *data, size_t dataSize, size_t bucketBits); +inline static void* acl_hashmap_elementFromBucketAndKey(void **bucket, void *key, union acl_hashmap_meta *hashmap_meta) { + void **hashmap_buckets = (void**)(hashmap_meta + 1); + *bucket = hashmap_buckets[acl_hash(key, hashmap_meta->keySize, hashmap_meta->bucketBits)]; + char *i = *bucket; + for(char *dest = *bucket + acl_arraylist_len(*bucket); i < dest; i += hashmap_meta->sizeOneElement) { if(!memcmp(key, i, hashmap_meta->keySize)) { - return i + hashmap_meta->offset; + return i; } } return NULL; @@ -81,10 +84,9 @@ union acl_hashmap_meta* acl_hashmap_init(size_t bucketCount, size_t sizeOneEleme return hashmap_meta; } -void* acl_hashmap_put(union acl_hashmap_meta *hashmap_meta, void *key) { - void **hashmap_buckets = (void**)(hashmap_meta + 1); - void *bucket = hashmap_buckets[acl_hash(key, hashmap_meta->keySize, hashmap_meta->bucketBits)]; - if(acl_hashmap_elementFromBucketAndKey(bucket, key, hashmap_meta) != NULL) return NULL; +void* acl_hashmap_declare(union acl_hashmap_meta *hashmap_meta, void *key) { + void *bucket; + if(acl_hashmap_elementFromBucketAndKey(&bucket, key, hashmap_meta) != NULL) return NULL; void *elementKey; // pointer to the key inside the bucket void *bucketTmp = acl_arraylist_append_ptr(bucket, &elementKey); if(bucketTmp == NULL) return NULL; @@ -92,3 +94,9 @@ void* acl_hashmap_put(union acl_hashmap_meta *hashmap_meta, void *key) { memcpy(elementKey, key, hashmap_meta->keySize); return (char*)elementKey + hashmap_meta->offset; } +void* acl_hashmap_get(union acl_hashmap_meta *hashmap_meta, void *key) { + void *bucket; + char *elementKey = acl_hashmap_elementFromBucketAndKey(&bucket, key, hashmap_meta); + if(elementKey == NULL) return NULL; + return elementKey + hashmap_meta->offset; +}