Update map (vtype_variable)

This commit is contained in:
Gregory Lirent 2023-03-23 16:15:28 +03:00
parent 6fc4be2b75
commit 7657e72c4f
3 changed files with 34 additions and 32 deletions

View File

@ -8,7 +8,7 @@
#ifndef LIBCDSB_MAP_H
#define LIBCDSB_MAP_H
typedef int (*map_access_callback)(const void* key, vtype key_type, void* value, vtype value_type, void* data);
typedef int (*map_access_callback)(vtype_variable key, vtype_variable value, void* data);
/*#####################################################################################################################*/
@ -16,10 +16,10 @@ extern void map_init(vtype_map* x, vtype key_type) Nonnull__(1);
/*#####################################################################################################################*/
#define map_pop(x, key, data, callback) libcdsb_map_find (x, _LIBCDSB_value_pointer(key), _LIBCDSB_vtypeof(key), data, callback, 1)
#define map_get(x, key, data, callback) libcdsb_map_find (x, _LIBCDSB_value_pointer(key), _LIBCDSB_vtypeof(key), data, callback, 0)
#define map_update(x, key, value) libcdsb_map_update (x, _LIBCDSB_value_pointer(key), _LIBCDSB_vtypeof(key), _LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value), 0, 0)
#define map_inject(x, key, value) libcdsb_map_inject (x, _LIBCDSB_value_pointer(key), _LIBCDSB_vtypeof(key), _LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value), 0, 0)
#define map_pop(x, key, data, callback) libcdsb_map_find (x, libcdsb_variable_build(_LIBCDSB_value_pointer(key), _LIBCDSB_vtypeof(key)), data, callback, 1)
#define map_get(x, key, data, callback) libcdsb_map_find (x, libcdsb_variable_build(_LIBCDSB_value_pointer(key), _LIBCDSB_vtypeof(key)), data, callback, 0)
#define map_update(x, key, value) libcdsb_map_update (x, libcdsb_variable_build(_LIBCDSB_value_pointer(key), _LIBCDSB_vtypeof(key)), libcdsb_variable_build(_LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value)), 0, 0)
#define map_inject(x, key, value) libcdsb_map_inject (x, libcdsb_variable_build(_LIBCDSB_value_pointer(key), _LIBCDSB_vtypeof(key)), libcdsb_variable_build(_LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value)), 0, 0)
#define map_foreach(x, data, callback) libcdsb_map_foreach(x, data, callback, RBFOREACH_UNSPECIFIED, 0)
#define map_remove(x, key) map_pop (x, key, 0, 0)
@ -27,9 +27,9 @@ extern void map_init(vtype_map* x, vtype key_type) Nonnull__(1);
/*#####################################################################################################################*/
extern bool libcdsb_map_update (vtype_map* x, const void* key, vtype key_type, const void* value, vtype value_type, void* data, map_access_callback) Nonnull__(1);
extern bool libcdsb_map_inject (vtype_map* x, const void* key, vtype key_type, const void* value, vtype value_type, void* data, map_access_callback) Nonnull__(1);
extern int libcdsb_map_find (vtype_map* x, const void* key, vtype key_type, void* data, map_access_callback, bool cut) Nonnull__(1);
extern int libcdsb_map_foreach(vtype_map* x, void* data, map_access_callback, rbforeach_t, bool flush) Nonnull__(1,3);
extern bool libcdsb_map_update (vtype_map* x, vtype_variable key, vtype_variable value, void* data, map_access_callback) Nonnull__(1);
extern bool libcdsb_map_inject (vtype_map* x, vtype_variable key, vtype_variable value, void* data, map_access_callback) Nonnull__(1);
extern int libcdsb_map_find (vtype_map* x, vtype_variable key, void* data, map_access_callback, bool cut) Nonnull__(1);
extern int libcdsb_map_foreach(vtype_map* x, void* data, map_access_callback, rbforeach_t, bool flush) Nonnull__(1,3);
#endif /* LIBCDSB_MAP_H */

View File

