diff options
| author | Tyge Løvset <[email protected]> | 2021-02-20 15:50:36 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2021-02-20 15:50:36 +0100 |
| commit | b24dd76490733666f7f7b3147c5c64a75c973ac6 (patch) | |
| tree | f2d6833b289bbb40fbb7f81cdccc32ba5315777d /examples | |
| parent | ac14ed84b111762544bb850e869c5e98cfe89092 (diff) | |
| download | STC-modified-b24dd76490733666f7f7b3147c5c64a75c973ac6.tar.gz STC-modified-b24dd76490733666f7f7b3147c5c64a75c973ac6.zip | |
Rewamp of cmap/csmap API. More to come.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/advanced.c | 11 | ||||
| -rw-r--r-- | examples/complex.c | 12 | ||||
| -rw-r--r-- | examples/demos.c | 16 | ||||
| -rw-r--r-- | examples/inits.c | 10 | ||||
| -rw-r--r-- | examples/mapmap.c | 16 | ||||
| -rw-r--r-- | examples/phonebook.c | 2 | ||||
| -rw-r--r-- | examples/stc_astar.c | 4 |
7 files changed, 33 insertions, 38 deletions
diff --git a/examples/advanced.c b/examples/advanced.c index 9b1b1275..8d045b3a 100644 --- a/examples/advanced.c +++ b/examples/advanced.c @@ -12,7 +12,6 @@ void viking_del(Viking* vk) { cstr_del(&vk->country); } - // Define Viking raw struct with hash, equals, and convertion functions between Viking and VikingRaw structs: typedef struct VikingRaw { @@ -31,16 +30,14 @@ static inline int vikingraw_equals(const VikingRaw* rx, const VikingRaw* ry) { static inline Viking viking_fromRaw(VikingRaw raw) { // note: parameter is by value Viking vk = {cstr_from(raw.name), cstr_from(raw.country)}; return vk; } - static inline VikingRaw viking_toRaw(Viking* vk) { VikingRaw raw = {vk->name.str, vk->country.str}; return raw; } -// With this in place, we use the full using_cmap() macro to define {Viking -> int} hash map type: +// With this in place, we use the using_cmap_keyarg() macro to define {Viking -> int} hash map type: -using_cmap(vk, Viking, int, vikingraw_equals, vikingraw_hash, - c_default_del, c_default_clone, - viking_del, viking_fromRaw, viking_toRaw, VikingRaw); +using_cmap_keyarg(vk, Viking, int, vikingraw_equals, vikingraw_hash, + viking_del, viking_fromRaw, viking_toRaw, VikingRaw); int main() { @@ -50,7 +47,7 @@ int main() { {"Olaf", "Denmark"}, 24}, { {"Harald", "Iceland"}, 12}, }); - cmap_vk_put(&vikings, (VikingRaw){"Bjorn", "Sweden"}, 10); + cmap_vk_emplace_put(&vikings, (VikingRaw){"Bjorn", "Sweden"}, 10); VikingRaw lookup = {"Einar", "Norway"}; diff --git a/examples/complex.c b/examples/complex.c index d7ea016e..05d74557 100644 --- a/examples/complex.c +++ b/examples/complex.c @@ -5,10 +5,10 @@ void check_del(float* v) {printf("destroy %g\n", *v);}
-using_carray(f, float, check_del, c_default_clone); // normally omit the last 2 arguments - float type need no destroy.
-using_clist(a, carray2f, c_no_compare, carray2f_del, carray2f_clone);
-using_cmap(l, int, clist_a, c_default_equals, c_default_hash, clist_a_del, clist_a_clone);
-using_cmap_strkey(s, cmap_l, cmap_l_del, cmap_l_clone);
+using_carray(f, float, check_del);
+using_clist(a, carray2f, c_no_compare, carray2f_del);
+using_cmap(l, int, clist_a, c_default_equals, c_default_hash, clist_a_del);
+using_cmap_strkey(s, cmap_l, cmap_l_del);
int main() {
int xdim = 4, ydim = 6;
@@ -26,8 +26,8 @@ int main() { // Put in some data.
*carray2f_at(&arr_a, y, x) = 3.1415927f; // aa[y][x]
clist_a_push_back(&tableList, arr_a);
- cmap_l_put(&listMap, tableKey, tableList);
- cmap_s_put(&myMap, strKey, listMap);
+ cmap_l_insert(&listMap, tableKey, tableList);
+ cmap_s_insert(&myMap, cstr_from(strKey), listMap);
// Access the data entry
carray2f arr_b = *clist_a_back(&cmap_l_find(&cmap_s_find(&myMap, strKey).ref->second, tableKey).ref->second);
diff --git a/examples/demos.c b/examples/demos.c index 55de0c14..db284ea9 100644 --- a/examples/demos.c +++ b/examples/demos.c @@ -124,8 +124,8 @@ void mapdemo1() {
printf("\nMAPDEMO1\n");
cmap_ii nums = cmap_ii_init();
- cmap_ii_put(&nums, 8, 64);
- cmap_ii_put(&nums, 11, 121);
+ cmap_ii_emplace_put(&nums, 8, 64);
+ cmap_ii_emplace_put(&nums, 11, 121);
printf("val 8: %d\n", *cmap_ii_at(&nums, 8));
cmap_ii_del(&nums);
}
@@ -137,9 +137,9 @@ void mapdemo2() {
printf("\nMAPDEMO2\n");
cmap_si nums = cmap_si_init();
- cmap_si_put(&nums, "Hello", 64);
- cmap_si_put(&nums, "Groovy", 121);
- cmap_si_put(&nums, "Groovy", 200); // overwrite previous
+ cmap_si_emplace_put(&nums, "Hello", 64);
+ cmap_si_emplace_put(&nums, "Groovy", 121);
+ cmap_si_emplace_put(&nums, "Groovy", 200); // overwrite previous
// iterate the map:
for (cmap_si_iter_t i = cmap_si_begin(&nums); i.ref != cmap_si_end(&nums).ref; cmap_si_next(&i))
@@ -159,9 +159,9 @@ void mapdemo3() {
printf("\nMAPDEMO3\n");
cmap_str table = cmap_str_init();
- cmap_str_put(&table, "Map", "test");
- cmap_str_put(&table, "Make", "my");
- cmap_str_put(&table, "Sunny", "day");
+ cmap_str_emplace_put(&table, "Map", "test");
+ cmap_str_emplace_put(&table, "Make", "my");
+ cmap_str_emplace_put(&table, "Sunny", "day");
cmap_str_value_t *e = cmap_str_find(&table, "Make").ref;
c_foreach (i, cmap_str, table)
printf("entry: %s: %s\n", i.ref->first.str, i.ref->second.str);
diff --git a/examples/inits.c b/examples/inits.c index 2c138a43..6e2d1bd9 100644 --- a/examples/inits.c +++ b/examples/inits.c @@ -5,7 +5,7 @@ #include <stc/cpque.h>
#include <stc/clist.h>
-using_cmap(id, int, cstr, c_default_equals, c_default_hash, cstr_del, cstr_clone); // Map of int -> cstr
+using_cmap(id, int, cstr, c_default_equals, c_default_hash, cstr_del); // Map of int -> cstr
using_cmap_strkey(cnt, int);
typedef struct {int x, y;} ipair_t;
@@ -46,11 +46,9 @@ int main(void) int year = 2020;
cmap_id idnames = cmap_id_init();
- c_push_items(&idnames, cmap_id, {
- {100, cstr_from("Hello")},
- {110, cstr_from("World")},
- {120, cstr_from_fmt("Howdy, -%d-", year)},
- });
+ cmap_id_insert(&idnames, 100, cstr_from("Hello"));
+ cmap_id_insert(&idnames, 110, cstr_from("World"));
+ cmap_id_insert(&idnames, 120, cstr_from_fmt("Howdy, -%d-", year));
c_foreach (i, cmap_id, idnames)
printf("%d: %s\n", i.ref->first, i.ref->second.str);
diff --git a/examples/mapmap.c b/examples/mapmap.c index 7782f5bf..c55104c9 100644 --- a/examples/mapmap.c +++ b/examples/mapmap.c @@ -4,19 +4,19 @@ #include <stc/cstr.h>
using_cmap_str();
-using_cmap_strkey(cfg, cmap_str, cmap_str_del, cmap_str_clone);
+using_cmap_strkey(cfg, cmap_str, cmap_str_del);
int main(void) {
cmap_cfg config = cmap_cfg_init();
cmap_str init = cmap_str_init();
- cmap_str_put(&cmap_cfg_emplace(&config, "user", init).first->second, "name", "Joe");
- cmap_str_put(&cmap_cfg_emplace(&config, "user", init).first->second, "groups", "proj1,proj3");
- cmap_str_put(&cmap_cfg_emplace(&config, "group", init).first->second, "proj1", "Energy");
- cmap_str_put(&cmap_cfg_emplace(&config, "group", init).first->second, "proj2", "Windy");
- cmap_str_put(&cmap_cfg_emplace(&config, "group", init).first->second, "proj3", "Oil");
- cmap_str_put(&cmap_cfg_emplace(&config, "admin", init).first->second, "employees", "2302");
+ cmap_str_emplace(&cmap_cfg_insert(&config, cstr_from("user"), init).first->second, "name", "Joe");
+ cmap_str_emplace(&cmap_cfg_insert(&config, cstr_from("user"), init).first->second, "groups", "proj1,proj3");
+ cmap_str_emplace(&cmap_cfg_insert(&config, cstr_from("group"), init).first->second, "proj1", "Energy");
+ cmap_str_emplace(&cmap_cfg_insert(&config, cstr_from("group"), init).first->second, "proj2", "Windy");
+ cmap_str_emplace(&cmap_cfg_insert(&config, cstr_from("group"), init).first->second, "proj3", "Oil");
+ cmap_str_emplace(&cmap_cfg_insert(&config, cstr_from("admin"), init).first->second, "employees", "2302");
- cmap_str_put(&cmap_cfg_emplace(&config, "group", init).first->second, "proj2", "Wind"); // Update
+ cmap_str_emplace_put(&cmap_cfg_insert(&config, cstr_from("group"), init).first->second, "proj2", "Wind"); // Update
c_foreach (i, cmap_cfg, config)
c_foreach (j, cmap_str, i.ref->second)
diff --git a/examples/phonebook.c b/examples/phonebook.c index 395716bc..34364bcb 100644 --- a/examples/phonebook.c +++ b/examples/phonebook.c @@ -67,7 +67,7 @@ int main(int argc, char **argv) printf("\nPhone book after erasing Tariq and Elliott:\n");
print_phone_book(phone_book);
- cmap_str_put(&phone_book, "Zak Byers", "(555) 396-188");
+ cmap_str_emplace_put(&phone_book, "Zak Byers", "(555) 396-188");
printf("\nPhone book after update phone of Zak Byers:\n");
print_phone_book(phone_book);
diff --git a/examples/stc_astar.c b/examples/stc_astar.c index 91d475e9..e7bb86e5 100644 --- a/examples/stc_astar.c +++ b/examples/stc_astar.c @@ -106,10 +106,10 @@ astar(cstr maze, int width) csmap_mc_value_t* cost = csmap_mc_find(&cost_so_far, next).ref;
if (!cost || new_cost < cost->second)
{
- csmap_mc_put(&cost_so_far, next, new_cost); // update (put)
+ csmap_mc_emplace_put(&cost_so_far, next, new_cost); // update (put)
next.priorty = new_cost + abs(goal.x - next.x) + abs(goal.y - next.y);
cpque_mp_push(&frontier, next);
- csmap_ms_put(&came_from, next, current);
+ csmap_ms_emplace_put(&came_from, next, current);
}
}
}
|
