diff options
| author | Tyge Løvset <[email protected]> | 2023-01-15 21:18:39 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2023-01-15 21:18:39 +0100 |
| commit | 770a39ff6ed39494569a411949f22edcdb7eb30c (patch) | |
| tree | ddc38f64ed90eceff9571c1c2753b4425947629e /include/stc | |
| parent | c5e071c622d6f460aa1ad07b6543ceaaed5209fc (diff) | |
| download | STC-modified-770a39ff6ed39494569a411949f22edcdb7eb30c.tar.gz STC-modified-770a39ff6ed39494569a411949f22edcdb7eb30c.zip | |
Large commit:
- Moved stc/algo/cspan.h to stc/cspan.h - its a data view type similar to csview. +Many updates. Added docs/cspan_api.md page!
- Update c11/fmt.h to VER 2.0: NEW API, see test. NOTE: fmt.h is not officially part of STC, as it is C11, and STC is C99.
- Renamed crange_LITERAL() back to crange_literal(), and cspan_LITERAL() to cspan_literal(). These returns a compound literal (lvalue) that can be passed to a c_FOR*-iterator.
Diffstat (limited to 'include/stc')
| -rw-r--r-- | include/stc/algo/crange.h | 4 | ||||
| -rw-r--r-- | include/stc/ccommon.h | 5 | ||||
| -rw-r--r-- | include/stc/cspan.h (renamed from include/stc/algo/cspan.h) | 84 |
3 files changed, 60 insertions, 33 deletions
diff --git a/include/stc/algo/crange.h b/include/stc/algo/crange.h index 518320b5..63242a54 100644 --- a/include/stc/algo/crange.h +++ b/include/stc/algo/crange.h @@ -34,7 +34,7 @@ int main() // use a temporary crange object. int a = 100, b = INT32_MAX; - c_FORFILTER (i, crange, crange_object(a, b, 8) + c_FORFILTER (i, crange, crange_literal(a, b, 8) , i.index > 10 , c_FLT_TAKE(i, 3)) printf(" %lld", *i.ref); @@ -46,7 +46,7 @@ int main() #include <stc/ccommon.h> -#define crange_object(...) \ +#define crange_literal(...) \ (*(crange[]){crange_make(__VA_ARGS__)}) typedef long long crange_value; diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index a2c529a7..edf4b2cb 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -38,6 +38,11 @@ #define c_ZU PRIu64 #define c_NPOS INT64_MAX #endif +#if defined STC_NDEBUG || defined NDEBUG + #define c_ASSERT(expr) (void)(expr) +#else + #define c_ASSERT(expr) assert(expr) +#endif #if defined(_MSC_VER) #pragma warning(disable: 4116 4996) // unnamed type definition in parentheses diff --git a/include/stc/algo/cspan.h b/include/stc/cspan.h index 03cb8622..f5114a11 100644 --- a/include/stc/algo/cspan.h +++ b/include/stc/cspan.h @@ -22,19 +22,21 @@ */ /* #include <stdio.h> -#include <stc/algo/cspan.h> - -using_cspan(Span3f, float, 3); +#include <stc/cspan.h> +#include <stc/algo/filter.h> +using_cspan(Span2f, float, 2); +using_cspan(Intspan, int, 1); int demo1() { - float raw[3*4*5]; - Span3f span = cspan_make(raw, 3, 4, 5); - *cspan_at(&span, 2, 3, 4) = 100; + float raw[4*5]; + Span2f ms = cspan_make(raw, 4, 5); - printf("%f\n", *cspan_at(&span, 2, 3, 4)); -} + for (size_t i=0; i<ms.dim[0]; i++) + for (size_t j=0; j<ms.dim[1]; j++) + *cspan_at(&ms, i, j) = i*1000 + j; -using_cspan(Intspan, int, 1); + printf("%f\n", *cspan_at(&span, 3, 4)); +} int demo2() { int array[] = {1, 2, 3, 4, 5}; @@ -44,8 +46,8 @@ int demo2() { printf(" %d", *i.ref); puts(""); - // use a temporary IntSpan object. - c_FORFILTER (i, Intspan, cspan_object(Intspan, {10, 20, 30, 23, 22, 21}) + // use a temporary Intspan object. + c_FORFILTER (i, Intspan, cspan_literal(Intspan, {10, 20, 30, 23, 22, 21}) , c_FLT_SKIPWHILE(i, *i.ref < 25) && (*i.ref & 1) == 0 // even only , c_FLT_TAKE(i, 2)) // break after 2 @@ -56,12 +58,12 @@ int demo2() { #ifndef STC_CSPAN_H_INCLUDED #define STC_CSPAN_H_INCLUDED -#include "../ccommon.h" +#include "ccommon.h" #define using_cspan(Self, T, RANK) \ typedef T Self##_value, Self##_raw; \ - typedef struct { Self##_value *data; uint32_t dim[RANK]; } Self; \ typedef struct { Self##_value *ref, *end; } Self##_iter; \ + typedef struct { Self##_value *data; uint32_t dim[RANK]; } Self; \ \ STC_INLINE Self##_iter Self##_begin(const Self* self) { \ Self##_iter it = {self->data, self->data + cspan_size(self)}; \ @@ -75,26 +77,46 @@ int demo2() { { if (++it->ref == it->end) it->ref = NULL; } \ struct stc_nostruct -#define cspan_assert(self, rank) c_STATIC_ASSERT(cspan_rank(self) == rank) +#define using_cspan2(Self, T) using_cspan(Self##1, T, 1); using_cspan(Self##2, T, 2) +#define using_cspan3(Self, T) using_cspan2(Self, T); using_cspan(Self##3, T, 3) +#define using_cspan4(Self, T) using_cspan3(Self, T); using_cspan(Self##4, T, 4) + +#define cspan_check_rank(self, rank) c_STATIC_ASSERT(cspan_rank(self) == rank) -#define cspan_object(S, ...) \ +#define cspan_literal(S, ...) \ ((S){.data = (S##_value[])__VA_ARGS__, \ .dim = {sizeof((S##_value[])__VA_ARGS__)/sizeof(S##_value)}}) -#define cspan_make(data, ...) {data, {__VA_ARGS__}} -#define cspan_from(data) \ - {data + c_STATIC_ASSERT(sizeof(data) != sizeof(void*)), {c_ARRAYLEN(data)}} +#define cspan_make(data, ...) \ + {data, {__VA_ARGS__}} + +/* create a cspan from a cvec, cstack, cdeq, cqueue, or cpque (heap) */ +#define cspan_from(container) \ + {(container)->data, {(container)->_len}} + +#define cspan_from_array(array) \ + {(array) + c_STATIC_ASSERT(sizeof(array) != sizeof(void*)), {c_ARRAYLEN(array)}} #define cspan_size(self) _cspan_size((self)->dim, cspan_rank(self)) #define cspan_rank(self) c_ARRAYLEN((self)->dim) +#define cspan_index(self, ...) \ + c_PASTE(_cspan_i, c_NUMARGS(__VA_ARGS__))((self)->dim, __VA_ARGS__) + \ + cspan_check_rank(self, c_NUMARGS(__VA_ARGS__)) #define cspan_reshape(self, ...) \ - memcpy((self)->dim, (uint32_t[]){__VA_ARGS__}, \ - sizeof((self)->dim) + cspan_assert(self, c_NUMARGS(__VA_ARGS__))) + (void)memcpy((self)->dim, (uint32_t[]){__VA_ARGS__}, \ + sizeof((self)->dim) + cspan_check_rank(self, c_NUMARGS(__VA_ARGS__))) + +#define cspan_at(self, ...) ((self)->data + cspan_index(self, __VA_ARGS__)) -#define cspan_at(self, ...) \ - ((self)->data + c_PASTE(_cspan_i, c_NUMARGS(__VA_ARGS__))((self)->dim, __VA_ARGS__) \ - + cspan_assert(self, c_NUMARGS(__VA_ARGS__))) +#define cspan_slice4(self, x0, width) \ + {cspan_at(self, x0, 0, 0, 0), {width, (self)->dim[1], (self)->dim[2], (self)->dim[3]}} +#define cspan_slice3(self, x0, width) \ + {cspan_at(self, x0, 0, 0), {width, (self)->dim[1], (self)->dim[2]}} +#define cspan_slice2(self, x0, width) \ + {cspan_at(self, x0, 0), {width, (self)->dim[1]}} +#define cspan_slice1(self, x0, width) \ + {cspan_at(self, x0), {width}} #define cspan_4to3(self, x) \ {cspan_at(self, x, 0, 0, 0), {(self)->dim[1], (self)->dim[2], (self)->dim[3]}} @@ -109,18 +131,18 @@ int demo2() { #define cspan_2to1(self, x) \ {cspan_at(self, x, 0), {(self)->dim[1]}} -STC_INLINE uint32_t _cspan_i1(const uint32_t dim[1], uint32_t x) - { assert(x < dim[0]); return x; } +STC_INLINE size_t _cspan_i1(const uint32_t dim[1], uint32_t x) + { c_ASSERT(x < dim[0]); return x; } -STC_INLINE uint32_t _cspan_i2(const uint32_t dim[2], uint32_t x, uint32_t y) - { assert(x < dim[0] && y < dim[1]); return dim[1]*x + y; } +STC_INLINE size_t _cspan_i2(const uint32_t dim[2], uint32_t x, uint32_t y) + { c_ASSERT(x < dim[0] && y < dim[1]); return dim[1]*x + y; } -STC_INLINE uint32_t _cspan_i3(const uint32_t dim[3], uint32_t x, uint32_t y, uint32_t z) { - assert(x < dim[0] && y < dim[1] && z < dim[2]); +STC_INLINE size_t _cspan_i3(const uint32_t dim[3], uint32_t x, uint32_t y, uint32_t z) { + c_ASSERT(x < dim[0] && y < dim[1] && z < dim[2]); return dim[2]*(dim[1]*x + y) + z; } -STC_INLINE uint32_t _cspan_i4(const uint32_t dim[4], uint32_t x, uint32_t y, uint32_t z, uint32_t w) { - assert(x < dim[0] && y < dim[1] && z < dim[3] && w < dim[3]); +STC_INLINE size_t _cspan_i4(const uint32_t dim[4], uint32_t x, uint32_t y, uint32_t z, uint32_t w) { + c_ASSERT(x < dim[0] && y < dim[1] && z < dim[3] && w < dim[3]); return dim[3]*(dim[2]*(dim[1]*x + y) + z) + w; } STC_INLINE size_t _cspan_size(const uint32_t dim[], unsigned rank) { |
