summaryrefslogtreecommitdiff
path: root/src/array.h
diff options
context:
space:
mode:
authorJohnny Richard <johnny@johnnyrichard.com>2025-04-11 01:15:01 +0200
committerJohnny Richard <johnny@johnnyrichard.com>2025-04-14 23:11:22 +0200
commite7f69c8fbbbcbddde84933b2becd91e787d1ac63 (patch)
tree16cd17da17133494dd06aab614724e76b059d4ad /src/array.h
Intial commit
Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
Diffstat (limited to 'src/array.h')
-rw-r--r--src/array.h35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/array.h b/src/array.h
new file mode 100644
index 0000000..75d88d0
--- /dev/null
+++ b/src/array.h
@@ -0,0 +1,35 @@
+#ifndef ARRAY_H
+#define ARRAY_H
+#include <stddef.h>
+
+#define ARRAY_INITIAL_CAPACITY 4
+
+#define array(T) (T *)array_new(sizeof(T))
+
+#define array_append(arr, item) do { \
+ array_header_t *h = array_get_header(arr); \
+ if (h->capacity < h->length + 1) { \
+ arr = array_grow(arr); \
+ } \
+ arr[h->length++] = item; \
+ } while (0)
+
+typedef struct array_header {
+ size_t capacity;
+ size_t item_size;
+ size_t length;
+} array_header_t;
+
+void *
+array_new(size_t item_size);
+
+array_header_t *
+array_get_header(void *arr);
+
+void *
+array_grow(void *arr);
+
+size_t
+array_length(void *arr);
+
+#endif /* ARRAY_H */