summaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
authortylo <[email protected]>2021-08-25 17:28:52 +0200
committertylo <[email protected]>2021-08-25 17:28:52 +0200
commit4c9ad3e584feff277800e1dfe49ae470e3c4a9c4 (patch)
tree7514984d73dbc2148b447a6b49d348ee110abb1d /examples
parenta4bacdaf0feae20d417e53d8467ff332ac29413e (diff)
downloadSTC-modified-4c9ad3e584feff277800e1dfe49ae470e3c4a9c4.tar.gz
STC-modified-4c9ad3e584feff277800e1dfe49ae470e3c4a9c4.zip
Small updates of some examples.
Diffstat (limited to 'examples')
-rw-r--r--examples/csmap_find.c133
-rw-r--r--examples/ex_gauss1.c22
-rw-r--r--examples/mmap.c18
3 files changed, 88 insertions, 85 deletions
diff --git a/examples/csmap_find.c b/examples/csmap_find.c
index 314e702e..46ae01fa 100644
--- a/examples/csmap_find.c
+++ b/examples/csmap_find.c
@@ -1,65 +1,68 @@
-// 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);
-
-void print_elem(csmap_istr_rawvalue_t p) {
- printf("(%d, %s) ", p.first, p.second);
-}
-
-#define using_print_collection(CX) \
- void print_collection_##CX(CX t) { \
- printf("%zu elements: ", CX##_size(t)); \
- \
- c_foreach (p, CX, t) { \
- print_elem(CX##_value_toraw(p.ref)); \
- } \
- puts(""); \
- }
-
-using_print_collection(csmap_istr);
-using_print_collection(cvec_istr);
-
-
-void findit(csmap_istr c, csmap_istr_key_t val) {
- printf("Trying find() on value %d\n", val);
- csmap_istr_value_t* result = csmap_istr_get(&c, val); // easier with get than find.
- if (result) {
- printf("Element found: "); print_elem(csmap_istr_value_toraw(result)); puts("");
- } else {
- puts("Element not found.");
- }
-}
-
-int main()
-{
- c_forvar (csmap_istr m1 = csmap_istr_init(), csmap_istr_del(&m1))
- c_forvar (cvec_istr v = cvec_istr_init(), cvec_istr_del(&v)) {
- c_emplace(csmap_istr, m1, { { 40, "Zr" }, { 45, "Rh" } });
- puts("The starting map m1 is (key, value):");
- print_collection_csmap_istr(m1);
-
- typedef cvec_istr_value_t pair;
- cvec_istr_emplace_back(&v, (pair){43, "Tc"});
- cvec_istr_emplace_back(&v, (pair){41, "Nb"});
- cvec_istr_emplace_back(&v, (pair){46, "Pd"});
- cvec_istr_emplace_back(&v, (pair){42, "Mo"});
- cvec_istr_emplace_back(&v, (pair){44, "Ru"});
- cvec_istr_emplace_back(&v, (pair){44, "Ru"}); // attempt a duplicate
-
- 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):");
- print_collection_csmap_istr(m1);
- puts("");
- findit(m1, 45);
- findit(m1, 6);
- }
-} \ No newline at end of file
+// 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);
+
+void print_elem(csmap_istr_rawvalue_t p) {
+ printf("(%d, %s) ", p.first, p.second);
+}
+
+#define using_print_collection(CX) \
+ void print_collection_##CX(CX t) { \
+ printf("%zu elements: ", CX##_size(t)); \
+ \
+ c_foreach (p, CX, t) { \
+ print_elem(CX##_value_toraw(p.ref)); \
+ } \
+ puts(""); \
+ }
+
+using_print_collection(csmap_istr);
+using_print_collection(cvec_istr);
+
+
+void findit(csmap_istr c, csmap_istr_key_t val)
+{
+ printf("Trying find() on value %d\n", val);
+ csmap_istr_value_t* result = csmap_istr_get(&c, val); // easier with get than find.
+ if (result) {
+ printf("Element found: "); print_elem(csmap_istr_value_toraw(result)); puts("");
+ } else {
+ puts("Element not found.");
+ }
+}
+
+int main()
+{
+ c_forauto (csmap_istr, m1)
+ c_forauto (cvec_istr, v)
+ {
+ c_emplace(csmap_istr, m1, { { 40, "Zr" }, { 45, "Rh" } });
+ puts("The starting map m1 is (key, value):");
+ print_collection_csmap_istr(m1);
+
+ typedef cvec_istr_value_t pair;
+ cvec_istr_emplace_back(&v, (pair){43, "Tc"});
+ cvec_istr_emplace_back(&v, (pair){41, "Nb"});
+ cvec_istr_emplace_back(&v, (pair){46, "Pd"});
+ cvec_istr_emplace_back(&v, (pair){42, "Mo"});
+ cvec_istr_emplace_back(&v, (pair){44, "Ru"});
+ cvec_istr_emplace_back(&v, (pair){44, "Ru"}); // attempt a duplicate
+
+ 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):");
+ print_collection_csmap_istr(m1);
+ puts("");
+ findit(m1, 45);
+ findit(m1, 6);
+ }
+}
diff --git a/examples/ex_gauss1.c b/examples/ex_gauss1.c
index 9eb5982d..d089de89 100644
--- a/examples/ex_gauss1.c
+++ b/examples/ex_gauss1.c
@@ -6,8 +6,8 @@
#include <stc/cmap.h>
#include <stc/cvec.h>
-// Declare int -> int hashmap. Uses typetag 'i' for ints.
-using_cmap(i, int, size_t);
+// Declare int -> int hashmap. Uses typetag 'ii' for ints.
+using_cmap(ii, int32_t, size_t);
// Declare int vector with map entries that can be sorted by map keys.
typedef struct {int first; size_t second;} mapval;
@@ -15,7 +15,7 @@ static int compare(mapval *a, mapval *b) {
return c_default_compare(&a->first, &b->first);
}
-using_cvec(e, mapval, compare);
+using_cvec(pair, mapval, compare);
int main()
{
@@ -30,23 +30,23 @@ int main()
stc64_normalf_t dist = stc64_normalf_init(Mean, StdDev);
// Create and init histogram vec and map with defered destructors:
- c_forvar (cvec_e vhist = cvec_e_init(), cvec_e_del(&vhist))
- c_forvar (cmap_i mhist = cmap_i_init(), cmap_i_del(&mhist))
+ c_forauto (cvec_pair, histvec)
+ c_forauto (cmap_ii, histmap)
{
c_forrange (N) {
int index = (int) round( stc64_normalf(&rng, &dist) );
- cmap_i_emplace(&mhist, index, 0).ref->second += 1;
+ cmap_ii_emplace(&histmap, index, 0).ref->second += 1;
}
// Transfer map to vec and sort it by map keys.
- c_foreach (i, cmap_i, mhist)
- cvec_e_push_back(&vhist, c_make(mapval){i.ref->first, i.ref->second});
+ c_foreach (i, cmap_ii, histmap)
+ cvec_pair_push_back(&histvec, (mapval){i.ref->first, i.ref->second});
- cvec_e_sort(&vhist);
+ cvec_pair_sort(&histvec);
// Print the gaussian bar chart
- c_forvar (cstr bar = cstr_null, cstr_del(&bar))
- c_foreach (i, cvec_e, vhist) {
+ c_forauto (cstr, bar)
+ c_foreach (i, cvec_pair, histvec) {
size_t n = (size_t) (i.ref->second * StdDev * Scale * 2.5 / (float)N);
if (n > 0) {
cstr_resize(&bar, n, '*');
diff --git a/examples/mmap.c b/examples/mmap.c
index 04e501aa..9c92b19b 100644
--- a/examples/mmap.c
+++ b/examples/mmap.c
@@ -7,26 +7,26 @@
// Map of int => clist_str. Note the negation of c_default_compare
using_clist_str();
-using_csmap(m, int, clist_str, -c_default_compare, clist_str_del);
+using_csmap(mult, int, clist_str, -c_default_compare, clist_str_del);
-void print(const csmap_m mmap)
+void print(const csmap_mult mmap)
{
- c_foreach (e, csmap_m, mmap) {
+ c_foreach (e, csmap_mult, mmap) {
c_foreach (s, clist_str, e.ref->second)
printf("{%d,%s} ", e.ref->first, s.ref->str);
}
puts("");
}
-void insert(csmap_m* mmap, int key, const char* str)
+void insert(csmap_mult* mmap, int key, const char* str)
{
- clist_str *list = &csmap_m_insert(mmap, key, clist_str_init()).ref->second;
+ clist_str *list = &csmap_mult_insert(mmap, key, clist_str_init()).ref->second;
clist_str_emplace_back(list, str);
}
int main()
{
- c_forvar (csmap_m mmap = csmap_m_init(), csmap_m_del(&mmap))
+ c_forauto (csmap_mult, mmap)
{
// list-initialize
struct {int i; const char* s;} vals[] = {{2, "foo"}, {2, "bar"}, {3, "baz"}, {1, "abc"}, {5, "def"}};
@@ -49,13 +49,13 @@ int main()
print(mmap);
// erase all entries with key 5
- csmap_m_erase(&mmap, 5);
+ csmap_mult_erase(&mmap, 5);
print(mmap);
// find and erase a specific entry
clist_str_iter_t pos;
- c_foreach (e, csmap_m, mmap)
- if ((pos = clist_str_find(&e.ref->second, "bar")).ref) {
+ c_foreach (e, csmap_mult, mmap)
+ if ((pos = clist_str_find(&e.ref->second, "bar")).ref != clist_str_end(&e.ref->second).ref) {
clist_str_erase(&e.ref->second, pos);
break;
}