Refactor set, add attaching functional

This commit is contained in:
2022-08-19 17:19:22 +03:00
parent 925a8855ed
commit b789b133e2
12 changed files with 278 additions and 315 deletions
+1 -39
View File
@@ -1,47 +1,9 @@
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "../../include/extra/set.h"
#include "../../include/set.h"
#include "../__internal/rbtree.h"
bool libcdsb_vset_insert(set_t* x, const void* v, vtype t) {
int cmp;
rbnode_t* n;
rbnode_t* p;
vnode_t vn;
n = x->root;
vn = vnode_tcreate(x->type, v, t);
t = x->type;
v = vnode_peek(&vn, t);
if (!rbnode_is_empty(n)) {
do {
p = n;
cmp = vtype_compare(v, t, vnode_peek(&n->value, t), t);
if (cmp == 0) {
vnode_free(&vn, t);
return false;
}
n = (cmp < 0) ? n->left : n->right;
} while (!rbnode_is_empty(n));
n = rbnode_create(vn, p, 1);
if (cmp < 0) p->left = n;
else p->right = n;
if (!rbnode_is_root(p))
rbnode_fixup(&x->root, n);
} else n = x->root = rbnode_create(vn, rbnode_empty, 0);
return true;
}
int libcdsb_vset_find(vtype_set* x, const void* v, vtype t, void* _, vset_access_callback callback, bool cut) {
rbnode_t* c;
void *val;
-161
View File
@@ -1,161 +0,0 @@
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "../../include/set.h"
#include "../__internal/rbtree.h"
/*#####################################################################################################################*/
static inline int rbnode_compare(const rbnode_t* s0, const rbnode_t* s1, vtype t) {
return vnode_compare(s0->value, t, s1->value, t);
}
static inline hash_t rbnode_hash(const rbnode_t* s, vtype t) {
return vnode_hash(&s->value, t);
}
/*#####################################################################################################################*/
hash_t vset_hash(const set_t* s) {
stack_t z;
rbnode_t *c0, *c1;
hash_t hash, v;
if (rbnode_is_empty(s->root))
return 0;
stack_init(&z);
stack_push(&z, s->root->left);
hash = 1;
if (!rbnode_is_empty(c0 = stack_pop(&z))) {
do {
++hash;
if (!rbnode_is_empty(c0->right)) stack_push(&z, c0->right);
if (!rbnode_is_empty(c0->left)) stack_push(&z, c1 = c0->left);
} while (!is_null(c0 = stack_pop(&z)));
}
v = rbnode_hash(c1, s->type);
stack_push(&z, s->root->right);
if (!rbnode_is_empty(c0 = stack_pop(&z))) {
do {
++hash;
if (!rbnode_is_empty(c0->right)) stack_push(&z, c1 = c0->right);
if (!rbnode_is_empty(c0->left)) stack_push(&z, c0->left);
} while (!is_null(c0 = stack_pop(&z)));
}
v += rbnode_hash(c1, s->type);
return (hash ^ v) + VTYPE_SET;
}
/*#####################################################################################################################*/
void vset_init(set_t* x, vtype t) {
x->root = rbnode_empty;
x->type = t;
}
void vset_free(set_t* x) {
rbnode_t *t, *c;
c = x->root;
while (!rbnode_is_empty(x->root)) {
if (!rbnode_is_empty(c->left)) {
c = c->left;
} else if (!rbnode_is_empty(c->right)) {
c = c->right;
} else if (!rbnode_is_root(c)) {
vnode_free(&c->value, x->type);
t = c;
c = c->parent;
if (t == c->left) c->left = rbnode_empty;
else c->right = rbnode_empty;
free(t);
} else {
vnode_free(&c->value, x->type);
x->root = rbnode_empty;
}
}
x->type = 0;
}
/*#####################################################################################################################*/
size_t vset_size(const set_t* x) {
stack_t z;
size_t n;
rbnode_t* c;
stack_init(&z);
stack_push(&z, x->root);
n = 0;
if (!rbnode_is_empty(x->root)) {
while ((c = stack_pop(&z))) {
++n;
if (!rbnode_is_empty(c->right)) stack_push(&z, c->right);
if (!rbnode_is_empty(c->left)) stack_push(&z, c->left);
}
}
return n;
}
/*#####################################################################################################################*/
int vset_compare(const set_t* s0, const set_t* s1) {
stack_t z;
rbnode_t *c0, *c1;
int cmp;
if (s0 == s1 || s0->root == s1->root)
return 0;
if (s0->type != s1->type)
return s0->type - s1->type;
stack_init(&z);
stack_push_many(&z, 2, (void*)s1, (void*)s0);
cmp = 0;
do {
c0 = stack_pop(&z);
c1 = stack_pop(&z);
if (rbnode_is_empty(c0) || rbnode_is_empty(c1)) {
if (c0 != c1) {
stack_flush(&z);
return rbnode_is_empty(c0) ? -1 : 1;
}
} else if ((cmp = rbnode_compare(c0, c1, s0->type))) {
if (c0->left == c1->right) { // == rbnode_empty
cmp = rbnode_compare(c0->right, c1, s0->type);
if (!cmp) cmp = rbnode_compare(c0, c1->left, s0->type);
} else if (c0->right == c1->left) { // == rbnode_empty
cmp = rbnode_compare(c0, c1->right, s0->type);
if (!cmp) cmp = rbnode_compare(c0->left, c1, s0->type);
}
if (cmp) {
stack_flush(&z);
return cmp;
}
} else stack_push_many(&z, 4, c1->right, c0->right, c1->left, c0->left);
} while (!is_null(z.value));
return 0;
}
+55
View File
@@ -0,0 +1,55 @@
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "../../include/set.h"
#include "../__internal/rbtree.h"
static inline int rbnode_compare(const rbnode_t* s0, const rbnode_t* s1, vtype t) {
return vnode_compare(s0->value, t, s1->value, t);
}
/*#####################################################################################################################*/
int vset_compare(const set_t* s0, const set_t* s1) {
stack_t z;
rbnode_t *c0, *c1;
int cmp;
if (s0 == s1 || s0->root == s1->root)
return 0;
if (s0->type != s1->type)
return s0->type - s1->type;
stack_init(&z);
stack_push_many(&z, 2, (void*)s1, (void*)s0);
cmp = 0;
do {
c0 = stack_pop(&z);
c1 = stack_pop(&z);
if (rbnode_is_empty(c0) || rbnode_is_empty(c1)) {
if (c0 != c1) {
stack_flush(&z);
return rbnode_is_empty(c0) ? -1 : 1;
}
} else if ((cmp = rbnode_compare(c0, c1, s0->type))) {
if (c0->left == c1->right) { // == rbnode_empty
cmp = rbnode_compare(c0->right, c1, s0->type);
if (!cmp) cmp = rbnode_compare(c0, c1->left, s0->type);
} else if (c0->right == c1->left) { // == rbnode_empty
cmp = rbnode_compare(c0, c1->right, s0->type);
if (!cmp) cmp = rbnode_compare(c0->left, c1, s0->type);
}
if (cmp) {
stack_flush(&z);
return cmp;
}
} else stack_push_many(&z, 4, c1->right, c0->right, c1->left, c0->left);
} while (!is_null(z.value));
return 0;
}
+71
View File
@@ -0,0 +1,71 @@
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "../../include/set.h"
#include "../__internal/rbtree.h"
static inline hash_t rbnode_hash(const rbnode_t* s, vtype t) {
return vnode_hash(&s->value, t);
}
/*#####################################################################################################################*/
size_t vset_size(const set_t* x) {
stack_t z;
size_t n;
rbnode_t* c;
stack_init(&z);
stack_push(&z, x->root);
n = 0;
if (!rbnode_is_empty(x->root)) {
while ((c = stack_pop(&z))) {
++n;
if (!rbnode_is_empty(c->right)) stack_push(&z, c->right);
if (!rbnode_is_empty(c->left)) stack_push(&z, c->left);
}
}
return n;
}
hash_t vset_hash(const set_t* s) {
stack_t z;
rbnode_t *c0, *c1;
hash_t hash, v;
if (rbnode_is_empty(s->root))
return 0;
stack_init(&z);
stack_push(&z, s->root->left);
hash = 1;
if (!rbnode_is_empty(c0 = stack_pop(&z))) {
do {
++hash;
if (!rbnode_is_empty(c0->right)) stack_push(&z, c0->right);
if (!rbnode_is_empty(c0->left)) stack_push(&z, c1 = c0->left);
} while (!is_null(c0 = stack_pop(&z)));
}
v = rbnode_hash(c1, s->type);
stack_push(&z, s->root->right);
if (!rbnode_is_empty(c0 = stack_pop(&z))) {
do {
++hash;
if (!rbnode_is_empty(c0->right)) stack_push(&z, c1 = c0->right);
if (!rbnode_is_empty(c0->left)) stack_push(&z, c0->left);
} while (!is_null(c0 = stack_pop(&z)));
}
v += rbnode_hash(c1, s->type);
return (hash ^ v) + VTYPE_SET;
}
-47
View File
@@ -1,47 +0,0 @@
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "../../include/extra/set.h"
#include "../__internal/include.h"
int libcdsb_vset_find_pointer(set_t* x, const void* v, void* _, vset_access_callback cb, bool cut) { return libcdsb_vset_find(x, &v, vtypeof(&v), _, cb, cut); }
int libcdsb_vset_find_cstring(set_t* x, const char* v, void* _, vset_access_callback cb, bool cut) { return libcdsb_vset_find(x, &v, vtypeof(&v), _, cb, cut); }
int libcdsb_vset_find_string (set_t* x, const str_t* v, void* _, vset_access_callback cb, bool cut) { return libcdsb_vset_find(x, v, vtypeof( v), _, cb, cut); }
int libcdsb_vset_find_array (set_t* x, const arr_t* v, void* _, vset_access_callback cb, bool cut) { return libcdsb_vset_find(x, v, vtypeof( v), _, cb, cut); }
int libcdsb_vset_find_list (set_t* x, const list_t* v, void* _, vset_access_callback cb, bool cut) { return libcdsb_vset_find(x, v, vtypeof( v), _, cb, cut); }
int libcdsb_vset_find_map (set_t* x, const map_t* v, void* _, vset_access_callback cb, bool cut) { return libcdsb_vset_find(x, v, vtypeof( v), _, cb, cut); }
int libcdsb_vset_find_vset (set_t* x, const set_t* v, void* _, vset_access_callback cb, bool cut) { return libcdsb_vset_find(x, v, vtypeof( v), _, cb, cut); }
int libcdsb_vset_find_dict (set_t* x, const dict_t* v, void* _, vset_access_callback cb, bool cut) { return libcdsb_vset_find(x, v, vtypeof( v), _, cb, cut); }
int libcdsb_vset_find_boolean(set_t* x, bool v, void* _, vset_access_callback cb, bool cut) { return libcdsb_vset_find(x, &v, vtypeof(&v), _, cb, cut); }
int libcdsb_vset_find_int8 (set_t* x, s8_t v, void* _, vset_access_callback cb, bool cut) { return libcdsb_vset_find(x, &v, vtypeof(&v), _, cb, cut); }
int libcdsb_vset_find_int16 (set_t* x, s16_t v, void* _, vset_access_callback cb, bool cut) { return libcdsb_vset_find(x, &v, vtypeof(&v), _, cb, cut); }
int libcdsb_vset_find_int32 (set_t* x, s32_t v, void* _, vset_access_callback cb, bool cut) { return libcdsb_vset_find(x, &v, vtypeof(&v), _, cb, cut); }
int libcdsb_vset_find_int64 (set_t* x, s64_t v, void* _, vset_access_callback cb, bool cut) { return libcdsb_vset_find(x, &v, vtypeof(&v), _, cb, cut); }
int libcdsb_vset_find_uint8 (set_t* x, u8_t v, void* _, vset_access_callback cb, bool cut) { return libcdsb_vset_find(x, &v, vtypeof(&v), _, cb, cut); }
int libcdsb_vset_find_uint16 (set_t* x, u16_t v, void* _, vset_access_callback cb, bool cut) { return libcdsb_vset_find(x, &v, vtypeof(&v), _, cb, cut); }
int libcdsb_vset_find_uint32 (set_t* x, u32_t v, void* _, vset_access_callback cb, bool cut) { return libcdsb_vset_find(x, &v, vtypeof(&v), _, cb, cut); }
int libcdsb_vset_find_uint64 (set_t* x, u64_t v, void* _, vset_access_callback cb, bool cut) { return libcdsb_vset_find(x, &v, vtypeof(&v), _, cb, cut); }
int libcdsb_vset_find_float (set_t* x, fl_t v, void* _, vset_access_callback cb, bool cut) { return libcdsb_vset_find(x, &v, vtypeof(&v), _, cb, cut); }
int libcdsb_vset_find_double (set_t* x, dbl_t v, void* _, vset_access_callback cb, bool cut) { return libcdsb_vset_find(x, &v, vtypeof(&v), _, cb, cut); }
int libcdsb_vset_find_ldouble(set_t* x, ldbl_t v, void* _, vset_access_callback cb, bool cut) { return libcdsb_vset_find(x, &v, vtypeof(&v), _, cb, cut); }
bool libcdsb_vset_push_pointer(set_t* x, const void* v) { return libcdsb_vset_insert(x, &v, vtypeof(&v)); }
bool libcdsb_vset_push_cstring(set_t* x, const char* v) { return libcdsb_vset_insert(x, &v, vtypeof(&v)); }
bool libcdsb_vset_push_string (set_t* x, const str_t* v) { return libcdsb_vset_insert(x, v, vtypeof( v)); }
bool libcdsb_vset_push_array (set_t* x, const arr_t* v) { return libcdsb_vset_insert(x, v, vtypeof( v)); }
bool libcdsb_vset_push_list (set_t* x, const list_t* v) { return libcdsb_vset_insert(x, v, vtypeof( v)); }
bool libcdsb_vset_push_map (set_t* x, const map_t* v) { return libcdsb_vset_insert(x, v, vtypeof( v)); }
bool libcdsb_vset_push_vset (set_t* x, const set_t* v) { return libcdsb_vset_insert(x, v, vtypeof( v)); }
bool libcdsb_vset_push_dict (set_t* x, const dict_t* v) { return libcdsb_vset_insert(x, v, vtypeof( v)); }
bool libcdsb_vset_push_boolean(set_t* x, bool v) { return libcdsb_vset_insert(x, &v, vtypeof(&v)); }
bool libcdsb_vset_push_int8 (set_t* x, s8_t v) { return libcdsb_vset_insert(x, &v, vtypeof(&v)); }
bool libcdsb_vset_push_int16 (set_t* x, s16_t v) { return libcdsb_vset_insert(x, &v, vtypeof(&v)); }
bool libcdsb_vset_push_int32 (set_t* x, s32_t v) { return libcdsb_vset_insert(x, &v, vtypeof(&v)); }
bool libcdsb_vset_push_int64 (set_t* x, s64_t v) { return libcdsb_vset_insert(x, &v, vtypeof(&v)); }
bool libcdsb_vset_push_uint8 (set_t* x, u8_t v) { return libcdsb_vset_insert(x, &v, vtypeof(&v)); }
bool libcdsb_vset_push_uint16 (set_t* x, u16_t v) { return libcdsb_vset_insert(x, &v, vtypeof(&v)); }
bool libcdsb_vset_push_uint32 (set_t* x, u32_t v) { return libcdsb_vset_insert(x, &v, vtypeof(&v)); }
bool libcdsb_vset_push_uint64 (set_t* x, u64_t v) { return libcdsb_vset_insert(x, &v, vtypeof(&v)); }
bool libcdsb_vset_push_float (set_t* x, fl_t v) { return libcdsb_vset_insert(x, &v, vtypeof(&v)); }
bool libcdsb_vset_push_double (set_t* x, dbl_t v) { return libcdsb_vset_insert(x, &v, vtypeof(&v)); }
bool libcdsb_vset_push_ldouble(set_t* x, ldbl_t v) { return libcdsb_vset_insert(x, &v, vtypeof(&v)); }
+39
View File
@@ -0,0 +1,39 @@
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "../../include/set.h"
#include "../__internal/rbtree.h"
void vset_init(set_t* x, vtype t) {
x->root = rbnode_empty;
x->type = t;
}
void vset_free(set_t* x) {
rbnode_t *t, *c;
c = x->root;
while (!rbnode_is_empty(x->root)) {
if (!rbnode_is_empty(c->left)) {
c = c->left;
} else if (!rbnode_is_empty(c->right)) {
c = c->right;
} else if (!rbnode_is_root(c)) {
vnode_free(&c->value, x->type);
t = c;
c = c->parent;
if (t == c->left) c->left = rbnode_empty;
else c->right = rbnode_empty;
free(t);
} else {
vnode_free(&c->value, x->type);
x->root = rbnode_empty;
}
}
x->type = 0;
}
+88
View File
@@ -0,0 +1,88 @@
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "../../include/set.h"
#include "../__internal/rbtree.h"
#include "../__internal/assert.h"
bool libcdsb_vset_insert(set_t* x, const void* v, vtype t) {
int cmp;
rbnode_t* n;
rbnode_t* p;
vnode_t vn;
n = x->root;
vn = vnode_tcreate(x->type, v, t);
t = x->type;
v = vnode_peek(&vn, t);
if (!rbnode_is_empty(n)) {
do {
p = n;
cmp = vtype_compare(v, t, vnode_peek(&n->value, t), t);
if (cmp == 0) {
vnode_free(&vn, t);
return false;
}
n = (cmp < 0) ? n->left : n->right;
} while (!rbnode_is_empty(n));
n = rbnode_create(vn, p, 1);
if (cmp < 0) p->left = n;
else p->right = n;
if (!rbnode_is_root(p))
rbnode_fixup(&x->root, n);
} else n = x->root = rbnode_create(vn, rbnode_empty, 0);
return true;
}
bool libcdsb_vset_attach(set_t* x, const void* v, vtype t) {
int cmp;
rbnode_t* n;
rbnode_t* p;
n = x->root;
t = x->type;
if (!rbnode_is_empty(n)) {
do {
p = n;
cmp = vtype_compare(v, t, vnode_peek(&n->value, t), t);
if (cmp == 0) return false;
n = (cmp < 0) ? n->left : n->right;
} while (!rbnode_is_empty(n));
n = rbnode_create(nullptr, p, 1);
if (cmp < 0) p->left = n;
else p->right = n;
if (!rbnode_is_root(p))
rbnode_fixup(&x->root, n);
} else n = x->root = rbnode_create(nullptr, rbnode_empty, 0);
if (t < VTYPE_STRING) {
n->value = vnode_tcreate(x->type, v, t);
} else {
type_assert(x->type, t);
if (sizeof(str_t) == sizeof(void*) && t == VTYPE_STRING) {
n->value = *(char**)v;
} else {
n->value = malloc(vtype_size(t));
memcpy(n->value, v, vtype_size(t));
}
}
return true;
}