@ -22,7 +22,8 @@ static int libcdsb_builtin_foreach(map_t* x, void* data, map_access_callback cal
if (!mnode_is_empty(n->right)) cur = stack_insert(cur, n->right);
if (!r) {
r = callback(vnode_peek(&n->key, x->type), x->type, vnode_peek(&n->value, n->type), n->type, data);
r = callback(libcdsb_variable_build(vnode_peek(&n->key, x->type), x->type),
libcdsb_variable_build(vnode_peek(&n->value, n->type), n->type), data);
} else {
stack_flush(&z);
return r;
@ -45,7 +46,7 @@ static int libcdsb_builtin_foreach(map_t* x, void* data, map_access_callback cal
/*#####################################################################################################################*/
int libcdsb_map_find(map_t* x, const void* k, vtype t, void* _, map_access_callback callback, bool cut) {
int libcdsb_map_find(map_t* x, vtype_variable kvar, void* _, map_access_callback callback, bool cut) {
mnode_t* c;
void *key;
int cmp;
@ -54,10 +55,11 @@ int libcdsb_map_find(map_t* x, const void* k, vtype t, void* _, map_access_callb
while (!mnode_is_empty(c)) {
key = vnode_peek(&c->key, x->type);
cmp = vtype_compare(k, t, key, x->type);
cmp = vtype_compare(kvar.pointer, kvar.type, key, x->type);
if (cmp == 0) {
cmp = (callback) ? callback(key, x->type, vnode_peek(&c->value, c->type), c->type, _) : 0;
cmp = (callback) ? callback(libcdsb_variable_build(key, x->type),
libcdsb_variable_build(vnode_peek(&c->value, c->type), c->type), _) : 0;
if (cut) {
c = mnode_delete(&x->root, c);
@ -96,7 +98,8 @@ int libcdsb_map_foreach(map_t* x, void* data, map_access_callback callback, rbfo
while ((n = stack_pop(&iter))) {
if (!r) {
r = callback(vnode_peek(&n->key, x->type), x->type, vnode_peek(&n->value, n->type), n->type, data);
r = callback(libcdsb_variable_build(vnode_peek(&n->key, x->type), x->type),
libcdsb_variable_build(vnode_peek(&n->value, n->type), n->type), data);
} else if (!flush) {
stack_flush(&iter);
return r;

View File

@ -3,7 +3,7 @@
#include "include.h"
bool libcdsb_map_update(map_t* x, const void* k, vtype kt, const void* v, vtype vt, void* dt, map_access_callback callback) {
bool libcdsb_map_update(map_t* x, vtype_variable key, vtype_variable value, void* dt, map_access_callback callback) {
int cmp;
mnode_t* n;
mnode_t* p;
@ -11,15 +11,15 @@ bool libcdsb_map_update(map_t* x, const void* k, vtype kt, const void* v, vtype
if (!mnode_is_empty(n = x->root)) {
do {
p = n;
cmp = vtype_compare(k, kt, vnode_peek(&n->key, kt), kt);
cmp = vtype_compare(key.pointer, key.type, vnode_peek(&n->key, key.type), key.type);
if (cmp == 0) {
if (callback) callback(vnode_peek(&n->key, x->type), x->type,
vnode_peek(&n->value, n->type), n->type, dt);
if (callback) callback(libcdsb_variable_build(vnode_peek(&n->key, x->type), x->type),
libcdsb_variable_build(vnode_peek(&n->value, n->type), n->type), dt);
vnode_free(&n->value, n->type);
n->value = vnode_create(v, n->type = vt);
n->value = vnode_create(value.pointer, n->type = value.type);
return true;
}
@ -37,39 +37,38 @@ bool libcdsb_map_update(map_t* x, const void* k, vtype kt, const void* v, vtype
} else n = x->root = mnode_create(nullptr, mnode_empty, 0);
n->key = vnode_tcreate(x->type, k, kt);
n->value = vnode_create(v, vt);
n->type = vt;
n->key = vnode_tcreate(x->type, key.pointer, key.type);
n->value = vnode_create(value.pointer, n->type = value.type);
return false;
}
bool libcdsb_map_inject(map_t* x, const void* k, vtype kt, const void* v, vtype vt, void* dt, map_access_callback callback) {
bool libcdsb_map_inject(map_t* x, vtype_variable key, vtype_variable value, void* dt, map_access_callback callback) {
int cmp;
mnode_t* n;
mnode_t* p;
vnode_t kn;
vnode_tattach(&kn, x->type, k, kt);
n = x->root;
kt = x->type;
k = vnode_peek(&kn, kt);
vnode_tattach(&kn, x->type, key.pointer, key.type);
n = x->root;
key.type = x->type;
key.pointer = vnode_peek(&kn, key.type);
if (!mnode_is_empty(n)) {
do {
p = n;
cmp = vtype_compare(k, kt, vnode_peek(&n->key, kt), kt);
cmp = vtype_compare(key.pointer, key.type, vnode_peek(&n->key, key.type), key.type);
if (cmp == 0) {
if (callback) callback(vnode_peek(&n->key, x->type), x->type,
vnode_peek(&n->value, n->type), n->type, dt);
if (callback) callback(libcdsb_variable_build(vnode_peek(&n->key, x->type), x->type),
libcdsb_variable_build(vnode_peek(&n->value, n->type), n->type), dt);
vnode_free(&n->key, x->type);
vnode_free(&n->value, n->type);
n->key = kn;
vnode_attach(&n->value, v, n->type = vt);
vnode_attach(&n->value, value.pointer, n->type = value.type);
return true;
}
@ -87,7 +86,7 @@ bool libcdsb_map_inject(map_t* x, const void* k, vtype kt, const void* v, vtype
} else n = x->root = mnode_create(kn, mnode_empty, 0);
vnode_attach(&n->value, v, n->type = vt);
vnode_attach(&n->value, value.pointer, n->type = value.type);
return false;
}