summaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
authortylo <[email protected]>2021-10-06 09:24:37 +0200
committertylo <[email protected]>2021-10-06 09:24:37 +0200
commitd4ea76c273bbebdb2b1be7302a9a6edde2198134 (patch)
tree8100169462e7a4fbe3d17a49bc97da4ce6c187b5 /examples
parent6c342ffdeee5353ecf78493c41ee23ef79cbd940 (diff)
downloadSTC-modified-d4ea76c273bbebdb2b1be7302a9a6edde2198134.tar.gz
STC-modified-d4ea76c273bbebdb2b1be7302a9a6edde2198134.zip
Updates to previous commit on cvec and cdeq. Some example improvements.
Diffstat (limited to 'examples')
-rw-r--r--examples/complex.c48
-rw-r--r--examples/convert.c97
-rw-r--r--examples/cpque.c93
-rw-r--r--examples/csset_erase.c6
4 files changed, 125 insertions, 119 deletions
diff --git a/examples/complex.c b/examples/complex.c
index c910be80..69900bba 100644
--- a/examples/complex.c
+++ b/examples/complex.c
@@ -35,27 +35,29 @@ int main() {
int x = 1, y = 3, tableKey = 42;
const char* strKey = "first";
- cmap_map myMap = cmap_map_init();
- cmap_lst listMap = cmap_lst_init();
- clist_arr tableList = clist_arr_init();
- cstack_f stk = cstack_f_with_capacity(xdim * ydim);
- memset(stk.data, 0, xdim*ydim*sizeof *stk.data);
- stk.size = stk.capacity;
-
- printf("stk size: %zu\n", cstack_f_size(stk));
-
- // Put in some data in stack array
- stk.data[x] = 3.1415927f;
- clist_arr_push_back(&tableList, stk);
- cmap_lst_insert(&listMap, tableKey, tableList);
- cmap_map_insert(&myMap, cstr_from(strKey), listMap);
-
- // Access the data entry
- cmap_lst* mapL = &cmap_map_find(&myMap, strKey).ref->second;
- clist_arr* lstA = &cmap_lst_find(mapL, tableKey).ref->second;
- cstack_f arr = *clist_arr_back(lstA);
- printf("value (%d) is: %f\n", x, arr.data[x]);
-
- stk.data[x] = 1.41421356f; // change the value in array
- cmap_map_del(&myMap); // free up everything!
+ c_auto (cmap_map, myMap)
+ {
+ cstack_f stk = cstack_f_with_capacity(xdim * ydim);
+ memset(stk.data, 0, xdim*ydim*sizeof *stk.data);
+ stk.size = stk.capacity;
+
+ // Put in some data in stack array
+ stk.data[x] = 3.1415927f;
+ printf("stk size: %zu\n", cstack_f_size(stk));
+
+ clist_arr tableList = clist_arr_init();
+ clist_arr_push_back(&tableList, stk);
+
+ cmap_lst listMap = cmap_lst_init();
+ cmap_lst_insert(&listMap, tableKey, tableList);
+ cmap_map_insert(&myMap, cstr_from(strKey), listMap);
+
+ // Access the data entry
+ cmap_lst* mapL = &cmap_map_find(&myMap, strKey).ref->second;
+ clist_arr* lstA = &cmap_lst_find(mapL, tableKey).ref->second;
+ cstack_f arr = *clist_arr_back(lstA);
+ printf("value (%d) is: %f\n", x, arr.data[x]);
+
+ stk.data[x] = 1.41421356f; // change the value in array
+ }
}
diff --git a/examples/convert.c b/examples/convert.c
index d42454ee..83dc1b37 100644
--- a/examples/convert.c
+++ b/examples/convert.c
@@ -1,49 +1,48 @@
-
-#include <stc/cstr.h>
-#include <stdio.h>
-
-#define i_key_str
-#define i_val_str
-#include <stc/cmap.h>
-
-#define i_val_str
-#include <stc/cvec.h>
-
-#define i_val_str
-#include <stc/clist.h>
-
-int main()
-{
- cmap_str map = cmap_str_init();
- cmap_str_emplace(&map, "green", "#00ff00");
- cmap_str_emplace(&map, "blue", "#0000ff");
- cmap_str_emplace(&map, "yellow", "#ffff00");
-
- puts("MAP:");
- c_foreach (i, cmap_str, map)
- printf(" %s: %s\n", i.ref->first.str, i.ref->second.str);
-
- puts("\nCLONE MAP:");
- cmap_str mclone = cmap_str_clone(map);
- c_foreach (i, cmap_str, mclone)
- printf(" %s: %s\n", i.ref->first.str, i.ref->second.str);
-
-
- puts("\nMAP TO VECS:");
- cvec_str vec1 = cvec_str_init(), vec2 = cvec_str_init();
- c_foreach (i, cmap_str, mclone) {
- cvec_str_emplace_back(&vec1, i.ref->first.str);
- cvec_str_emplace_back(&vec2, i.ref->second.str);
- }
- c_forrange (i, cvec_str_size(vec1))
- printf(" %s: %s\n", vec1.data[i].str, vec2.data[i].str);
-
- puts("\nVEC TO LIST:");
- clist_str list = clist_str_init();
- c_foreach (i, cvec_str, vec1) clist_str_emplace_back(&list, i.ref->str);
- c_foreach (i, clist_str, list) printf(" %s\n", i.ref->str);
-
- c_del(cmap_str, &map, &mclone);
- c_del(cvec_str, &vec1, &vec2);
- clist_str_del(&list);
-} \ No newline at end of file
+#include <stc/cstr.h>
+
+#define i_key_str
+#define i_val_str
+#include <stc/cmap.h>
+
+#define i_val_str
+#include <stc/cvec.h>
+
+#define i_val_str
+#include <stc/clist.h>
+
+int main()
+{
+ c_auto (cmap_str, map, mclone)
+ c_auto (cvec_str, keys, values)
+ c_auto (clist_str, list)
+ {
+ c_apply_pair(cmap_str, emplace, &map, {
+ {"green", "#00ff00"},
+ {"blue", "#0000ff"},
+ {"yellow", "#ffff00"},
+ });
+ puts("MAP:");
+ c_foreach (i, cmap_str, map)
+ printf(" %s: %s\n", i.ref->first.str, i.ref->second.str);
+
+ puts("\nCLONE MAP:");
+ mclone = cmap_str_clone(map);
+ c_foreach (i, cmap_str, mclone)
+ printf(" %s: %s\n", i.ref->first.str, i.ref->second.str);
+
+ puts("\nCOPY MAP TO VECS:");
+ c_foreach (i, cmap_str, mclone) {
+ cvec_str_emplace_back(&keys, i.ref->first.str);
+ cvec_str_emplace_back(&values, i.ref->second.str);
+ }
+ c_forrange (i, cvec_str_size(keys))
+ printf(" %s: %s\n", keys.data[i].str, values.data[i].str);
+
+ puts("\nCOPY VEC TO LIST:");
+ c_foreach (i, cvec_str, keys)
+ clist_str_emplace_back(&list, i.ref->str);
+
+ c_foreach (i, clist_str, list)
+ printf(" %s\n", i.ref->str);
+ }
+}
diff --git a/examples/cpque.c b/examples/cpque.c
index 541dcb6e..db7bc02c 100644
--- a/examples/cpque.c
+++ b/examples/cpque.c
@@ -1,46 +1,47 @@
-// Implements c++ example: https://en.cppreference.com/w/cpp/container/priority_queue
-#include <stdio.h>
-#include <stc/ccommon.h>
-
-// Example of dynamic compare function
-
-static int (*icmp_fn)(const int* x, const int* y);
-
-#define i_val int
-#define i_cmp icmp_fn
-#define i_cnt ipque
-#include <stc/cpque.h>
-
-#define imix_less(x, y) ((*(x) ^ 1) < (*(y) ^ 1))
-static int imax_cmp(const int* x, const int* y) { return *x - *y; }
-static int imin_cmp(const int* x, const int* y) { return *y - *x; }
-static int imix_cmp(const int* x, const int* y) { return c_less_compare(imix_less, x, y); }
-
-void print_pque(ipque q) {
- ipque copy = ipque_clone(q);
- while (!ipque_empty(copy)) {
- printf("%d ", *ipque_top(&copy));
- ipque_pop(&copy);
- }
- puts("");
- ipque_del(&copy);
-}
-
-int main()
-{
- const int data[] = {1,8,5,6,3,4,0,9,7,2}, n = c_arraylen(data);
- c_auto (ipque, q, q2, q3) // init() and defered del()
- {
- icmp_fn = imax_cmp;
- c_apply_n(ipque, push, &q, data, n);
- print_pque(q);
-
- icmp_fn = imin_cmp;
- c_apply_n(ipque, push, &q2, data, n);
- print_pque(q2);
-
- icmp_fn = imix_cmp;
- c_apply_n(ipque, push, &q3, data, n);
- print_pque(q3);
- }
-} \ No newline at end of file
+// Implements c++ example: https://en.cppreference.com/w/cpp/container/priority_queue
+#include <stdio.h>
+
+// Example of dynamic compare function
+
+static int (*icmp_fn)(const int* x, const int* y);
+
+#define i_val int
+#define i_cmp icmp_fn
+#define i_cnt ipque
+#include <stc/cpque.h>
+
+#define imix_less(left, right) ((*(left) ^ 1) < (*(right) ^ 1))
+static int imax_cmp(const int* x, const int* y) { return *x - *y; }
+static int imin_cmp(const int* x, const int* y) { return *y - *x; }
+static int imix_cmp(const int* x, const int* y) { return c_less_compare(imix_less, x, y); }
+
+void print_queue(ipque q) {
+ ipque copy = ipque_clone(q);
+ while (!ipque_empty(copy)) {
+ printf("%d ", *ipque_top(&copy));
+ ipque_pop(&copy);
+ }
+ puts("");
+ ipque_del(&copy);
+}
+
+int main()
+{
+ const int data[] = {1,8,5,6,3,4,0,9,7,2}, n = c_arraylen(data);
+ c_auto (ipque, q, q2, q3) // init() and defered del()
+ {
+ icmp_fn = imax_cmp;
+ c_forrange (i, n)
+ ipque_push(&q, data[i]);
+
+ print_queue(q);
+
+ icmp_fn = imin_cmp;
+ c_apply_n(ipque, push, &q2, data, n);
+ print_queue(q2);
+
+ icmp_fn = imix_cmp;
+ c_apply_n(ipque, push, &q3, data, n);
+ print_queue(q3);
+ }
+}
diff --git a/examples/csset_erase.c b/examples/csset_erase.c
index df9b3d54..6104a2b2 100644
--- a/examples/csset_erase.c
+++ b/examples/csset_erase.c
@@ -16,11 +16,14 @@ int main()
csset_int_iter_t it;
printf("Show values >= %d:\n", val);
it = csset_int_lower_bound(&set, val);
+
c_foreach (k, csset_int, it, csset_int_end(&set))
printf(" %d", *k.ref); puts("");
printf("Erase values >= %d:\n", val);
- while (it.ref) it = csset_int_erase_at(&set, it);
+ while (it.ref != csset_int_end(&set).ref)
+ it = csset_int_erase_at(&set, it);
+
c_foreach (k, csset_int, set)
printf(" %d", *k.ref);
puts("");
@@ -29,6 +32,7 @@ int main()
printf("Erase values < %d:\n", val);
it = csset_int_lower_bound(&set, val);
csset_int_erase_range(&set, csset_int_begin(&set), it);
+
c_foreach (k, csset_int, set)
printf(" %d", *k.ref);
puts("");