diff options
Diffstat (limited to 'src/array.h')
-rw-r--r-- | src/array.h | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/array.h b/src/array.h new file mode 100644 index 0000000..9affdde --- /dev/null +++ b/src/array.h @@ -0,0 +1,21 @@ +// SPDX-FileCopyrightText: 2024 Lizzy Fleckenstein <lizzy@vlhl.dev> +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +#ifndef ARRAY_H +#define ARRAY_H + +#include <stddef.h> +#include <string.h> +#include <stdlib.h> + +#define len(X) (sizeof X / sizeof *X) +#define array(T) struct { size_t len; T *data; } +#define arraybuf(T) struct { size_t cap; size_t len; T *data; } + +#define ARR_REMOVE(A, P) memmove((P), (P)+1, sizeof *(P) * (--(A).len - ((P) - (A).data))); +#define ARR_APPEND(A) (((A).cap == (A).len) \ + ? (A).data = realloc((A).data, sizeof *(A).data * ((A).cap = (A).cap ? (A).cap * 2 : 1)) \ + : NULL, &((A).data)[(A).len++]) + +#endif |