From 0180dd1abd14949194aadb14b4a3d1ccf5749af8 Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Fri, 24 Mar 2023 21:25:42 +0300 Subject: [PATCH] Update vnode (vtype_variable) --- include/vtype.h | 6 +- src/__internal/include.h | 8 +-- src/__internal/vnode.h | 62 ++++++++++--------- src/array/compute.c | 4 +- src/array/modify.c | 6 +- src/dict/access.c | 2 +- src/dict/modify.c | 30 ++++++---- src/list/modify.c | 6 +- src/map/comparison.c | 2 +- src/map/modify.c | 23 +++---- src/set/modify.c | 4 +- src/stringify.c | 2 +- src/vnode.c | 125 +++++++++++++++++++-------------------- src/vtype.c | 33 +++++------ tests/src/test.c | 2 +- 15 files changed, 161 insertions(+), 154 deletions(-) diff --git a/include/vtype.h b/include/vtype.h index 8468322..693c436 100644 --- a/include/vtype.h +++ b/include/vtype.h @@ -80,10 +80,10 @@ typedef struct libcdsb_string vtype_string; typedef struct libcdsb_variable vtype_variable; -extern const char* libcdsb_vtype_name (vtype t) Warn_unused_result__; -extern const char* libcdsb_vtype_stringify(vtype_variable value) Warn_unused_result__; +extern const char* libcdsb_vtype_name(vtype t) Warn_unused_result__; -inline vtype_variable libcdsb_variable_build(void* value, vtype t) Always_inline__; +extern const char* libcdsb_variable_stringify(vtype_variable value) Warn_unused_result__; +inline vtype_variable libcdsb_variable_build (void* value, vtype t) Always_inline__; /*#####################################################################################################################*/ diff --git a/src/__internal/include.h b/src/__internal/include.h index 5593756..cd185c6 100644 --- a/src/__internal/include.h +++ b/src/__internal/include.h @@ -61,7 +61,7 @@ typedef vtype_hash hash_t; extern const size_t LIBCDSB_BUILTIN_VTYPE_SIZES[19]; extern int libcdsb_builtin_variable_compare(vtype_variable s0, vtype_variable s1) pure__ wur__; -extern hash_t libcdsb_builtin_vtype_hash (const void* value, vtype type) pure__ wur__; +extern hash_t libcdsb_builtin_variable_hash (vtype_variable value) pure__ wur__; ainline(stack_t* libcdsb_builtin_stack_insert(stack_t* x, void* v)) { stack_t* p = x->prev; @@ -92,11 +92,11 @@ ainline(stack_t* libcdsb_builtin_stack_insert(stack_t* x, void* v)) { #define strndup libcdsb_strndup #define memndup libcdsb_memndup -#define vtype_stringify libcdsb_vtype_stringify #define vtype_name libcdsb_vtype_name -#define vtype_hash libcdsb_builtin_vtype_hash #define vtype_size(type) (LIBCDSB_BUILTIN_VTYPE_SIZES[type]) -#define variable_compare libcdsb_builtin_variable_compare +#define variable_stringify libcdsb_variable_stringify +#define variable_hash libcdsb_builtin_variable_hash +#define variable_compare libcdsb_builtin_variable_compare #endif /* LIBCDSB_SRC_INTERNAL_INCLUDE */ diff --git a/src/__internal/vnode.h b/src/__internal/vnode.h index 4d3502b..8f8bb59 100644 --- a/src/__internal/vnode.h +++ b/src/__internal/vnode.h @@ -19,53 +19,51 @@ typedef union { typedef void* vnode_t; -extern vnode_t libcdsb_builtin_vnode_create (const void* value, vtype type) wur__; -extern vnode_t libcdsb_builtin_vnode_create_target(vtype target_type, const void* value, vtype type) wur__; - -extern void libcdsb_builtin_vnode_free(vnode_t* x, vtype type) Nonnull__(1); +extern vnode_t libcdsb_builtin_vnode_create ( vtype_variable) wur__; +extern vnode_t libcdsb_builtin_vnode_create_ex(vtype xt, vtype_variable) wur__; extern vtype_variable libcdsb_builtin_vnode_peek(const vnode_t* x, vtype type) pure__ wur__ Nonnull__(1); +extern void libcdsb_builtin_vnode_free( vnode_t* x, vtype type) Nonnull__(1); - -ainline(void libcdsb_builtin_vnode_attach(vnode_t* node, const void* value, vtype type)) { - if (type < VTYPE_STRING) { - *node = libcdsb_builtin_vnode_create(value, type); - } else if (sizeof(str_t) == sizeof(void*) && type == VTYPE_STRING) { - *node = *(char**)value; +ainline(void libcdsb_builtin_vnode_attach(vnode_t* node, vtype_variable value)) { + if (value.type < VTYPE_STRING) { + *node = libcdsb_builtin_vnode_create(value); + } else if (sizeof(str_t) == sizeof(void*) && value.type == VTYPE_STRING) { + *node = *(char**)value.pointer; } else { - *node = malloc(vtype_size(type)); - memcpy(*node, value, vtype_size(type)); + *node = malloc(vtype_size(value.type)); + memcpy(*node, value.pointer, vtype_size(value.type)); } } -ainline(void libcdsb_builtin_vnode_tattach(vnode_t* node, vtype target_type, const void* value, vtype type)) { - if (type < VTYPE_STRING) { - *node = libcdsb_builtin_vnode_create_target(target_type, value, type); +ainline(void libcdsb_builtin_vnode_attach_ex(vnode_t* node, vtype target, vtype_variable value)) { + if (value.type < VTYPE_STRING) { + *node = libcdsb_builtin_vnode_create_ex(target, value); } else { - type_assert(target_type, type); + type_assert(target, value.type); - if (sizeof(str_t) == sizeof(void*) && type == VTYPE_STRING) { - *node = *(char**)value; + if (sizeof(str_t) == sizeof(void*) && value.type == VTYPE_STRING) { + *node = *(char**)value.pointer; } else { - *node = malloc(vtype_size(type)); - memcpy(*node, value, vtype_size(type)); + *node = malloc(vtype_size(value.type)); + memcpy(*node, value.pointer, vtype_size(value.type)); } } } -#define vnode_create libcdsb_builtin_vnode_create -#define vnode_tcreate libcdsb_builtin_vnode_create_target -#define vnode_attach libcdsb_builtin_vnode_attach -#define vnode_tattach libcdsb_builtin_vnode_tattach -#define vnode_peek libcdsb_builtin_vnode_peek -#define vnode_free libcdsb_builtin_vnode_free +#define vnode_create libcdsb_builtin_vnode_create +#define vnode_attach libcdsb_builtin_vnode_attach +#define vnode_create_ex libcdsb_builtin_vnode_create_ex +#define vnode_attach_ex libcdsb_builtin_vnode_attach_ex -#define vnode_hash(vnode, type) vtype_hash(vnode_peek(vnode, type).pointer, type) -#define vnode_compare(s0, t0, s1, t1) variable_compare(vnode_peek(s0, t0), vnode_peek(s1, t1)) -#define vnode_compare_eq(s0, s1, t) variable_compare(vnode_peek(s0, t ), vnode_peek(s1, t )) -#define vnode_duplicate(vnode, type) vnode_create(vnode_peek(vnode, type).pointer, type) -#define vnode_tduplicate(target_type, vnode, type) vnode_tcreate(target_type, vnode_peek(vnode, type), type) +#define vnode_peek libcdsb_builtin_vnode_peek +#define vnode_free libcdsb_builtin_vnode_free -#define vnode_stringify(n, t) vtype_stringify(vnode_peek(n, t)) +#define vnode_hash(n, t) variable_hash(vnode_peek(n, t)) +#define vnode_compare(s0, t0, s1, t1) variable_compare(vnode_peek(s0, t0), vnode_peek(s1, t1)) +#define vnode_stringify(n, t) variable_stringify(vnode_peek(n, t)) + +#define vnode_duplicate(n, t) vnode_create(vnode_peek(n, t)) +#define vnode_duplicate_ex(xt, n, t) vnode_create_ex(xt, vnode_peek(n, t)) #endif /* LIBCDSB_SRC_INTERNAL_VNODE_H */ diff --git a/src/array/compute.c b/src/array/compute.c index cd7f898..2b2c215 100644 --- a/src/array/compute.c +++ b/src/array/compute.c @@ -13,10 +13,10 @@ hash_t array_hash(const arr_t* s) { hash_t hash = 0; if (s->size > 0) - hash = vtype_hash(s->mem, s->type); + hash = variable_hash(libcdsb_variable_build(s->mem, s->type)); if (s->size > 1) - hash += vtype_hash(array_internal_at(s, s->size - 1), s->type); + hash += variable_hash(libcdsb_variable_build(array_internal_at(s, s->size - 1), s->type)); hash ^= s->size; diff --git a/src/array/modify.c b/src/array/modify.c index ada7bb1..96eaba4 100644 --- a/src/array/modify.c +++ b/src/array/modify.c @@ -10,11 +10,11 @@ ssize_t libcdsb_array_insert(arr_t* x, vtype_variable var) { vnode_t n; i = x->size; - n = vnode_tcreate(x->type, var.pointer, var.type); + n = vnode_create_ex(x->type, var); x->mem = realloc(x->mem, ++x->size * vtype_size(x->type)); if (var.type < VTYPE_STRING) { - n = vnode_tcreate(x->type, var.pointer, var.type); + n = vnode_create_ex(x->type, var); memcpy(array_internal_at(x, i), vnode_peek(&n, x->type).pointer, vtype_size(x->type)); if (vtype_size(x->type) > sizeof(vnode_t)) @@ -37,7 +37,7 @@ ssize_t libcdsb_array_attach(arr_t* x, vtype_variable var) { x->mem = realloc(x->mem, ++x->size * vtype_size(x->type)); if (var.type < VTYPE_STRING) { - n = vnode_tcreate(x->type, var.pointer, var.type); + n = vnode_create_ex(x->type, var); memcpy(array_internal_at(x, i), vnode_peek(&n, x->type).pointer, vtype_size(x->type)); if (vtype_size(x->type) > sizeof(vnode_t)) diff --git a/src/dict/access.c b/src/dict/access.c index 216ed6f..81d2187 100644 --- a/src/dict/access.c +++ b/src/dict/access.c @@ -9,7 +9,7 @@ int libcdsb_dict_find(dict_t* x, vtype_variable kvar, void* dt, dict_access_call vtype_variable k, v; if (x->capacity) { - c = *(p = x->nodes + (vtype_hash(kvar.pointer, kvar.type) % x->capacity)); + c = *(p = x->nodes + (variable_hash(kvar) % x->capacity)); while (!is_null(c)) { k = vnode_peek(&c->key, c->key_type); diff --git a/src/dict/modify.c b/src/dict/modify.c index 7df42a2..3febcf4 100644 --- a/src/dict/modify.c +++ b/src/dict/modify.c @@ -57,7 +57,7 @@ bool libcdsb_dict_update(dict_t* x, vtype_variable key, vtype_variable value, vo if (!x->capacity || (double)x->size / x->capacity > REBUILD_POINT_MAX) libcdsb_builtin_rehash(x, x->capacity + CAPACITY_BLOCK); - c = *(p = x->nodes + (vtype_hash(key.pointer, key.type) % x->capacity)); + c = *(p = x->nodes + (variable_hash(key) % x->capacity)); while (!is_null(c)) { k = vnode_peek(&c->key, c->key_type); @@ -67,7 +67,8 @@ bool libcdsb_dict_update(dict_t* x, vtype_variable key, vtype_variable value, vo vnode_free(&c->value, c->value_type); - c->value = vnode_create(value.pointer, c->value_type = value.type); + c->value = vnode_create(value); + c->value_type = value.type; return true; } else c = c->prev; @@ -75,9 +76,11 @@ bool libcdsb_dict_update(dict_t* x, vtype_variable key, vtype_variable value, vo c = malloc(sizeof(*c)); - c->prev = *p; - c->key = vnode_create(key.pointer, c->key_type = key.type); - c->value = vnode_create(value.pointer, c->value_type = value.type); + c->prev = *p; + c->key = vnode_create(key); + c->value = vnode_create(value); + c->key_type = key.type; + c->value_type = value.type; *p = c; ++x->size; @@ -93,7 +96,7 @@ bool libcdsb_dict_inject(dict_t* x, vtype_variable key, vtype_variable value, vo if (!x->capacity || (double)x->size / x->capacity > REBUILD_POINT_MAX) libcdsb_builtin_rehash(x, x->capacity + CAPACITY_BLOCK); - c = *(p = x->nodes + (vtype_hash(key.pointer, key.type) % x->capacity)); + c = *(p = x->nodes + (variable_hash(key) % x->capacity)); while (!is_null(c)) { k = vnode_peek(&c->key, c->key_type); @@ -104,16 +107,23 @@ bool libcdsb_dict_inject(dict_t* x, vtype_variable key, vtype_variable value, vo vnode_free(&c->key, c->key_type); vnode_free(&c->value, c->value_type); - vnode_attach(&c->key, key.pointer, c->key_type = key.type); - vnode_attach(&c->value, value.pointer, c->value_type = value.type); + vnode_attach(&c->key, key); + vnode_attach(&c->value, value); + + c->key_type = key.type; + c->value_type = value.type; return true; } else c = c->prev; } (c = malloc(sizeof(*c)))->prev = *p; - vnode_attach(&c->key, key.pointer, c->key_type = key.type); - vnode_attach(&c->value, value.pointer, c->value_type = value.type); + + vnode_attach(&c->key, key); + vnode_attach(&c->value, value); + + c->key_type = key.type; + c->value_type = value.type; *p = c; ++x->size; diff --git a/src/list/modify.c b/src/list/modify.c index 744a42e..12b81f3 100644 --- a/src/list/modify.c +++ b/src/list/modify.c @@ -45,7 +45,8 @@ bool libcdsb_list_insert(list_t* x, ssize_t i, vtype_variable value, int ins, vo vnode_free(&c->node, c->type); } - c->node = vnode_create(value.pointer, c->type = value.type); + c->node = vnode_create(value); + c->type = value.type; return true; } @@ -93,7 +94,8 @@ bool libcdsb_list_attach(list_t* x, ssize_t i, vtype_variable value, int ins, vo vnode_free(&c->node, c->type); } - vnode_attach(&c->node, value.pointer, c->type = value.type); + vnode_attach(&c->node, value); + c->type = value.type; return true; } diff --git a/src/map/comparison.c b/src/map/comparison.c index a2b74a0..acf16ae 100644 --- a/src/map/comparison.c +++ b/src/map/comparison.c @@ -4,7 +4,7 @@ #include "include.h" static inline int libcdsb_builtin_compare(const mnode_t* s0, const mnode_t* s1, vtype t) { - int c = vnode_compare_eq(&s0->key, &s1->key, t); + int c = vnode_compare(&s0->key, t, &s1->key, t); return !c ? vnode_compare(&s0->value, s0->type, &s1->value, s1->type) : c; } diff --git a/src/map/modify.c b/src/map/modify.c index a43c9b4..4b06da4 100644 --- a/src/map/modify.c +++ b/src/map/modify.c @@ -20,7 +20,8 @@ bool libcdsb_map_update(map_t* x, vtype_variable key, vtype_variable value, void vnode_free(&n->value, n->type); - n->value = vnode_create(value.pointer, n->type = value.type); + n->value = vnode_create(value); + n->type = value.type; return true; } @@ -38,8 +39,9 @@ bool libcdsb_map_update(map_t* x, vtype_variable key, vtype_variable value, void } else n = x->root = mnode_create(nullptr, mnode_empty, 0); - n->key = vnode_tcreate(x->type, key.pointer, key.type); - n->value = vnode_create(value.pointer, n->type = value.type); + n->key = vnode_create_ex(x->type, key); + n->value = vnode_create(value); + n->type = value.type; return false; } @@ -52,10 +54,9 @@ bool libcdsb_map_inject(map_t* x, vtype_variable key, vtype_variable value, void vnode_t kn; vtype_variable k; - vnode_tattach(&kn, x->type, key.pointer, key.type); - n = x->root; - key.type = x->type; - key.pointer = vnode_peek(&kn, key.type).pointer; + vnode_attach_ex(&kn, x->type, key); + n = x->root; + key = vnode_peek(&kn, x->type); if (!mnode_is_empty(n)) { do { @@ -69,8 +70,9 @@ bool libcdsb_map_inject(map_t* x, vtype_variable key, vtype_variable value, void vnode_free(&n->key, x->type); vnode_free(&n->value, n->type); - n->key = kn; - vnode_attach(&n->value, value.pointer, n->type = value.type); + n->key = kn; + n->type = value.type; + vnode_attach(&n->value, value); return true; } @@ -88,7 +90,8 @@ bool libcdsb_map_inject(map_t* x, vtype_variable key, vtype_variable value, void } else n = x->root = mnode_create(kn, mnode_empty, 0); - vnode_attach(&n->value, value.pointer, n->type = value.type); + vnode_attach(&n->value, value); + n->type = value.type; return false; } diff --git a/src/set/modify.c b/src/set/modify.c index 11e8612..d5f28da 100644 --- a/src/set/modify.c +++ b/src/set/modify.c @@ -30,7 +30,7 @@ bool libcdsb_vset_insert(set_t* x, vtype_variable value) { } else n = x->root = rbnode_create(nullptr, rbnode_empty, 0); - n->value = vnode_tcreate(x->type, value.pointer, value.type); + n->value = vnode_create_ex(x->type, value); return true; } @@ -42,7 +42,7 @@ bool libcdsb_vset_attach(set_t* x, vtype_variable value) { rbnode_t* p; vnode_t vn; - vnode_tattach(&vn, x->type, value.pointer, value.type); + vnode_attach_ex(&vn, x->type, value); n = x->root; value.type = x->type; value.pointer = vnode_peek(&vn, value.type).pointer; diff --git a/src/stringify.c b/src/stringify.c index c88e333..a1e3adb 100644 --- a/src/stringify.c +++ b/src/stringify.c @@ -62,7 +62,7 @@ const char* libcdsb_vtype_name(vtype t) { } -const char* libcdsb_vtype_stringify(vtype_variable value) { +const char* libcdsb_variable_stringify(vtype_variable value) { if (is_null(value.pointer) || (value.type == VTYPE_POINTER && is_null(*(void**)value.pointer))) return "null"; if (value.type == VTYPE_BOOLEAN) return (*(vtype_bool*)value.pointer) ? "true" : "false"; if (value.type == VTYPE_STRING) return *(char**)value.pointer; diff --git a/src/vnode.c b/src/vnode.c index 042f909..2aca342 100644 --- a/src/vnode.c +++ b/src/vnode.c @@ -15,7 +15,6 @@ static vtype libcdsb_builtin_round(u64_t* x, ldbl_t v) { } } - static vnode_t libcdsb_builtin_create_value(vtype xt, const void* v, vtype t) { var_t _; @@ -82,7 +81,6 @@ static vnode_t libcdsb_builtin_create_value(vtype xt, const void* v, vtype t) { return _.ptr; } - static vnode_t libcdsb_builtin_create_float(vtype xt, const void* v, vtype t) { var_t _; @@ -131,68 +129,107 @@ static vnode_t libcdsb_builtin_create_float(vtype xt, const void* v, vtype t) { /*#####################################################################################################################*/ -vnode_t libcdsb_builtin_vnode_create(const void* v, vtype t) { +vnode_t libcdsb_builtin_vnode_create(vtype_variable var) { var_t _ = { .ptr = 0 }; - switch (t) { default: abort(); + switch (var.type) { default: abort(); case VTYPE_INT8: - case VTYPE_UINT8: _.u8 = *(u8_t*)v; + case VTYPE_UINT8: _.u8 = *(u8_t*)var.pointer; break; case VTYPE_INT16: - case VTYPE_UINT16: _.u16 = *(u16_t*)v; + case VTYPE_UINT16: _.u16 = *(u16_t*)var.pointer; break; case VTYPE_INT32: - case VTYPE_UINT32: _.u32 = *(u32_t*)v; + case VTYPE_UINT32: _.u32 = *(u32_t*)var.pointer; break; case VTYPE_INT64: - case VTYPE_UINT64: if (is_x64) _.u64 = *(u64_t*)v; - else _.ptr = memndup(v, sizeof(u64_t)); + case VTYPE_UINT64: if (is_x64) _.u64 = *(u64_t*)var.pointer; + else _.ptr = memndup(var.pointer, sizeof(u64_t)); break; - case VTYPE_FLOAT: if (is_permissible(fl_t)) _.f = *(fl_t*)v; - else _.ptr = memndup(v, sizeof(fl_t)); + case VTYPE_FLOAT: if (is_permissible(fl_t)) _.f = *(fl_t*)var.pointer; + else _.ptr = memndup(var.pointer, sizeof(fl_t)); break; - case VTYPE_DOUBLE: if (is_permissible(dbl_t)) _.d = *(dbl_t*)v; - else _.ptr = memndup(v, sizeof(dbl_t)); + case VTYPE_DOUBLE: if (is_permissible(dbl_t)) _.d = *(dbl_t*)var.pointer; + else _.ptr = memndup(var.pointer, sizeof(dbl_t)); break; - case VTYPE_LDOUBLE: if (is_permissible(ldbl_t)) _.ld = *(ldbl_t*)v; - else _.ptr = memndup(v, sizeof(ldbl_t)); + case VTYPE_LDOUBLE: if (is_permissible(ldbl_t)) _.ld = *(ldbl_t*)var.pointer; + else _.ptr = memndup(var.pointer, sizeof(ldbl_t)); break; - case VTYPE_BOOLEAN: _.b = *(bool*)v; + case VTYPE_BOOLEAN: _.b = *(bool*)var.pointer; break; - case VTYPE_POINTER: _.ptr = *(void**)v; + case VTYPE_POINTER: _.ptr = *(void**)var.pointer; break; case VTYPE_STRING: if (sizeof(str_t) == sizeof(void*)) { - _.ptr = strdup(*(char**)v); - } else _.ptr = string_duplicate(v); + _.ptr = strdup(*(char**)var.pointer); + } else _.ptr = string_duplicate(var.pointer); break; - case VTYPE_MAP: _.ptr = map_duplicate(v); + case VTYPE_MAP: _.ptr = map_duplicate(var.pointer); break; - case VTYPE_ARRAY: _.ptr = array_duplicate(v); + case VTYPE_ARRAY: _.ptr = array_duplicate(var.pointer); break; - case VTYPE_LIST: _.ptr = list_duplicate(v); + case VTYPE_LIST: _.ptr = list_duplicate(var.pointer); break; - case VTYPE_SET: _.ptr = vset_duplicate(v); + case VTYPE_SET: _.ptr = vset_duplicate(var.pointer); break; } return _.ptr; } +vnode_t libcdsb_builtin_vnode_create_ex(vtype xt, vtype_variable var) { + var_t _ = { .ptr = 0 }; + + if (xt <= VTYPE_LDOUBLE) { + if (var.type >= VTYPE_STRING) var.type = VTYPE_POINTER; + + if (xt <= VTYPE_INT64) + return libcdsb_builtin_create_value(xt, var.pointer, var.type); + return libcdsb_builtin_create_float(xt, var.pointer, var.type); + } else if (var.type == VTYPE_POINTER && (var.type = xt) > VTYPE_STRING) { + var.pointer = *(void**)var.pointer; + } + + type_assert(xt, var.type); + + switch (xt) { default: abort(); + case VTYPE_STRING: if (sizeof(str_t) == sizeof(void*)) { + _.ptr = strdup(*(char**)var.pointer); + } else _.ptr = string_duplicate(var.pointer); + break; + + case VTYPE_MAP: _.ptr = map_duplicate(var.pointer); + break; + + case VTYPE_ARRAY: _.ptr = array_duplicate(var.pointer); + break; + + case VTYPE_LIST: _.ptr = list_duplicate(var.pointer); + break; + + case VTYPE_SET: _.ptr = vset_duplicate(var.pointer); + break; + + case VTYPE_DICT: _.ptr = dict_duplicate(var.pointer); + break; + } + + return _.ptr; +} vtype_variable libcdsb_builtin_vnode_peek(const vnode_t* x, vtype t) { vtype_variable var = { .type = t }; @@ -230,7 +267,6 @@ vtype_variable libcdsb_builtin_vnode_peek(const vnode_t* x, vtype t) { return var; } - void libcdsb_builtin_vnode_free(vnode_t* x, vtype t) { switch (t) { default: abort(); @@ -268,44 +304,3 @@ void libcdsb_builtin_vnode_free(vnode_t* x, vtype t) { *x = 0; } - - -vnode_t libcdsb_builtin_vnode_create_target(vtype xt, const void* v, vtype t) { - var_t _ = { .ptr = 0 }; - - if (xt <= VTYPE_LDOUBLE) { - if (t >= VTYPE_STRING) t = VTYPE_POINTER; - - if (xt <= VTYPE_INT64) - return libcdsb_builtin_create_value(xt, v, t); - return libcdsb_builtin_create_float(xt, v, t); - } else if (t == VTYPE_POINTER && (t = xt) > VTYPE_STRING) { - v = *(void**)v; - } - - type_assert(xt, t); - - switch (xt) { default: abort(); - case VTYPE_STRING: if (sizeof(str_t) == sizeof(void*)) { - _.ptr = strdup(*(char**)v); - } else _.ptr = string_duplicate(v); - break; - - case VTYPE_MAP: _.ptr = map_duplicate(v); - break; - - case VTYPE_ARRAY: _.ptr = array_duplicate(v); - break; - - case VTYPE_LIST: _.ptr = list_duplicate(v); - break; - - case VTYPE_SET: _.ptr = vset_duplicate(v); - break; - - case VTYPE_DICT: _.ptr = dict_duplicate(v); - break; - } - - return _.ptr; -} diff --git a/src/vtype.c b/src/vtype.c index 3ef7134..aff7252 100644 --- a/src/vtype.c +++ b/src/vtype.c @@ -116,38 +116,37 @@ int libcdsb_builtin_variable_compare(vtype_variable s0, vtype_variable s1) { /*#####################################################################################################################*/ -hash_t libcdsb_builtin_vtype_hash(const void* v, vtype t) { - - switch (t) { default: abort(); +hash_t libcdsb_builtin_variable_hash(vtype_variable value) { + switch (value.type) { default: abort(); case VTYPE_BOOLEAN: case VTYPE_INT8: - case VTYPE_UINT8: return (hash_t)(*(u8_t*)v); + case VTYPE_UINT8: return (hash_t)(*(u8_t*)value.pointer); case VTYPE_INT16: - case VTYPE_UINT16: return (hash_t)(*(u16_t*)v); + case VTYPE_UINT16: return (hash_t)(*(u16_t*)value.pointer); case VTYPE_INT32: case VTYPE_UINT32: - x86_ptr: return (hash_t)(*(u32_t*)v); + x86_ptr: return (hash_t)(*(u32_t*)value.pointer); case VTYPE_POINTER: if (!is_x64) goto x86_ptr; case VTYPE_INT64: case VTYPE_UINT64: if (sizeof(hash_t) == sizeof(u64_t)) { - return (hash_t)(*(u64_t*)v); + return (hash_t)(*(u64_t*)value.pointer); } else { - return (hash_t)(*(u32_t*)v) ^ (*((u32_t*)v + 1)); + return (hash_t)(*(u32_t*)value.pointer) ^ (*((u32_t*)value.pointer + 1)); } - case VTYPE_STRING: return string_hash(v); - case VTYPE_ARRAY: return array_hash(v); - case VTYPE_LIST: return list_hash(v); - case VTYPE_MAP: return map_hash(v); - case VTYPE_SET: return vset_hash(v); - case VTYPE_DICT: return dict_hash(v); + case VTYPE_STRING: return string_hash(value.pointer); + case VTYPE_ARRAY: return array_hash(value.pointer); + case VTYPE_LIST: return list_hash(value.pointer); + case VTYPE_MAP: return map_hash(value.pointer); + case VTYPE_SET: return vset_hash(value.pointer); + case VTYPE_DICT: return dict_hash(value.pointer); - case VTYPE_FLOAT: return libcdsb_builtin_hash_float(*(fl_t*)v); - case VTYPE_DOUBLE: return libcdsb_builtin_hash_float(*(dbl_t*)v); - case VTYPE_LDOUBLE: return libcdsb_builtin_hash_float(*(ldbl_t*)v); + case VTYPE_FLOAT: return libcdsb_builtin_hash_float(*(fl_t*)value.pointer); + case VTYPE_DOUBLE: return libcdsb_builtin_hash_float(*(dbl_t*)value.pointer); + case VTYPE_LDOUBLE: return libcdsb_builtin_hash_float(*(ldbl_t*)value.pointer); } } diff --git a/tests/src/test.c b/tests/src/test.c index ab6619a..674f8bf 100644 --- a/tests/src/test.c +++ b/tests/src/test.c @@ -70,7 +70,7 @@ void print_container_value(const ssize_t* index, const void* value, const vtype printf("\e[%dG ", hpos+1); } - printf("\e[31m%24s\e[m", libcdsb_vtype_stringify(libcdsb_variable_build((void*)value, type))); + printf("\e[31m%24s\e[m", libcdsb_variable_stringify(libcdsb_variable_build((void*)value, type))); if (print_type) { printf(" \e[36m(\e[m\e[32;1m%s\e[m\e[36m)\e[m", libcdsb_vtype_name(type));