From 0722bcf51b7cc32f6338a2c05e833dfebbf305bf Mon Sep 17 00:00:00 2001 From: cannoli-fruit Date: Wed, 15 Jul 2026 23:58:18 -0400 Subject: [PATCH] Implemented filling dynamics arrays with normal array data --- examples/da_from_array.c | 16 ++++++++++++++++ lazy.h | 11 +++++++++++ 2 files changed, 27 insertions(+) create mode 100644 examples/da_from_array.c diff --git a/examples/da_from_array.c b/examples/da_from_array.c new file mode 100644 index 0000000..1e360e8 --- /dev/null +++ b/examples/da_from_array.c @@ -0,0 +1,16 @@ +#include +#include +#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; +} diff --git a/lazy.h b/lazy.h index 61ec9c3..a6eb099 100644 --- a/lazy.h +++ b/lazy.h @@ -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;