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"
|
|
|
|
|
2022-08-19 13:41:19 +03:00
|
|
|
void* at_array(const arr_t* x, ssize_t i) {
|
|
|
|
if (i < 0 && (i += x->size) < 0)
|
|
|
|
i = 0;
|
|
|
|
return x->mem + i * vtype_size(x->type);
|
2022-06-02 15:52:43 +03:00
|
|
|
}
|
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-09 16:26:11 +03:00
|
|
|
int libcdsb_array_find(arr_t* x, const void* v, vtype t, void* _, array_access_callback callback, bool r, bool cut) {
|
|
|
|
void *p;
|
|
|
|
ssize_t i;
|
|
|
|
int cmp;
|
|
|
|
|
|
|
|
if (!x->size) return -1;
|
|
|
|
|
|
|
|
if (!r) {
|
|
|
|
p = x->mem;
|
|
|
|
i = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
cmp = vtype_compare(p, x->type, v, t);
|
|
|
|
|
|
|
|
if (cmp == 0) break;
|
|
|
|
|
|
|
|
p += vtype_size(x->type);
|
|
|
|
++i;
|
|
|
|
} while (i < x->size);
|
|
|
|
|
|
|
|
if (i >= x->size) return -1;
|
|
|
|
} else {
|
|
|
|
p = array_end(x);
|
|
|
|
i = x->size;
|
|
|
|
|
|
|
|
while (i--) {
|
|
|
|
p -= vtype_size(x->type);
|
|
|
|
cmp = vtype_compare(p, x->type, v, t);
|
|
|
|
|
|
|
|
if (cmp == 0) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i < 0) return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (callback) cmp = callback(p, i, x->type, _);
|
|
|
|
if (cut) array_cut(x, i, 1);
|
|
|
|
|
|
|
|
return cmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|