Merge branch 'master' into discrete-tests

This commit is contained in:
Gregory Lirent 2022-06-06 23:51:02 +03:00
commit 0760d7a6a8
7 changed files with 58 additions and 44 deletions

View File

@ -17,6 +17,6 @@ extern ssize_t libcdsb_array_get(vtype_value* x, vtype_array* s, ssize_t index,
extern ssize_t libcdsb_array_find(const vtype_array* x, const void* value, vtype value_type) LIBCDSB_nt__ LIBCDSB_nn1__; extern ssize_t libcdsb_array_find(const vtype_array* x, const void* value, vtype value_type) LIBCDSB_nt__ LIBCDSB_nn1__;
extern ssize_t libcdsb_array_push( vtype_array* x, const void* value, vtype value_type) LIBCDSB_nt__ LIBCDSB_nn1__; extern ssize_t libcdsb_array_push( vtype_array* x, const void* value, vtype value_type) LIBCDSB_nt__ LIBCDSB_nn1__;
extern int libcdsb_array_foreach(vtype_array* x, int (*callback)(void* value, ssize_t index, vtype type)) LIBCDSB_nt__ LIBCDSB_nn12__; extern int libcdsb_array_foreach(const vtype_array* x, int (*callback)(void* value, ssize_t index, vtype type)) LIBCDSB_nt__ LIBCDSB_nn12__;
#endif /* LIBCDSB_EXTRA_ARRAY_H */ #endif /* LIBCDSB_EXTRA_ARRAY_H */

View File

@ -9,10 +9,10 @@
extern void vset_init(vtype_set* x, vtype type); extern void vset_init(vtype_set* x, vtype type);
#define vset_remove(x, value) _LIBCDSB_Generic (libcdsb_vset, touch, key)(x, value, 1) #define vset_remove(x, value) _LIBCDSB_Generic (libcdsb_vset, touch, value)(x, value, 1)
#define vset_push(x, value) _LIBCDSB_Generic (libcdsb_vset, push, key)(x, value) #define vset_push(x, value) _LIBCDSB_Generic (libcdsb_vset, push, value)(x, value)
#define in_vset(x, value) _LIBCDSB_Generic (libcdsb_vset, touch, key)(x, value, 0) #define in_vset(x, value) _LIBCDSB_Generic (libcdsb_vset, touch, value)(x, value, 0)
extern _Bool libcdsb_vset_push_pointer(vtype_set* x, const void* value); extern _Bool libcdsb_vset_push_pointer(vtype_set* x, const void* value);
extern _Bool libcdsb_vset_push_cstring(vtype_set* x, const char* value); extern _Bool libcdsb_vset_push_cstring(vtype_set* x, const char* value);

View File

@ -76,7 +76,7 @@ ssize_t libcdsb_array_get(val_t* x, arr_t* s, ssize_t i, _Bool cut) {
/*#####################################################################################################################*/ /*#####################################################################################################################*/
int libcdsb_array_foreach(vtype_array* x, int (*callback)(void* value, ssize_t index, vtype type)) { int libcdsb_array_foreach(const vtype_array* x, int (*callback)(void* value, ssize_t index, vtype type)) {
void* p; void* p;
void* e; void* e;

View File

@ -32,19 +32,15 @@ static inline void rotate(rbnode_t **x, rbnode_t *c, rbdir_t d) {
rbdir_inv(c, d) = rbdir_dir(n, d); rbdir_inv(c, d) = rbdir_dir(n, d);
n->parent = c->parent; n->parent = c->parent;
if (!rbnode_is_empty(rbdir_dir(n, d))) { if (!rbnode_is_empty(rbdir_dir(n, d)))
rbdir_dir(n, d)->parent = c; rbdir_dir(n, d)->parent = c;
}
if (!rbnode_is_root(c)) { if (!rbnode_is_root(c)) {
rbnode_t* p = c->parent; if (c->parent->left == c) c->parent->left = n;
else c->parent->right = n;
if (rbdir_dir(p, d) == c) {
rbdir_dir(p, d) = n;
} else rbdir_inv(c, d) = n;
} else *x = n; } else *x = n;
rbdir_inv(n, d) = c; rbdir_dir(n, d) = c;
c->parent = n; c->parent = n;
} }
@ -202,7 +198,6 @@ static void delete_fixup(rbnode_t** x, rbnode_t* n) {
rbnode_t* libcdsb_rbtree_node_delete(rbnode_t** x, rbnode_t* c) { rbnode_t* libcdsb_rbtree_node_delete(rbnode_t** x, rbnode_t* c) {
rbnode_t *n, *t; rbnode_t *n, *t;
int s; int s;
void* v;
s = c->colored; s = c->colored;
@ -269,7 +264,7 @@ void libcdsb_rbtree_node_fixup(rbnode_t** x, rbnode_t* n) {
p = n->parent; p = n->parent;
gp = p->parent; gp = p->parent;
} }
p->colored = 0; p->colored = 0;
gp->colored = 1; gp->colored = 1;
rotate(x, gp, d[0]); rotate(x, gp, d[0]);
@ -346,7 +341,7 @@ void libcdsb_rbtree_iter_reset(rbiter_t* iter) {
} }
rbnode_t* hhttpc_rbtree_iter_next(rbiter_t* iter) { rbnode_t* libcdsb_rbtree_iter_next(rbiter_t* iter) {
rbnode_t* c; rbnode_t* c;

View File

@ -36,33 +36,38 @@ _Bool libcdsb_vset_find(val_t* x, set_t* s, const void* v, vtype t, _Bool cut) {
_Bool libcdsb_vset_insert(set_t* x, const void* v, vtype t) { _Bool libcdsb_vset_insert(set_t* x, const void* v, vtype t) {
int cmp; int cmp;
rbnode_t* n; rbnode_t* n;
rbnode_t* p; rbnode_t* p;
vnode_t vn;
n = x->root; n = x->root;
vn = vnode_tcreate(x->type, v, t);
t = x->type;
v = vnode_peek(&vn, t);
if (!rbnode_is_empty(n)) { if (!rbnode_is_empty(n)) {
do { do {
p = n; p = n;
cmp = vtype_compare(vnode_peek(&n->value, x->type), x->type, v, t); cmp = vtype_compare(v, t, vnode_peek(&n->value, t), t);
if (cmp == 0) return false; if (cmp == 0) {
vnode_free(&vn, t);
return false;
}
n = (cmp > 0) ? n->left : n->right; n = (cmp < 0) ? n->left : n->right;
} while (!rbnode_is_empty(n)); } while (!rbnode_is_empty(n));
n = rbnode_create(0, p, 1); n = rbnode_create(vn, p, 1);
if (cmp > 0) p->left = n; if (cmp < 0) p->left = n;
else p->right = n; else p->right = n;
if (!rbnode_is_root(n->parent)) if (!rbnode_is_root(p))
rbnode_fixup(&x->root, n); rbnode_fixup(&x->root, n);
} else n = x->root = rbnode_create(0, p, 1); } else n = x->root = rbnode_create(vn, rbnode_empty, 0);
n->value = vnode_tcreate(x->type, v, t);
return true; return true;
} }
@ -74,10 +79,11 @@ _Bool libcdsb_vset_insert(set_t* x, const void* v, vtype t) {
int libcdsb_vset_foreach(const vtype_set* x, int (*callback)(const void* value, vtype type)) { int libcdsb_vset_foreach(const vtype_set* x, int (*callback)(const void* value, vtype type)) {
rbiter_t i; rbiter_t i;
int r; int r;
rbnode_t* c;
if (rbiter_init(&i, &x->root, 0)) { if (rbiter_init(&i, &x->root, 0)) {
while (!rbnode_is_empty(rbiter_next(&i))) { while (!rbnode_is_empty(c = rbiter_next(&i))) {
if ((r = callback(vnode_peek(&i.cursor->value, x->type), x->type))) if ((r = callback(vnode_peek(&c->value, x->type), x->type)))
return r; return r;
} }
} }

View File

@ -5,6 +5,16 @@
#include "__internal/vnode.h" #include "__internal/vnode.h"
#include "../include/string.h" #include "../include/string.h"
static vtype internal_round(u64_t* x, ldbl_t v) {
if (v > 0) {
*x = (u64_t)(v + 0.5);
return VTYPE_UINT64;
} else {
*x = (s64_t)(v - 0.5);
return VTYPE_INT64;
}
}
/*#####################################################################################################################*/ /*#####################################################################################################################*/
@ -12,25 +22,28 @@ static vnode_t create_value(vtype xt, const void* v, vtype t) {
var_t _; var_t _;
if (t == VTYPE_FLOAT) { if (t == VTYPE_FLOAT) {
_.u64 = (s64_t)roundf(*(fl_t*)v); t = internal_round(&_.u64, *(fl_t*)v);
t = VTYPE_INT64;
} else if (t == VTYPE_DOUBLE) { } else if (t == VTYPE_DOUBLE) {
_.u64 = (s64_t)round (*(dbl_t*)v); t = internal_round(&_.u64, *(dbl_t*)v);
t = VTYPE_INT64; } else if (t == VTYPE_LDOUBLE) {
} else { t = internal_round(&_.u64, *(ldbl_t*)v);
_.u64 = (s64_t)roundl(*(ldbl_t*)v);
t = VTYPE_INT64;
} }
if (sizeof(void*) == 8) { if (sizeof(void*) == 8) {
if (t == VTYPE_UINT8 || t == VTYPE_INT8 || t == VTYPE_BOOLEAN ) { if (t == VTYPE_UINT8 || t == VTYPE_BOOLEAN ) {
_.u64 = *(u8_t*)v; _.u64 = *(u8_t*)v;
} else if (t == VTYPE_UINT16 || t == VTYPE_INT16) { } else if (t == VTYPE_UINT16) {
_.u64 = *(u16_t*)v; _.u64 = *(u16_t*)v;
} else if (t == VTYPE_UINT32 || t == VTYPE_INT32 || (sizeof(void*) == 4 && t == VTYPE_POINTER)) { } else if (t == VTYPE_UINT32 || (sizeof(void*) == 4 && t == VTYPE_POINTER)) {
_.u64 = *(u32_t*)v; _.u64 = *(u32_t*)v;
} else if (t == VTYPE_UINT64 || t == VTYPE_INT64 || (sizeof(void*) == 8 && t == VTYPE_POINTER)) { } else if (t == VTYPE_UINT64 || t == VTYPE_INT64 || (sizeof(void*) == 8 && t == VTYPE_POINTER)) {
_.u64 = *(u64_t*)v; _.u64 = *(u64_t*)v;
} else if (t == VTYPE_INT8) {
_.u64 = *(s8_t*)v;
} else if (t == VTYPE_INT16) {
_.u64 = *(s16_t*)v;
} else if (t == VTYPE_INT32) {
_.u64 = *(s32_t*)v;
} }
if (is_big_endian) { if (is_big_endian) {
@ -105,7 +118,7 @@ static vnode_t create_float(vtype xt, const void* v, vtype t) {
_.ptr = memndup(&_.f, sizeof(_.f)); _.ptr = memndup(&_.f, sizeof(_.f));
} }
} else if (xt == VTYPE_DOUBLE) { } else if (xt == VTYPE_DOUBLE) {
_.f = _.d; _.d = _.ld;
if (!is_permissible(dbl_t)) { if (!is_permissible(dbl_t)) {
_.ptr = memndup(&_.d, sizeof(_.d)); _.ptr = memndup(&_.d, sizeof(_.d));
} }
@ -267,7 +280,7 @@ vnode_t libcdsb_vnode_create_target(vtype xt, const void* v, vtype t) {
if (xt <= VTYPE_INT64) if (xt <= VTYPE_INT64)
return create_value(xt, v, t); return create_value(xt, v, t);
return create_value(xt, v, t); return create_float(xt, v, t);
} else if (t == VTYPE_POINTER && (t = xt) > VTYPE_STRING) { } else if (t == VTYPE_POINTER && (t = xt) > VTYPE_STRING) {
v = *(void**)v; v = *(void**)v;
} }

View File

@ -30,6 +30,8 @@ static ldbl_t normalize_value(const void* v, vtype t) {
} else return abs(*(ldbl_t*)v) <= LDBL_EPSILON ? 0 : *(ldbl_t*)v; } else return abs(*(ldbl_t*)v) <= LDBL_EPSILON ? 0 : *(ldbl_t*)v;
} }
/*#####################################################################################################################*/
int libcdsb_vtype_compare_values(const void* s0, vtype t0, const void* s1, vtype t1) { int libcdsb_vtype_compare_values(const void* s0, vtype t0, const void* s1, vtype t1) {
if (t0 == t1) return libcdsb_vtype_compare_values_eq(s0, s1, t0); if (t0 == t1) return libcdsb_vtype_compare_values_eq(s0, s1, t0);
@ -45,8 +47,6 @@ int libcdsb_vtype_compare_values(const void* s0, vtype t0, const void* s1, vtype
return t0 - t1; return t0 - t1;
} }
/*#####################################################################################################################*/
int libcdsb_vtype_compare_values_eq(const void* s0, const void* s1, vtype t) { int libcdsb_vtype_compare_values_eq(const void* s0, const void* s1, vtype t) {