summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/new_smap.c
diff options
context:
space:
mode:
authorTyge Lovset <[email protected]>2022-12-20 23:31:51 +0100
committerTyge Lovset <[email protected]>2022-12-20 23:31:51 +0100
commit5f57d597cd27aef55adbcb3b452973b0c6e33667 (patch)
treedfd59c2fd0e36a6ef37912a9d0cc5a65970f1524 /misc/examples/new_smap.c
parent1763be8c8cbbc0896477fcf924edd4180d1345a9 (diff)
downloadSTC-modified-5f57d597cd27aef55adbcb3b452973b0c6e33667.tar.gz
STC-modified-5f57d597cd27aef55adbcb3b452973b0c6e33667.zip
Restructured folders: examples, benchmarks, tests into misc folder.
Diffstat (limited to 'misc/examples/new_smap.c')
-rw-r--r--misc/examples/new_smap.c77
1 files changed, 77 insertions, 0 deletions
diff --git a/misc/examples/new_smap.c b/misc/examples/new_smap.c
new file mode 100644
index 00000000..c77aa185
--- /dev/null
+++ b/misc/examples/new_smap.c
@@ -0,0 +1,77 @@
+#include <stc/cstr.h>
+#include <stc/forward.h>
+
+forward_csmap(PMap, struct Point, int);
+
+// Use forward declared PMap in struct
+struct MyStruct {
+ PMap pntmap;
+ cstr name;
+} typedef MyStruct;
+
+// int => int map
+#define i_key int
+#define i_val int
+#include <stc/csmap.h>
+
+// Point => int map
+struct Point { int x, y; } typedef Point;
+int point_cmp(const Point* a, const Point* b) {
+ int c = a->x - b->x;
+ return c ? c : a->y - b->y;
+}
+
+#define i_type PMap
+#define i_key Point
+#define i_val int
+#define i_cmp point_cmp
+#define i_opt c_is_forward
+#include <stc/csmap.h>
+
+// cstr => cstr map
+#define i_type SMap
+#define i_key_str
+#define i_val_str
+#include <stc/csmap.h>
+
+// cstr set
+#define i_type SSet
+#define i_key_str
+#include <stc/csset.h>
+
+
+int main()
+{
+ c_auto (csmap_int, imap) {
+ csmap_int_insert(&imap, 123, 321);
+ }
+
+ c_auto (PMap, pmap) {
+ c_forlist (i, PMap_value, {
+ {{42, 14}, 1},
+ {{32, 94}, 2},
+ {{62, 81}, 3},
+ }) PMap_insert(&pmap, c_PAIR(i.ref));
+
+ c_forpair (p, i, PMap, pmap)
+ printf(" (%d,%d: %d)", _.p->x, _.p->y, *_.i);
+ puts("");
+ }
+
+ c_auto (SMap, smap) {
+ c_forlist (i, SMap_raw, {
+ {"Hello, friend", "this is the mapped value"},
+ {"The brown fox", "jumped"},
+ {"This is the time", "for all good things"},
+ }) SMap_emplace(&smap, c_PAIR(i.ref));
+
+ c_forpair (i, j, SMap, smap)
+ printf(" (%s: %s)\n", cstr_str(_.i), cstr_str(_.j));
+ }
+
+ c_auto (SSet, sset) {
+ SSet_emplace(&sset, "Hello, friend");
+ SSet_emplace(&sset, "Goodbye, foe");
+ printf("Found? %s\n", SSet_contains(&sset, "Hello, friend") ? "true" : "false");
+ }
+}