Refactor array, add attaching functional
This commit is contained in:
@@ -4,6 +4,38 @@
|
||||
#ifndef LIBCDSB_CORE_GENERICS_H
|
||||
#define LIBCDSB_CORE_GENERICS_H
|
||||
|
||||
#define vtypeof(x) (vtype)(_Generic((x),\
|
||||
const void*: VTYPE_POINTER, void*: VTYPE_POINTER,\
|
||||
const char**: VTYPE_STRING, char**: VTYPE_STRING,\
|
||||
const vtype_string*: VTYPE_STRING, vtype_string*: VTYPE_STRING,\
|
||||
const vtype_array*: VTYPE_ARRAY, vtype_array*: VTYPE_ARRAY,\
|
||||
const vtype_list*: VTYPE_LIST, vtype_list*: VTYPE_LIST,\
|
||||
const vtype_map*: VTYPE_MAP, vtype_map*: VTYPE_MAP,\
|
||||
const vtype_set*: VTYPE_SET, vtype_set*: VTYPE_SET,\
|
||||
const vtype_dict*: VTYPE_DICT, vtype_dict*: VTYPE_DICT,\
|
||||
vtype_bool: VTYPE_BOOLEAN,\
|
||||
vtype_uint8: VTYPE_UINT8,\
|
||||
vtype_uint16: VTYPE_UINT16,\
|
||||
vtype_uint32: VTYPE_UINT32,\
|
||||
vtype_uint64: VTYPE_UINT64,\
|
||||
vtype_int8: VTYPE_INT8,\
|
||||
vtype_int16: VTYPE_INT16,\
|
||||
vtype_int32: VTYPE_INT32,\
|
||||
vtype_int64: VTYPE_INT64,\
|
||||
vtype_float: VTYPE_FLOAT,\
|
||||
vtype_double: VTYPE_DOUBLE,\
|
||||
vtype_ldouble: VTYPE_LDOUBLE))
|
||||
|
||||
#define _LIBCDSB_vtypeof(x) vtypeof(_Generic((x), default: (x), const char*: &(x), char*: &(x)))
|
||||
#define _LIBCDSB_value_pointer(x) _Generic((x), default: &(x),\
|
||||
vtype_string*: (x), const vtype_string*: (x),\
|
||||
vtype_array*: (x), const vtype_array*: (x),\
|
||||
vtype_list*: (x), const vtype_list*: (x),\
|
||||
vtype_map*: (x), const vtype_map*: (x),\
|
||||
vtype_set*: (x), const vtype_set*: (x),\
|
||||
vtype_dict*: (x), const vtype_dict*: (x))
|
||||
|
||||
|
||||
#define _LIBCDSB_Generic(T, f, v) _Generic((v),\
|
||||
void*: T ## _ ## f ## _pointer, const void*: T ## _ ## f ## _pointer,\
|
||||
char*: T ## _ ## f ## _cstring, const char*: T ## _ ## f ## _cstring,\
|
||||
@@ -50,6 +82,29 @@
|
||||
vtype_ldouble: _LIBCDSB_Generic(T, f ## _ldouble, v)\
|
||||
)
|
||||
|
||||
#define _LIBCDSB_Generic_attach(T, ins_f, att_f, v) _Generic((v),\
|
||||
void*: T ## _ ## ins_f ## _pointer, const void*: T ## _ ## ins_f ## _pointer,\
|
||||
char*: T ## _ ## ins_f ## _cstring, const char*: T ## _ ## ins_f ## _cstring,\
|
||||
vtype_string*: T ## _ ## att_f ## _string, const vtype_string*: T ## _ ## att_f ## _string,\
|
||||
vtype_array*: T ## _ ## att_f ## _array, const vtype_array*: T ## _ ## att_f ## _array,\
|
||||
vtype_list*: T ## _ ## att_f ## _list, const vtype_list*: T ## _ ## att_f ## _list,\
|
||||
vtype_map*: T ## _ ## att_f ## _map, const vtype_map*: T ## _ ## att_f ## _map,\
|
||||
vtype_set*: T ## _ ## att_f ## _vset, const vtype_set*: T ## _ ## att_f ## _vset,\
|
||||
vtype_dict*: T ## _ ## att_f ## _dict, const vtype_dict*: T ## _ ## att_f ## _dict,\
|
||||
vtype_bool: T ## _ ## ins_f ## _boolean,\
|
||||
vtype_uint8: T ## _ ## ins_f ## _uint8,\
|
||||
vtype_uint16: T ## _ ## ins_f ## _uint16,\
|
||||
vtype_uint32: T ## _ ## ins_f ## _uint32,\
|
||||
vtype_uint64: T ## _ ## ins_f ## _uint64,\
|
||||
vtype_int8: T ## _ ## ins_f ## _int8,\
|
||||
vtype_int16: T ## _ ## ins_f ## _int16,\
|
||||
vtype_int32: T ## _ ## ins_f ## _int32,\
|
||||
vtype_int64: T ## _ ## ins_f ## _int64,\
|
||||
vtype_float: T ## _ ## ins_f ## _float,\
|
||||
vtype_double: T ## _ ## ins_f ## _double,\
|
||||
vtype_ldouble: T ## _ ## ins_f ## _ldouble\
|
||||
)
|
||||
|
||||
#define _LIBCDSB_GenericS(T, f, v) _Generic((v),\
|
||||
void*: T ## _ ## f ## _cstring, const void*: T ## _ ## f ## _cstring,\
|
||||
char*: T ## _ ## f ## _cstring, const char*: T ## _ ## f ## _cstring,\
|
||||
|
||||
+28
-75
@@ -7,89 +7,42 @@
|
||||
#ifndef LIBCDSB_ARRAY_H
|
||||
#define LIBCDSB_ARRAY_H
|
||||
|
||||
/*#####################################################################################################################*/
|
||||
|
||||
typedef int (*array_access_callback)(void* value, ssize_t index, vtype type, void* data);
|
||||
|
||||
extern void array_init (vtype_array* x, vtype type) Nonnull__(1);
|
||||
extern void* array_at (const vtype_array* s, ssize_t index) Nonnull__(1);
|
||||
extern size_t array_slice(vtype_array* x, vtype_array* src, ssize_t index, size_t count, bool cut) Nonnull__(1);
|
||||
/*#####################################################################################################################*/
|
||||
|
||||
extern void array_sort (vtype_array* x) Nonnull__(1);
|
||||
extern void array_reverse(vtype_array* x) Nonnull__(1);
|
||||
extern void array_init (vtype_array* x, vtype type) Nonnull__(1);
|
||||
extern size_t array_slice (vtype_array* x, vtype_array* src, ssize_t index, size_t count, bool cut) Nonnull__(1);
|
||||
extern void array_sort (vtype_array* x) Nonnull__(1);
|
||||
extern void array_reverse (vtype_array* x) Nonnull__(1);
|
||||
|
||||
#define array_pop(x, value, data, callback) _LIBCDSB_Generic(libcdsb_array, find, value)(x, value, data, callback, 0, 1)
|
||||
#define array_find(x, value, data, callback) _LIBCDSB_Generic(libcdsb_array, find, value)(x, value, data, callback, 0, 0)
|
||||
#define array_rfind(x, value, data, callback) _LIBCDSB_Generic(libcdsb_array, find, value)(x, value, data, callback, 1, 0)
|
||||
#define array_countof(x, value) _LIBCDSB_Generic(libcdsb_array, count, value)(x, value)
|
||||
#define array_remove(x, value) array_pop(x, value, 0, 0)
|
||||
#define in_array(x, value) (array_find(x, value, 0, 0) == 0)
|
||||
|
||||
#define array_push_back(x, value) _LIBCDSB_Generic(libcdsb_array, push, value)(x, value)
|
||||
extern void* at_array(const vtype_array* s, ssize_t index) Nonnull__(1);
|
||||
|
||||
/*#####################################################################################################################*/
|
||||
|
||||
extern void libcdsb_array_push_pointer(vtype_array* x, const void* value) Nonnull__(1);
|
||||
extern void libcdsb_array_push_cstring(vtype_array* x, const char* value) Nonnull__(1,2);
|
||||
extern void libcdsb_array_push_string (vtype_array* x, const vtype_string* value) Nonnull__(1,2);
|
||||
extern void libcdsb_array_push_array (vtype_array* x, const vtype_array* value) Nonnull__(1,2);
|
||||
extern void libcdsb_array_push_list (vtype_array* x, const vtype_list* value) Nonnull__(1,2);
|
||||
extern void libcdsb_array_push_map (vtype_array* x, const vtype_map* value) Nonnull__(1,2);
|
||||
extern void libcdsb_array_push_vset (vtype_array* x, const vtype_set* value) Nonnull__(1,2);
|
||||
extern void libcdsb_array_push_dict (vtype_array* x, const vtype_dict* value) Nonnull__(1,2);
|
||||
extern void libcdsb_array_push_boolean(vtype_array* x, vtype_bool value) Nonnull__(1);
|
||||
extern void libcdsb_array_push_uint8 (vtype_array* x, vtype_uint8 value) Nonnull__(1);
|
||||
extern void libcdsb_array_push_uint16 (vtype_array* x, vtype_uint16 value) Nonnull__(1);
|
||||
extern void libcdsb_array_push_uint32 (vtype_array* x, vtype_uint32 value) Nonnull__(1);
|
||||
extern void libcdsb_array_push_uint64 (vtype_array* x, vtype_uint64 value) Nonnull__(1);
|
||||
extern void libcdsb_array_push_int8 (vtype_array* x, vtype_int8 value) Nonnull__(1);
|
||||
extern void libcdsb_array_push_int16 (vtype_array* x, vtype_int16 value) Nonnull__(1);
|
||||
extern void libcdsb_array_push_int32 (vtype_array* x, vtype_int32 value) Nonnull__(1);
|
||||
extern void libcdsb_array_push_int64 (vtype_array* x, vtype_int64 value) Nonnull__(1);
|
||||
extern void libcdsb_array_push_float (vtype_array* x, vtype_float value) Nonnull__(1);
|
||||
extern void libcdsb_array_push_double (vtype_array* x, vtype_double value) Nonnull__(1);
|
||||
extern void libcdsb_array_push_ldouble(vtype_array* x, vtype_ldouble value) Nonnull__(1);
|
||||
#define array_pop(x, value, data, callback) libcdsb_array_find (x, _LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value), data, callback, 0, 1)
|
||||
#define array_get array_find
|
||||
#define array_pop_by_index(x, index, data, callback) libcdsb_array_get (x, index, data, callback, 1)
|
||||
#define array_get_by_index(x, index, data, callback) libcdsb_array_get (x, index, data, callback, 0)
|
||||
#define array_find(x, value, data, callback) libcdsb_array_find (x, _LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value), data, callback, 0, 0)
|
||||
#define array_rfind(x, value, data, callback) libcdsb_array_find (x, _LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value), data, callback, 1, 0)
|
||||
#define array_countof(x, value) libcdsb_array_count (x, _LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value))
|
||||
#define array_push_back(x, value) libcdsb_array_insert (x, _LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value))
|
||||
#define array_attach_back(x, value) libcdsb_array_attach (x, _LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value))
|
||||
#define array_foreach(x, data, callback) libcdsb_array_foreach(x, data, callback, 0)
|
||||
#define array_remove(x, value) array_pop (x, value, 0, 0)
|
||||
#define array_remove_by_index(x, index) array_pop_by_index (x, index, 0, 0)
|
||||
|
||||
extern size_t libcdsb_array_count_pointer(const vtype_array* s, const void* value) Nonnull__(1);
|
||||
extern size_t libcdsb_array_count_cstring(const vtype_array* s, const char* value) Nonnull__(1,2);
|
||||
extern size_t libcdsb_array_count_string (const vtype_array* s, const vtype_string* value) Nonnull__(1,2);
|
||||
extern size_t libcdsb_array_count_array (const vtype_array* s, const vtype_array* value) Nonnull__(1,2);
|
||||
extern size_t libcdsb_array_count_list (const vtype_array* s, const vtype_list* value) Nonnull__(1,2);
|
||||
extern size_t libcdsb_array_count_map (const vtype_array* s, const vtype_map* value) Nonnull__(1,2);
|
||||
extern size_t libcdsb_array_count_vset (const vtype_array* s, const vtype_set* value) Nonnull__(1,2);
|
||||
extern size_t libcdsb_array_count_dict (const vtype_array* s, const vtype_dict* value) Nonnull__(1,2);
|
||||
extern size_t libcdsb_array_count_boolean(const vtype_array* s, vtype_bool value) Nonnull__(1);
|
||||
extern size_t libcdsb_array_count_int8 (const vtype_array* s, vtype_int8 value) Nonnull__(1);
|
||||
extern size_t libcdsb_array_count_int16 (const vtype_array* s, vtype_int16 value) Nonnull__(1);
|
||||
extern size_t libcdsb_array_count_int32 (const vtype_array* s, vtype_int32 value) Nonnull__(1);
|
||||
extern size_t libcdsb_array_count_int64 (const vtype_array* s, vtype_int64 value) Nonnull__(1);
|
||||
extern size_t libcdsb_array_count_uint8 (const vtype_array* s, vtype_uint8 value) Nonnull__(1);
|
||||
extern size_t libcdsb_array_count_uint16 (const vtype_array* s, vtype_uint16 value) Nonnull__(1);
|
||||
extern size_t libcdsb_array_count_uint32 (const vtype_array* s, vtype_uint32 value) Nonnull__(1);
|
||||
extern size_t libcdsb_array_count_uint64 (const vtype_array* s, vtype_uint64 value) Nonnull__(1);
|
||||
extern size_t libcdsb_array_count_float (const vtype_array* s, vtype_float value) Nonnull__(1);
|
||||
extern size_t libcdsb_array_count_double (const vtype_array* s, vtype_double value) Nonnull__(1);
|
||||
extern size_t libcdsb_array_count_ldouble(const vtype_array* s, vtype_ldouble value) Nonnull__(1);
|
||||
#define in_array(x, value) (array_get(x, value, 0, 0) == 0)
|
||||
|
||||
extern int libcdsb_array_find_pointer(vtype_array* x, const void* value, void* data, array_access_callback, bool reverse, bool cut) Nonnull__(1);
|
||||
extern int libcdsb_array_find_cstring(vtype_array* x, const char* value, void* data, array_access_callback, bool reverse, bool cut) Nonnull__(1,2);
|
||||
extern int libcdsb_array_find_string (vtype_array* x, const vtype_string* value, void* data, array_access_callback, bool reverse, bool cut) Nonnull__(1,2);
|
||||
extern int libcdsb_array_find_array (vtype_array* x, const vtype_array* value, void* data, array_access_callback, bool reverse, bool cut) Nonnull__(1,2);
|
||||
extern int libcdsb_array_find_list (vtype_array* x, const vtype_list* value, void* data, array_access_callback, bool reverse, bool cut) Nonnull__(1,2);
|
||||
extern int libcdsb_array_find_map (vtype_array* x, const vtype_map* value, void* data, array_access_callback, bool reverse, bool cut) Nonnull__(1,2);
|
||||
extern int libcdsb_array_find_vset (vtype_array* x, const vtype_set* value, void* data, array_access_callback, bool reverse, bool cut) Nonnull__(1,2);
|
||||
extern int libcdsb_array_find_dict (vtype_array* x, const vtype_dict* value, void* data, array_access_callback, bool reverse, bool cut) Nonnull__(1,2);
|
||||
extern int libcdsb_array_find_boolean(vtype_array* x, vtype_bool value, void* data, array_access_callback, bool reverse, bool cut) Nonnull__(1);
|
||||
extern int libcdsb_array_find_uint8 (vtype_array* x, vtype_uint8 value, void* data, array_access_callback, bool reverse, bool cut) Nonnull__(1);
|
||||
extern int libcdsb_array_find_uint16 (vtype_array* x, vtype_uint16 value, void* data, array_access_callback, bool reverse, bool cut) Nonnull__(1);
|
||||
extern int libcdsb_array_find_uint32 (vtype_array* x, vtype_uint32 value, void* data, array_access_callback, bool reverse, bool cut) Nonnull__(1);
|
||||
extern int libcdsb_array_find_uint64 (vtype_array* x, vtype_uint64 value, void* data, array_access_callback, bool reverse, bool cut) Nonnull__(1);
|
||||
extern int libcdsb_array_find_int8 (vtype_array* x, vtype_int8 value, void* data, array_access_callback, bool reverse, bool cut) Nonnull__(1);
|
||||
extern int libcdsb_array_find_int16 (vtype_array* x, vtype_int16 value, void* data, array_access_callback, bool reverse, bool cut) Nonnull__(1);
|
||||
extern int libcdsb_array_find_int32 (vtype_array* x, vtype_int32 value, void* data, array_access_callback, bool reverse, bool cut) Nonnull__(1);
|
||||
extern int libcdsb_array_find_int64 (vtype_array* x, vtype_int64 value, void* data, array_access_callback, bool reverse, bool cut) Nonnull__(1);
|
||||
extern int libcdsb_array_find_float (vtype_array* x, vtype_float value, void* data, array_access_callback, bool reverse, bool cut) Nonnull__(1);
|
||||
extern int libcdsb_array_find_double (vtype_array* x, vtype_double value, void* data, array_access_callback, bool reverse, bool cut) Nonnull__(1);
|
||||
extern int libcdsb_array_find_ldouble(vtype_array* x, vtype_ldouble value, void* data, array_access_callback, bool reverse, bool cut) Nonnull__(1);
|
||||
/*#####################################################################################################################*/
|
||||
|
||||
extern ssize_t libcdsb_array_insert (vtype_array* x, const void* value, vtype type) Nonnull__(1);
|
||||
extern ssize_t libcdsb_array_attach (vtype_array* x, const void* value, vtype type) Nonnull__(1);
|
||||
extern int libcdsb_array_find (vtype_array* x, const void* value, vtype type, void* data, array_access_callback, bool reverse, bool cut) Nonnull__(1);
|
||||
extern int libcdsb_array_get (vtype_array* x, ssize_t index, void* data, array_access_callback, bool cut) Nonnull__(1);
|
||||
extern int libcdsb_array_foreach (vtype_array* x, void* data, array_access_callback, bool flush) Nonnull__(1,3);
|
||||
|
||||
extern size_t libcdsb_array_count(const vtype_array* s, const void* value, vtype type) Pure__ Warn_unused_result__ Nonnull__(1);
|
||||
|
||||
#endif /* LIBCDSB_ARRAY_H */
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
/* 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
|
||||
|
||||
#define array_get_by_index(s, index, data, callback) libcdsb_array_get(s, index, data, callback, 0)
|
||||
#define array_pop_by_index(s, index, data, callback) libcdsb_array_get(s, index, data, callback, 1)
|
||||
#define array_remove_by_index(s, index) libcdsb_array_get(s, index, 0, 0, 1)
|
||||
|
||||
#define array_foreach(x, data, callback) libcdsb_array_foreach(x, data, callback, 0)
|
||||
|
||||
extern ssize_t libcdsb_array_push (vtype_array* x, const void* value, vtype value_type) Nonnull__(1);
|
||||
extern size_t libcdsb_array_count (const vtype_array* s, const void* value, vtype type) Pure__ Warn_unused_result__ Nonnull__(1);
|
||||
extern int libcdsb_array_find (vtype_array* x, const void* value, vtype type, void* data, array_access_callback, bool reverse, bool cut) Nonnull__(1);
|
||||
extern int libcdsb_array_get (vtype_array* x, ssize_t index, void* data, array_access_callback, bool cut) Nonnull__(1);
|
||||
extern int libcdsb_array_foreach(vtype_array* x, void* data, array_access_callback, bool flush) Nonnull__(1,3);
|
||||
|
||||
#endif /* LIBCDSB_EXTRA_ARRAY_H */
|
||||
Reference in New Issue
Block a user