Added foreach macro for dynamic arrays
This commit is contained in:
parent
611f5d5b5a
commit
9c7147354a
2 changed files with 19 additions and 0 deletions
17
examples/da_foreach.c
Normal file
17
examples/da_foreach.c
Normal 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
2
lazy.h
|
|
@ -87,6 +87,8 @@ char *lz_cstr_dup(const char *s);
|
||||||
(da).data = realloc((da).data, (da).cnt * sizeof(*(da).data));\
|
(da).data = realloc((da).data, (da).cnt * sizeof(*(da).data));\
|
||||||
(da).cap = (da).cnt;\
|
(da).cap = (da).cnt;\
|
||||||
} while(0)
|
} 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;
|
typedef Lz_DA(char) Lz_SB;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue