summaryrefslogtreecommitdiff
path: root/examples/geset.c
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2023-01-02 00:06:15 +0100
committerThomas Voss <mail@thomasvoss.com> 2023-01-02 00:06:15 +0100
commit6337f29dd5310fd3c471ee1261077e755c3c2c39 (patch)
treef78160e3ee129b1f109a5ae02ce9ed730428d65a /examples/geset.c
parent9325ee9ae425d79c3c8d35fd706cdde03c0555c2 (diff)
Add sets
Diffstat (limited to 'examples/geset.c')
-rw-r--r--examples/geset.c73
1 files changed, 73 insertions, 0 deletions
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 <stdio.h>
+#include <stdlib.h>
+
+#include <geset.h>
+
+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);
+}