Implemented filling dynamics arrays with normal array data

This commit is contained in:
cannoli-fruit 2026-07-15 23:58:18 -04:00
commit 0722bcf51b
2 changed files with 27 additions and 0 deletions

16
examples/da_from_array.c Normal file
View file

@ -0,0 +1,16 @@
#include <stdio.h>
#include <stddef.h>
#define LAZY_IMPL
#include "../lazy.h"
int main() {
float xs[] = {1, 2, 3, 4};
Lz_DA(float) nums = {0};
lz_da_fill_from_array(&nums, xs);
for (size_t i = 0; i < nums.cnt; ++i) {
printf("%f\n", nums.data[i]);
}
return 0;
}

11
lazy.h
View file

@ -130,6 +130,17 @@ long long lz_slc_atoll(Lz_Slc *src);
for (T *name = (da).data; name-(da).data < (da).cnt; ++name)
#endif
#ifndef lz_da_fill_from_array
#define lz_da_fill_from_array(p_da, arr) do {\
free((p_da)->data);\
size_t cnt = sizeof(arr)/sizeof(*arr);\
(p_da)->cap = cnt;\
(p_da)->cnt = cnt;\
(p_da)->data = malloc(sizeof(arr));\
memcpy((p_da)->data, arr, sizeof(arr));\
} while(0)
#endif
#ifndef LZ_SB_DEFINITION
#define LZ_SB_DEFINITION
typedef Lz_DA(char) Lz_SB;