From b2df541c5d0dc00368754fd35e0af2341d1458eb Mon Sep 17 00:00:00 2001 From: justanothercatgirl Date: Wed, 19 Jun 2024 14:17:30 +0300 Subject: moved all containers to a single header "container.h" TODO: add linked list, hash map and other shit like this to the header --- tests/dynarray_struct.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 tests/dynarray_struct.c (limited to 'tests/dynarray_struct.c') diff --git a/tests/dynarray_struct.c b/tests/dynarray_struct.c new file mode 100644 index 0000000..6cce97c --- /dev/null +++ b/tests/dynarray_struct.c @@ -0,0 +1,40 @@ + +#define CONTAINER_IMPLEMENTATION +#include "../include/container.h" + +#include + +struct point { + float x, y, z, w; +}; + +void print_point(struct point* p) { + printf("{ .x=%.2f, .y=%.2f, .z=%.2f, .w=%.2f };\n", p->x, p->y, p->z, p->w); +} + +void print_array(struct point* p) { + for (size_t i = 0; i < array_size(p); ++i) { + printf("%zu: ", i); + print_point(p+i); + } +} + +int point_abs(const void* a, const void* b) { + struct point* c = (struct point*) a; + struct point* d = (struct point*) b; + float absa = c->x * c->x + c->y * c->y + c->z * c->z + c->w * c->w; + float absb = d->x * d->x + d->y * d->y + d->z * d->z + d->w * d->w; + return absa-absb; +} + +int main() +{ + struct point *points = array_new(struct point, 4); + for (size_t i = 0; i < array_size(points); ++i) points[i] = (struct point){.x = i, .y = 2 * i, .z = 4 * i, .w = 8 * i}; + array_push(points, ((struct point){.0f, 2.5f, 3.0, 4.0}) ); + array_insert(points, ((struct point){0}), 3); + array_qsort(points, point_abs); + print_array(points); + array_free(points); + return 0; +} -- cgit v1.2.3-70-g09d2