From c23041781b3cb6c4f72e4ad4d11a8f051e7428fe Mon Sep 17 00:00:00 2001 From: Justin Ethier Date: Sat, 9 Jan 2021 07:43:43 -0800 Subject: [PATCH] Build out CK hs functions --- ck-polyfill.c | 55 ++++++++++++++++++++++++++++----------------------- ck-polyfill.h | 19 +++++++++++++++++- 2 files changed, 48 insertions(+), 26 deletions(-) diff --git a/ck-polyfill.c b/ck-polyfill.c index bd8cc48c..534839ba 100644 --- a/ck-polyfill.c +++ b/ck-polyfill.c @@ -36,12 +36,38 @@ bool ck_hs_init(ck_hs_t *hs, unsigned int mode, ck_hs_hash_cb_t *hash_func, return true; } -void *ck_hs_get(ck_hs_t *hs, unsigned long hash, const void *key) +void *ck_hs_get(ck_hs_t *_hs, unsigned long hash, const void *key) { + void *result = NULL; + int index = -1; + simple_hashset_t set = (*_hs).hs; + + pthread_mutex_lock(&((*_hs).lock)); + + index = simple_hashset_is_member(set, (symbol_type *)key); + if (index > 0) { + result = (void *)(set->items[index].item); + } + + pthread_mutex_unlock(&((*_hs).lock)); + return result; } -bool ck_hs_put(ck_hs_t *hs, unsigned long hash, const void *key) +bool ck_hs_put(ck_hs_t *_hs, unsigned long hash, const void *key) { + bool result = false; + int rv; + simple_hashset_t hs = (*_hs).hs; + + pthread_mutex_lock(&((*_hs).lock)); + + rv = simple_hashset_add(hs, (symbol_type *)key); + if (rv >= 0) { + result = true; + } + + pthread_mutex_unlock(&((*_hs).lock)); + return result; } // CK Array section @@ -218,23 +244,6 @@ ck_pr_load_8(const uint8_t *target) static const size_t prime_1 = 73; static const size_t prime_2 = 5009; -struct simple_hashset_item_st { - size_t hash; - symbol_type* item; -}; - -struct simple_hashset_st { - size_t nbits; - size_t mask; - - size_t capacity; - struct simple_hashset_item_st *items; - size_t nitems; - size_t n_deleted_items; - - hash_func_t hash_func; -}; - size_t hash_function(const char* p, size_t len) { size_t hash = 0; @@ -325,9 +334,7 @@ static void set_maybe_rehash(simple_hashset_t set) int simple_hashset_add(simple_hashset_t set, symbol_type* key) { -// TODO: get from symbol type:, size_t key_len) -size_t key_len = strlen(key->desc); - + size_t key_len = strlen(key->desc); size_t hash = set->hash_func(key->desc, key_len); int rv = simple_hashset_add_member(set, key, hash); set_maybe_rehash(set); @@ -336,9 +343,7 @@ size_t key_len = strlen(key->desc); int simple_hashset_is_member(simple_hashset_t set, symbol_type* key) { -// TODO: get from symbol type, size_t key_len) -size_t key_len = strlen(key->desc); - + size_t key_len = strlen(key->desc); size_t hash = set->hash_func(key->desc, key_len); size_t index = set->mask & (prime_1 * hash); diff --git a/ck-polyfill.h b/ck-polyfill.h index cfc5138f..49a3b210 100644 --- a/ck-polyfill.h +++ b/ck-polyfill.h @@ -19,9 +19,26 @@ struct ck_malloc { /* hash function */ typedef size_t(*hash_func_t)(const char*, size_t); - struct simple_hashset_st; + struct simple_hashset_item_st { + size_t hash; + symbol_type* item; + }; + + struct simple_hashset_st { + size_t nbits; + size_t mask; + + size_t capacity; + struct simple_hashset_item_st *items; + size_t nitems; + size_t n_deleted_items; + + hash_func_t hash_func; + }; +// struct simple_hashset_st; typedef struct simple_hashset_st *simple_hashset_t; + struct hashmap_st; typedef struct hashmap_st *hashmap_t;