summaryrefslogtreecommitdiffhomepage
path: root/misc/examples
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2023-02-04 23:35:56 +0100
committerGitHub <[email protected]>2023-02-04 23:35:56 +0100
commitadc47cefc2976768c3f0b773bd26bfd1062e8a53 (patch)
tree4923f88afb0d091d5d39ae03d65a4998a0517652 /misc/examples
parent0c4c4f8bba17562735b67b2923cd23c773aa53a7 (diff)
parentd2ff84c53aa9bd3857fdf22dcf7cd9398a4780be (diff)
downloadSTC-modified-adc47cefc2976768c3f0b773bd26bfd1062e8a53.tar.gz
STC-modified-adc47cefc2976768c3f0b773bd26bfd1062e8a53.zip
Merge pull request #46 from tylov/newinit
Version 4.1 RC2: signed sizes and indices, cspan with numpy slicing.
Diffstat (limited to 'misc/examples')
-rw-r--r--misc/examples/astar.c4
-rw-r--r--misc/examples/birthday.c6
-rw-r--r--misc/examples/bits.c12
-rw-r--r--misc/examples/bits2.c4
-rw-r--r--misc/examples/books.c2
-rw-r--r--misc/examples/box.c2
-rw-r--r--misc/examples/city.c2
-rw-r--r--misc/examples/complex.c2
-rw-r--r--misc/examples/csmap_erase.c6
-rw-r--r--misc/examples/csmap_find.c2
-rw-r--r--misc/examples/cstr_match.c6
-rw-r--r--misc/examples/demos.c12
-rw-r--r--misc/examples/forfilter.c30
-rw-r--r--misc/examples/forloops.c10
-rw-r--r--misc/examples/functor.c2
-rw-r--r--misc/examples/gauss1.c4
-rw-r--r--misc/examples/gauss2.c4
-rwxr-xr-xmisc/examples/make.sh12
-rw-r--r--misc/examples/multidim.c103
-rw-r--r--misc/examples/music_arc.c2
-rw-r--r--misc/examples/new_map.c17
-rw-r--r--misc/examples/new_pque.c2
-rw-r--r--misc/examples/new_queue.c10
-rw-r--r--misc/examples/new_sptr.c2
-rw-r--r--misc/examples/person_arc.c2
-rw-r--r--misc/examples/prime.c23
-rw-r--r--misc/examples/printspan.c47
-rw-r--r--misc/examples/priority.c6
-rw-r--r--misc/examples/queue.c6
-rw-r--r--misc/examples/random.c18
-rw-r--r--misc/examples/rawptr_elements.c2
-rw-r--r--misc/examples/regex1.c2
-rw-r--r--misc/examples/regex2.c2
-rw-r--r--misc/examples/regex_match.c5
-rw-r--r--misc/examples/regex_replace.c6
-rw-r--r--misc/examples/shape.c14
-rw-r--r--misc/examples/sort.c8
-rw-r--r--misc/examples/splitstr.c2
-rw-r--r--misc/examples/sso_map.c2
-rw-r--r--misc/examples/sso_substr.c4
-rw-r--r--misc/examples/stack.c2
-rw-r--r--misc/examples/sview_split.c2
-rw-r--r--misc/examples/utf8replace_c.c7
-rw-r--r--misc/examples/vikings.c4
44 files changed, 225 insertions, 197 deletions
diff --git a/misc/examples/astar.c b/misc/examples/astar.c
index 828de8ce..ddc9de3a 100644
--- a/misc/examples/astar.c
+++ b/misc/examples/astar.c
@@ -37,7 +37,7 @@ point_equal(const point* a, const point* b)
point
point_from(const cstr* maze, const char* c, int width)
{
- int index = cstr_find(maze, c);
+ int index = (int)cstr_find(maze, c);
return point_init(index % width, index / width, width);
}
@@ -156,7 +156,7 @@ main(void)
"# # # # # # #\n"
"#########################################################################\n"), cstr_drop(&maze))
{
- int width = cstr_find(&maze, "\n") + 1;
+ int width = (int)cstr_find(&maze, "\n") + 1;
c_WITH (cdeq_point path = astar(&maze, width), cdeq_point_drop(&path))
{
c_FOREACH (it, cdeq_point, path)
diff --git a/misc/examples/birthday.c b/misc/examples/birthday.c
index be7f0457..fb887cd7 100644
--- a/misc/examples/birthday.c
+++ b/misc/examples/birthday.c
@@ -50,19 +50,19 @@ void test_distribution(void)
uint64_t sum = 0;
c_FOREACH (i, cmap_x, map) sum += i.ref->second;
- sum /= map.size;
+ sum /= (uint64_t)map.size;
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));
+ (1.0 - (double)i.ref->second / (double)sum));
}
}
}
int main()
{
- seed = time(NULL);
+ seed = (uint64_t)time(NULL);
test_distribution();
test_repeats();
}
diff --git a/misc/examples/bits.c b/misc/examples/bits.c
index c50eac6e..03b74881 100644
--- a/misc/examples/bits.c
+++ b/misc/examples/bits.c
@@ -4,18 +4,18 @@
int main()
{
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));
+ printf("count %" c_ZI ", %" c_ZI "\n", cbits_count(&set), cbits_size(&set));
cbits s1 = cbits_from("1110100110111");
char buf[256];
cbits_to_str(&s1, buf, 0, 255);
- printf("buf: %s: %" c_ZU "\n", buf, cbits_count(&s1));
+ printf("buf: %s: %" c_ZI "\n", buf, cbits_count(&s1));
cbits_drop(&s1);
cbits_reset(&set, 9);
cbits_resize(&set, 43, false);
printf(" str: %s\n", cbits_to_str(&set, buf, 0, 255));
- printf("%4" c_ZU ": ", cbits_size(&set));
+ printf("%4" c_ZI ": ", cbits_size(&set));
c_FORRANGE (i, cbits_size(&set))
printf("%d", cbits_test(&set, i));
puts("");
@@ -25,12 +25,12 @@ int main()
cbits_resize(&set, 93, false);
cbits_resize(&set, 102, true);
cbits_set_value(&set, 99, false);
- printf("%4" c_ZU ": ", cbits_size(&set));
+ printf("%4" c_ZI ": ", cbits_size(&set));
c_FORRANGE (i, cbits_size(&set))
printf("%d", cbits_test(&set, i));
puts("\nIterate:");
- printf("%4" c_ZU ": ", cbits_size(&set));
+ printf("%4" c_ZI ": ", cbits_size(&set));
c_FORRANGE (i, cbits_size(&set))
printf("%d", cbits_test(&set, i));
puts("");
@@ -52,7 +52,7 @@ int main()
puts("");
cbits_set_all(&set, false);
- printf("%4" c_ZU ": ", cbits_size(&set));
+ printf("%4" c_ZI ": ", 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 502e7268..9b1d1af7 100644
--- a/misc/examples/bits2.c
+++ b/misc/examples/bits2.c
@@ -9,10 +9,10 @@ int main()
{
Bits s1 = Bits_from("1110100110111");
- printf("size %" c_ZU "\n", Bits_size(&s1));
+ printf("size %" c_ZI "\n", Bits_size(&s1));
char buf[256];
Bits_to_str(&s1, buf, 0, 256);
- printf("buf: %s: count=%" c_ZU "\n", buf, Bits_count(&s1));
+ printf("buf: %s: count=%" c_ZI "\n", buf, Bits_count(&s1));
Bits_reset(&s1, 8);
printf(" s1: %s\n", Bits_to_str(&s1, buf, 0, 256));
diff --git a/misc/examples/books.c b/misc/examples/books.c
index 96c7ff6e..4695941a 100644
--- a/misc/examples/books.c
+++ b/misc/examples/books.c
@@ -32,7 +32,7 @@ int main()
// When collections store owned values (String), they can still be
// queried using references (&str).
if (cmap_str_contains(&book_reviews, "Les Misérables")) {
- printf("We've got %" c_ZU " reviews, but Les Misérables ain't one.",
+ printf("We've got %" c_ZI " reviews, but Les Misérables ain't one.",
cmap_str_size(&book_reviews));
}
diff --git a/misc/examples/box.c b/misc/examples/box.c
index 446a7603..3a7a8139 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
diff --git a/misc/examples/city.c b/misc/examples/city.c
index 7f355bdf..e24c90de 100644
--- a/misc/examples/city.c
+++ b/misc/examples/city.c
@@ -44,7 +44,7 @@ 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);
}
diff --git a/misc/examples/complex.c b/misc/examples/complex.c
index 6fed1d4d..5c212554 100644
--- a/misc/examples/complex.c
+++ b/misc/examples/complex.c
@@ -37,7 +37,7 @@ int main()
// Put in some data in the structures
stack.data[3] = 3.1415927f;
- printf("stack size: %" c_ZU "\n", FloatStack_size(&stack));
+ printf("stack size: %" c_ZI "\n", FloatStack_size(&stack));
StackList list = StackList_init();
StackList_push_back(&list, stack);
diff --git a/misc/examples/csmap_erase.c b/misc/examples/csmap_erase.c
index bef00a17..a41a1ad1 100644
--- a/misc/examples/csmap_erase.c
+++ b/misc/examples/csmap_erase.c
@@ -12,7 +12,7 @@ void printmap(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));
+ printf("\nsize() == %" c_ZI "\n\n", mymap_size(&m));
}
int main()
@@ -75,9 +75,9 @@ int main()
puts("Starting data of map m3 is:");
printmap(m3);
// The 3rd member function removes elements with a given Key
- size_t count = mymap_erase(&m3, 2);
+ int count = mymap_erase(&m3, 2);
// The 3rd member function also returns the number of elements removed
- printf("The number of elements removed from m3 is: %" c_ZU "\n", count);
+ printf("The number of elements removed from m3 is: %d\n", count);
puts("After the element with a key of 2 is deleted, the map m3 is:");
printmap(m3);
}
diff --git a/misc/examples/csmap_find.c b/misc/examples/csmap_find.c
index a61a47be..201c94e9 100644
--- a/misc/examples/csmap_find.c
+++ b/misc/examples/csmap_find.c
@@ -18,7 +18,7 @@ void print_elem(csmap_istr_raw p) {
#define using_print_collection(CX) \
void print_collection_##CX(const CX* t) { \
- printf("%" c_ZU " elements: ", CX##_size(t)); \
+ printf("%" c_ZI " elements: ", CX##_size(t)); \
\
c_FOREACH (p, CX, *t) { \
print_elem(CX##_value_toraw(p.ref)); \
diff --git a/misc/examples/cstr_match.c b/misc/examples/cstr_match.c
index 116e5dd4..bfb0df6b 100644
--- a/misc/examples/cstr_match.c
+++ b/misc/examples/cstr_match.c
@@ -5,8 +5,8 @@
int main()
{
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);
+ intptr_t pos = cstr_find_at(&ss, 0, "brown");
+ printf("%" c_ZI " [%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"));
printf("contains: %d\n", cstr_contains(&ss, "umps ove"));
printf("starts_with: %d\n", cstr_starts_with(&ss, "The quick brown"));
@@ -16,7 +16,7 @@ int main()
cstr s1 = cstr_lit("hell😀 w😀rl🐨");
csview ch1 = cstr_u8_chr(&s1, 7);
csview ch2 = cstr_u8_chr(&s1, 10);
- printf("%s\nsize: %" c_ZU ", %" c_ZU "\n", cstr_str(&s1), cstr_u8_size(&s1), cstr_size(&s1));
+ printf("%s\nsize: %" c_ZI ", %" c_ZI "\n", cstr_str(&s1), cstr_u8_size(&s1), cstr_size(&s1));
printf("ch1: %.*s\n", c_ARGSV(ch1));
printf("ch2: %.*s\n", c_ARGSV(ch2));
}
diff --git a/misc/examples/demos.c b/misc/examples/demos.c
index 4455b840..7070d860 100644
--- a/misc/examples/demos.c
+++ b/misc/examples/demos.c
@@ -13,7 +13,7 @@ void stringdemo1()
cstr_erase(&cs, 7, 5); // -nine
printf("%s.\n", cstr_str(&cs));
- cstr_replace_ex(&cs, "seven", "four", 1);
+ cstr_replace(&cs, "seven", "four", 1);
printf("%s.\n", cstr_str(&cs));
cstr_take(&cs, cstr_from_fmt("%s *** %s", cstr_str(&cs), cstr_str(&cs)));
@@ -38,7 +38,7 @@ void vectordemo1()
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)
+ for (int i = 10; i <= 100; i += 10)
cvec_ix_push(&bignums, i * i);
printf("erase - %d: %" PRIu64 "\n", 3, bignums.data[3]);
@@ -47,8 +47,8 @@ void vectordemo1()
cvec_ix_pop(&bignums); // erase the last
cvec_ix_erase_n(&bignums, 0, 1); // erase the first
- for (size_t i = 0; i < cvec_ix_size(&bignums); ++i) {
- printf("%" c_ZU ": %" PRIu64 "\n", i, bignums.data[i]);
+ for (int i = 0; i < cvec_ix_size(&bignums); ++i) {
+ printf("%d: %" PRIu64 "\n", i, bignums.data[i]);
}
}
}
@@ -175,11 +175,11 @@ void mapdemo3()
cmap_str_iter it = cmap_str_find(&table, "Make");
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));
+ printf("size %" c_ZI ": 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));
+ printf("size %" c_ZI "\n", cmap_str_size(&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.
diff --git a/misc/examples/forfilter.c b/misc/examples/forfilter.c
index 05d0bc28..1c64c9e0 100644
--- a/misc/examples/forfilter.c
+++ b/misc/examples/forfilter.c
@@ -33,12 +33,12 @@ void demo1(void)
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_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);
@@ -64,12 +64,12 @@ void demo2(void)
{
c_AUTO (IVec, vector) {
puts("demo2:");
-
- c_FORFILTER (x, crange, crange_literal(INT64_MAX)
- , c_FLT_SKIPWHILE(x, *x.ref != 11)
+ crange R = crange_make(INT64_MAX);
+ c_FORFILTER (x, crange, R
+ , c_flt_skipwhile(x, *x.ref != 11)
&& *x.ref % 2 != 0
- , c_FLT_TAKE(x, 5))
- IVec_push(&vector, *x.ref * *x.ref);
+ , c_flt_take(x, 5))
+ IVec_push(&vector, (int)(*x.ref * *x.ref));
c_FOREACH (x, IVec, vector)
printf(" %d", *x.ref);
puts("");
@@ -126,13 +126,13 @@ void demo5(void)
#define flt_even(i) ((*i.ref & 1) == 0)
#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)
+ crange R = crange_make(1963, INT32_MAX);
+ c_FORFILTER (i, crange, R
+ , 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 69c1c193..cd3c4d9e 100644
--- a/misc/examples/forloops.c
+++ b/misc/examples/forloops.c
@@ -68,12 +68,12 @@ int main()
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_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/functor.c b/misc/examples/functor.c
index 54e2702d..f1db3644 100644
--- a/misc/examples/functor.c
+++ b/misc/examples/functor.c
@@ -23,7 +23,7 @@ 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)
+#define i_less_functor(self, x, y) c_container_of(self, IPQueue, Q)->less(x, y)
#include <stc/cpque.h>
void print_queue(const char* name, IPQueue q) {
diff --git a/misc/examples/gauss1.c b/misc/examples/gauss1.c
index 519a7895..6b06b4e8 100644
--- a/misc/examples/gauss1.c
+++ b/misc/examples/gauss1.c
@@ -24,7 +24,7 @@ int main()
printf("Demo of gaussian / normal distribution of %d random samples\n", N);
// Setup random engine with normal distribution.
- uint64_t seed = time(NULL);
+ uint64_t seed = (uint64_t)time(NULL);
stc64_t rng = stc64_new(seed);
stc64_normalf_t dist = stc64_normalf_new(Mean, StdDev);
@@ -45,7 +45,7 @@ int main()
// Print the gaussian bar chart
c_FOREACH (i, cvec_ii, histvec) {
- int n = (int)(i.ref->second * StdDev * Scale * 2.5 / N);
+ int n = (int)(i.ref->second * StdDev * Scale * 2.5 / (double)N);
if (n > 0) {
printf("%4d ", i.ref->first);
c_FORRANGE (n) printf("*");
diff --git a/misc/examples/gauss2.c b/misc/examples/gauss2.c
index c2ed2e00..c627fc91 100644
--- a/misc/examples/gauss2.c
+++ b/misc/examples/gauss2.c
@@ -18,7 +18,7 @@ int main()
printf("Demo of gaussian / normal distribution of %d random samples\n", N);
// Setup random engine with normal distribution.
- uint64_t seed = time(NULL);
+ uint64_t seed = (uint64_t)time(NULL);
stc64_t rng = stc64_new(seed);
stc64_normalf_t dist = stc64_normalf_new(Mean, StdDev);
@@ -33,7 +33,7 @@ int main()
// Print the gaussian bar chart
c_AUTO (cstr, bar)
c_FORPAIR (index, count, csmap_int, mhist) {
- size_t n = (size_t) (*_.count * StdDev * Scale * 2.5 / (float)N);
+ int n = (int)((float)*_.count * StdDev * Scale * 2.5f / (float)N);
if (n > 0) {
cstr_resize(&bar, n, '*');
printf("%4d %s\n", *_.index, cstr_str(&bar));
diff --git a/misc/examples/make.sh b/misc/examples/make.sh
index bd1392fc..19a53880 100755
--- a/misc/examples/make.sh
+++ b/misc/examples/make.sh
@@ -6,14 +6,14 @@ if [ "$(uname)" = 'Linux' ]; then
oflag='-o '
fi
+cc=gcc; cflags="-s -O2 -std=c99 -Wconversion -Wpedantic -Wall -Wsign-compare -Wwrite-strings"
#cc=gcc; cflags="-s -O2 -std=c99 -Werror -Wfatal-errors -Wpedantic -Wall $sanitize"
-cc=gcc; cflags="-s -O2 -std=c99 -Werror -Wfatal-errors -Wpedantic -Wall -Wsign-compare -Wwrite-strings" # -Wconversion
#cc=tcc; cflags="-Wall -std=c99"
#cc=clang; cflags="-s -O2 -std=c99 -Werror -Wfatal-errors -Wpedantic -Wall -Wsign-compare -Wwrite-strings"
#cc=clang; cflags="-s -O2 -std=c99 -Werror -Wfatal-errors -Wpedantic -Wall -DSTC_CSTR_V1 -DSTC_CSMAP_V1"
#cc=gcc; cflags="-x c++ -s -O2 -Wall -std=c++20"
#cc=g++; cflags="-x c++ -s -O2 -Wall"
-#cc=cl; cflags="-O2 -nologo -W2 -MD"
+#cc=cl; cflags="-O2 -nologo -W3 -MD"
#cc=cl; cflags="-nologo -TP"
#cc=cl; cflags="-nologo -std:c11"
@@ -40,13 +40,13 @@ fi
if [ $run = 0 ] ; then
for i in *.c ; do
- echo $comp -I../include $i $clibs $oflag$(basename $i .c).exe
- $comp -I../include $i $clibs $oflag$(basename $i .c).exe
+ echo $comp -I../../include $i $clibs $oflag$(basename $i .c).exe
+ $comp -I../../include $i $clibs $oflag$(basename $i .c).exe
done
else
for i in *.c ; do
- echo $comp -I../include $i $clibs
- $comp -I../include $i $clibs
+ echo $comp -I../../include $i $clibs
+ $comp -I../../include $i $clibs
if [ -f $(basename -s .c $i).exe ]; then ./$(basename -s .c $i).exe; fi
if [ -f ./a.exe ]; then ./a.exe; fi
if [ -f ./a.out ]; then ./a.out; fi
diff --git a/misc/examples/multidim.c b/misc/examples/multidim.c
index 945741b1..e61959ca 100644
--- a/misc/examples/multidim.c
+++ b/misc/examples/multidim.c
@@ -5,53 +5,66 @@
#include <stdio.h>
using_cspan3(ispan, int);
-/*
-using_cspan(ispan, int, 1);
-using_cspan(ispan2, int, 2);
-using_cspan(ispan3, int, 3);
-*/
int main()
{
- cstack_int v = {0};
- c_FORLIST (i, unsigned, {1,2,3,4,5,6,7,8,9,10,11,12})
- cstack_int_push(&v, *i.ref);
-
- // View data as contiguous memory representing 12 ints
- ispan ms1 = cspan_from(&v);
- // View data as contiguous memory representing 2 rows of 6 ints each
- ispan2 ms2 = cspan_make(v.data, 2, 6);
- // View the same data as a 3D array 2 x 3 x 2
- ispan3 ms3 = cspan_make(v.data, 2, 3, 2);
-
- // write data using 2D view
- for (unsigned i=0; i != ms2.dim[0]; i++)
- for (unsigned j=0; j != ms2.dim[1]; j++)
- *cspan_at(&ms2, i, j) = i*1000 + j;
-
- // print all items using 1D view
- printf("all: ");
- for (unsigned i=0; i != cspan_size(&ms1); i++)
- printf(" %d", *cspan_at(&ms1, i));
- puts("");
-
- // or iterate a subspan...
- ispan2 sub = cspan_at3(&ms3, 1);
- printf("sub: ");
- c_FOREACH (i, ispan2, sub)
- printf(" %d", *i.ref);
- puts("");
-
- // read back using 3D view
- for (unsigned i=0; i != ms3.dim[0]; i++)
- {
- printf("slice @ i = %u\n", i);
- for (unsigned j=0; j != ms3.dim[1]; j++)
- {
- for (unsigned k=0; k != ms3.dim[2]; k++)
- printf("%d ", *cspan_at(&ms3, i, j, k));
- puts("");
+ cstack_int v = {0};
+ c_FORLIST (i, int, {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24})
+ cstack_int_push(&v, *i.ref);
+
+ // View data as contiguous memory representing 24 ints
+ ispan ms1 = cspan_from(&v);
+
+ // View the same data as a 3D array 2 x 3 x 4
+ ispan3 ms3 = cspan_md(v.data, 2, 3, 4);
+
+ puts("ms3:");
+ for (int i=0; i != ms3.shape[0]; i++) {
+ for (int j=0; j != ms3.shape[1]; j++) {
+ for (int k=0; k != ms3.shape[2]; k++) {
+ printf(" %2d", *cspan_at(&ms3, i, j, k));
+ }
+ puts("");
+ }
+ puts("");
}
- }
- cstack_int_drop(&v);
+ puts("ss3 = ms3[:, 1:3, 1:3]");
+ ispan3 ss3 = ms3;
+ //cspan_slice(&ss3, {c_ALL}, {1,3}, {1,3});
+ ss3 = cspan_slice(ispan3, &ms3, {c_ALL}, {1,3}, {1,3});
+
+ for (int i=0; i != ss3.shape[0]; i++) {
+ for (int j=0; j != ss3.shape[1]; j++) {
+ for (int k=0; k != ss3.shape[2]; k++) {
+ printf(" %2d", *cspan_at(&ss3, i, j, k));
+ }
+ puts("");
+ }
+ puts("");
+ }
+
+ puts("Iterate ss3 flat:");
+ c_FOREACH (i, ispan3, ss3)
+ printf(" %d", *i.ref);
+ puts("");
+
+ ispan2 ms2 = cspan_submd3(&ms3, 0);
+
+ // write data using 2D view
+ for (int i=0; i != ms2.shape[0]; i++)
+ for (int j=0; j != ms2.shape[1]; j++)
+ *cspan_at(&ms2, i, j) = i*1000 + j;
+
+ puts("\nview data as 1D view:");
+ for (int i=0; i != cspan_size(&ms1); i++)
+ printf(" %d", *cspan_at(&ms1, i));
+ puts("");
+
+ puts("iterate subspan ms3[1]:");
+ ispan2 sub = cspan_submd3(&ms3, 1);
+ c_FOREACH (i, ispan2, sub)
+ printf(" %d", *i.ref);
+ puts("");
+
+ cstack_int_drop(&v);
}
diff --git a/misc/examples/music_arc.c b/misc/examples/music_arc.c
index 9896c591..fad7ddcf 100644
--- a/misc/examples/music_arc.c
+++ b/misc/examples/music_arc.c
@@ -16,7 +16,7 @@ Song Song_make(const char* artist, const char* 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
diff --git a/misc/examples/new_map.c b/misc/examples/new_map.c
index 72705eb2..9cee987d 100644
--- a/misc/examples/new_map.c
+++ b/misc/examples/new_map.c
@@ -48,24 +48,25 @@ int main()
c_AUTO (cset_str, sset)
{
cmap_int_insert(&map, 123, 321);
+ cmap_int_insert(&map, 456, 654);
+ cmap_int_insert(&map, 789, 987);
- c_FORLIST (i, cmap_pnt_raw, {{{42, 14}, 1}, {{32, 94}, 2}, {{62, 81}, 3}})
- cmap_pnt_insert(&pmap, c_PAIR(i.ref));
+ pmap = c_make(cmap_pnt, {{{42, 14}, 1}, {{32, 94}, 2}, {{62, 81}, 3}});
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, {
+ smap = c_make(cmap_str, {
{"Hello, friend", "long time no see"},
- {"So long, friend", "see you around"},
- }) cmap_str_emplace(&smap, c_PAIR(i.ref));
+ {"So long", "see you around"},
+ });
- c_FORLIST (i, const char*, {
+ sset = c_make(cset_str, {
"Hello, friend",
"Nice to see you again",
- "So long, friend",
- }) cset_str_emplace(&sset, *i.ref);
+ "So long",
+ });
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 d127b592..b3d94c2f 100644
--- a/misc/examples/new_pque.c
+++ b/misc/examples/new_pque.c
@@ -48,7 +48,7 @@ int main()
cpque_int_push(&ique, 123);
cpque_int_push(&ique, 321);
// print
- for (size_t i=0; i<cpque_int_size(&ique); ++i)
+ for (int i=0; i<cpque_int_size(&ique); ++i)
printf(" %d", ique.data[i]);
puts("");
}
diff --git a/misc/examples/new_queue.c b/misc/examples/new_queue.c
index 828387b5..5c25a229 100644
--- a/misc/examples/new_queue.c
+++ b/misc/examples/new_queue.c
@@ -22,25 +22,25 @@ int point_cmp(const Point* a, const Point* b) {
int main() {
int n = 50000000;
- stc64_t rng = stc64_new(time(NULL));
+ stc64_t rng = stc64_new((uint64_t)time(NULL));
stc64_uniform_t dist = stc64_uniform_new(0, n);
c_AUTO (IQ, Q)
{
// Push 50'000'000 random numbers onto the queue.
c_FORRANGE (n)
- IQ_push(&Q, stc64_uniform(&rng, &dist));
+ IQ_push(&Q, (int)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));
+ printf("befor: size %" c_ZI ", capacity %" c_ZI "\n", IQ_size(&Q), IQ_capacity(&Q));
c_FORRANGE (n) {
- int r = stc64_uniform(&rng, &dist);
+ int r = (int)stc64_uniform(&rng, &dist);
if (r & 3)
IQ_push(&Q, r);
else
IQ_pop(&Q);
}
- printf("after: size %" c_ZU ", capacity %" c_ZU "\n", IQ_size(&Q), IQ_capacity(&Q));
+ printf("after: size %" c_ZI ", capacity %" c_ZI "\n", IQ_size(&Q), IQ_capacity(&Q));
}
}
diff --git a/misc/examples/new_sptr.c b/misc/examples/new_sptr.c
index 5441ae01..116827a4 100644
--- a/misc/examples/new_sptr.c
+++ b/misc/examples/new_sptr.c
@@ -43,7 +43,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);
}
diff --git a/misc/examples/person_arc.c b/misc/examples/person_arc.c
index f782c9da..4d9c2a51 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
diff --git a/misc/examples/prime.c b/misc/examples/prime.c
index 40ccc299..11c1f1c7 100644
--- a/misc/examples/prime.c
+++ b/misc/examples/prime.c
@@ -6,19 +6,19 @@
#include <stc/algo/crange.h>
-cbits sieveOfEratosthenes(size_t n)
+cbits sieveOfEratosthenes(intptr_t n)
{
cbits bits = cbits_with_size(n/2 + 1, true);
- size_t q = (size_t) sqrt((double) n) + 1;
- for (size_t i = 3; i < q; i += 2) {
- size_t j = i;
+ intptr_t q = (intptr_t)sqrt((double) n) + 1;
+ for (intptr_t i = 3; i < q; i += 2) {
+ intptr_t j = i;
for (; j < n; j += 2) {
if (cbits_test(&bits, j>>1)) {
i = j;
break;
}
}
- for (size_t j = i*i; j < n; j += i*2)
+ for (intptr_t j = i*i; j < n; j += i*2)
cbits_reset(&bits, j>>1);
}
return bits;
@@ -26,16 +26,16 @@ cbits sieveOfEratosthenes(size_t n)
int main(void)
{
- size_t n = 1000000000;
- printf("computing prime numbers up to %" c_ZU "\n", n);
+ intptr_t n = 1000000000;
+ printf("computing prime numbers up to %" c_ZI "\n", n);
clock_t t1 = clock();
c_WITH (cbits primes = sieveOfEratosthenes(n + 1), cbits_drop(&primes)) {
puts("done");
- size_t np = cbits_count(&primes);
+ intptr_t np = cbits_count(&primes);
clock_t t2 = clock();
- printf("number of primes: %" c_ZU ", time: %f\n", np, (t2 - t1) / (float)CLOCKS_PER_SEC);
+ printf("number of primes: %" c_ZI ", time: %f\n", np, (float)(t2 - t1) / (float)CLOCKS_PER_SEC);
puts("Show all the primes in the range [2, 1000):");
printf("2");
c_FORRANGE (i, 3, 1000, 2)
@@ -43,9 +43,10 @@ int main(void)
puts("");
puts("Show the last 50 primes using a temporary crange generator:");
- c_FORFILTER (i, crange, crange_literal(n - 1, 0, -2)
+ crange R = crange_make(n - 1, 0, -2);
+ c_FORFILTER (i, crange, R
, 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/printspan.c b/misc/examples/printspan.c
index a897e669..82b54367 100644
--- a/misc/examples/printspan.c
+++ b/misc/examples/printspan.c
@@ -1,42 +1,57 @@
// printspan.c
#include <stdio.h>
+#include <stc/cstr.h>
#define i_val int
#include <stc/cvec.h>
#define i_val int
#include <stc/cstack.h>
#define i_val int
#include <stc/cdeq.h>
+#define i_val_str
+#include <stc/cset.h>
#include <stc/cspan.h>
-using_cspan(ispan, int, 1);
+using_cspan(intspan, int, 1);
-void printMe(ispan container) {
- printf("%d\n", (int)cspan_size(&container));
- c_FOREACH (e, ispan, container) printf("%d ", *e.ref);
+void printMe(intspan container) {
+ printf("%d:", (int)cspan_size(&container));
+ c_FOREACH (e, intspan, container) printf(" %d", *e.ref);
puts("");
}
-int main() {
+int main()
+{
c_AUTO (cvec_int, vec)
c_AUTO (cstack_int, stk)
c_AUTO (cdeq_int, deq)
+ c_AUTO (cset_str, set)
{
- int arr[] = {1, 2, 3, 4, 5};
- ispan sp = cspan_from_array(arr);
- printMe((ispan)cspan_subspan(&sp, 1, 3));
+ intspan sp1 = cspan_make(intspan, {1, 2});
+ printMe( sp1 );
+
+ printMe( c_make(intspan, {1, 2, 3}) );
- printMe((ispan)cspan_from_list(int, {1, 2, 3, 4}));
+ int arr[] = {1, 2, 3, 4, 5, 6};
+ intspan sp2 = cspan_from_array(arr);
+ printMe( (intspan)cspan_subspan(&sp2, 1, 4) );
- printMe((ispan)cspan_from_array(arr));
+ c_FORLIST (i, int, {1, 2, 3, 4, 5})
+ cvec_int_push(&vec, *i.ref);
+ printMe( (intspan)cspan_from(&vec) );
- c_FORLIST (i, int, {1, 2, 3, 4, 5, 6}) cvec_int_push(&vec, *i.ref);
- printMe((ispan)cspan_from(&vec));
+ printMe( sp2 );
- c_FORLIST (i, int, {1, 2, 3, 4, 5, 6, 7}) cstack_int_push(&stk, *i.ref);
- printMe((ispan)cspan_from(&stk));
+ stk = c_make(cstack_int, {1, 2, 3, 4, 5, 6, 7});
+ printMe( (intspan)cspan_from(&stk) );
- c_FORLIST (i, int, {1, 2, 3, 4, 5, 6, 7, 8}) cdeq_int_push_front(&deq, *i.ref);
- printMe((ispan)cspan_from(&deq));
+ deq = c_make(cdeq_int, {1, 2, 3, 4, 5, 6, 7, 8});
+ printMe( (intspan)cspan_from(&deq) );
+
+ set = c_make(cset_str, {"1", "2", "3", "4", "5", "6", "7", "8", "9"});
+ printf("%d:", (int)cset_str_size(&set));
+ c_FOREACH (e, cset_str, set)
+ printf(" %s", cstr_str(e.ref));
+ puts("");
}
}
diff --git a/misc/examples/priority.c b/misc/examples/priority.c
index 22da6f60..f39c0634 100644
--- a/misc/examples/priority.c
+++ b/misc/examples/priority.c
@@ -9,13 +9,13 @@
#include <stc/cpque.h>
int main() {
- size_t N = 10000000;
- stc64_t rng = stc64_new(time(NULL));
+ intptr_t N = 10000000;
+ stc64_t rng = stc64_new((uint64_t)time(NULL));
stc64_uniform_t dist = stc64_uniform_new(0, N * 10);
c_AUTO (cpque_i, heap)
{
// Push ten million random numbers to priority queue
- printf("Push %" c_ZU " numbers\n", N);
+ printf("Push %" c_ZI " numbers\n", N);
c_FORRANGE (N)
cpque_i_push(&heap, stc64_uniform(&rng, &dist));
diff --git a/misc/examples/queue.c b/misc/examples/queue.c
index 1d325fc6..ee537b58 100644
--- a/misc/examples/queue.c
+++ b/misc/examples/queue.c
@@ -15,17 +15,17 @@ int main() {
{
// Push ten million random numbers onto the queue.
c_FORRANGE (n)
- cqueue_i_push(&queue, stc64_uniform(&rng, &dist));
+ cqueue_i_push(&queue, (int)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.
- int r = stc64_uniform(&rng, &dist);
+ int r = (int)stc64_uniform(&rng, &dist);
if (r & 1)
++n, cqueue_i_push(&queue, r);
else
--n, cqueue_i_pop(&queue);
}
- printf("%d, %" c_ZU "\n", n, cqueue_i_size(&queue));
+ printf("%d, %" c_ZI "\n", n, cqueue_i_size(&queue));
}
}
diff --git a/misc/examples/random.c b/misc/examples/random.c
index 82214924..fc4576dd 100644
--- a/misc/examples/random.c
+++ b/misc/examples/random.c
@@ -5,10 +5,10 @@
int main()
{
const size_t N = 1000000000;
- const uint64_t seed = time(NULL), range = 1000000;
+ const uint64_t seed = (uint64_t)time(NULL), range = 1000000;
stc64_t rng = stc64_new(seed);
- uint64_t sum;
+ int64_t sum;
clock_t diff, before;
printf("Compare speed of full and unbiased ranged random numbers...\n");
@@ -18,8 +18,8 @@ int main()
sum += (uint32_t)stc64_rand(&rng);
}
diff = clock() - before;
- printf("full range\t\t: %f secs, %" c_ZU ", avg: %f\n",
- (float)diff / CLOCKS_PER_SEC, N, (double)sum / N);
+ printf("full range\t\t: %f secs, %" c_ZI ", avg: %f\n",
+ (float)diff / CLOCKS_PER_SEC, N, (float)sum / (float)N);
stc64_uniform_t dist1 = stc64_uniform_new(0, range);
rng = stc64_new(seed);
@@ -29,17 +29,17 @@ int main()
sum += stc64_uniform(&rng, &dist1); // unbiased
}
diff = clock() - before;
- printf("unbiased 0-%" PRIu64 "\t: %f secs, %" c_ZU ", avg: %f\n",
- range, (float)diff/CLOCKS_PER_SEC, N, (double)sum / N);
+ printf("unbiased 0-%" PRIu64 "\t: %f secs, %" c_ZI ", avg: %f\n",
+ range, (float)diff/CLOCKS_PER_SEC, N, (float)sum / (float)N);
sum = 0;
rng = stc64_new(seed);
before = clock();
c_FORRANGE (N) {
- sum += stc64_rand(&rng) % (range + 1); // biased
+ sum += (int64_t)(stc64_rand(&rng) % (range + 1)); // biased
}
diff = clock() - before;
- printf("biased 0-%" PRIu64 " \t: %f secs, %" c_ZU ", avg: %f\n",
- range, (float)diff / CLOCKS_PER_SEC, N, (double)sum / N);
+ printf("biased 0-%" PRIu64 " \t: %f secs, %" c_ZI ", avg: %f\n",
+ range, (float)diff / CLOCKS_PER_SEC, N, (float)sum / (float)N);
}
diff --git a/misc/examples/rawptr_elements.c b/misc/examples/rawptr_elements.c
index 05910c77..96a9ba86 100644
--- a/misc/examples/rawptr_elements.c
+++ b/misc/examples/rawptr_elements.c
@@ -13,7 +13,7 @@ typedef int64_t inttype;
#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_valdrop(x) c_free(*x)
#include <stc/cmap.h>
// With cbox:
diff --git a/misc/examples/regex1.c b/misc/examples/regex1.c
index 9552db62..ae00b71c 100644
--- a/misc/examples/regex1.c
+++ b/misc/examples/regex1.c
@@ -10,7 +10,7 @@ int main(int argc, char* argv[])
c_AUTO (cstr, input)
c_AUTO (cregex, float_expr)
{
- int res = cregex_compile(&float_expr, "^[+-]?[0-9]+((\\.[0-9]*)?|\\.[0-9]+)$", CREG_DEFAULT);
+ int res = cregex_compile(&float_expr, "^[+-]?[0-9]+((\\.[0-9]*)?|\\.[0-9]+)$");
// Until "q" is given, ask for another number
if (res > 0) while (true)
{
diff --git a/misc/examples/regex2.c b/misc/examples/regex2.c
index e24cce51..2b742529 100644
--- a/misc/examples/regex2.c
+++ b/misc/examples/regex2.c
@@ -18,7 +18,7 @@ int main()
c_AUTO (cregex, re)
c_FORRANGE (i, c_ARRAYLEN(s))
{
- int res = cregex_compile(&re, s[i].pattern, CREG_DEFAULT);
+ int res = cregex_compile(&re, s[i].pattern);
if (res < 0) {
printf("error in regex pattern: %d\n", res);
continue;
diff --git a/misc/examples/regex_match.c b/misc/examples/regex_match.c
index b6bd6521..b7d6ed3a 100644
--- a/misc/examples/regex_match.c
+++ b/misc/examples/regex_match.c
@@ -1,6 +1,7 @@
#define i_extern
#include <stc/cregex.h>
#include <stc/csview.h>
+
#define i_val float
#include <stc/cstack.h>
@@ -17,12 +18,12 @@ int main()
c_AUTO (cstr, nums)
{
const char* pattern = "[+-]?([0-9]*\\.)?\\d+([Ee][+-]?\\d+)?";
- int res = cregex_compile(&re, pattern, CREG_DEFAULT);
+ int res = cregex_compile(&re, pattern);
printf("%d: %s\n", res, pattern);
// extract and convert all numbers in str to floats
c_FORMATCH (i, &re, str)
- cstack_float_push(&vec, atof(i.match[0].str));
+ cstack_float_push(&vec, (float)atof(i.match[0].str));
c_FOREACH (i, cstack_float, vec)
printf(" %g\n", *i.ref);
diff --git a/misc/examples/regex_replace.c b/misc/examples/regex_replace.c
index a170856b..ec38ca56 100644
--- a/misc/examples/regex_replace.c
+++ b/misc/examples/regex_replace.c
@@ -26,15 +26,15 @@ int main()
printf("fixed: %s\n", cstr_str(&str));
/* US date format, and add 10 years to dates: */
- cstr_take(&str, cregex_replace_pattern_ex(pattern, input, "$1/$3/$2", 0, add_10_years, CREG_DEFAULT));
+ cstr_take(&str, cregex_replace_pattern(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_ex(pattern, input, "[$0]", 1, NULL, CREG_DEFAULT));
+ cstr_take(&str, cregex_replace_pattern(pattern, input, "[$0]"));
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), cregex_drop(&re)) {
if (cregex_captures(&re) == 0)
continue; /* break c_WITH */
/* European date format. */
diff --git a/misc/examples/shape.c b/misc/examples/shape.c
index ed0c3fe1..9ce0b946 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);
}
}
@@ -50,12 +50,12 @@ typedef struct {
Point p[3];
} Triangle;
-static struct ShapeAPI Triangle_api;
+extern struct ShapeAPI Triangle_api;
Triangle Triangle_from(Point a, Point b, Point c)
{
- Triangle t = {{.api=&Triangle_api}, .p={a, b, c}};
+ Triangle t = {.shape={.api=&Triangle_api}, .p={a, b, c}};
return t;
}
@@ -68,7 +68,7 @@ static void Triangle_draw(const Shape* shape)
self->p[2].x, self->p[2].y);
}
-static struct ShapeAPI Triangle_api = {
+struct ShapeAPI Triangle_api = {
.drop = Shape_drop,
.draw = Triangle_draw,
};
@@ -85,12 +85,12 @@ typedef struct {
PointVec points;
} Polygon;
-static struct ShapeAPI Polygon_api;
+extern struct ShapeAPI Polygon_api;
Polygon Polygon_init(void)
{
- Polygon p = {{.api=&Polygon_api}, .points=PointVec_init()};
+ Polygon p = {.shape={.api=&Polygon_api}, .points=PointVec_init()};
return p;
}
@@ -116,7 +116,7 @@ static void Polygon_draw(const Shape* shape)
puts("");
}
-static struct ShapeAPI Polygon_api = {
+struct ShapeAPI Polygon_api = {
.drop = Polygon_drop,
.draw = Polygon_draw,
};
diff --git a/misc/examples/sort.c b/misc/examples/sort.c
index de94cede..65077143 100644
--- a/misc/examples/sort.c
+++ b/misc/examples/sort.c
@@ -14,7 +14,7 @@
#define fmt_Elem "%lld"
-int testsort(csort_elm_value *a, size_t size, const char *desc) {
+int testsort(csort_elm_value *a, long size, const char *desc) {
clock_t t = clock();
#ifdef __cplusplus
printf("std::sort: ");
@@ -26,13 +26,13 @@ int testsort(csort_elm_value *a, size_t size, const char *desc) {
t = clock() - t;
printf("%s: %d elements sorted in %.2f ms\n",
- desc, (int)size, t*1000.0/CLOCKS_PER_SEC);
+ desc, (int)size, (float)t*1000.0f/CLOCKS_PER_SEC);
return 0;
}
int main(int argc, char *argv[]) {
- size_t i, size = argc > 1 ? strtoull(argv[1], NULL, 0) : 10000000;
- csort_elm_value *a = (csort_elm_value*)malloc(sizeof(*a) * size);
+ long i, size = argc > 1 ? strtol(argv[1], NULL, 0) : 10000000;
+ csort_elm_value *a = (csort_elm_value*)c_malloc(c_sizeof(*a) * size);
if (a == NULL) return -1;
for (i = 0; i < size; i++)
diff --git a/misc/examples/splitstr.c b/misc/examples/splitstr.c
index 70d1935e..ba927412 100644
--- a/misc/examples/splitstr.c
+++ b/misc/examples/splitstr.c
@@ -13,7 +13,7 @@ int main()
puts("\nSplit with c_FORMATCH (regex):");
- c_WITH (cregex re = cregex_from("[^ ]+", CREG_DEFAULT), cregex_drop(&re))
+ c_WITH (cregex re = cregex_from("[^ ]+"), 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 f88e5f79..0841a316 100644
--- a/misc/examples/sso_map.c
+++ b/misc/examples/sso_map.c
@@ -10,7 +10,7 @@ int main()
cmap_str_emplace(&m, "Test long ", "This is a longer string");
c_FORPAIR (k, v, cmap_str, m)
- printf("%s: '%s' Len=%" c_ZU ", Is long: %s\n",
+ printf("%s: '%s' Len=%" c_ZI ", 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 8e7450ba..7e858ec4 100644
--- a/misc/examples/sso_substr.c
+++ b/misc/examples/sso_substr.c
@@ -6,7 +6,7 @@ int main ()
{
cstr str = cstr_lit("We think in generalities, but we live in details.");
csview sv1 = cstr_substr_ex(&str, 3, 5); // "think"
- size_t pos = cstr_find(&str, "live"); // position of "live"
+ intptr_t pos = cstr_find(&str, "live"); // position of "live"
csview sv2 = cstr_substr_ex(&str, pos, 4); // "live"
csview sv3 = cstr_slice_ex(&str, -8, -1); // "details"
printf("%.*s, %.*s, %.*s\n", c_ARGSV(sv1), c_ARGSV(sv2), c_ARGSV(sv3));
@@ -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 370577b9..86099607 100644
--- a/misc/examples/stack.c
+++ b/misc/examples/stack.c
@@ -15,7 +15,7 @@ int main() {
c_AUTO (cstack_c, chars)
{
c_FORRANGE (i, 101)
- cstack_i_push(&stack, i*i);
+ cstack_i_push(&stack, (int)(i*i));
printf("%d\n", *cstack_i_top(&stack));
diff --git a/misc/examples/sview_split.c b/misc/examples/sview_split.c
index 5658924e..2d4b5324 100644
--- a/misc/examples/sview_split.c
+++ b/misc/examples/sview_split.c
@@ -6,7 +6,7 @@ int main()
{
// No memory allocations or string length calculations!
const csview date = c_SV("2021/03/12");
- size_t pos = 0;
+ intptr_t pos = 0;
const csview year = csview_token(date, "/", &pos);
const csview month = csview_token(date, "/", &pos);
const csview day = csview_token(date, "/", &pos);
diff --git a/misc/examples/utf8replace_c.c b/misc/examples/utf8replace_c.c
index 2d8d1921..3f98a2a6 100644
--- a/misc/examples/utf8replace_c.c
+++ b/misc/examples/utf8replace_c.c
@@ -16,11 +16,8 @@ int main() {
c_FOREACH (c, cstr, hello)
printf("%.*s,", c_ARGSV(c.u8.chr));
- //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");
+ str = cstr_lit("scooby, dooby doo");
+ cstr_replace(&str, "oo", "00");
printf("\n%s\n", cstr_str(&str));
}
}
diff --git a/misc/examples/vikings.c b/misc/examples/vikings.c
index c65a9caa..86d60519 100644
--- a/misc/examples/vikings.c
+++ b/misc/examples/vikings.c
@@ -23,11 +23,11 @@ static inline int RViking_cmp(const RViking* rx, const RViking* ry) {
}
static inline Viking Viking_from(RViking raw) { // note: parameter is by value
- return c_INIT(Viking){cstr_from(raw.name), cstr_from(raw.country)};
+ return c_LITERAL(Viking){cstr_from(raw.name), cstr_from(raw.country)};
}
static inline RViking Viking_toraw(const Viking* vp) {
- return c_INIT(RViking){cstr_str(&vp->name), cstr_str(&vp->country)};
+ return c_LITERAL(RViking){cstr_str(&vp->name), cstr_str(&vp->country)};
}
// With this in place, we define the Viking => int hash map type: