diff options
| author | Tyge Løvset <[email protected]> | 2022-12-30 19:26:51 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-12-30 19:26:51 +0100 |
| commit | 9d59845f622fbb9c225bbe32246a3128ad9ec0b1 (patch) | |
| tree | 528ebcc735cdb5d958b987c886d369bc02a11320 /misc | |
| parent | 5f57d597cd27aef55adbcb3b452973b0c6e33667 (diff) | |
| parent | e124d8c7377de1dfce45790a196312596f6b9be5 (diff) | |
| download | STC-modified-9d59845f622fbb9c225bbe32246a3128ad9ec0b1.tar.gz STC-modified-9d59845f622fbb9c225bbe32246a3128ad9ec0b1.zip | |
Merge pull request #41 from tylov/allcaps
Allcaps
Diffstat (limited to 'misc')
82 files changed, 881 insertions, 730 deletions
diff --git a/misc/benchmarks/misc/rust_cmap.c b/misc/benchmarks/misc/rust_cmap.c index 83b7dd19..18635c35 100644 --- a/misc/benchmarks/misc/rust_cmap.c +++ b/misc/benchmarks/misc/rust_cmap.c @@ -24,7 +24,7 @@ uint64_t romu_trio(uint64_t s[3]) { int main() { - c_auto (cmap_u64, m) { + c_AUTO (cmap_u64, m) { const size_t n = 50000000, mask = (1 << 25) - 1, ms = CLOCKS_PER_SEC/1000; @@ -33,7 +33,7 @@ int main() uint64_t rng[3] = {1872361123, 123879177, 87739234}, sum; clock_t now = clock(); - c_forrange (n) { + c_FORRANGE (n) { uint64_t key = romu_trio(rng) & mask; cmap_u64_insert(&m, key, 0).ref->second += 1; } @@ -41,17 +41,17 @@ int main() now = clock(); sum = 0; - c_forrange (key, mask + 1) { sum += cmap_u64_contains(&m, key); } + c_FORRANGE (key, mask + 1) { sum += cmap_u64_contains(&m, key); } printf("lookup : %" c_ZU "ms \tsum : %" c_ZU "\n", (clock() - now)/ms, sum); now = clock(); sum = 0; - c_foreach (i, cmap_u64, m) { sum += i.ref->second; } + c_FOREACH (i, cmap_u64, m) { sum += i.ref->second; } printf("iterate : %" c_ZU "ms \tsum : %" c_ZU "\n", (clock() - now)/ms, sum); uint64_t rng2[3] = {1872361123, 123879177, 87739234}; now = clock(); - c_forrange (n) { + c_FORRANGE (n) { uint64_t key = romu_trio(rng2) & mask; cmap_u64_erase(&m, key); } diff --git a/misc/benchmarks/misc/string_bench_STC.cpp b/misc/benchmarks/misc/string_bench_STC.cpp index ae8e4c38..39a46d50 100644 --- a/misc/benchmarks/misc/string_bench_STC.cpp +++ b/misc/benchmarks/misc/string_bench_STC.cpp @@ -37,8 +37,8 @@ cvec_str read_file(const char* name) { cvec_str data = cvec_str_init(); - c_auto (cstr, line) - c_with (FILE* f = fopen(name, "r"), fclose(f)) + c_AUTO (cstr, line) + c_WITH (FILE* f = fopen(name, "r"), fclose(f)) while (cstr_getline(&line, f)) cvec_str_emplace_back(&data, cstr_str(&line)); return data; @@ -185,12 +185,12 @@ const size_t MAX_LOOP = 2000; int main() { - c_auto (cvec_str, vec_string) - c_auto (cvec_sv, vec_stringview) - c_auto (csmap_str, mapTrans) - c_auto (csmap_ssv, mapSview) - c_auto (cmap_str, unordmapTrans) - c_auto (cmap_ssv, unordmapSview) + c_AUTO (cvec_str, vec_string) + c_AUTO (cvec_sv, vec_stringview) + c_AUTO (csmap_str, mapTrans) + c_AUTO (csmap_ssv, mapSview) + c_AUTO (cmap_str, unordmapTrans) + c_AUTO (cmap_ssv, unordmapSview) { std::cout << "Short String Benchmark" << std::endl; std::cout << "======================" << std::endl; diff --git a/misc/benchmarks/misc/string_bench_STD.cpp b/misc/benchmarks/misc/string_bench_STD.cpp index 8bb87937..68065923 100644 --- a/misc/benchmarks/misc/string_bench_STD.cpp +++ b/misc/benchmarks/misc/string_bench_STD.cpp @@ -16,8 +16,8 @@ std::vector<std::string> read_file(const char* name) { std::vector<std::string> data; - c_auto (cstr, line) - c_with (FILE* f = fopen(name, "r"), fclose(f)) + c_AUTO (cstr, line) + c_WITH (FILE* f = fopen(name, "r"), fclose(f)) while (cstr_getline(&line, f)) data.emplace_back(cstr_str(&line)); return data; diff --git a/misc/benchmarks/plotbench/cpque_benchmark.cpp b/misc/benchmarks/plotbench/cpque_benchmark.cpp index a729c09f..d6c2eda4 100644 --- a/misc/benchmarks/plotbench/cpque_benchmark.cpp +++ b/misc/benchmarks/plotbench/cpque_benchmark.cpp @@ -40,7 +40,7 @@ void stc_test() stc64_t rng; int N = 10000000, M = 10; - c_auto (cpque_f, pq) + c_AUTO (cpque_f, pq) { rng = stc64_new(seed); clock_t start = clock(); diff --git a/misc/examples/arc_containers.c b/misc/examples/arc_containers.c index b577f2c8..026f9eb5 100644 --- a/misc/examples/arc_containers.c +++ b/misc/examples/arc_containers.c @@ -27,8 +27,8 @@ int main() { - c_auto (Stack, stack) - c_auto (List, list) + c_AUTO (Stack, stack) + c_AUTO (List, list) { // POPULATE stack with shared pointers to Maps: Map *map; @@ -64,14 +64,14 @@ int main() puts("STACKS"); - c_foreach (i, Stack, stack) { - c_forpair (name, year, Map, *i.ref->get) + c_FOREACH (i, Stack, stack) { + c_FORPAIR (name, year, Map, *i.ref->get) printf(" %s:%d", cstr_str(_.name), *_.year); puts(""); } puts("LIST"); - c_foreach (i, List, list) { - c_forpair (name, year, Map, *i.ref->get) + c_FOREACH (i, List, list) { + c_FORPAIR (name, year, Map, *i.ref->get) printf(" %s:%d", cstr_str(_.name), *_.year); puts(""); } diff --git a/misc/examples/arc_demo.c b/misc/examples/arc_demo.c index 087f90ac..f1cf3fda 100644 --- a/misc/examples/arc_demo.c +++ b/misc/examples/arc_demo.c @@ -21,19 +21,19 @@ void int_drop(int* x) { int main() { - c_auto (cvec_Arc, vec) // declare and init vec, call cvec_Arc_drop() at scope exit - c_auto (csset_Arc, set) // declare and init set, call csset_Arc_drop() at scope exit + c_AUTO (cvec_Arc, vec) // declare and init vec, call cvec_Arc_drop() at scope exit + c_AUTO (csset_Arc, set) // declare and init set, call csset_Arc_drop() at scope exit { const int years[] = {2021, 2012, 2022, 2015}; - c_forrange (i, c_arraylen(years)) + c_FORRANGE (i, c_ARRAYLEN(years)) cvec_Arc_push(&vec, Arc_from(years[i])); printf("vec:"); - c_foreach (i, cvec_Arc, vec) printf(" %d", *i.ref->get); + c_FOREACH (i, cvec_Arc, vec) printf(" %d", *i.ref->get); puts(""); // add odd numbers from vec to set - c_foreach (i, cvec_Arc, vec) + c_FOREACH (i, cvec_Arc, vec) if (*i.ref->get & 1) csset_Arc_insert(&set, Arc_clone(*i.ref)); // copy shared pointer => increments counter. @@ -42,12 +42,12 @@ int main() cvec_Arc_pop_back(&vec); printf("vec:"); - c_foreach (i, cvec_Arc, vec) printf(" %d", *i.ref->get); + c_FOREACH (i, cvec_Arc, vec) printf(" %d", *i.ref->get); printf("\nset:"); - c_foreach (i, csset_Arc, set) printf(" %d", *i.ref->get); + c_FOREACH (i, csset_Arc, set) printf(" %d", *i.ref->get); - c_with (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/misc/examples/arcvec_erase.c b/misc/examples/arcvec_erase.c index c70d59d9..eb54084a 100644 --- a/misc/examples/arcvec_erase.c +++ b/misc/examples/arcvec_erase.c @@ -15,9 +15,9 @@ void show_drop(int* x) { printf("drop: %d\n", *x); } int main() { - c_auto (Vec, vec) + c_AUTO (Vec, vec) { - c_forlist (i, int, {2012, 1990, 2012, 2019, 2015}) + c_FORLIST (i, int, {2012, 1990, 2012, 2019, 2015}) Vec_emplace(&vec, *i.ref); // clone the second 2012 and push it back. @@ -25,7 +25,7 @@ int main() Vec_push(&vec, Arc_clone(vec.data[2])); printf("vec before erase :"); - c_foreach (i, Vec, vec) + c_FOREACH (i, Vec, vec) printf(" %d", *i.ref->get); printf("\nerase vec.data[2]; or first matching value depending on compare.\n"); @@ -40,12 +40,12 @@ int main() Vec_erase_at(&vec, it); printf("vec after erase :"); - c_foreach (i, Vec, vec) + c_FOREACH (i, Vec, vec) printf(" %d", *i.ref->get); Vec_sort(&vec); printf("\nvec after sort :"); - c_foreach (i, Vec, vec) + c_FOREACH (i, Vec, vec) printf(" %d", *i.ref->get); puts("\nDone"); diff --git a/misc/examples/astar.c b/misc/examples/astar.c index d4a821f9..e308eb78 100644 --- a/misc/examples/astar.c +++ b/misc/examples/astar.c @@ -80,9 +80,9 @@ astar(cstr* maze, int width) { cdeq_point path = cdeq_point_init(); - c_auto (cpque_point, front) - c_auto (csmap_pstep, from) - c_auto (csmap_pcost, costs) + c_AUTO (cpque_point, front) + c_AUTO (csmap_pstep, from) + c_AUTO (csmap_pcost, costs) { point start = point_from(maze, "@", width); point goal = point_from(maze, "!", width); @@ -99,7 +99,7 @@ astar(cstr* maze, int width) { -1, 0, 0, width }, /* ~ ~ ~ ~ ~ ~ ~ */ { 1, 0, 0, width }, { -1, -1, 0, width }, { 0, -1, 0, width }, { 1, -1, 0, width }, }; - for (size_t i = 0; i < c_arraylen(deltas); i++) + for (size_t i = 0; i < c_ARRAYLEN(deltas); i++) { point delta = deltas[i]; point next = point_init(current.x + delta.x, current.y + delta.y, width); @@ -131,7 +131,7 @@ astar(cstr* maze, int width) int main(void) { - c_with (cstr maze = cstr_lit( + c_WITH (cstr maze = cstr_lit( "#########################################################################\n" "# # # # # # #\n" "# # ######### # ##### ######### ##### ##### ##### # ! #\n" @@ -157,9 +157,9 @@ main(void) "#########################################################################\n"), cstr_drop(&maze)) { int width = cstr_find(&maze, "\n") + 1; - c_with (cdeq_point path = astar(&maze, width), cdeq_point_drop(&path)) + 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'; + c_FOREACH (it, cdeq_point, path) cstr_data(&maze)[point_index(it.ref)] = 'x'; printf("%s", cstr_str(&maze)); } } diff --git a/misc/examples/birthday.c b/misc/examples/birthday.c index 48b8ceaa..be7f0457 100644 --- a/misc/examples/birthday.c +++ b/misc/examples/birthday.c @@ -18,10 +18,10 @@ static void test_repeats(void) printf("birthday paradox: value range: 2^%d, testing repeats of 2^%d values\n", BITS, BITS_TEST); stc64_t rng = stc64_new(seed); - c_auto (cmap_ic, m) + c_AUTO (cmap_ic, m) { cmap_ic_reserve(&m, N); - c_forrange (i, N) { + c_FORRANGE (i, N) { uint64_t k = stc64_rand(&rng) & mask; int v = cmap_ic_insert(&m, k, 0).ref->second += 1; if (v > 1) printf("repeated value %" PRIu64 " (%d) at 2^%d\n", @@ -42,17 +42,17 @@ void test_distribution(void) stc64_t rng = stc64_new(seed); const size_t N = 1ull << BITS ; - c_auto (cmap_x, map) { - c_forrange (N) { + c_AUTO (cmap_x, map) { + c_FORRANGE (N) { uint64_t k = stc64_rand(&rng); cmap_x_insert(&map, k & 0xf, 0).ref->second += 1; } uint64_t sum = 0; - c_foreach (i, cmap_x, map) sum += i.ref->second; + c_FOREACH (i, cmap_x, map) sum += i.ref->second; sum /= map.size; - c_foreach (i, cmap_x, map) { + c_FOREACH (i, cmap_x, map) { printf("%4" PRIu32 ": %" PRIu64 " - %" PRIu64 ": %11.8f\n", i.ref->first, i.ref->second, sum, (1 - (double)i.ref->second / sum)); diff --git a/misc/examples/bits.c b/misc/examples/bits.c index 82fd65ec..c50eac6e 100644 --- a/misc/examples/bits.c +++ b/misc/examples/bits.c @@ -3,7 +3,7 @@ int main() { - c_with (cbits set = cbits_with_size(23, true), cbits_drop(&set)) { + c_WITH (cbits set = cbits_with_size(23, true), cbits_drop(&set)) { printf("count %" c_ZU ", %" c_ZU "\n", cbits_count(&set), cbits_size(&set)); cbits s1 = cbits_from("1110100110111"); char buf[256]; @@ -16,7 +16,7 @@ int main() printf(" str: %s\n", cbits_to_str(&set, buf, 0, 255)); printf("%4" c_ZU ": ", cbits_size(&set)); - c_forrange (i, cbits_size(&set)) + c_FORRANGE (i, cbits_size(&set)) printf("%d", cbits_test(&set, i)); puts(""); @@ -26,34 +26,34 @@ int main() cbits_resize(&set, 102, true); cbits_set_value(&set, 99, false); printf("%4" c_ZU ": ", cbits_size(&set)); - c_forrange (i, cbits_size(&set)) + c_FORRANGE (i, cbits_size(&set)) printf("%d", cbits_test(&set, i)); puts("\nIterate:"); printf("%4" c_ZU ": ", cbits_size(&set)); - c_forrange (i, cbits_size(&set)) + c_FORRANGE (i, cbits_size(&set)) printf("%d", cbits_test(&set, i)); puts(""); - c_with (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); cbits_set(&s2, 18); printf(" new: "); - c_forrange (i, cbits_size(&s2)) + c_FORRANGE (i, cbits_size(&s2)) printf("%d", cbits_test(&s2, i)); puts(""); printf(" xor: "); cbits_xor(&set, &s2); - c_forrange (i, cbits_size(&set)) + c_FORRANGE (i, cbits_size(&set)) printf("%d", cbits_test(&set, i)); puts(""); cbits_set_all(&set, false); printf("%4" c_ZU ": ", cbits_size(&set)); - c_forrange (i, cbits_size(&set)) + c_FORRANGE (i, cbits_size(&set)) printf("%d", cbits_test(&set, i)); puts(""); } diff --git a/misc/examples/bits2.c b/misc/examples/bits2.c index 59e0b337..502e7268 100644 --- a/misc/examples/bits2.c +++ b/misc/examples/bits2.c @@ -23,19 +23,19 @@ int main() Bits_reset(&s2, 66); Bits_reset(&s2, 67); printf(" s2: "); - c_forrange (i, Bits_size(&s2)) + c_FORRANGE (i, Bits_size(&s2)) printf("%d", Bits_test(&s2, i)); puts(""); printf("xor: "); Bits_xor(&s1, &s2); - c_forrange (i, Bits_size(&s1)) + c_FORRANGE (i, Bits_size(&s1)) printf("%d", Bits_test(&s1, i)); puts(""); printf("all: "); Bits_set_pattern(&s1, 0x3333333333333333); - c_forrange (i, Bits_size(&s1)) + c_FORRANGE (i, Bits_size(&s1)) printf("%d", Bits_test(&s1, i)); puts(""); } diff --git a/misc/examples/books.c b/misc/examples/books.c index b6067d81..96c7ff6e 100644 --- a/misc/examples/books.c +++ b/misc/examples/books.c @@ -8,7 +8,7 @@ // would be `HashMap<String, String>` in this example). int main() { - c_auto (cmap_str, book_reviews) + c_AUTO (cmap_str, book_reviews) { // Review some books. cmap_str_emplace(&book_reviews, @@ -41,7 +41,7 @@ int main() // Look up the values associated with some keys. const char* to_find[] = {"Pride and Prejudice", "Alice's Adventure in Wonderland"}; - c_forrange (i, c_arraylen(to_find)) { + c_FORRANGE (i, c_ARRAYLEN(to_find)) { const cmap_str_value* b = cmap_str_get(&book_reviews, to_find[i]); if (b) printf("%s: %s\n", cstr_str(&b->first), cstr_str(&b->second)); @@ -53,7 +53,7 @@ int main() printf("Review for Jane: %s\n", cstr_str(cmap_str_at(&book_reviews, "Pride and Prejudice"))); // Iterate over everything. - c_forpair (book, review, cmap_str, book_reviews) { + c_FORPAIR (book, review, cmap_str, book_reviews) { printf("%s: \"%s\"\n", cstr_str(_.book), cstr_str(_.review)); } } diff --git a/misc/examples/box.c b/misc/examples/box.c index da13501f..446a7603 100644 --- a/misc/examples/box.c +++ b/misc/examples/box.c @@ -24,7 +24,7 @@ Person Person_clone(Person p) { void Person_drop(Person* p) { printf("drop: %s %s\n", cstr_str(&p->name), cstr_str(&p->last)); - c_drop(cstr, &p->name, &p->last); + c_DROP(cstr, &p->name, &p->last); } #define i_type PBox @@ -37,8 +37,8 @@ void Person_drop(Person* p) { int main() { - c_auto (Persons, vec) - c_auto (PBox, p, q) + c_AUTO (Persons, vec) + c_AUTO (PBox, p, q) { p = PBox_from(Person_make("Laura", "Palmer")); q = PBox_clone(p); @@ -54,12 +54,12 @@ int main() Persons_push(&vec, PBox_clone(p)); Persons_push(&vec, PBox_clone(q)); - c_foreach (i, Persons, vec) + c_FOREACH (i, Persons, vec) printf("%s %s\n", cstr_str(&i.ref->get->name), cstr_str(&i.ref->get->last)); puts(""); // Look-up Audrey! Create a temporary Person for lookup. - c_with (Person a = Person_make("Audrey", "Home"), Person_drop(&a)) { + c_WITH (Person a = Person_make("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/misc/examples/box2.c b/misc/examples/box2.c index b9628eeb..6281f800 100644 --- a/misc/examples/box2.c +++ b/misc/examples/box2.c @@ -51,9 +51,9 @@ int main(void) { }; // Declare auto-deleted box objects - c_auto (cbox_Rectangle, boxed_rectangle) - c_auto (cbox_Point, boxed_point) - c_auto (cbox_BoxPoint, box_in_a_box, boxbox2) + c_AUTO (cbox_Rectangle, boxed_rectangle) + c_AUTO (cbox_Point, boxed_point) + c_AUTO (cbox_BoxPoint, box_in_a_box, boxbox2) { // Heap allocated rectangle boxed_rectangle = cbox_Rectangle_make((Rectangle){ diff --git a/misc/examples/city.c b/misc/examples/city.c index 8557c6cf..7f355bdf 100644 --- a/misc/examples/city.c +++ b/misc/examples/city.c @@ -44,16 +44,16 @@ City City_clone(City c) { void City_drop(City* c) { printf("drop %s\n", cstr_str(&c->name)); - c_drop(cstr, &c->name, &c->country); + c_DROP(cstr, &c->name, &c->country); } int main(void) { - c_auto (Cities, cities, copy) - c_auto (CityMap, map) + c_AUTO (Cities, cities, copy) + c_AUTO (CityMap, map) { - c_forlist (i, City, { + c_FORLIST (i, City, { {cstr_lit("New York"), cstr_lit("US"), 4.3f, 23.2f, 9000000}, {cstr_lit("Paris"), cstr_lit("France"), 4.3f, 23.2f, 9000000}, {cstr_lit("Berlin"), cstr_lit("Germany"), 4.3f, 23.2f, 9000000}, @@ -63,7 +63,7 @@ int main(void) Cities_sort(&cities); printf("Vec:\n"); - c_foreach (c, Cities, cities) + 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)); @@ -71,19 +71,19 @@ int main(void) copy = Cities_clone(cities); // share each element! int k = 0, id[] = {8, 4, 3, 9, 2, 5}; - c_foreach (i, Cities, cities) + c_FOREACH (i, Cities, cities) CityMap_insert(&map, id[k++], CityArc_clone(*i.ref)); Cities_pop(&cities); Cities_pop(&cities); printf("Vec:\n"); - c_foreach (c, Cities, cities) + 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("\nMap:\n"); - c_forpair (id, city, CityMap, map) + 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)); puts(""); diff --git a/misc/examples/complex.c b/misc/examples/complex.c index dd2f951a..6fed1d4d 100644 --- a/misc/examples/complex.c +++ b/misc/examples/complex.c @@ -31,7 +31,7 @@ int main() { - c_auto (MapMap, mmap) + c_AUTO (MapMap, mmap) { FloatStack stack = FloatStack_with_size(10, 0); diff --git a/misc/examples/convert.c b/misc/examples/convert.c index 18be3c81..f2324268 100644 --- a/misc/examples/convert.c +++ b/misc/examples/convert.c @@ -13,38 +13,38 @@ int main() { - c_auto (cmap_str, map, mclone) - c_auto (cvec_str, keys, values) - c_auto (clist_str, list) + c_AUTO (cmap_str, map, mclone) + c_AUTO (cvec_str, keys, values) + c_AUTO (clist_str, list) { - c_forlist (i, cmap_str_raw, { + c_FORLIST (i, cmap_str_raw, { {"green", "#00ff00"}, {"blue", "#0000ff"}, {"yellow", "#ffff00"}, }) cmap_str_emplace(&map, c_PAIR(i.ref)); puts("MAP:"); - c_foreach (i, cmap_str, map) + c_FOREACH (i, cmap_str, map) printf(" %s: %s\n", cstr_str(&i.ref->first), cstr_str(&i.ref->second)); puts("\nCLONE MAP:"); mclone = cmap_str_clone(map); - c_foreach (i, cmap_str, mclone) + c_FOREACH (i, cmap_str, mclone) printf(" %s: %s\n", cstr_str(&i.ref->first), cstr_str(&i.ref->second)); puts("\nCOPY MAP TO VECS:"); - c_foreach (i, cmap_str, mclone) { + c_FOREACH (i, cmap_str, mclone) { cvec_str_emplace_back(&keys, cstr_str(&i.ref->first)); cvec_str_emplace_back(&values, cstr_str(&i.ref->second)); } - c_forrange (i, cvec_str_size(&keys)) + c_FORRANGE (i, cvec_str_size(&keys)) printf(" %s: %s\n", cstr_str(keys.data + i), cstr_str(values.data + i)); puts("\nCOPY VEC TO LIST:"); - c_foreach (i, cvec_str, keys) + c_FOREACH (i, cvec_str, keys) clist_str_emplace_back(&list, cstr_str(i.ref)); - c_foreach (i, clist_str, list) + c_FOREACH (i, clist_str, list) printf(" %s\n", cstr_str(i.ref)); } } diff --git a/misc/examples/cpque.c b/misc/examples/cpque.c index 7254b0f7..2ec841ae 100644 --- a/misc/examples/cpque.c +++ b/misc/examples/cpque.c @@ -8,7 +8,7 @@ #include <stdio.h> #include <stdbool.h> #include <stc/forward.h> -#include <stc/views.h> +#include <stc/algo/crange.h> #include <stc/cstr.h> // predeclare @@ -24,12 +24,12 @@ struct { #define i_type ipque #define i_val int #define i_opt c_is_forward // needed to avoid re-type-define container type -#define i_less_functor(self, x, y) c_container_of(self, IPQueue, Q)->less(x, y) // <== This. +#define i_less_functor(self, x, y) c_CONTAINER_OF(self, IPQueue, Q)->less(x, y) // <== This. #include <stc/cpque.h> #define print(name, q, n) do { \ printf("%s: \t", name); \ - c_forrange (i, n) printf("%d ", q[i]); \ + c_FORRANGE (i, n) printf("%d ", q[i]); \ puts(""); \ } while(0) @@ -50,22 +50,22 @@ static bool int_lambda(const int* x, const int* y) { return (*x ^ 1) < (*y ^ 1); int main() { - const int data[] = {1,8,5,6,3,4,0,9,7,2}, n = c_arraylen(data); + const int data[] = {1,8,5,6,3,4,0,9,7,2}, n = c_ARRAYLEN(data); print("data", data, n); - c_autodrop (IPQueue, q1, {ipque_init(), int_less}) // Max priority queue - c_autodrop (IPQueue, minq1, {ipque_init(), int_greater}) // Min priority queue - c_autodrop (IPQueue, q5, {ipque_init(), int_lambda}) // Using lambda to compare elements. + c_AUTODROP (IPQueue, q1, {ipque_init(), int_less}) // Max priority queue + c_AUTODROP (IPQueue, minq1, {ipque_init(), int_greater}) // Min priority queue + c_AUTODROP (IPQueue, q5, {ipque_init(), int_lambda}) // Using lambda to compare elements. { - c_forrange (i, n) + c_FORRANGE (i, n) ipque_push(&q1.Q, data[i]); print_queue("q1", q1); - c_forrange (i, n) + c_FORRANGE (i, n) ipque_push(&minq1.Q, data[i]); print_queue("minq1", minq1); - c_forrange (i, n) + c_FORRANGE (i, n) ipque_push(&q5.Q, data[i]); print_queue("q5", q5); } diff --git a/misc/examples/csmap_erase.c b/misc/examples/csmap_erase.c index 48d8ceef..bef00a17 100644 --- a/misc/examples/csmap_erase.c +++ b/misc/examples/csmap_erase.c @@ -10,14 +10,14 @@ void printmap(mymap m) { - c_foreach (elem, mymap, m) + c_FOREACH (elem, mymap, m) printf(" [%d, %s]", elem.ref->first, cstr_str(&elem.ref->second)); printf("\nsize() == %" c_ZU "\n\n", mymap_size(&m)); } int main() { - c_auto (mymap, m1) + c_AUTO (mymap, m1) { // Fill in some data to test with, one at a time mymap_insert(&m1, 1, cstr_lit("A")); @@ -34,10 +34,10 @@ int main() printmap(m1); } - c_auto (mymap, m2) + c_AUTO (mymap, m2) { // Fill in some data to test with, one at a time - c_forlist (i, mymap_raw, { + c_FORLIST (i, mymap_raw, { {10, "Bob"}, {11, "Rob"}, {12, "Robert"}, @@ -51,7 +51,7 @@ int main() mymap_iter it2 = mymap_find(&m2, mymap_back(&m2)->first); puts("to remove:"); - c_foreach (i, mymap, it1, it2) + c_FOREACH (i, mymap, it1, it2) printf(" [%d, %s]", i.ref->first, cstr_str(&i.ref->second)); puts(""); // The 2nd member function removes elements @@ -61,7 +61,7 @@ int main() printmap(m2); } - c_auto (mymap, m3) + c_AUTO (mymap, m3) { // Fill in some data to test with, one at a time, using emplace mymap_emplace(&m3, 1, "red"); diff --git a/misc/examples/csmap_find.c b/misc/examples/csmap_find.c index 3c507476..a61a47be 100644 --- a/misc/examples/csmap_find.c +++ b/misc/examples/csmap_find.c @@ -20,7 +20,7 @@ void print_elem(csmap_istr_raw p) { void print_collection_##CX(const CX* t) { \ printf("%" c_ZU " elements: ", CX##_size(t)); \ \ - c_foreach (p, CX, *t) { \ + c_FOREACH (p, CX, *t) { \ print_elem(CX##_value_toraw(p.ref)); \ } \ puts(""); \ @@ -42,10 +42,10 @@ void findit(csmap_istr c, csmap_istr_key val) int main() { - c_auto (csmap_istr, m1) - c_auto (cvec_istr, v) + c_AUTO (csmap_istr, m1) + c_AUTO (cvec_istr, v) { - c_forlist (i, csmap_istr_raw, {{40, "Zr"}, {45, "Rh"}}) + c_FORLIST (i, csmap_istr_raw, {{40, "Zr"}, {45, "Rh"}}) csmap_istr_emplace(&m1, c_PAIR(i.ref)); puts("The starting map m1 is (key, value):"); @@ -62,7 +62,7 @@ int main() puts("Inserting the following vector data into m1:"); print_collection_cvec_istr(&v); - c_foreach (i, cvec_istr, cvec_istr_begin(&v), cvec_istr_end(&v)) + c_FOREACH (i, cvec_istr, cvec_istr_begin(&v), cvec_istr_end(&v)) csmap_istr_emplace(&m1, c_PAIR(i.ref)); puts("The modified map m1 is (key, value):"); diff --git a/misc/examples/csmap_insert.c b/misc/examples/csmap_insert.c index 8f777fc6..b1b0a5be 100644 --- a/misc/examples/csmap_insert.c +++ b/misc/examples/csmap_insert.c @@ -19,13 +19,13 @@ #include <stc/cvec.h> void print_ii(csmap_ii map) { - c_foreach (e, csmap_ii, map) + c_FOREACH (e, csmap_ii, map) printf("(%d, %d) ", e.ref->first, e.ref->second); puts(""); } void print_istr(csmap_istr map) { - c_foreach (e, csmap_istr, map) + c_FOREACH (e, csmap_istr, map) printf("(%d, %s) ", e.ref->first, cstr_str(&e.ref->second)); puts(""); } @@ -33,7 +33,7 @@ void print_istr(csmap_istr map) { int main() { // insert single values - c_auto (csmap_ii, m1) { + c_AUTO (csmap_ii, m1) { csmap_ii_insert(&m1, 1, 10); csmap_ii_insert(&m1, 2, 20); @@ -60,8 +60,8 @@ int main() } // The templatized version inserting a jumbled range - c_auto (csmap_ii, m2) - c_auto (cvec_ii, v) { + c_AUTO (csmap_ii, m2) + c_AUTO (cvec_ii, v) { typedef cvec_ii_value ipair; cvec_ii_push(&v, (ipair){43, 294}); cvec_ii_push(&v, (ipair){41, 262}); @@ -70,21 +70,21 @@ int main() cvec_ii_push(&v, (ipair){44, 311}); puts("Inserting the following vector data into m2:"); - c_foreach (e, cvec_ii, v) + c_FOREACH (e, cvec_ii, v) printf("(%d, %d) ", e.ref->first, e.ref->second); puts(""); - c_foreach (e, cvec_ii, v) + c_FOREACH (e, cvec_ii, v) csmap_ii_insert_or_assign(&m2, e.ref->first, e.ref->second); puts("The modified key and mapped values of m2 are:"); - c_foreach (e, csmap_ii, m2) + c_FOREACH (e, csmap_ii, m2) printf("(%d, %d) ", e.ref->first, e.ref->second); puts("\n"); } // The templatized versions move-constructing elements - c_auto (csmap_istr, m3) { + c_AUTO (csmap_istr, m3) { csmap_istr_value ip1 = {475, cstr_lit("blue")}, ip2 = {510, cstr_lit("green")}; // single element @@ -99,9 +99,9 @@ int main() puts(""); } - c_auto (csmap_ii, m4) { + c_AUTO (csmap_ii, m4) { // Insert the elements from an initializer_list - c_forlist (i, csmap_ii_raw, {{ 4, 44 }, { 2, 22 }, { 3, 33 }, + c_FORLIST (i, csmap_ii_raw, {{ 4, 44 }, { 2, 22 }, { 3, 33 }, { 1, 11 }, { 5, 55 }}) csmap_ii_insert(&m4, c_PAIR(i.ref)); diff --git a/misc/examples/csset_erase.c b/misc/examples/csset_erase.c index 33eb2163..c281c626 100644 --- a/misc/examples/csset_erase.c +++ b/misc/examples/csset_erase.c @@ -5,12 +5,12 @@ int main() { - c_auto (csset_int, set) + c_AUTO (csset_int, set) { - c_forlist (i, int, {30, 20, 80, 40, 60, 90, 10, 70, 50}) + c_FORLIST (i, int, {30, 20, 80, 40, 60, 90, 10, 70, 50}) csset_int_insert(&set, *i.ref); - c_foreach (k, csset_int, set) + c_FOREACH (k, csset_int, set) printf(" %d", *k.ref); puts(""); @@ -19,7 +19,7 @@ int main() printf("Show values >= %d:\n", val); it = csset_int_lower_bound(&set, val); - c_foreach (k, csset_int, it, csset_int_end(&set)) + c_FOREACH (k, csset_int, it, csset_int_end(&set)) printf(" %d", *k.ref); puts(""); @@ -27,7 +27,7 @@ int main() while (it.ref != csset_int_end(&set).ref) it = csset_int_erase_at(&set, it); - c_foreach (k, csset_int, set) + c_FOREACH (k, csset_int, set) printf(" %d", *k.ref); puts(""); @@ -36,7 +36,7 @@ int main() it = csset_int_lower_bound(&set, val); csset_int_erase_range(&set, csset_int_begin(&set), it); - c_foreach (k, csset_int, set) + c_FOREACH (k, csset_int, set) printf(" %d", *k.ref); puts(""); } diff --git a/misc/examples/cstr_match.c b/misc/examples/cstr_match.c index 286ba505..116e5dd4 100644 --- a/misc/examples/cstr_match.c +++ b/misc/examples/cstr_match.c @@ -4,7 +4,7 @@ int main() { - c_with (cstr ss = cstr_lit("The quick brown fox jumps over the lazy dog.JPG"), cstr_drop(&ss)) { + c_WITH (cstr ss = cstr_lit("The quick brown fox jumps over the lazy dog.JPG"), cstr_drop(&ss)) { size_t pos = cstr_find_at(&ss, 0, "brown"); printf("%" c_ZU " [%s]\n", pos, pos == c_NPOS ? "<NULL>" : cstr_str(&ss) + pos); printf("equals: %d\n", cstr_equals(&ss, "The quick brown fox jumps over the lazy dog.JPG")); diff --git a/misc/examples/demos.c b/misc/examples/demos.c index fc3771cb..4455b840 100644 --- a/misc/examples/demos.c +++ b/misc/examples/demos.c @@ -3,7 +3,7 @@ void stringdemo1() { printf("\nSTRINGDEMO1\n"); - c_with (cstr cs = cstr_lit("one-nine-three-seven-five"), cstr_drop(&cs)) + c_WITH (cstr cs = cstr_lit("one-nine-three-seven-five"), cstr_drop(&cs)) { printf("%s.\n", cstr_str(&cs)); @@ -13,7 +13,7 @@ void stringdemo1() cstr_erase(&cs, 7, 5); // -nine printf("%s.\n", cstr_str(&cs)); - cstr_replace(&cs, "seven", "four", 1); + cstr_replace_ex(&cs, "seven", "four", 1); printf("%s.\n", cstr_str(&cs)); cstr_take(&cs, cstr_from_fmt("%s *** %s", cstr_str(&cs), cstr_str(&cs))); @@ -35,7 +35,7 @@ void stringdemo1() void vectordemo1() { printf("\nVECTORDEMO1\n"); - c_with (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) @@ -59,7 +59,7 @@ void vectordemo1() void vectordemo2() { printf("\nVECTORDEMO2\n"); - c_auto (cvec_str, names) { + c_AUTO (cvec_str, names) { cvec_str_emplace_back(&names, "Mary"); cvec_str_emplace_back(&names, "Joe"); cvec_str_emplace_back(&names, "Chris"); @@ -67,7 +67,7 @@ void vectordemo2() printf("names[1]: %s\n", cstr_str(&names.data[1])); cvec_str_sort(&names); // Sort the array - c_foreach (i, cvec_str, names) + c_FOREACH (i, cvec_str, names) printf("sorted: %s\n", cstr_str(i.ref)); } } @@ -80,7 +80,7 @@ void vectordemo2() void listdemo1() { printf("\nLISTDEMO1\n"); - c_auto (clist_ix, nums, nums2) + c_AUTO (clist_ix, nums, nums2) { for (int i = 0; i < 10; ++i) clist_ix_push_back(&nums, i); @@ -89,7 +89,7 @@ void listdemo1() /* splice nums2 to front of nums */ clist_ix_splice(&nums, clist_ix_begin(&nums), &nums2); - c_foreach (i, clist_ix, nums) + c_FOREACH (i, clist_ix, nums) printf("spliced: %d\n", *i.ref); puts(""); @@ -101,7 +101,7 @@ void listdemo1() clist_ix_push_back(&nums, -99); clist_ix_sort(&nums); - c_foreach (i, clist_ix, nums) + c_FOREACH (i, clist_ix, nums) printf("sorted: %d\n", *i.ref); } } @@ -117,7 +117,7 @@ void setdemo1() cset_i_insert(&nums, 8); cset_i_insert(&nums, 11); - c_foreach (i, cset_i, nums) + c_FOREACH (i, cset_i, nums) printf("set: %d\n", *i.ref); cset_i_drop(&nums); } @@ -145,7 +145,7 @@ void mapdemo1() void mapdemo2() { printf("\nMAPDEMO2\n"); - c_auto (cmap_si, nums) + c_AUTO (cmap_si, nums) { cmap_si_emplace_or_assign(&nums, "Hello", 64); cmap_si_emplace_or_assign(&nums, "Groovy", 121); @@ -156,7 +156,7 @@ void mapdemo2() printf("long: %s: %d\n", cstr_str(&i.ref->first), i.ref->second); // or rather use the short form: - c_foreach (i, cmap_si, nums) + c_FOREACH (i, cmap_si, nums) printf("short: %s: %d\n", cstr_str(&i.ref->first), i.ref->second); } } @@ -173,47 +173,18 @@ void mapdemo3() cmap_str_emplace(&table, "Make", "my"); cmap_str_emplace(&table, "Sunny", "day"); cmap_str_iter it = cmap_str_find(&table, "Make"); - c_foreach (i, cmap_str, table) + c_FOREACH (i, cmap_str, table) printf("entry: %s: %s\n", cstr_str(&i.ref->first), cstr_str(&i.ref->second)); printf("size %" c_ZU ": remove: Make: %s\n", cmap_str_size(&table), cstr_str(&it.ref->second)); //cmap_str_erase(&table, "Make"); cmap_str_erase_at(&table, it); printf("size %" c_ZU "\n", cmap_str_size(&table)); - c_foreach (i, cmap_str, table) + c_FOREACH (i, cmap_str, table) printf("entry: %s: %s\n", cstr_str(&i.ref->first), cstr_str(&i.ref->second)); cmap_str_drop(&table); // frees key and value cstrs, and hash table. } -#define i_val float -#define i_tag f -#include <stc/carr3.h> - -void arraydemo1() -{ - printf("\nARRAYDEMO1\n"); - 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]; - float *arr1 = arr3.data[5][4]; - - printf("arr3: %" c_ZU ": (%" c_ZU ", %" c_ZU ", %" c_ZU ") = %" c_ZU "\n", sizeof(arr3), - arr3.xdim, arr3.ydim, arr3.zdim, carr3_f_size(&arr3)); - - printf("%g\n", arr1[3]); // = 10.2 - printf("%g\n", arr2[4][3]); // = 10.2 - printf("%g\n", arr3.data[5][4][3]); // = 10.2 - - float x = 0.0; - c_foreach (i, carr3_f, arr3) - *i.ref = ++x; - printf("%g\n", arr3.data[29][19][9]); // = 6000 - } -} - - int main() { stringdemo1(); @@ -224,5 +195,4 @@ int main() mapdemo1(); mapdemo2(); mapdemo3(); - arraydemo1(); } diff --git a/misc/examples/forfilter.c b/misc/examples/forfilter.c index ba4dce7f..c0a4ccad 100644 --- a/misc/examples/forfilter.c +++ b/misc/examples/forfilter.c @@ -2,7 +2,8 @@ #define i_extern #include <stc/cstr.h> #include <stc/csview.h> -#include <stc/views.h> +#include <stc/algo/filter.h> +#include <stc/algo/crange.h> #define i_type IVec #define i_val int @@ -20,23 +21,23 @@ void demo1(void) { - c_auto (IVec, vec) { - c_forlist (i, int, {0, 1, 2, 3, 4, 5, 80, 6, 7, 80, 8, 9, 80, 10, 11, 12, 13, 14, 15, 80, 16, 17}) + c_AUTO (IVec, vec) { + c_FORLIST (i, int, {0, 1, 2, 3, 4, 5, 80, 6, 7, 80, 8, 9, 80, 10, 11, 12, 13, 14, 15, 80, 16, 17}) IVec_push(&vec, *i.ref); puts("demo1:"); - c_forfilter (i, IVec, vec, flt_skipValue(i, 80)) + c_FORFILTER (i, IVec, vec, flt_skipValue(i, 80)) printf(" %d", *i.ref); puts(""); int res, sum = 0; - c_forfilter (i, IVec, vec - , c_flt_skipwhile(i, *i.ref != 80) - && c_flt_skip(i, 1) - && c_flt_skipwhile(i, *i.ref != 80) + c_FORFILTER (i, IVec, vec + , c_FLT_SKIPWHILE(i, *i.ref != 80) + && c_FLT_SKIP(i, 1) + && c_FLT_SKIPWHILE(i, *i.ref != 80) && flt_isEven(i) && flt_skipValue(i, 80) - , c_flt_take(i, 5) // short-circuit + , c_FLT_TAKE(i, 5) // short-circuit ){ sum += res = flt_square(i); printf(" %d", res); @@ -60,15 +61,16 @@ fn main() { void demo2(void) { - c_auto (IVec, vector) { + c_AUTO (IVec, vector) { puts("demo2:"); - c_forfilter (x, crange, crange_literal(INT64_MAX) - , c_flt_skipwhile(x, *x.ref != 11) + c_FORFILTER (x, crange, crange_LITERAL(INT64_MAX) + , c_FLT_SKIPWHILE(x, *x.ref != 11) && *x.ref % 2 != 0 - , c_flt_take(x, 5)) + , c_FLT_TAKE(x, 5)) IVec_push(&vector, *x.ref * *x.ref); - c_foreach (x, IVec, vector) printf(" %d", *x.ref); + c_FOREACH (x, IVec, vector) + printf(" %d", *x.ref); puts(""); } } @@ -88,17 +90,17 @@ fn main() { */ void demo3(void) { - c_auto (SVec, words, words_containing_i) { + c_AUTO (SVec, words, words_containing_i) { const char* sentence = "This is a sentence in C99."; - c_fortoken (w, sentence, " ") + c_FORTOKEN (w, sentence, " ") SVec_push(&words, *w.ref); - c_forfilter (w, SVec, words, + c_FORFILTER (w, SVec, words, csview_contains(*w.ref, "i")) SVec_push(&words_containing_i, *w.ref); puts("demo3:"); - c_foreach (w, SVec, words_containing_i) + c_FOREACH (w, SVec, words_containing_i) printf(" %.*s", c_ARGSV(*w.ref)); puts(""); } @@ -107,8 +109,8 @@ void demo3(void) void demo4(void) { csview s = c_SV("ab123cReAghNGnΩoEp"); // Ω = multi-byte - c_auto (cstr, out) { - c_forfilter (i, csview, s, utf8_isupper(utf8_peek(i.ref))) { + c_AUTO (cstr, out) { + c_FORFILTER (i, csview, s, utf8_isupper(utf8_peek(i.ref))) { char chr[4]; utf8_encode(chr, utf8_tolower(utf8_peek(i.ref))); cstr_push(&out, chr); @@ -124,12 +126,12 @@ void demo5(void) #define flt_mid_decade(i) ((*i.ref % 10) != 0) puts("demo5:"); crange r1 = crange_make(1963, INT32_MAX); - c_forfilter (i, crange, r1 - , c_flt_skip(i,15) - && c_flt_skipwhile(i, flt_mid_decade(i)) - && c_flt_skip(i,30) + c_FORFILTER (i, crange, r1 + , c_FLT_SKIP(i,15) + && c_FLT_SKIPWHILE(i, flt_mid_decade(i)) + && c_FLT_SKIP(i,30) && flt_even(i) - , c_flt_take(i,10)) + , c_FLT_TAKE(i,10)) printf(" %lld", *i.ref); puts(""); } diff --git a/misc/examples/forloops.c b/misc/examples/forloops.c index 2654f095..69c1c193 100644 --- a/misc/examples/forloops.c +++ b/misc/examples/forloops.c @@ -1,5 +1,5 @@ #include <stdio.h>
-#include <stc/views.h>
+#include <stc/algo/filter.h>
#define i_type IVec
#define i_val int
@@ -13,67 +13,67 @@ int main()
{
- puts("c_forrange:");
- c_forrange (30) printf(" xx");
+ puts("c_FORRANGE:");
+ c_FORRANGE (30) printf(" xx");
puts("");
- c_forrange (i, 30) printf(" %lld", i);
+ c_FORRANGE (i, 30) printf(" %lld", i);
puts("");
- c_forrange (i, 30, 60) printf(" %lld", i);
+ c_FORRANGE (i, 30, 60) printf(" %lld", i);
puts("");
- c_forrange (i, 30, 90, 2) printf(" %lld", i);
+ c_FORRANGE (i, 30, 90, 2) printf(" %lld", i);
puts("\n\nc_forlist:");
- c_forlist (i, int, {12, 23, 453, 65, 676})
+ c_FORLIST (i, int, {12, 23, 453, 65, 676})
printf(" %d", *i.ref);
puts("");
- c_forlist (i, const char*, {"12", "23", "453", "65", "676"})
+ c_FORLIST (i, const char*, {"12", "23", "453", "65", "676"})
printf(" %s", *i.ref);
puts("");
- c_forlist (i, const char*, {"12", "23", "453", "65", "676"})
+ c_FORLIST (i, const char*, {"12", "23", "453", "65", "676"})
printf(" %s", i.data[i.size - 1 - i.index]);
- c_auto (IVec, vec)
- c_auto (IMap, map)
+ c_AUTO (IVec, vec)
+ c_AUTO (IMap, map)
{
- c_forlist (i, int, {12, 23, 453, 65, 113, 215, 676, 34, 67, 20, 27, 66, 189, 45, 280, 199})
+ c_FORLIST (i, int, {12, 23, 453, 65, 113, 215, 676, 34, 67, 20, 27, 66, 189, 45, 280, 199})
IVec_push(&vec, *i.ref);
- c_forlist (i, IMap_value, {{12, 23}, {453, 65}, {676, 123}, {34, 67}})
+ c_FORLIST (i, IMap_value, {{12, 23}, {453, 65}, {676, 123}, {34, 67}})
IMap_push(&map, *i.ref);
puts("\n\nc_foreach:");
- c_foreach (i, IVec, vec)
+ c_FOREACH (i, IVec, vec)
printf(" %d", *i.ref);
puts("");
- c_foreach (i, IMap, map)
+ c_FOREACH (i, IMap, map)
printf(" (%d %d)", i.ref->first, i.ref->second);
puts("\n\nc_forpair:");
- c_forpair (key, val, IMap, map)
+ c_FORPAIR (key, val, IMap, map)
printf(" (%d %d)", *_.key, *_.val);
puts("\n\nc_forwhile:");
- c_forwhile (i, IVec, IVec_begin(&vec), i.index < 3)
+ c_FORWHILE (i, IVec, IVec_begin(&vec), i.index < 3)
printf(" %d", *i.ref);
#define isOdd(i) (*i.ref & 1)
puts("\n\nc_forfilter:");
- c_forfilter (i, IVec, vec
- , c_flt_skipwhile(i, *i.ref != 65)
- && c_flt_takewhile(i, *i.ref != 280)
- && c_flt_skipwhile(i, isOdd(i))
+ c_FORFILTER (i, IVec, vec
+ , c_FLT_SKIPWHILE(i, *i.ref != 65)
+ && c_FLT_TAKEWHILE(i, *i.ref != 280)
+ && c_FLT_SKIPWHILE(i, isOdd(i))
&& isOdd(i)
- && c_flt_skip(i, 2)
- , c_flt_take(i, 1))
+ && c_FLT_SKIP(i, 2)
+ , c_FLT_TAKE(i, 1))
printf(" %d", *i.ref);
puts("");
// 189
diff --git a/misc/examples/gauss1.c b/misc/examples/gauss1.c index 675ff338..519a7895 100644 --- a/misc/examples/gauss1.c +++ b/misc/examples/gauss1.c @@ -29,27 +29,27 @@ int main() stc64_normalf_t dist = stc64_normalf_new(Mean, StdDev); // Create and init histogram vec and map with defered destructors: - c_auto (cvec_ii, histvec) - c_auto (cmap_ii, histmap) + c_AUTO (cvec_ii, histvec) + c_AUTO (cmap_ii, histmap) { - c_forrange (N) { - int index = (int) round( stc64_normalf(&rng, &dist) ); + c_FORRANGE (N) { + int index = (int)round( stc64_normalf(&rng, &dist) ); cmap_ii_insert(&histmap, index, 0).ref->second += 1; } // Transfer map to vec and sort it by map keys. - c_foreach (i, cmap_ii, histmap) + c_FOREACH (i, cmap_ii, histmap) cvec_ii_push(&histvec, (cmap_ii_value){i.ref->first, i.ref->second}); cvec_ii_sort(&histvec); // Print the gaussian bar chart - c_auto (cstr, bar) - c_foreach (i, cvec_ii, histvec) { - size_t n = (size_t) (i.ref->second * StdDev * Scale * 2.5 / (float)N); + c_FOREACH (i, cvec_ii, histvec) { + int n = (int)(i.ref->second * StdDev * Scale * 2.5 / N); if (n > 0) { - cstr_resize(&bar, n, '*'); - printf("%4d %s\n", i.ref->first, cstr_str(&bar)); + printf("%4d ", i.ref->first); + c_FORRANGE (n) printf("*"); + puts(""); } } } diff --git a/misc/examples/gauss2.c b/misc/examples/gauss2.c index 2e07c5a5..c2ed2e00 100644 --- a/misc/examples/gauss2.c +++ b/misc/examples/gauss2.c @@ -23,16 +23,16 @@ int main() stc64_normalf_t dist = stc64_normalf_new(Mean, StdDev); // Create and init histogram map with defered destruct - c_auto (csmap_int, mhist) + c_AUTO (csmap_int, mhist) { - c_forrange (N) { + c_FORRANGE (N) { int index = (int) round( stc64_normalf(&rng, &dist) ); csmap_int_insert(&mhist, index, 0).ref->second += 1; } // Print the gaussian bar chart - c_auto (cstr, bar) - c_forpair (index, count, csmap_int, mhist) { + c_AUTO (cstr, bar) + c_FORPAIR (index, count, csmap_int, mhist) { size_t n = (size_t) (*_.count * StdDev * Scale * 2.5 / (float)N); if (n > 0) { cstr_resize(&bar, n, '*'); diff --git a/misc/examples/hashmap.c b/misc/examples/hashmap.c index f59ed824..bc4f2566 100644 --- a/misc/examples/hashmap.c +++ b/misc/examples/hashmap.c @@ -17,7 +17,7 @@ const char* call(const char* number) { } int main(void) { - c_auto (cmap_str, contacts) + c_AUTO (cmap_str, contacts) { cmap_str_emplace(&contacts, "Daniel", "798-1364"); cmap_str_emplace(&contacts, "Ashley", "645-7689"); @@ -40,7 +40,7 @@ int main(void) { cmap_str_erase(&contacts, "Ashley"); puts(""); - c_forpair (contact, number, cmap_str, contacts) { + c_FORPAIR (contact, number, cmap_str, contacts) { printf("Calling %s: %s\n", cstr_str(_.contact), call(cstr_str(_.number))); } puts(""); diff --git a/misc/examples/inits.c b/misc/examples/inits.c index 021a3e0a..459a0ec1 100644 --- a/misc/examples/inits.c +++ b/misc/examples/inits.c @@ -36,11 +36,11 @@ int main(void) { // CVEC FLOAT / PRIORITY QUEUE - c_auto (cpque_f, floats) { + c_AUTO (cpque_f, floats) { const float nums[] = {4.0f, 2.0f, 5.0f, 3.0f, 1.0f}; // PRIORITY QUEUE - c_forrange (i, c_arraylen(nums)) + c_FORRANGE (i, c_ARRAYLEN(nums)) cpque_f_push(&floats, nums[i]); puts("\npop and show high priorites first:"); @@ -54,20 +54,20 @@ int main(void) // CMAP ID int year = 2020; - c_auto (cmap_id, idnames) { + c_AUTO (cmap_id, idnames) { cmap_id_emplace(&idnames, 100, "Hello"); cmap_id_insert(&idnames, 110, cstr_lit("World")); cmap_id_insert(&idnames, 120, cstr_from_fmt("Howdy, -%d-", year)); - c_foreach (i, cmap_id, idnames) + c_FOREACH (i, cmap_id, idnames) printf("%d: %s\n", i.ref->first, cstr_str(&i.ref->second)); puts(""); } // CMAP CNT - c_auto (cmap_cnt, countries) { - c_forlist (i, cmap_cnt_raw, { + c_AUTO (cmap_cnt, countries) { + c_FORLIST (i, cmap_cnt_raw, { {"Norway", 100}, {"Denmark", 50}, {"Iceland", 10}, @@ -83,33 +83,33 @@ int main(void) cmap_cnt_emplace(&countries, "Norway", 0).ref->second += 20; cmap_cnt_emplace(&countries, "Finland", 0).ref->second += 20; - c_forpair (country, health, cmap_cnt, countries) + c_FORPAIR (country, health, cmap_cnt, countries) printf("%s: %d\n", cstr_str(_.country), *_.health); puts(""); } // CVEC PAIR - c_auto (cvec_ip, pairs1) { - c_forlist (i, ipair_t, {{5, 6}, {3, 4}, {1, 2}, {7, 8}}) + c_AUTO (cvec_ip, pairs1) { + c_FORLIST (i, ipair_t, {{5, 6}, {3, 4}, {1, 2}, {7, 8}}) cvec_ip_push_back(&pairs1, *i.ref); cvec_ip_sort(&pairs1); - c_foreach (i, cvec_ip, pairs1) + c_FOREACH (i, cvec_ip, pairs1) printf("(%d %d) ", i.ref->x, i.ref->y); puts(""); } // CLIST PAIR - c_auto (clist_ip, pairs2) { - c_forlist (i, ipair_t, {{5, 6}, {3, 4}, {1, 2}, {7, 8}}) + c_AUTO (clist_ip, pairs2) { + c_FORLIST (i, ipair_t, {{5, 6}, {3, 4}, {1, 2}, {7, 8}}) clist_ip_push_back(&pairs2, *i.ref); clist_ip_sort(&pairs2); - c_foreach (i, clist_ip, pairs2) + c_FOREACH (i, clist_ip, pairs2) printf("(%d %d) ", i.ref->x, i.ref->y); puts(""); } diff --git a/misc/examples/intrusive.c b/misc/examples/intrusive.c index acf4416d..4135317e 100644 --- a/misc/examples/intrusive.c +++ b/misc/examples/intrusive.c @@ -1,4 +1,4 @@ -// Example of intrusive list by using the node API and typesafe c_container_of(). +// Example of intrusive list by using the node API and typesafe c_CONTAINER_OF(). #include <stdio.h> @@ -14,39 +14,39 @@ int main() { - c_auto (List2, list2) + c_AUTO (List2, list2) { List1 list1 = List1_init(); // should not be destroyed, list2 will destroy shared nodes. - c_forlist (i, int, {6, 9, 3, 1, 7, 4, 5, 2, 8}) + c_FORLIST (i, int, {6, 9, 3, 1, 7, 4, 5, 2, 8}) List2_push_back(&list2, (List1_node){NULL, *i.ref}); - c_foreach (i, List2, list2) - List1_push_node_back(&list1, c_container_of(&i.ref->value, List1_node, value)); + c_FOREACH (i, List2, list2) + List1_push_node_back(&list1, c_CONTAINER_OF(&i.ref->value, List1_node, value)); printf("list1:"); - c_foreach (i, List1, list1) printf(" %d", *i.ref); + c_FOREACH (i, List1, list1) printf(" %d", *i.ref); printf("\nlist2:"); - c_foreach (i, List2, list2) printf(" %d", i.ref->value); + c_FOREACH (i, List2, list2) printf(" %d", i.ref->value); printf("\nsort list1"); List1_sort(&list1); printf("\nlist1:"); - c_foreach (i, List1, list1) printf(" %d", *i.ref); + c_FOREACH (i, List1, list1) printf(" %d", *i.ref); printf("\nlist2:"); - c_foreach (i, List2, list2) printf(" %d", i.ref->value); + c_FOREACH (i, List2, list2) printf(" %d", i.ref->value); printf("\nremove 5 from both lists in O(1) time"); List1_iter it1 = List1_find(&list1, 5); if (it1.ref) { List1_unlink_node_after(&list1, it1.prev); - free(List2_unlink_node_after(&list2, c_container_of(it1.prev, List2_node, value))); + free(List2_unlink_node_after(&list2, c_CONTAINER_OF(it1.prev, List2_node, value))); } printf("\nlist1:"); - c_foreach (i, List1, list1) printf(" %d", *i.ref); + c_FOREACH (i, List1, list1) printf(" %d", *i.ref); printf("\nlist2:"); - c_foreach (i, List2, list2) printf(" %d", i.ref->value); + c_FOREACH (i, List2, list2) printf(" %d", i.ref->value); puts(""); } } diff --git a/misc/examples/list.c b/misc/examples/list.c index a538d93c..4aacfe38 100644 --- a/misc/examples/list.c +++ b/misc/examples/list.c @@ -12,37 +12,37 @@ int main() { const int n = 2000000; - c_auto (clist_fx, list) + c_AUTO (clist_fx, list) { stc64_t rng = stc64_new(1234); stc64_uniformf_t dist = stc64_uniformf_new(100.0f, n); int m = 0; - c_forrange (n) + c_FORRANGE (n) clist_fx_push_back(&list, stc64_uniformf(&rng, &dist)), ++m; double sum = 0.0; printf("sumarize %d:\n", m); - c_foreach (i, clist_fx, list) + c_FOREACH (i, clist_fx, list) sum += *i.ref; printf("sum %f\n\n", sum); - c_forwhile (i, clist_fx, clist_fx_begin(&list), i.index < 10) + c_FORWHILE (i, clist_fx, clist_fx_begin(&list), i.index < 10) printf("%8d: %10f\n", (int)i.index, *i.ref); puts("sort"); clist_fx_sort(&list); // mergesort O(n*log n) puts("sorted"); - c_forwhile (i, clist_fx, clist_fx_begin(&list), i.index < 10) + c_FORWHILE (i, clist_fx, clist_fx_begin(&list), i.index < 10) printf("%8d: %10f\n", (int)i.index, *i.ref); puts(""); clist_fx_clear(&list); - c_forlist (i, int, {10, 20, 30, 40, 30, 50}) + c_FORLIST (i, int, {10, 20, 30, 40, 30, 50}) clist_fx_push_back(&list, *i.ref); const double* v = clist_fx_get(&list, 30); printf("found: %f\n", *v); - c_foreach (i, clist_fx, list) printf(" %g", *i.ref); + c_FOREACH (i, clist_fx, list) printf(" %g", *i.ref); puts(""); clist_fx_remove(&list, 30); @@ -51,10 +51,10 @@ int main() { clist_fx_push_front(&list, 1964); clist_fx_iter it = clist_fx_begin(&list); printf("Full: "); - c_foreach (i, clist_fx, list) + c_FOREACH (i, clist_fx, list) printf(" %g", *i.ref); printf("\nSubs: "); - c_foreach (i, clist_fx, clist_fx_advance(it, 4), clist_fx_end(&list)) + c_FOREACH (i, clist_fx, clist_fx_advance(it, 4), clist_fx_end(&list)) printf(" %g", *i.ref); puts(""); } diff --git a/misc/examples/list_erase.c b/misc/examples/list_erase.c index c1a2aa97..18ad0d16 100644 --- a/misc/examples/list_erase.c +++ b/misc/examples/list_erase.c @@ -7,12 +7,12 @@ int main () { - c_with (IList L = IList_init(), IList_drop(&L)) + c_WITH (IList L = IList_init(), IList_drop(&L)) { - c_forlist (i, int, {10, 20, 30, 40, 50}) + c_FORLIST (i, int, {10, 20, 30, 40, 50}) IList_push(&L, *i.ref); - c_foreach (x, IList, L) + c_FOREACH (x, IList, L) printf("%d ", *x.ref); puts(""); // 10 20 30 40 50 @@ -25,7 +25,7 @@ int main () it = IList_erase_range(&L, it, end); // 10 30 // ^ printf("list contains:"); - c_foreach (x, IList, L) + c_FOREACH (x, IList, L) printf(" %d", *x.ref); puts(""); } diff --git a/misc/examples/list_splice.c b/misc/examples/list_splice.c index f7bac0d6..8a5dae76 100644 --- a/misc/examples/list_splice.c +++ b/misc/examples/list_splice.c @@ -8,7 +8,7 @@ void print_ilist(const char* s, clist_i list) { printf("%s", s); - c_foreach (i, clist_i, list) { + c_FOREACH (i, clist_i, list) { printf(" %d", *i.ref); } puts(""); @@ -16,12 +16,12 @@ void print_ilist(const char* s, clist_i list) int main () { - c_auto (clist_i, list1, list2) + c_AUTO (clist_i, list1, list2) { - c_forlist (i, int, {1, 2, 3, 4, 5}) + c_FORLIST (i, int, {1, 2, 3, 4, 5}) clist_i_push_back(&list1, *i.ref); - c_forlist (i, int, {10, 20, 30, 40, 50}) + c_FORLIST (i, int, {10, 20, 30, 40, 50}) clist_i_push_back(&list2, *i.ref); print_ilist("list1:", list1); diff --git a/misc/examples/lower_bound.c b/misc/examples/lower_bound.c index 2477bc14..2b6f3cc6 100644 --- a/misc/examples/lower_bound.c +++ b/misc/examples/lower_bound.c @@ -9,11 +9,11 @@ int main() { // TEST SORTED VECTOR - c_auto (cvec_int, vec) + c_AUTO (cvec_int, vec) { int key, *res; - c_forlist (i, int, {40, 600, 1, 7000, 2, 500, 30}) + c_FORLIST (i, int, {40, 600, 1, 7000, 2, 500, 30}) cvec_int_push(&vec, *i.ref); cvec_int_sort(&vec); @@ -36,11 +36,11 @@ int main() } // TEST SORTED SET - c_auto (csset_int, set) + c_AUTO (csset_int, set) { int key, *res; - c_forlist (i, int, {40, 600, 1, 7000, 2, 500, 30}) + c_FORLIST (i, int, {40, 600, 1, 7000, 2, 500, 30}) csset_int_push(&set, *i.ref); key = 500; diff --git a/misc/examples/mapmap.c b/misc/examples/mapmap.c index 488cc539..d5fe9c81 100644 --- a/misc/examples/mapmap.c +++ b/misc/examples/mapmap.c @@ -4,18 +4,24 @@ // People: std::map<std::string, std::string> #define i_type People -#define i_key_str -#define i_val_str +#define i_key_str // name +#define i_val_str // email #define i_keydrop(p) (printf("kdrop: %s\n", cstr_str(p)), cstr_drop(p)) // override #include <stc/csmap.h> // Departments: std::map<std::string, People> #define i_type Departments -#define i_key_str +#define i_key_str // dep. name #define i_valclass People -// Shorthand for: +// i_key_str implies: +// #define i_tag str +// #define i_key cstr +// #define i_keyclone cstr_clone +// #define i_keydrop cstr_drop +// #define i_cmp cstr_cmp +// #define i_hash cstr_hash +// i_valclass implies: // #define i_val People -// #define i_cmp People_cmp // #define i_valclone People_clone // #define i_valdrop People_drop #include <stc/csmap.h> @@ -23,14 +29,14 @@ void add(Departments* deps, const char* name, const char* email, const char* dep) { - People *people = &Departments_insert(deps, cstr_from(dep), People_init()).ref->second; + People *people = &Departments_emplace(deps, dep, People_init()).ref->second; People_emplace_or_assign(people, name, email); } int contains(Departments* map, const char* name) { int count = 0; - c_foreach (i, Departments, *map) + c_FOREACH (i, Departments, *map) if (People_contains(&i.ref->second, name)) ++count; return count; @@ -38,7 +44,7 @@ int contains(Departments* map, const char* name) int main(void) { - c_auto (Departments, map) + c_AUTO (Departments, map) { add(&map, "Anna Kendro", "[email protected]", "Support"); add(&map, "Terry Dane", "[email protected]", "Development"); @@ -54,8 +60,8 @@ int main(void) add(&map, "Dennis Kay", "[email protected]", "Marketing"); add(&map, "Anne Dickens", "[email protected]", "Development"); - c_foreach (i, Departments, map) - c_forpair (name, email, People, i.ref->second) + c_FOREACH (i, Departments, map) + c_FORPAIR (name, email, People, i.ref->second) printf("%s: %s - %s\n", cstr_str(&i.ref->first), cstr_str(_.name), cstr_str(_.email)); puts(""); diff --git a/misc/examples/mmap.c b/misc/examples/mmap.c index 3934cf26..ada30b98 100644 --- a/misc/examples/mmap.c +++ b/misc/examples/mmap.c @@ -18,8 +18,8 @@ void print(const char* lbl, const Multimap mmap) { printf("%s ", lbl); - c_foreach (e, Multimap, mmap) { - c_foreach (s, clist_str, e.ref->second) + c_FOREACH (e, Multimap, mmap) { + c_FOREACH (s, clist_str, e.ref->second) printf("{%d,%s} ", e.ref->first, cstr_str(s.ref)); } puts(""); @@ -33,12 +33,12 @@ void insert(Multimap* mmap, int key, const char* str) int main() { - c_auto (Multimap, mmap) + c_AUTO (Multimap, mmap) { typedef struct {int a; const char* b;} pair; // list-initialize - c_forlist (i, pair, {{2, "foo"}, {2, "bar"}, {3, "baz"}, {1, "abc"}, {5, "def"}}) + c_FORLIST (i, pair, {{2, "foo"}, {2, "bar"}, {3, "baz"}, {1, "abc"}, {5, "def"}}) insert(&mmap, i.ref->a, i.ref->b); print("#1", mmap); @@ -54,7 +54,7 @@ int main() print("#4", mmap); // insert using initialization_list - c_forlist (i, pair, {{5, "one"}, {5, "two"}}) + c_FORLIST (i, pair, {{5, "one"}, {5, "two"}}) insert(&mmap, i.ref->a, i.ref->b); print("#5", mmap); @@ -65,7 +65,7 @@ int main() Multimap_clear(&mmap); - c_forlist (i, pair, {{1, "ä"}, {2, "ё"}, {2, "ö"}, {3, "ü"}}) + c_FORLIST (i, pair, {{1, "ä"}, {2, "ё"}, {2, "ö"}, {3, "ü"}}) insert(&mmap, i.ref->a, i.ref->b); print("#6", mmap); } diff --git a/misc/examples/multimap.c b/misc/examples/multimap.c index e72bdce3..ba0bf71b 100644 --- a/misc/examples/multimap.c +++ b/misc/examples/multimap.c @@ -32,14 +32,14 @@ struct OlympicsData { int year; const char *city, *country, *date; } ol_data[] = {1924, "Chamonix", "France", "January 25 - February 5"}, }; -typedef struct { int year; cstr city, date; } OlympicLocation; +typedef struct { int year; cstr city, date; } OlympicLoc; -int OlympicLocation_cmp(const OlympicLocation* a, const OlympicLocation* b); -OlympicLocation OlympicLocation_clone(OlympicLocation loc); -void OlympicLocation_drop(OlympicLocation* self); +int OlympicLoc_cmp(const OlympicLoc* a, const OlympicLoc* b); +OlympicLoc OlympicLoc_clone(OlympicLoc loc); +void OlympicLoc_drop(OlympicLoc* self); -// Create a clist<OlympicLocation>, can be sorted by year. -#define i_valclass OlympicLocation // binds _cmp, _clone and _drop. +// Create a clist<OlympicLoc>, can be sorted by year. +#define i_valclass OlympicLoc // binds _cmp, _clone and _drop. #define i_tag OL #define i_extern // define _clist_mergesort() #include <stc/clist.h> @@ -50,46 +50,49 @@ void OlympicLocation_drop(OlympicLocation* self); #define i_tag OL #include <stc/csmap.h> -int OlympicLocation_cmp(const OlympicLocation* a, const OlympicLocation* b) { +int OlympicLoc_cmp(const OlympicLoc* a, const OlympicLoc* b) { return a->year - b->year; } -OlympicLocation OlympicLocation_clone(OlympicLocation loc) { +OlympicLoc OlympicLoc_clone(OlympicLoc loc) { loc.city = cstr_clone(loc.city); loc.date = cstr_clone(loc.date); return loc; } -void OlympicLocation_drop(OlympicLocation* self) { - c_drop(cstr, &self->city, &self->date); + +void OlympicLoc_drop(OlympicLoc* self) { + cstr_drop(&self->city); + cstr_drop(&self->date); } + int main() { // Define the multimap with destructor defered to when block is completed. - c_auto (csmap_OL, multimap) + c_AUTO (csmap_OL, multimap) { const clist_OL empty = clist_OL_init(); - for (size_t i = 0; i < c_arraylen(ol_data); ++i) + for (size_t i = 0; i < c_ARRAYLEN(ol_data); ++i) { struct OlympicsData* d = &ol_data[i]; - OlympicLocation loc = {.year = d->year, - .city = cstr_from(d->city), - .date = cstr_from(d->date)}; + OlympicLoc loc = {.year = d->year, + .city = cstr_from(d->city), + .date = cstr_from(d->date)}; // Insert an empty list for each new country, and append the entry to the list. // If country already exist in map, its list is returned from the insert function. - clist_OL* list = &csmap_OL_insert(&multimap, cstr_from(d->country), empty).ref->second; + clist_OL* list = &csmap_OL_emplace(&multimap, d->country, empty).ref->second; clist_OL_push_back(list, loc); } // Sort locations by year for each country. - c_foreach (country, csmap_OL, multimap) + c_FOREACH (country, csmap_OL, multimap) clist_OL_sort(&country.ref->second); // Print the multimap: - c_foreach (country, csmap_OL, multimap) + c_FOREACH (country, csmap_OL, multimap) { // Loop the locations for a country sorted by year - c_foreach (loc, clist_OL, country.ref->second) + c_FOREACH (loc, clist_OL, country.ref->second) printf("%s: %d, %s, %s\n", cstr_str(&country.ref->first), loc.ref->year, cstr_str(&loc.ref->city), diff --git a/misc/examples/music_arc.c b/misc/examples/music_arc.c index 162c4c2f..9896c591 100644 --- a/misc/examples/music_arc.c +++ b/misc/examples/music_arc.c @@ -2,21 +2,21 @@ // based on https://docs.microsoft.com/en-us/cpp/cpp/how-to-create-and-use-shared-ptr-instances?view=msvc-160 #include <stc/cstr.h> -struct Song +typedef struct { cstr artist; cstr title; -} typedef Song; +} Song; 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) +Song Song_make(const char* artist, const char* title) { return (Song){cstr_from(artist), cstr_from(title)}; } void Song_drop(Song* s) { printf("drop: %s\n", cstr_str(&s->title)); - c_drop(cstr, &s->artist, &s->title); + c_DROP(cstr, &s->artist, &s->title); } // Define the reference counted type @@ -32,29 +32,29 @@ void Song_drop(Song* s) { void example3() { - c_auto (SongVec, vec1, vec2) + c_AUTO (SongVec, vec1, vec2) { - c_forlist (i, Song, { - 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") + c_FORLIST (i, Song, { + Song_make("Bob Dylan", "The Times They Are A Changing"), + Song_make("Aretha Franklin", "Bridge Over Troubled Water"), + Song_make("Thalia", "Entre El Mar y Una Estrella") }) SongVec_emplace(&vec1, *i.ref); // Share all entries in vec with vec2, except Bob Dylan. - c_foreach (s, SongVec, vec1) + c_FOREACH (s, SongVec, vec1) 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")); + SongVec_emplace(&vec2, Song_make("Michael Jackson", "Billie Jean")); + SongVec_emplace(&vec2, Song_make("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"))); + // SongVec_push(&vec2, SongArc_from(Song_make("Rihanna", "Stay"))); // We now have two vectors with some shared, some unique entries. - c_forlist (i, SongVec, {vec1, vec2}) { + c_FORLIST (i, SongVec, {vec1, vec2}) { puts("VEC:"); - c_foreach (s, SongVec, *i.ref) + c_FOREACH (s, SongVec, *i.ref) printf(" %s - %s, REFS: %ld\n", cstr_str(&s.ref->get->artist), cstr_str(&s.ref->get->title), *s.ref->use_count); diff --git a/misc/examples/new_deq.c b/misc/examples/new_deq.c index 39149140..df9142e7 100644 --- a/misc/examples/new_deq.c +++ b/misc/examples/new_deq.c @@ -36,25 +36,25 @@ int point_cmp(const Point* a, const Point* b) { int main() { - c_auto (cdeq_i32, vec) + c_AUTO (cdeq_i32, vec) { cdeq_i32_push_back(&vec, 123); } - c_auto (cdeq_float, fvec) + c_AUTO (cdeq_float, fvec) { cdeq_float_push_back(&fvec, 123.3f); } - c_auto (cdeq_pnt, pvec) + c_AUTO (cdeq_pnt, pvec) { cdeq_pnt_push_back(&pvec, (Point){42, 14}); cdeq_pnt_push_back(&pvec, (Point){32, 94}); cdeq_pnt_push_front(&pvec, (Point){62, 81}); cdeq_pnt_sort(&pvec); - c_foreach (i, cdeq_pnt, pvec) + c_FOREACH (i, cdeq_pnt, pvec) printf(" (%d %d)", i.ref->x, i.ref->y); puts(""); } - c_auto (cdeq_str, svec) + c_AUTO (cdeq_str, svec) { cdeq_str_emplace_back(&svec, "Hello, friend"); } diff --git a/misc/examples/new_list.c b/misc/examples/new_list.c index 6dbe80b4..c5d90ad1 100644 --- a/misc/examples/new_list.c +++ b/misc/examples/new_list.c @@ -35,27 +35,27 @@ int point_cmp(const Point* a, const Point* b) { int main() { - c_auto (clist_i32, lst) + c_AUTO (clist_i32, lst) clist_i32_push_back(&lst, 123); - c_auto (clist_pnt, plst) { - c_forlist (i, Point, {{42, 14}, {32, 94}, {62, 81}}) + c_AUTO (clist_pnt, plst) { + c_FORLIST (i, Point, {{42, 14}, {32, 94}, {62, 81}}) clist_pnt_push_back(&plst, *i.ref); clist_pnt_sort(&plst); - c_foreach (i, clist_pnt, plst) + c_FOREACH (i, clist_pnt, plst) printf(" (%d %d)", i.ref->x, i.ref->y); puts(""); } - c_auto (clist_float, flst) { - c_forlist (i, float, {123.3f, 321.2f, -32.2f, 78.2f}) + c_AUTO (clist_float, flst) { + c_FORLIST (i, float, {123.3f, 321.2f, -32.2f, 78.2f}) clist_float_push_back(&flst, *i.ref); - c_foreach (i, clist_float, flst) printf(" %g", *i.ref); + c_FOREACH (i, clist_float, flst) printf(" %g", *i.ref); } - c_auto (clist_str, slst) + c_AUTO (clist_str, slst) clist_str_emplace_back(&slst, "Hello, friend"); } diff --git a/misc/examples/new_map.c b/misc/examples/new_map.c index f43d4217..72705eb2 100644 --- a/misc/examples/new_map.c +++ b/misc/examples/new_map.c @@ -42,32 +42,32 @@ int point_cmp(const Point* a, const Point* b) { int main() { - c_auto (cmap_int, map) - c_auto (cmap_pnt, pmap) - c_auto (cmap_str, smap) - c_auto (cset_str, sset) + c_AUTO (cmap_int, map) + c_AUTO (cmap_pnt, pmap) + c_AUTO (cmap_str, smap) + c_AUTO (cset_str, sset) { cmap_int_insert(&map, 123, 321); - c_forlist (i, cmap_pnt_raw, {{{42, 14}, 1}, {{32, 94}, 2}, {{62, 81}, 3}}) + c_FORLIST (i, cmap_pnt_raw, {{{42, 14}, 1}, {{32, 94}, 2}, {{62, 81}, 3}}) cmap_pnt_insert(&pmap, c_PAIR(i.ref)); - c_foreach (i, cmap_pnt, pmap) + c_FOREACH (i, cmap_pnt, pmap) printf(" (%d, %d: %d)", i.ref->first.x, i.ref->first.y, i.ref->second); puts(""); - c_forlist (i, cmap_str_raw, { + c_FORLIST (i, cmap_str_raw, { {"Hello, friend", "long time no see"}, {"So long, friend", "see you around"}, }) cmap_str_emplace(&smap, c_PAIR(i.ref)); - c_forlist (i, const char*, { + c_FORLIST (i, const char*, { "Hello, friend", "Nice to see you again", "So long, friend", }) cset_str_emplace(&sset, *i.ref); - c_foreach (i, cset_str, sset) + c_FOREACH (i, cset_str, sset) printf(" %s\n", cstr_str(i.ref)); } } diff --git a/misc/examples/new_pque.c b/misc/examples/new_pque.c index 2bb1d729..d127b592 100644 --- a/misc/examples/new_pque.c +++ b/misc/examples/new_pque.c @@ -20,16 +20,16 @@ int Point_cmp(const Point* a, const Point* b) { int main() { - c_auto (cstack_int, istk) + c_AUTO (cstack_int, istk) { cstack_int_push(&istk, 123); cstack_int_push(&istk, 321); // print - c_foreach (i, cstack_int, istk) + c_FOREACH (i, cstack_int, istk) printf(" %d", *i.ref); puts(""); } - c_auto (cpque_pnt, pque) + c_AUTO (cpque_pnt, pque) { cpque_pnt_push(&pque, (Point){23, 80}); cpque_pnt_push(&pque, (Point){12, 32}); @@ -43,7 +43,7 @@ int main() } puts(""); } - c_auto (cpque_int, ique) + c_AUTO (cpque_int, ique) { cpque_int_push(&ique, 123); cpque_int_push(&ique, 321); diff --git a/misc/examples/new_queue.c b/misc/examples/new_queue.c index bc7a95c9..c72c94a0 100644 --- a/misc/examples/new_queue.c +++ b/misc/examples/new_queue.c @@ -25,15 +25,15 @@ int main() { stc64_t rng = stc64_new(time(NULL)); stc64_uniform_t dist = stc64_uniform_new(0, n); - c_auto (IQ, Q) + c_AUTO (IQ, Q) { // Push eight million random numbers onto the queue. - c_forrange (n) + c_FORRANGE (n) IQ_push(&Q, stc64_uniform(&rng, &dist)); // Push or pop on the queue 50 million times printf("befor: size %" c_ZU ", capacity %" c_ZU "\n", IQ_size(&Q), IQ_capacity(&Q)); - c_forrange (n) { + c_FORRANGE (n) { int r = stc64_uniform(&rng, &dist); if (r & 3) IQ_push(&Q, r); diff --git a/misc/examples/new_smap.c b/misc/examples/new_smap.c index c77aa185..8b1df828 100644 --- a/misc/examples/new_smap.c +++ b/misc/examples/new_smap.c @@ -42,34 +42,34 @@ int point_cmp(const Point* a, const Point* b) { int main() { - c_auto (csmap_int, imap) { + c_AUTO (csmap_int, imap) { csmap_int_insert(&imap, 123, 321); } - c_auto (PMap, pmap) { - c_forlist (i, PMap_value, { + c_AUTO (PMap, pmap) { + c_FORLIST (i, PMap_value, { {{42, 14}, 1}, {{32, 94}, 2}, {{62, 81}, 3}, }) PMap_insert(&pmap, c_PAIR(i.ref)); - c_forpair (p, i, PMap, pmap) + c_FORPAIR (p, i, PMap, pmap) printf(" (%d,%d: %d)", _.p->x, _.p->y, *_.i); puts(""); } - c_auto (SMap, smap) { - c_forlist (i, SMap_raw, { + c_AUTO (SMap, smap) { + c_FORLIST (i, SMap_raw, { {"Hello, friend", "this is the mapped value"}, {"The brown fox", "jumped"}, {"This is the time", "for all good things"}, }) SMap_emplace(&smap, c_PAIR(i.ref)); - c_forpair (i, j, SMap, smap) + c_FORPAIR (i, j, SMap, smap) printf(" (%s: %s)\n", cstr_str(_.i), cstr_str(_.j)); } - c_auto (SSet, sset) { + c_AUTO (SSet, sset) { SSet_emplace(&sset, "Hello, friend"); SSet_emplace(&sset, "Goodbye, foe"); printf("Found? %s\n", SSet_contains(&sset, "Hello, friend") ? "true" : "false"); diff --git a/misc/examples/new_sptr.c b/misc/examples/new_sptr.c index 2c6b28d6..90ce30b2 100644 --- a/misc/examples/new_sptr.c +++ b/misc/examples/new_sptr.c @@ -42,12 +42,12 @@ Person Person_clone(Person p) { } void Person_drop(Person* p) { printf("drop: %s %s\n", cstr_str(&p->name), cstr_str(&p->last)); - c_drop(cstr, &p->name, &p->last); + c_DROP(cstr, &p->name, &p->last); } int main(void) { - c_auto (PersonArc, p, q, r, s) + c_AUTO (PersonArc, p, q, r, s) { puts("Ex1"); p = PersonArc_from(Person_make("John", "Smiths")); @@ -56,7 +56,7 @@ int main(void) { s = PersonArc_from(Person_clone(*p.get)); // deep copy printf("%s %s: refs %ld\n", cstr_str(&p.get->name), cstr_str(&p.get->last), *p.use_count); } - c_auto (IPStack, vec) + c_AUTO (IPStack, vec) { puts("Ex2"); IPStack_push(&vec, IPtr_from(10)); @@ -65,7 +65,7 @@ int main(void) { IPStack_push(&vec, IPtr_clone(*IPStack_back(&vec))); IPStack_push(&vec, IPtr_clone(*IPStack_front(&vec))); - c_foreach (i, IPStack, vec) + c_FOREACH (i, IPStack, vec) printf(" (%d: refs %ld)", *i.ref->get, *i.ref->use_count); puts(""); } diff --git a/misc/examples/new_vec.c b/misc/examples/new_vec.c index 84e4c7b2..f18bf368 100644 --- a/misc/examples/new_vec.c +++ b/misc/examples/new_vec.c @@ -36,10 +36,10 @@ int point_cmp(const Point* a, const Point* b) { int main() { - c_auto (cvec_i32, vec) - c_auto (cvec_float, fvec) - c_auto (cvec_pnt, pvec) - c_auto (cvec_str, svec) + c_AUTO (cvec_i32, vec) + c_AUTO (cvec_float, fvec) + c_AUTO (cvec_pnt, pvec) + c_AUTO (cvec_str, svec) { cvec_i32_push(&vec, 123); cvec_float_push(&fvec, 123.3f); @@ -49,7 +49,7 @@ int main() cvec_pnt_push(&pvec, (Point){62, 81}); cvec_pnt_push(&pvec, (Point){32, 91}); cvec_pnt_sort(&pvec); - c_foreach (i, cvec_pnt, pvec) + c_FOREACH (i, cvec_pnt, pvec) printf(" (%d %d)", i.ref->x, i.ref->y); puts(""); diff --git a/misc/examples/person_arc.c b/misc/examples/person_arc.c index a7bf2a6f..f782c9da 100644 --- a/misc/examples/person_arc.c +++ b/misc/examples/person_arc.c @@ -24,7 +24,7 @@ Person Person_clone(Person p) { void Person_drop(Person* p) { printf("drop: %s %s\n", cstr_str(&p->name), cstr_str(&p->last)); - c_drop(cstr, &p->name, &p->last); + c_DROP(cstr, &p->name, &p->last); } #define i_type PSPtr @@ -39,8 +39,8 @@ void Person_drop(Person* p) { int main() { - c_auto (Persons, vec) - c_auto (PSPtr, p, q) + c_AUTO (Persons, vec) + c_AUTO (PSPtr, p, q) { p = PSPtr_from(Person_make("Laura", "Palmer")); @@ -57,15 +57,15 @@ int main() Persons_emplace(&vec, Person_make("Dale", "Cooper")); // Clone/share p and q to the vector - c_forlist (i, PSPtr, {p, q}) + c_FORLIST (i, PSPtr, {p, q}) Persons_push(&vec, PSPtr_clone(*i.ref)); - c_foreach (i, Persons, vec) + c_FOREACH (i, Persons, vec) printf("%s %s\n", cstr_str(&i.ref->get->name), cstr_str(&i.ref->get->last)); puts(""); // Look-up Audrey! - c_with (Person a = Person_make("Audrey", "Home"), Person_drop(&a)) { + c_WITH (Person a = Person_make("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/misc/examples/phonebook.c b/misc/examples/phonebook.c index be068409..d0bc5b3a 100644 --- a/misc/examples/phonebook.c +++ b/misc/examples/phonebook.c @@ -32,23 +32,23 @@ void print_phone_book(cmap_str phone_book) { - c_foreach (i, cmap_str, phone_book) + c_FOREACH (i, cmap_str, phone_book) printf("%s\t- %s\n", cstr_str(&i.ref->first), cstr_str(&i.ref->second)); } int main(int argc, char **argv) { - c_auto (cset_str, names) { - c_forlist (i, const char*, {"Hello", "Cool", "True"}) + c_AUTO (cset_str, names) { + c_FORLIST (i, const char*, {"Hello", "Cool", "True"}) cset_str_emplace(&names, *i.ref); - c_foreach (i, cset_str, names) + c_FOREACH (i, cset_str, names) printf("%s ", cstr_str(i.ref)); puts(""); } - c_auto (cmap_str, phone_book) { - c_forlist (i, cmap_str_raw, { + c_AUTO (cmap_str, phone_book) { + c_FORLIST (i, cmap_str_raw, { {"Lilia Friedman", "(892) 670-4739"}, {"Tariq Beltran", "(489) 600-7575"}, {"Laiba Juarez", "(303) 885-5692"}, diff --git a/misc/examples/prime.c b/misc/examples/prime.c index 287fb69b..f6e89e09 100644 --- a/misc/examples/prime.c +++ b/misc/examples/prime.c @@ -2,7 +2,9 @@ #include <math.h> #include <time.h> #include <stc/cbits.h> -#include <stc/views.h> +#include <stc/algo/filter.h> +#include <stc/algo/crange.h> + cbits sieveOfEratosthenes(size_t n) { @@ -28,7 +30,7 @@ int main(void) printf("computing prime numbers up to %" c_ZU "\n", n); clock_t t1 = clock(); - c_with (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(); @@ -36,14 +38,14 @@ int main(void) printf("number of primes: %" c_ZU ", time: %f\n", np, (t2 - t1) / (float)CLOCKS_PER_SEC); puts("Show all the primes in the range [2, 1000):"); printf("2"); - c_forrange (i, 3, 1000, 2) + c_FORRANGE (i, 3, 1000, 2) if (cbits_test(&primes, i>>1)) printf(" %lld", i); puts(""); puts("Show the last 50 primes using a temporary crange generator:"); - c_forfilter (i, crange, crange_literal(n - 1, 0, -2) + c_FORFILTER (i, crange, crange_LITERAL(n - 1, 0, -2) , cbits_test(&primes, *i.ref>>1) - , c_flt_take(i, 50)) { + , c_FLT_TAKE(i, 50)) { printf("%lld ", *i.ref); if (i.count % 10 == 0) puts(""); } diff --git a/misc/examples/priority.c b/misc/examples/priority.c index f6e63205..22da6f60 100644 --- a/misc/examples/priority.c +++ b/misc/examples/priority.c @@ -12,22 +12,22 @@ int main() { size_t N = 10000000; stc64_t rng = stc64_new(time(NULL)); stc64_uniform_t dist = stc64_uniform_new(0, N * 10); - c_auto (cpque_i, heap) + c_AUTO (cpque_i, heap) { // Push ten million random numbers to priority queue printf("Push %" c_ZU " numbers\n", N); - c_forrange (N) + c_FORRANGE (N) cpque_i_push(&heap, stc64_uniform(&rng, &dist)); // push some negative numbers too. - c_forlist (i, int, {-231, -32, -873, -4, -343}) + c_FORLIST (i, int, {-231, -32, -873, -4, -343}) cpque_i_push(&heap, *i.ref); - c_forrange (N) + c_FORRANGE (N) cpque_i_push(&heap, stc64_uniform(&rng, &dist)); puts("Extract the hundred smallest."); - c_forrange (100) { + c_FORRANGE (100) { printf("%" PRId64 " ", *cpque_i_top(&heap)); cpque_i_pop(&heap); } diff --git a/misc/examples/queue.c b/misc/examples/queue.c index f39c4b8b..1d325fc6 100644 --- a/misc/examples/queue.c +++ b/misc/examples/queue.c @@ -11,15 +11,15 @@ int main() { stc64_t rng = stc64_new(1234); dist = stc64_uniform_new(0, n); - c_auto (cqueue_i, queue) + c_AUTO (cqueue_i, queue) { // Push ten million random numbers onto the queue. - c_forrange (n) + c_FORRANGE (n) cqueue_i_push(&queue, stc64_uniform(&rng, &dist)); // Push or pop on the queue ten million times printf("%d\n", n); - c_forrange (n) { // forrange uses initial n only. + c_FORRANGE (n) { // forrange uses initial n only. int r = stc64_uniform(&rng, &dist); if (r & 1) ++n, cqueue_i_push(&queue, r); diff --git a/misc/examples/random.c b/misc/examples/random.c index fe64290d..82214924 100644 --- a/misc/examples/random.c +++ b/misc/examples/random.c @@ -14,7 +14,7 @@ int main() printf("Compare speed of full and unbiased ranged random numbers...\n"); sum = 0; before = clock(); - c_forrange (N) { + c_FORRANGE (N) { sum += (uint32_t)stc64_rand(&rng); } diff = clock() - before; @@ -25,7 +25,7 @@ int main() rng = stc64_new(seed); sum = 0; before = clock(); - c_forrange (N) { + c_FORRANGE (N) { sum += stc64_uniform(&rng, &dist1); // unbiased } diff = clock() - before; @@ -35,7 +35,7 @@ int main() sum = 0; rng = stc64_new(seed); before = clock(); - c_forrange (N) { + c_FORRANGE (N) { sum += stc64_rand(&rng) % (range + 1); // biased } diff = clock() - before; diff --git a/misc/examples/rawptr_elements.c b/misc/examples/rawptr_elements.c index 4b3d2056..05910c77 100644 --- a/misc/examples/rawptr_elements.c +++ b/misc/examples/rawptr_elements.c @@ -10,10 +10,10 @@ typedef int64_t inttype; #define i_key_str #define i_val inttype* #define i_valraw inttype -#define i_valfrom(raw) c_new(inttype, raw) +#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) +#define i_valclone(x) c_NEW(inttype, *x) +#define i_valdrop(x) c_FREE(*x) #include <stc/cmap.h> // With cbox: @@ -28,12 +28,12 @@ typedef int64_t inttype; int main() { - c_auto (SIPtrMap, map, m1) - c_auto (SIBoxMap, m2) + c_AUTO (SIPtrMap, map, m1) + c_AUTO (SIBoxMap, m2) { printf("\nMap with pointer elements:\n"); - SIPtrMap_insert(&map, cstr_from("testing"), c_new(inttype, 1)); - SIPtrMap_insert(&map, cstr_from("done"), c_new(inttype, 2)); + 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: SIPtrMap_emplace(&map, "hello", 3); @@ -41,14 +41,14 @@ int main() m1 = SIPtrMap_clone(map); - c_forpair (name, number, SIPtrMap, m1) + c_FORPAIR (name, number, SIPtrMap, m1) printf("%s: %" PRId64 "\n", cstr_str(_.name), **_.number); puts("\nIBox map:"); SIBoxMap_insert(&m2, cstr_from("Hello"), IBox_make(123)); SIBoxMap_emplace(&m2, "World", 999); - c_forpair (name, number, SIBoxMap, m2) + c_FORPAIR (name, number, SIBoxMap, m2) printf("%s: %d\n", cstr_str(_.name), *_.number->get); puts(""); } diff --git a/misc/examples/read.c b/misc/examples/read.c index 4efdcfeb..ee5dd377 100644 --- a/misc/examples/read.c +++ b/misc/examples/read.c @@ -6,8 +6,8 @@ cvec_str read_file(const char* name) { cvec_str vec = cvec_str_init(); - c_with (FILE* f = fopen(name, "r"), fclose(f)) - c_with (cstr line = cstr_NULL, 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,8 +16,8 @@ cvec_str read_file(const char* name) int main() { int n = 0; - c_with (cvec_str vec = read_file(__FILE__), cvec_str_drop(&vec)) - c_foreach (i, cvec_str, 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)); if (errno) diff --git a/misc/examples/regex1.c b/misc/examples/regex1.c index 7a22220d..9552db62 100644 --- a/misc/examples/regex1.c +++ b/misc/examples/regex1.c @@ -7,8 +7,8 @@ int main(int argc, char* argv[]) printf("Usage: regex1 -i\n"); return 0; } - c_auto (cstr, input) - c_auto (cregex, float_expr) + c_AUTO (cstr, input) + c_AUTO (cregex, float_expr) { int res = cregex_compile(&float_expr, "^[+-]?[0-9]+((\\.[0-9]*)?|\\.[0-9]+)$", CREG_DEFAULT); // Until "q" is given, ask for another number diff --git a/misc/examples/regex2.c b/misc/examples/regex2.c index dbf6aae4..55f8cfc2 100644 --- a/misc/examples/regex2.c +++ b/misc/examples/regex2.c @@ -13,8 +13,8 @@ int main() {"!((abc|123)+)!", "!123abcabc!"} }; - c_auto (cregex, re) - c_forrange (i, c_arraylen(s)) + c_AUTO (cregex, re) + c_FORRANGE (i, c_ARRAYLEN(s)) { int res = cregex_compile(&re, s[i].pattern, CREG_DEFAULT); if (res < 0) { @@ -23,8 +23,8 @@ int main() } printf("input: %s\n", s[i].input); - c_formatch (j, &re, s[i].input) { - c_forrange (k, cregex_captures(&re)) + c_FORMATCH (j, &re, s[i].input) { + c_FORRANGE (k, cregex_captures(&re)) printf(" submatch %lld: %.*s\n", k, c_ARGSV(j.match[k])); puts(""); } diff --git a/misc/examples/regex_match.c b/misc/examples/regex_match.c index 376b002e..b6bd6521 100644 --- a/misc/examples/regex_match.c +++ b/misc/examples/regex_match.c @@ -12,19 +12,19 @@ int main() " Boltzmann const: 1.38064852E-23, is very small." " Bohrradius is 5.29177210903e-11, and Avogadros number is 6.02214076e23."; - c_auto (cregex, re) - c_auto (cstack_float, vec) - c_auto (cstr, nums) + c_AUTO (cregex, re) + c_AUTO (cstack_float, vec) + c_AUTO (cstr, nums) { const char* pattern = "[+-]?([0-9]*\\.)?\\d+([Ee][+-]?\\d+)?"; int res = cregex_compile(&re, pattern, CREG_DEFAULT); printf("%d: %s\n", res, pattern); // extract and convert all numbers in str to floats - c_formatch (i, &re, str) + c_FORMATCH (i, &re, str) cstack_float_push(&vec, atof(i.match[0].str)); - c_foreach (i, cstack_float, vec) + c_FOREACH (i, cstack_float, vec) printf(" %g\n", *i.ref); // extracts the numbers only to a comma separated string. diff --git a/misc/examples/regex_replace.c b/misc/examples/regex_replace.c index e296dfd8..a170856b 100644 --- a/misc/examples/regex_replace.c +++ b/misc/examples/regex_replace.c @@ -17,7 +17,7 @@ int main() const char* pattern = "\\b(\\d\\d\\d\\d)-(1[0-2]|0[1-9])-(3[01]|[12][0-9]|0[1-9])\\b"; const char* input = "start date: 2015-12-31, end date: 2022-02-28"; - c_auto (cstr, str) + c_AUTO (cstr, str) { printf("INPUT: %s\n", input); @@ -26,17 +26,17 @@ int main() printf("fixed: %s\n", cstr_str(&str)); /* US date format, and add 10 years to dates: */ - cstr_take(&str, cregex_replace_pattern_n(pattern, input, "$1/$3/$2", 0, add_10_years, CREG_DEFAULT)); + cstr_take(&str, cregex_replace_pattern_ex(pattern, input, "$1/$3/$2", 0, add_10_years, CREG_DEFAULT)); printf("us+10: %s\n", cstr_str(&str)); /* Wrap first date inside []: */ - cstr_take(&str, cregex_replace_pattern_n(pattern, input, "[$0]", 1, NULL, CREG_DEFAULT)); + cstr_take(&str, cregex_replace_pattern_ex(pattern, input, "[$0]", 1, NULL, CREG_DEFAULT)); printf("brack: %s\n", cstr_str(&str)); /* Shows how to compile RE separately */ - c_with (cregex re = cregex_from(pattern, CREG_DEFAULT), cregex_drop(&re)) { + c_WITH (cregex re = cregex_from(pattern, CREG_DEFAULT), cregex_drop(&re)) { if (cregex_captures(&re) == 0) - continue; /* break c_with */ + continue; /* break c_WITH */ /* European date format. */ cstr_take(&str, cregex_replace(&re, input, "$3.$2.$1")); printf("euros: %s\n", cstr_str(&str)); diff --git a/misc/examples/replace.c b/misc/examples/replace.c index 15cf3bae..ca305ecd 100644 --- a/misc/examples/replace.c +++ b/misc/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_defer (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/misc/examples/shape.c b/misc/examples/shape.c index a17208ef..4f7903da 100644 --- a/misc/examples/shape.c +++ b/misc/examples/shape.c @@ -38,7 +38,7 @@ void Shape_delete(Shape* shape) { if (shape) { shape->api->drop(shape); - c_free(shape); + c_FREE(shape); } } @@ -111,7 +111,7 @@ static void Polygon_draw(const Shape* shape) { const Polygon* self = c_dyn_cast(Polygon, shape); printf("Polygon :"); - c_foreach (i, PointVec, self->points) + c_FOREACH (i, PointVec, self->points) printf(" (%g,%g)", i.ref->x, i.ref->y); puts(""); } @@ -137,23 +137,23 @@ void testShape(const Shape* shape) int main(void) { - c_auto (Shapes, shapes) + c_AUTO (Shapes, shapes) { - Triangle* tri1 = c_new(Triangle, Triangle_from((Point){5, 7}, (Point){12, 7}, (Point){12, 20})); - Polygon* pol1 = c_new(Polygon, Polygon_init()); - Polygon* pol2 = c_new(Polygon, Polygon_init()); + Triangle* tri1 = c_NEW(Triangle, Triangle_from((Point){5, 7}, (Point){12, 7}, (Point){12, 20})); + Polygon* pol1 = c_NEW(Polygon, Polygon_init()); + Polygon* pol2 = c_NEW(Polygon, Polygon_init()); - c_forlist (i, Point, {{50, 72}, {123, 73}, {127, 201}, {828, 333}}) + c_FORLIST (i, Point, {{50, 72}, {123, 73}, {127, 201}, {828, 333}}) Polygon_addPoint(pol1, *i.ref); - c_forlist (i, Point, {{5, 7}, {12, 7}, {12, 20}, {82, 33}, {17, 56}}) + c_FORLIST (i, Point, {{5, 7}, {12, 7}, {12, 20}, {82, 33}, {17, 56}}) Polygon_addPoint(pol2, *i.ref); Shapes_push(&shapes, &tri1->shape); Shapes_push(&shapes, &pol1->shape); Shapes_push(&shapes, &pol2->shape); - c_foreach (i, Shapes, shapes) + c_FOREACH (i, Shapes, shapes) testShape(*i.ref); } } diff --git a/misc/examples/sidebyside.cpp b/misc/examples/sidebyside.cpp index 80c934a4..ca80664d 100644 --- a/misc/examples/sidebyside.cpp +++ b/misc/examples/sidebyside.cpp @@ -25,7 +25,7 @@ int main() { std::cout << std::endl; } - c_auto (IIMap, hist) + c_AUTO (IIMap, hist) { IIMap_insert(&hist, 12, 100).ref->second += 1; IIMap_insert(&hist, 13, 100).ref->second += 1; @@ -45,9 +45,9 @@ int main() { std::cout << std::endl; } - c_auto (SIMap, food) + c_AUTO (SIMap, food) { - c_forlist (i, SIMap_raw, {{"burger", 5}, {"pizza", 12}, {"steak", 15}}) + c_FORLIST (i, SIMap_raw, {{"burger", 5}, {"pizza", 12}, {"steak", 15}}) SIMap_emplace(&food, c_PAIR(i.ref)); c_foreach (i, SIMap, food) diff --git a/misc/examples/sort.c b/misc/examples/sort.c new file mode 100644 index 00000000..65ae7359 --- /dev/null +++ b/misc/examples/sort.c @@ -0,0 +1,48 @@ +#include <stdio.h> +#include <time.h> +#include <stdlib.h> +#define i_val int +#include <stc/crandom.h> +#include <stc/algo/csort.h> +#ifdef __cplusplus +#include <algorithm> +#endif + + +int testsort(csortval_int *a, size_t size, const char *desc) { + clock_t t = clock(); +#ifdef __cplusplus + printf("std::sort: "); + std::sort(a, a + size); +#else + printf("csort: "); + csort_int(a, size); +#endif + t = clock() - t; + + printf("%s: %d elements sorted in %.3fms\n", + desc, (int)size, t*1000.0/CLOCKS_PER_SEC); + return 0; +} + +int main(int argc, char *argv[]) { + size_t i, size = argc > 1 ? strtoull(argv[1], NULL, 0) : 10000000; + csortval_int *a = (csortval_int*)malloc(sizeof(*a) * size); + if (a == NULL) return -1; + + for (i = 0; i < size; i++) + a[i] = crandom() & ((1U << 28) - 1); + testsort(a, size, "random"); + for (i = 0; i < 20; i++) printf(" %d", a[i]); + puts(""); + + testsort(a, size, "sorted"); + + for (i = 0; i < size; i++) a[i] = size - i; + testsort(a, size, "reverse sorted"); + + for (i = 0; i < size; i++) a[i] = 126735; + testsort(a, size, "constant"); + + free(a); +} diff --git a/misc/examples/sorted_map.c b/misc/examples/sorted_map.c index c4a05c76..47509edb 100644 --- a/misc/examples/sorted_map.c +++ b/misc/examples/sorted_map.c @@ -9,7 +9,7 @@ int main() { // empty map containers - c_auto (csmap_int, gquiz1, gquiz2) + c_AUTO (csmap_int, gquiz1, gquiz2) { // insert elements in random order csmap_int_insert(&gquiz1, 2, 30); @@ -22,17 +22,17 @@ int main() // printing map gquiz1 printf("\nThe map gquiz1 is :\n\tKEY\tELEMENT\n"); - c_foreach (itr, csmap_int, gquiz1) + c_FOREACH (itr, csmap_int, gquiz1) printf("\t%d\t%d\n", itr.ref->first, itr.ref->second); printf("\n"); // assigning the elements from gquiz1 to gquiz2 - c_foreach (i, csmap_int, gquiz1) + c_FOREACH (i, csmap_int, gquiz1) csmap_int_insert(&gquiz2, i.ref->first, i.ref->second); // print all elements of the map gquiz2 printf("\nThe map gquiz2 is :\n\tKEY\tELEMENT\n"); - c_foreach (itr, csmap_int, gquiz2) + c_FOREACH (itr, csmap_int, gquiz2) printf("\t%d\t%d\n", itr.ref->first, itr.ref->second); printf("\n"); @@ -41,7 +41,7 @@ int main() printf("\tKEY\tELEMENT\n"); csmap_int_erase_range(&gquiz2, csmap_int_begin(&gquiz2), csmap_int_find(&gquiz2, 3)); - c_foreach (itr, csmap_int, gquiz2) + c_FOREACH (itr, csmap_int, gquiz2) printf("\t%d\t%d\n", itr.ref->first, itr.ref->second); printf("\n"); @@ -49,7 +49,7 @@ int main() int num = csmap_int_erase(&gquiz2, 4); printf("\ngquiz2.erase(4) : %d removed\n", num); printf("\tKEY\tELEMENT\n"); - c_foreach (itr, csmap_int, gquiz2) + c_FOREACH (itr, csmap_int, gquiz2) printf("\t%d\t%d\n", itr.ref->first, itr.ref->second); printf("\n"); diff --git a/misc/examples/splitstr.c b/misc/examples/splitstr.c index a3c12a3a..70d1935e 100644 --- a/misc/examples/splitstr.c +++ b/misc/examples/splitstr.c @@ -5,15 +5,15 @@ int main() { - puts("Split with c_fortoken (csview):"); + puts("Split with c_FORTOKEN (csview):"); - c_fortoken (i, "Hello World C99!", " ") + c_FORTOKEN (i, "Hello World C99!", " ") printf("'%.*s'\n", c_ARGSV(i.token)); - puts("\nSplit with c_formatch (regex):"); + puts("\nSplit with c_FORMATCH (regex):"); - c_with (cregex re = cregex_from("[^ ]+", CREG_DEFAULT), cregex_drop(&re)) - c_formatch (i, &re, " Hello World C99! ") + c_WITH (cregex re = cregex_from("[^ ]+", CREG_DEFAULT), cregex_drop(&re)) + c_FORMATCH (i, &re, " Hello World C99! ") printf("'%.*s'\n", c_ARGSV(i.match[0])); } diff --git a/misc/examples/sso_map.c b/misc/examples/sso_map.c index cc5e16a1..3627d101 100644 --- a/misc/examples/sso_map.c +++ b/misc/examples/sso_map.c @@ -5,11 +5,11 @@ int main() { - c_auto (cmap_str, m) { + c_AUTO (cmap_str, m) { cmap_str_emplace(&m, "Test short", "This is a short string."); cmap_str_emplace(&m, "Test long ", "This is a longer string."); - c_forpair (k, v, cmap_str, m) + c_FORPAIR (k, v, cmap_str, m) printf("%s: '%s' Len=%" c_ZU ", Is long: %s\n", cstr_str(_.k), cstr_str(_.v), cstr_size(_.v), cstr_is_long(_.v)?"true":"false"); diff --git a/misc/examples/sso_substr.c b/misc/examples/sso_substr.c index be372a8d..8e7450ba 100644 --- a/misc/examples/sso_substr.c +++ b/misc/examples/sso_substr.c @@ -16,5 +16,5 @@ int main () cstr s3 = cstr_from_sv(cstr_substr_ex(&str, 0, 6)); // "apples" printf("%s %s: %d, %d\n", cstr_str(&s2), cstr_str(&s3), cstr_is_long(&str), cstr_is_long(&s2)); - c_drop (cstr, &str, &s2, &s3); + c_DROP (cstr, &str, &s2, &s3); } diff --git a/misc/examples/stack.c b/misc/examples/stack.c index a6a4a492..370577b9 100644 --- a/misc/examples/stack.c +++ b/misc/examples/stack.c @@ -11,18 +11,18 @@ #include <stc/cstack.h> int main() { - c_auto (cstack_i, stack) - c_auto (cstack_c, chars) + c_AUTO (cstack_i, stack) + c_AUTO (cstack_c, chars) { - c_forrange (i, 101) + c_FORRANGE (i, 101) cstack_i_push(&stack, i*i); printf("%d\n", *cstack_i_top(&stack)); - c_forrange (i, 90) + c_FORRANGE (i, 90) cstack_i_pop(&stack); - c_foreach (i, cstack_i, stack) + c_FOREACH (i, cstack_i, stack) printf(" %d", *i.ref); puts(""); printf("top: %d\n", *cstack_i_top(&stack)); diff --git a/misc/examples/sview_split.c b/misc/examples/sview_split.c index d22fccd1..5658924e 100644 --- a/misc/examples/sview_split.c +++ b/misc/examples/sview_split.c @@ -13,7 +13,7 @@ int main() printf("%.*s, %.*s, %.*s\n", c_ARGSV(year), c_ARGSV(month), c_ARGSV(day)); - c_auto (cstr, y, m, d) { + c_AUTO (cstr, y, m, d) { y = cstr_from_sv(year), m = cstr_from_sv(month), d = cstr_from_sv(day); printf("%s, %s, %s\n", cstr_str(&y), cstr_str(&m), cstr_str(&d)); } diff --git a/misc/examples/unordered_set.c b/misc/examples/unordered_set.c index a0b639da..509f97e1 100644 --- a/misc/examples/unordered_set.c +++ b/misc/examples/unordered_set.c @@ -7,7 +7,7 @@ int main() { // declaring set for storing string data-type - c_auto (cset_str, stringSet) + c_AUTO (cset_str, stringSet) { // inserting various string, same string will be stored // once in set @@ -36,7 +36,7 @@ int main() // now iterating over whole set and printing its // content printf("All elements :\n"); - c_foreach (itr, cset_str, stringSet) + c_FOREACH (itr, cset_str, stringSet) printf("%s\n", cstr_str(itr.ref)); } } diff --git a/misc/examples/utf8replace_c.c b/misc/examples/utf8replace_c.c index 22a5c990..2d8d1921 100644 --- a/misc/examples/utf8replace_c.c +++ b/misc/examples/utf8replace_c.c @@ -1,23 +1,26 @@ -#define i_extern // add utf8 dependencies #include <stc/cstr.h> -#include <stc/csview.h> int main() { - c_auto (cstr, hello, upper) { + c_AUTO (cstr, hello, str) { hello = cstr_lit("hell😀 w😀rld"); printf("%s\n", cstr_str(&hello)); /* replace second smiley at utf8 codepoint pos 7 */ - cstr_u8_replace(&hello, cstr_u8_to_pos(&hello, 7), 1, c_SV("🐨")); + cstr_u8_replace_at(&hello, + cstr_u8_to_pos(&hello, 7), + 1, + c_SV("🐨") + ); printf("%s\n", cstr_str(&hello)); - cstr_replace(&hello, "🐨", "ø", 1); - printf("%s\n", cstr_str(&hello)); - - upper = cstr_toupper_sv(cstr_sv(&hello)); - - c_foreach (c, cstr, hello) + c_FOREACH (c, cstr, hello) printf("%.*s,", c_ARGSV(c.u8.chr)); - puts(""); + + //csview sv = c_SV("If you find the time, you will find the winner"); + //str = cstr_replace_sv(sv, c_SV("find"), c_SV("match"), 0); + + str = cstr_lit("If you find the time, you will find the winner"); + cstr_replace(&str, "find", "match"); + printf("\n%s\n", cstr_str(&str)); } } diff --git a/misc/examples/utf8replace_rs.rs b/misc/examples/utf8replace_rs.rs index 717978aa..8b163b4e 100644 --- a/misc/examples/utf8replace_rs.rs +++ b/misc/examples/utf8replace_rs.rs @@ -1,12 +1,12 @@ - pub fn main() { - let mut hello = String::from("hell😀 world"); + let mut hello = String::from("hell😀 w😀rld"); println!("{}", hello); - + + /* replace second smiley at utf8 codepoint pos 7 */ hello.replace_range( hello .char_indices() - .nth(4) + .nth(7) .map(|(pos, ch)| (pos..pos + ch.len_utf8())) .unwrap(), "🐨", @@ -16,4 +16,7 @@ pub fn main() { for c in hello.chars() { print!("{},", c); } + + let str = "If you find the time, you will find the winner"; + println!("\n{}", str.replace("find", "match")); } diff --git a/misc/examples/vikings.c b/misc/examples/vikings.c index 26a74757..c65a9caa 100644 --- a/misc/examples/vikings.c +++ b/misc/examples/vikings.c @@ -50,7 +50,7 @@ static inline RViking Viking_toraw(const Viking* vp) { int main() { - c_auto (Vikings, vikings) { + c_AUTO (Vikings, vikings) { Vikings_emplace(&vikings, (RViking){"Einar", "Norway"}, 20); Vikings_emplace(&vikings, (RViking){"Olaf", "Denmark"}, 24); Vikings_emplace(&vikings, (RViking){"Harald", "Iceland"}, 12); @@ -59,7 +59,7 @@ int main() Vikings_value* v = Vikings_get_mut(&vikings, (RViking){"Einar", "Norway"}); v->second += 3; // add 3 hp points to Einar - c_forpair (vk, hp, Vikings, vikings) { + c_FORPAIR (vk, hp, Vikings, vikings) { printf("%s of %s has %d hp\n", cstr_str(&_.vk->name), cstr_str(&_.vk->country), *_.hp); } } diff --git a/misc/examples/words.c b/misc/examples/words.c index 097447aa..b945092f 100644 --- a/misc/examples/words.c +++ b/misc/examples/words.c @@ -10,19 +10,19 @@ int main1() { - c_auto (cvec_str, words) - c_auto (cmap_str, word_map) + c_AUTO (cvec_str, words) + c_AUTO (cmap_str, word_map) { - c_forlist (i, const char*, { + c_FORLIST (i, const char*, { "this", "sentence", "is", "not", "a", "sentence", "this", "sentence", "is", "a", "hoax" }) cvec_str_emplace_back(&words, *i.ref); - c_foreach (w, cvec_str, words) { + c_FOREACH (w, cvec_str, words) { cmap_str_emplace(&word_map, cstr_str(w.ref), 0).ref->second += 1; } - c_foreach (i, cmap_str, word_map) { + c_FOREACH (i, cmap_str, word_map) { printf("%d occurrences of word '%s'\n", i.ref->second, cstr_str(&i.ref->first)); } diff --git a/misc/include/fmt.h b/misc/include/c11/fmt.h index df1fe990..d5ba3575 100644 --- a/misc/include/fmt.h +++ b/misc/include/c11/fmt.h @@ -30,12 +30,14 @@ int main() { const double pi = 3.141592653589793;
const size_t x = 1234567890;
const char* string = "Hello world";
+ const wchar_t* wstr = L"The whole";
const char z = 'z';
_Bool flag = 1;
unsigned char r = 123, g = 214, b = 90, w = 110;
char buffer[64];
fmt_print(1, "Color: ({} {} {}), {}\n", r, g, b, flag);
+ fmt_print(1, "Wide: {}, {}\n", wstr, L"wide world");
fmt_print(1, "{:10} {:10} {:10.2f}\n", 42ull, 43, pi);
fmt_print(stdout, "{:>10} {:>10} {:>10}\n", z, z, w);
fmt_print(stdout, "{:10} {:10} {:10}\n", "Hello", "Mad", "World");
@@ -72,6 +74,11 @@ int main() { #else
# define FMT_API static inline
#endif
+#if defined FMT_NDEBUG || defined NDEBUG
+# define fmt_OK(exp) (void)(exp)
+#else
+# define fmt_OK(exp) assert(exp)
+#endif
typedef struct {
char* data;
@@ -92,48 +99,48 @@ FMT_API void _fmt_bprint(fmt_buffer*, const char* fmt, ...); #define fmt_print(...) fmt_OVERLOAD(fmt_print, __VA_ARGS__)
#define fmt_print2(to, fmt) \
do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 0, fmt); \
- assert(_n == 0); _fmt_fn(to)(to, fmt); } while (0)
+ fmt_OK(_n == 0); _fmt_fn(to)(to, fmt); } while (0)
#define fmt_print3(to, fmt, c) \
do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 1, fmt, _fc(c)); \
- assert(_n == 1); _fmt_fn(to)(to, _fs, c); } while (0)
+ fmt_OK(_n == 1); _fmt_fn(to)(to, _fs, c); } while (0)
#define fmt_print4(to, fmt, c, d) \
do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 2, fmt, _fc(c), _fc(d)); \
- assert(_n == 2); _fmt_fn(to)(to, _fs, c, d); } while (0)
+ fmt_OK(_n == 2); _fmt_fn(to)(to, _fs, c, d); } while (0)
#define fmt_print5(to, fmt, c, d, e) \
do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 3, fmt, _fc(c), _fc(d), _fc(e)); \
- assert(_n == 3); _fmt_fn(to)(to, _fs, c, d, e); } while (0)
+ fmt_OK(_n == 3); _fmt_fn(to)(to, _fs, c, d, e); } while (0)
#define fmt_print6(to, fmt, c, d, e, f) \
do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 4, fmt, _fc(c), _fc(d), _fc(e), _fc(f)); \
- assert(_n == 4); _fmt_fn(to)(to, _fs, c, d, e, f); } while (0)
+ fmt_OK(_n == 4); _fmt_fn(to)(to, _fs, c, d, e, f); } while (0)
#define fmt_print7(to, fmt, c, d, e, f, g) \
do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 5, fmt, _fc(c), _fc(d), _fc(e), _fc(f), _fc(g)); \
- assert(_n == 5); _fmt_fn(to)(to, _fs, c, d, e, f, g); } while (0)
+ fmt_OK(_n == 5); _fmt_fn(to)(to, _fs, c, d, e, f, g); } while (0)
#define fmt_print8(to, fmt, c, d, e, f, g, h) \
do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 6, fmt, _fc(c), _fc(d), _fc(e), _fc(f), _fc(g), _fc(h)); \
- assert(_n == 6); _fmt_fn(to)(to, _fs, c, d, e, f, g, h); } while (0)
+ fmt_OK(_n == 6); _fmt_fn(to)(to, _fs, c, d, e, f, g, h); } while (0)
#define fmt_print9(to, fmt, c, d, e, f, g, h, i) \
do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 7, fmt, _fc(c), _fc(d), _fc(e), _fc(f), _fc(g), _fc(h), _fc(i)); \
- assert(_n == 7); _fmt_fn(to)(to, _fs, c, d, e, f, g, h, i); } while (0)
+ fmt_OK(_n == 7); _fmt_fn(to)(to, _fs, c, d, e, f, g, h, i); } while (0)
#define fmt_print10(to, fmt, c, d, e, f, g, h, i, j) \
do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 8, fmt, _fc(c), _fc(d), _fc(e), _fc(f), _fc(g), _fc(h), \
_fc(i), _fc(j)); \
- assert(_n == 8); _fmt_fn(to)(to, _fs, c, d, e, f, g, h, i, j); } while (0)
+ fmt_OK(_n == 8); _fmt_fn(to)(to, _fs, c, d, e, f, g, h, i, j); } while (0)
#define fmt_print11(to, fmt, c, d, e, f, g, h, i, j, k) \
do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 9, fmt, _fc(c), _fc(d), _fc(e), _fc(f), _fc(g), _fc(h), \
_fc(i), _fc(j), _fc(k)); \
- assert(_n == 9); _fmt_fn(to)(to, _fs, c, d, e, f, g, h, i, j, k); } while (0)
+ fmt_OK(_n == 9); _fmt_fn(to)(to, _fs, c, d, e, f, g, h, i, j, k); } while (0)
#define fmt_print12(to, fmt, c, d, e, f, g, h, i, j, k, m) \
do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 10, fmt, _fc(c), _fc(d), _fc(e), _fc(f), _fc(g), _fc(h), \
_fc(i), _fc(j), _fc(k), _fc(m)); \
- assert(_n == 10); _fmt_fn(to)(to, _fs, c, d, e, f, g, h, i, j, k, m); } while (0)
+ fmt_OK(_n == 10); _fmt_fn(to)(to, _fs, c, d, e, f, g, h, i, j, k, m); } while (0)
#define fmt_print13(to, fmt, c, d, e, f, g, h, i, j, k, m, n) \
do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 11, fmt, _fc(c), _fc(d), _fc(e), _fc(f), _fc(g), _fc(h), \
_fc(i), _fc(j), _fc(k), _fc(m), _fc(n)); \
- assert(_n == 11); _fmt_fn(to)(to, _fs, c, d, e, f, g, h, i, j, k, m, n); } while (0)
+ fmt_OK(_n == 11); _fmt_fn(to)(to, _fs, c, d, e, f, g, h, i, j, k, m, n); } while (0)
#define fmt_print14(to, fmt, c, d, e, f, g, h, i, j, k, m, n, o) \
do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 12, fmt, _fc(c), _fc(d), _fc(e), _fc(f), _fc(g), _fc(h), \
_fc(i), _fc(j), _fc(k), _fc(m), _fc(n), _fc(o)); \
- assert(_n == 12); _fmt_fn(to)(to, _fs, c, d, e, f, g, h, i, j, k, m, n, o); } while (0)
+ fmt_OK(_n == 12); _fmt_fn(to)(to, _fs, c, d, e, f, g, h, i, j, k, m, n, o); } while (0)
#define _fmt_fn(x) _Generic ((x), \
FILE*: fprintf, \
@@ -164,8 +171,10 @@ FMT_API void _fmt_bprint(fmt_buffer*, const char* fmt, ...); double: "@g", \
long double: "@Lg", \
char*: "s", \
+ wchar_t*: "ls", \
void*: "p", \
const char*: "s", \
+ const wchar_t*: "ls", \
const void*: "p")
#if defined FMT_IMPLEMENT || !(defined FMT_HEADER || defined FMT_IMPLEMENT)
@@ -250,4 +259,4 @@ FMT_API int _fmt_parse(char* p, int nargs, const char *fmt, ...) { return n;
}
#endif
-#endif
\ No newline at end of file +#endif
diff --git a/misc/include/old/carr2.h b/misc/include/old/carr2.h new file mode 100644 index 00000000..aebaec81 --- /dev/null +++ b/misc/include/old/carr2.h @@ -0,0 +1,152 @@ +/* MIT License + * + * Copyright (c) 2022 Tyge Løvset, NORCE, www.norceresearch.no + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include <stc/ccommon.h> + +#ifndef CARR2_H_INCLUDED +#define CARR2_H_INCLUDED +#include <stc/forward.h> +#include <stdlib.h> +#endif +/* +// carr2- 2D dynamic array in one memory block with easy indexing. +#define i_key int +#include <stc/carr2.h> +#include <stdio.h> + +int main() { + int w = 7, h = 5; + c_with (carr2_int image = carr2_int_new_uninit(w, h), carr2_int_drop(&image)) + { + int *dat = carr2_int_data(&image); + for (int i = 0; i < carr2_int_size(&image); ++i) + dat[i] = i; + + for (int x = 0; x < image.xdim; ++x) + for (int y = 0; y < image.ydim; ++y) + printf(" %d", image.data[x][y]); + puts("\n"); + + c_foreach (i, carr2_int, image) + printf(" %d", *i.ref); + puts(""); + } +} +*/ + +#ifndef _i_prefix +#define _i_prefix carr2_ +#endif +#include <stc/priv/template.h> +#if !c_option(c_is_forward) +_cx_deftypes(_c_carr2_types, _cx_self, i_key); +#endif + +STC_API _cx_self _cx_memb(_with_size)(size_t xdim, size_t ydim, i_key null); +STC_API _cx_self _cx_memb(_with_data)(size_t xdim, size_t ydim, _cx_value* storage); +STC_API _cx_value* _cx_memb(_release)(_cx_self* self); +STC_API void _cx_memb(_drop)(_cx_self* self); +#if !defined i_no_clone +STC_API _cx_self _cx_memb(_clone)(_cx_self src); +STC_API void _cx_memb(_copy)(_cx_self *self, const _cx_self* other); +#endif + +STC_INLINE _cx_self _cx_memb(_new_uninit)(size_t xdim, size_t ydim) { + return _cx_memb(_with_data)(xdim, ydim, c_alloc_n(_cx_value, xdim*ydim)); +} +STC_INLINE size_t _cx_memb(_size)(const _cx_self* self) + { return self->xdim*self->ydim; } + +STC_INLINE _cx_value *_cx_memb(_data)(_cx_self* self) + { return *self->data; } + +STC_INLINE const _cx_value *_cx_memb(_at)(const _cx_self* self, size_t x, size_t y) { + assert(x < self->xdim && y < self->ydim); + return *self->data + self->ydim*x + y; +} + +STC_INLINE size_t _cx_memb(_idx)(const _cx_self* self, size_t x, size_t y) { + return self->ydim*x + y; +} + + +STC_INLINE _cx_iter _cx_memb(_begin)(const _cx_self* self) { + size_t n = self->xdim*self->ydim; + return c_INIT(_cx_iter){n ? *self->data : NULL, *self->data + n}; +} + +STC_INLINE _cx_iter _cx_memb(_end)(const _cx_self* self) + { return c_INIT(_cx_iter){NULL, *self->data + self->xdim*self->ydim}; } + +STC_INLINE void _cx_memb(_next)(_cx_iter* it) + { if (++it->ref == it->end) it->ref = NULL; } + +/* -------------------------- IMPLEMENTATION ------------------------- */ +#if defined(i_implement) + +STC_DEF _cx_self _cx_memb(_with_data)(size_t xdim, size_t ydim, _cx_value* block) { + _cx_self _arr = {c_alloc_n(_cx_value*, xdim), xdim, ydim}; + for (size_t x = 0; x < xdim; ++x, block += ydim) + _arr.data[x] = block; + return _arr; +} + +STC_DEF _cx_self _cx_memb(_with_size)(size_t xdim, size_t ydim, i_key null) { + _cx_self _arr = _cx_memb(_new_uninit)(xdim, ydim); + for (_cx_value* p = _arr.data[0], *e = p + xdim*ydim; p != e; ++p) + *p = null; + return _arr; +} + +#if !defined i_no_clone + +STC_DEF _cx_self _cx_memb(_clone)(_cx_self src) { + _cx_self _arr = _cx_memb(_new_uninit)(src.xdim, src.ydim); + for (_cx_value* p = _arr.data[0], *q = src.data[0], *e = p + _cx_memb(_size)(&src); p != e; ++p, ++q) + *p = i_keyclone((*q)); + return _arr; +} + +STC_DEF void _cx_memb(_copy)(_cx_self *self, const _cx_self* other) { + if (self->data == other->data) return; + _cx_memb(_drop)(self); *self = _cx_memb(_clone)(*other); +} +#endif + +STC_DEF _cx_value *_cx_memb(_release)(_cx_self* self) { + _cx_value *values = self->data[0]; + c_free(self->data); + self->data = NULL; + return values; +} + +STC_DEF void _cx_memb(_drop)(_cx_self* self) { + if (!self->data) return; + for (_cx_value* p = self->data[0], *q = p + _cx_memb(_size)(self); p != q; ) { + --q; i_keydrop(q); + } + c_free(self->data[0]); /* values */ + c_free(self->data); /* pointers */ +} + +#endif +#include <stc/priv/template.h> diff --git a/misc/include/old/carr3.h b/misc/include/old/carr3.h new file mode 100644 index 00000000..a0148992 --- /dev/null +++ b/misc/include/old/carr3.h @@ -0,0 +1,157 @@ +/* MIT License + * + * Copyright (c) 2022 Tyge Løvset, NORCE, www.norceresearch.no + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include <stc/ccommon.h> + +#ifndef CARR3_H_INCLUDED +#define CARR3_H_INCLUDED +#include <stc/forward.h> +#include <stdlib.h> +#endif +/* +// carr3 - 3D dynamic array in one memory block with easy indexing. +#define i_key int +#include <stc/carr3.h> +#include <stdio.h> + +int main() { + int w = 7, h = 5, d = 3; + c_with (carr3_int image = carr3_int_new_uninit(w, h, d), carr3_int_drop(&image)) + { + int *dat = carr3_int_data(&image); + for (int i = 0; i < carr3_int_size(&image); ++i) + dat[i] = i; + + for (int x = 0; x < image.xdim; ++x) + for (int y = 0; y < image.ydim; ++y) + for (int z = 0; z < image.zdim; ++z) + printf(" %d", image.data[x][y][z]); + puts("\n"); + + c_foreach (i, carr3_int, image) + printf(" %d", *i.ref); + puts(""); + } +} +*/ + +#ifndef _i_prefix +#define _i_prefix carr3_ +#endif +#include <stc/priv/template.h> + +#if !c_option(c_is_forward) +_cx_deftypes(_c_carr3_types, _cx_self, i_key); +#endif + +STC_API _cx_self _cx_memb(_with_size)(size_t xdim, size_t ydim, size_t zdim, i_key null); +STC_API _cx_self _cx_memb(_with_data)(size_t xdim, size_t ydim, size_t zdim, _cx_value* storage); +STC_API _cx_value* _cx_memb(_release)(_cx_self* self); +STC_API void _cx_memb(_drop)(_cx_self* self); +#if !defined i_no_clone +STC_API _cx_self _cx_memb(_clone)(_cx_self src); +STC_API void _cx_memb(_copy)(_cx_self *self, const _cx_self* other); +#endif + +STC_INLINE _cx_self _cx_memb(_new_uninit)(size_t xdim, size_t ydim, size_t zdim) { + return _cx_memb(_with_data)(xdim, ydim, zdim, c_alloc_n(_cx_value, xdim*ydim*zdim)); +} + +STC_INLINE size_t _cx_memb(_size)(const _cx_self* self) + { return self->xdim*self->ydim*self->zdim; } + +STC_INLINE _cx_value* _cx_memb(_data)(_cx_self* self) + { return **self->data; } + +STC_INLINE const _cx_value* _cx_memb(_at)(const _cx_self* self, size_t x, size_t y, size_t z) { + assert(x < self->xdim && y < self->ydim && z < self->zdim); + return **self->data + self->zdim*(self->ydim*x + y) + z; +} + +STC_INLINE size_t _cx_memb(_idx)(const _cx_self* self, size_t x, size_t y, size_t z) { + return self->zdim*(self->ydim*x + y) + z; +} + + +STC_INLINE _cx_iter _cx_memb(_begin)(const _cx_self* self) { + size_t n = _cx_memb(_size)(self); + return c_INIT(_cx_iter){n ? **self->data : NULL, **self->data + n}; +} + +STC_INLINE _cx_iter _cx_memb(_end)(const _cx_self* self) + { return c_INIT(_cx_iter){NULL, **self->data + _cx_memb(_size)(self)}; } + +STC_INLINE void _cx_memb(_next)(_cx_iter* it) + { if (++it->ref == it->end) it->ref = NULL; } + +/* -------------------------- IMPLEMENTATION ------------------------- */ +#if defined(i_implement) + +STC_DEF _cx_self _cx_memb(_with_data)(size_t xdim, size_t ydim, size_t zdim, _cx_value* block) { + _cx_self _arr = {c_alloc_n(_cx_value**, xdim*(ydim + 1)), xdim, ydim, zdim}; + _cx_value** p = (_cx_value**) &_arr.data[xdim]; + for (size_t x = 0, y; x < xdim; ++x, p += ydim) + for (y = 0, _arr.data[x] = p; y < ydim; ++y, block += zdim) + p[y] = block; + return _arr; +} + +STC_DEF _cx_self _cx_memb(_with_size)(size_t xdim, size_t ydim, size_t zdim, i_key null) { + _cx_self _arr = _cx_memb(_new_uninit)(xdim, ydim, zdim); + for (_cx_value* p = **_arr.data, *e = p + xdim*ydim*zdim; p != e; ++p) + *p = null; + return _arr; +} + +#if !defined i_no_clone + +STC_DEF _cx_self _cx_memb(_clone)(_cx_self src) { + _cx_self _arr = _cx_memb(_new_uninit)(src.xdim, src.ydim, src.zdim); + for (_cx_value* p = **_arr.data, *q = **src.data, *e = p + _cx_memb(_size)(&src); p != e; ++p, ++q) + *p = i_keyclone((*q)); + return _arr; +} + +STC_DEF void _cx_memb(_copy)(_cx_self *self, const _cx_self* other) { + if (self->data == other->data) return; + _cx_memb(_drop)(self); *self = _cx_memb(_clone)(*other); +} +#endif + +STC_DEF _cx_value* _cx_memb(_release)(_cx_self* self) { + _cx_value *values = self->data[0][0]; + c_free(self->data); + self->data = NULL; + return values; +} + +STC_DEF void _cx_memb(_drop)(_cx_self* self) { + if (!self->data) return; + for (_cx_value* p = **self->data, *q = p + _cx_memb(_size)(self); p != q; ) { + --q; i_keydrop(q); + } + c_free(self->data[0][0]); /* data */ + c_free(self->data); /* pointers */ +} + +#endif +#include <stc/priv/template.h> diff --git a/misc/include/alt/csmap.h b/misc/include/old/csmap.h index 4b4e764c..4b4e764c 100644 --- a/misc/include/alt/csmap.h +++ b/misc/include/old/csmap.h diff --git a/misc/include/alt/cstr.h b/misc/include/old/cstr.h index ecb9c9fb..8de6c4b0 100644 --- a/misc/include/alt/cstr.h +++ b/misc/include/old/cstr.h @@ -182,12 +182,12 @@ STC_INLINE uint64_t cstr_hash(const cstr *self) { return cfasthash(self->str, _cstr_p(self)->size); } -STC_INLINE void -cstr_replace(cstr* self, const char* find, const char* repl, unsigned count) { - csview in = cstr_sv(self); - cstr_take(self, cstr_replace_sv(in, c_SV(find, strlen(find)), - c_SV(repl, strlen(repl)), count)); +STC_INLINE void cstr_replace_ex(cstr* self, const char* find, const char* repl, unsigned count) { + cstr_take(self, cstr_replace_sv(cstr_sv(self), c_SV(find, strlen(find)), + c_SV(repl, strlen(repl)), count)); } +STC_INLINE void cstr_replace(cstr* self, const char* search, const char* repl) + { cstr_replace_ex(self, search, repl, ~0U); } /* -------------------------- IMPLEMENTATION ------------------------- */ #if defined(i_implement) @@ -324,7 +324,6 @@ STC_DEF cstr cstr_replace_sv(csview str, csview find, csview repl, unsigned count) { cstr out = cstr_NULL; size_t from = 0; char* res; - if (count == 0) count = ~0; if (find.size) while (count-- && (res = cstrnstrn(str.str + from, find.str, str.size - from, find.size))) { const size_t pos = res - str.str; diff --git a/misc/examples/new_arr.c b/misc/include/old/new_arr.c index 871cfae0..1006439d 100644 --- a/misc/examples/new_arr.c +++ b/misc/include/old/new_arr.c @@ -1,19 +1,19 @@ #include <stc/cstr.h> #define i_val int -#include <stc/carr2.h> +#include "carr2.h" #define i_val int -#include <stc/carr3.h> +#include "carr3.h" #define i_val_str -#include <stc/carr2.h> +#include "carr2.h" int main() { int w = 7, h = 5, d = 3; - c_with (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) @@ -24,12 +24,12 @@ int main() printf(" %d", volume.data[x][y]); puts(""); - c_foreach (i, carr2_int, volume) + c_FOREACH (i, carr2_int, volume) printf(" %d", *i.ref); puts("\n"); } - c_with (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) @@ -41,17 +41,17 @@ int main() printf(" %d", volume.data[x][y][z]); puts(""); - c_foreach (i, carr3_int, volume) + c_FOREACH (i, carr3_int, volume) printf(" %d", *i.ref); puts(""); } - c_with (carr2_str text2d = carr2_str_with_size(h, d, cstr_NULL), carr2_str_drop(&text2d)) + c_WITH (carr2_str text2d = carr2_str_with_size(h, d, cstr_NULL), carr2_str_drop(&text2d)) { cstr_assign(&text2d.data[2][1], "hello"); cstr_assign(&text2d.data[4][0], "world"); - c_foreach (i, carr2_str, text2d) + c_FOREACH (i, carr2_str, text2d) printf("line: %s\n", cstr_str(i.ref)); } } diff --git a/misc/include/stctest.h b/misc/include/stctest.h deleted file mode 100644 index 9718af7f..00000000 --- a/misc/include/stctest.h +++ /dev/null @@ -1,203 +0,0 @@ -/* MIT License
- *
- * Copyright (c) 2022 Tyge Løvset, NORCE, www.norceresearch.no
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
-/* stctest: A small and simple C11 unit-testing framework.
-
- Features:
- - Requires C11. Should work with any C compiler supporting _Generic.
- - No library dependencies. Not even itself. Just a header file.
- - Reports assertion failures, including expressions and line numbers.
- - ANSI color output for maximum visibility.
- - Easy to embed in apps for runtime tests (e.g. environment tests).
-
- Example:
-
- #include "stctest.h"
- #include "mylib.h"
-
- void test_sheep()
- {
- EXPECT_EQ("Sheep are cool", are_sheep_cool());
- EXPECT_EQ(4, sheep.legs);
- }
-
- void test_cheese()
- {
- EXPECT_GT(cheese.tanginess, 0);
- EXPECT_EQ("Wensleydale", cheese.name);
- }
-
- int main()
- {
- RUN_TEST(test_sheep);
- RUN_TEST(test_cheese);
- return REPORT_TESTS();
- }
- */
-
-#ifndef STCTEST_INCLUDED
-#define STCTEST_INCLUDED
-
-#include <stdio.h>
-#include <time.h>
-#include <string.h>
-#include <float.h>
-#include <stdarg.h>
-#include <inttypes.h>
-
-#define STC_FLOAT_EPSILON 1e-6
-#define STC_DOUBLE_EPSILON 1e-13
-
-
-#define EXPECT_TRUE(expr) \
- do { if (!_stctest_assert(__FILE__, __LINE__, #expr, (expr) != 0)) puts(""); } while (0)
-
-#define EXPECT_TRUE1(expr, v) \
- do { if (!_stctest_assert(__FILE__, __LINE__, #expr, (expr) != 0)) { \
- char _fmt[32]; sprintf(_fmt, " ; %%s --> %s\n", _stctest_FMT(v)); \
- printf(_fmt, #v, v); \
- }} while (0)
-
-#define EXPECT_FALSE(expr) EXPECT_TRUE(!(expr))
-#define EXPECT_FALSE1(expr, v) EXPECT_TRUE1(!(expr), v)
-
-/* NB! (char*) are compared as strings. Cast to (void*) to compare pointers only */
-#define EXPECT_EQ(a, b) _stctest_CHECK(a, ==, b, -STC_DOUBLE_EPSILON)
-#define EXPECT_NE(a, b) _stctest_CHECK(a, !=, b, -STC_DOUBLE_EPSILON)
-#define EXPECT_GT(a, b) _stctest_CHECK(a, >, b, -STC_DOUBLE_EPSILON)
-#define EXPECT_LT(a, b) _stctest_CHECK(a, <, b, -STC_DOUBLE_EPSILON)
-#define EXPECT_LE(a, b) _stctest_CHECK(a, <=, b, -STC_DOUBLE_EPSILON)
-#define EXPECT_GE(a, b) _stctest_CHECK(a, >=, b, -STC_DOUBLE_EPSILON)
-#define EXPECT_FLOAT_EQ(a, b) _stctest_CHECK((float)(a), ==, (float)(b), -STC_FLOAT_EPSILON)
-#define EXPECT_DOUBLE_EQ(a, b) _stctest_CHECK((double)(a), ==, (double)(b), -STC_DOUBLE_EPSILON)
-#define EXPECT_NEAR(a, b, abs_error) _stctest_CHECK((double)(a), ==, (double)(b), abs_error)
-
-/* Run a test() function */
-#define RUN_TEST(test, ...) do { \
- puts(#test "(" #__VA_ARGS__ "):"); \
- const int ps = _stctest_s.passes; \
- const int fs = _stctest_s.fails; \
- const clock_t _start = clock(); \
- test(__VA_ARGS__); \
- const int _sum = (clock() - _start)*1000 / CLOCKS_PER_SEC; \
- _stctest_s.total_ms += _sum; \
- printf(" passed: %d/%d. duration: %d ms\n", \
- _stctest_s.passes - ps, _stctest_s.passes + _stctest_s.fails - (ps + fs), _sum); \
-} while (0)
-
-#define REPORT_TESTS() stctest_report()
-
-/* ----------------------------------------------------------------------------- */
-
-#define _stctest_CHECK(a, OP, b, e) \
- do { if (!_stctest_assert(__FILE__, __LINE__, #a " " #OP " " #b, _stctest_CMP(a, OP, b, e))) { \
- char _fmt[32]; sprintf(_fmt, " ; %s %s %s\n", _stctest_FMT(a), #OP, _stctest_FMT(b)); \
- printf(_fmt, a, b); \
- }} while (0)
-
-#define _stctest_CMP(a, OP, b, e) _Generic((a), \
- const char*: _stctest_strcmp, char*: _stctest_strcmp, \
- double: _Generic((b), double: _stctest_dblcmp, float: _stctest_dblcmp, default: _stctest_valcmp), \
- float: _Generic((b), double: _stctest_dblcmp, float: _stctest_dblcmp, default: _stctest_valcmp), \
- default: _stctest_valcmp)((a) OP (b), #OP, a, b, (double)(e))
-
-#define _stctest_FMT(a) _Generic((a), \
- float: "%.8gf", double: "%.15g", \
- int64_t: "%" PRId64, int32_t: "%" PRId32, int16_t: "%" PRId16, int8_t: "%" PRId8, \
- uint64_t: "%" PRIu64 "u", uint32_t: "%" PRIu32 "u", uint16_t: "%" PRIu16 "u", uint8_t: "%" PRIu8 "u", \
- char*: "`%s`", const char*: "`%s`", \
- default: "%p")
-
-static int _stctest_strcmp(int res, const char* OP, ...) {
- va_list ap;
- va_start(ap, OP);
- const char* a = va_arg(ap, const char *);
- const char* b = va_arg(ap, const char *);
- va_end(ap);
- int c = strcmp(a, b);
- if (OP[0] == '<') return OP[1] == '=' ? c <= 0 : c < 0;
- if (OP[0] == '>') return OP[1] == '=' ? c >= 0 : c > 0;
- return (OP[0] == '!') ^ (c == 0);
-}
-
-// Knuth:
-static int approximately_equal(double a, double b, double epsilon) {
- double d = a - b; if (d < 0) d = -d;
- if (a < 0) a = -a; if (b < 0) b = -b;
- return d <= ((a < b ? b : a) * epsilon); // (a > b ? b : a) => essentially_equal:
-}
-
-static int _stctest_dblcmp(int res, const char* OP, ...) {
- va_list ap;
- va_start(ap, OP);
- double a = va_arg(ap, double);
- double b = va_arg(ap, double);
- double e = va_arg(ap, double);
- double c = a - b;
- va_end(ap);
- if (OP[0] == '<') return OP[1] == '=' ? c <= 0 : c < 0;
- if (OP[0] == '>') return OP[1] == '=' ? c >= 0 : c > 0;
- return (OP[0] == '!') ^ (e < 0 ? approximately_equal(a, b, -e) : (c < 0 ? -c : c) <= e);
-}
-
-static int _stctest_valcmp(int res, const char* OP, ...)
- { return res; }
-
-#define _stctest_COLOR_CODE 0x1B
-#define _stctest_COLOR_RED "[1;31m"
-#define _stctest_COLOR_GREEN "[1;32m"
-#define _stctest_COLOR_RESET "[0m"
-
-static struct stctest_s {
- int passes;
- int fails;
- const char* current_file;
- int total_ms;
-} _stctest_s = {0};
-
-static int _stctest_assert(const char* file, int line, const char* expression, int pass) {
- if (pass)
- _stctest_s.passes++;
- else {
- _stctest_s.fails++;
- printf(" failed \"%s:%d\": (%s)", file, line, expression);
- }
- _stctest_s.current_file = file;
- return pass;
-}
-
-static int stctest_report(void) {
- if (_stctest_s.fails) {
- printf("%c%sFAILED%c%s: \"%s\": (failed %d, passed %d, total %d)\n",
- _stctest_COLOR_CODE, _stctest_COLOR_RED, _stctest_COLOR_CODE, _stctest_COLOR_RESET,
- _stctest_s.current_file, _stctest_s.fails, _stctest_s.passes, _stctest_s.passes + _stctest_s.fails);
- } else {
- printf("%c%sPASSED%c%s: \"%s\": (total %d)\n",
- _stctest_COLOR_CODE, _stctest_COLOR_GREEN, _stctest_COLOR_CODE, _stctest_COLOR_RESET,
- _stctest_s.current_file, _stctest_s.passes);
- }
- printf(" duration: %d ms\n", _stctest_s.total_ms); \
- return -_stctest_s.fails;
-}
-
-#endif
diff --git a/misc/tests/cregex_test.c b/misc/tests/cregex_test.c index b9642159..cd812aa2 100644 --- a/misc/tests/cregex_test.c +++ b/misc/tests/cregex_test.c @@ -49,7 +49,7 @@ END_TEST START_TEST(compile_match_quantifiers) { - c_auto (cregex, re) { + c_AUTO (cregex, re) { re = cregex_from("ä+", 0); EXPECT_EQ(re.error, 0); @@ -64,7 +64,7 @@ START_TEST(compile_match_quantifiers) EXPECT_FALSE(cregex_find("bbb", &re, &match, 0)); } - c_auto (cregex, re) { + c_AUTO (cregex, re) { re = cregex_from("bä*", 0); EXPECT_EQ(re.error, 0); @@ -99,7 +99,7 @@ END_TEST START_TEST(compile_match_class_simple) { - c_auto (cregex, re1, re2, re3) + c_AUTO (cregex, re1, re2, re3) { re1 = cregex_from("\\s", 0); EXPECT_EQ(re1.error, 0); @@ -125,7 +125,7 @@ END_TEST START_TEST(compile_match_or) { - c_auto (cregex, re, re2) + c_AUTO (cregex, re, re2) { re = cregex_from("as|df", 0); EXPECT_EQ(re.error, 0); @@ -190,7 +190,7 @@ END_TEST START_TEST(search_all) { - c_auto (cregex, re) + c_AUTO (cregex, re) { re = cregex_from("ab", 0); csview m = {0}; @@ -210,7 +210,7 @@ END_TEST START_TEST(captures_len) { - c_auto (cregex, re) { + c_AUTO (cregex, re) { cregex re = cregex_from("(ab(cd))(ef)", 0); EXPECT_EQ(cregex_captures(&re), 3); } @@ -219,7 +219,7 @@ END_TEST START_TEST(captures_cap) { - c_auto (cregex, re) { + c_AUTO (cregex, re) { re = cregex_from("(ab)((cd)+)", 0); EXPECT_EQ(cregex_captures(&re), 3); |
