summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2023-01-25 20:18:02 +0100
committerTyge Løvset <[email protected]>2023-01-25 20:18:02 +0100
commit68fd366ceaa919293d348ce15c2596d485160cec (patch)
treee50caaf6dc2952da36b5cd0c60252f94ee2b77ec
parentaf4c2d6d2f3353364632701ef8208a9dcbe18623 (diff)
downloadSTC-modified-68fd366ceaa919293d348ce15c2596d485160cec.tar.gz
STC-modified-68fd366ceaa919293d348ce15c2596d485160cec.zip
Updates on cspan ++.
-rw-r--r--docs/cspan_api.md21
-rw-r--r--include/stc/cspan.h8
-rw-r--r--misc/examples/forfilter.c8
-rw-r--r--misc/examples/gauss1.c2
-rw-r--r--misc/examples/multidim.c4
-rw-r--r--misc/examples/prime.c3
-rw-r--r--misc/examples/printspan.c2
-rw-r--r--misc/examples/shape.c12
-rw-r--r--src/utf8code.c243
9 files changed, 156 insertions, 147 deletions
diff --git a/docs/cspan_api.md b/docs/cspan_api.md
index ce9236c3..6da2acca 100644
--- a/docs/cspan_api.md
+++ b/docs/cspan_api.md
@@ -18,27 +18,28 @@ using_cspan3(S, ValueType); // define span types S, S2, S3 with rank
using_cspan4(S, ValueType); // define span types S, S2, S3, S4 with ranks 1, 2, 3, 4.
```
## Methods
-Note that `cspan_make()`, `cmake_from*()`, `cspan_atN()`, `and cspan_subspanN()` require a (safe) cast to its span-type
+Note that `cspan_multidim()`, `cmake_from*()`, `cspan_atN()`, `and cspan_subspanN()` require a (safe) cast to its span-type
on assignment, but not on initialization of a span variable. All functions are type-safe, and arguments are side-effect safe, except for SpanType arg. which must not have side-effects.
```c
-SpanTypeN cspan_make(ValueType* data, size_t xdim, ...); // make N-dimensional cspan
-SpanType cspan_from(STCContainer* cnt); // create a 1D cspan from a compatible STC container
-SpanType cspan_from_array(ValueType array[]); // create a 1D cspan from a C array
+SpanType cspan_make(T SpanType, {v1, v2, ...}); // make a 1d-dimensional cspan from values
+SpanTypeN cspan_multidim(ValueType* data, size_t xdim, ...); // create a multi-dimensional cspan
+SpanType cspan_from(STCContainer* cnt); // create a 1d cspan from a compatible STC container
+SpanType cspan_from_array(ValueType array[]); // create a 1d cspan from a C array
-SpanType cspan_flatten(SpanTypeN* span); // create a 1D cspan from a multidim span
+SpanType cspan_flatten(SpanTypeN* span); // create a 1d cspan from a multidim span
void cspan_resize(SpanTypeN* self, size_t xdim, ...); // change the extent of each dimension
size_t cspan_size(const SpanTypeN* self); // return number of elements
unsigned cspan_rank(const SpanTypeN* self); // return number of dimensions
size_t cspan_index(const SpanTypeN* self, size_t x, ...); // index of element
-ValueType* cspan_at(SpanTypeN* self, size_t x, ...); // at(): num of args decides input SpanTypeN.
+ValueType* cspan_at(SpanTypeN* self, size_t x, ...); // at(): num of args specifies rank of input span.
ValueType* cspan_front(SpanTypeN* self);
ValueType* cspan_back(SpanTypeN* self);
-SpanType cspan_at2(SpanType2* self, size_t x); // return a cspan from a 2D SpanType.
-SpanTypeN cspan_at3(SpanType3* self, size_t x, ...); // atN(): N decides input SpanType,
-SpanTypeN cspan_at4(SpanType4* self, size_t x, ...); // and num of args decides returned SpanTypeN.
+SpanType cspan_at2(SpanType2* self, size_t x); // return a 1d subspan from a 2d span.
+SpanTypeN cspan_at3(SpanType3* self, size_t x, ...); // return a 1 or 2d subspan from a 3d span.
+SpanTypeN cspan_at4(SpanType4* self, size_t x, ...); // number of args determines rank of output span.
// return a subspan of same rank:
SpanType cspan_subspan(const SpanType* self, size_t offset, size_t count);
@@ -76,7 +77,7 @@ int main()
cstack_float_push(&vec, i);
// define "span3[xd][yd][zd]"
- Span3 span3 = cspan_make(vec.data, xd, yd, zd);
+ Span3 span3 = cspan_multidim(vec.data, xd, yd, zd);
*cspan_at(&span3, 4, 3, 2) = 3.14159f;
printf("index: %d", (int)cspan_index(&span3, 4, 3, 2));
diff --git a/include/stc/cspan.h b/include/stc/cspan.h
index 2b600a2a..5b0358e2 100644
--- a/include/stc/cspan.h
+++ b/include/stc/cspan.h
@@ -30,7 +30,7 @@ using_cspan(Intspan, int, 1);
int demo1() {
float raw[4*5];
- Span2f ms = cspan_make(raw, 4, 5);
+ Span2f ms = cspan_multidim(raw, 4, 5);
for (size_t i=0; i<ms.dim[0]; i++)
for (size_t j=0; j<ms.dim[1]; j++)
@@ -87,11 +87,11 @@ int demo2() {
#define cspan_rank_ok(self, rank) c_static_assert(cspan_rank(self) == rank)
-#define cspan_make(array, ...) \
+#define cspan_multidim(array, ...) \
{.data=array, .dim={__VA_ARGS__}}
-/* For static initialization use , cspan_init(). c_make() only works for non-static. */
-#define cspan_init(SpanType, ...) \
+/* For static initialization, use cspan_make(). c_make() for non-static only. */
+#define cspan_make(SpanType, ...) \
{.data=(SpanType##_value[])__VA_ARGS__, .dim={sizeof((SpanType##_value[])__VA_ARGS__)/sizeof(SpanType##_value)}}
#define cspan_flatten(span) \
diff --git a/misc/examples/forfilter.c b/misc/examples/forfilter.c
index e041fd68..fdc013a3 100644
--- a/misc/examples/forfilter.c
+++ b/misc/examples/forfilter.c
@@ -64,8 +64,8 @@ void demo2(void)
{
c_AUTO (IVec, vector) {
puts("demo2:");
-
- c_FORFILTER (x, crange, crange_literal(INT64_MAX)
+ 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))
@@ -126,8 +126,8 @@ 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
+ 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)
diff --git a/misc/examples/gauss1.c b/misc/examples/gauss1.c
index 519a7895..db103945 100644
--- a/misc/examples/gauss1.c
+++ b/misc/examples/gauss1.c
@@ -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/multidim.c b/misc/examples/multidim.c
index 3eef1497..8a5492dc 100644
--- a/misc/examples/multidim.c
+++ b/misc/examples/multidim.c
@@ -15,9 +15,9 @@ int main()
// 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);
+ ispan2 ms2 = cspan_multidim(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);
+ ispan3 ms3 = cspan_multidim(v.data, 2, 3, 2);
// write data using 2D view
for (unsigned i=0; i != ms2.dim[0]; i++)
diff --git a/misc/examples/prime.c b/misc/examples/prime.c
index 4a6b0f68..5c8d65d3 100644
--- a/misc/examples/prime.c
+++ b/misc/examples/prime.c
@@ -43,7 +43,8 @@ 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)) {
printf("%lld ", *i.ref);
diff --git a/misc/examples/printspan.c b/misc/examples/printspan.c
index 227fd2ed..82b54367 100644
--- a/misc/examples/printspan.c
+++ b/misc/examples/printspan.c
@@ -27,7 +27,7 @@ int main()
c_AUTO (cdeq_int, deq)
c_AUTO (cset_str, set)
{
- intspan sp1 = cspan_init(intspan, {1, 2});
+ intspan sp1 = cspan_make(intspan, {1, 2});
printMe( sp1 );
printMe( c_make(intspan, {1, 2, 3}) );
diff --git a/misc/examples/shape.c b/misc/examples/shape.c
index ed0c3fe1..75a5e174 100644
--- a/misc/examples/shape.c
+++ b/misc/examples/shape.c
@@ -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/src/utf8code.c b/src/utf8code.c
index ecf79880..3ad47941 100644
--- a/src/utf8code.c
+++ b/src/utf8code.c
@@ -122,13 +122,18 @@ typedef struct {
int nr16;
} UGroup;
-static const UGroup unicode_groups[U8G_SIZE];
+#ifndef __cplusplus
+static
+#else
+extern
+#endif
+const UGroup _utf8_unicode_groups[U8G_SIZE];
bool utf8_isgroup(int group, uint32_t c) {
- for (int j=0; j<unicode_groups[group].nr16; ++j) {
- if (c < unicode_groups[group].r16[j].lo)
+ for (int j=0; j<_utf8_unicode_groups[group].nr16; ++j) {
+ if (c < _utf8_unicode_groups[group].r16[j].lo)
return false;
- if (c <= unicode_groups[group].r16[j].hi)
+ if (c <= _utf8_unicode_groups[group].r16[j].hi)
return true;
}
return false;
@@ -323,139 +328,141 @@ static const URange16 Zs_range16[] = { // Space separator
};
static const URange16 Arabic_range16[] = {
- { 1536, 1540 },
- { 1542, 1547 },
- { 1549, 1562 },
- { 1564, 1566 },
- { 1568, 1599 },
- { 1601, 1610 },
- { 1622, 1647 },
- { 1649, 1756 },
- { 1758, 1791 },
- { 1872, 1919 },
- { 2160, 2190 },
- { 2192, 2193 },
- { 2200, 2273 },
- { 2275, 2303 },
- { 64336, 64450 },
- { 64467, 64829 },
- { 64832, 64911 },
- { 64914, 64967 },
- { 64975, 64975 },
- { 65008, 65023 },
- { 65136, 65140 },
- { 65142, 65276 },
+ { 1536, 1540 },
+ { 1542, 1547 },
+ { 1549, 1562 },
+ { 1564, 1566 },
+ { 1568, 1599 },
+ { 1601, 1610 },
+ { 1622, 1647 },
+ { 1649, 1756 },
+ { 1758, 1791 },
+ { 1872, 1919 },
+ { 2160, 2190 },
+ { 2192, 2193 },
+ { 2200, 2273 },
+ { 2275, 2303 },
+ { 64336, 64450 },
+ { 64467, 64829 },
+ { 64832, 64911 },
+ { 64914, 64967 },
+ { 64975, 64975 },
+ { 65008, 65023 },
+ { 65136, 65140 },
+ { 65142, 65276 },
};
static const URange16 Cyrillic_range16[] = {
- { 1024, 1156 },
- { 1159, 1327 },
- { 7296, 7304 },
- { 7467, 7467 },
- { 7544, 7544 },
- { 11744, 11775 },
- { 42560, 42655 },
- { 65070, 65071 },
+ { 1024, 1156 },
+ { 1159, 1327 },
+ { 7296, 7304 },
+ { 7467, 7467 },
+ { 7544, 7544 },
+ { 11744, 11775 },
+ { 42560, 42655 },
+ { 65070, 65071 },
};
static const URange16 Devanagari_range16[] = {
- { 2304, 2384 },
- { 2389, 2403 },
- { 2406, 2431 },
- { 43232, 43263 },
+ { 2304, 2384 },
+ { 2389, 2403 },
+ { 2406, 2431 },
+ { 43232, 43263 },
};
static const URange16 Greek_range16[] = {
- { 880, 883 },
- { 885, 887 },
- { 890, 893 },
- { 895, 895 },
- { 900, 900 },
- { 902, 902 },
- { 904, 906 },
- { 908, 908 },
- { 910, 929 },
- { 931, 993 },
- { 1008, 1023 },
- { 7462, 7466 },
- { 7517, 7521 },
- { 7526, 7530 },
- { 7615, 7615 },
- { 7936, 7957 },
- { 7960, 7965 },
- { 7968, 8005 },
- { 8008, 8013 },
- { 8016, 8023 },
- { 8025, 8025 },
- { 8027, 8027 },
- { 8029, 8029 },
- { 8031, 8061 },
- { 8064, 8116 },
- { 8118, 8132 },
- { 8134, 8147 },
- { 8150, 8155 },
- { 8157, 8175 },
- { 8178, 8180 },
- { 8182, 8190 },
- { 8486, 8486 },
- { 43877, 43877 },
+ { 880, 883 },
+ { 885, 887 },
+ { 890, 893 },
+ { 895, 895 },
+ { 900, 900 },
+ { 902, 902 },
+ { 904, 906 },
+ { 908, 908 },
+ { 910, 929 },
+ { 931, 993 },
+ { 1008, 1023 },
+ { 7462, 7466 },
+ { 7517, 7521 },
+ { 7526, 7530 },
+ { 7615, 7615 },
+ { 7936, 7957 },
+ { 7960, 7965 },
+ { 7968, 8005 },
+ { 8008, 8013 },
+ { 8016, 8023 },
+ { 8025, 8025 },
+ { 8027, 8027 },
+ { 8029, 8029 },
+ { 8031, 8061 },
+ { 8064, 8116 },
+ { 8118, 8132 },
+ { 8134, 8147 },
+ { 8150, 8155 },
+ { 8157, 8175 },
+ { 8178, 8180 },
+ { 8182, 8190 },
+ { 8486, 8486 },
+ { 43877, 43877 },
};
static const URange16 Han_range16[] = {
- { 11904, 11929 },
- { 11931, 12019 },
- { 12032, 12245 },
- { 12293, 12293 },
- { 12295, 12295 },
- { 12321, 12329 },
- { 12344, 12347 },
- { 13312, 19903 },
- { 19968, 40959 },
- { 63744, 64109 },
- { 64112, 64217 },
+ { 11904, 11929 },
+ { 11931, 12019 },
+ { 12032, 12245 },
+ { 12293, 12293 },
+ { 12295, 12295 },
+ { 12321, 12329 },
+ { 12344, 12347 },
+ { 13312, 19903 },
+ { 19968, 40959 },
+ { 63744, 64109 },
+ { 64112, 64217 },
};
static const URange16 Latin_range16[] = {
- { 65, 90 },
- { 97, 122 },
- { 170, 170 },
- { 186, 186 },
- { 192, 214 },
- { 216, 246 },
- { 248, 696 },
- { 736, 740 },
- { 7424, 7461 },
- { 7468, 7516 },
- { 7522, 7525 },
- { 7531, 7543 },
- { 7545, 7614 },
- { 7680, 7935 },
- { 8305, 8305 },
- { 8319, 8319 },
- { 8336, 8348 },
- { 8490, 8491 },
- { 8498, 8498 },
- { 8526, 8526 },
- { 8544, 8584 },
- { 11360, 11391 },
- { 42786, 42887 },
- { 42891, 42954 },
- { 42960, 42961 },
- { 42963, 42963 },
- { 42965, 42969 },
- { 42994, 43007 },
- { 43824, 43866 },
- { 43868, 43876 },
- { 43878, 43881 },
- { 64256, 64262 },
- { 65313, 65338 },
- { 65345, 65370 },
+ { 65, 90 },
+ { 97, 122 },
+ { 170, 170 },
+ { 186, 186 },
+ { 192, 214 },
+ { 216, 246 },
+ { 248, 696 },
+ { 736, 740 },
+ { 7424, 7461 },
+ { 7468, 7516 },
+ { 7522, 7525 },
+ { 7531, 7543 },
+ { 7545, 7614 },
+ { 7680, 7935 },
+ { 8305, 8305 },
+ { 8319, 8319 },
+ { 8336, 8348 },
+ { 8490, 8491 },
+ { 8498, 8498 },
+ { 8526, 8526 },
+ { 8544, 8584 },
+ { 11360, 11391 },
+ { 42786, 42887 },
+ { 42891, 42954 },
+ { 42960, 42961 },
+ { 42963, 42963 },
+ { 42965, 42969 },
+ { 42994, 43007 },
+ { 43824, 43866 },
+ { 43868, 43876 },
+ { 43878, 43881 },
+ { 64256, 64262 },
+ { 65313, 65338 },
+ { 65345, 65370 },
};
#define UNI_ENTRY(Code) \
{ Code##_range16, sizeof(Code##_range16)/sizeof(URange16) }
-
-static const UGroup unicode_groups[U8G_SIZE] = {
+#ifndef __cplusplus
+static
+#endif
+const UGroup _utf8_unicode_groups[U8G_SIZE] = {
[U8G_Cc] = UNI_ENTRY(Cc),
[U8G_Lt] = UNI_ENTRY(Lt),
[U8G_Nd] = UNI_ENTRY(Nd),