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 /misc | |
| 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 'misc')
| -rw-r--r-- | misc/archived/carr2.h | 152 | ||||
| -rw-r--r-- | misc/archived/carr3.h | 157 | ||||
| -rw-r--r-- | misc/examples/forfilter.c | 2 | ||||
| -rw-r--r-- | misc/examples/multidim.c | 18 | ||||
| -rw-r--r-- | misc/examples/prime.c | 2 |
5 files changed, 14 insertions, 317 deletions
diff --git a/misc/archived/carr2.h b/misc/archived/carr2.h deleted file mode 100644 index c400b6d3..00000000 --- a/misc/archived/carr2.h +++ /dev/null @@ -1,152 +0,0 @@ -/* MIT License - * - * Copyright (c) 2023 Tyge Løvset - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * 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 <stc/ccommon.h> - -#ifndef CARR2_H_INCLUDED -#define CARR2_H_INCLUDED -#include <stc/forward.h> -#include <stdlib.h> -#endif -/* -// carr2- 2D dynamic array in one memory block with easy indexing. -#define i_key int -#include <stc/carr2.h> -#include <stdio.h> - -int main() { - int w = 7, h = 5; - c_WITH (carr2_int image = carr2_int_new_uninit(w, h), carr2_int_drop(&image)) - { - int *dat = carr2_int_data(&image); - for (int i = 0; i < carr2_int_size(&image); ++i) - dat[i] = i; - - for (int x = 0; x < image.xdim; ++x) - for (int y = 0; y < image.ydim; ++y) - printf(" %d", image.data[x][y]); - puts("\n"); - - c_FOREACH (i, carr2_int, image) - printf(" %d", *i.ref); - puts(""); - } -} -*/ - -#ifndef _i_prefix -#define _i_prefix carr2_ -#endif -#include <stc/priv/template.h> -#if !c_option(c_is_forward) -_cx_deftypes(_c_carr2_types, _cx_self, i_key); -#endif - -STC_API _cx_self _cx_memb(_with_size)(size_t xdim, size_t ydim, i_key null); -STC_API _cx_self _cx_memb(_with_data)(size_t xdim, size_t ydim, _cx_value* storage); -STC_API _cx_value* _cx_memb(_release)(_cx_self* self); -STC_API void _cx_memb(_drop)(_cx_self* self); -#if !defined i_no_clone -STC_API _cx_self _cx_memb(_clone)(_cx_self src); -STC_API void _cx_memb(_copy)(_cx_self *self, const _cx_self* other); -#endif - -STC_INLINE _cx_self _cx_memb(_new_uninit)(size_t xdim, size_t ydim) { - return _cx_memb(_with_data)(xdim, ydim, c_ALLOC_N(_cx_value, xdim*ydim)); -} -STC_INLINE size_t _cx_memb(_size)(const _cx_self* self) - { return self->xdim*self->ydim; } - -STC_INLINE _cx_value *_cx_memb(_data)(_cx_self* self) - { return *self->data; } - -STC_INLINE const _cx_value *_cx_memb(_at)(const _cx_self* self, size_t x, size_t y) { - assert(x < self->xdim && y < self->ydim); - return *self->data + self->ydim*x + y; -} - -STC_INLINE size_t _cx_memb(_idx)(const _cx_self* self, size_t x, size_t y) { - return self->ydim*x + y; -} - - -STC_INLINE _cx_iter _cx_memb(_begin)(const _cx_self* self) { - size_t n = self->xdim*self->ydim; - return c_INIT(_cx_iter){n ? *self->data : NULL, *self->data + n}; -} - -STC_INLINE _cx_iter _cx_memb(_end)(const _cx_self* self) - { return c_INIT(_cx_iter){NULL, *self->data + self->xdim*self->ydim}; } - -STC_INLINE void _cx_memb(_next)(_cx_iter* it) - { if (++it->ref == it->end) it->ref = NULL; } - -/* -------------------------- IMPLEMENTATION ------------------------- */ -#if defined(i_implement) - -STC_DEF _cx_self _cx_memb(_with_data)(size_t xdim, size_t ydim, _cx_value* block) { - _cx_self _arr = {c_ALLOC_N(_cx_value*, xdim), xdim, ydim}; - for (size_t x = 0; x < xdim; ++x, block += ydim) - _arr.data[x] = block; - return _arr; -} - -STC_DEF _cx_self _cx_memb(_with_size)(size_t xdim, size_t ydim, i_key null) { - _cx_self _arr = _cx_memb(_new_uninit)(xdim, ydim); - for (_cx_value* p = _arr.data[0], *e = p + xdim*ydim; p != e; ++p) - *p = null; - return _arr; -} - -#if !defined i_no_clone - -STC_DEF _cx_self _cx_memb(_clone)(_cx_self src) { - _cx_self _arr = _cx_memb(_new_uninit)(src.xdim, src.ydim); - for (_cx_value* p = _arr.data[0], *q = src.data[0], *e = p + _cx_memb(_size)(&src); p != e; ++p, ++q) - *p = i_keyclone((*q)); - return _arr; -} - -STC_DEF void _cx_memb(_copy)(_cx_self *self, const _cx_self* other) { - if (self->data == other->data) return; - _cx_memb(_drop)(self); *self = _cx_memb(_clone)(*other); -} -#endif - -STC_DEF _cx_value *_cx_memb(_release)(_cx_self* self) { - _cx_value *values = self->data[0]; - c_FREE(self->data); - self->data = NULL; - return values; -} - -STC_DEF void _cx_memb(_drop)(_cx_self* self) { - if (!self->data) return; - for (_cx_value* p = self->data[0], *q = p + _cx_memb(_size)(self); p != q; ) { - --q; i_keydrop(q); - } - c_FREE(self->data[0]); /* values */ - c_FREE(self->data); /* pointers */ -} - -#endif -#include <stc/priv/template.h> diff --git a/misc/archived/carr3.h b/misc/archived/carr3.h deleted file mode 100644 index 31ed2d9a..00000000 --- a/misc/archived/carr3.h +++ /dev/null @@ -1,157 +0,0 @@ -/* MIT License - * - * Copyright (c) 2023 Tyge Løvset - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * 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 <stc/ccommon.h> - -#ifndef CARR3_H_INCLUDED -#define CARR3_H_INCLUDED -#include <stc/forward.h> -#include <stdlib.h> -#endif -/* -// carr3 - 3D dynamic array in one memory block with easy indexing. -#define i_key int -#include <stc/carr3.h> -#include <stdio.h> - -int main() { - int w = 7, h = 5, d = 3; - c_WITH (carr3_int image = carr3_int_new_uninit(w, h, d), carr3_int_drop(&image)) - { - int *dat = carr3_int_data(&image); - for (int i = 0; i < carr3_int_size(&image); ++i) - dat[i] = i; - - for (int x = 0; x < image.xdim; ++x) - for (int y = 0; y < image.ydim; ++y) - for (int z = 0; z < image.zdim; ++z) - printf(" %d", image.data[x][y][z]); - puts("\n"); - - c_FOREACH (i, carr3_int, image) - printf(" %d", *i.ref); - puts(""); - } -} -*/ - -#ifndef _i_prefix -#define _i_prefix carr3_ -#endif -#include <stc/priv/template.h> - -#if !c_option(c_is_forward) -_cx_deftypes(_c_carr3_types, _cx_self, i_key); -#endif - -STC_API _cx_self _cx_memb(_with_size)(size_t xdim, size_t ydim, size_t zdim, i_key null); -STC_API _cx_self _cx_memb(_with_data)(size_t xdim, size_t ydim, size_t zdim, _cx_value* storage); -STC_API _cx_value* _cx_memb(_release)(_cx_self* self); -STC_API void _cx_memb(_drop)(_cx_self* self); -#if !defined i_no_clone -STC_API _cx_self _cx_memb(_clone)(_cx_self src); -STC_API void _cx_memb(_copy)(_cx_self *self, const _cx_self* other); -#endif - -STC_INLINE _cx_self _cx_memb(_new_uninit)(size_t xdim, size_t ydim, size_t zdim) { - return _cx_memb(_with_data)(xdim, ydim, zdim, c_ALLOC_N(_cx_value, xdim*ydim*zdim)); -} - -STC_INLINE size_t _cx_memb(_size)(const _cx_self* self) - { return self->xdim*self->ydim*self->zdim; } - -STC_INLINE _cx_value* _cx_memb(_data)(_cx_self* self) - { return **self->data; } - -STC_INLINE const _cx_value* _cx_memb(_at)(const _cx_self* self, size_t x, size_t y, size_t z) { - assert(x < self->xdim && y < self->ydim && z < self->zdim); - return **self->data + self->zdim*(self->ydim*x + y) + z; -} - -STC_INLINE size_t _cx_memb(_idx)(const _cx_self* self, size_t x, size_t y, size_t z) { - return self->zdim*(self->ydim*x + y) + z; -} - - -STC_INLINE _cx_iter _cx_memb(_begin)(const _cx_self* self) { - size_t n = _cx_memb(_size)(self); - return c_INIT(_cx_iter){n ? **self->data : NULL, **self->data + n}; -} - -STC_INLINE _cx_iter _cx_memb(_end)(const _cx_self* self) - { return c_INIT(_cx_iter){NULL, **self->data + _cx_memb(_size)(self)}; } - -STC_INLINE void _cx_memb(_next)(_cx_iter* it) - { if (++it->ref == it->end) it->ref = NULL; } - -/* -------------------------- IMPLEMENTATION ------------------------- */ -#if defined(i_implement) - -STC_DEF _cx_self _cx_memb(_with_data)(size_t xdim, size_t ydim, size_t zdim, _cx_value* block) { - _cx_self _arr = {c_ALLOC_N(_cx_value**, xdim*(ydim + 1)), xdim, ydim, zdim}; - _cx_value** p = (_cx_value**) &_arr.data[xdim]; - for (size_t x = 0, y; x < xdim; ++x, p += ydim) - for (y = 0, _arr.data[x] = p; y < ydim; ++y, block += zdim) - p[y] = block; - return _arr; -} - -STC_DEF _cx_self _cx_memb(_with_size)(size_t xdim, size_t ydim, size_t zdim, i_key null) { - _cx_self _arr = _cx_memb(_new_uninit)(xdim, ydim, zdim); - for (_cx_value* p = **_arr.data, *e = p + xdim*ydim*zdim; p != e; ++p) - *p = null; - return _arr; -} - -#if !defined i_no_clone - -STC_DEF _cx_self _cx_memb(_clone)(_cx_self src) { - _cx_self _arr = _cx_memb(_new_uninit)(src.xdim, src.ydim, src.zdim); - for (_cx_value* p = **_arr.data, *q = **src.data, *e = p + _cx_memb(_size)(&src); p != e; ++p, ++q) - *p = i_keyclone((*q)); - return _arr; -} - -STC_DEF void _cx_memb(_copy)(_cx_self *self, const _cx_self* other) { - if (self->data == other->data) return; - _cx_memb(_drop)(self); *self = _cx_memb(_clone)(*other); -} -#endif - -STC_DEF _cx_value* _cx_memb(_release)(_cx_self* self) { - _cx_value *values = self->data[0][0]; - c_FREE(self->data); - self->data = NULL; - return values; -} - -STC_DEF void _cx_memb(_drop)(_cx_self* self) { - if (!self->data) return; - for (_cx_value* p = **self->data, *q = p + _cx_memb(_size)(self); p != q; ) { - --q; i_keydrop(q); - } - c_FREE(self->data[0][0]); /* data */ - c_FREE(self->data); /* pointers */ -} - -#endif -#include <stc/priv/template.h> diff --git a/misc/examples/forfilter.c b/misc/examples/forfilter.c index bd3d346d..05d0bc28 100644 --- a/misc/examples/forfilter.c +++ b/misc/examples/forfilter.c @@ -65,7 +65,7 @@ void demo2(void) c_AUTO (IVec, vector) { puts("demo2:"); - c_FORFILTER (x, crange, crange_object(INT64_MAX) + c_FORFILTER (x, crange, crange_literal(INT64_MAX) , c_FLT_SKIPWHILE(x, *x.ref != 11) && *x.ref % 2 != 0 , c_FLT_TAKE(x, 5)) diff --git a/misc/examples/multidim.c b/misc/examples/multidim.c index 2cf16b7f..99310847 100644 --- a/misc/examples/multidim.c +++ b/misc/examples/multidim.c @@ -1,13 +1,15 @@ -// Example from https://en.cppreference.com/w/cpp/container/mdspan +// Example based on https://en.cppreference.com/w/cpp/container/mdspan #define i_val int #include <stc/cstack.h> -#include <stc/algo/cspan.h> +#include <stc/cspan.h> #include <stdio.h> +using_cspan3(ispan, int); +/* using_cspan(ispan1, int, 1); using_cspan(ispan2, int, 2); using_cspan(ispan3, int, 3); - +*/ int main() { @@ -16,7 +18,7 @@ int main() cstack_int_push(&v, *i.ref); // View data as contiguous memory representing 12 ints - ispan1 ms1 = cspan_make(v.data, 12); + ispan1 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 @@ -27,12 +29,16 @@ int main() for (unsigned j=0; j != ms2.dim[1]; j++) *cspan_at(&ms2, i, j) = i*1000 + j; - // print data using 1D view + // print all items using 1D view + printf("all: "); for (unsigned i=0; i != ms1.dim[0]; i++) printf(" %d", *cspan_at(&ms1, i)); puts(""); - c_FOREACH (i, ispan1, ms1) + // or iterate a subspan... + ispan2 sub = cspan_3to2(&ms3, 1); + printf("sub: "); + c_FOREACH (i, ispan2, sub) printf(" %d", *i.ref); puts(""); diff --git a/misc/examples/prime.c b/misc/examples/prime.c index b4d81868..40ccc299 100644 --- a/misc/examples/prime.c +++ b/misc/examples/prime.c @@ -43,7 +43,7 @@ int main(void) puts(""); puts("Show the last 50 primes using a temporary crange generator:"); - c_FORFILTER (i, crange, crange_object(n - 1, 0, -2) + c_FORFILTER (i, crange, crange_literal(n - 1, 0, -2) , cbits_test(&primes, *i.ref>>1) , c_FLT_TAKE(i, 50)) { printf("%lld ", *i.ref); |
