Update array (vtype_variable)
This commit is contained in:
parent
554ea487cc
commit
c2144f04cf
@ -7,7 +7,7 @@
|
||||
#ifndef LIBCDSB_ARRAY_H
|
||||
#define LIBCDSB_ARRAY_H
|
||||
|
||||
typedef int (*array_access_callback)(void* value, ssize_t index, vtype type, void* data);
|
||||
typedef int (*array_access_callback)(vtype_variable, ssize_t index, void* data);
|
||||
|
||||
/*#####################################################################################################################*/
|
||||
|
||||
@ -22,15 +22,15 @@ inline void array_init (vtype_array* x, vtype type) { x->type = type; x->me
|
||||
|
||||
/*#####################################################################################################################*/
|
||||
|
||||
#define array_pop(x, value, data, callback) libcdsb_array_find (x, _LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value), data, callback, 0, 1)
|
||||
#define array_pop(x, value, data, callback) libcdsb_array_find (x, libcdsb_variable_build(_LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value)), data, callback, 0, 1)
|
||||
#define array_get array_find
|
||||
#define array_pop_by_index(x, index, data, callback) libcdsb_array_get (x, index, data, callback, 1)
|
||||
#define array_get_by_index(x, index, data, callback) libcdsb_array_get (x, index, data, callback, 0)
|
||||
#define array_find(x, value, data, callback) libcdsb_array_find (x, _LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value), data, callback, 0, 0)
|
||||
#define array_rfind(x, value, data, callback) libcdsb_array_find (x, _LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value), data, callback, 1, 0)
|
||||
#define array_countof(x, value) libcdsb_array_count (x, _LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value))
|
||||
#define array_push_back(x, value) libcdsb_array_insert (x, _LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value))
|
||||
#define array_attach_back(x, value) libcdsb_array_attach (x, _LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value))
|
||||
#define array_find(x, value, data, callback) libcdsb_array_find (x, libcdsb_variable_build(_LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value)), data, callback, 0, 0)
|
||||
#define array_rfind(x, value, data, callback) libcdsb_array_find (x, libcdsb_variable_build(_LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value)), data, callback, 1, 0)
|
||||
#define array_countof(x, value) libcdsb_array_count (x, libcdsb_variable_build(_LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value)))
|
||||
#define array_push_back(x, value) libcdsb_array_insert (x, libcdsb_variable_build(_LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value)))
|
||||
#define array_attach_back(x, value) libcdsb_array_attach (x, libcdsb_variable_build(_LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value)))
|
||||
#define array_foreach(x, data, callback) libcdsb_array_foreach(x, data, callback, 0)
|
||||
#define array_remove(x, value) array_pop (x, value, 0, 0)
|
||||
#define array_remove_by_index(x, index) array_pop_by_index (x, index, 0, 0)
|
||||
@ -39,12 +39,12 @@ inline void array_init (vtype_array* x, vtype type) { x->type = type; x->me
|
||||
|
||||
/*#####################################################################################################################*/
|
||||
|
||||
extern ssize_t libcdsb_array_insert (vtype_array* x, const void* value, vtype type) Nonnull__(1);
|
||||
extern ssize_t libcdsb_array_attach (vtype_array* x, const void* value, vtype type) Nonnull__(1);
|
||||
extern int libcdsb_array_find (vtype_array* x, const void* value, vtype type, void* data, array_access_callback, bool reverse, bool cut) Nonnull__(1);
|
||||
extern int libcdsb_array_get (vtype_array* x, ssize_t index, void* data, array_access_callback, bool cut) Nonnull__(1);
|
||||
extern int libcdsb_array_foreach (vtype_array* x, void* data, array_access_callback, bool flush) Nonnull__(1,3);
|
||||
extern ssize_t libcdsb_array_insert (vtype_array* x, vtype_variable) Nonnull__(1);
|
||||
extern ssize_t libcdsb_array_attach (vtype_array* x, vtype_variable) Nonnull__(1);
|
||||
extern int libcdsb_array_find (vtype_array* x, vtype_variable, void* data, array_access_callback, bool reverse, bool cut) Nonnull__(1);
|
||||
extern int libcdsb_array_get (vtype_array* x, ssize_t index, void* data, array_access_callback, bool cut) Nonnull__(1);
|
||||
extern int libcdsb_array_foreach (vtype_array* x, void* data, array_access_callback, bool flush) Nonnull__(1,3);
|
||||
|
||||
extern size_t libcdsb_array_count(const vtype_array* s, const void* value, vtype type) Pure__ Warn_unused_result__ Nonnull__(1);
|
||||
extern size_t libcdsb_array_count(const vtype_array* s, vtype_variable) Pure__ Warn_unused_result__ Nonnull__(1);
|
||||
|
||||
#endif /* LIBCDSB_ARRAY_H */
|
||||
|
@ -16,14 +16,14 @@ int libcdsb_array_get(vtype_array* x, ssize_t i, void* _, array_access_callback
|
||||
if (i < 0 && (i += x->size) < 0) i = 0;
|
||||
|
||||
if (i < x->size) {
|
||||
if (callback) r = callback(array_internal_at(x, i), i, x->type, _);
|
||||
if (callback) r = callback(libcdsb_variable_build(array_internal_at(x, i), x->type), i, _);
|
||||
if (cut) array_cut(x, i, 1);
|
||||
} else return -1;
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
int libcdsb_array_find(arr_t* x, const void* v, vtype t, void* _, array_access_callback callback, bool r, bool cut) {
|
||||
int libcdsb_array_find(arr_t* x, vtype_variable var, void* _, array_access_callback callback, bool r, bool cut) {
|
||||
void *p;
|
||||
ssize_t i;
|
||||
int cmp;
|
||||
@ -35,7 +35,7 @@ int libcdsb_array_find(arr_t* x, const void* v, vtype t, void* _, array_access_c
|
||||
i = 0;
|
||||
|
||||
do {
|
||||
cmp = vtype_compare(p, x->type, v, t);
|
||||
cmp = vtype_compare(p, x->type, var.pointer, var.type);
|
||||
|
||||
if (cmp == 0) break;
|
||||
|
||||
@ -50,7 +50,7 @@ int libcdsb_array_find(arr_t* x, const void* v, vtype t, void* _, array_access_c
|
||||
|
||||
while (i--) {
|
||||
p -= vtype_size(x->type);
|
||||
cmp = vtype_compare(p, x->type, v, t);
|
||||
cmp = vtype_compare(p, x->type, var.pointer, var.type);
|
||||
|
||||
if (cmp == 0) break;
|
||||
}
|
||||
@ -58,7 +58,7 @@ int libcdsb_array_find(arr_t* x, const void* v, vtype t, void* _, array_access_c
|
||||
if (i < 0) return i;
|
||||
}
|
||||
|
||||
if (callback) cmp = callback(p, i, x->type, _);
|
||||
if (callback) cmp = callback(libcdsb_variable_build(p, x->type), i, _);
|
||||
if (cut) array_cut(x, i, 1);
|
||||
|
||||
return cmp;
|
||||
@ -77,7 +77,7 @@ int libcdsb_array_foreach(vtype_array* x, void* data, array_access_callback call
|
||||
r = 0;
|
||||
|
||||
while (p < e) {
|
||||
if ((r = callback(p, n, x->type, data)))
|
||||
if ((r = callback(libcdsb_variable_build(p, x->type), n, data)))
|
||||
break;
|
||||
|
||||
p += vtype_size(x->type);
|
||||
|
@ -24,7 +24,7 @@ hash_t array_hash(const arr_t* s) {
|
||||
}
|
||||
|
||||
|
||||
size_t libcdsb_array_count(const arr_t* s, const void* v, vtype t) {
|
||||
size_t libcdsb_array_count(const arr_t* s, vtype_variable var) {
|
||||
void *p;
|
||||
void *e;
|
||||
int cmp;
|
||||
@ -35,7 +35,7 @@ size_t libcdsb_array_count(const arr_t* s, const void* v, vtype t) {
|
||||
n = 0;
|
||||
|
||||
do {
|
||||
cmp = vtype_compare(p, s->type, v, t);
|
||||
cmp = vtype_compare(p, s->type, var.pointer, var.type);
|
||||
|
||||
if (cmp == 0) ++n;
|
||||
|
||||
|
@ -5,47 +5,47 @@
|
||||
#include "../__internal/assert.h"
|
||||
#include "../__internal/vnode.h"
|
||||
|
||||
ssize_t libcdsb_array_insert(arr_t* x, const void* v, vtype t) {
|
||||
ssize_t libcdsb_array_insert(arr_t* x, vtype_variable var) {
|
||||
ssize_t i;
|
||||
vnode_t n;
|
||||
|
||||
i = x->size;
|
||||
n = vnode_tcreate(x->type, v, t);
|
||||
n = vnode_tcreate(x->type, var.pointer, var.type);
|
||||
x->mem = realloc(x->mem, ++x->size * vtype_size(x->type));
|
||||
|
||||
if (t < VTYPE_STRING) {
|
||||
n = vnode_tcreate(x->type, v, t);
|
||||
if (var.type < VTYPE_STRING) {
|
||||
n = vnode_tcreate(x->type, var.pointer, var.type);
|
||||
memcpy(array_internal_at(x, i), vnode_peek(&n, x->type), vtype_size(x->type));
|
||||
|
||||
if (vtype_size(x->type) > sizeof(vnode_t))
|
||||
vnode_free(&n, x->type);
|
||||
|
||||
} else {
|
||||
type_assert(x->type, t);
|
||||
copy_init(array_internal_at(x, i), v, t);
|
||||
type_assert(x->type, var.type);
|
||||
copy_init(array_internal_at(x, i), var.pointer, var.type);
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
|
||||
ssize_t libcdsb_array_attach(arr_t* x, const void* v, vtype t) {
|
||||
ssize_t libcdsb_array_attach(arr_t* x, vtype_variable var) {
|
||||
ssize_t i;
|
||||
vnode_t n;
|
||||
|
||||
i = x->size;
|
||||
x->mem = realloc(x->mem, ++x->size * vtype_size(x->type));
|
||||
|
||||
if (t < VTYPE_STRING) {
|
||||
n = vnode_tcreate(x->type, v, t);
|
||||
if (var.type < VTYPE_STRING) {
|
||||
n = vnode_tcreate(x->type, var.pointer, var.type);
|
||||
memcpy(array_internal_at(x, i), vnode_peek(&n, x->type), vtype_size(x->type));
|
||||
|
||||
if (vtype_size(x->type) > sizeof(vnode_t))
|
||||
vnode_free(&n, x->type);
|
||||
|
||||
} else {
|
||||
type_assert(x->type, t);
|
||||
memcpy(array_internal_at(x, i), v, vtype_size(t));
|
||||
type_assert(x->type, var.type);
|
||||
memcpy(array_internal_at(x, i), var.pointer, vtype_size(var.type));
|
||||
}
|
||||
|
||||
return i;
|
||||
|
Loading…
Reference in New Issue
Block a user