2022-06-02 15:52:43 +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"
|
|
|
|
|
2022-06-04 21:10:19 +03:00
|
|
|
ssize_t libcdsb_array_find(const arr_t* x, const void* v, vtype vt) {
|
2022-06-02 15:52:43 +03:00
|
|
|
int c;
|
|
|
|
ssize_t index;
|
|
|
|
|
|
|
|
if (is_integer(x->type)) {
|
|
|
|
tvalue_assert(vt);
|
|
|
|
} else if (is_float(x->type)) {
|
|
|
|
tvalue_assert(vt);
|
|
|
|
} else type_assert(x->type, vt);
|
|
|
|
|
|
|
|
index = -1;
|
|
|
|
|
2022-06-03 11:58:02 +03:00
|
|
|
if (!x->size)
|
|
|
|
return index;
|
2022-06-02 15:52:43 +03:00
|
|
|
|
2022-06-03 11:58:02 +03:00
|
|
|
void* p = x->mem;
|
|
|
|
void* e = array_end(x);
|
2022-06-02 15:52:43 +03:00
|
|
|
|
2022-06-03 11:58:02 +03:00
|
|
|
assert(!is_null(x->mem));
|
2022-06-02 15:52:43 +03:00
|
|
|
|
2022-06-03 14:10:38 +03:00
|
|
|
do {
|
|
|
|
++index;
|
|
|
|
c = vtype_compare(p, x->type, v, vt);
|
2022-06-02 15:52:43 +03:00
|
|
|
|
2022-06-03 14:10:38 +03:00
|
|
|
if (c == 0)
|
|
|
|
return index;
|
2022-06-02 15:52:43 +03:00
|
|
|
|
2022-06-03 14:10:38 +03:00
|
|
|
p += vtype_size(x->type);
|
|
|
|
} while (p < e);
|
2022-06-02 15:52:43 +03:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-06-08 20:56:59 +03:00
|
|
|
|
2022-06-04 21:10:19 +03:00
|
|
|
ssize_t libcdsb_array_push(arr_t* x, const void* v, vtype vt) {
|
2022-06-03 11:58:02 +03:00
|
|
|
ssize_t i = x->size;
|
2022-06-02 15:52:43 +03:00
|
|
|
vnode_t n = vnode_tcreate(x->type, v, vt);
|
|
|
|
|
2022-06-03 11:58:02 +03:00
|
|
|
x->mem = realloc(x->mem, ++x->size * vtype_size(x->type));
|
|
|
|
memcpy(array_internal_at(x, i), vnode_peek(&n, x->type), vtype_size(x->type));
|
2022-06-02 15:52:43 +03:00
|
|
|
|
2022-06-03 11:58:02 +03:00
|
|
|
if (vtype_size(x->type) > sizeof(void*) && x->type < VTYPE_STRING)
|
|
|
|
vnode_free(&n, x->type);
|
2022-06-02 15:52:43 +03:00
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
2022-06-04 21:52:00 +03:00
|
|
|
|
|
|
|
|
2022-06-08 20:56:59 +03:00
|
|
|
int libcdsb_array_get(vtype_array* x, ssize_t i, void* _, array_access_callback callback, vtype_bool cut) {
|
2022-06-04 21:52:00 +03:00
|
|
|
|
2022-06-08 20:56:59 +03:00
|
|
|
int r = 0;
|
2022-06-04 21:52:00 +03:00
|
|
|
|
2022-06-08 20:56:59 +03:00
|
|
|
if (i < 0 && (i += x->size) < 0) i = 0;
|
2022-06-04 21:52:00 +03:00
|
|
|
|
2022-06-08 20:56:59 +03:00
|
|
|
if (i < x->size) {
|
|
|
|
if (callback) r = callback(array_internal_at(x, i), i, x->type, _);
|
|
|
|
if (cut) array_cut(x, i, 1);
|
|
|
|
} else return -1;
|
2022-06-04 21:52:00 +03:00
|
|
|
|
2022-06-08 20:56:59 +03:00
|
|
|
return r;
|
2022-06-04 21:52:00 +03:00
|
|
|
}
|
2022-06-05 20:01:10 +03:00
|
|
|
|
|
|
|
|
2022-06-08 20:56:59 +03:00
|
|
|
int libcdsb_array_foreach(vtype_array* x, void* data, array_access_callback callback, _Bool flush) {
|
2022-06-05 20:01:10 +03:00
|
|
|
|
|
|
|
void* p;
|
|
|
|
void* e;
|
|
|
|
size_t n;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
p = x->mem;
|
|
|
|
e = x->mem + x->size*vtype_size(x->type);
|
|
|
|
n = 0;
|
2022-06-08 09:57:41 +03:00
|
|
|
r = 0;
|
2022-06-05 20:01:10 +03:00
|
|
|
|
|
|
|
while (p < e) {
|
2022-06-07 21:42:11 +03:00
|
|
|
if ((r = callback(p, n, x->type, data)))
|
2022-06-08 09:57:41 +03:00
|
|
|
break;
|
2022-06-05 20:01:10 +03:00
|
|
|
|
|
|
|
p += vtype_size(x->type);
|
|
|
|
++n;
|
|
|
|
}
|
|
|
|
|
2022-06-08 09:57:41 +03:00
|
|
|
if (flush) {
|
|
|
|
free(x->mem);
|
|
|
|
memset(x, 0, sizeof(*x));
|
|
|
|
}
|
|
|
|
return r;
|
2022-06-05 20:01:10 +03:00
|
|
|
}
|