summaryrefslogtreecommitdiff
path: root/src/array.h
diff options
context:
space:
mode:
authorLizzy Fleckenstein <lizzy@vlhl.dev>2024-07-13 12:51:05 +0200
committerLizzy Fleckenstein <lizzy@vlhl.dev>2024-07-13 12:51:05 +0200
commit591232eba1809643df1f4a6075ff9581b8bb2f7a (patch)
tree92e2c464158d03ef9c83f153ba5b88c39581d9f8 /src/array.h
parent95283d70eee749d984b2ca07c0b61d39f9bb350d (diff)
move headers to src
Signed-off-by: Lizzy Fleckenstein <lizzy@vlhl.dev>
Diffstat (limited to 'src/array.h')
-rw-r--r--src/array.h21
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