Refactor array headers

This commit is contained in:
2022-06-04 21:52:00 +03:00
parent 53fab0bb6c
commit a7aacd9160
6 changed files with 39 additions and 37 deletions
+5
View File
@@ -12,6 +12,11 @@ size_t array_nmemb(const arr_t* x) {
return x->size*vtype_size(x->type);
}
void* array_at(const arr_t* x, ssize_t i) {
if (i < 0 && (i += x->size) < 0) i = 0;
return x->mem + i*vtype_size(x->type);
}
void array_init(arr_t* x, vtype t) {
x->type = t;
x->size = 0;
+23
View File
@@ -50,3 +50,26 @@ ssize_t libcdsb_array_push(arr_t* x, const void* v, vtype vt) {
return i;
}
ssize_t libcdsb_array_get(val_t* x, arr_t* s, ssize_t i, _Bool cut) {
if (i < 0 && (i += s->size) < 0) i = 0;
if (i < s->size) {
assert(!is_null(s->mem));
if (cut) {
if (!is_null(x)) {
vnode_t n = vnode_create(array_internal_at(s, i), s->type);
value_set(x, n, s->type, VF_WRITEABLE|VF_REMOVABLE);
}
array_cut(s, i, 1);
} else value_set(x, array_internal_at(s, i), s->type, VF_WRITEABLE);
} else {
i = -1;
memset(x, 0, sizeof(*x));
}
return i;
}
-28
View File
@@ -5,34 +5,6 @@
#include "../__internal/assert.h"
#include "../__internal/vnode.h"
void* libcdsb_array_at(const arr_t* x, ssize_t i) {
if (i < 0 && (i += x->size) < 0) i = 0;
return x->mem + i*vtype_size(x->type);
}
ssize_t libcdsb_array_get(val_t* x, arr_t* s, ssize_t i, _Bool cut) {
if (i < 0 && (i += s->size) < 0) i = 0;
if (i < s->size) {
assert(!is_null(s->mem));
if (cut) {
if (!is_null(x)) {
vnode_t n = vnode_create(array_internal_at(s, i), s->type);
value_set(x, n, s->type, VF_WRITEABLE|VF_REMOVABLE);
}
array_cut(s, i, 1);
} else value_set(x, array_internal_at(s, i), s->type, VF_WRITEABLE);
} else {
i = -1;
memset(x, 0, sizeof(*x));
}
return i;
}
_Bool array_slice(arr_t* x, arr_t* s, ssize_t i, size_t n, _Bool cut) {
if (n && s->size) {
assert(!is_null(s->mem));