summaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/new_sptr.c2
-rw-r--r--examples/sharedptr.c31
-rw-r--r--examples/sptr_ex.c10
-rw-r--r--examples/words.c2
4 files changed, 19 insertions, 26 deletions
diff --git a/examples/new_sptr.c b/examples/new_sptr.c
index 002f6ade..aad6192a 100644
--- a/examples/new_sptr.c
+++ b/examples/new_sptr.c
@@ -23,7 +23,7 @@ void Person_del(Person* p) {
#include <stc/csptr.h>
#define i_tag iptr
-#define i_key_csptr int
+#define i_key_csptr csptr_int
#include <stc/csset.h>
int main(void) {
diff --git a/examples/sharedptr.c b/examples/sharedptr.c
index e684b9a2..e062025a 100644
--- a/examples/sharedptr.c
+++ b/examples/sharedptr.c
@@ -5,20 +5,20 @@ void int_del(int* x) {
}
#define i_val int
-#define i_valdel int_del // optional func to show elements destroyed
-#define i_nonatomic // only for single thread; faster.
-#include <stc/csptr.h> // define csptr_int shared pointers
+#define i_valdel int_del // optional func to show elements destroyed
+#include <stc/csptr.h> // csptr_int: shared pointer to int
-#define i_key_csptr int // refer to csptr_int definition above
-#include <stc/csset.h> // define a sorted set of csptr_int
-
-#define i_val_csptr int
-#include <stc/cvec.h>
+#define i_key_csptr csptr_int
+#define i_tag int
+#include <stc/csset.h> // csset_int: csset<csptr_int>
+#define i_val_csptr csptr_int
+#define i_tag int
+#include <stc/cvec.h> // cvec_int: cvec<csptr_int>
int main()
{
- c_auto (cvec_int, vec) // declare and init vec, call del at scope exit
- c_auto (csset_int, set) // declare and init set, call del at scope exit
+ c_auto (cvec_int, vec)
+ c_auto (csset_int, set) // declare and init set, call del at scope exit
{
cvec_int_push_back(&vec, csptr_int_make(2021));
cvec_int_push_back(&vec, csptr_int_make(2012));
@@ -51,14 +51,3 @@ int main()
puts("Done");
}
}
-
-/* output:
-vec: 2021 2012 2022 2015
-del: 2022
-vec: 2021 2012
-set: 2015 2021
-Done
-del: 2015
-del: 2021
-del: 2012
-*/
diff --git a/examples/sptr_ex.c b/examples/sptr_ex.c
index 2464ed79..184acd05 100644
--- a/examples/sptr_ex.c
+++ b/examples/sptr_ex.c
@@ -33,7 +33,8 @@ void Song_del(Song* s) {
#define i_del Song_del
#include <stc/csptr.h>
-#define i_val_csptr song
+#define i_val_csptr csptr_song
+#define i_tag song
#include <stc/cvec.h>
void example3()
@@ -48,9 +49,12 @@ void example3()
c_foreach (s, cvec_song, v)
if (!cstr_equalto(s.ref->get->artist, "Bob Dylan"))
- cvec_song_emplace_back(&v2, *s.ref); // calls csptr_song_clone()
+ cvec_song_emplace_back(&v2, *s.ref); // note: calls csptr_song_clone()
- cvec_song_push_back(&v2, csptr_song_make(Song_from("Pink Floyd", "Dogs")));
+ c_apply(cvec_song, push_back, &v2, {
+ csptr_song_make(Song_from("Michael Jackson", "Billie Jean")),
+ csptr_song_make(Song_from("Rihanna", "Stay")),
+ });
c_foreach (s, cvec_song, v2)
printf("%s - %s: refs %u\n", s.ref->get->artist.str, s.ref->get->title.str,
diff --git a/examples/words.c b/examples/words.c
index 87327d71..951c3ded 100644
--- a/examples/words.c
+++ b/examples/words.c
@@ -46,7 +46,7 @@ int main2()
std::unordered_map<std::string, size_t> word_map;
for (const auto &w : words) {
- ++word_map[w];
+ word_map[w] += 1;
}
for (const auto &pair : word_map) {