From 5cf6b762012168be51b32a1a85ab2bc33504f020 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Thu, 11 Aug 2022 17:58:15 +0200 Subject: Fixed issue with cbox / carc. Minor update some examples. --- examples/music_arc.c | 37 +++++++++++---------- examples/prime.c | 7 ++++ examples/rawptr_elements.c | 82 ++++++++++++++++++++-------------------------- 3 files changed, 61 insertions(+), 65 deletions(-) (limited to 'examples') 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 #define i_type SongVec -#define i_val_arcbox SongPtr +#define i_val_arcbox SongArc #include 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 #include -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 - #include -// 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 -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 // - 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 - 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(""); } } -- cgit v1.2.3