Added foreach macro for dynamic arrays

This commit is contained in:
cannoli-fruit 2026-07-12 20:40:24 -04:00
commit 9c7147354a
2 changed files with 19 additions and 0 deletions

17
examples/da_foreach.c Normal file
View file

@ -0,0 +1,17 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define LAZY_IMPL
#include "../lazy.h"
int main() {
srand(time(NULL));
Lz_DA(int) nums = {0};
for (int i = 0; i < 10; ++i) {
lz_da_append(nums, rand()%100);
}
lz_da_foreach(int, x, nums) {
printf("%d\n", *x);
}
}

2
lazy.h
View file

@ -87,6 +87,8 @@ char *lz_cstr_dup(const char *s);
(da).data = realloc((da).data, (da).cnt * sizeof(*(da).data));\
(da).cap = (da).cnt;\
} while(0)
#define lz_da_foreach(T, name, da) \
for (T *name = (da).data; name-(da).data < (da).cnt; ++name)
typedef Lz_DA(char) Lz_SB;