summaryrefslogtreecommitdiff
path: root/examples
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
parent9325ee9ae425d79c3c8d35fd706cdde03c0555c2 (diff)
Add sets
Diffstat (limited to 'examples')
-rw-r--r--examples/Makefile5
-rw-r--r--examples/gehashmap.c14
-rw-r--r--examples/geset.c73
3 files changed, 78 insertions, 14 deletions
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 <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -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 <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);
+}