diff options
| author | Tyge Løvset <[email protected]> | 2022-08-11 17:58:15 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2022-08-11 17:58:15 +0200 |
| commit | 5cf6b762012168be51b32a1a85ab2bc33504f020 (patch) | |
| tree | 9fd59787a0d33d23196b77cecbd31ef5d44e103a /examples/rawptr_elements.c | |
| parent | 9831e8d6ee6772a4f9899cf9e3d36e3de47bbaf5 (diff) | |
| download | STC-modified-5cf6b762012168be51b32a1a85ab2bc33504f020.tar.gz STC-modified-5cf6b762012168be51b32a1a85ab2bc33504f020.zip | |
Fixed issue with cbox / carc. Minor update some examples.
Diffstat (limited to 'examples/rawptr_elements.c')
| -rw-r--r-- | examples/rawptr_elements.c | 82 |
1 files changed, 35 insertions, 47 deletions
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(""); } } |
