libcdsb/include/list.h

54 lines
4.4 KiB
C

/* This software is licensed by the MIT License, see LICENSE file */
/* Copyright © 2022 Gregory Lirent */
#include "bits/__generics.h"
#include "vtype.h"
#ifndef LIBCDSB_LIST_H
#define LIBCDSB_LIST_H
typedef int (*list_access_callback)(void* value, ssize_t index, vtype type, void* data);
/*#####################################################################################################################*/
inline void list_init (vtype_list* x) Always_inline__ Nonnull__(1);
extern void list_extend (vtype_list* x, const vtype_list* s) Nonnull__(1,2);
extern size_t list_slice (vtype_list* x, vtype_list* src, ssize_t index, size_t count, bool cut) Nonnull__(1,2);
extern void list_sort (vtype_list* x) Nonnull__(1);
extern void list_reverse(vtype_list* x) Nonnull__(1);
inline void list_init (vtype_list* x) { x->first = x->last = 0; }
/*#####################################################################################################################*/
#define list_pop(x, value, data, callback) libcdsb_list_find (x, _LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value), data, callback, 0, 1)
#define list_get list_find
#define list_pop_by_index(x, index, data, callback) libcdsb_list_get (x, index, data, callback, 1)
#define list_get_by_index(x, index, data, callback) libcdsb_list_get (x, index, data, callback, 0)
#define list_find(x, value, data, callback) libcdsb_list_find (x, _LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value), data, callback, 0, 0)
#define list_rfind(x, value, data, callback) libcdsb_list_find (x, _LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value), data, callback, 1, 0)
#define list_countof(x, value) libcdsb_list_count (x, _LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value))
#define list_insert(x, index, value) libcdsb_list_insert (x, index, _LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value), -1, 0, 0)
#define list_replace(x, index, value) libcdsb_list_insert (x, index, _LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value), 0, 0, 0)
#define list_push_back(x, value) libcdsb_list_insert (x, -1, _LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value), 1, 0, 0)
#define list_push_front(x, value) libcdsb_list_insert (x, 0, _LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value), -1, 0, 0)
#define list_attach_back(x, value) libcdsb_list_attach (x, -1, _LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value), 1, 0, 0)
#define list_attach_front(x, value) libcdsb_list_attach (x, 0, _LIBCDSB_value_pointer(value), _LIBCDSB_vtypeof(value), -1, 0, 0)
#define list_foreach(x, data, callback) libcdsb_list_foreach(x, data, callback, 0)
#define list_remove(x, value) list_pop (x, value, 0, 0)
#define list_remove_by_index(x, index) list_pop_by_index (x, index, 0, 0)
#define in_list(x, value) (list_get(x, value, 0, 0) == 0)
/*#####################################################################################################################*/
extern bool libcdsb_list_insert (vtype_list* x, ssize_t index, const void* value, vtype type, int ins_direction, void* data, list_access_callback) Nonnull__(1);
extern bool libcdsb_list_attach (vtype_list* x, ssize_t index, const void* value, vtype type, int ins_direction, void* data, list_access_callback) Nonnull__(1);
extern int libcdsb_list_find (vtype_list* x, const void* value, vtype type, void* data, list_access_callback, bool reverse, bool cut) Nonnull__(1);
extern int libcdsb_list_get (vtype_list* x, ssize_t index, void* data, list_access_callback, bool cut) Nonnull__(1);
extern int libcdsb_list_foreach (vtype_list* x, void* data, list_access_callback, bool flush) Nonnull__(1,3);
extern size_t libcdsb_list_count(const vtype_list* s, const void* value, vtype type) Pure__ Warn_unused_result__ Nonnull__(1);
#endif /* LIBCDSB_LIST_H */