diff options
| author | Tyge Løvset <[email protected]> | 2021-09-12 09:26:03 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2021-09-12 09:26:03 +0200 |
| commit | 1a9020f895754f564e480ba9ed564c637f26060e (patch) | |
| tree | 86a66e45a19fb60aa4747b351ba53600d074c5fa | |
| parent | a2aa14cc91ccbd42cd1188c6fac6126a38d0de77 (diff) | |
| download | STC-modified-1a9020f895754f564e480ba9ed564c637f26060e.tar.gz STC-modified-1a9020f895754f564e480ba9ed564c637f26060e.zip | |
Some improvements in examples.
| -rw-r--r-- | examples/complex.c | 2 | ||||
| -rw-r--r-- | examples/csmap_find.c | 1 | ||||
| -rw-r--r-- | examples/multimap.c | 29 | ||||
| -rw-r--r-- | examples/read.c | 29 |
4 files changed, 30 insertions, 31 deletions
diff --git a/examples/complex.c b/examples/complex.c index 5cbaaa40..e7789815 100644 --- a/examples/complex.c +++ b/examples/complex.c @@ -44,7 +44,7 @@ int main() { printf("stk size: %zu\n", cstack_f_size(stk));
- // Put in some data in 2D array
+ // Put in some data in stack array
stk.data[x] = 3.1415927f;
clist_arr_push_back(&tableList, stk);
cmap_lst_insert(&listMap, tableKey, tableList);
diff --git a/examples/csmap_find.c b/examples/csmap_find.c index 79a0f993..66768016 100644 --- a/examples/csmap_find.c +++ b/examples/csmap_find.c @@ -1,7 +1,6 @@ // 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/cstr.h> -#include <stdio.h> #define i_tag istr #define i_key int diff --git a/examples/multimap.c b/examples/multimap.c index eeb7a501..0f5ce538 100644 --- a/examples/multimap.c +++ b/examples/multimap.c @@ -45,11 +45,11 @@ void OlympicLocation_del(OlympicLocation* self) { // Create a clist<OlympicLocation>, can be sorted by year.
#define i_tag OL
#define i_val OlympicLocation
-#define i_cmp OlympicLocation_compare
#define i_valdel OlympicLocation_del
+#define i_cmp OlympicLocation_compare
#include <stc/clist.h>
-// Create a csmap<cstr, clist_OL> where key is country
+// Create a csmap<cstr, clist_OL> where key is country name
#define i_tag OL
#define i_key_str
#define i_val clist_OL
@@ -59,27 +59,28 @@ void OlympicLocation_del(OlympicLocation* self) { int main()
{
// Define the multimap with destructor defered to when block is completed.
- c_forvar (csmap_OL multimap = csmap_OL_init(), csmap_OL_del(&multimap))
+ c_forauto (csmap_OL, multimap)
{
- clist_OL empty = clist_OL_init();
+ const clist_OL empty = clist_OL_init();
for (int i = 0; i < c_arraylen(ol_data); ++i)
{
- struct OlympicsData* it = &ol_data[i];
- OlympicLocation loc = {.year = it->year,
- .city = cstr_from(it->city),
- .date = cstr_from(it->date)};
+ struct OlympicsData* d = &ol_data[i];
+ OlympicLocation loc = {.year = d->year,
+ .city = cstr_from(d->city),
+ .date = cstr_from(d->date)};
// Insert an empty list for each new country, and append the entry to the list.
- // If list already exist in map, it returns it from the insert function.
- clist_OL_push_back(&csmap_OL_insert(&multimap, cstr_from(it->country), empty).ref->second, loc);
+ // If country already exist in map, its list is returned from the insert function.
+ clist_OL* list = &csmap_OL_insert(&multimap, cstr_from(d->country), empty).ref->second;
+ clist_OL_push_back(list, loc);
}
-
- // Iterate the multimap:
+ // Sort locations by year for each country.
c_foreach (country, csmap_OL, multimap)
- {
- // Sort locations by year for each country.
clist_OL_sort(&country.ref->second);
+ // Print the multimap:
+ c_foreach (country, csmap_OL, multimap)
+ {
// Loop the locations for a country sorted by year
c_foreach (loc, clist_OL, country.ref->second)
printf("%s: %d, %s, %s\n", country.ref->first.str, loc.ref->year,
diff --git a/examples/read.c b/examples/read.c index bb0e139f..2e8320eb 100644 --- a/examples/read.c +++ b/examples/read.c @@ -1,27 +1,26 @@ -#include <errno.h>
#include <stc/cstr.h>
+#include <errno.h>
#define i_val_str
#include <stc/cvec.h>
-cvec_str read_file(const char* name) {
+cvec_str read_file(const char* name)
+{
cvec_str vec = cvec_str_init();
- for (FILE* f = fopen(name, "r"); f; fclose(f), f=NULL) {
- cstr line = cstr_init();
- while (cstr_getline(&line, f))
- cvec_str_emplace_back(&vec, line.str);
- cstr_del(&line);
- }
+ c_forvar (FILE* f = fopen(name, "r"), fclose(f))
+ c_forauto (cstr, line)
+ while (cstr_getline(&line, f))
+ cvec_str_emplace_back(&vec, line.str);
return vec;
}
-int main() {
- cvec_str vec = read_file("read.c");
- if (errno) printf("errno: %d\n", errno);
-
+int main()
+{
int n = 0;
- c_foreach (i, cvec_str, vec)
- printf("%5d: %s\n", ++n, i.ref->str);
+ c_forvar (cvec_str vec = read_file(__FILE__), cvec_str_del(&vec))
+ c_foreach (i, cvec_str, vec)
+ printf("%5d: %s\n", ++n, i.ref->str);
- cvec_str_del(&vec);
+ if (errno)
+ printf("error: read_file(" __FILE__ "). errno: %d\n", errno);
}
\ No newline at end of file |
