From 432f24319319fe040e142059eb83279c53f90ab8 Mon Sep 17 00:00:00 2001 From: Aldrik Ramaekers Date: Sat, 9 Aug 2025 08:35:03 +0200 Subject: refactor 2 --- libs/simclist-1.5/examples/._ex1.c | Bin 0 -> 229 bytes libs/simclist-1.5/examples/._ex2.c | Bin 0 -> 229 bytes libs/simclist-1.5/examples/._ex3.c | Bin 0 -> 229 bytes libs/simclist-1.5/examples/ex1.c | 28 ++++++++++++++++++ libs/simclist-1.5/examples/ex2.c | 38 ++++++++++++++++++++++++ libs/simclist-1.5/examples/ex3.c | 58 +++++++++++++++++++++++++++++++++++++ 6 files changed, 124 insertions(+) create mode 100644 libs/simclist-1.5/examples/._ex1.c create mode 100644 libs/simclist-1.5/examples/._ex2.c create mode 100644 libs/simclist-1.5/examples/._ex3.c create mode 100644 libs/simclist-1.5/examples/ex1.c create mode 100644 libs/simclist-1.5/examples/ex2.c create mode 100644 libs/simclist-1.5/examples/ex3.c (limited to 'libs/simclist-1.5/examples') diff --git a/libs/simclist-1.5/examples/._ex1.c b/libs/simclist-1.5/examples/._ex1.c new file mode 100644 index 0000000..3a559d8 Binary files /dev/null and b/libs/simclist-1.5/examples/._ex1.c differ diff --git a/libs/simclist-1.5/examples/._ex2.c b/libs/simclist-1.5/examples/._ex2.c new file mode 100644 index 0000000..536216c Binary files /dev/null and b/libs/simclist-1.5/examples/._ex2.c differ diff --git a/libs/simclist-1.5/examples/._ex3.c b/libs/simclist-1.5/examples/._ex3.c new file mode 100644 index 0000000..89542cf Binary files /dev/null and b/libs/simclist-1.5/examples/._ex3.c differ diff --git a/libs/simclist-1.5/examples/ex1.c b/libs/simclist-1.5/examples/ex1.c new file mode 100644 index 0000000..13c67c3 --- /dev/null +++ b/libs/simclist-1.5/examples/ex1.c @@ -0,0 +1,28 @@ +#include + +#include /* use the SimCList library */ + + +int main() { + list_t mylist; /* declare a list */ + int userval; + + + list_init(& mylist); /* initialize the list */ + + printf("Insert your number: "); + scanf("%d", & userval); + + list_append(& mylist, & userval); /* add an element to the list */ + + printf("The list now holds %u elements.\n", \ + list_size(& mylist)); /* get the size of the list */ + + printf("Your number was: %d\n", \ + * (int*)list_get_at(& mylist, 0)); /* extract the first element of the list */ + + list_destroy(&mylist); + + return 0; +} + diff --git a/libs/simclist-1.5/examples/ex2.c b/libs/simclist-1.5/examples/ex2.c new file mode 100644 index 0000000..3fc2c5e --- /dev/null +++ b/libs/simclist-1.5/examples/ex2.c @@ -0,0 +1,38 @@ +#include + +#include + +int main() { + int val; + list_t l; + + list_init(&l); + + /* request to store copies, and provide the metric function */ + list_attributes_copy(&l, list_meter_int32_t, 1); + + printf("Give numbers. Terminate with one negative.\n"); + scanf("%d", &val); + while (val > 0) { + list_append(&l, &val); + scanf("%d", &val); + } + + /* setting the comparator, so the list can sort, find the min, max etc */ + list_attributes_comparator(&l, list_comparator_int32_t); + list_sort(&l, -1); /* sorting the list in descending (-1) order */ + + /* printing out the result */ + printf("Sorted values:\n"); + + list_iterator_start(&l); /* starting an iteration "session" */ + while (list_iterator_hasnext(&l)) { /* tell whether more values available */ + printf("%d\n", *(int *)list_iterator_next(&l)); /* get the next value */ + } + list_iterator_stop(&l); /* starting the iteration "session" */ + + list_destroy(&l); + + return 0; +} + diff --git a/libs/simclist-1.5/examples/ex3.c b/libs/simclist-1.5/examples/ex3.c new file mode 100644 index 0000000..74d3091 --- /dev/null +++ b/libs/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