hashmap_get

This commit is contained in:
2020-08-07 12:48:41 +02:00
parent 064885298f
commit bb988bbef1
2 changed files with 19 additions and 10 deletions

View File

@ -2,7 +2,7 @@
#define _acl_hashmap_h
#include <stddef.h>
#include <stdbool.h>
#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

View File

@ -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;
}