Adding functions table to hashset and fixing root makefile

This commit is contained in:
Leonardo committed 2020-02-06 14:10:12 -03:00
1 parent 7dd6218017
commit fa49fddd8f
5 files changed
+190 -124

No files matched your search

+86 -67
View File
@@ -148,8 +148,8 @@ static const char *cmc_string_fmt_hashset = "struct %s<%s> "
"capacity:%" PRIuMAX ", "
"count:%" PRIuMAX ", "
"load:%lf, "
"cmp:%p, "
"hash:%p, "
"flag:%d, "
"f_val:%p, "
"alloc:%p, "
"callbacks: %p }";
@@ -198,23 +198,23 @@ struct cmc_callbacks_hashset
/* Load factor in range (0.0, 1.0) */ \
double load; \
\
/* Element comparison function */ \
int (*cmp)(V, V); \
/* Flags indicating errors or success */ \
int flag; \
\
/* Element hash function */ \
size_t (*hash)(V); \
\
/* Function that returns an iterator to the start of the hashset */ \
struct SNAME##_iter (*it_start)(struct SNAME *); \
\
/* Function that returns an iterator to the end of the hashset */ \
struct SNAME##_iter (*it_end)(struct SNAME *); \
/* Value function table */ \
struct SNAME##_ftab_val *f_val; \
\
/* Custom allocation functions */ \
struct cmc_alloc_node *alloc; \
\
/* Custom callback functions */ \
struct cmc_callbacks_hashset *callbacks; \
\
/* Function that returns an iterator to the start of the hashset */ \
struct SNAME##_iter (*it_start)(struct SNAME *); \
\
/* Function that returns an iterator to the end of the hashset */ \
struct SNAME##_iter (*it_end)(struct SNAME *); \
}; \
\
struct SNAME##_entry \
@@ -222,14 +222,36 @@ struct cmc_callbacks_hashset
/* Entry element */ \
V value; \
\
/* The distance of this node to its original position, used by \
* robin-hood hashing */ \
/* The distance of this node to its original position, used by */ \
/* robin-hood hashing */ \
size_t dist; \
\
/* The sate of this node (DELETED, EMPTY, FILLED) */ \
enum cmc_entry_state state; \
}; \
\
/* Value struct function table */ \
struct SNAME##_ftab_val \
{ \
/* Comparator function */ \
int (*cmp)(V, V); \
\
/* Copy function */ \
V (*cpy)(V); \
\
/* To string function */ \
bool (*str)(FILE *, V); \
\
/* Free from memory function */ \
void (*free)(V); \
\
/* Hash function */ \
size_t (*hash)(V); \
\
/* Priority function */ \
int (*pri)(V, V); \
}; \
\
/* Hashset Iterator */ \
struct SNAME##_iter \
{ \
@@ -258,13 +280,13 @@ struct cmc_callbacks_hashset
/* Collection Functions */ \
/* Collection Allocation and Deallocation */ \
struct SNAME *PFX##_new(size_t capacity, double load, \
int (*compare)(V, V), size_t (*hash)(V)); \
struct SNAME##_ftab_val *f_val); \
struct SNAME *PFX##_new_custom(size_t capacity, double load, \
int (*compare)(V, V), size_t (*hash)(V), \
struct SNAME##_ftab_val *f_val, \
struct cmc_alloc_node *alloc, \
struct cmc_callbacks_hashset *callbacks); \
void PFX##_clear(struct SNAME *_set_, void (*deallocator)(V)); \
void PFX##_free(struct SNAME *_set_, void (*deallocator)(V)); \
void PFX##_clear(struct SNAME *_set_); \
void PFX##_free(struct SNAME *_set_); \
/* Customization of Allocation and Callbacks */ \
void PFX##_customize(struct SNAME *_set_, struct cmc_alloc_node *alloc, \
struct cmc_callbacks_hashset *callbacks); \
@@ -283,7 +305,7 @@ struct cmc_callbacks_hashset
double PFX##_load(struct SNAME *_set_); \
/* Collection Utility */ \
bool PFX##_resize(struct SNAME *_set_, size_t capacity); \
struct SNAME *PFX##_copy_of(struct SNAME *_set_, V (*copy_func)(V)); \
struct SNAME *PFX##_copy_of(struct SNAME *_set_); \
bool PFX##_equals(struct SNAME *_set1_, struct SNAME *_set2_); \
struct cmc_string PFX##_to_string(struct SNAME *_set_); \
\
@@ -335,7 +357,7 @@ struct cmc_callbacks_hashset
static struct SNAME##_iter PFX##_impl_it_end(struct SNAME *_set_); \
\
struct SNAME *PFX##_new(size_t capacity, double load, \
int (*compare)(V, V), size_t (*hash)(V)) \
struct SNAME##_ftab_val *f_val) \
{ \
struct cmc_alloc_node *alloc = &cmc_alloc_node_default; \
\
@@ -346,6 +368,9 @@ struct cmc_callbacks_hashset
if (capacity >= UINTMAX_MAX * load) \
return NULL; \
\
if (!f_val) \
return NULL; \
\
size_t real_capacity = PFX##_impl_calculate_size(capacity / load); \
\
struct SNAME *_set_ = alloc->malloc(sizeof(struct SNAME)); \
@@ -365,20 +390,18 @@ struct cmc_callbacks_hashset
_set_->count = 0; \
_set_->capacity = real_capacity; \
_set_->load = load; \
_set_->cmp = compare; \
_set_->hash = hash; \
\
_set_->it_start = PFX##_impl_it_start; \
_set_->it_end = PFX##_impl_it_end; \
\
_set_->flag = cmc_flags.OK; \
_set_->f_val = f_val; \
_set_->alloc = alloc; \
_set_->callbacks = NULL; \
_set_->it_start = PFX##_impl_it_start; \
_set_->it_end = PFX##_impl_it_end; \
\
return _set_; \
} \
\
struct SNAME *PFX##_new_custom( \
size_t capacity, double load, int (*compare)(V, V), size_t (*hash)(V), \
size_t capacity, double load, struct SNAME##_ftab_val *f_val, \
struct cmc_alloc_node *alloc, struct cmc_callbacks_hashset *callbacks) \
{ \
if (capacity == 0 || load <= 0 || load >= 1) \
@@ -388,6 +411,9 @@ struct cmc_callbacks_hashset
if (capacity >= UINTMAX_MAX * load) \
return NULL; \
\
if (!f_val) \
return NULL; \
\
size_t real_capacity = PFX##_impl_calculate_size(capacity / load); \
\
if (!alloc) \
@@ -410,21 +436,19 @@ struct cmc_callbacks_hashset
_set_->count = 0; \
_set_->capacity = real_capacity; \
_set_->load = load; \
_set_->cmp = compare; \
_set_->hash = hash; \
\
_set_->it_start = PFX##_impl_it_start; \
_set_->it_end = PFX##_impl_it_end; \
\
_set_->flag = cmc_flags.OK; \
_set_->f_val = f_val; \
_set_->alloc = alloc; \
_set_->callbacks = callbacks; \
_set_->it_start = PFX##_impl_it_start; \
_set_->it_end = PFX##_impl_it_end; \
\
return _set_; \
} \
\
void PFX##_clear(struct SNAME *_set_, void (*deallocator)(V)) \
void PFX##_clear(struct SNAME *_set_) \
{ \
if (deallocator) \
if (_set_->f_val->free) \
{ \
for (size_t i = 0; i < _set_->capacity; i++) \
{ \
@@ -432,7 +456,7 @@ struct cmc_callbacks_hashset
\
if (entry->state == CMC_ES_FILLED) \
{ \
deallocator(entry->value); \
_set_->f_val->free(entry->value); \
} \
} \
} \
@@ -443,9 +467,9 @@ struct cmc_callbacks_hashset
_set_->count = 0; \
} \
\
void PFX##_free(struct SNAME *_set_, void (*deallocator)(V)) \
void PFX##_free(struct SNAME *_set_) \
{ \
if (deallocator) \
if (_set_->f_val->free) \
{ \
for (size_t i = 0; i < _set_->capacity; i++) \
{ \
@@ -453,7 +477,7 @@ struct cmc_callbacks_hashset
\
if (entry->state == CMC_ES_FILLED) \
{ \
deallocator(entry->value); \
_set_->f_val->free(entry->value); \
} \
} \
} \
@@ -480,7 +504,7 @@ struct cmc_callbacks_hashset
return false; \
} \
\
size_t hash = _set_->hash(element); \
size_t hash = _set_->f_val->hash(element); \
size_t original_pos = hash % _set_->capacity; \
size_t pos = original_pos; \
\
@@ -561,7 +585,7 @@ struct cmc_callbacks_hashset
\
if (index == 0) \
*value = result; \
else if (_set_->cmp(result, *value) > 0) \
else if (_set_->f_val->cmp(result, *value) > 0) \
*value = result; \
} \
\
@@ -583,7 +607,7 @@ struct cmc_callbacks_hashset
\
if (index == 0) \
*value = result; \
else if (_set_->cmp(result, *value) < 0) \
else if (_set_->f_val->cmp(result, *value) < 0) \
*value = result; \
} \
\
@@ -641,7 +665,7 @@ struct cmc_callbacks_hashset
return false; \
\
struct SNAME *_new_set_ = \
PFX##_new_custom(capacity, _set_->load, _set_->cmp, _set_->hash, \
PFX##_new_custom(capacity, _set_->load, _set_->f_val, \
_set_->alloc, _set_->callbacks); \
\
if (!_new_set_) \
@@ -659,7 +683,7 @@ struct cmc_callbacks_hashset
\
if (PFX##_count(_set_) != PFX##_count(_new_set_)) \
{ \
PFX##_free(_new_set_, NULL); \
PFX##_free(_new_set_); \
return false; \
} \
\
@@ -671,21 +695,21 @@ struct cmc_callbacks_hashset
_set_->capacity = _new_set_->capacity; \
_new_set_->capacity = tmp_c; \
\
PFX##_free(_new_set_, NULL); \
PFX##_free(_new_set_); \
\
return true; \
} \
\
struct SNAME *PFX##_copy_of(struct SNAME *_set_, V (*copy_func)(V)) \
struct SNAME *PFX##_copy_of(struct SNAME *_set_) \
{ \
struct SNAME *result = \
PFX##_new_custom(_set_->capacity, _set_->load, _set_->cmp, \
_set_->hash, _set_->alloc, _set_->callbacks); \
PFX##_new_custom(_set_->capacity, _set_->load, _set_->f_val, \
_set_->alloc, _set_->callbacks); \
\
if (!result) \
return NULL; \
\
if (copy_func) \
if (_set_->f_val->cpy) \
{ \
for (size_t i = 0; i < _set_->capacity; i++) \
{ \
@@ -702,7 +726,7 @@ struct cmc_callbacks_hashset
target->state = scan->state; \
target->dist = scan->dist; \
\
target->value = copy_func(scan->value); \
target->value = _set_->f_val->cpy(scan->value); \
} \
} \
} \
@@ -745,21 +769,16 @@ struct cmc_callbacks_hashset
int n = \
snprintf(str.s, cmc_string_len, cmc_string_fmt_hashset, #SNAME, \
#V, s_, s_->buffer, s_->capacity, s_->count, s_->load, \
s_->cmp, s_->hash, s_->alloc, s_->callbacks); \
s_->flag, s_->f_val, s_->alloc, s_->callbacks); \
\
if (n < 0 || n == (int)cmc_string_len) \
return (struct cmc_string){ 0 }; \
\
str.s[n] = '\0'; \
\
return str; \
return n >= 0 ? str : (struct cmc_string){ 0 }; \
} \
\
struct SNAME *PFX##_union(struct SNAME *_set1_, struct SNAME *_set2_) \
{ \
struct SNAME *_set_r_ = \
PFX##_new_custom(_set1_->capacity, _set1_->load, _set1_->cmp, \
_set1_->hash, _set1_->alloc, _set1_->callbacks); \
PFX##_new_custom(_set1_->capacity, _set1_->load, _set1_->f_val, \
_set1_->alloc, _set1_->callbacks); \
\
if (!_set_r_) \
return NULL; \
@@ -787,8 +806,8 @@ struct cmc_callbacks_hashset
struct SNAME *_set2_) \
{ \
struct SNAME *_set_r_ = \
PFX##_new_custom(_set1_->capacity, _set1_->load, _set1_->cmp, \
_set1_->hash, _set1_->alloc, _set1_->callbacks); \
PFX##_new_custom(_set1_->capacity, _set1_->load, _set1_->f_val, \
_set1_->alloc, _set1_->callbacks); \
\
if (!_set_r_) \
return NULL; \
@@ -815,8 +834,8 @@ struct cmc_callbacks_hashset
struct SNAME *PFX##_difference(struct SNAME *_set1_, struct SNAME *_set2_) \
{ \
struct SNAME *_set_r_ = \
PFX##_new_custom(_set1_->capacity, _set1_->load, _set1_->cmp, \
_set1_->hash, _set1_->alloc, _set1_->callbacks); \
PFX##_new_custom(_set1_->capacity, _set1_->load, _set1_->f_val, \
_set1_->alloc, _set1_->callbacks); \
\
if (!_set_r_) \
return NULL; \
@@ -842,8 +861,8 @@ struct cmc_callbacks_hashset
struct SNAME##_iter iter1, iter2; \
\
struct SNAME *_set_r_ = \
PFX##_new_custom(_set1_->capacity, _set1_->load, _set1_->cmp, \
_set1_->hash, _set1_->alloc, _set1_->callbacks); \
PFX##_new_custom(_set1_->capacity, _set1_->load, _set1_->f_val, \
_set1_->alloc, _set1_->callbacks); \
\
if (!_set_r_) \
return NULL; \
@@ -1195,7 +1214,7 @@ struct cmc_callbacks_hashset
static struct SNAME##_entry *PFX##_impl_get_entry(struct SNAME *_set_, \
V element) \
{ \
size_t hash = _set_->hash(element); \
size_t hash = _set_->f_val->hash(element); \
size_t pos = hash % _set_->capacity; \
\
struct SNAME##_entry *target = &(_set_->buffer[pos]); \
@@ -1203,7 +1222,7 @@ struct cmc_callbacks_hashset
while (target->state == CMC_ES_FILLED || \
target->state == CMC_ES_DELETED) \
{ \
if (_set_->cmp(target->value, element) == 0) \
if (_set_->f_val->cmp(target->value, element) == 0) \
return target; \
\
pos++; \