Refactor array headers

This commit is contained in:
Gregory Lirent 2022-06-04 21:52:00 +03:00
parent 53fab0bb6c
commit a7aacd9160
6 changed files with 39 additions and 37 deletions

View File

@ -6,6 +6,7 @@
#define LIBCDSB_nt__ __attribute__ ((nothrow))
#define LIBCDSB_nn1__ __attribute__ ((nonnull (1)))
#define LIBCDSB_nn2__ __attribute__ ((nonnull (2)))
#define LIBCDSB_nn12__ __attribute__ ((nonnull (1,2)))
#define LIBCDSB_nn123__ __attribute__ ((nonnull (1,2,3)))
#define LIBCDSB_nn13__ __attribute__ ((nonnull (1,3)))

View File

@ -10,6 +10,8 @@
/*#####################################################################################################################*/
extern void array_init(vtype_array* x, vtype type) LIBCDSB_nt__ LIBCDSB_nn1__;
extern void* array_at(const vtype_array* s, ssize_t index) LIBCDSB_nt__ LIBCDSB_nn1__;
extern _Bool array_slice(vtype_array* x, vtype_array* src, ssize_t index, size_t count, _Bool cut) LIBCDSB_nt__ LIBCDSB_nn1__;
extern void array_sort (vtype_array* x) LIBCDSB_nt__ LIBCDSB_nn1__;
@ -18,15 +20,8 @@ extern void array_reverse(vtype_array* x) LIBCDSB_nt__ LIBCDSB_nn1__;
#define array_push(x, value) _LIBCDSB_Generic(libcdsb_array, push, value)(x, value)
#define array_indexof(x, value) _LIBCDSB_Generic(libcdsb_array, indexof, value)(x, value)
#define array_get(x, s, index) libcdsb_array_get(x, s, index, 0)
#define array_pop(x, s, index) libcdsb_array_get(x, s, index, 1)
#define array_remove(s, index) libcdsb_array_get(0, s, index, 1)
#define in_array(x, value) (array_indexof(x, value) >= 0)
extern void* libcdsb_array_at (const vtype_array* s, ssize_t index);
extern ssize_t libcdsb_array_get(vtype_value* x, vtype_array* s, ssize_t index, _Bool cut);
/*#####################################################################################################################*/
extern void libcdsb_array_push_pointer(vtype_array* x, const void* value) LIBCDSB_nt__ LIBCDSB_nn1__;

View File

@ -6,7 +6,13 @@
#ifndef LIBCDSB_EXTRA_ARRAY_H
#define LIBCDSB_EXTRA_ARRAY_H
extern ssize_t libcdsb_array_find(const vtype_array* x, const void* value, vtype value_type);
extern ssize_t libcdsb_array_push( vtype_array* x, const void* value, vtype value_type);
#define array_get(x, s, index) libcdsb_array_get(x, s, index, 0)
#define array_pop(x, s, index) libcdsb_array_get(x, s, index, 1)
#define array_remove(s, index) libcdsb_array_get(0, s, index, 1)
extern ssize_t libcdsb_array_get(vtype_value* x, vtype_array* s, ssize_t index, _Bool cut) LIBCDSB_nt__ LIBCDSB_nn2__;
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__;
#endif /* LIBCDSB_EXTRA_ARRAY_H */

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;

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;
}

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));