diff --git a/include/__attributes.h b/include/__attributes.h index 7c6c1f0..1039aba 100644 --- a/include/__attributes.h +++ b/include/__attributes.h @@ -6,6 +6,7 @@ #define LIBCDSB_nt__ __attribute__ ((nothrow)) #define LIBCDSB_nn1__ __attribute__ ((nonnull (1))) +#define LIBCDSB_nn2__ __attribute__ ((nonnull (2))) #define LIBCDSB_nn12__ __attribute__ ((nonnull (1,2))) #define LIBCDSB_nn123__ __attribute__ ((nonnull (1,2,3))) #define LIBCDSB_nn13__ __attribute__ ((nonnull (1,3))) diff --git a/include/array.h b/include/array.h index 83f993c..05042f0 100644 --- a/include/array.h +++ b/include/array.h @@ -10,6 +10,8 @@ /*#####################################################################################################################*/ extern void array_init(vtype_array* x, vtype type) LIBCDSB_nt__ LIBCDSB_nn1__; + +extern void* array_at(const vtype_array* s, ssize_t index) 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__; @@ -18,15 +20,8 @@ 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, 0, index) - #define in_array(x, value) (array_indexof(x, value) >= 0) -extern void* array_at (const vtype_array* s, ssize_t index); -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__; diff --git a/include/extra/array.h b/include/extra/array.h index 69b2bd6..8cc7c9a 100644 --- a/include/extra/array.h +++ b/include/extra/array.h @@ -6,11 +6,13 @@ #ifndef LIBCDSB_EXTRA_ARRAY_H #define LIBCDSB_EXTRA_ARRAY_H -#ifdef array_push -# undef array_push -#endif +#define array_get(x, s, index) libcdsb_array_get(x, s, index, 0) +#define array_pop(x, s, index) libcdsb_array_get(x, s, index, 1) +#define array_remove(s, index) libcdsb_array_get(0, s, index, 1) -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); +extern ssize_t libcdsb_array_get(vtype_value* x, vtype_array* s, ssize_t index, _Bool cut) LIBCDSB_nt__ LIBCDSB_nn2__; + +extern ssize_t libcdsb_array_find(const vtype_array* x, const void* value, vtype value_type) LIBCDSB_nt__ LIBCDSB_nn1__; +extern ssize_t libcdsb_array_push( vtype_array* x, const void* value, vtype value_type) LIBCDSB_nt__ LIBCDSB_nn1__; #endif /* LIBCDSB_EXTRA_ARRAY_H */ diff --git a/include/extra/list.h b/include/extra/list.h new file mode 100644 index 0000000..6fc62e5 --- /dev/null +++ b/include/extra/list.h @@ -0,0 +1,21 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include +#include "../list.h" + +#ifndef LIBCDSB_EXTRA_LIST_H +#define LIBCDSB_EXTRA_LIST_H + +#define list_get_by_index(x, s, index) libcdsb_list_get(x, s, index, 0) +#define list_pop_by_index(x, s, index) libcdsb_list_get(x, s, index, 1) +#define list_remove_by_index(s, index) libcdsb_list_get(0, s, index, 1) + +extern ssize_t libcdsb_list_find (vtype_value* x, vtype_list* s, const void* value, vtype type, _Bool reverse, _Bool cut); +extern _Bool libcdsb_list_update(vtype_list* x, ssize_t index, const void* value, vtype type, int ins_direction); + +extern size_t libcdsb_list_count(const vtype_list* s, const void* value, vtype type); + +extern ssize_t libcdsb_list_get(vtype_value* x, vtype_list* s, ssize_t index, _Bool cut); + +#endif /* LIBCDSB_EXTRA_LIST_H */ diff --git a/include/list.h b/include/list.h new file mode 100644 index 0000000..7e1aece --- /dev/null +++ b/include/list.h @@ -0,0 +1,93 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include "__generics.h" +#include "vtype.h" + +#ifndef LIBCDSB_LIST_H +#define LIBCDSB_LIST_H + +/*#####################################################################################################################*/ + +extern void list_init(vtype_list* x); +extern void list_extend(vtype_list* x, const vtype_list* s); + +extern void list_sort(vtype_list* x); +extern void list_reverse(vtype_list* x); + +#define list_pop(x, s, value) _LIBCDSB_Generic(libcdsb_list, find, value)(x, s, value, 0, 1) +#define list_find(x, s, value) _LIBCDSB_Generic(libcdsb_list, find, value)(x, s, value, 0, 0) +#define list_rfind(x, s, value) _LIBCDSB_Generic(libcdsb_list, find, value)(x, s, value, 1, 0) +#define list_countof(s, value) _LIBCDSB_Generic(libcdsb_list, count, value)(x, value) +#define list_indexof(s, value) list_find(0, s, value) +#define list_remove(s, value) list_pop(0, s, value) +#define in_list(s, value) (list_indexof(s, value) >= 0) + +#define list_insert(x, index, value) _LIBCDSB_Generic(libcdsb_list, update, value)(x, index, value, -1) +#define list_replace(x, index, value) _LIBCDSB_Generic(libcdsb_list, update, value)(x, index, value, 0) +#define list_push_back(x, value) _LIBCDSB_Generic(libcdsb_list, update, value)(x, -1, value, 1) +#define list_push_front(x, value) _LIBCDSB_Generic(libcdsb_list, update, value)(x, 0, value, -1) + +/*#####################################################################################################################*/ + +extern ssize_t libcdsb_list_find_pointer(vtype_value* x, vtype_list* s, const void* value, _Bool reverse, _Bool cut); +extern ssize_t libcdsb_list_find_cstring(vtype_value* x, vtype_list* s, const char* value, _Bool reverse, _Bool cut); +extern ssize_t libcdsb_list_find_string (vtype_value* x, vtype_list* s, const vtype_string* value, _Bool reverse, _Bool cut); +extern ssize_t libcdsb_list_find_array (vtype_value* x, vtype_list* s, const vtype_array* value, _Bool reverse, _Bool cut); +extern ssize_t libcdsb_list_find_list (vtype_value* x, vtype_list* s, const vtype_list* value, _Bool reverse, _Bool cut); +extern ssize_t libcdsb_list_find_map (vtype_value* x, vtype_list* s, const vtype_map* value, _Bool reverse, _Bool cut); +extern ssize_t libcdsb_list_find_vset (vtype_value* x, vtype_list* s, const vtype_set* value, _Bool reverse, _Bool cut); +extern ssize_t libcdsb_list_find_boolean(vtype_value* x, vtype_list* s, vtype_bool value, _Bool reverse, _Bool cut); +extern ssize_t libcdsb_list_find_int8 (vtype_value* x, vtype_list* s, vtype_int8 value, _Bool reverse, _Bool cut); +extern ssize_t libcdsb_list_find_int16 (vtype_value* x, vtype_list* s, vtype_int16 value, _Bool reverse, _Bool cut); +extern ssize_t libcdsb_list_find_int32 (vtype_value* x, vtype_list* s, vtype_int32 value, _Bool reverse, _Bool cut); +extern ssize_t libcdsb_list_find_int64 (vtype_value* x, vtype_list* s, vtype_int64 value, _Bool reverse, _Bool cut); +extern ssize_t libcdsb_list_find_uint8 (vtype_value* x, vtype_list* s, vtype_uint8 value, _Bool reverse, _Bool cut); +extern ssize_t libcdsb_list_find_uint16 (vtype_value* x, vtype_list* s, vtype_uint16 value, _Bool reverse, _Bool cut); +extern ssize_t libcdsb_list_find_uint32 (vtype_value* x, vtype_list* s, vtype_uint32 value, _Bool reverse, _Bool cut); +extern ssize_t libcdsb_list_find_uint64 (vtype_value* x, vtype_list* s, vtype_uint64 value, _Bool reverse, _Bool cut); +extern ssize_t libcdsb_list_find_float (vtype_value* x, vtype_list* s, vtype_float value, _Bool reverse, _Bool cut); +extern ssize_t libcdsb_list_find_double (vtype_value* x, vtype_list* s, vtype_double value, _Bool reverse, _Bool cut); +extern ssize_t libcdsb_list_find_ldouble(vtype_value* x, vtype_list* s, vtype_ldouble value, _Bool reverse, _Bool cut); + +extern size_t libcdsb_list_count_pointer(const vtype_list* s, const void* value); +extern size_t libcdsb_list_count_cstring(const vtype_list* s, const char* value); +extern size_t libcdsb_list_count_string (const vtype_list* s, const vtype_string* value); +extern size_t libcdsb_list_count_array (const vtype_list* s, const vtype_array* value); +extern size_t libcdsb_list_count_list (const vtype_list* s, const vtype_list* value); +extern size_t libcdsb_list_count_map (const vtype_list* s, const vtype_map* value); +extern size_t libcdsb_list_count_vset (const vtype_list* s, const vtype_set* value); +extern size_t libcdsb_list_count_boolean(const vtype_list* s, vtype_bool value); +extern size_t libcdsb_list_count_int8 (const vtype_list* s, vtype_int8 value); +extern size_t libcdsb_list_count_int16 (const vtype_list* s, vtype_int16 value); +extern size_t libcdsb_list_count_int32 (const vtype_list* s, vtype_int32 value); +extern size_t libcdsb_list_count_int64 (const vtype_list* s, vtype_int64 value); +extern size_t libcdsb_list_count_uint8 (const vtype_list* s, vtype_uint8 value); +extern size_t libcdsb_list_count_uint16 (const vtype_list* s, vtype_uint16 value); +extern size_t libcdsb_list_count_uint32 (const vtype_list* s, vtype_uint32 value); +extern size_t libcdsb_list_count_uint64 (const vtype_list* s, vtype_uint64 value); +extern size_t libcdsb_list_count_float (const vtype_list* s, vtype_float value); +extern size_t libcdsb_list_count_double (const vtype_list* s, vtype_double value); +extern size_t libcdsb_list_count_ldouble(const vtype_list* s, vtype_ldouble value); + +extern _Bool libcdsb_list_update_pointer(vtype_list* x, ssize_t index, const void* value, int ins_direction); +extern _Bool libcdsb_list_update_cstring(vtype_list* x, ssize_t index, const char* value, int ins_direction); +extern _Bool libcdsb_list_update_string (vtype_list* x, ssize_t index, const vtype_string* value, int ins_direction); +extern _Bool libcdsb_list_update_array (vtype_list* x, ssize_t index, const vtype_array* value, int ins_direction); +extern _Bool libcdsb_list_update_list (vtype_list* x, ssize_t index, const vtype_list* value, int ins_direction); +extern _Bool libcdsb_list_update_map (vtype_list* x, ssize_t index, const vtype_map* value, int ins_direction); +extern _Bool libcdsb_list_update_vset (vtype_list* x, ssize_t index, const vtype_set* value, int ins_direction); +extern _Bool libcdsb_list_update_boolean(vtype_list* x, ssize_t index, vtype_bool value, int ins_direction); +extern _Bool libcdsb_list_update_int8 (vtype_list* x, ssize_t index, vtype_int8 value, int ins_direction); +extern _Bool libcdsb_list_update_int16 (vtype_list* x, ssize_t index, vtype_int16 value, int ins_direction); +extern _Bool libcdsb_list_update_int32 (vtype_list* x, ssize_t index, vtype_int32 value, int ins_direction); +extern _Bool libcdsb_list_update_int64 (vtype_list* x, ssize_t index, vtype_int64 value, int ins_direction); +extern _Bool libcdsb_list_update_uint8 (vtype_list* x, ssize_t index, vtype_uint8 value, int ins_direction); +extern _Bool libcdsb_list_update_uint16 (vtype_list* x, ssize_t index, vtype_uint16 value, int ins_direction); +extern _Bool libcdsb_list_update_uint32 (vtype_list* x, ssize_t index, vtype_uint32 value, int ins_direction); +extern _Bool libcdsb_list_update_uint64 (vtype_list* x, ssize_t index, vtype_uint64 value, int ins_direction); +extern _Bool libcdsb_list_update_float (vtype_list* x, ssize_t index, vtype_float value, int ins_direction); +extern _Bool libcdsb_list_update_double (vtype_list* x, ssize_t index, vtype_double value, int ins_direction); +extern _Bool libcdsb_list_update_ldouble(vtype_list* x, ssize_t index, vtype_ldouble value, int ins_direction); + +#endif /* LIBCDSB_LIST_H */ diff --git a/include/vtype.h b/include/vtype.h index 7815c82..dca09a8 100644 --- a/include/vtype.h +++ b/include/vtype.h @@ -99,26 +99,26 @@ extern int map_compare(const vtype_map* s0, const vtype_map* s1) LIBCDS extern int vset_compare(const vtype_set* s0, const vtype_set* s1) LIBCDSB_cmpattr__; /* Copy string to another */ -extern vtype_string string_copy(const vtype_string* x) LIBCDSB_cpyattr__; +extern vtype_string string_copy(const vtype_string* s) LIBCDSB_cpyattr__; /* Copy array to another */ -extern vtype_array array_copy(const vtype_array* x) LIBCDSB_cpyattr__; +extern vtype_array array_copy(const vtype_array* s) LIBCDSB_cpyattr__; /* Copy list to another */ -extern vtype_list list_copy(const vtype_list* x) LIBCDSB_cpyattr__; +extern vtype_list list_copy(const vtype_list* s) LIBCDSB_cpyattr__; /* Copy map to another */ -extern vtype_map map_copy(const vtype_map* x) LIBCDSB_cpyattr__; +extern vtype_map map_copy(const vtype_map* s) LIBCDSB_cpyattr__; /* Copy set to another */ -extern vtype_set vset_copy(const vtype_set* x) LIBCDSB_cpyattr__; +extern vtype_set vset_copy(const vtype_set* s) LIBCDSB_cpyattr__; /* Duplicate string memory block */ -extern vtype_string* string_duplicate(const vtype_string* x) LIBCDSB_dupattr__; +extern vtype_string* string_duplicate(const vtype_string* s) LIBCDSB_dupattr__; /* Duplicate array memory block */ -extern vtype_array* array_duplicate(const vtype_array* x) LIBCDSB_dupattr__; +extern vtype_array* array_duplicate(const vtype_array* s) LIBCDSB_dupattr__; /* Duplicate list memory block */ -extern vtype_list* list_duplicate(const vtype_list* x) LIBCDSB_dupattr__; +extern vtype_list* list_duplicate(const vtype_list* s) LIBCDSB_dupattr__; /* Duplicate map memory block */ -extern vtype_map* map_duplicate(const vtype_map* x) LIBCDSB_dupattr__; +extern vtype_map* map_duplicate(const vtype_map* s) LIBCDSB_dupattr__; /* Duplicate set memory block */ -extern vtype_set* vset_duplicate(const vtype_set* x) LIBCDSB_dupattr__; +extern vtype_set* vset_duplicate(const vtype_set* s) LIBCDSB_dupattr__; /* Copy string and store result to the memory block */ extern void string_copy_init(vtype_string* x, const vtype_string* s) LIBCDSB_nn12__; diff --git a/src/__internal/include.h b/src/__internal/include.h index 1844919..25d00e9 100644 --- a/src/__internal/include.h +++ b/src/__internal/include.h @@ -58,6 +58,7 @@ typedef vtype_kvpair kvp_t; typedef vtype_iterator iter_t; typedef enum { + VF_UNDEFINED = 0x00, VF_WRITEABLE = 0x01, VF_CHANGEABLE = 0x02, VF_REMOVABLE = 0x0f diff --git a/src/array/base.c b/src/array/base.c index 47e70e2..1133689 100644 --- a/src/array/base.c +++ b/src/array/base.c @@ -12,6 +12,11 @@ size_t array_nmemb(const arr_t* x) { return x->size*vtype_size(x->type); } +void* array_at(const arr_t* x, ssize_t i) { + if (i < 0 && (i += x->size) < 0) i = 0; + return x->mem + i*vtype_size(x->type); +} + void array_init(arr_t* x, vtype t) { x->type = t; x->size = 0; diff --git a/src/array/extra.c b/src/array/extra.c index bf9027b..3baefbb 100644 --- a/src/array/extra.c +++ b/src/array/extra.c @@ -5,7 +5,7 @@ #include "../__internal/assert.h" #include "../__internal/vnode.h" -ssize_t array_find(const arr_t* x, const void* v, vtype vt) { +ssize_t libcdsb_array_find(const arr_t* x, const void* v, vtype vt) { int c; ssize_t index; @@ -38,7 +38,7 @@ ssize_t array_find(const arr_t* x, const void* v, vtype vt) { return -1; } -ssize_t array_push(arr_t* x, const void* v, vtype vt) { +ssize_t libcdsb_array_push(arr_t* x, const void* v, vtype vt) { ssize_t i = x->size; vnode_t n = vnode_tcreate(x->type, v, vt); @@ -50,3 +50,26 @@ ssize_t array_push(arr_t* x, const void* v, vtype vt) { return i; } + +ssize_t libcdsb_array_get(val_t* x, arr_t* s, ssize_t i, _Bool cut) { + + if (i < 0 && (i += s->size) < 0) i = 0; + + if (i < s->size) { + assert(!is_null(s->mem)); + + if (cut) { + if (!is_null(x)) { + vnode_t n = vnode_create(array_internal_at(s, i), s->type); + value_set(x, n, s->type, VF_WRITEABLE|VF_REMOVABLE); + } + + array_cut(s, i, 1); + } else value_set(x, array_internal_at(s, i), s->type, VF_WRITEABLE); + } else { + i = -1; + memset(x, 0, sizeof(*x)); + } + + return i; +} diff --git a/src/array/generics.c b/src/array/generics.c index a78ead1..be0352a 100644 --- a/src/array/generics.c +++ b/src/array/generics.c @@ -3,42 +3,42 @@ #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)); } +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)); } -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)); } +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)); } +void libcdsb_array_push_string (arr_t* x, const str_t* v) { libcdsb_array_push(x, v, vtypeof( v)); } +void libcdsb_array_push_array (arr_t* x, const arr_t* v) { libcdsb_array_push(x, v, vtypeof( v)); } +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_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)); } +void libcdsb_array_push_int64 (arr_t* x, s64_t v) { libcdsb_array_push(x, &v, vtypeof(&v)); } +void libcdsb_array_push_uint8 (arr_t* x, u8_t v) { libcdsb_array_push(x, &v, vtypeof(&v)); } +void libcdsb_array_push_uint16 (arr_t* x, u16_t v) { libcdsb_array_push(x, &v, vtypeof(&v)); } +void libcdsb_array_push_uint32 (arr_t* x, u32_t v) { libcdsb_array_push(x, &v, vtypeof(&v)); } +void libcdsb_array_push_uint64 (arr_t* x, u64_t v) { libcdsb_array_push(x, &v, vtypeof(&v)); } +void libcdsb_array_push_float (arr_t* x, fl_t v) { libcdsb_array_push(x, &v, vtypeof(&v)); } +void libcdsb_array_push_double (arr_t* x, dbl_t v) { libcdsb_array_push(x, &v, vtypeof(&v)); } +void libcdsb_array_push_ldouble(arr_t* x, ldbl_t v) { libcdsb_array_push(x, &v, vtypeof(&v)); } diff --git a/src/array/get.c b/src/array/slice.c similarity index 70% rename from src/array/get.c rename to src/array/slice.c index 23a2323..00c9549 100644 --- a/src/array/get.c +++ b/src/array/slice.c @@ -5,34 +5,6 @@ #include "../__internal/assert.h" #include "../__internal/vnode.h" -void* array_at(const arr_t* x, ssize_t i) { - if (i < 0 && (i += x->size) < 0) i = 0; - return x->mem + i*vtype_size(x->type); -} - -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_internal_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_internal_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)); diff --git a/src/list/base.c b/src/list/base.c new file mode 100644 index 0000000..90adb5e --- /dev/null +++ b/src/list/base.c @@ -0,0 +1,77 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include "include.h" + +/*#####################################################################################################################*/ + +void list_init(list_t* x) { + memset(x, 0, sizeof(*x)); +} + +void list_free(list_t* x) { + lnode_t* c; + lnode_t* next; + + c = x->first; + + while (!is_null(c)) { + next = c->next; + vnode_free(c->node, c->type); + free(c); + c = next; + } + + memset(x, 0, sizeof(*x)); +} + +/*#####################################################################################################################*/ + +size_t list_size(const list_t* x) { + lnode_t* c; + size_t n; + + c = x->first; + n = 0; + + while (!is_null(c)) { + c = c->next; + ++n; + } + + return n; +} + +/*#####################################################################################################################*/ + +int list_compare(const list_t* s0, const list_t* s1) { + lnode_t *c0, *c1; + int c; + + if (s0 == s1) return 0; + + c0 = s0->first; + c1 = s1->first; + + for (;;) { + if (is_null(c0) || is_null(c1)) { + return (c0 == c1) ? 0 : (ssize_t)c0 - (ssize_t)c1; + } + + c = lnode_compare(c0, c1); + + if (c != 0) break; + + c0 = c0->next; + c1 = c1->next; + } + + for (;;) { + c0 = c0->next; + c1 = c1->next; + + if (is_null(c0) || is_null(c1)) { + return (c0 == c1) ? 0 : (ssize_t)c0 - (ssize_t)c1; + } + } +} diff --git a/src/list/copy.c b/src/list/copy.c new file mode 100644 index 0000000..a68334f --- /dev/null +++ b/src/list/copy.c @@ -0,0 +1,109 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include "include.h" + +/*#####################################################################################################################*/ + +static void init_first(list_t* x, vnode_t v, vtype t) { + lnode_t* node = malloc(sizeof(*node)); + + node->next = nullptr; + node->prev = nullptr; + node->node = v; + node->type = t; + + x->first = node; + x->last = node; +} + +static void push_next(list_t* x, vnode_t v, vtype t) { + lnode_t* node = malloc(sizeof(*node)); + + node->next = nullptr; + node->prev = x->last; + node->node = v; + node->type = t; + + x->last = node; + x->last->next = node; +} + +/*#####################################################################################################################*/ + +list_t list_copy(const list_t* s) { + list_t x; + lnode_t* c; + + c = s->first; + memset(&x, 0, sizeof(x)); + + if (is_null(c)) + return x; + + init_first(&x, vnode_duplicate(&c->node, c->type), c->type); + + while (!is_null(c = c->next)) { + push_next(&x, vnode_duplicate(&c->node, c->type), c->type); + } + + return x; +} + +list_t* list_duplicate(const list_t* s) { + list_t* x; + lnode_t* c; + + c = s->first; + x = calloc(sizeof(*x), 1); + + if (is_null(c)) + return x; + + init_first(x, vnode_duplicate(&c->node, c->type), c->type); + + while (!is_null(c = c->next)) { + push_next(x, vnode_duplicate(&c->node, c->type), c->type); + } + + return x; +} + +void list_copy_init(list_t* x, const list_t* s) { + lnode_t* c; + + c = s->first; + memset(x, 0, sizeof(*x)); + + if (is_null(c)) + return; + + init_first(x, vnode_duplicate(&c->node, c->type), c->type); + + while (!is_null(c = c->next)) { + push_next(x, vnode_duplicate(&c->node, c->type), c->type); + } +} + +/*#####################################################################################################################*/ + +void list_extend(list_t* x, const list_t* s) { + lnode_t* c; + + c = s->first; + + if (is_null(c)) + return; + + if (is_null(x->first)) { + init_first(x, vnode_duplicate(&c->node, c->type), c->type); + c = c->next; + + if (is_null(c)) + return; + } + + do { + push_next(x, vnode_duplicate(&c->node, c->type), c->type); + } while (!is_null(c = c->next)); +} diff --git a/src/list/extra.c b/src/list/extra.c new file mode 100644 index 0000000..6514860 --- /dev/null +++ b/src/list/extra.c @@ -0,0 +1,161 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include "include.h" + +/*#####################################################################################################################*/ + +static void lnode_cut(val_t* x, list_t* s, lnode_t* cur) { + if (!is_null(x)) { + value_set(x, cur->node, cur->type, VF_WRITEABLE|VF_REMOVABLE); + } else vnode_free(&cur->node, cur->type); + + if (!is_null(cur->prev)) { + cur->prev->next = cur->next; + } else s->first = cur->next; + + if (!is_null(cur->next)) { + cur->next->prev = cur->prev; + } else s->last = cur->prev; + + free(cur); +} + +/*#####################################################################################################################*/ + +ssize_t libcdsb_list_get(val_t* x, list_t* s, ssize_t i, _Bool cut) { + + ldir_t dir; + lnode_t* c; + size_t n; + int cmp; + + if (i < 0) { + n = (i = ~i); + dir = LD_PREV; + } else { + n = i; + dir = LD_NEXT; + } + + c = ldir_dir((lnode_t*)s, dir); + + memset(x, 0, sizeof(*x)); + + while (n && !is_null(c)) { + c = ldir_dir(c, dir); + --n; + } + + if (n || is_null(c)) return -1; + + if (!cut && !is_null(x)) { + value_set(x, &c->node, c->type, VF_WRITEABLE|VF_CHANGEABLE); + } else if (cut) lnode_cut(x, s, c); + + return i; +} + + +/*#####################################################################################################################*/ + +ssize_t libcdsb_list_find(val_t* x, list_t* s, const void* v, vtype t, _Bool r, _Bool cut) { + ldir_t dir = r ? LD_PREV : LD_NEXT; + + lnode_t* c; + ssize_t i; + int cmp; + + c = ldir_dir((lnode_t*)s, dir); + i = 0; + + memset(x, 0, sizeof(*x)); + + while (!is_null(c)) { + cmp = vtype_compare(vnode_peek(c->node, c->type), c->type, v, t); + + if (cmp == 0) { + if (!cut && !is_null(x)) { + value_set(x, &c->node, c->type, VF_WRITEABLE|VF_CHANGEABLE); + } else if (cut) lnode_cut(x, s, c); + + return i; + } + + c = ldir_dir(c, dir); + ++i; + } + + return -1; +} + + +size_t libcdsb_list_count(const list_t* s, const void* v, vtype t) { + + lnode_t* c; + size_t n; + int cmp; + + c = s->first; + n = 0; + + while (!is_null(c)) { + cmp = vtype_compare(vnode_peek(c->node, c->type), c->type, v, t); + + if (cmp == 0) ++n; + + c = c->next; + } + + return n; +} + +/*#####################################################################################################################*/ + + +_Bool libcdsb_list_update(list_t* x, ssize_t i, const void* v, vtype t, int ins) { + + ldir_t dir; + lnode_t* c; + + if (i < 0) { + i = ~i; + dir = LD_PREV; + } else dir = LD_NEXT; + + c = ldir_dir((lnode_t*)x, dir); + + while (i && !is_null(c)) { + c = ldir_dir(c, dir); + --i; + } + + if (i && dir == LD_PREV) { + c = x->first; + } else if (i) return false; + + if (is_null(c)) { + x->first = x->last = c = calloc(sizeof(*c), 1); + } else if (ins) { + lnode_t *x = malloc(sizeof(*x)); + + dir = (ins < 0) ? LD_PREV : LD_NEXT; + + ldir_dir(x, dir) = ldir_dir(c, dir); + ldir_inv(x, dir) = c; + + c = x; + + if (!is_null(ldir_dir(c, dir))) { + ldir_inv(ldir_dir(c, dir), dir) = c; + } + + ldir_dir(ldir_inv(c, dir), dir) = c; + + } else vnode_free(&c->node, c->type); + + c->node = vnode_create(v, t); + c->type = t; + + return true; +} diff --git a/src/list/generics.c b/src/list/generics.c new file mode 100644 index 0000000..f86b4a7 --- /dev/null +++ b/src/list/generics.c @@ -0,0 +1,64 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include "include.h" + +ssize_t libcdsb_list_find_pointer(val_t* x, list_t* s, const void* v, _Bool r, _Bool cut) { return libcdsb_list_find(x, s, &v, vtypeof(&v), r, cut); } +ssize_t libcdsb_list_find_cstring(val_t* x, list_t* s, const char* v, _Bool r, _Bool cut) { return libcdsb_list_find(x, s, &v, vtypeof(&v), r, cut); } +ssize_t libcdsb_list_find_string (val_t* x, list_t* s, const str_t* v, _Bool r, _Bool cut) { return libcdsb_list_find(x, s, v, vtypeof( v), r, cut); } +ssize_t libcdsb_list_find_array (val_t* x, list_t* s, const arr_t* v, _Bool r, _Bool cut) { return libcdsb_list_find(x, s, v, vtypeof( v), r, cut); } +ssize_t libcdsb_list_find_list (val_t* x, list_t* s, const list_t* v, _Bool r, _Bool cut) { return libcdsb_list_find(x, s, v, vtypeof( v), r, cut); } +ssize_t libcdsb_list_find_map (val_t* x, list_t* s, const map_t* v, _Bool r, _Bool cut) { return libcdsb_list_find(x, s, v, vtypeof( v), r, cut); } +ssize_t libcdsb_list_find_vset (val_t* x, list_t* s, const set_t* v, _Bool r, _Bool cut) { return libcdsb_list_find(x, s, v, vtypeof( v), r, cut); } +ssize_t libcdsb_list_find_boolean(val_t* x, list_t* s, _Bool v, _Bool r, _Bool cut) { return libcdsb_list_find(x, s, &v, vtypeof(&v), r, cut); } +ssize_t libcdsb_list_find_int8 (val_t* x, list_t* s, s8_t v, _Bool r, _Bool cut) { return libcdsb_list_find(x, s, &v, vtypeof(&v), r, cut); } +ssize_t libcdsb_list_find_int16 (val_t* x, list_t* s, s16_t v, _Bool r, _Bool cut) { return libcdsb_list_find(x, s, &v, vtypeof(&v), r, cut); } +ssize_t libcdsb_list_find_int32 (val_t* x, list_t* s, s32_t v, _Bool r, _Bool cut) { return libcdsb_list_find(x, s, &v, vtypeof(&v), r, cut); } +ssize_t libcdsb_list_find_int64 (val_t* x, list_t* s, s64_t v, _Bool r, _Bool cut) { return libcdsb_list_find(x, s, &v, vtypeof(&v), r, cut); } +ssize_t libcdsb_list_find_uint8 (val_t* x, list_t* s, u8_t v, _Bool r, _Bool cut) { return libcdsb_list_find(x, s, &v, vtypeof(&v), r, cut); } +ssize_t libcdsb_list_find_uint16 (val_t* x, list_t* s, u16_t v, _Bool r, _Bool cut) { return libcdsb_list_find(x, s, &v, vtypeof(&v), r, cut); } +ssize_t libcdsb_list_find_uint32 (val_t* x, list_t* s, u32_t v, _Bool r, _Bool cut) { return libcdsb_list_find(x, s, &v, vtypeof(&v), r, cut); } +ssize_t libcdsb_list_find_uint64 (val_t* x, list_t* s, u64_t v, _Bool r, _Bool cut) { return libcdsb_list_find(x, s, &v, vtypeof(&v), r, cut); } +ssize_t libcdsb_list_find_float (val_t* x, list_t* s, fl_t v, _Bool r, _Bool cut) { return libcdsb_list_find(x, s, &v, vtypeof(&v), r, cut); } +ssize_t libcdsb_list_find_double (val_t* x, list_t* s, dbl_t v, _Bool r, _Bool cut) { return libcdsb_list_find(x, s, &v, vtypeof(&v), r, cut); } +ssize_t libcdsb_list_find_ldouble(val_t* x, list_t* s, ldbl_t v, _Bool r, _Bool cut) { return libcdsb_list_find(x, s, &v, vtypeof(&v), r, cut); } + +size_t libcdsb_list_count_pointer(const list_t* s, const void* v) { return libcdsb_list_count((void*)s, &v, vtypeof(&v)); } +size_t libcdsb_list_count_cstring(const list_t* s, const char* v) { return libcdsb_list_count((void*)s, &v, vtypeof(&v)); } +size_t libcdsb_list_count_string (const list_t* s, const str_t* v) { return libcdsb_list_count((void*)s, v, vtypeof( v)); } +size_t libcdsb_list_count_array (const list_t* s, const arr_t* v) { return libcdsb_list_count((void*)s, v, vtypeof( v)); } +size_t libcdsb_list_count_list (const list_t* s, const list_t* v) { return libcdsb_list_count((void*)s, v, vtypeof( v)); } +size_t libcdsb_list_count_map (const list_t* s, const map_t* v) { return libcdsb_list_count((void*)s, v, vtypeof( v)); } +size_t libcdsb_list_count_vset (const list_t* s, const set_t* v) { return libcdsb_list_count((void*)s, v, vtypeof( v)); } +size_t libcdsb_list_count_boolean(const list_t* s, _Bool v) { return libcdsb_list_count((void*)s, &v, vtypeof(&v)); } +size_t libcdsb_list_count_int8 (const list_t* s, s8_t v) { return libcdsb_list_count((void*)s, &v, vtypeof(&v)); } +size_t libcdsb_list_count_int16 (const list_t* s, s16_t v) { return libcdsb_list_count((void*)s, &v, vtypeof(&v)); } +size_t libcdsb_list_count_int32 (const list_t* s, s32_t v) { return libcdsb_list_count((void*)s, &v, vtypeof(&v)); } +size_t libcdsb_list_count_int64 (const list_t* s, s64_t v) { return libcdsb_list_count((void*)s, &v, vtypeof(&v)); } +size_t libcdsb_list_count_uint8 (const list_t* s, u8_t v) { return libcdsb_list_count((void*)s, &v, vtypeof(&v)); } +size_t libcdsb_list_count_uint16 (const list_t* s, u16_t v) { return libcdsb_list_count((void*)s, &v, vtypeof(&v)); } +size_t libcdsb_list_count_uint32 (const list_t* s, u32_t v) { return libcdsb_list_count((void*)s, &v, vtypeof(&v)); } +size_t libcdsb_list_count_uint64 (const list_t* s, u64_t v) { return libcdsb_list_count((void*)s, &v, vtypeof(&v)); } +size_t libcdsb_list_count_float (const list_t* s, fl_t v) { return libcdsb_list_count((void*)s, &v, vtypeof(&v)); } +size_t libcdsb_list_count_double (const list_t* s, dbl_t v) { return libcdsb_list_count((void*)s, &v, vtypeof(&v)); } +size_t libcdsb_list_count_ldouble(const list_t* s, ldbl_t v) { return libcdsb_list_count((void*)s, &v, vtypeof(&v)); } + +_Bool libcdsb_list_update_pointer(list_t* x, ssize_t i, const void* v, int ins) { return libcdsb_list_update(x, i, &v, vtypeof(&v), ins); } +_Bool libcdsb_list_update_cstring(list_t* x, ssize_t i, const char* v, int ins) { return libcdsb_list_update(x, i, &v, vtypeof(&v), ins); } +_Bool libcdsb_list_update_string (list_t* x, ssize_t i, const str_t* v, int ins) { return libcdsb_list_update(x, i, v, vtypeof( v), ins); } +_Bool libcdsb_list_update_array (list_t* x, ssize_t i, const arr_t* v, int ins) { return libcdsb_list_update(x, i, v, vtypeof( v), ins); } +_Bool libcdsb_list_update_list (list_t* x, ssize_t i, const list_t* v, int ins) { return libcdsb_list_update(x, i, v, vtypeof( v), ins); } +_Bool libcdsb_list_update_map (list_t* x, ssize_t i, const map_t* v, int ins) { return libcdsb_list_update(x, i, v, vtypeof( v), ins); } +_Bool libcdsb_list_update_vset (list_t* x, ssize_t i, const set_t* v, int ins) { return libcdsb_list_update(x, i, v, vtypeof( v), ins); } +_Bool libcdsb_list_update_boolean(list_t* x, ssize_t i, _Bool v, int ins) { return libcdsb_list_update(x, i, &v, vtypeof(&v), ins); } +_Bool libcdsb_list_update_int8 (list_t* x, ssize_t i, s8_t v, int ins) { return libcdsb_list_update(x, i, &v, vtypeof(&v), ins); } +_Bool libcdsb_list_update_int16 (list_t* x, ssize_t i, s16_t v, int ins) { return libcdsb_list_update(x, i, &v, vtypeof(&v), ins); } +_Bool libcdsb_list_update_int32 (list_t* x, ssize_t i, s32_t v, int ins) { return libcdsb_list_update(x, i, &v, vtypeof(&v), ins); } +_Bool libcdsb_list_update_int64 (list_t* x, ssize_t i, s64_t v, int ins) { return libcdsb_list_update(x, i, &v, vtypeof(&v), ins); } +_Bool libcdsb_list_update_uint8 (list_t* x, ssize_t i, u8_t v, int ins) { return libcdsb_list_update(x, i, &v, vtypeof(&v), ins); } +_Bool libcdsb_list_update_uint16 (list_t* x, ssize_t i, u16_t v, int ins) { return libcdsb_list_update(x, i, &v, vtypeof(&v), ins); } +_Bool libcdsb_list_update_uint32 (list_t* x, ssize_t i, u32_t v, int ins) { return libcdsb_list_update(x, i, &v, vtypeof(&v), ins); } +_Bool libcdsb_list_update_uint64 (list_t* x, ssize_t i, u64_t v, int ins) { return libcdsb_list_update(x, i, &v, vtypeof(&v), ins); } +_Bool libcdsb_list_update_float (list_t* x, ssize_t i, fl_t v, int ins) { return libcdsb_list_update(x, i, &v, vtypeof(&v), ins); } +_Bool libcdsb_list_update_double (list_t* x, ssize_t i, dbl_t v, int ins) { return libcdsb_list_update(x, i, &v, vtypeof(&v), ins); } +_Bool libcdsb_list_update_ldouble(list_t* x, ssize_t i, ldbl_t v, int ins) { return libcdsb_list_update(x, i, &v, vtypeof(&v), ins); } diff --git a/src/list/include.h b/src/list/include.h new file mode 100644 index 0000000..e88d623 --- /dev/null +++ b/src/list/include.h @@ -0,0 +1,28 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include "../../include/extra/list.h" +#include "../__internal/vnode.h" + +#ifndef LIBCDSB_SRC_LIST_INCLUDE_H +#define LIBCDSB_SRC_LIST_INCLUDE_H + +typedef enum libcdsb_list_direction { + LD_PREV = 1, + LD_NEXT = 2 +} ldir_t; + +typedef struct libcdsb_list_node { + struct libcdsb_list_node* prev; + struct libcdsb_list_node* next; + + vnode_t node; + vtype type; +} lnode_t; + +#define ldir_dir(cur, d) (&((cur)->prev))[(d)>>1] +#define ldir_inv(cur, d) (&((cur)->prev))[(d)&1] + +#define lnode_compare(s0, s1) vnode_compare((s0)->node, (s0)->type, (s1)->node, (s1)->type) + +#endif /* LIBCDSB_SRC_LIST_INCLUDE_H */ diff --git a/src/list/sort.c b/src/list/sort.c new file mode 100644 index 0000000..b4623d9 --- /dev/null +++ b/src/list/sort.c @@ -0,0 +1,104 @@ +/* This software is licensed by the MIT License, see LICENSE file */ +/* Copyright © 2022 Gregory Lirent */ + +#include "include.h" + +/*#####################################################################################################################*/ + +static inline void lnode_swap(lnode_t* s0, lnode_t* s1) { + vnode_t v = s0->node; + vtype t = s0->type; + + s0->node = s1->node; + s0->type = s1->type; + + s1->node = v; + s1->type = t; +} + +static inline lnode_t* mid(lnode_t* l, lnode_t *r) { + while (l != r) { + if ((r = r->prev) == l) + break; + l = l->next; + } + + return l; +} + +static inline lnode_t* pivot(lnode_t *l, lnode_t *r) { + + lnode_t *m = mid(l, r); + + if (lnode_compare(m, l) < 0) + lnode_swap(m, l); + + if (lnode_compare(m, r) > 0) { + lnode_swap(m, r); + + if (lnode_compare(m, l) < 0) + lnode_swap(m, l); + } + + return m; +} + +/*#####################################################################################################################*/ + +static void quick_sort(lnode_t *l, lnode_t *r) { + + lnode_t *c; + lnode_t *m = pivot(l, r); + lnode_t *n = l; + lnode_t *p = r; + int f = 0; + + for (;;) { + while (lnode_compare(l, m) < 0) { + l = l->next; + if (p == l) /* l >= p */ + f |= 0x01; + } + + c = l->prev; + + while (lnode_compare(r, m) > 0) { + r = r->prev; + if (n == r) /* n >= r */ + f |= 0x02; + if (c == r) /* l > r */ + f |= 0x04; + } + + if (!(f&0x04)) { /* l <= r */ + lnode_swap(l, r); + l = l->next; + r = r->prev;; + } else break; + } + + if (!(f&0x02)) /* n < r */ + quick_sort(n, r); + if (!(f&0x01)) /* l < p */ + quick_sort(l, p); +} + +/*#####################################################################################################################*/ + +void list_sort(list_t* x) { + if (x->first != x->last) + return quick_sort(x->first, x->last);; +} + + +void list_reverse(list_t* x) { + lnode_t *l = x->first; + lnode_t *r = x->last; + + while (l != r) { + lnode_swap(l, r); + if ((r = r->prev) == l) + break; + l = l->next; + } +}