diff options
| author | Tyge Løvset <[email protected]> | 2022-08-17 09:35:40 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-08-17 09:35:40 +0200 |
| commit | 0681b8a9af2ac6041e937bc8ca7bc8d496dcc0d4 (patch) | |
| tree | 03825b177d4d5259f629957b751b000e1b224db1 /examples | |
| parent | 17f1d2ed83952df00525f4be1d30a6c12e04a0f6 (diff) | |
| parent | a06463c2f0747bc142a9d5b2bf455c64aaf39890 (diff) | |
| download | STC-modified-0681b8a9af2ac6041e937bc8ca7bc8d496dcc0d4.tar.gz STC-modified-0681b8a9af2ac6041e937bc8ca7bc8d496dcc0d4.zip | |
Merge pull request #34 from tylov/iter_exp_dont_use
VERSION 4.0 RC
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/arc_containers.c | 2 | ||||
| -rw-r--r-- | examples/arc_demo.c | 2 | ||||
| -rw-r--r-- | examples/arcvec_erase.c | 7 | ||||
| -rw-r--r-- | examples/astar.c | 8 | ||||
| -rw-r--r-- | examples/bits.c | 4 | ||||
| -rw-r--r-- | examples/box.c | 2 | ||||
| -rw-r--r-- | examples/city.c | 26 | ||||
| -rw-r--r-- | examples/csmap_erase.c | 5 | ||||
| -rw-r--r-- | examples/cstr_match.c | 16 | ||||
| -rw-r--r-- | examples/demos.c | 10 | ||||
| -rw-r--r-- | examples/mmap.c | 29 | ||||
| -rw-r--r-- | examples/music_arc.c | 55 | ||||
| -rw-r--r-- | examples/new_arr.c | 12 | ||||
| -rw-r--r-- | examples/person_arc.c | 2 | ||||
| -rw-r--r-- | examples/prime.c | 9 | ||||
| -rw-r--r-- | examples/rawptr_elements.c | 82 | ||||
| -rw-r--r-- | examples/read.c | 6 | ||||
| -rw-r--r-- | examples/regex1.c | 2 | ||||
| -rw-r--r-- | examples/regex_replace.c | 4 | ||||
| -rw-r--r-- | examples/replace.c | 2 | ||||
| -rw-r--r-- | examples/splitstr.c | 2 | ||||
| -rw-r--r-- | examples/sso_map.c | 2 | ||||
| -rw-r--r-- | examples/sso_substr.c | 2 | ||||
| -rw-r--r-- | examples/utf8replace_c.c | 4 |
24 files changed, 151 insertions, 144 deletions
diff --git a/examples/arc_containers.c b/examples/arc_containers.c index e8716129..debc6617 100644 --- a/examples/arc_containers.c +++ b/examples/arc_containers.c @@ -53,7 +53,7 @@ int main() // Clone (deep copy) a Map from the stack to the list // List will contain two shared and two unshared maps. - map = List_push_back(&list, Arc_make(Map_clone(*stack.data[1].get)))->get; + map = List_push_back(&list, Arc_from(Map_clone(*stack.data[1].get)))->get; // Add one more element to the cloned map: Map_emplace_or_assign(map, "CLONED", 2021); diff --git a/examples/arc_demo.c b/examples/arc_demo.c index 85e3886f..688fe72f 100644 --- a/examples/arc_demo.c +++ b/examples/arc_demo.c @@ -47,7 +47,7 @@ int main() printf("\nset:"); c_foreach (i, csset_Arc, set) printf(" %d", *i.ref->get); - c_autovar (Arc p = Arc_clone(vec.data[0]), Arc_drop(&p)) { + c_with (Arc p = Arc_clone(vec.data[0]), Arc_drop(&p)) { printf("\n%d is now owned by %ld objects\n", *p.get, *p.use_count); } diff --git a/examples/arcvec_erase.c b/examples/arcvec_erase.c index eba77f51..b96f4278 100644 --- a/examples/arcvec_erase.c +++ b/examples/arcvec_erase.c @@ -20,13 +20,12 @@ int main() { c_auto (Vec, vec) { - const int v[] = {2012, 1990, 2012, 2019, 2015}; - c_forrange (i, c_arraylen(v)) - Vec_push_back(&vec, Arc_make(v[i])); + c_forarray (int, v, {2012, 1990, 2012, 2019, 2015}) + Vec_emplace(&vec, *v); // clone the second 2012 and push it back. // note: cloning make sure that vec.data[2] has ref count 2. - Vec_push_back(&vec, Arc_clone(vec.data[2])); + Vec_push(&vec, Arc_clone(vec.data[2])); printf("vec before erase :"); c_foreach (i, Vec, vec) diff --git a/examples/astar.c b/examples/astar.c index 7d90bf35..4d9f2469 100644 --- a/examples/astar.c +++ b/examples/astar.c @@ -37,7 +37,7 @@ point_equal(const point* a, const point* b) point point_from(const cstr* maze, const char* c, int width) { - int index = cstr_find(*maze, c); + int index = cstr_find(maze, c); return point_init(index % width, index / width, width); } @@ -131,7 +131,7 @@ astar(cstr* maze, int width) int main(void) { - c_autovar (cstr maze = cstr_new( + c_with (cstr maze = cstr_new( "#########################################################################\n" "# # # # # # #\n" "# # ######### # ##### ######### ##### ##### ##### # ! #\n" @@ -156,8 +156,8 @@ main(void) "# # # # # # #\n" "#########################################################################\n"), cstr_drop(&maze)) { - int width = cstr_find(maze, "\n") + 1; - c_autovar (cdeq_point path = astar(&maze, width), cdeq_point_drop(&path)) + int width = cstr_find(&maze, "\n") + 1; + c_with (cdeq_point path = astar(&maze, width), cdeq_point_drop(&path)) { c_foreach (it, cdeq_point, path) cstr_data(&maze)[point_index(it.ref)] = 'x'; printf("%s", cstr_str(&maze)); diff --git a/examples/bits.c b/examples/bits.c index 8cce573e..c6e70517 100644 --- a/examples/bits.c +++ b/examples/bits.c @@ -3,7 +3,7 @@ int main() { - c_autovar (cbits set = cbits_with_size(23, true), cbits_drop(&set)) { + c_with (cbits set = cbits_with_size(23, true), cbits_drop(&set)) { printf("count %" PRIuMAX ", %" PRIuMAX "\n", cbits_count(&set), cbits_size(&set)); cbits s1 = cbits_from("1110100110111"); char buf[256]; @@ -36,7 +36,7 @@ int main() printf("%d", cbits_test(&set, i)); puts(""); - c_autovar (cbits s2 = cbits_clone(set), cbits_drop(&s2)) { + c_with (cbits s2 = cbits_clone(set), cbits_drop(&s2)) { cbits_flip_all(&s2); cbits_set(&s2, 16); cbits_set(&s2, 17); diff --git a/examples/box.c b/examples/box.c index c7e649bf..b12f1f71 100644 --- a/examples/box.c +++ b/examples/box.c @@ -59,7 +59,7 @@ int main() puts(""); // Look-up Audrey! Create a temporary Person for lookup. - c_autovar (Person a = Person_new("Audrey", "Home"), Person_drop(&a)) { + c_with (Person a = Person_new("Audrey", "Home"), Person_drop(&a)) { const PBox *v = Persons_get(&vec, a); // lookup if (v) printf("found: %s %s\n", cstr_str(&v->get->name), cstr_str(&v->get->last)); } diff --git a/examples/city.c b/examples/city.c index c22693f9..c6a9417f 100644 --- a/examples/city.c +++ b/examples/city.c @@ -32,8 +32,9 @@ static inline void City_drop(City* c) { #define i_type CityArc #define i_key_bind City -#include <stc/cbox.h> -//#include <stc/carc.h> // try instead of cbox.h +#define i_opt c_no_atomic +//#include <stc/cbox.h> +#include <stc/carc.h> // try instead of cbox.h #define i_type Cities #define i_key_arcbox CityArc @@ -50,16 +51,13 @@ int main(void) c_auto (Cities, cities, copy) c_auto (CityMap, map) { - struct City_s { const char *name, *country; float lat, lon; int pop; }; - - c_forarray (struct City_s, c, { - {"New York", "US", 4.3, 23.2, 9000000}, - {"Paris", "France", 4.3, 23.2, 9000000}, - {"Berlin", "Germany", 4.3, 23.2, 9000000}, - {"London", "UK", 4.3, 23.2, 9000000}, + c_forarray (City, c, { + {cstr_new("New York"), cstr_new("US"), 4.3, 23.2, 9000000}, + {cstr_new("Paris"), cstr_new("France"), 4.3, 23.2, 9000000}, + {cstr_new("Berlin"), cstr_new("Germany"), 4.3, 23.2, 9000000}, + {cstr_new("London"), cstr_new("UK"), 4.3, 23.2, 9000000}, }) { - Cities_push(&cities, CityArc_make((City){cstr_from(c->name), cstr_from(c->country), - c->lat, c->lon, c->pop})); + Cities_emplace(&cities, *c); } copy = Cities_clone(cities); // share each element! @@ -73,12 +71,14 @@ int main(void) printf("Vec:\n"); c_foreach (c, Cities, cities) - printf("city:%s, %d, use:%ld\n", cstr_str(&c.ref->get->name), c.ref->get->population, CityArc_use_count(*c.ref)); + printf("city:%s, %d, use:%ld\n", cstr_str(&c.ref->get->name), + c.ref->get->population, + CityArc_use_count(c.ref)); printf("\nMap:\n"); c_forpair (id, city, CityMap, map) printf("id:%d, city:%s, %d, use:%ld\n", *_.id, cstr_str(&_.city->get->name), - _.city->get->population, CityArc_use_count(*_.city)); + _.city->get->population, CityArc_use_count(_.city)); puts(""); } } diff --git a/examples/csmap_erase.c b/examples/csmap_erase.c index 1c533a99..cbcc2607 100644 --- a/examples/csmap_erase.c +++ b/examples/csmap_erase.c @@ -49,6 +49,11 @@ int main() printmap(m2); mymap_iter it1 = mymap_advance(mymap_begin(&m2), 1); mymap_iter it2 = mymap_find(&m2, mymap_back(&m2)->first); + + puts("to remove:"); + c_foreach (i, mymap, it1, it2) + printf(" [%d, %s]", i.ref->first, cstr_str(&i.ref->second)); + puts(""); // The 2nd member function removes elements // in the range [First, Last) mymap_erase_range(&m2, it1, it2); diff --git a/examples/cstr_match.c b/examples/cstr_match.c index 6927ed80..614af490 100644 --- a/examples/cstr_match.c +++ b/examples/cstr_match.c @@ -4,19 +4,19 @@ int main() { - c_autovar (cstr ss = cstr_new("The quick brown fox jumps over the lazy dog.JPG"), cstr_drop(&ss)) { - size_t pos = cstr_find_at(ss, 0, "brown"); + c_with (cstr ss = cstr_new("The quick brown fox jumps over the lazy dog.JPG"), cstr_drop(&ss)) { + size_t pos = cstr_find_at(&ss, 0, "brown"); printf("%" PRIuMAX " [%s]\n", pos, pos == cstr_npos ? "<NULL>" : cstr_str(&ss) + pos); - printf("equals: %d\n", cstr_equals(ss, "The quick brown fox jumps over the lazy dog.JPG")); - printf("contains: %d\n", cstr_contains(ss, "umps ove")); - printf("starts_with: %d\n", cstr_starts_with(ss, "The quick brown")); - printf("ends_with: %d\n", cstr_ends_with(ss, ".jpg")); - printf("ends_with: %d\n", cstr_ends_with(ss, ".JPG")); + printf("equals: %d\n", cstr_equals(&ss, "The quick brown fox jumps over the lazy dog.JPG")); + printf("contains: %d\n", cstr_contains(&ss, "umps ove")); + printf("starts_with: %d\n", cstr_starts_with(&ss, "The quick brown")); + printf("ends_with: %d\n", cstr_ends_with(&ss, ".jpg")); + printf("ends_with: %d\n", cstr_ends_with(&ss, ".JPG")); cstr s1 = cstr_new("hell😀 w😀rl🐨"); csview ch1 = cstr_u8_chr(&s1, 7); csview ch2 = cstr_u8_chr(&s1, 10); - printf("%s\nsize: %" PRIuMAX ", %" PRIuMAX "\n", cstr_str(&s1), cstr_u8_size(s1), cstr_size(s1)); + printf("%s\nsize: %" PRIuMAX ", %" PRIuMAX "\n", cstr_str(&s1), cstr_u8_size(&s1), cstr_size(&s1)); printf("ch1: %.*s\n", c_ARGsv(ch1)); printf("ch2: %.*s\n", c_ARGsv(ch2)); } diff --git a/examples/demos.c b/examples/demos.c index 052c4e32..331ef04f 100644 --- a/examples/demos.c +++ b/examples/demos.c @@ -3,7 +3,7 @@ void stringdemo1() { printf("\nSTRINGDEMO1\n"); - c_autovar (cstr cs = cstr_new("one-nine-three-seven-five"), cstr_drop(&cs)) + c_with (cstr cs = cstr_new("one-nine-three-seven-five"), cstr_drop(&cs)) { printf("%s.\n", cstr_str(&cs)); @@ -19,7 +19,7 @@ void stringdemo1() cstr_take(&cs, cstr_from_fmt("%s *** %s", cstr_str(&cs), cstr_str(&cs))); printf("%s.\n", cstr_str(&cs)); - printf("find \"four\": %s\n", cstr_str(&cs) + cstr_find(cs, "four")); + printf("find \"four\": %s\n", cstr_str(&cs) + cstr_find(&cs, "four")); // reassign: cstr_assign(&cs, "one two three four five six seven"); @@ -35,7 +35,7 @@ void stringdemo1() void vectordemo1() { printf("\nVECTORDEMO1\n"); - c_autovar (cvec_ix bignums = cvec_ix_with_capacity(100), cvec_ix_drop(&bignums)) + c_with (cvec_ix bignums = cvec_ix_with_capacity(100), cvec_ix_drop(&bignums)) { cvec_ix_reserve(&bignums, 100); for (size_t i = 10; i <= 100; i += 10) @@ -192,8 +192,8 @@ void mapdemo3() void arraydemo1() { printf("\nARRAYDEMO1\n"); - c_autovar (carr3_f arr3 = carr3_f_with_size(30, 20, 10, 0.0f), - carr3_f_drop(&arr3)) + c_with (carr3_f arr3 = carr3_f_with_size(30, 20, 10, 0.0f), + carr3_f_drop(&arr3)) { arr3.data[5][4][3] = 10.2f; float **arr2 = arr3.data[5]; diff --git a/examples/mmap.c b/examples/mmap.c index 5ca2f92a..ed78d2af 100644 --- a/examples/mmap.c +++ b/examples/mmap.c @@ -4,6 +4,7 @@ // Multimap entries #include <stc/cstr.h> #define i_val_str +//#define i_valdrop(x) (printf("drop %s\n", cstr_str(x)), cstr_drop(x)) #define i_extern // define _clist_mergesort() once #include <stc/clist.h> @@ -34,10 +35,11 @@ int main() { c_auto (Multimap, mmap) { + typedef struct {int a; const char* b;} pair; + // list-initialize - struct { int first; const char* second; } vals[] = - {{2, "foo"}, {2, "bar"}, {3, "baz"}, {1, "abc"}, {5, "def"}}; - c_forrange (i, c_arraylen(vals)) insert(&mmap, c_pair(&vals[i])); + c_forarray (pair, v, {{2, "foo"}, {2, "bar"}, {3, "baz"}, {1, "abc"}, {5, "def"}}) + insert(&mmap, v->a, v->b); print("#1", mmap); // insert using value_type @@ -52,24 +54,19 @@ int main() print("#4", mmap); // insert using initialization_list - insert(&mmap, 5, "one"); - insert(&mmap, 5, "two"); + c_forarray (pair, v, {{5, "one"}, {5, "two"}}) + insert(&mmap, v->a, v->b); print("#5", mmap); // FOLLOWING NOT IN ORIGINAL EXAMPLE: - // erase all entries with key 5 Multimap_erase(&mmap, 5); - print("+6", mmap); + print("+5", mmap); + - // find and erase first entry containing "bar" - clist_str_iter pos; - c_foreach (e, Multimap, mmap) { - if ((pos = clist_str_find(&e.ref->second, "bar")).ref != clist_str_end(&e.ref->second).ref) { - clist_str_erase_at(&e.ref->second, pos); - break; - } - } - print("+7", mmap); + Multimap_clear(&mmap); + c_forarray (pair, v, {{1, "ä"}, {2, "ё"}, {2, "ö"}, {3, "ü"}}) + insert(&mmap, v->a, v->b); + print("#6", mmap); } } diff --git a/examples/music_arc.c b/examples/music_arc.c index ac730bc3..6f9c1c72 100644 --- a/examples/music_arc.c +++ b/examples/music_arc.c @@ -8,7 +8,10 @@ struct Song cstr title; } typedef Song; -Song Song_new(const char* artist, const char* title) +int Song_cmp(const Song* x, const Song* y) + { return cstr_cmp(&x->title, &y->title); } + +Song Song_from(const char* artist, const char* title) { return (Song){cstr_from(artist), cstr_from(title)}; } void Song_drop(Song* s) { @@ -16,40 +19,48 @@ void Song_drop(Song* s) { c_drop(cstr, &s->artist, &s->title); } -#define i_type SongPtr +// Define the reference counted type +#define i_type SongArc #define i_val Song #define i_valdrop Song_drop -#define i_opt c_no_cmp +#define i_cmp Song_cmp #include <stc/carc.h> +// ... and a vector of it #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")) - }) SongVec_push_back(&vec, *v); + c_forarray (Song, v, { + Song_from("Bob Dylan", "The Times They Are A Changing"), + Song_from("Aretha Franklin", "Bridge Over Troubled Water"), + Song_from("Thalia", "Entre El Mar y Una Estrella") + }) SongVec_emplace(&vec, *v); + // Share all entries in vec with vec2, except Bob Dylan. 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); - } + if (!cstr_equals(&s.ref->get->artist, "Bob Dylan")) + SongVec_push(&vec2, SongArc_clone(*s.ref)); + + // Add a few more to vec2. We can use emplace when creating new entries + SongVec_emplace(&vec2, Song_from("Michael Jackson", "Billie Jean")); + SongVec_emplace(&vec2, Song_from("Rihanna", "Stay")); + // If we use push, we would need to construct the Arc explicitly (as in c++, make_shared): + // SongVec_push(&vec2, SongArc_from(Song_from("Rihanna", "Stay"))); + + // We now have two vectors with some shared, some unique entries. + 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); + } + } // because the shared elem. are ref. counted, they are only dropped once here. } int main() diff --git a/examples/new_arr.c b/examples/new_arr.c index 17a96062..598e5323 100644 --- a/examples/new_arr.c +++ b/examples/new_arr.c @@ -13,8 +13,8 @@ int main() { int w = 7, h = 5, d = 3; - c_autovar (carr2_int volume = carr2_int_new_uninit(w, h), - carr2_int_drop(&volume)) + c_with (carr2_int volume = carr2_int_new_uninit(w, h), + carr2_int_drop(&volume)) { int *dat = carr2_int_data(&volume); for (size_t i = 0; i < carr2_int_size(&volume); ++i) @@ -30,8 +30,8 @@ int main() puts("\n"); } - c_autovar (carr3_int volume = carr3_int_new_uninit(w, h, d), - carr3_int_drop(&volume)) + c_with (carr3_int volume = carr3_int_new_uninit(w, h, d), + carr3_int_drop(&volume)) { int *dat = carr3_int_data(&volume); for (size_t i = 0; i < carr3_int_size(&volume); ++i) @@ -48,8 +48,8 @@ int main() puts(""); } - c_autovar (carr2_str text2d = carr2_str_with_size(h, d, cstr_init()), - carr2_str_drop(&text2d)) + c_with (carr2_str text2d = carr2_str_with_size(h, d, cstr_init()), + carr2_str_drop(&text2d)) { cstr_assign(&text2d.data[2][1], "hello"); cstr_assign(&text2d.data[4][0], "world"); diff --git a/examples/person_arc.c b/examples/person_arc.c index 272a3f72..bddf7bd6 100644 --- a/examples/person_arc.c +++ b/examples/person_arc.c @@ -62,7 +62,7 @@ int main() puts(""); // Look-up Audrey! - c_autovar (Person a = Person_new("Audrey", "Home"), Person_drop(&a)) { + c_with (Person a = Person_new("Audrey", "Home"), Person_drop(&a)) { const PSPtr *v = Persons_get(&vec, a); if (v) printf("found: %s %s\n", cstr_str(&v->get->name), cstr_str(&v->get->last)); } diff --git a/examples/prime.c b/examples/prime.c index 01a6800b..7af66f33 100644 --- a/examples/prime.c +++ b/examples/prime.c @@ -27,7 +27,7 @@ int main(void) printf("computing prime numbers up to %" PRIuMAX "\n", n); clock_t t1 = clock(); - c_autovar (cbits primes = sieveOfEratosthenes(n + 1), cbits_drop(&primes)) { + c_with (cbits primes = sieveOfEratosthenes(n + 1), cbits_drop(&primes)) { puts("done"); size_t np = cbits_count(&primes); clock_t t2 = clock(); @@ -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(""); } } diff --git a/examples/read.c b/examples/read.c index 26fc46dd..5a9a30d5 100644 --- a/examples/read.c +++ b/examples/read.c @@ -6,8 +6,8 @@ cvec_str read_file(const char* name) { cvec_str vec = cvec_str_init(); - c_autovar (FILE* f = fopen(name, "r"), fclose(f)) - c_autovar (cstr line = cstr_init(), cstr_drop(&line)) + c_with (FILE* f = fopen(name, "r"), fclose(f)) + c_with (cstr line = cstr_null, cstr_drop(&line)) while (cstr_getline(&line, f)) cvec_str_push(&vec, cstr_clone(line)); return vec; @@ -16,7 +16,7 @@ cvec_str read_file(const char* name) int main() { int n = 0; - c_autovar (cvec_str vec = read_file(__FILE__), cvec_str_drop(&vec)) + c_with (cvec_str vec = read_file(__FILE__), cvec_str_drop(&vec)) c_foreach (i, cvec_str, vec) printf("%5d: %s\n", ++n, cstr_str(i.ref)); diff --git a/examples/regex1.c b/examples/regex1.c index 5981e878..c8b3a4f5 100644 --- a/examples/regex1.c +++ b/examples/regex1.c @@ -19,7 +19,7 @@ int main(int argc, char* argv[]) cstr_getline(&input, stdin); // Exit when the user inputs q - if (cstr_equals(input, "q")) + if (cstr_equals(&input, "q")) break; if (cregex_is_match(cstr_str(&input), &float_expr)) diff --git a/examples/regex_replace.c b/examples/regex_replace.c index 1b140676..8640ced1 100644 --- a/examples/regex_replace.c +++ b/examples/regex_replace.c @@ -34,9 +34,9 @@ int main() printf("brack: %s\n", cstr_str(&str)); /* Shows how to compile RE separately */ - c_autovar (cregex re = cregex_from(pattern, 0), cregex_drop(&re)) { + c_with (cregex re = cregex_from(pattern, 0), cregex_drop(&re)) { if (cregex_captures(&re) == 0) - continue; + continue; // break c_with /* European date format. */ cstr_take(&str, cregex_replace(input, &re, "$3.$2.$1", 0)); printf("euros: %s\n", cstr_str(&str)); diff --git a/examples/replace.c b/examples/replace.c index 13b6eaaf..c22c71ff 100644 --- a/examples/replace.c +++ b/examples/replace.c @@ -11,7 +11,7 @@ int main () // Ustring positions: 0123456789*123456789*12345 cstr s = cstr_from(base); // "this is a test string." cstr m = cstr_clone(s); - c_autodefer (cstr_drop(&s), cstr_drop(&m)) { + c_defer (cstr_drop(&s), cstr_drop(&m)) { cstr_append(&m, cstr_str(&m)); cstr_append(&m, cstr_str(&m)); printf("%s\n", cstr_str(&m)); diff --git a/examples/splitstr.c b/examples/splitstr.c index 68c36291..c483fbe0 100644 --- a/examples/splitstr.c +++ b/examples/splitstr.c @@ -33,7 +33,7 @@ int main() print_split(c_sv("This has no matching separator"), c_sv("xx")); puts(""); - c_autovar (cstack_str s = string_split(c_sv("Split,this,,string,now,"), c_sv(",")), cstack_str_drop(&s)) + c_with (cstack_str s = string_split(c_sv("Split,this,,string,now,"), c_sv(",")), cstack_str_drop(&s)) c_foreach (i, cstack_str, s) printf("[%s]\n", cstr_str(i.ref)); } diff --git a/examples/sso_map.c b/examples/sso_map.c index a32a9a3d..43bcb40b 100644 --- a/examples/sso_map.c +++ b/examples/sso_map.c @@ -11,7 +11,7 @@ int main() c_forpair (k, v, cmap_str, m) printf("%s: '%s' Len=%" PRIuMAX ", Is long: %s\n", - cstr_str(_.k), cstr_str(_.v), cstr_size(*_.v), + cstr_str(_.k), cstr_str(_.v), cstr_size(_.v), cstr_is_long(_.v)?"true":"false"); } } diff --git a/examples/sso_substr.c b/examples/sso_substr.c index b47512ea..098d9b4b 100644 --- a/examples/sso_substr.c +++ b/examples/sso_substr.c @@ -6,7 +6,7 @@ int main () { cstr str = cstr_new("We think in generalities, but we live in details."); csview sv1 = cstr_substr_ex(&str, 3, 5); // "think" - size_t pos = cstr_find(str, "live"); // position of "live" + size_t pos = cstr_find(&str, "live"); // position of "live" csview sv2 = cstr_substr_ex(&str, pos, 4); // "live" csview sv3 = cstr_slice_ex(&str, -8, -1); // "details" printf("%.*s, %.*s, %.*s\n", c_ARGsv(sv1), c_ARGsv(sv2), c_ARGsv(sv3)); diff --git a/examples/utf8replace_c.c b/examples/utf8replace_c.c index 792654b6..e7659cfd 100644 --- a/examples/utf8replace_c.c +++ b/examples/utf8replace_c.c @@ -8,7 +8,7 @@ int main() { printf("%s\n", cstr_str(&hello)); /* replace second smiley at utf8 codepoint pos 7 */ - cstr_u8_replace_at(&hello, 7, 1, c_sv("🐨")); + cstr_u8_replace(&hello, cstr_find(&hello, "😀rld"), 1, c_sv("🐨")); printf("%s\n", cstr_str(&hello)); @@ -16,7 +16,7 @@ int main() { printf("%s\n", cstr_str(&hello)); c_foreach (c, cstr, hello) - printf("%.*s,", c_ARGsv(c.chr)); + printf("%.*s,", c_ARGsv(c.u8.chr)); puts(""); } } |
