summaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2020-09-09 10:09:18 +0200
committerTyge Løvset <[email protected]>2020-09-09 10:09:18 +0200
commiteee239dadee4d599c279a7e24972d72c1edcce4d (patch)
treea84b034cb0e7ea2a2208aa7aa85eee1aead1f3b9 /examples
parent98d80fcef94ccd738173196dc3de0c889d847f28 (diff)
downloadSTC-modified-eee239dadee4d599c279a7e24972d72c1edcce4d.tar.gz
STC-modified-eee239dadee4d599c279a7e24972d72c1edcce4d.zip
More compliance with std:: containers.
Diffstat (limited to 'examples')
-rw-r--r--examples/advanced.c31
-rw-r--r--examples/benchmark.c10
-rw-r--r--examples/demos.c2
-rw-r--r--examples/list.c6
4 files changed, 27 insertions, 22 deletions
diff --git a/examples/advanced.c b/examples/advanced.c
index 29ba55aa..aa82e38e 100644
--- a/examples/advanced.c
+++ b/examples/advanced.c
@@ -8,12 +8,25 @@
* calculate hash values for the individual members, and then somehow combine them into one
* hash value for the entire object.
* In order to use Viking as a map key, it is smart to define a plain-old-data "view"
- * of the Viking struct first.
+ * of the Viking struct, to simplfy insert and lookup in the map.
*/
#include <stdio.h>
#include <stc/cmap.h>
#include <stc/cstr.h>
+// Viking data struct -----------------------
+
+typedef struct Viking {
+ cstr_t name;
+ cstr_t country;
+} Viking;
+
+
+void viking_destroy(Viking* vk) {
+ cstr_destroy(&vk->name);
+ cstr_destroy(&vk->country);
+}
+
// Viking view struct -----------------------
typedef struct VikingVw {
@@ -30,19 +43,6 @@ int vikingvw_equals(const VikingVw* x, const VikingVw* y) {
return strcmp(x->country, y->country) == 0;
}
-// Viking data struct -----------------------
-
-typedef struct Viking {
- cstr_t name;
- cstr_t country;
-} Viking;
-
-
-void viking_destroy(Viking* vk) {
- cstr_destroy(&vk->name);
- cstr_destroy(&vk->country);
-}
-
VikingVw viking_toVw(Viking* vk) {
VikingVw vw = {vk->name.str, vk->country.str}; return vw;
}
@@ -50,7 +50,6 @@ Viking viking_fromVw(VikingVw vw) {
Viking vk = {cstr_make(vw.name), cstr_make(vw.country)}; return vk;
}
-
// Using the full declare_cmap() macro to define [Viking -> int] hash map type:
declare_cmap(vk, Viking, int, c_default_destroy, vikingvw_equals, vikingvw_hash,
viking_destroy, VikingVw, viking_toVw, viking_fromVw);
@@ -72,7 +71,7 @@ int main()
VikingVw einar = {"Einar", "Norway"};
cmap_vk_entry_t *e = cmap_vk_find(&vikings, einar);
e->value += 5; // update
- cmap_vk_emplace(&vikings, einar, 0).item->value += 5; // again
+ cmap_vk_insert(&vikings, einar, 0).item->value += 5; // again
c_foreach (k, cmap_vk, vikings) {
printf("%s of %s has %d hp\n", k.item->key.name.str, k.item->key.country.str, k.item->value);
diff --git a/examples/benchmark.c b/examples/benchmark.c
index 15d659ff..99e06fee 100644
--- a/examples/benchmark.c
+++ b/examples/benchmark.c
@@ -36,7 +36,7 @@ crand_rng64_t rng;
#define CMAP_SETUP(tt, Key, Value) cmap_##tt map = cmap_ini \
; cmap_##tt##_set_load_factors(&map, max_load_factor, 0.0)
-#define CMAP_PUT(tt, key, val) cmap_##tt##_put(&map, key, val)->value
+#define CMAP_PUT(tt, key, val) cmap_##tt##_put(&map, key, val).item->value
#define CMAP_EMPLACE(tt, key, val) cmap_##tt##_emplace(&map, key, val)
#define CMAP_ERASE(tt, key) cmap_##tt##_erase(&map, key)
#define CMAP_FIND(tt, key) (cmap_##tt##_find(map, key) != NULL)
@@ -130,7 +130,7 @@ crand_rng64_t rng;
#define SMAP_DTOR(tt) UMAP_DTOR(tt)
enum {
- FAC = 5,
+ FAC = 3,
N1 = 10000000 * FAC,
N2 = 10000000 * FAC,
N3 = 10000000 * FAC,
@@ -218,13 +218,13 @@ int main(int argc, char* argv[])
seed = time(NULL);
printf("\nRandom keys are in range [0, 2^%d), seed = %zu:\n", rr, seed);
printf("\nUnordered maps: %d repeats of Insert random key + try to remove a random key:\n", N1);
- //RUN_TEST(1)
+ RUN_TEST(1)
printf("\nUnordered maps: Insert %d index keys, then remove them in same order:\n", N2);
- //RUN_TEST(2)
+ RUN_TEST(2)
printf("\nUnordered maps: Insert %d random keys, then remove them in same order:\n", N3);
- //RUN_TEST(3)
+ RUN_TEST(3)
printf("\nUnordered maps: Iterate %d random keys:\n", N4);
RUN_TEST(4)
diff --git a/examples/demos.c b/examples/demos.c
index 21d9efaa..8ff72840 100644
--- a/examples/demos.c
+++ b/examples/demos.c
@@ -90,7 +90,7 @@ void listdemo1()
c_foreach (i, clist_ix, nums)
printf("spliced: %d\n", i.item->value);
- *clist_ix_find(&nums, 100) *= 10;
+ clist_ix_find(&nums, 100).item->value *= 10;
clist_ix_sort(&nums); // Sort the array
clist_ix_remove(&nums, 105);
clist_ix_pop_front(&nums);
diff --git a/examples/list.c b/examples/list.c
index 4eea5620..24dfc82f 100644
--- a/examples/list.c
+++ b/examples/list.c
@@ -31,8 +31,14 @@ int main() {
clist_fx_insert_after(&list, clist_fx_before_begin(&list), 5); // same as push_front()
clist_fx_push_back(&list, 500);
clist_fx_push_front(&list, 1964);
+ clist_fx_iter_t it = clist_fx_before_begin(&list);
+ printf("Full: ");
c_foreach (i, clist_fx, list)
printf(" %g", i.item->value);
+ for (int i=0; i<4; ++i) clist_fx_next(&it);
+ printf("\nSubs: ");
+ c_foreach (i, clist_fx, it, clist_fx_end(&list))
+ printf(" %g", i.item->value);
puts("");
clist_fx_destroy(&list);
} \ No newline at end of file