mirror of
https://github.com/justinethier/cyclone.git
synced 2025-07-06 20:56:36 +02:00
Integrate simple hashset of symbols
This commit is contained in:
parent
c9f4d8243c
commit
e0b11d2f43
2 changed files with 46 additions and 75 deletions
|
@ -24,6 +24,26 @@ void ck_polyfill_init()
|
|||
}
|
||||
}
|
||||
|
||||
// CK Hashset section
|
||||
bool ck_hs_init(ck_hs_t *hs, unsigned int mode, ck_hs_hash_cb_t *hash_func,
|
||||
ck_hs_compare_cb_t *cmp, struct ck_malloc *alloc, unsigned long capacity, unsigned long seed)
|
||||
{
|
||||
(*hs).hs = simple_hashset_create();
|
||||
if (pthread_mutex_init(&((*hs).lock), NULL) != 0) {
|
||||
fprintf(stderr, "Unable to initialize ck hashset mutex\n");
|
||||
exit(1);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void *ck_hs_get(ck_hs_t *hs, unsigned long hash, const void *key)
|
||||
{
|
||||
}
|
||||
|
||||
bool ck_hs_put(ck_hs_t *hs, unsigned long hash, const void *key)
|
||||
{
|
||||
}
|
||||
|
||||
// CK Array section
|
||||
bool
|
||||
ck_array_init(ck_array_t *array, unsigned int mode,
|
||||
|
@ -200,7 +220,7 @@ static const size_t prime_2 = 5009;
|
|||
|
||||
struct simple_hashset_item_st {
|
||||
size_t hash;
|
||||
char* item;
|
||||
symbol_type* item;
|
||||
};
|
||||
|
||||
struct simple_hashset_st {
|
||||
|
@ -215,7 +235,7 @@ struct simple_hashset_st {
|
|||
hash_func_t hash_func;
|
||||
};
|
||||
|
||||
size_t hash_function(char* p, size_t len)
|
||||
size_t hash_function(const char* p, size_t len)
|
||||
{
|
||||
size_t hash = 0;
|
||||
for (; *p; ++p)
|
||||
|
@ -245,35 +265,12 @@ simple_hashset_t simple_hashset_create()
|
|||
return set;
|
||||
}
|
||||
|
||||
void simple_hashset_clean(simple_hashset_t set)
|
||||
{
|
||||
set->nitems = 0;
|
||||
set->n_deleted_items = 0;
|
||||
|
||||
size_t i = 0;
|
||||
while(i != set->capacity)
|
||||
set->items[i++].hash = 0;
|
||||
}
|
||||
|
||||
size_t simple_hashset_num_items(simple_hashset_t set)
|
||||
{
|
||||
return set->nitems;
|
||||
}
|
||||
|
||||
void simple_hashset_destroy(simple_hashset_t set)
|
||||
{
|
||||
if (set) {
|
||||
free(set->items);
|
||||
}
|
||||
free(set);
|
||||
}
|
||||
|
||||
void simple_hashset_set_hash_function(simple_hashset_t set, hash_func_t func)
|
||||
{
|
||||
set->hash_func = func;
|
||||
}
|
||||
|
||||
static int simple_hashset_add_member(simple_hashset_t set, char* key, size_t hash)
|
||||
static int simple_hashset_add_member(simple_hashset_t set, symbol_type* key, size_t hash)
|
||||
{
|
||||
size_t index;
|
||||
|
||||
|
@ -326,40 +323,28 @@ static void set_maybe_rehash(simple_hashset_t set)
|
|||
}
|
||||
}
|
||||
|
||||
int simple_hashset_add(simple_hashset_t set, char* key, size_t key_len)
|
||||
int simple_hashset_add(simple_hashset_t set, symbol_type* key)
|
||||
{
|
||||
size_t hash = set->hash_func(key, key_len);
|
||||
// TODO: get from symbol type:, size_t key_len)
|
||||
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);
|
||||
return rv;
|
||||
}
|
||||
|
||||
int simple_hashset_remove(simple_hashset_t set, char* key, size_t key_len)
|
||||
int simple_hashset_is_member(simple_hashset_t set, symbol_type* key)
|
||||
{
|
||||
size_t hash = set->hash_func(key, key_len);
|
||||
// TODO: get from symbol type, size_t key_len)
|
||||
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);
|
||||
|
||||
while (set->items[index].hash != 0) {
|
||||
if (set->items[index].hash == hash) {
|
||||
set->items[index].hash = 1;
|
||||
--set->nitems;
|
||||
++set->n_deleted_items;
|
||||
return 1;
|
||||
} else {
|
||||
index = set->mask & (index + prime_2);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int simple_hashset_is_member(simple_hashset_t set, char* key, size_t key_len)
|
||||
{
|
||||
size_t hash = set->hash_func(key, key_len);
|
||||
size_t index = set->mask & (prime_1 * hash);
|
||||
|
||||
while (set->items[index].hash != 0) {
|
||||
if (set->items[index].hash == hash) {
|
||||
return 1;
|
||||
return index;
|
||||
} else {
|
||||
index = set->mask & (index + prime_2);
|
||||
}
|
||||
|
|
|
@ -8,10 +8,16 @@
|
|||
|
||||
void ck_polyfill_init();
|
||||
|
||||
struct ck_malloc {
|
||||
void *(*malloc)(size_t);
|
||||
void *(*realloc)(void *, size_t, size_t, bool);
|
||||
void (*free)(void *, size_t, bool);
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Simple hashset (hashset with string support)
|
||||
/* hash function */
|
||||
typedef size_t(*hash_func_t)(char*, size_t);
|
||||
typedef size_t(*hash_func_t)(const char*, size_t);
|
||||
|
||||
struct simple_hashset_st;
|
||||
typedef struct simple_hashset_st *simple_hashset_t;
|
||||
|
@ -32,12 +38,6 @@ void ck_polyfill_init();
|
|||
/* set hash function */
|
||||
void simple_hashset_set_hash_function(simple_hashset_t set, hash_func_t func);
|
||||
|
||||
/* Just clear data but do not create anything*/
|
||||
void simple_hashset_clean(simple_hashset_t set);
|
||||
|
||||
/* total items count */
|
||||
size_t simple_hashset_num_items(simple_hashset_t set);
|
||||
|
||||
/* add item into the hashset.
|
||||
*
|
||||
* @note 0 and 1 is special values, meaning nil and deleted items. the
|
||||
|
@ -45,20 +45,13 @@ void ck_polyfill_init();
|
|||
*
|
||||
* returns zero if the item already in the set and non-zero otherwise
|
||||
*/
|
||||
int simple_hashset_add(simple_hashset_t set, char* key, size_t key_len);
|
||||
|
||||
/* remove item from the hashset
|
||||
*
|
||||
* returns non-zero if the item was removed and zero if the item wasn't
|
||||
* exist
|
||||
*/
|
||||
int simple_hashset_remove(simple_hashset_t set, char *key, size_t key_len);
|
||||
int simple_hashset_add(simple_hashset_t set, symbol_type* key);
|
||||
|
||||
/* check if existence of the item
|
||||
*
|
||||
* returns non-zero if the item exists and zero otherwise
|
||||
*/
|
||||
int simple_hashset_is_member(simple_hashset_t set, char* key, size_t key_len);
|
||||
int simple_hashset_is_member(simple_hashset_t set, symbol_type* key);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// CK Hashset section
|
||||
|
@ -67,7 +60,8 @@ void ck_polyfill_init();
|
|||
#define CK_HS_MODE_SPMC 0
|
||||
|
||||
struct ck_hs {
|
||||
// TODO
|
||||
pthread_mutex_t lock;
|
||||
simple_hashset_t hs;
|
||||
};
|
||||
|
||||
typedef struct ck_hs ck_hs_t;
|
||||
|
@ -82,15 +76,13 @@ typedef unsigned long ck_hs_hash_cb_t(const void *, unsigned long);
|
|||
*/
|
||||
typedef bool ck_hs_compare_cb_t(const void *, const void *);
|
||||
|
||||
/*
|
||||
CK_HS_HASH(hs, hs_hash, value);
|
||||
#define CK_HS_HASH(hs, hs_hash, value) 0
|
||||
|
||||
bool ck_hs_init(ck_hs_t *, unsigned int, ck_hs_hash_cb_t *,
|
||||
ck_hs_compare_cb_t *, struct ck_malloc *, unsigned long, unsigned long);
|
||||
|
||||
void *ck_hs_get(ck_hs_t *, unsigned long, const void *);
|
||||
bool ck_hs_put(ck_hs_t *, unsigned long, const void *);
|
||||
*/
|
||||
|
||||
/*
|
||||
struct ck_hs {
|
||||
|
@ -119,12 +111,6 @@ struct ck_array_iterator {
|
|||
};
|
||||
typedef struct ck_array_iterator ck_array_iterator_t;
|
||||
|
||||
struct ck_malloc {
|
||||
void *(*malloc)(size_t);
|
||||
void *(*realloc)(void *, size_t, size_t, bool);
|
||||
void (*free)(void *, size_t, bool);
|
||||
};
|
||||
|
||||
#define CK_ARRAY_MODE_SPMC 0
|
||||
|
||||
// DESCRIPTION
|
||||
|
|
Loading…
Add table
Reference in a new issue