From 4c3af1db79222bd34623273ec4b2b2548a209415 Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Thu, 2 Jun 2022 15:52:43 +0300 Subject: [PATCH] Add array --- include/array.h | 68 +++++++++++++++++++++++++ include/extra/array.h | 19 +++++++ src/array/_Generics.c | 44 +++++++++++++++++ src/array/base-copy.c | 112 ++++++++++++++++++++++++++++++++++++++++++ src/array/base.c | 87 ++++++++++++++++++++++++++++++++ src/array/extra.c | 68 +++++++++++++++++++++++++ src/array/get.c | 77 +++++++++++++++++++++++++++++ src/array/include.h | 41 ++++++++++++++++ src/array/sort.c | 95 +++++++++++++++++++++++++++++++++++ 9 files changed, 611 insertions(+) create mode 100644 include/array.h create mode 100644 include/extra/array.h create mode 100644 src/array/_Generics.c create mode 100644 src/array/base-copy.c create mode 100644 src/array/base.c create mode 100644 src/array/extra.c create mode 100644 src/array/get.c create mode 100644 src/array/include.h create mode 100644 src/array/sort.c diff --git a/include/array.h b/include/array.h new file mode 100644 index 0000000..b438ba7 --- /dev/null +++ b/include/array.h @@ -0,0 +1,68 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include "__generics.h" +#include "vtype.h" + +#ifndef LIBCDSB_ARRAY_H +#define LIBCDSB_ARRAY_H + +extern void array_init(vtype_array* x, vtype type) 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__; +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_by_index(s, x, index) array_get(s, x, index, 0) +#define array_pop_by_index(s, x, index) array_get(s, x, index, 1) +#define array_remove_by_index(s, index) array_pop_by_index(s, nullptr, index) + +#define in_array(x, value) (array_indexof(x, value) >= 0) + +extern ssize_t array_get(vtype_array* s, vtype_value* x, ssize_t index, _Bool cut); + + +extern void libcdsb_array_push_pointer(vtype_array* x, const void* value) LIBCDSB_nt__ LIBCDSB_nn1__; +extern void libcdsb_array_push_cstring(vtype_array* x, const char* value) LIBCDSB_nt__ LIBCDSB_nn1__; +extern void libcdsb_array_push_string (vtype_array* x, const vtype_string* value) LIBCDSB_nt__ LIBCDSB_nn12__; +extern void libcdsb_array_push_array (vtype_array* x, const vtype_array* value) LIBCDSB_nt__ LIBCDSB_nn12__; +extern void libcdsb_array_push_list (vtype_array* x, const vtype_list* value) LIBCDSB_nt__ LIBCDSB_nn12__; +extern void libcdsb_array_push_map (vtype_array* x, const vtype_map* value) LIBCDSB_nt__ LIBCDSB_nn12__; +extern void libcdsb_array_push_vset (vtype_array* x, const vtype_set* value) LIBCDSB_nt__ LIBCDSB_nn12__; +extern void libcdsb_array_push_boolean(vtype_array* x, vtype_bool value) LIBCDSB_nt__ LIBCDSB_nn1__; +extern void libcdsb_array_push_uint8 (vtype_array* x, vtype_uint8 value) LIBCDSB_nt__ LIBCDSB_nn1__; +extern void libcdsb_array_push_uint16 (vtype_array* x, vtype_uint16 value) LIBCDSB_nt__ LIBCDSB_nn1__; +extern void libcdsb_array_push_uint32 (vtype_array* x, vtype_uint32 value) LIBCDSB_nt__ LIBCDSB_nn1__; +extern void libcdsb_array_push_uint64 (vtype_array* x, vtype_uint64 value) LIBCDSB_nt__ LIBCDSB_nn1__; +extern void libcdsb_array_push_int8 (vtype_array* x, vtype_int8 value) LIBCDSB_nt__ LIBCDSB_nn1__; +extern void libcdsb_array_push_int16 (vtype_array* x, vtype_int16 value) LIBCDSB_nt__ LIBCDSB_nn1__; +extern void libcdsb_array_push_int32 (vtype_array* x, vtype_int32 value) LIBCDSB_nt__ LIBCDSB_nn1__; +extern void libcdsb_array_push_int64 (vtype_array* x, vtype_int64 value) LIBCDSB_nt__ LIBCDSB_nn1__; +extern void libcdsb_array_push_float (vtype_array* x, vtype_float value) LIBCDSB_nt__ LIBCDSB_nn1__; +extern void libcdsb_array_push_double (vtype_array* x, vtype_double value) LIBCDSB_nt__ LIBCDSB_nn1__; +extern void libcdsb_array_push_ldouble(vtype_array* x, vtype_ldouble value) LIBCDSB_nt__ LIBCDSB_nn1__; + +extern ssize_t libcdsb_array_indexof_pointer(const vtype_array* x, const void* value) LIBCDSB_pure__ LIBCDSB_nn1__; +extern ssize_t libcdsb_array_indexof_string (const vtype_array* x, const char* value) LIBCDSB_pure__ LIBCDSB_nn12__; +extern ssize_t libcdsb_array_indexof_array (const vtype_array* x, const vtype_string* value) LIBCDSB_pure__ LIBCDSB_nn12__; +extern ssize_t libcdsb_array_indexof_list (const vtype_array* x, const vtype_array* value) LIBCDSB_pure__ LIBCDSB_nn12__; +extern ssize_t libcdsb_array_indexof_map (const vtype_array* x, const vtype_list* value) LIBCDSB_pure__ LIBCDSB_nn12__; +extern ssize_t libcdsb_array_indexof_vset (const vtype_array* x, const vtype_map* value) LIBCDSB_pure__ LIBCDSB_nn12__; +extern ssize_t libcdsb_array_indexof_cstring(const vtype_array* x, const vtype_set* value) LIBCDSB_pure__ LIBCDSB_nn1__; +extern ssize_t libcdsb_array_indexof_boolean(const vtype_array* x, vtype_bool value) LIBCDSB_pure__ LIBCDSB_nn1__; +extern ssize_t libcdsb_array_indexof_uint8 (const vtype_array* x, vtype_uint8 value) LIBCDSB_pure__ LIBCDSB_nn1__; +extern ssize_t libcdsb_array_indexof_uint16 (const vtype_array* x, vtype_uint16 value) LIBCDSB_pure__ LIBCDSB_nn1__; +extern ssize_t libcdsb_array_indexof_uint32 (const vtype_array* x, vtype_uint32 value) LIBCDSB_pure__ LIBCDSB_nn1__; +extern ssize_t libcdsb_array_indexof_uint64 (const vtype_array* x, vtype_uint64 value) LIBCDSB_pure__ LIBCDSB_nn1__; +extern ssize_t libcdsb_array_indexof_int8 (const vtype_array* x, vtype_int8 value) LIBCDSB_pure__ LIBCDSB_nn1__; +extern ssize_t libcdsb_array_indexof_int16 (const vtype_array* x, vtype_int16 value) LIBCDSB_pure__ LIBCDSB_nn1__; +extern ssize_t libcdsb_array_indexof_int32 (const vtype_array* x, vtype_int32 value) LIBCDSB_pure__ LIBCDSB_nn1__; +extern ssize_t libcdsb_array_indexof_int64 (const vtype_array* x, vtype_int64 value) LIBCDSB_pure__ LIBCDSB_nn1__; +extern ssize_t libcdsb_array_indexof_float (const vtype_array* x, vtype_float value) LIBCDSB_pure__ LIBCDSB_nn1__; +extern ssize_t libcdsb_array_indexof_double (const vtype_array* x, vtype_double value) LIBCDSB_pure__ LIBCDSB_nn1__; +extern ssize_t libcdsb_array_indexof_ldouble(const vtype_array* x, vtype_ldouble value) LIBCDSB_pure__ LIBCDSB_nn1__; + +#endif /* LIBCDSB_ARRAY_H */ diff --git a/include/extra/array.h b/include/extra/array.h new file mode 100644 index 0000000..e72a9b6 --- /dev/null +++ b/include/extra/array.h @@ -0,0 +1,19 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include "../array.h" + +#ifndef LIBCDSB_EXTRA_ARRAY_H +#define LIBCDSB_EXTRA_ARRAY_H + +#ifdef array_find +# undef array_find +#endif +#ifdef array_push +# undef array_push +#endif + +extern ssize_t array_find(const vtype_array* x, const void* value, vtype value_type); +extern ssize_t array_push( vtype_array* x, const void* value, vtype value_type); + +#endif /* LIBCDSB_EXTRA_ARRAY_H */ diff --git a/src/array/_Generics.c b/src/array/_Generics.c new file mode 100644 index 0000000..a78ead1 --- /dev/null +++ b/src/array/_Generics.c @@ -0,0 +1,44 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include "include.h" + +ssize_t libcdsb_array_indexof_pointer(const arr_t* x, const void* v) { return array_find(x, &v, vtypeof(&v)); } +ssize_t libcdsb_array_indexof_string (const arr_t* x, const char* v) { return array_find(x, &v, vtypeof(&v)); } +ssize_t libcdsb_array_indexof_array (const arr_t* x, const str_t* v) { return array_find(x, v, vtypeof( v)); } +ssize_t libcdsb_array_indexof_list (const arr_t* x, const arr_t* v) { return array_find(x, v, vtypeof( v)); } +ssize_t libcdsb_array_indexof_map (const arr_t* x, const list_t* v) { return array_find(x, v, vtypeof( v)); } +ssize_t libcdsb_array_indexof_vset (const arr_t* x, const map_t* v) { return array_find(x, v, vtypeof( v)); } +ssize_t libcdsb_array_indexof_cstring(const arr_t* x, const set_t* v) { return array_find(x, v, vtypeof( v)); } +ssize_t libcdsb_array_indexof_boolean(const arr_t* x, _Bool v) { return array_find(x, &v, vtypeof(&v)); } +ssize_t libcdsb_array_indexof_int8 (const arr_t* x, s8_t v) { return array_find(x, &v, vtypeof(&v)); } +ssize_t libcdsb_array_indexof_int16 (const arr_t* x, s16_t v) { return array_find(x, &v, vtypeof(&v)); } +ssize_t libcdsb_array_indexof_int32 (const arr_t* x, s32_t v) { return array_find(x, &v, vtypeof(&v)); } +ssize_t libcdsb_array_indexof_int64 (const arr_t* x, s64_t v) { return array_find(x, &v, vtypeof(&v)); } +ssize_t libcdsb_array_indexof_uint8 (const arr_t* x, u8_t v) { return array_find(x, &v, vtypeof(&v)); } +ssize_t libcdsb_array_indexof_uint16 (const arr_t* x, u16_t v) { return array_find(x, &v, vtypeof(&v)); } +ssize_t libcdsb_array_indexof_uint32 (const arr_t* x, u32_t v) { return array_find(x, &v, vtypeof(&v)); } +ssize_t libcdsb_array_indexof_uint64 (const arr_t* x, u64_t v) { return array_find(x, &v, vtypeof(&v)); } +ssize_t libcdsb_array_indexof_float (const arr_t* x, fl_t v) { return array_find(x, &v, vtypeof(&v)); } +ssize_t libcdsb_array_indexof_double (const arr_t* x, dbl_t v) { return array_find(x, &v, vtypeof(&v)); } +ssize_t libcdsb_array_indexof_ldouble(const arr_t* x, ldbl_t v) { return array_find(x, &v, vtypeof(&v)); } + +void libcdsb_array_push_pointer(arr_t* x, const void* v) { array_push(x, &v, vtypeof(&v)); } +void libcdsb_array_push_cstring(arr_t* x, const char* v) { array_push(x, &v, vtypeof(&v)); } +void libcdsb_array_push_string (arr_t* x, const str_t* v) { array_push(x, v, vtypeof( v)); } +void libcdsb_array_push_array (arr_t* x, const arr_t* v) { array_push(x, v, vtypeof( v)); } +void libcdsb_array_push_list (arr_t* x, const list_t* v) { array_push(x, v, vtypeof( v)); } +void libcdsb_array_push_map (arr_t* x, const map_t* v) { array_push(x, v, vtypeof( v)); } +void libcdsb_array_push_vset (arr_t* x, const set_t* v) { array_push(x, v, vtypeof( v)); } +void libcdsb_array_push_boolean(arr_t* x, _Bool v) { array_push(x, &v, vtypeof(&v)); } +void libcdsb_array_push_int8 (arr_t* x, s8_t v) { array_push(x, &v, vtypeof(&v)); } +void libcdsb_array_push_int16 (arr_t* x, s16_t v) { array_push(x, &v, vtypeof(&v)); } +void libcdsb_array_push_int32 (arr_t* x, s32_t v) { array_push(x, &v, vtypeof(&v)); } +void libcdsb_array_push_int64 (arr_t* x, s64_t v) { array_push(x, &v, vtypeof(&v)); } +void libcdsb_array_push_uint8 (arr_t* x, u8_t v) { array_push(x, &v, vtypeof(&v)); } +void libcdsb_array_push_uint16 (arr_t* x, u16_t v) { array_push(x, &v, vtypeof(&v)); } +void libcdsb_array_push_uint32 (arr_t* x, u32_t v) { array_push(x, &v, vtypeof(&v)); } +void libcdsb_array_push_uint64 (arr_t* x, u64_t v) { array_push(x, &v, vtypeof(&v)); } +void libcdsb_array_push_float (arr_t* x, fl_t v) { array_push(x, &v, vtypeof(&v)); } +void libcdsb_array_push_double (arr_t* x, dbl_t v) { array_push(x, &v, vtypeof(&v)); } +void libcdsb_array_push_ldouble(arr_t* x, ldbl_t v) { array_push(x, &v, vtypeof(&v)); } diff --git a/src/array/base-copy.c b/src/array/base-copy.c new file mode 100644 index 0000000..a017b41 --- /dev/null +++ b/src/array/base-copy.c @@ -0,0 +1,112 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include "include.h" +#include "../__internal/assert.h" + +arr_t array_copy(const arr_t* s) { + arr_t x = { .mem = 0, .size = 0, .type = 0 }; + + if (s->size > 0) { + assert(!is_null(s->mem)); + + x.type = s->type; + x.size = s->size; + + if (s->type >= VTYPE_STRING) { + void *p, *v, *e; + void (*init)(void*, const void*); + + x.mem = p = malloc(array_allocated_nmemb(s)); + v = s->mem; + e = array_end(&x); + + switch (s->type) { default: abort(); + case VTYPE_STRING: init = (void*)string_copy_init; break; + case VTYPE_ARRAY: init = (void*) array_copy_init; break; + case VTYPE_LIST: init = (void*) list_copy_init; break; + case VTYPE_MAP: init = (void*) map_copy_init; break; + case VTYPE_SET: init = (void*) vset_copy_init; break; + } + + do { + init(p, v); + p += vtype_size(x.type); + v += vtype_size(x.type); + } while (p < e); + + } else x.mem = memndup(s->mem, array_allocated_nmemb(s)); + } + + return x; +} + +arr_t* array_duplicate(const arr_t* s) { + arr_t* x = malloc(sizeof(*x)); + + if (s->size > 0) { + assert(!is_null(s->mem)); + + x->type = s->type; + x->size = s->size; + + if (s->type >= VTYPE_STRING) { + void *p, *v, *e; + void (*init)(void*, const void*); + + x->mem = p = malloc(array_allocated_nmemb(s)); + v = s->mem; + e = array_end(x); + + switch (s->type) { default: abort(); + case VTYPE_STRING: init = (void*)string_copy_init; break; + case VTYPE_ARRAY: init = (void*) array_copy_init; break; + case VTYPE_LIST: init = (void*) list_copy_init; break; + case VTYPE_MAP: init = (void*) map_copy_init; break; + case VTYPE_SET: init = (void*) vset_copy_init; break; + } + + do { + init(p, v); + p += vtype_size(x->type); + v += vtype_size(x->type); + } while (p < e); + + } else x->mem = memndup(s->mem, array_allocated_nmemb(s)); + } else memset(x, 0, sizeof(*x)); + + return x; +} + +void array_copy_init(arr_t* x, const arr_t* s) { + if (s->size > 0) { + assert(!is_null(s->mem)); + + x->type = s->type; + x->size = s->size; + + if (s->type >= VTYPE_STRING) { + void *p, *v, *e; + void (*init)(void*, const void*); + + x->mem = p = malloc(array_allocated_nmemb(s)); + v = s->mem; + e = array_end(x); + + switch (s->type) { default: abort(); + case VTYPE_STRING: init = (void*)string_copy_init; break; + case VTYPE_ARRAY: init = (void*) array_copy_init; break; + case VTYPE_LIST: init = (void*) list_copy_init; break; + case VTYPE_MAP: init = (void*) map_copy_init; break; + case VTYPE_SET: init = (void*) vset_copy_init; break; + } + + do { + init(p, v); + p += vtype_size(x->type); + v += vtype_size(x->type); + } while (p < e); + + } else x->mem = memndup(s->mem, array_allocated_nmemb(s)); + } else memset(x, 0, sizeof(*x)); +} diff --git a/src/array/base.c b/src/array/base.c new file mode 100644 index 0000000..47e70e2 --- /dev/null +++ b/src/array/base.c @@ -0,0 +1,87 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include "include.h" +#include "../__internal/assert.h" + +size_t array_size (const arr_t* x) { + return x->size; +} + +size_t array_nmemb(const arr_t* x) { + return x->size*vtype_size(x->type); +} + +void array_init(arr_t* x, vtype t) { + x->type = t; + x->size = 0; + x->mem = nullptr; +} + +void array_free(arr_t* x) { + if (x->size && x->type >= VTYPE_STRING) { + void* p = x->mem; + void (*free_)(void*); + + assert(!is_null(p)); + + switch (x->type) { + default: + #ifndef NDEBUG + abort(); + #endif + case VTYPE_STRING: free_ = (void*)string_free; break; + case VTYPE_ARRAY: free_ = (void*) array_free; break; + case VTYPE_LIST: free_ = (void*) list_free; break; + case VTYPE_MAP: free_ = (void*) map_free; break; + case VTYPE_SET: free_ = (void*) vset_free; break; + } + + do { + free_(p); + p += vtype_size(x->type); + } while (--x->size); + } + + free(x->mem); + memset(x, 0, sizeof(*x)); +} + +int array_compare(const arr_t* s0, const arr_t* s1) { + + void *e; + void *p0; + void *p1; + int c; + + if (s0 == s1) + return 0; + + if (s0->type != s1->type) + return (s0->type < s1->type) ? -1 : 1; + + if (s0->size != s1->size) + return (s0->size < s1->size) ? -1 : 1; + + if (!s0->size && !s0->size) + return 0; + + assert(!is_null(s0->mem) && !is_null(s1->mem)); + + p0 = s0->mem; + p1 = s1->mem; + + e = array_end(s0); + + do { + c = vtype_compare_eq(p0, p1, s0->type); + + if (c == 0) { + p0 += vtype_size(s0->type); + p1 += vtype_size(s0->type); + } else return c; + + } while (p0 < e); + + return 0; +} diff --git a/src/array/extra.c b/src/array/extra.c new file mode 100644 index 0000000..c94e0d0 --- /dev/null +++ b/src/array/extra.c @@ -0,0 +1,68 @@ +/* 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 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) { + void* p = x->mem; + void* e = array_end(x); + + assert(!is_null(x->mem)); + + if (x->type != vt) { + do { + ++index; + c = vtype_compare(p, x->type, v, vt); + + if (c == 0) + return index; + + p += vtype_size(x->type); + } while (p < e); + } else { + do { + ++index; + c = vtype_compare_eq(p, v, vt); + + if (c == 0) + return index; + + p += vtype_size(x->type); + } while (p < e); + } + } + + return -1; +} + +ssize_t array_push(arr_t* x, const void* v, vtype vt) { + + void* p; + ssize_t i = x->size; + vnode_t n = vnode_tcreate(x->type, v, vt); + + if ((ssize_t)array_allocated_nmemb(x) - vtype_size(x->type) < 0) { + ++x->size; + x->mem = realloc(x->mem, array_allocated_nmemb(x)); + } else ++x->size; + + memcpy(array_at(x, i), vnode_peek(&n, x->type), vtype_size(x->type)); + + if (x->type < VTYPE_STRING) vnode_free(&n, x->type); + + return i; +} diff --git a/src/array/get.c b/src/array/get.c new file mode 100644 index 0000000..3c8cdae --- /dev/null +++ b/src/array/get.c @@ -0,0 +1,77 @@ +/* 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 array_get(arr_t* x, val_t* d, ssize_t i, _Bool cut) { + + if (i < 0 && (i += x->size) < 0) i = 0; + + if (i < x->size) { + assert(!is_null(x->mem)); + + if (cut) { + if (!is_null(d)) { + vnode_t n = vnode_create(array_at(x, i), x->type); + value_set(d, n, x->type, VF_WRITEABLE|VF_REMOVABLE); + } + + array_cut(x, i, 1); + } else value_set(d, array_at(x, i), x->type, VF_WRITEABLE); + } else { + i = -1; + memset(d, 0, sizeof(*d)); + } + + 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)); + + if (i < 0 && (i += s->size) < 0) i = 0; + + if (i + n > s->size) { + memset(x, 0, sizeof(*x)); + return false; + } + + x->type = s->type; + x->size = n; + x->mem = malloc(array_allocated_nmemb(x)); + + if (!cut && s->type >= VTYPE_STRING) { + void *p, *v, *e; + void (*init)(void*, const void*); + + p = x->mem; + v = array_at(s, i); + e = array_at(x, n); + + switch (s->type) { default: abort(); + case VTYPE_STRING: init = (void*)string_copy_init; break; + case VTYPE_ARRAY: init = (void*) array_copy_init; break; + case VTYPE_LIST: init = (void*) list_copy_init; break; + case VTYPE_MAP: init = (void*) map_copy_init; break; + case VTYPE_SET: init = (void*) vset_copy_init; break; + } + + do { + init(p, v); + p += vtype_size(x->type); + v += vtype_size(x->type); + } while (p < e); + } else memcpy(x->mem, array_at(s, i), n*vtype_size(x->type)); + + if (cut) array_cut(s, i, n); + } else { + memset(x, 0, sizeof(*x)); + + if (n && !s->size) return false; + } + + return true; +} diff --git a/src/array/include.h b/src/array/include.h new file mode 100644 index 0000000..06badcf --- /dev/null +++ b/src/array/include.h @@ -0,0 +1,41 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include "../../include/array.h" +#include "../../include/extra/array.h" +#include "../__internal/include.h" + +#ifndef LIBCDSB_SRC_ARRAY_INCLUDE_H +#define LIBCDSB_SRC_ARRAY_INCLUDE_H + +#define ARRAY_MEM_BLOCK_SIZE 256 + +ainline(size_t array_nblocks(const arr_t* x)) { + size_t n = x->size*vtype_size(x->type); + + if (n%vtype_size(x->type)) { + return n / ARRAY_MEM_BLOCK_SIZE + 1; + } else return n / ARRAY_MEM_BLOCK_SIZE; +} + +ainline(size_t array_allocated_nmemb(const arr_t* x)) { + return array_nblocks(x) * ARRAY_MEM_BLOCK_SIZE; +} + +ainline(void array_cut(arr_t* x, size_t i, size_t n)) { + void* v = x->mem + i*vtype_size(x->type); + void* e = v + n*vtype_size(x->type); + + memmove(v, e, (x->size-(i+n))*vtype_size(x->type)); + x->size -= n; +} + +ainline(void* array_end(const arr_t* x)) { + return x->mem + x->size*vtype_size(x->type); +} + +ainline(void* array_at(const arr_t* x, size_t i)) { + return x->mem + i*vtype_size(x->type); +} + +#endif /* LIBCDSB_SRC_ARRAY_INCLUDE_H */ diff --git a/src/array/sort.c b/src/array/sort.c new file mode 100644 index 0000000..415f868 --- /dev/null +++ b/src/array/sort.c @@ -0,0 +1,95 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include "include.h" +#include "../__internal/assert.h" + +static_assert(( + VTYPE_UINT8 == 2 && + VTYPE_UINT16 == 3 && + VTYPE_UINT32 == 4 && + VTYPE_UINT64 == 5 && + VTYPE_INT8 == 6 && + VTYPE_INT16 == 7 && + VTYPE_INT32 == 8 && + VTYPE_INT64 == 9 && + VTYPE_FLOAT == 10 && + VTYPE_DOUBLE == 11 && + VTYPE_LDOUBLE == 12 && + VTYPE_STRING == 13 && + VTYPE_MAP == 14 && + VTYPE_ARRAY == 15 && + VTYPE_LIST == 16 && + VTYPE_SET == 17 +), "enum values assertion"); + +static int uint8_compare (const u8_t* s0, const u8_t* s1) { if (*s0 == *s1) return 0; return *s0 < *s1 ? -1 : 1; } +static int uint16_compare (const u16_t* s0, const u16_t* s1) { if (*s0 == *s1) return 0; return *s0 < *s1 ? -1 : 1; } +static int uint32_compare (const u32_t* s0, const u32_t* s1) { if (*s0 == *s1) return 0; return *s0 < *s1 ? -1 : 1; } +static int uint64_compare (const u64_t* s0, const u64_t* s1) { if (*s0 == *s1) return 0; return *s0 < *s1 ? -1 : 1; } +static int int8_compare (const s8_t* s0, const s8_t* s1) { if (*s0 == *s1) return 0; return *s0 < *s1 ? -1 : 1; } +static int int16_compare (const s16_t* s0, const s16_t* s1) { if (*s0 == *s1) return 0; return *s0 < *s1 ? -1 : 1; } +static int int32_compare (const s32_t* s0, const s32_t* s1) { if (*s0 == *s1) return 0; return *s0 < *s1 ? -1 : 1; } +static int int64_compare (const s64_t* s0, const s64_t* s1) { if (*s0 == *s1) return 0; return *s0 < *s1 ? -1 : 1; } +static int float_compare (const fl_t* s0, const fl_t* s1) { if (*s0 == *s1) return 0; return *s0 < *s1 ? -1 : 1; } +static int double_compare (const dbl_t* s0, const dbl_t* s1) { if (*s0 == *s1) return 0; return *s0 < *s1 ? -1 : 1; } +static int ldouble_compare(const ldbl_t* s0, const ldbl_t* s1) { if (*s0 == *s1) return 0; return *s0 < *s1 ? -1 : 1; } + +static int (*COMPARE[16])(const void*, const void*) = { + (void*) uint8_compare, + (void*) uint16_compare, + (void*) uint32_compare, + (void*) uint64_compare, + (void*) int8_compare, + (void*) int16_compare, + (void*) int32_compare, + (void*) int64_compare, + (void*) float_compare, + (void*) double_compare, + (void*) ldouble_compare, + (void*) string_compare, + (void*) map_compare, + (void*) array_compare, + (void*) list_compare, + (void*) vset_compare +}; + + +/*#####################################################################################################################*/ + + +ainline(int get_compare_index(vtype t)) { + if (t == VTYPE_POINTER) { + if (is_x64) t = VTYPE_UINT64; + else t = VTYPE_UINT32; + } else if (t == VTYPE_BOOLEAN) t = VTYPE_UINT8; + + return t - VTYPE_UINT8; +} + + +/*#####################################################################################################################*/ + + +void array_sort(arr_t* x) { + int i = get_compare_index(x->type); + + if (x->size > 1) + qsort(x->mem, x->size, vtype_size(x->type), COMPARE[i]); +} + +void array_reverse(arr_t* x) { + if (x->size >= 2) { + void *l = x->mem; + void *r = array_at(x, x->size-1); + char b[vtype_size(x->type)]; + + do { + memcpy(b, l, vtype_size(x->type)); + memcpy(l, r, vtype_size(x->type)); + memcpy(r, b, vtype_size(x->type)); + l += vtype_size(x->type); + r -= vtype_size(x->type); + } while (l < r); + } +}