update array names to be shorter
This commit is contained in:
@@ -119,38 +119,42 @@ typedef struct dyn_array_create_func_args {
|
||||
int line;
|
||||
} dyn_array_create_func_args_t;
|
||||
void *dyn_array_create_func(dyn_array_create_func_args_t args);
|
||||
#define dyn_array_create(ALLOCATOR, TYPE, ...) \
|
||||
((TYPE *)dyn_array_create_func( \
|
||||
(dyn_array_create_func_args_t){.allocator = ALLOCATOR, \
|
||||
|
||||
#define make_arr(ALLOCATOR, TYPE, ...) ((TYPE *)dyn_array_create_func((dyn_array_create_func_args_t){ \
|
||||
.allocator = ALLOCATOR, \
|
||||
.itemsize = sizeof(TYPE), \
|
||||
.file = __FILE__, \
|
||||
.line = __LINE__, \
|
||||
__VA_ARGS__}))
|
||||
__VA_ARGS__ \
|
||||
}))
|
||||
|
||||
// Always reassign the array. if multiple variables reference the same growing
|
||||
// array, then you should be using pointer pointers.
|
||||
void *dyn_array_grow_func(void *this, size_t n_new_items, const char *file, int line);
|
||||
void dyn_array_shrink_func(void *this, size_t n_items_to_remove, const char *file, int line);
|
||||
|
||||
#define dyn_array_grow(THIS, N_NEW_ITEMS) \
|
||||
#define arr_grow(THIS, N_NEW_ITEMS) \
|
||||
dyn_array_grow_func(THIS, N_NEW_ITEMS, __FILE__, __LINE__)
|
||||
|
||||
#define dyn_array_shrink(THIS, N_ITEMS_TO_REMOVE) \
|
||||
#define arr_shrink(THIS, N_ITEMS_TO_REMOVE) \
|
||||
dyn_array_shrink_func(THIS, N_ITEMS_TO_REMOVE, __FILE__, __LINE__)
|
||||
|
||||
#define dyn_array_reset(THIS) \
|
||||
#define arr_reset(THIS)\
|
||||
do {\
|
||||
dyn_array_shrink(THIS, dyn_array_length(THIS)); \
|
||||
dyn_array_header_t *header = PTR_FROM_FIELD_PTR(dyn_array_header_t, bytes, THIS);\
|
||||
header->size = 0;\
|
||||
} while (0)
|
||||
|
||||
size_t dyn_array_length(void *this);
|
||||
size_t dyn_array_capacity(void *this);
|
||||
size_t arr_len(void *this);
|
||||
size_t arr_cap(void *this);
|
||||
|
||||
#define dyn_array_append(THIS, VAL) do { \
|
||||
THIS = dyn_array_grow(THIS, 1); \
|
||||
THIS[dyn_array_length(THIS)-1] = VAL; \
|
||||
#define arr_append(THIS, VAL) do { \
|
||||
THIS = arr_grow(THIS, 1); \
|
||||
THIS[arr_len(THIS)-1] = VAL; \
|
||||
} while(0)
|
||||
|
||||
#define dyn_array_pop(THIS) (dyn_array_shrink(THIS, 1), THIS[dyn_array_length(THIS)])
|
||||
|
||||
#define arr_pop(THIS) (arr_shrink(THIS, 1), THIS[arr_len(THIS)])
|
||||
|
||||
// CLI /////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
+2
-2
@@ -70,7 +70,7 @@ static inline void *todo_remove_grow_func(void *this, size_t n_new_items, const
|
||||
}
|
||||
|
||||
void dyn_array_shrink_func(void *this, size_t n_items_to_remove, const char *file, int line) {
|
||||
if (dyn_array_length(this) < n_items_to_remove) {
|
||||
if (arr_len(this) < n_items_to_remove) {
|
||||
fprintf(
|
||||
stderr,
|
||||
"%s:%d: tried to remove more items than contained in dynamic array.\n",
|
||||
@@ -83,7 +83,7 @@ void dyn_array_shrink_func(void *this, size_t n_items_to_remove, const char *fil
|
||||
header->size -= n_items_to_remove;
|
||||
}
|
||||
|
||||
size_t dyn_array_length(void *this) {
|
||||
size_t arr_len(void *this) {
|
||||
if (this == NULL) { return 0; }
|
||||
dyn_array_header_t *header = PTR_FROM_FIELD_PTR(dyn_array_header_t, bytes, this);
|
||||
return header->size;
|
||||
|
||||
+29
-29
@@ -6,19 +6,19 @@
|
||||
|
||||
Test(dynamic_arrays, append) {
|
||||
with_borrow(alloc) {
|
||||
int *numbers = dyn_array_create(alloc, int);
|
||||
dyn_array_append(numbers, 40);
|
||||
dyn_array_append(numbers, 41);
|
||||
dyn_array_append(numbers, 42);
|
||||
dyn_array_append(numbers, 43);
|
||||
dyn_array_append(numbers, 44);
|
||||
dyn_array_append(numbers, 45);
|
||||
dyn_array_append(numbers, 46);
|
||||
dyn_array_append(numbers, 47);
|
||||
dyn_array_append(numbers, 48);
|
||||
dyn_array_append(numbers, 49);
|
||||
dyn_array_append(numbers, 50);
|
||||
for (int i = 0; i < (int)dyn_array_length(numbers); i++) {
|
||||
int *numbers = make_arr(alloc, int);
|
||||
arr_append(numbers, 40);
|
||||
arr_append(numbers, 41);
|
||||
arr_append(numbers, 42);
|
||||
arr_append(numbers, 43);
|
||||
arr_append(numbers, 44);
|
||||
arr_append(numbers, 45);
|
||||
arr_append(numbers, 46);
|
||||
arr_append(numbers, 47);
|
||||
arr_append(numbers, 48);
|
||||
arr_append(numbers, 49);
|
||||
arr_append(numbers, 50);
|
||||
for (int i = 0; i < (int)arr_len(numbers); i++) {
|
||||
cr_assert_eq(numbers[i], i+40);
|
||||
}
|
||||
}
|
||||
@@ -26,26 +26,26 @@ Test(dynamic_arrays, append) {
|
||||
|
||||
Test(dynamic_arrays, pop) {
|
||||
with_borrow(alloc) {
|
||||
int *numbers = dyn_array_create(alloc, int);
|
||||
dyn_array_append(numbers, 40);
|
||||
dyn_array_append(numbers, 41);
|
||||
dyn_array_append(numbers, 42);
|
||||
dyn_array_append(numbers, 43);
|
||||
dyn_array_append(numbers, 44);
|
||||
dyn_array_append(numbers, 45);
|
||||
dyn_array_append(numbers, 46);
|
||||
dyn_array_append(numbers, 47);
|
||||
dyn_array_append(numbers, 48);
|
||||
dyn_array_append(numbers, 49);
|
||||
dyn_array_append(numbers, 50);
|
||||
cr_assert_eq(dyn_array_length(numbers), 11);
|
||||
int *numbers = make_arr(alloc, int);
|
||||
arr_append(numbers, 40);
|
||||
arr_append(numbers, 41);
|
||||
arr_append(numbers, 42);
|
||||
arr_append(numbers, 43);
|
||||
arr_append(numbers, 44);
|
||||
arr_append(numbers, 45);
|
||||
arr_append(numbers, 46);
|
||||
arr_append(numbers, 47);
|
||||
arr_append(numbers, 48);
|
||||
arr_append(numbers, 49);
|
||||
arr_append(numbers, 50);
|
||||
cr_assert_eq(arr_len(numbers), 11);
|
||||
// NOTE: you can stack for loops to have scoped variables you can abuse
|
||||
// in macros. e.g.
|
||||
// for (TYPE UNIQUE = (int)dyn_array_length(numbers); UNIQUE != 0; UNIQUE = 0;)
|
||||
// for (TYPE UNIQUE = (int)arr_len(numbers); UNIQUE != 0; UNIQUE = 0;)
|
||||
// for (int i = 0, i < UNIQUE; i++)
|
||||
int len = (int)dyn_array_length(numbers);
|
||||
int len = (int)arr_len(numbers);
|
||||
for (int i = 0; i < len; i++) {
|
||||
int num = dyn_array_pop(numbers);
|
||||
int num = arr_pop(numbers);
|
||||
cr_assert_eq(num, 50-i);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user