libcdsb/include/array.h

51 lines
3.8 KiB
C
Raw Permalink Normal View History

2022-06-02 15:52:43 +03:00
/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
2022-08-24 11:43:27 +03:00
#include "bits/__generics.h"
2022-06-02 15:52:43 +03:00
#include "vtype.h"
#ifndef LIBCDSB_ARRAY_H
#define LIBCDSB_ARRAY_H
2022-06-09 16:26:11 +03:00
typedef int (*array_access_callback)(void* value, ssize_t index, vtype type, void* data);
/*#####################################################################################################################*/
2022-06-02 15:52:43 +03:00
2022-08-19 20:00:10 +03:00
inline void array_init (vtype_array* x, vtype type) Always_inline__ 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);
2022-06-02 15:52:43 +03:00
extern void* at_array(const vtype_array* s, ssize_t index) Nonnull__(1);
2022-06-02 15:52:43 +03:00
2022-08-19 20:00:10 +03:00
inline void array_init (vtype_array* x, vtype type) { x->type = type; x->mem = (void*)(x->size = 0); }
2022-06-03 18:34:57 +03:00
/*#####################################################################################################################*/
#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)
#define in_array(x, value) (array_get(x, value, 0, 0) == 0)
/*#####################################################################################################################*/
2022-06-02 15:52:43 +03:00
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);
2022-06-09 16:26:11 +03:00
extern size_t libcdsb_array_count(const vtype_array* s, const void* value, vtype type) Pure__ Warn_unused_result__ Nonnull__(1);
2022-06-02 15:52:43 +03:00
#endif /* LIBCDSB_ARRAY_H */