summaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2022-08-11 17:58:15 +0200
committerTyge Løvset <[email protected]>2022-08-11 17:58:15 +0200
commit5cf6b762012168be51b32a1a85ab2bc33504f020 (patch)
tree9fd59787a0d33d23196b77cecbd31ef5d44e103a /examples
parent9831e8d6ee6772a4f9899cf9e3d36e3de47bbaf5 (diff)
downloadSTC-modified-5cf6b762012168be51b32a1a85ab2bc33504f020.tar.gz
STC-modified-5cf6b762012168be51b32a1a85ab2bc33504f020.zip
Fixed issue with cbox / carc. Minor update some examples.
Diffstat (limited to 'examples')
-rw-r--r--examples/music_arc.c37
-rw-r--r--examples/prime.c7
-rw-r--r--examples/rawptr_elements.c82
3 files changed, 61 insertions, 65 deletions
diff --git a/examples/music_arc.c b/examples/music_arc.c
index ac730bc3..8cbcbb59 100644
--- a/examples/music_arc.c
+++ b/examples/music_arc.c
@@ -8,7 +8,7 @@ struct Song
cstr title;
} typedef Song;
-Song Song_new(const char* artist, const char* title)
+Song Song_from(const char* artist, const char* title)
{ return (Song){cstr_from(artist), cstr_from(title)}; }
void Song_drop(Song* s) {
@@ -16,39 +16,40 @@ void Song_drop(Song* s) {
c_drop(cstr, &s->artist, &s->title);
}
-#define i_type SongPtr
+#define i_type SongArc
#define i_val Song
#define i_valdrop Song_drop
#define i_opt c_no_cmp
#include <stc/carc.h>
#define i_type SongVec
-#define i_val_arcbox SongPtr
+#define i_val_arcbox SongArc
#include <stc/cvec.h>
void example3()
{
c_auto (SongVec, vec, vec2)
{
- c_forarray (SongPtr, v, {
- SongPtr_make(Song_new("Bob Dylan", "The Times They Are A Changing")),
- SongPtr_make(Song_new("Aretha Franklin", "Bridge Over Troubled Water")),
- SongPtr_make(Song_new("Thalia", "Entre El Mar y Una Estrella"))
+ c_forarray (SongArc, v, {
+ SongArc_make(Song_from("Bob Dylan", "The Times They Are A Changing")),
+ SongArc_make(Song_from("Aretha Franklin", "Bridge Over Troubled Water")),
+ SongArc_make(Song_from("Thalia", "Entre El Mar y Una Estrella"))
}) SongVec_push_back(&vec, *v);
c_foreach (s, SongVec, vec)
if (!cstr_equals(s.ref->get->artist, "Bob Dylan"))
- SongVec_push_back(&vec2, SongPtr_clone(*s.ref));
-
- c_forarray (SongPtr, v, {
- SongPtr_make(Song_new("Michael Jackson", "Billie Jean")),
- SongPtr_make(Song_new("Rihanna", "Stay")),
- }) SongVec_push_back(&vec2, *v);
-
- c_foreach (s, SongVec, vec2)
- printf("%s - %s: refs %lu\n", cstr_str(&s.ref->get->artist),
- cstr_str(&s.ref->get->title),
- *s.ref->use_count);
+ SongVec_push_back(&vec2, SongArc_clone(*s.ref));
+
+ SongVec_push_back(&vec2, SongArc_make(Song_from("Michael Jackson", "Billie Jean")));
+ SongVec_push_back(&vec2, SongArc_make(Song_from("Rihanna", "Stay")));
+
+ c_forarray (SongVec, v, {vec, vec2}) {
+ puts("VEC:");
+ c_foreach (s, SongVec, *v)
+ printf(" %s - %s, REFS: %lu\n", cstr_str(&s.ref->get->artist),
+ cstr_str(&s.ref->get->title),
+ *s.ref->use_count);
+ }
}
}
diff --git a/examples/prime.c b/examples/prime.c
index 01a6800b..85a66ee5 100644
--- a/examples/prime.c
+++ b/examples/prime.c
@@ -37,5 +37,12 @@ int main(void)
for (size_t i = 3; i < 1000; i += 2)
if (cbits_test(&primes, i>>1)) printf(" %" PRIuMAX "", i);
puts("");
+
+ int k = 20;
+ c_forrange (intptr_t, i, n-1, 1, -2) {
+ if (k == 0) break;
+ else if (cbits_test(&primes, i>>1)) printf("%" PRIdMAX "\n", i), k--;
+ }
+ puts("");
}
}
diff --git a/examples/rawptr_elements.c b/examples/rawptr_elements.c
index c3e3188d..bae314fd 100644
--- a/examples/rawptr_elements.c
+++ b/examples/rawptr_elements.c
@@ -1,67 +1,55 @@
#include <stc/ccommon.h>
#include <stdio.h>
-struct { double x, y; } typedef Point;
-
-// Set of Point pointers: define all template parameters "in-line"
-// Note it may be simpler to use a cbox for this.
-#define i_key Point*
-#define i_keydrop(x) c_free(*(x))
-#define i_keyclone(x) c_new(Point, *(x))
-#define i_hash(x) c_default_hash(*(x))
-#define i_cmp(x, y) memcmp(*(x), *(y), sizeof **(x)) // not good!
-#define i_tag pnt
-#include <stc/cset.h>
-
#include <stc/cstr.h>
-// Map of int64 pointers: Define i_valraw as int64_t for easy emplace calls!
+// Map of cstr => int64 pointers
typedef int64_t inttype;
+
+// Do it without cbox:
+#define i_type SIPtrMap
#define i_key_str
#define i_val inttype*
#define i_valraw inttype
-#define i_valfrom(raw) (puts("from"), c_new(inttype, raw))
-#define i_valto(x) (puts("to"), **(x))
-#define i_valclone c_derived_valclone // enables clone via valto+valfrom
-#define i_valdrop(x) c_free(*(x))
+#define i_valfrom(raw) c_new(inttype, raw)
+#define i_valto(x) **x
+#define i_valclone(x) c_new(inttype, *x)
+#define i_valdrop(x) c_free(*x)
#include <stc/cmap.h>
-int main()
-{
- c_auto (cset_pnt, set, cpy)
- {
- printf("Set with pointer elements:\n");
- // c++: set.insert(new Point{1.2, 3.4});
- cset_pnt_insert(&set, c_new(Point, {1.2, 3.4}));
- Point* q = *cset_pnt_insert(&set, c_new(Point, {6.1, 4.7})).ref;
- cset_pnt_insert(&set, c_new(Point, {5.7, 2.3}));
-
- cpy = cset_pnt_clone(set);
- cset_pnt_erase(&cpy, q);
+// With cbox:
+#define i_type IBox
+#define i_val int
+#include <stc/cbox.h> //<stc/carc.h>
- printf("set:");
- c_foreach (i, cset_pnt, set)
- printf(" (%g %g)", i.ref[0]->x, i.ref[0]->y);
-
- printf("\ncpy:");
- c_foreach (i, cset_pnt, cpy)
- printf(" (%g %g)", i.ref[0]->x, i.ref[0]->y);
- puts("");
- }
+#define i_type SIBoxMap
+#define i_key_str
+#define i_val_arcbox IBox
+#include <stc/cmap.h>
- c_auto (cmap_str, map, m2)
+int main()
+{
+ c_auto (SIPtrMap, map, m1)
+ c_auto (SIBoxMap, m2)
{
printf("\nMap with pointer elements:\n");
- cmap_str_insert(&map, cstr_new("testing"), c_new(inttype, 999));
- cmap_str_insert(&map, cstr_new("done"), c_new(inttype, 111));
+ SIPtrMap_insert(&map, cstr_from("testing"), c_new(inttype, 1));
+ SIPtrMap_insert(&map, cstr_from("done"), c_new(inttype, 2));
- // Emplace: implicit key, val construction using i_keyfrom/i_valfrom:
- cmap_str_emplace(&map, "hello", 200);
- cmap_str_emplace(&map, "goodbye", 400);
+ // Emplace: implicit key, val construction:
+ SIPtrMap_emplace(&map, "hello", 3);
+ SIPtrMap_emplace(&map, "goodbye", 4);
- // default uses i_valfrom+i_valto when no i_valclone defined:
- m2 = cmap_str_clone(map);
+ m1 = SIPtrMap_clone(map);
- c_forpair (name, number, cmap_str, m2)
+ c_forpair (name, number, SIPtrMap, m1)
printf("%s: %" PRIdMAX "\n", cstr_str(_.name), **_.number);
+
+
+ puts("\nIBox map:");
+ SIBoxMap_insert(&m2, cstr_from("Hello"), IBox_from(123));
+ SIBoxMap_emplace(&m2, "World", 999);
+ c_forpair (name, number, SIBoxMap, m2)
+ printf("%s: %d\n", cstr_str(_.name), *_.number->get);
+ puts("");
}
}