From 6337f29dd5310fd3c471ee1261077e755c3c2c39 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Mon, 2 Jan 2023 00:06:15 +0100 Subject: Add sets --- examples/Makefile | 5 ++-- examples/gehashmap.c | 14 ++-------- examples/geset.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 14 deletions(-) create mode 100644 examples/geset.c (limited to 'examples') diff --git a/examples/Makefile b/examples/Makefile index c6c5643..e86f074 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -2,14 +2,15 @@ CC = cc CFLAGS = -Wall -Wextra -Wpedantic -Werror \ - -Og -g -ggdb + -Og -g -ggdb -std=c99 LDFLAGS = -I../src -progs = gehashmap gevector +progs = gehashmap geset gevector all: ${progs} gehashmap: gehashmap.c +geset: geset.c gevector: gevector.c clean: diff --git a/examples/gehashmap.c b/examples/gehashmap.c index a29ef14..85c007f 100644 --- a/examples/gehashmap.c +++ b/examples/gehashmap.c @@ -1,3 +1,5 @@ +#define _XOPEN_SOURCE 500 + #include #include #include @@ -19,18 +21,6 @@ itoumap_key_iseq(int a, int b) return a == b; } -void -atoimap_key_free(char *s) -{ - free(s); -} - -void -itoumap_key_free(int a) -{ - (void) a; -} - size_t atoimap_key_hash(char *s) { diff --git a/examples/geset.c b/examples/geset.c new file mode 100644 index 0000000..e4514cd --- /dev/null +++ b/examples/geset.c @@ -0,0 +1,73 @@ +#include +#include + +#include + +GESET_DEF(size_t, sizeset) + +bool +sizeset_elem_iseq(size_t a, size_t b) +{ + return a == b; +} + +size_t +sizeset_elem_hash(size_t n) +{ + return n; +} + +int +main(void) +{ + size_t i; + sizeset_t *set; + + sizeset_new(set = malloc(sizeof(sizeset_t)), 0, 0); + + if (sizeset_has(set, 69)) { + puts("Something went wrong..."); + exit(EXIT_FAILURE); + } + + sizeset_add(set, 69); + sizeset_add(set, 69); + sizeset_add(set, 69); + sizeset_add(set, 69); + + if (!sizeset_has(set, 69)) { + puts("Something went wrong..."); + exit(EXIT_FAILURE); + } + + sizeset_add(set, 42); + sizeset_add(set, 420); + sizeset_remove(set, 42); + + GESET_FOREACH(sizeset, size_t, n, *set) { + GESET_FOREACH(sizeset, size_t, m, *set) + printf("Set -> %zu, %zu\n", n, m); + } + + sizeset_add(set, 1); + sizeset_add(set, 2); + sizeset_add(set, 3); + sizeset_add(set, 4); + sizeset_add(set, 5); + sizeset_add(set, 6); + sizeset_add(set, 7); + sizeset_add(set, 8); + sizeset_add(set, 9); + + i = 0; + GESET_FOREACH(sizeset, size_t, n, *set) + printf("Set[%zu] -> %zu\n", i++, n); + + printf("Set Size -> %zu\n", sizeset_size(set)); + + GESET_FOREACH_SAFE(sizeset, size_t, n, *set) + sizeset_remove(set, n); + + sizeset_free(set); + free(set); +} -- cgit v1.2.3