diff options
author | justanothercatgirl <sotov2070@gmail.com> | 2024-06-19 14:17:30 +0300 |
---|---|---|
committer | justanothercatgirl <sotov2070@gmail.com> | 2024-06-19 14:17:30 +0300 |
commit | b2df541c5d0dc00368754fd35e0af2341d1458eb (patch) | |
tree | e657b490c7b97814120026f1112fa1a7265d5a10 /tests/dynarray_struct.c | |
parent | dff89dcae590315c6916bc24407f9c80dd74f049 (diff) |
moved all containers to a single header "container.h"
TODO: add linked list, hash map and other shit like this to the header
Diffstat (limited to 'tests/dynarray_struct.c')
-rw-r--r-- | tests/dynarray_struct.c | 40 |
1 files changed, 40 insertions, 0 deletions
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 <stdio.h> + +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; +} |