aboutsummaryrefslogtreecommitdiff
path: root/teensy/dyn_arr.h
diff options
context:
space:
mode:
authoriamcheeseman <[email protected]>2026-05-09 17:15:10 -0400
committeriamcheeseman <[email protected]>2026-05-09 17:15:10 -0400
commit9ed5698b3c74c7ce1784d3bebe2aa73d5a0c319d (patch)
treea80df2a06ea9cf3ac2443a2fe683aaa2fdbafab5 /teensy/dyn_arr.h
parentcda8e15a87e46342a268b5da79f53ff53ebf6cbb (diff)
move more of the dynamic array code to be macros
Diffstat (limited to 'teensy/dyn_arr.h')
-rw-r--r--teensy/dyn_arr.h50
1 files changed, 33 insertions, 17 deletions
diff --git a/teensy/dyn_arr.h b/teensy/dyn_arr.h
index 7a45d4e..a764cda 100644
--- a/teensy/dyn_arr.h
+++ b/teensy/dyn_arr.h
@@ -2,32 +2,48 @@
#define DYN_ARR_H_
#include "common.h"
+#include "mem.h"
+
+struct ty_list_header {
+ size_t cap;
+ size_t len;
+};
#define TY_LIST_MIN_CAP 8
#define TY_LIST_GROW_RATE 2
-#define ty_list_create(T, init_capacity) \
- ((T*)_ty_list_create(sizeof(T), init_capacity))
-
-#define ty_list_len_ptr(arr) ((int*)arr - 2)
-#define ty_list_len(arr) (*ty_list_len_ptr(arr))
+#define ty_list_get_header(arr) ((struct ty_list_header*)arr - 1)
+#define ty_list_cap(arr) (ty_list_get_header(arr)->cap)
+#define ty_list_len(arr) (ty_list_get_header(arr)->len)
-#define ty_list_cap_ptr(arr) ((int*)arr - 1)
-#define ty_list_cap(arr) (*ty_list_cap_ptr(arr))
-#define ty_list_base(arr) ((void*)ty_list_len_ptr(arr))
+#define ty_list_reserve(arr, amt) \
+ do { \
+ struct ty_list_header *header = ty_list_get_header(arr); \
+ if (amt > header->cap) { \
+ header->cap = header->cap < TY_LIST_MIN_CAP \
+ ? TY_LIST_MIN_CAP \
+ : header->cap * TY_LIST_GROW_RATE \
+ header = ty_realloc( \
+ header, \
+ (sizeof(*(arr)) * count) + sizeof(struct ty_list_header) \
+ ); \
+ (arr) = (void*)(header + 1); \
+ } \
+ } while (0)
-#define ty_list_append_slot(T, arr) \
- ((T*)_ty_list_append_slot(sizeof(T), (void**)arr))
-
-#define ty_list_append(T, arr, elem) \
- (*ty_list_append_slot(T, arr) = elem)
+#define ty_list_append(arr, elem) \
+ do { \
+ struct ty_list_header *header = ty_list_get_header(arr); \
+ ty_list_reserve(arr, header->len + 1); \
+ (arr)[header->len++] = (elem); \
+ } while (0)
#define ty_list_clear(arr) \
- (*ty_list_len_ptr(arr) = 0)
+ (ty_list_get_header(arr)->len = 0)
+
+#define ty_list_free(arr) (ty_free(ty_list_get_header(arr)))
-void *_ty_list_create(size_t type_size, int init_cap);
-void *_ty_list_append_slot(size_t type_size, void **arr);
-void ty_list_free(void *arr);
+void *ty_list_create(void);
#endif // DYN_ARR_H_