Array interface standardization

This commit is contained in:
2022-06-09 16:26:11 +03:00
parent 41c46ed1f9
commit 5a8a7ee6ef
4 changed files with 165 additions and 89 deletions
+67 -37
View File
@@ -5,43 +5,9 @@
#include "../__internal/assert.h"
#include "../__internal/vnode.h"
ssize_t libcdsb_array_find(const arr_t* x, const void* v, vtype vt) {
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;
if (!x->size)
return index;
void* p = x->mem;
void* e = array_end(x);
assert(!is_null(x->mem));
do {
++index;
c = vtype_compare(p, x->type, v, vt);
if (c == 0)
return index;
p += vtype_size(x->type);
} while (p < e);
return -1;
}
ssize_t libcdsb_array_push(arr_t* x, const void* v, vtype vt) {
ssize_t libcdsb_array_push(arr_t* x, const void* v, vtype t) {
ssize_t i = x->size;
vnode_t n = vnode_tcreate(x->type, v, vt);
vnode_t n = vnode_tcreate(x->type, v, t);
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));
@@ -53,6 +19,28 @@ ssize_t libcdsb_array_push(arr_t* x, const void* v, vtype vt) {
}
size_t libcdsb_array_count(const arr_t* s, const void* v, vtype t) {
void *p;
void *e;
int cmp;
size_t n;
p = s->mem;
e = array_end(s);
n = 0;
do {
cmp = vtype_compare(p, s->type, v, t);
if (cmp == 0) ++n;
p += vtype_size(s->type);
} while (p < e);
return n;
}
int libcdsb_array_get(vtype_array* x, ssize_t i, void* _, array_access_callback callback, vtype_bool cut) {
int r = 0;
@@ -68,7 +56,49 @@ int libcdsb_array_get(vtype_array* x, ssize_t i, void* _, array_access_callback
}
int libcdsb_array_foreach(vtype_array* x, void* data, array_access_callback callback, _Bool flush) {
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) {
void* p;
void* e;
+40 -20
View File
@@ -3,25 +3,45 @@
#include "include.h"
ssize_t libcdsb_array_indexof_pointer(const arr_t* x, const void* v) { return libcdsb_array_find(x, &v, vtypeof(&v)); }
ssize_t libcdsb_array_indexof_string (const arr_t* x, const char* v) { return libcdsb_array_find(x, &v, vtypeof(&v)); }
ssize_t libcdsb_array_indexof_array (const arr_t* x, const str_t* v) { return libcdsb_array_find(x, v, vtypeof( v)); }
ssize_t libcdsb_array_indexof_list (const arr_t* x, const arr_t* v) { return libcdsb_array_find(x, v, vtypeof( v)); }
ssize_t libcdsb_array_indexof_map (const arr_t* x, const list_t* v) { return libcdsb_array_find(x, v, vtypeof( v)); }
ssize_t libcdsb_array_indexof_vset (const arr_t* x, const map_t* v) { return libcdsb_array_find(x, v, vtypeof( v)); }
ssize_t libcdsb_array_indexof_cstring(const arr_t* x, const set_t* v) { return libcdsb_array_find(x, v, vtypeof( v)); }
ssize_t libcdsb_array_indexof_boolean(const arr_t* x, _Bool v) { return libcdsb_array_find(x, &v, vtypeof(&v)); }
ssize_t libcdsb_array_indexof_int8 (const arr_t* x, s8_t v) { return libcdsb_array_find(x, &v, vtypeof(&v)); }
ssize_t libcdsb_array_indexof_int16 (const arr_t* x, s16_t v) { return libcdsb_array_find(x, &v, vtypeof(&v)); }
ssize_t libcdsb_array_indexof_int32 (const arr_t* x, s32_t v) { return libcdsb_array_find(x, &v, vtypeof(&v)); }
ssize_t libcdsb_array_indexof_int64 (const arr_t* x, s64_t v) { return libcdsb_array_find(x, &v, vtypeof(&v)); }
ssize_t libcdsb_array_indexof_uint8 (const arr_t* x, u8_t v) { return libcdsb_array_find(x, &v, vtypeof(&v)); }
ssize_t libcdsb_array_indexof_uint16 (const arr_t* x, u16_t v) { return libcdsb_array_find(x, &v, vtypeof(&v)); }
ssize_t libcdsb_array_indexof_uint32 (const arr_t* x, u32_t v) { return libcdsb_array_find(x, &v, vtypeof(&v)); }
ssize_t libcdsb_array_indexof_uint64 (const arr_t* x, u64_t v) { return libcdsb_array_find(x, &v, vtypeof(&v)); }
ssize_t libcdsb_array_indexof_float (const arr_t* x, fl_t v) { return libcdsb_array_find(x, &v, vtypeof(&v)); }
ssize_t libcdsb_array_indexof_double (const arr_t* x, dbl_t v) { return libcdsb_array_find(x, &v, vtypeof(&v)); }
ssize_t libcdsb_array_indexof_ldouble(const arr_t* x, ldbl_t v) { return libcdsb_array_find(x, &v, vtypeof(&v)); }
int libcdsb_array_find_pointer(arr_t* x, const void* v, void* _, array_access_callback cb, bool r, bool cut) { return libcdsb_array_find(x, &v, vtypeof(&v), _, cb, r, cut); }
int libcdsb_array_find_string (arr_t* x, const char* v, void* _, array_access_callback cb, bool r, bool cut) { return libcdsb_array_find(x, &v, vtypeof(&v), _, cb, r, cut); }
int libcdsb_array_find_array (arr_t* x, const str_t* v, void* _, array_access_callback cb, bool r, bool cut) { return libcdsb_array_find(x, v, vtypeof( v), _, cb, r, cut); }
int libcdsb_array_find_list (arr_t* x, const arr_t* v, void* _, array_access_callback cb, bool r, bool cut) { return libcdsb_array_find(x, v, vtypeof( v), _, cb, r, cut); }
int libcdsb_array_find_map (arr_t* x, const list_t* v, void* _, array_access_callback cb, bool r, bool cut) { return libcdsb_array_find(x, v, vtypeof( v), _, cb, r, cut); }
int libcdsb_array_find_vset (arr_t* x, const map_t* v, void* _, array_access_callback cb, bool r, bool cut) { return libcdsb_array_find(x, v, vtypeof( v), _, cb, r, cut); }
int libcdsb_array_find_cstring(arr_t* x, const set_t* v, void* _, array_access_callback cb, bool r, bool cut) { return libcdsb_array_find(x, v, vtypeof( v), _, cb, r, cut); }
int libcdsb_array_find_boolean(arr_t* x, bool v, void* _, array_access_callback cb, bool r, bool cut) { return libcdsb_array_find(x, &v, vtypeof(&v), _, cb, r, cut); }
int libcdsb_array_find_int8 (arr_t* x, s8_t v, void* _, array_access_callback cb, bool r, bool cut) { return libcdsb_array_find(x, &v, vtypeof(&v), _, cb, r, cut); }
int libcdsb_array_find_int16 (arr_t* x, s16_t v, void* _, array_access_callback cb, bool r, bool cut) { return libcdsb_array_find(x, &v, vtypeof(&v), _, cb, r, cut); }
int libcdsb_array_find_int32 (arr_t* x, s32_t v, void* _, array_access_callback cb, bool r, bool cut) { return libcdsb_array_find(x, &v, vtypeof(&v), _, cb, r, cut); }
int libcdsb_array_find_int64 (arr_t* x, s64_t v, void* _, array_access_callback cb, bool r, bool cut) { return libcdsb_array_find(x, &v, vtypeof(&v), _, cb, r, cut); }
int libcdsb_array_find_uint8 (arr_t* x, u8_t v, void* _, array_access_callback cb, bool r, bool cut) { return libcdsb_array_find(x, &v, vtypeof(&v), _, cb, r, cut); }
int libcdsb_array_find_uint16 (arr_t* x, u16_t v, void* _, array_access_callback cb, bool r, bool cut) { return libcdsb_array_find(x, &v, vtypeof(&v), _, cb, r, cut); }
int libcdsb_array_find_uint32 (arr_t* x, u32_t v, void* _, array_access_callback cb, bool r, bool cut) { return libcdsb_array_find(x, &v, vtypeof(&v), _, cb, r, cut); }
int libcdsb_array_find_uint64 (arr_t* x, u64_t v, void* _, array_access_callback cb, bool r, bool cut) { return libcdsb_array_find(x, &v, vtypeof(&v), _, cb, r, cut); }
int libcdsb_array_find_float (arr_t* x, fl_t v, void* _, array_access_callback cb, bool r, bool cut) { return libcdsb_array_find(x, &v, vtypeof(&v), _, cb, r, cut); }
int libcdsb_array_find_double (arr_t* x, dbl_t v, void* _, array_access_callback cb, bool r, bool cut) { return libcdsb_array_find(x, &v, vtypeof(&v), _, cb, r, cut); }
int libcdsb_array_find_ldouble(arr_t* x, ldbl_t v, void* _, array_access_callback cb, bool r, bool cut) { return libcdsb_array_find(x, &v, vtypeof(&v), _, cb, r, cut); }
size_t libcdsb_array_count_pointer(const arr_t* x, const void* v) { return libcdsb_array_count(x, &v, vtypeof(&v)); }
size_t libcdsb_array_count_cstring(const arr_t* x, const char* v) { return libcdsb_array_count(x, &v, vtypeof(&v)); }
size_t libcdsb_array_count_string (const arr_t* x, const str_t* v) { return libcdsb_array_count(x, v, vtypeof( v)); }
size_t libcdsb_array_count_array (const arr_t* x, const arr_t* v) { return libcdsb_array_count(x, v, vtypeof( v)); }
size_t libcdsb_array_count_list (const arr_t* x, const list_t* v) { return libcdsb_array_count(x, v, vtypeof( v)); }
size_t libcdsb_array_count_map (const arr_t* x, const map_t* v) { return libcdsb_array_count(x, v, vtypeof( v)); }
size_t libcdsb_array_count_vset (const arr_t* x, const set_t* v) { return libcdsb_array_count(x, v, vtypeof( v)); }
size_t libcdsb_array_count_boolean(const arr_t* x, bool v) { return libcdsb_array_count(x, &v, vtypeof(&v)); }
size_t libcdsb_array_count_int8 (const arr_t* x, s8_t v) { return libcdsb_array_count(x, &v, vtypeof(&v)); }
size_t libcdsb_array_count_int16 (const arr_t* x, s16_t v) { return libcdsb_array_count(x, &v, vtypeof(&v)); }
size_t libcdsb_array_count_int32 (const arr_t* x, s32_t v) { return libcdsb_array_count(x, &v, vtypeof(&v)); }
size_t libcdsb_array_count_int64 (const arr_t* x, s64_t v) { return libcdsb_array_count(x, &v, vtypeof(&v)); }
size_t libcdsb_array_count_uint8 (const arr_t* x, u8_t v) { return libcdsb_array_count(x, &v, vtypeof(&v)); }
size_t libcdsb_array_count_uint16 (const arr_t* x, u16_t v) { return libcdsb_array_count(x, &v, vtypeof(&v)); }
size_t libcdsb_array_count_uint32 (const arr_t* x, u32_t v) { return libcdsb_array_count(x, &v, vtypeof(&v)); }
size_t libcdsb_array_count_uint64 (const arr_t* x, u64_t v) { return libcdsb_array_count(x, &v, vtypeof(&v)); }
size_t libcdsb_array_count_float (const arr_t* x, fl_t v) { return libcdsb_array_count(x, &v, vtypeof(&v)); }
size_t libcdsb_array_count_double (const arr_t* x, dbl_t v) { return libcdsb_array_count(x, &v, vtypeof(&v)); }
size_t libcdsb_array_count_ldouble(const arr_t* x, ldbl_t v) { return libcdsb_array_count(x, &v, vtypeof(&v)); }
void libcdsb_array_push_pointer(arr_t* x, const void* v) { libcdsb_array_push(x, &v, vtypeof(&v)); }
void libcdsb_array_push_cstring(arr_t* x, const char* v) { libcdsb_array_push(x, &v, vtypeof(&v)); }
@@ -30,7 +50,7 @@ void libcdsb_array_push_array (arr_t* x, const arr_t* v) { libcdsb_array_push(
void libcdsb_array_push_list (arr_t* x, const list_t* v) { libcdsb_array_push(x, v, vtypeof( v)); }
void libcdsb_array_push_map (arr_t* x, const map_t* v) { libcdsb_array_push(x, v, vtypeof( v)); }
void libcdsb_array_push_vset (arr_t* x, const set_t* v) { libcdsb_array_push(x, v, vtypeof( v)); }
void libcdsb_array_push_boolean(arr_t* x, _Bool v) { libcdsb_array_push(x, &v, vtypeof(&v)); }
void libcdsb_array_push_boolean(arr_t* x, bool v) { libcdsb_array_push(x, &v, vtypeof(&v)); }
void libcdsb_array_push_int8 (arr_t* x, s8_t v) { libcdsb_array_push(x, &v, vtypeof(&v)); }
void libcdsb_array_push_int16 (arr_t* x, s16_t v) { libcdsb_array_push(x, &v, vtypeof(&v)); }
void libcdsb_array_push_int32 (arr_t* x, s32_t v) { libcdsb_array_push(x, &v, vtypeof(&v)); }