diff options
| author | Tyge Løvset <[email protected]> | 2023-01-14 00:05:33 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2023-01-14 00:08:57 +0100 |
| commit | c5e071c622d6f460aa1ad07b6543ceaaed5209fc (patch) | |
| tree | d5aad0ee32f47d57301cce8ded60226bd328c98b /include/stc | |
| parent | bb92cdbeaa279d8ef90dff0cf088585a50e7d52c (diff) | |
| download | STC-modified-c5e071c622d6f460aa1ad07b6543ceaaed5209fc.tar.gz STC-modified-c5e071c622d6f460aa1ad07b6543ceaaed5209fc.zip | |
Merged cmspan into cspan. cspan is now multi-dimensional array view (also 1-dimensional).
Diffstat (limited to 'include/stc')
| -rw-r--r-- | include/stc/algo/cmspan.h | 74 | ||||
| -rw-r--r-- | include/stc/algo/cspan.h | 104 | ||||
| -rw-r--r-- | include/stc/ccommon.h | 2 | ||||
| -rw-r--r-- | include/stc/cpque.h | 6 |
4 files changed, 82 insertions, 104 deletions
diff --git a/include/stc/algo/cmspan.h b/include/stc/algo/cmspan.h deleted file mode 100644 index 8edf956f..00000000 --- a/include/stc/algo/cmspan.h +++ /dev/null @@ -1,74 +0,0 @@ -/* -#include <stdio.h> -#include <stc/algo/cmspan.h> -using_cmspan(S3f, float, 3); - -int main() -{ - float raw[3*4*5]; - S3f span = cmspan_make(raw, 3, 4, 5); - *cmspan_at(&span, 2, 3, 4) = 100; - - printf("%f\n", *cmspan_at(&span, 2, 3, 4)); -} -*/ -#ifndef STC_CMSPAN_H_INCLUDED -#define STC_CMSPAN_H_INCLUDED - -#include <stc/ccommon.h> - -#define using_cmspan(Self, T, DIM) \ - typedef struct { T *data; uint32_t dim[DIM]; } Self; \ - typedef T Self##_raw, Self##_value; \ - typedef struct { Self##_value *ref, *end; } Self##_iter; \ - \ - STC_INLINE Self##_iter Self##_begin(const Self* self) { \ - Self##_iter it = {self->data, self->data + cmspan_size(self)}; \ - return it; \ - } \ - STC_INLINE Self##_iter Self##_end(const Self* self) { \ - Self##_iter it = {NULL, self->data + cmspan_size(self)}; \ - return it; \ - } \ - STC_INLINE void Self##_next(Self##_iter* it) \ - { if (++it->ref == it->end) it->ref = NULL; } \ - struct stc_nostruct - -#define cmspan_assert(self, rank) c_STATIC_ASSERT(cmspan_rank(self) == rank) - -#define cmspan_init() {NULL} -#define cmspan_make(data, ...) {data, {__VA_ARGS__}} - -#define cmspan_reshape(self, ...) \ - memcpy((self)->dim, (uint32_t[]){__VA_ARGS__}, \ - sizeof((self)->dim) + cmspan_assert(self, c_NUMARGS(__VA_ARGS__))) - -#define cmspan_at(self, ...) \ - ((self)->data + c_PASTE(_cmspan_i, c_NUMARGS(__VA_ARGS__))((self)->dim, __VA_ARGS__) \ - + cmspan_assert(self, c_NUMARGS(__VA_ARGS__))) - -#define cmspan_size(self) _cmspan_size((self)->dim, cmspan_rank(self)) -#define cmspan_rank(self) c_ARRAYLEN((self)->dim) - -STC_INLINE uint32_t _cmspan_i1(const uint32_t dim[1], uint32_t x) - { assert(x < dim[0]); return x; } - -STC_INLINE uint32_t _cmspan_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 uint32_t _cmspan_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]); - return dim[2]*(dim[1]*x + y) + z; -} -STC_INLINE uint32_t _cmspan_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]); - return dim[3]*(dim[2]*(dim[1]*x + y) + z) + w; -} - -STC_INLINE size_t _cmspan_size(const uint32_t dim[], unsigned rank) { - size_t sz = dim[0]; - while (rank --> 1) sz *= dim[rank]; - return sz; -} - -#endif diff --git a/include/stc/algo/cspan.h b/include/stc/algo/cspan.h index 23507fde..03cb8622 100644 --- a/include/stc/algo/cspan.h +++ b/include/stc/algo/cspan.h @@ -19,23 +19,33 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. -*/ -/* + */ +/* #include <stdio.h> #include <stc/algo/cspan.h> -using_cspan(IntSpan, int); -int main() -{ +using_cspan(Span3f, float, 3); + +int demo1() { + float raw[3*4*5]; + Span3f span = cspan_make(raw, 3, 4, 5); + *cspan_at(&span, 2, 3, 4) = 100; + + printf("%f\n", *cspan_at(&span, 2, 3, 4)); +} + +using_cspan(Intspan, int, 1); + +int demo2() { int array[] = {1, 2, 3, 4, 5}; - IntSpan span = cspan_from(array); + Intspan span = cspan_from(array); - c_FOREACH (i, IntSpan, span) + c_FOREACH (i, Intspan, span) printf(" %d", *i.ref); puts(""); // use a temporary IntSpan object. - c_FORFILTER (i, IntSpan, cspan_object(IntSpan, {10, 20, 30, 23, 22, 21}) + c_FORFILTER (i, Intspan, cspan_object(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 @@ -46,35 +56,77 @@ int main() #ifndef STC_CSPAN_H_INCLUDED #define STC_CSPAN_H_INCLUDED -#include <stc/ccommon.h> +#include "../ccommon.h" -#define cspan_object(C, ...) \ - ((C){.data = (C##_value[])__VA_ARGS__, \ - .size = sizeof((C##_value[])__VA_ARGS__)/sizeof(C##_value)}) - -#define cspan_from(data) \ - {data + c_STATIC_ASSERT(sizeof(data) != sizeof(void*)), c_ARRAYLEN(data)} -#define cspan_make(data, size) \ - {data + c_STATIC_ASSERT(c_ARRAYLEN(data) >= size || sizeof(data) == sizeof(void*)), size} -#define cspan_size(self) ((size_t)(self)->size) - -#define using_cspan(Self, T) \ - typedef T Self##_raw, Self##_value; \ - typedef struct { Self##_value *data; size_t size; } Self; \ +#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; \ \ - STC_INLINE Self##_value* Self##_at(const Self* self, size_t idx) \ - { assert(idx < self->size); return self->data + idx; } \ STC_INLINE Self##_iter Self##_begin(const Self* self) { \ - Self##_iter it = {self->data, self->data + self->size}; \ + Self##_iter it = {self->data, self->data + cspan_size(self)}; \ return it; \ } \ STC_INLINE Self##_iter Self##_end(const Self* self) { \ - Self##_iter it = {NULL, self->data + self->size}; \ + Self##_iter it = {NULL, self->data + cspan_size(self)}; \ return it; \ } \ STC_INLINE void Self##_next(Self##_iter* it) \ { if (++it->ref == it->end) it->ref = NULL; } \ struct stc_nostruct +#define cspan_assert(self, rank) c_STATIC_ASSERT(cspan_rank(self) == rank) + +#define cspan_object(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_size(self) _cspan_size((self)->dim, cspan_rank(self)) +#define cspan_rank(self) c_ARRAYLEN((self)->dim) + +#define cspan_reshape(self, ...) \ + memcpy((self)->dim, (uint32_t[]){__VA_ARGS__}, \ + sizeof((self)->dim) + cspan_assert(self, c_NUMARGS(__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_4to3(self, x) \ + {cspan_at(self, x, 0, 0, 0), {(self)->dim[1], (self)->dim[2], (self)->dim[3]}} +#define cspan_4to2(self, x, y) \ + {cspan_at(self, x, y, 0, 0), {(self)->dim[2], (self)->dim[3]}} +#define cspan_4to1(self, x, y, z) \ + {cspan_at(self, x, y, z, 0), {(self)->dim[3]}} +#define cspan_3to2(self, x) \ + {cspan_at(self, x, 0, 0), {(self)->dim[1], (self)->dim[2]}} +#define cspan_3to1(self, x, y) \ + {cspan_at(self, x, y, 0), {(self)->dim[2]}} +#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 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 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]); + 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]); + return dim[3]*(dim[2]*(dim[1]*x + y) + z) + w; +} +STC_INLINE size_t _cspan_size(const uint32_t dim[], unsigned rank) { + size_t sz = dim[0]; + while (rank --> 1) sz *= dim[rank]; + return sz; +} + #endif diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index aa22976f..a2c529a7 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -80,7 +80,7 @@ #define c_FREE(p) free(p) #endif -#define c_STATIC_ASSERT(b, ...) ((int)(0*sizeof(int[(b) ? 1 : -1]))) +#define c_STATIC_ASSERT(b) ((int)(0*sizeof(int[(b) ? 1 : -1]))) #define c_CONTAINER_OF(p, T, m) ((T*)((char*)(p) + 0*sizeof((p) == &((T*)0)->m) - offsetof(T, m))) #define c_DELETE(T, ptr) do { T *_tp = ptr; T##_drop(_tp); c_FREE(_tp); } while (0) #define c_SWAP(T, xp, yp) do { T *_xp = xp, *_yp = yp, \ diff --git a/include/stc/cpque.h b/include/stc/cpque.h index 74e0a5b7..28b5eb86 100644 --- a/include/stc/cpque.h +++ b/include/stc/cpque.h @@ -112,11 +112,11 @@ STC_INLINE void _cx_memb(_emplace)(_cx_self* self, _cx_raw raw) STC_DEF void _cx_memb(_sift_down_)(_cx_self* self, const size_t idx, const size_t n) { - _cx_value* arr = self->data - 1; + _cx_value t, *arr = self->data - 1; for (size_t r = idx, c = idx*2; c <= n; c *= 2) { - c += (c < n && (i_less_functor(self, (&arr[c]), (&arr[c + 1])))); + c += i_less_functor(self, (&arr[c]), (&arr[c + (c < n)])); if (!(i_less_functor(self, (&arr[r]), (&arr[c])))) return; - _cx_value t = arr[r]; arr[r] = arr[c]; arr[r = c] = t; + t = arr[r], arr[r] = arr[c], arr[r = c] = t; } } |
