From 853bbb3752a5fa2f58ef456ffb6e3a552e13cb11 Mon Sep 17 00:00:00 2001 From: Aldrik Ramaekers Date: Sun, 3 Aug 2025 19:22:36 +0200 Subject: initial commit --- simclist-1.5/examples/ex3.c | 58 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 simclist-1.5/examples/ex3.c (limited to 'simclist-1.5/examples/ex3.c') diff --git a/simclist-1.5/examples/ex3.c b/simclist-1.5/examples/ex3.c new file mode 100644 index 0000000..74d3091 --- /dev/null +++ b/simclist-1.5/examples/ex3.c @@ -0,0 +1,58 @@ +#include + +#include + +typedef struct { + int x, y; +} point2D; + +typedef struct { + point2D a, b, c, d; +} rectangle; /* custom data type to store in list */ + +/* this function returns the size of elements */ +size_t mymeter(const void *el) { + /* every element has the constant size of a rectangle structure */ + return sizeof(rectangle); +} + +/* + * compare rectangles by area + * + * this function compares two elements: + * <0: a greater than b + * 0: a equivalent to b + * >0: b greater than a + */ +int mycomparator(const void *a, const void *b) { + /* compare areas */ + const rectangle *A = (rectangle *) a; + const rectangle *B = (rectangle *) b; + unsigned int areaA, areaB; + areaA = ((A->c.y - A->b.y) * (A->b.x - A->a.x)); + areaB = ((B->c.y - B->b.y) * (B->b.x - B->a.x)); + return (areaA < areaB) - (areaA > areaB); +} + +int main() { + rectangle rect; + list_t l; + + list_init(&l); + + /* setting the custom spanning function */ + list_attributes_copy(&l, mymeter, 1); + + /* acquire rectangles and insert in list ... */ + + /* setting the custom area comparator */ + list_attributes_comparator(&l, mycomparator); + list_sort(&l, -1); /* sorting by area (descending) */ + + /* [display list ...] */ + + list_destroy(&l); + + return 0; +} + -- cgit v1.2.3-70-g09d2