summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2020-10-26 20:25:03 +0100
committerTyge Løvset <[email protected]>2020-10-26 20:25:03 +0100
commit0b95e794fe91b69928bb111d66fc973d18de7a1f (patch)
tree35843d6e5f17e07a3cbb3f8b59df4965661a35d8
parent0c64199a35a8901a4969f8c2ce0c0483f184732c (diff)
downloadSTC-modified-0b95e794fe91b69928bb111d66fc973d18de7a1f.tar.gz
STC-modified-0b95e794fe91b69928bb111d66fc973d18de7a1f.zip
Updates examples using cfmt.h c_print().
-rw-r--r--examples/advanced.c5
-rw-r--r--examples/birthday.c10
-rw-r--r--examples/bits.c35
-rw-r--r--examples/complex.c8
-rw-r--r--examples/demos.c76
-rw-r--r--examples/ex_gaussian.c6
-rw-r--r--examples/heap.c27
-rw-r--r--examples/inits.c14
-rw-r--r--examples/list.c21
-rw-r--r--examples/mapmap.c4
-rw-r--r--examples/phonebook.c4
-rw-r--r--examples/prime.c14
-rw-r--r--examples/priority.c5
-rw-r--r--examples/ptr.c12
-rw-r--r--examples/queue.c6
-rw-r--r--examples/random.c28
-rw-r--r--examples/rngtest.c21
-rw-r--r--examples/share_ptr2.c8
-rw-r--r--examples/stack.c14
-rw-r--r--examples/words.c6
-rw-r--r--stc/cfmt.h8
21 files changed, 168 insertions, 164 deletions
diff --git a/examples/advanced.c b/examples/advanced.c
index e9e0002b..83155405 100644
--- a/examples/advanced.c
+++ b/examples/advanced.c
@@ -10,9 +10,8 @@
* In order to use Viking as a map key, it is smart to define a plain-old-data "view"
* of the Viking struct, to simplfy insert and lookup in the map.
*/
-#include <stdio.h>
#include <stc/cmap.h>
-#include <stc/cstr.h>
+#include <stc/cfmt.h>
// Viking data struct -----------------------
@@ -74,7 +73,7 @@ int main()
cmap_vk_emplace(&vikings, einar, 0).first->second += 5; // again
c_foreach (k, cmap_vk, vikings) {
- printf("%s of %s has %d hp\n", k.val->first.name.str, k.val->first.country.str, k.val->second);
+ c_print(1, "{} of {} has {} HP\n", k.val->first.name.str, k.val->first.country.str, k.val->second);
}
cmap_vk_del(&vikings);
}
diff --git a/examples/birthday.c b/examples/birthday.c
index 92423b7b..bd3c85f3 100644
--- a/examples/birthday.c
+++ b/examples/birthday.c
@@ -5,7 +5,7 @@
#include <stc/crandom.h>
#include <stc/cmap.h>
#include <stc/cvec.h>
-#include <stc/cstr.h>
+#include <stc/cfmt.h>
using_cmap(ic, uint64_t, uint8_t);
@@ -22,10 +22,10 @@ void repeats(void)
c_forrange (i, N) {
uint64_t k = crand_i64(&rng) & mask;
int v = ++cmap_ic_emplace(&m, k, 0).first->second;
- if (v > 1) printf("%zu: %llx - %d\n", i, k, v);
+ if (v > 1) c_print(1, "{}: {:x} - {}\n", i, k, v);
}
float diff = (float) (clock() - now) / CLOCKS_PER_SEC;
- printf("%.02f", diff);
+ c_print(1, "{}", diff);
}
@@ -50,9 +50,9 @@ void distribution(void)
sum /= map.size;
c_foreach (i, cmap_x, map)
- printf("%u: %zu - %zu\n", i.val->first, i.val->second, sum);
+ c_print(1, "{}: {} - {}\n", i.val->first, i.val->second, sum);
- printf("%.02f\n", diff);
+ c_print(1, "{:.02}\n", diff);
}
int main()
diff --git a/examples/bits.c b/examples/bits.c
index f59555e4..8bb187a6 100644
--- a/examples/bits.c
+++ b/examples/bits.c
@@ -1,19 +1,20 @@
#include <stdio.h>
#include <stc/cbitset.h>
+#include <stc/cfmt.h>
int main() {
cbitset_t set = cbitset_with_size(23, true);
- printf("count %zu, %zu\n", cbitset_count(set), set.size);
+ c_print(1, "count {}, {}\n", cbitset_count(set), set.size);
cbitset_reset(&set, 9);
cbitset_resize(&set, 43, false);
cstr_t str = cbitset_to_str(set);
- printf(" str: %s\n", str.str);
+ c_print(1, " str: {}\n", str.str);
cstr_del(&str);
- printf("%4zu: ", set.size);
+ c_print(1, "{:4}: ", set.size);
c_forrange (i, int, set.size)
- printf("%d", cbitset_test(set, i));
+ c_print(1, "{}", cbitset_test(set, i));
puts("");
cbitset_set(&set, 28);
@@ -21,38 +22,38 @@ int main() {
cbitset_resize(&set, 93, false);
cbitset_resize(&set, 102, true);
cbitset_set_value(&set, 99, false);
- printf("%4zu: ", set.size);
+ c_print(1, "{:4}: ", set.size);
c_forrange (i, int, set.size)
- printf("%d", cbitset_test(set, i));
+ c_print(1, "{}", cbitset_test(set, i));
puts("\nIterator:");
- printf("%4zu: ", set.size);
+ c_print(1, "{:4}: ", set.size);
c_foreach (i, cbitset, set)
- printf("%d", cbitset_itval(i));
- puts("");
+ c_print(1, "{}", cbitset_itval(i));
+ puts("");
cbitset_t s2 = cbitset_clone(set);
cbitset_flip_all(&s2);
cbitset_set(&s2, 16);
cbitset_set(&s2, 17);
- cbitset_set(&s2, 18);
- printf(" new: ");
+ cbitset_set(&s2, 18);
+ c_print(1, " new: ");
c_forrange (i, int, s2.size)
- printf("%d", cbitset_test(s2, i));
+ c_print(1, "{}", cbitset_test(s2, i));
puts("");
- printf(" xor: ");
+ c_print(1, " xor: ");
cbitset_xor_with(&set, s2);
c_forrange (i, int, set.size)
- printf("%d", cbitset_test(set, i));
+ c_print(1, "{}", cbitset_test(set, i));
puts("");
cbitset_set_all(&set, false);
- printf("%4zu: ", set.size);
+ c_print(1, "{:4}: ", set.size);
c_forrange (i, int, set.size)
- printf("%d", cbitset_test(set, i));
+ c_print(1, "{}", cbitset_test(set, i));
puts("");
-
+
cbitset_del(&s2);
cbitset_del(&set);
} \ No newline at end of file
diff --git a/examples/complex.c b/examples/complex.c
index c9704d73..db22f235 100644
--- a/examples/complex.c
+++ b/examples/complex.c
@@ -1,9 +1,9 @@
-#include <stc/cstr.h>
+#include <stc/cfmt.h>
#include <stc/cmap.h>
#include <stc/clist.h>
#include <stc/carray.h>
-void check_del(float* v) {printf("destroy %g\n", *v);}
+void check_del(float* v) {c_print(1, "destroy {}\n", *v);}
using_carray(f, float, check_del); // normally omit the last argument - float type need no destroy.
using_clist(y, carray2f, carray2f_del, c_no_compare);
@@ -18,7 +18,7 @@ int main() {
{ // Construct.
carray2f table = carray2f_init(ydim, xdim, 0.f);
- printf("table: (%zu, %zu)\n", carray2_ydim(table), carray2_xdim(table));
+ c_print(1, "table: ({}, {})\n", carray2_ydim(table), carray2_xdim(table));
clist_y tableList = clist_INIT;
// Put in some data.
cmap_g listMap = cmap_INIT;
@@ -30,7 +30,7 @@ int main() {
}
{ // Access the data entry
carray2f table = *clist_y_back(&cmap_g_find(&cmap_s_find(&myMap, strKey)->second, tableKey)->second);
- printf("value (%d, %d) is: %f\n", y, x, *carray2f_at(&table, y, x));
+ c_print(1, "value ({}, {}) is: {}\n", y, x, *carray2f_at(&table, y, x));
}
cmap_s_del(&myMap); // free up everything!
diff --git a/examples/demos.c b/examples/demos.c
index 18b0f1ef..16d77319 100644
--- a/examples/demos.c
+++ b/examples/demos.c
@@ -2,33 +2,33 @@
#include <stc/clist.h>
#include <stc/carray.h>
#include <stc/cmap.h>
-#include <stc/cstr.h>
+#include <stc/cfmt.h>
void stringdemo1()
{
- printf("\nSTRINGDEMO1\n");
+ c_print(1, "\nSTRINGDEMO1\n");
cstr_t cs = cstr_from("one-nine-three-seven-five");
- printf("%s.\n", cs.str);
+ c_print(1, "{}.\n", cs.str);
cstr_insert(&cs, 3, "-two");
- printf("%s.\n", cs.str);
+ c_print(1, "{}.\n", cs.str);
cstr_erase(&cs, 7, 5); // -nine
- printf("%s.\n", cs.str);
+ c_print(1, "{}.\n", cs.str);
cstr_replace(&cs, cstr_find(&cs, "seven"), 5, "four");
- printf("%s.\n", cs.str);
+ c_print(1, "{}.\n", cs.str);
cstr_take(&cs, cstr_from_fmt("%s *** %s", cs.str, cs.str));
- printf("%s.\n", cs.str);
+ c_print(1, "{}.\n", cs.str);
- printf("find \"four\": %s\n", cs.str + cstr_find(&cs, "four"));
+ c_print(1, "find \"four\": {}\n", cs.str + cstr_find(&cs, "four"));
// reassign:
cstr_assign(&cs, "one two three four five six seven");
cstr_append(&cs, " eight");
- printf("append: %s\n", cs.str);
+ c_print(1, "append: {}\n", cs.str);
cstr_del(&cs);
}
@@ -38,17 +38,17 @@ using_cvec(ix, int64_t); // ix is just an example tag name.
void vectordemo1()
{
- printf("\nVECTORDEMO1\n");
+ c_print(1, "\nVECTORDEMO1\n");
cvec_ix bignums = cvec_INIT; // = (cvec_ix) cvec_INIT; if initializing after declaration.
cvec_ix_reserve(&bignums, 100);
for (size_t i = 0; i<=100; ++i)
cvec_ix_push_back(&bignums, i * i * i);
- printf("erase - %d: %zu\n", 100, bignums.data[100]);
+ c_print(1, "erase - {}: {}\n", 100, bignums.data[100]);
cvec_ix_pop_back(&bignums); // erase the last
for (size_t i = 0; i < cvec_size(bignums); ++i) {
- if (i >= 90) printf("%zu: %zu\n", i, bignums.data[i]);
+ if (i >= 90) c_print(1, "{}: {}\n", i, bignums.data[i]);
}
cvec_ix_del(&bignums);
}
@@ -59,17 +59,17 @@ using_cvec_str();
void vectordemo2()
{
- printf("\nVECTORDEMO2\n");
+ c_print(1, "\nVECTORDEMO2\n");
cvec_str names = cvec_INIT;
cvec_str_emplace_back(&names, "Mary");
cvec_str_emplace_back(&names, "Joe");
cvec_str_emplace_back(&names, "Chris");
cstr_assign(&names.data[1], "Jane"); // replace Joe
- printf("names[1]: %s\n", names.data[1].str);
+ c_print(1, "names[1]: {}\n", names.data[1].str);
cvec_str_sort(&names); // Sort the array
c_foreach (i, cvec_str, names)
- printf("sorted: %s\n", i.val->str);
+ c_print(1, "sorted: {}\n", i.val->str);
cvec_str_del(&names);
}
@@ -77,18 +77,18 @@ using_clist(ix, int);
void listdemo1()
{
- printf("\nLISTDEMO1\n");
+ c_print(1, "\nLISTDEMO1\n");
clist_ix nums = clist_INIT, nums2 = clist_INIT;
for (int i = 0; i < 10; ++i)
clist_ix_push_back(&nums, i);
for (int i = 100; i < 110; ++i)
clist_ix_push_back(&nums2, i);
c_foreach (i, clist_ix, nums)
- printf("value: %d\n", *i.val);
+ c_print(1, "value: {}\n", *i.val);
/* merge/append nums2 to nums */
clist_ix_splice_front(&nums, &nums2);
c_foreach (i, clist_ix, nums)
- printf("spliced: %d\n", *i.val);
+ c_print(1, "spliced: {}\n", *i.val);
*clist_ix_find(&nums, 100).val *= 10;
clist_ix_sort(&nums); // Sort the array
@@ -96,7 +96,7 @@ void listdemo1()
clist_ix_pop_front(&nums);
clist_ix_push_front(&nums, -99);
c_foreach (i, clist_ix, nums)
- printf("sorted: %d\n", *i.val);
+ c_print(1, "sorted: {}\n", *i.val);
clist_ix_del(&nums);
}
@@ -104,13 +104,13 @@ using_cset(i, int);
void setdemo1()
{
- printf("\nSETDEMO1\n");
+ c_print(1, "\nSETDEMO1\n");
cset_i nums = cset_INIT;
cset_i_insert(&nums, 8);
cset_i_insert(&nums, 11);
c_foreach (i, cset_i, nums)
- printf("set: %d\n", *i.val);
+ c_print(1, "set: {}\n", *i.val);
cset_i_del(&nums);
}
@@ -119,11 +119,11 @@ using_cmap(ii, int, int);
void mapdemo1()
{
- printf("\nMAPDEMO1\n");
+ c_print(1, "\nMAPDEMO1\n");
cmap_ii nums = cmap_INIT;
cmap_ii_put(&nums, 8, 64);
cmap_ii_put(&nums, 11, 121);
- printf("val 8: %d\n", *cmap_ii_at(&nums, 8));
+ c_print(1, "val 8: {}\n", *cmap_ii_at(&nums, 8));
cmap_ii_del(&nums);
}
@@ -132,7 +132,7 @@ using_cmap_strkey(si, int); // Shorthand macro for the general using_cmap expans
void mapdemo2()
{
- printf("\nMAPDEMO2\n");
+ c_print(1, "\nMAPDEMO2\n");
cmap_si nums = cmap_INIT;
cmap_si_put(&nums, "Hello", 64);
cmap_si_put(&nums, "Groovy", 121);
@@ -140,11 +140,11 @@ void mapdemo2()
// iterate the map:
for (cmap_si_iter_t i = cmap_si_begin(&nums); i.val != cmap_si_end(&nums).val; cmap_si_next(&i))
- printf("long: %s: %d\n", i.val->first.str, i.val->second);
+ c_print(1, "long: {}: {}\n", i.val->first.str, i.val->second);
// or rather use the short form:
c_foreach (i, cmap_si, nums)
- printf("short: %s: %d\n", i.val->first.str, i.val->second);
+ c_print(1, "short: {}: {}\n", i.val->first.str, i.val->second);
cmap_si_del(&nums);
}
@@ -154,20 +154,20 @@ using_cmap_str();
void mapdemo3()
{
- printf("\nMAPDEMO3\n");
+ c_print(1, "\nMAPDEMO3\n");
cmap_str table = cmap_INIT;
cmap_str_put(&table, "Map", "test");
cmap_str_put(&table, "Make", "my");
cmap_str_put(&table, "Sunny", "day");
cmap_str_value_t *e = cmap_str_find(&table, "Make");
c_foreach (i, cmap_str, table)
- printf("entry: %s: %s\n", i.val->first.str, i.val->second.str);
- printf("size %zu: remove: Make: %s\n", cmap_size(table), e->second.str);
+ c_print(1, "entry: {}: {}\n", i.val->first.str, i.val->second.str);
+ c_print(1, "size {}: remove: Make: {}\n", cmap_size(table), e->second.str);
cmap_str_erase(&table, "Make");
- printf("size %zu\n", cmap_size(table));
+ c_print(1, "size {}\n", cmap_size(table));
c_foreach (i, cmap_str, table)
- printf("entry: %s: %s\n", i.val->first.str, i.val->second.str);
+ c_print(1, "entry: {}: {}\n", i.val->first.str, i.val->second.str);
cmap_str_del(&table); // frees key and value cstrs, and hash table.
}
@@ -176,22 +176,22 @@ using_carray(f, float);
void arraydemo1()
{
- printf("\nARRAYDEMO1\n");
+ c_print(1, "\nARRAYDEMO1\n");
carray3f a3 = carray3f_init(30, 20, 10, 0.0f);
*carray3f_at(&a3, 5, 4, 3) = 10.2f; // a3[5][4][3]
carray2f a2 = carray3f_at1(&a3, 5); // sub-array reference: a2 = a3[5]
carray1f a1 = carray3f_at2(&a3, 5, 4); // sub-array reference: a1 = a3[5][4]
- printf("a3: %zu: (%zu, %zu, %zu) = %zu\n", sizeof(a3), carray3_xdim(a3), carray3_ydim(a3), carray3_zdim(a3), carray3_size(a3));
- printf("a2: %zu: (%zu, %zu) = %zu\n", sizeof(a2), carray2_xdim(a2), carray2_ydim(a2), carray2_size(a2));
+ c_print(1, "a3: {}: ({}, {}, {}) = {}\n", sizeof(a3), carray3_xdim(a3), carray3_ydim(a3), carray3_zdim(a3), carray3_size(a3));
+ c_print(1, "a2: {}: ({}, {}) = {}\n", sizeof(a2), carray2_xdim(a2), carray2_ydim(a2), carray2_size(a2));
- printf("%f\n", a1.data[3]); // lookup a1[3] (=10.2f)
- printf("%f\n", *carray2f_at(&a2, 4, 3)); // lookup a2[4][3] (=10.2f)
- printf("%f\n", *carray3f_at(&a3, 5, 4, 3)); // lookup a3[5][4][3] (=10.2f)
+ c_print(1, "{}\n", a1.data[3]); // lookup a1[3] (=10.2f)
+ c_print(1, "{}\n", *carray2f_at(&a2, 4, 3)); // lookup a2[4][3] (=10.2f)
+ c_print(1, "{}\n", *carray3f_at(&a3, 5, 4, 3)); // lookup a3[5][4][3] (=10.2f)
c_foreach (i, carray3f, a3)
*i.val = 1.0f;
- printf("%f\n", *carray3f_at(&a3, 29, 19, 9));
+ c_print(1, "{:.1f}\n", *carray3f_at(&a3, 29, 19, 9));
carray2f_del(&a2); // does nothing, since it is a sub-array.
carray3f_del(&a3); // also invalidates a2.
diff --git a/examples/ex_gaussian.c b/examples/ex_gaussian.c
index dc04215c..e60962b4 100644
--- a/examples/ex_gaussian.c
+++ b/examples/ex_gaussian.c
@@ -2,7 +2,7 @@
#include <time.h>
#include <math.h>
#include <stc/crandom.h>
-#include <stc/cstr.h>
+#include <stc/cfmt.h>
#include <stc/cmap.h>
#include <stc/cvec.h>
@@ -21,7 +21,7 @@ int main()
enum {N = 10000000};
const double Mean = -12.0, StdDev = 8.0, Mag = 12000.0 / StdDev;
- printf("Demo of gaussian / normal distribution of %d random samples\n", N);
+ c_print(1, "Demo of gaussian / normal distribution of {} random samples\n", N);
// Setup random engine with normal distribution.
uint64_t seed = time(NULL);
@@ -48,7 +48,7 @@ int main()
if (n > 0) {
// bar string: take ownership in new str after freeing current.
cstr_take(&bar, cstr_with_size(n, '*'));
- printf("%4d %s\n", i.val->first, bar.str);
+ c_print(1, "{:4} {}\n", i.val->first, bar.str);
}
}
// Cleanup
diff --git a/examples/heap.c b/examples/heap.c
index cc8d1df1..17f5fc70 100644
--- a/examples/heap.c
+++ b/examples/heap.c
@@ -3,6 +3,7 @@
#include <stc/crandom.h>
#include <stc/cvec.h>
#include <stc/cpqueue.h>
+#include <stc/cfmt.h>
using_cvec(f, float);
using_cpqueue(f, cvec_f, >);
@@ -14,31 +15,35 @@ int main()
int N = 3000000, M = 100;
cpqueue_f pq = cpqueue_f_init();
-
+
pcg = crand_rng32_init(seed);
clock_t start = clock();
c_forrange (i, int, N)
cvec_f_push_back(&pq, (float) crand_f32(&pcg)*100000);
-
+
cpqueue_f_make_heap(&pq);
- printf("Built priority queue: %f secs\n", (clock() - start) / (float) CLOCKS_PER_SEC);
+ c_print(1, "Built priority queue: {} secs\n", (clock() - start) / (float) CLOCKS_PER_SEC);
+
+ c_forrange (i, int, M) {
+ c_print(1, "{} ", *cpqueue_f_top(&pq));
+ cpqueue_f_pop(&pq);
+ }
- c_forrange (i, int, M)
- printf("%g ", *cpqueue_f_top(&pq)), cpqueue_f_pop(&pq);
-
start = clock();
c_forrange (i, int, M, N)
cpqueue_f_pop(&pq);
- printf("\n\npopped PQ: %f secs\n", (clock() - start) / (float) CLOCKS_PER_SEC);
+ c_print(1, "\n\npopped PQ: {} secs\n", (clock() - start) / (float) CLOCKS_PER_SEC);
pcg = crand_rng32_init(seed);
start = clock();
c_forrange (i, int, N)
cpqueue_f_push(&pq, (float) crand_f32(&pcg)*100000);
- printf("pushed PQ: %f secs\n", (clock() - start) / (float) CLOCKS_PER_SEC);
-
- c_forrange (i, int, M)
- printf("%g ", *cpqueue_f_top(&pq)), cpqueue_f_pop(&pq);
+ c_print(1, "pushed PQ: {} secs\n", (clock() - start) / (float) CLOCKS_PER_SEC);
+
+ c_forrange (i, int, M) {
+ c_print(1, "{} ", *cpqueue_f_top(&pq));
+ cpqueue_f_pop(&pq);
+ }
puts("");
cpqueue_f_del(&pq);
diff --git a/examples/inits.c b/examples/inits.c
index 3565c189..9436310e 100644
--- a/examples/inits.c
+++ b/examples/inits.c
@@ -1,5 +1,5 @@
#include <stdio.h>
-#include <stc/cstr.h>
+#include <stc/cfmt.h>
#include <stc/cmap.h>
#include <stc/cvec.h>
#include <stc/cpqueue.h>
@@ -26,7 +26,7 @@ int main(void)
cvec_f floats = cvec_INIT;
c_push_items(&floats, cvec_f, {4.0f, 2.0f, 5.0f, 3.0f, 1.0f});
- c_foreach (i, cvec_f, floats) printf("%.1f ", *i.val);
+ c_foreach (i, cvec_f, floats) c_print(1, "{:.1f} ", *i.val);
puts("");
// CVEC PRIORITY QUEUE
@@ -36,7 +36,7 @@ int main(void)
// sorted:
while (! cpqueue_f_empty(floats)) {
- printf("%.1f ", *cpqueue_f_top(&floats));
+ c_print(1, "{:.1f} ", *cpqueue_f_top(&floats));
cpqueue_f_pop(&floats);
}
puts("\n");
@@ -53,7 +53,7 @@ int main(void)
});
c_foreach (i, cmap_id, idnames)
- printf("%d: %s\n", i.val->first, i.val->second.str);
+ c_print(1, "{}: {}\n", i.val->first, i.val->second.str);
puts("");
cmap_id_del(&idnames);
@@ -76,7 +76,7 @@ int main(void)
cmap_cnt_emplace(&countries, "Finland", 0).first->second += 20;
c_foreach (i, cmap_cnt, countries)
- printf("%s: %d\n", i.val->first.str, i.val->second);
+ c_print(1, "{}: {}\n", i.val->first.str, i.val->second);
puts("");
cmap_cnt_del(&countries);
@@ -92,7 +92,7 @@ int main(void)
cvec_ip_sort(&pairs1);
c_foreach (i, cvec_ip, pairs1)
- printf("(%d %d) ", i.val->x, i.val->y);
+ c_print(1, "({} {}) ", i.val->x, i.val->y);
puts("");
cvec_ip_del(&pairs1);
@@ -108,7 +108,7 @@ int main(void)
clist_ip_sort(&pairs2);
c_foreach (i, clist_ip, pairs2)
- printf("(%d %d) ", i.val->x, i.val->y);
+ c_print(1, "({} {}) ", i.val->x, i.val->y);
puts("");
clist_ip_del(&pairs2);
} \ No newline at end of file
diff --git a/examples/list.c b/examples/list.c
index 2cc433c6..84a13bf5 100644
--- a/examples/list.c
+++ b/examples/list.c
@@ -1,7 +1,8 @@
-#include <stdio.h>
#include <time.h>
#include <stc/clist.h>
#include <stc/crandom.h>
+#include <stc/cfmt.h>
+
using_clist(fx, double);
int main() {
@@ -15,26 +16,26 @@ int main() {
c_forrange (i, int, n)
clist_fx_push_back(&list, crand_uniform_f64(&eng, &dist)), ++m;
double sum = 0.0;
- printf("sumarize %d:\n", m);
+ c_print(1, "sumarize {}:\n", m);
c_foreach (i, clist_fx, list)
sum += *i.val;
- printf("sum %f\n\n", sum);
+ c_print(1, "sum {}\n\n", sum);
k = 0;
c_foreach (i, clist_fx, list)
- if (++k <= 10) printf("%8d: %10f\n", k, *i.val); else break;
+ if (++k <= 10) c_print(1, "{:3}: {:16.6f}\n", k, *i.val); else break;
puts("sort");
clist_fx_sort(&list); // mergesort O(n*log n)
puts("sorted");
k = 0;
c_foreach (i, clist_fx, list)
- if (++k <= 10) printf("%8d: %10f\n", k, *i.val); else break;
+ if (++k <= 10) c_print(1, "{:3}: {:16.6f}\n", k, *i.val); else break;
puts("");
clist_fx_clear(&list);
c_push_items(&list, clist_fx, {10, 20, 30, 40, 30, 50});
- c_foreach (i, clist_fx, list) printf(" %g", *i.val);
+ c_foreach (i, clist_fx, list) c_print(1, " {}", *i.val);
puts("");
int removed = clist_fx_remove(&list, 30);
@@ -42,13 +43,13 @@ int main() {
clist_fx_push_back(&list, 500);
clist_fx_push_front(&list, 1964);
clist_fx_iter_t it = clist_fx_before_begin(&list);
- printf("Full: ");
+ c_print(1, "Full: ");
c_foreach (i, clist_fx, list)
- printf(" %g", *i.val);
+ c_print(1, " {}", *i.val);
for (int i=0; i<4; ++i) clist_fx_next(&it);
- printf("\nSubs: ");
+ c_print(1, "\nSubs: ");
c_foreach (i, clist_fx, it, clist_fx_end(&list))
- printf(" %g", *i.val);
+ c_print(1, " {}", *i.val);
puts("");
clist_fx_del(&list);
} \ No newline at end of file
diff --git a/examples/mapmap.c b/examples/mapmap.c
index 6800601f..4e6c2f1a 100644
--- a/examples/mapmap.c
+++ b/examples/mapmap.c
@@ -1,7 +1,7 @@
#include <stdio.h>
#include <stc/cmap.h>
-#include <stc/cstr.h>
+#include <stc/cfmt.h>
using_cmap_str();
using_cmap_strkey(cfg, cmap_str, cmap_str_del);
@@ -20,7 +20,7 @@ int main(void) {
c_foreach (i, cmap_cfg, config)
c_foreach (j, cmap_str, i.val->second)
- printf("%s: %s - %s (%u)\n", i.val->first.str, j.val->first.str, j.val->second.str, i.val->second.bucket_count);
+ c_print(1, "{}: {} - {} ({})\n", i.val->first.str, j.val->first.str, j.val->second.str, i.val->second.bucket_count);
cmap_cfg_del(&config);
} \ No newline at end of file
diff --git a/examples/phonebook.c b/examples/phonebook.c
index c7a02e74..e567c442 100644
--- a/examples/phonebook.c
+++ b/examples/phonebook.c
@@ -23,14 +23,14 @@
#include <stdio.h>
#include <stc/cmap.h>
-#include <stc/cstr.h>
+#include <stc/cfmt.h>
using_cmap_str();
void print_phone_book(cmap_str phone_book)
{
c_foreach (i, cmap_str, phone_book)
- printf("%s\t- %s\n", i.val->first.str, i.val->second.str);
+ c_print(1, "{}\t- {}\n", i.val->first.str, i.val->second.str);
}
int main(int argc, char **argv)
diff --git a/examples/prime.c b/examples/prime.c
index 95e1fd46..a21e791a 100644
--- a/examples/prime.c
+++ b/examples/prime.c
@@ -1,4 +1,4 @@
-#include <stdio.h>
+#include <stc/cfmt.h>
#include <stc/cbitset.h>
static inline cbitset_t sieveOfEratosthenes(size_t n)
@@ -16,23 +16,23 @@ static inline cbitset_t sieveOfEratosthenes(size_t n)
}
}
return pbits;
-}
+}
int main(void)
{
int n = 100000000;
- printf("computing prime numbers up to %u\n", n);
-
+ c_print(1, "computing prime numbers up to {}\n", n);
+
cbitset_t primes = sieveOfEratosthenes(n);
puts("done");
-
+
size_t np = cbitset_count(primes);
- printf("number of primes: %zu\n", np);
+ c_print(1, "number of primes: {}\n", np);
printf("2 ");
c_forrange (i, int, 3, 1001, 2) {
- if (cbitset_test(primes, i)) printf("%d ", i);
+ if (cbitset_test(primes, i)) c_print(1, "{} ", i);
}
puts("");
cbitset_del(&primes);
diff --git a/examples/priority.c b/examples/priority.c
index e4a20da9..730ae5e3 100644
--- a/examples/priority.c
+++ b/examples/priority.c
@@ -1,7 +1,6 @@
-
-#include <stdio.h>
#include <time.h>
#include <stc/cvec.h>
+#include <stc/cfmt.h>
#include <stc/cpqueue.h>
#include <stc/cmap.h>
#include <stc/crandom.h>
@@ -28,7 +27,7 @@ int main() {
// Extract the hundred smallest.
c_forrange (100) {
- printf("%zd ", *cpqueue_i_top(&heap));
+ c_print(1, "{} ", *cpqueue_i_top(&heap));
cpqueue_i_pop(&heap);
}
cpqueue_i_del(&heap);
diff --git a/examples/ptr.c b/examples/ptr.c
index 3ffb9292..56da4b38 100644
--- a/examples/ptr.c
+++ b/examples/ptr.c
@@ -1,5 +1,5 @@
#include <stc/cptr.h>
-#include <stc/cstr.h>
+#include <stc/cfmt.h>
#include <stc/cvec.h>
typedef struct { cstr_t name, last; } Person;
@@ -9,7 +9,7 @@ Person* Person_make(Person* p, const char* name, const char* last) {
return p;
}
void Person_del(Person* p) {
- printf("del: %s\n", p->name.str);
+ c_print(1, "del: {}\n", p->name.str);
c_del(cstr, &p->name, &p->last);
}
int Person_compare(const Person* p, const Person* q) {
@@ -22,7 +22,7 @@ using_cptr(pe, Person, Person_del, Person_compare);
using_cvec(pp, Person*, cptr_pe_del, cptr_pe_compare);
int main() {
- puts("Vec of Person *:");
+ puts("Vec of Person*:\n--------------");
cvec_pp pvec = cvec_pp_init();
cvec_pp_push_back(&pvec, Person_make(c_new(Person), "Joe", "Jordan"));
cvec_pp_push_back(&pvec, Person_make(c_new(Person), "Annie", "Aniston"));
@@ -30,9 +30,9 @@ int main() {
cvec_pp_sort(&pvec);
c_foreach (i, cvec_pp, pvec)
- printf("%s %s\n", (*i.val)->name.str, (*i.val)->last.str);
+ c_print(1, "{} {}\n", (*i.val)->name.str, (*i.val)->last.str);
- puts("\nVec of Person:");
+ puts("\nVec of Person:\n-------------");
cvec_pe vec = cvec_pe_init();
Person tmp;
cvec_pe_push_back(&vec, *Person_make(&tmp, "Joe", "Jordan"));
@@ -41,7 +41,7 @@ int main() {
cvec_pe_sort(&vec);
c_foreach (i, cvec_pe, vec)
- printf("%s %s\n", i.val->name.str, i.val->last.str);
+ c_print(1, "{} {}\n", i.val->name.str, i.val->last.str);
puts("\nDestroy pvec:");
cvec_pp_del(&pvec);
diff --git a/examples/queue.c b/examples/queue.c
index c56214a7..ba3d6788 100644
--- a/examples/queue.c
+++ b/examples/queue.c
@@ -1,6 +1,6 @@
#include <stc/crandom.h>
#include <stc/cqueue.h>
-#include <stdio.h>
+#include <stc/cfmt.h>
using_clist(i, int);
using_cqueue(i, clist_i); // min-heap (increasing values)
@@ -18,7 +18,7 @@ int main() {
cqueue_i_push(&queue, crand_uniform_i32(&rng, &dist));
// Push or pop on the queue ten million times
- printf("%d\n", n);
+ c_print(1, "{}\n", n);
c_forrange (n) { // range uses initial n only.
int r = crand_uniform_i32(&rng, &dist);
if (r & 1)
@@ -26,6 +26,6 @@ int main() {
else
--n, cqueue_i_pop(&queue);
}
- printf("%d, %zu\n", n, cqueue_i_size(queue));
+ c_print(1, "{}, {}\n", n, cqueue_i_size(queue));
cqueue_i_del(&queue);
}
diff --git a/examples/random.c b/examples/random.c
index 7f1be3f5..5649a930 100644
--- a/examples/random.c
+++ b/examples/random.c
@@ -1,13 +1,13 @@
#include <stdio.h>
#include <time.h>
#include <stc/crandom.h>
-#include <stc/cstr.h>
+#include <stc/cfmt.h>
int main()
{
enum {R = 30};
const size_t N = 1000000000;
- clock_t difference, before;
+ clock_t difference, before;
uint64_t sum = 0;
uint64_t seed = time(NULL);
@@ -15,36 +15,36 @@ int main()
uint32_t range = crand_i32(&pcg) & ((1u << 28) - 1);
crand_uniform_i32_t dist0 = crand_uniform_i32_init(0, range);
- printf("32 uniform: %u\n", dist0.range);
- double fsum = 0;
+ c_print(1, "32 uniform: {}\n", dist0.range);
+ sum = 0;
before = clock();
c_forrange (N) {
- fsum += (double) crand_uniform_i32(&pcg, &dist0) / dist0.range;
+ sum += crand_uniform_i32(&pcg, &dist0);
}
difference = clock() - before;
- printf("%zu %f: %f secs\n", N, fsum / N, (float) difference / CLOCKS_PER_SEC);
+ c_print(1, "{} {}: {} secs\n", N, (double) sum / N / dist0.range, (float) difference / CLOCKS_PER_SEC);
pcg = crand_rng32_init(seed);
dist0 = crand_uniform_i32_init(0, range);
puts("32 unbiased");
- fsum = 0;
+ sum = 0;
before = clock();
c_forrange (N) {
- fsum += (double) crand_unbiased_i32(&pcg, &dist0) / dist0.range;
+ sum += crand_unbiased_i32(&pcg, &dist0);
}
difference = clock() - before;
- printf("%zu %f: %f secs\n", N, fsum / N, (float) difference / CLOCKS_PER_SEC);
+ c_print(1, "{} {}: {} secs\n", N, (double) sum / N / dist0.range, (float) difference / CLOCKS_PER_SEC);
puts("64 uniform");
crand_rng64_t stc = crand_rng64_init(seed);
- crand_uniform_i64_t dist1 = crand_uniform_i64_init(0, N);
+ crand_uniform_i64_t dist1 = crand_uniform_i64_init(0, dist0.range);
sum = 0;
- before = clock();
+ before = clock();
c_forrange (N) {
sum += crand_uniform_i64(&stc, &dist1);
}
difference = clock() - before;
- printf("%zu %f: %f secs\n", N, (double) sum / N, (float) difference / CLOCKS_PER_SEC);
+ c_print(1, "{} {}: {} secs\n", N, (double) sum / N / dist1.range, (float) difference / CLOCKS_PER_SEC);
puts("normal distribution");
@@ -57,11 +57,11 @@ int main()
sum += n;
if (n >= 0 && n < R) ++hist[n];
}
-
+
cstr_t bar = cstr_init();
c_forrange (i, int, R) {
cstr_take(&bar, cstr_with_size(hist[i] * 25ull * R / N2, '*'));
- printf("%2d %s\n", i, bar.str);
+ c_print(1, "{:3} {}\n", i, bar.str);
}
cstr_del(&bar);
} \ No newline at end of file
diff --git a/examples/rngtest.c b/examples/rngtest.c
index df3aa41c..0ca7f79c 100644
--- a/examples/rngtest.c
+++ b/examples/rngtest.c
@@ -1,23 +1,24 @@
#include <stdio.h>
#include <time.h>
#include <stc/crandom.h>
+#include <stc/cfmt.h>
#ifdef __cplusplus
#include <random>
#endif
-#define NN 3000000000
+#define NN 300000000
int main(void)
{
clock_t difference, before;
uint64_t v;
-
+
crand_rng64_t stc = crand_rng64_init(time(NULL));
crand_uniform_i64_t idist = crand_uniform_i64_init(10, 20);
crand_uniform_f64_t fdist = crand_uniform_f64_init(10, 20);
- c_forrange (30) printf("%02zd ", crand_uniform_i64(&stc, &idist));
+ c_forrange (30) c_print(1, "{:02} ", crand_uniform_i64(&stc, &idist));
puts("");
crand_rng32_t pcg = crand_rng32_init(time(NULL));
@@ -31,7 +32,7 @@ int main(void)
v += crand_uniform_i32(&pcg, &i32dist);
}
difference = clock() - before;
- printf("pcg32: %.02f, %zu\n", (float) difference / CLOCKS_PER_SEC, v);
+ c_print(1, "pcg32: {:.02f}, {}\n", (float) difference / CLOCKS_PER_SEC, v);
before = clock(); \
v = 0;
@@ -40,15 +41,15 @@ int main(void)
v += crand_uniform_i64(&stc, &idist);
}
difference = clock() - before;
- printf("stc64: %.02f, %zu\n", (float) difference / CLOCKS_PER_SEC, v);
+ c_print(1, "stc64: {:.02f}, {}\n", (float) difference / CLOCKS_PER_SEC, v);
- c_forrange (8) printf("%d ", crand_uniform_i32(&pcg, &i32dist));
+ c_forrange (8) c_print(1, "{} ", crand_uniform_i32(&pcg, &i32dist));
puts("");
-
- c_forrange (8) printf("%f ", crand_uniform_f32(&pcg, &f32dist));
+
+ c_forrange (8) c_print(1, "{} ", crand_uniform_f32(&pcg, &f32dist));
puts("");
-
- c_forrange (8) printf("%f ", crand_uniform_f64(&stc, &fdist));
+
+ c_forrange (8) c_print(1, "{} ", crand_uniform_f64(&stc, &fdist));
puts("");
} \ No newline at end of file
diff --git a/examples/share_ptr2.c b/examples/share_ptr2.c
index d4b536ac..3ef62805 100644
--- a/examples/share_ptr2.c
+++ b/examples/share_ptr2.c
@@ -1,12 +1,12 @@
#include <stc/cptr.h>
#include <stc/cmap.h>
-#include <stc/cstr.h>
+#include <stc/cfmt.h>
#include <stdio.h>
typedef struct { cstr_t name, last; } Person;
Person* Person_from(Person* p, cstr_t name, cstr_t last) {
- printf("make %s\n", name.str);
+ c_print(1, "make {}\n", name.str);
p->name = name, p->last = last;
return p;
}
@@ -15,7 +15,7 @@ Person* Person_make(Person* p, const char* name, const char* last) {
return p;
}
void Person_del(Person* p) {
- printf("del: %s\n", p->name.str);
+ c_print(1, "del: {}\n", p->name.str);
c_del(cstr, &p->name, &p->last);
}
@@ -36,7 +36,7 @@ int main() {
c_try_emplace(&map, cmap_pe, 11, csptr_pe_from(Person_make(c_new(Person), "Hello", "World!")));
c_foreach (i, cmap_pe, map)
- printf(" %d: %s\n", i.val->first, i.val->second.get->name.str);
+ c_print(1, " {}: {}\n", i.val->first, i.val->second.get->name.str);
puts("Destroy map:");
cmap_pe_del(&map);
diff --git a/examples/stack.c b/examples/stack.c
index f65b9243..428f99a0 100644
--- a/examples/stack.c
+++ b/examples/stack.c
@@ -1,5 +1,5 @@
-#include <stdio.h>
+#include <stc/cfmt.h>
#include <stc/cstr.h>
#include <stc/cstack.h>
@@ -12,16 +12,16 @@ int main() {
cstack_i stack = cstack_i_init();
cstack_c chars = cstack_c_init();
- c_forrange (i, int, 101)
+ c_forrange (i, int, 101)
cstack_i_push(&stack, i*i);
- printf("%d\n", *cstack_i_top(&stack));
+ c_print(1, "{}\n", *cstack_i_top(&stack));
- c_forrange (i, int, 90)
+ c_forrange (i, int, 90)
cstack_i_pop(&stack);
-
+
c_foreach (i, cstack_i, stack)
- printf(" %d", *i.val);
+ c_print(1, " {}", *i.val);
puts("");
- printf("top: %d\n", *cstack_i_top(&stack));
+ c_print(1, "top: {}\n", *cstack_i_top(&stack));
} \ No newline at end of file
diff --git a/examples/words.c b/examples/words.c
index fb8b9b8c..b72251c6 100644
--- a/examples/words.c
+++ b/examples/words.c
@@ -1,5 +1,5 @@
-#include <stc/cstr.h>
+#include <stc/cfmt.h>
#include <stc/cmap.h>
#include <stc/clist.h>
#include <stc/cvec.h>
@@ -18,7 +18,7 @@ int main1()
});
clist_str_push_back(&lwords, cstr_from_fmt("%f", 123897.0 / 23.0));
c_foreach (w, clist_str, lwords)
- printf("%s\n", w.val->str);
+ c_print(1, "{}\n", w.val->str);
puts("");
cvec_str words = cvec_INIT;
@@ -32,7 +32,7 @@ int main1()
cmap_si_emplace(&word_map, w.val->str, 0).first->second += 1;
c_foreach (i, cmap_si, word_map) {
- printf("%d occurrences of word '%s'\n", i.val->second, i.val->first.str);
+ c_print(1, "{} occurrences of word '{}'\n", i.val->second, i.val->first.str);
}
cmap_si_del(&word_map);
diff --git a/stc/cfmt.h b/stc/cfmt.h
index afb53018..534c9964 100644
--- a/stc/cfmt.h
+++ b/stc/cfmt.h
@@ -38,7 +38,7 @@ int main() {
c_print(1, "Color: ({} {} {}), {}\n", r, g, b, flag);
c_print(1, "{:10}, {:10}, {:.2f}\n", 42, 43, 3.141592267);
c_print(stdout, "{:10} {:10} {:10}\n", z, z, w);
- c_print(stderr, "100%: {} {:.*} {}\n", string.str, 4, pi, x);
+ c_print(stderr, "100%: {:>} {:.*} {}\n", string.str, 4, pi, x);
c_print(&string, "Precision: {} {:.16} {}", string.str, pi, x);
c_print(1, "{}\nvector: ({}, {}, {})\n", string.str, 3.2, 3.3, 4.2/3.2);
cstr_del(&string);
@@ -73,7 +73,7 @@ STC_INLINE const char* _cfmt_strftime(char* n, char buf[][_cfmt_sn], size_t maxs
#endif
#define _cfmt(x) _Generic (x, \
- _Bool: "B", \
+ _Bool: "hhu", \
_cfmt_uschar(), \
short: "hd", \
unsigned short: "hu", \
@@ -95,7 +95,7 @@ STC_INLINE const char* _cfmt_strftime(char* n, char buf[][_cfmt_sn], size_t maxs
inline auto _cfmt_fn(char*) {return sprintf;}
inline auto _cfmt_fn(int s) {return _cfmt_printf;}
inline auto _cfmt_fn(cstr_t*) {return cstr_fmt;}
- inline auto _cfmt(bool x) {return "B";}
+ inline auto _cfmt(bool x) {return "hhu";}
inline auto _cfmt(char x) {return "c";}
inline auto _cfmt(unsigned char x) {return "hhu";}
inline auto _cfmt(short x) {return "hd";}
@@ -218,8 +218,6 @@ _cfmt_conv(int nargs, const char *fmt, ...) {
done:
if (!strchr("csdioxXufFeEaAgGnp", fmt[-1]))
while (*arg) *p++ = *arg++;
- if (p[-1] == 'B')
- memmove(p0+1, p0, p-p0), *p0 = '+', *p++ = 'd';
else if (!align && strchr("cs", p[-1]))
memmove(p0+1, p0, p-p0), *p0 = '-', ++p;
fmt += *fmt == '}';