2022-08-19 13:41:19 +03:00
|
|
|
/* This software is licensed by the MIT License, see LICENSE file */
|
|
|
|
/* Copyright © 2022 Gregory Lirent */
|
|
|
|
|
|
|
|
#include "include.h"
|
|
|
|
#include "../__internal/assert.h"
|
|
|
|
#include "../__internal/vnode.h"
|
|
|
|
|
|
|
|
ssize_t libcdsb_array_insert(arr_t* x, const void* v, vtype t) {
|
|
|
|
ssize_t i;
|
|
|
|
vnode_t n;
|
|
|
|
|
|
|
|
i = x->size;
|
|
|
|
n = vnode_tcreate(x->type, v, t);
|
|
|
|
x->mem = realloc(x->mem, ++x->size * vtype_size(x->type));
|
|
|
|
|
|
|
|
if (t < VTYPE_STRING) {
|
|
|
|
n = vnode_tcreate(x->type, v, t);
|
|
|
|
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);
|
|
|
|
|
2022-08-19 20:00:10 +03:00
|
|
|
} else {
|
|
|
|
type_assert(x->type, t);
|
2022-08-22 11:59:09 +03:00
|
|
|
copy_init(array_internal_at(x, i), v, t);
|
2022-08-19 13:41:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2022-08-19 20:00:10 +03:00
|
|
|
|
2022-08-19 13:41:19 +03:00
|
|
|
ssize_t libcdsb_array_attach(arr_t* x, const void* v, vtype t) {
|
|
|
|
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);
|
|
|
|
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);
|
|
|
|
|
2022-08-19 20:00:10 +03:00
|
|
|
} else {
|
|
|
|
type_assert(x->type, t);
|
2022-08-19 13:41:19 +03:00
|
|
|
memcpy(array_internal_at(x, i), v, vtype_size(t));
|
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|