summaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2021-09-08 22:27:45 +0200
committerTyge Løvset <[email protected]>2021-09-08 22:27:45 +0200
commit26152f06399506bf5bca823f42ed3089ce404aa8 (patch)
treea2b282fc0d651298ed8ff5f551c0506d8cda4062 /examples
parentc86e3d30299baea44f98f153c4b01ea5e244feaa (diff)
downloadSTC-modified-26152f06399506bf5bca823f42ed3089ce404aa8.tar.gz
STC-modified-26152f06399506bf5bca823f42ed3089ce404aa8.zip
Updated rest of examples, except cqueue.c
Diffstat (limited to 'examples')
-rw-r--r--examples/csmap_find.c15
-rw-r--r--examples/list_erase.c7
-rw-r--r--examples/mapmap.c40
-rw-r--r--examples/phonebook.c16
-rw-r--r--examples/read.c4
-rw-r--r--examples/splitstr.c4
-rw-r--r--examples/stc_astar.c30
-rw-r--r--examples/words.c41
8 files changed, 90 insertions, 67 deletions
diff --git a/examples/csmap_find.c b/examples/csmap_find.c
index 46ae01fa..79a0f993 100644
--- a/examples/csmap_find.c
+++ b/examples/csmap_find.c
@@ -1,12 +1,17 @@
// This implements the c++ std::map::find example at:
// https://docs.microsoft.com/en-us/cpp/standard-library/map-class?view=msvc-160#example-17
-#include <stc/csmap.h>
-#include <stc/cvec.h>
#include <stc/cstr.h>
#include <stdio.h>
-using_csmap_strval(istr, int);
-using_cvec(istr, csmap_istr_rawvalue_t, c_no_compare);
+#define i_tag istr
+#define i_key int
+#define i_val_str
+#include <stc/csmap.h>
+
+#define i_tag istr
+#define i_val csmap_istr_rawvalue_t
+#define i_cmp c_no_compare
+#include <stc/cvec.h>
void print_elem(csmap_istr_rawvalue_t p) {
printf("(%d, %s) ", p.first, p.second);
@@ -56,7 +61,7 @@ int main()
puts("Inserting the following vector data into m1:");
print_collection_cvec_istr(v);
-
+
c_foreach (i, cvec_istr, v) csmap_istr_emplace(&m1, i.ref->first, i.ref->second);
puts("The modified map m1 is (key, value):");
diff --git a/examples/list_erase.c b/examples/list_erase.c
index 523ee0b8..6d619b9d 100644
--- a/examples/list_erase.c
+++ b/examples/list_erase.c
@@ -1,8 +1,9 @@
// erasing from clist
-#include <stc/clist.h>
#include <stdio.h>
-using_clist(i, int);
+#define i_tag i
+#define i_val int
+#include <stc/clist.h>
int main ()
{
@@ -11,7 +12,7 @@ int main ()
c_emplace(clist_i, L, {10, 20, 30, 40, 50});
// 10 20 30 40 50
clist_i_iter_t it = clist_i_begin(&L); // ^
- clist_i_next(&it);
+ clist_i_next(&it);
it = clist_i_erase_at(&L, it); // 10 30 40 50
// ^
clist_i_iter_t end = clist_i_end(&L); //
diff --git a/examples/mapmap.c b/examples/mapmap.c
index e582ab64..b7dd8afb 100644
--- a/examples/mapmap.c
+++ b/examples/mapmap.c
@@ -1,24 +1,36 @@
#include <stdio.h>
-#include <stc/cmap.h>
#include <stc/cstr.h>
-// unordered_map<string, unordered_map<string, string>>:
+// unordered_map<string, unordered_map<string, string>>:
+
+#define i_key_str
+#define i_val_str
+#include <stc/cmap.h>
-using_cmap_str();
-using_cmap_strkey(cfg, cmap_str, cmap_str_del, c_no_clone);
+#define i_tag cfg
+#define i_key_str
+#define i_val cmap_str
+#define i_valdel cmap_str_del
+#include <stc/cmap.h>
-int main(void) {
- c_forvar (cmap_cfg cfg = cmap_cfg_init(), cmap_cfg_del(&cfg)) {
+int main(void)
+{
+ c_forauto (cmap_cfg, cfg)
+ {
cmap_str init = cmap_str_init();
- cmap_str_emplace(&cmap_cfg_insert(&cfg, cstr_from("user"), init).ref->second, "name", "Joe");
- cmap_str_emplace(&cmap_cfg_insert(&cfg, cstr_from("user"), init).ref->second, "groups", "proj1,proj3");
- cmap_str_emplace(&cmap_cfg_insert(&cfg, cstr_from("group"), init).ref->second, "proj1", "Energy");
- cmap_str_emplace(&cmap_cfg_insert(&cfg, cstr_from("group"), init).ref->second, "proj2", "Windy");
- cmap_str_emplace(&cmap_cfg_insert(&cfg, cstr_from("group"), init).ref->second, "proj3", "Oil");
- cmap_str_emplace(&cmap_cfg_insert(&cfg, cstr_from("admin"), init).ref->second, "employees", "2302");
-
- cmap_str_emplace_or_assign(&cmap_cfg_insert(&cfg, cstr_from("group"), init).ref->second, "proj2", "Wind"); // Update
+ cmap_cfg_insert(&cfg, cstr_from("user"), init);
+ cmap_cfg_insert(&cfg, cstr_from("group"), init);
+ cmap_cfg_insert(&cfg, cstr_from("admin"), init);
+
+ cmap_str_emplace(cmap_cfg_at(&cfg, "user"), "name", "Joe");
+ cmap_str_emplace(cmap_cfg_at(&cfg, "user"), "groups", "proj1,proj3");
+ cmap_str_emplace(cmap_cfg_at(&cfg, "group"), "proj1", "Energy");
+ cmap_str_emplace(cmap_cfg_at(&cfg, "group"), "proj2", "Windy");
+ cmap_str_emplace(cmap_cfg_at(&cfg, "group"), "proj3", "Oil");
+ cmap_str_emplace(cmap_cfg_at(&cfg, "admin"), "employees", "2302");
+
+ cmap_str_emplace_or_assign(cmap_cfg_at(&cfg, "group"), "proj2", "Wind"); // Update
c_foreach (i, cmap_cfg, cfg)
c_foreach (j, cmap_str, i.ref->second)
diff --git a/examples/phonebook.c b/examples/phonebook.c
index a69e701b..a4e2e0f2 100644
--- a/examples/phonebook.c
+++ b/examples/phonebook.c
@@ -22,12 +22,14 @@
// Program to emulates the phone book.
#include <stdio.h>
-#include <stc/cset.h>
-#include <stc/cmap.h>
#include <stc/cstr.h>
-using_cmap_str();
-using_cset_str();
+#define i_key_str
+#define i_val_str
+#include <stc/cmap.h>
+
+#define i_key_str
+#include <stc/cset.h>
void print_phone_book(cmap_str phone_book)
{
@@ -39,14 +41,14 @@ int main(int argc, char **argv)
{
c_static_assert(3 == 3, "hello");
- c_forvar (cset_str names = cset_str_init(), cset_str_del(&names)) {
+ c_forauto (cset_str, names) {
c_emplace (cset_str, names, {"Hello", "Cool", "True"});
c_foreach (i, cset_str, names) printf("%s ", i.ref->str);
puts("");
}
bool erased;
- c_forvar (cmap_str phone_book = cmap_str_init(), cmap_str_del(&phone_book)) {
+ c_forauto (cmap_str, phone_book) {
c_emplace (cmap_str, phone_book, {
{"Lilia Friedman", "(892) 670-4739"},
{"Tariq Beltran", "(489) 600-7575"},
@@ -63,7 +65,7 @@ int main(int argc, char **argv)
printf("\nPhone book after adding Zak Byers:\n");
print_phone_book(phone_book);
- if (cmap_str_find(&phone_book, "Tariq Beltran").ref != NULL)
+ if (cmap_str_contains(&phone_book, "Tariq Beltran"))
printf("\nTariq Beltran is in phone book\n");
erased = cmap_str_erase(&phone_book, "Tariq Beltran");
diff --git a/examples/read.c b/examples/read.c
index 2324d1df..bb0e139f 100644
--- a/examples/read.c
+++ b/examples/read.c
@@ -1,8 +1,8 @@
#include <errno.h>
#include <stc/cstr.h>
-#include <stc/cvec.h>
-using_cvec_str();
+#define i_val_str
+#include <stc/cvec.h>
cvec_str read_file(const char* name) {
cvec_str vec = cvec_str_init();
diff --git a/examples/splitstr.c b/examples/splitstr.c
index 54bbe92d..42dd4dbd 100644
--- a/examples/splitstr.c
+++ b/examples/splitstr.c
@@ -1,5 +1,4 @@
#include <stc/csview.h>
-#include <stc/cvec.h>
void print_split(csview str, csview sep)
{
@@ -12,7 +11,8 @@ void print_split(csview str, csview sep)
}
}
-using_cvec_str();
+#define i_val_str
+#include <stc/cvec.h>
cvec_str string_split(csview str, csview sep)
{
diff --git a/examples/stc_astar.c b/examples/stc_astar.c
index 6122aad4..4f5075a1 100644
--- a/examples/stc_astar.c
+++ b/examples/stc_astar.c
@@ -5,10 +5,6 @@
// https://www.redblobgames.com/pathfinding/a-star/introduction.html
#include <stc/cstr.h>
-#include <stc/cpque.h>
-#include <stc/cdeq.h>
-#include <stc/csmap.h>
-
#include <stdio.h>
typedef struct {
@@ -66,11 +62,27 @@ typedef struct {
MazePoint b;
} MazeStep;
-using_cdeq(pnt, MazePoint, c_no_compare);
-using_cpque(pnt, cdeq_pnt, mpnt_compare_priority);
-using_csmap(step, MazePoint, MazePoint, mpnt_key_compare);
-using_csmap(cost, MazePoint, int, mpnt_key_compare);
+#define i_tag pnt
+#define i_val MazePoint
+#define i_cmp c_no_compare
+#include <stc/cdeq.h>
+#define i_tag pnt
+#define i_val MazePoint
+#define i_cmp mpnt_compare_priority
+#include <stc/cpque.h>
+
+#define i_tag step
+#define i_key MazePoint
+#define i_val MazePoint
+#define i_cmp mpnt_key_compare
+#include <stc/csmap.h>
+
+#define i_tag cost
+#define i_key MazePoint
+#define i_val int
+#define i_cmp mpnt_key_compare
+#include <stc/csmap.h>
cdeq_pnt
astar(cstr maze, int width)
@@ -79,7 +91,7 @@ astar(cstr maze, int width)
MazePoint goal = mpnt_from(maze, "!", width);
cdeq_pnt path = cdeq_pnt_init(); // returned
-
+
cpque_pnt frontier = cpque_pnt_init();
csmap_step came_from = csmap_step_init();
csmap_cost cost_so_far = csmap_cost_init();
diff --git a/examples/words.c b/examples/words.c
index afc1f989..442f04f0 100644
--- a/examples/words.c
+++ b/examples/words.c
@@ -1,40 +1,31 @@
#include <math.h>
#include <stc/cstr.h>
-#include <stc/cmap.h>
-#include <stc/clist.h>
-#include <stc/cvec.h>
-using_cvec_str();
-using_clist_str();
-using_cmap_strkey(si, int);
+#define i_val_str
+#include <stc/cvec.h>
+#define i_tag strn
+#define i_key_str
+#define i_val int
+#include <stc/cmap.h>
int main1()
{
- c_var (clist_str, lwords, {
- "this", "sentence", "is", "not", "a", "sentence",
- "this", "sentence", "is", "a", "hoax"
- });
- c_fordefer (clist_str_del(&lwords))
+ c_forauto (cvec_str, words)
+ c_forauto (cmap_strn, word_map)
{
- clist_str_push_back(&lwords, cstr_from_fmt("%.15f", sqrt(2)));
- c_foreach (w, clist_str, lwords)
- printf("%s\n", w.ref->str);
- puts("");
-
- c_var (cvec_str, words, {
+ c_emplace (cvec_str, words, {
"this", "sentence", "is", "not", "a", "sentence",
"this", "sentence", "is", "a", "hoax"
});
- c_fordefer (cvec_str_del(&words))
- {
- cmap_si word_map = cmap_si_init();
- c_foreach (w, cvec_str, words)
- cmap_si_emplace(&word_map, w.ref->str, 0).ref->second += 1;
- c_foreach (i, cmap_si, word_map) {
- printf("%d occurrences of word '%s'\n", i.ref->second, i.ref->first.str);
- }
+ c_foreach (w, cvec_str, words) {
+ cmap_strn_emplace(&word_map, w.ref->str, 0).ref->second += 1;
+ }
+
+ c_foreach (i, cmap_strn, word_map) {
+ printf("%d occurrences of word '%s'\n",
+ i.ref->second, i.ref->first.str);
}
}
return 0;