From bd4df4343950959f1e2541e2fe27512de24df472 Mon Sep 17 00:00:00 2001 From: tylo Date: Fri, 17 Sep 2021 10:02:56 +0200 Subject: Added carr2.h and carr3.h (replaces carray.h) + test. --- include/stc/carr2.h | 131 ++++++++++++++++++++++++++++++ include/stc/carr3.h | 135 +++++++++++++++++++++++++++++++ include/stc/carray.h | 189 -------------------------------------------- include/stc/forward.h | 10 +-- include/stc/test_new_carr.c | 47 +++++++++++ 5 files changed, 318 insertions(+), 194 deletions(-) create mode 100644 include/stc/carr2.h create mode 100644 include/stc/carr3.h delete mode 100644 include/stc/carray.h create mode 100644 include/stc/test_new_carr.c diff --git a/include/stc/carr2.h b/include/stc/carr2.h new file mode 100644 index 00000000..e390469e --- /dev/null +++ b/include/stc/carr2.h @@ -0,0 +1,131 @@ +/* MIT License + * + * Copyright (c) 2021 Tyge Løvset, NORCE, www.norceresearch.no + * + * 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. + */ +#ifndef CARR2_H_INCLUDED +#define CARR2_H_INCLUDED +#include "ccommon.h" +#include "forward.h" +#include +#endif +/* +// carr2- 2D dynamic array in one memory block with easy indexing. +#define i_val int +#include +#include + +int main() { + int w = 7, h = 5; + c_forvar (carr2_int image = carr2_int_init(w, h), carr2_int_del(&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 "template.h" + +#ifndef i_fwd +cx_deftypes(_c_carr2_types, Self, i_val); +#endif + +STC_API Self cx_memb(_with_values)(size_t xdim, size_t ydim, i_val value); +STC_API Self cx_memb(_with_storage)(size_t xdim, size_t ydim, cx_value_t* storage); +STC_API Self cx_memb(_clone)(Self src); +STC_API cx_value_t* cx_memb(_release)(Self* self); +STC_API void cx_memb(_del)(Self* self); + +STC_INLINE Self cx_memb(_init)(size_t xdim, size_t ydim) { + return cx_memb(_with_storage)(xdim, ydim, c_new_n(cx_value_t, xdim*ydim)); +} +STC_INLINE size_t cx_memb(_size)(Self arr) + { return arr.xdim*arr.ydim; } + +STC_INLINE cx_value_t *cx_memb(_data)(Self* self) + { return *self->data; } + +STC_INLINE cx_value_t *cx_memb(_at)(Self* self, size_t x, size_t y) + { return *self->data + self->ydim*x + y; } + +STC_INLINE cx_iter_t cx_memb(_begin)(const Self* self) + { return c_make(cx_iter_t){*self->data}; } + +STC_INLINE cx_iter_t cx_memb(_end)(const Self* self) + { return c_make(cx_iter_t){*self->data + self->xdim*self->ydim}; } + +STC_INLINE void cx_memb(_next)(cx_iter_t* it) + { ++it->ref; } + +/* -------------------------- IMPLEMENTATION ------------------------- */ +#if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION) || defined(i_imp) + +STC_DEF Self cx_memb(_with_storage)(size_t xdim, size_t ydim, cx_value_t* block) { + Self _arr = {c_new_n(cx_value_t*, xdim), xdim, ydim}; + for (size_t x = 0; x < xdim; ++x, block += ydim) + _arr.data[x] = block; + return _arr; +} + +STC_DEF Self cx_memb(_with_values)(size_t xdim, size_t ydim, i_val value) { + Self _arr = cx_memb(_init)(xdim, ydim); + for (cx_value_t* p = _arr.data[0], *e = p + xdim*ydim; p != e; ++p) + *p = value; + return _arr; +} + +STC_DEF Self cx_memb(_clone)(Self src) { + Self _arr = cx_memb(_init)(src.xdim, src.ydim); + for (cx_value_t* p = _arr.data[0], *q = src.data[0], *e = p + cx_memb(_size)(src); p != e; ++p, ++q) + *p = i_valfrom(*q); + return _arr; +} + +STC_DEF cx_value_t *cx_memb(_release)(Self* self) { + cx_value_t *values = self->data[0]; + c_free(self->data); + self->data = NULL; + return values; +} + +STC_DEF void cx_memb(_del)(Self* self) { + if (!self->data) return; + for (cx_value_t* p = self->data[0], *e = p + cx_memb(_size)(*self); p != e; ++p) + i_valdel(p); + c_free(self->data[0]); /* values */ + c_free(self->data); /* pointers */ +} + +#endif +#include "template.h" diff --git a/include/stc/carr3.h b/include/stc/carr3.h new file mode 100644 index 00000000..5f32ef50 --- /dev/null +++ b/include/stc/carr3.h @@ -0,0 +1,135 @@ +/* MIT License + * + * Copyright (c) 2021 Tyge Løvset, NORCE, www.norceresearch.no + * + * 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. + */ +#ifndef CARR3_H_INCLUDED +#define CARR3_H_INCLUDED +#include "ccommon.h" +#include "forward.h" +#include +#endif +/* +// carr3 - 3D dynamic array in one memory block with easy indexing. +#define i_val int +#include +#include + +int main() { + int w = 7, h = 5, d = 3; + c_forvar (carr3_int image = carr_3_int_init(w, h, d), carr3_int_del(&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 "template.h" + +#ifndef i_fwd +cx_deftypes(_c_carr3_types, Self, i_val); +#endif + +STC_API Self cx_memb(_with_values)(size_t xdim, size_t ydim, size_t zdim, i_val value); +STC_API Self cx_memb(_with_storage)(size_t xdim, size_t ydim, size_t zdim, cx_value_t* storage); +STC_API Self cx_memb(_clone)(Self src); +STC_API cx_value_t* cx_memb(_release)(Self* self); +STC_API void cx_memb(_del)(Self* self); + +STC_INLINE Self cx_memb(_init)(size_t xdim, size_t ydim, size_t zdim) { + return cx_memb(_with_storage)(xdim, ydim, zdim, c_new_n(cx_value_t, xdim*ydim*zdim)); +} + +STC_INLINE size_t cx_memb(_size)(Self arr) + { return arr.xdim*arr.ydim*arr.zdim; } + +STC_INLINE cx_value_t* cx_memb(_data)(Self* self) + { return **self->data; } + +STC_INLINE cx_value_t* cx_memb(_at)(Self* self, size_t x, size_t y, size_t z) + { return **self->data + self->zdim*(self->ydim*x + y) + z; } + +STC_INLINE cx_iter_t cx_memb(_begin)(const Self* self) + { return c_make(cx_iter_t){**self->data}; } + +STC_INLINE cx_iter_t cx_memb(_end)(const Self* self) + { return c_make(cx_iter_t){**self->data + cx_memb(_size)(*self)}; } + +STC_INLINE void cx_memb(_next)(cx_iter_t* it) + { ++it->ref; } + +/* -------------------------- IMPLEMENTATION ------------------------- */ +#if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION) || defined(i_imp) + +STC_DEF Self cx_memb(_with_storage)(size_t xdim, size_t ydim, size_t zdim, cx_value_t* block) { + Self _arr = {c_new_n(cx_value_t**, xdim*(ydim + 1)), xdim, ydim, zdim}; + cx_value_t** p = (cx_value_t**) &_arr.data[xdim]; + for (size_t x = 0, y; x < xdim; ++x, p += ydim) + for (_arr.data[x] = p, y = 0; y < ydim; ++y, block += zdim) + _arr.data[x][y] = block; + return _arr; +} + +STC_DEF Self cx_memb(_with_values)(size_t xdim, size_t ydim, size_t zdim, i_val value) { + Self _arr = cx_memb(_init)(xdim, ydim, zdim); + for (cx_value_t* p = **_arr.data, *e = p + xdim*ydim*zdim; p != e; ++p) + *p = value; + return _arr; +} + +STC_DEF Self cx_memb(_clone)(Self src) { + Self _arr = cx_memb(_init)(src.xdim, src.ydim, src.zdim); + for (cx_value_t* p = **_arr.data, *q = **src.data, *e = p + cx_memb(_size)(src); p != e; ++p, ++q) + *p = i_valfrom(*q); + return _arr; +} + +STC_DEF cx_value_t* cx_memb(_release)(Self* self) { + cx_value_t *values = self->data[0][0]; + c_free(self->data); + self->data = NULL; + return values; +} + +STC_DEF void cx_memb(_del)(Self* self) { + if (!self->data) return; + for (cx_value_t* p = **self->data, *e = p + cx_memb(_size)(*self); p != e; ++p) + i_valdel(p); + c_free(self->data[0][0]); /* data */ + c_free(self->data); /* pointers */ +} + +#endif +#include "template.h" diff --git a/include/stc/carray.h b/include/stc/carray.h deleted file mode 100644 index e70a4b64..00000000 --- a/include/stc/carray.h +++ /dev/null @@ -1,189 +0,0 @@ -/* MIT License - * - * Copyright (c) 2021 Tyge Løvset, NORCE, www.norceresearch.no - * - * 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. - */ -#ifndef CARRAY_H_INCLUDED -#define CARRAY_H_INCLUDED - -#include "ccommon.h" -#include -/* -// carray2 and carray3 - 2D and 3D dynamic arrays in one memory block with easy indexing. -#include -#include -using_carray2(i, int); - -int main() { - int w = 7, h = 5; - c_forvar (carray2i image = carray2i_init(w, h), carray2i_del(&image)) - { - int *dat = carray2i_data(&image); - for (int i = 0; i < carray2i_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, carray2i, image) - printf(" %d", *i.ref); - puts(""); - } -} -*/ - defTypes( _c_carray2_types(Self, i_val); ) -\ - STC_API Self cx_memb(_with_values)(size_t xdim, size_t ydim, i_val value); \ - STC_API Self cx_memb(_with_storage)(size_t xdim, size_t ydim, cx_value_t* storage); \ - STC_API Self cx_memb(_clone)(Self src); \ -\ - STC_INLINE Self cx_memb(_init)(size_t xdim, size_t ydim) { \ - return cx_memb(_with_storage)(xdim, ydim, c_new_n(cx_value_t, xdim*ydim)); \ - } \ - STC_INLINE size_t cx_memb(_size)(Self arr) { return arr.xdim*arr.ydim; } \ - STC_INLINE cx_value_t *cx_memb(_data)(Self* self) { return *self->data; } \ - STC_INLINE cx_value_t *cx_memb(_at)(Self* self, size_t x, size_t y) { \ - return *self->data + self->ydim*x + y; \ - } \ - STC_INLINE cx_value_t *cx_memb(_release)(Self* self) { \ - cx_value_t *t = *self->data; c_free(self->data); self->data = NULL; return t; \ - } \ -\ - STC_INLINE cx_iter_t cx_memb(_begin)(const Self* self) { \ - return c_make(cx_iter_t){*self->data}; \ - } \ - STC_INLINE cx_iter_t cx_memb(_end)(const Self* self) { \ - return c_make(cx_iter_t){*self->data + self->xdim*self->ydim}; \ - } \ - STC_INLINE void cx_memb(_next)(cx_iter_t* it) { ++it->ref; } \ -\ - _c_implement_carray2(Self, i_val, i_valdel, i_valfrom) \ - STC_API void cx_memb(_del)(Self* self) - -// carray3: - - - defTypes( _c_carray3_types(Self, i_val); ) -\ - STC_API Self cx_memb(_with_values)(size_t xdim, size_t ydim, size_t zdim, i_val value); \ - STC_API Self cx_memb(_with_storage)(size_t xdim, size_t ydim, size_t zdim, cx_value_t* storage); \ - STC_API Self cx_memb(_clone)(Self src); \ -\ - STC_INLINE Self cx_memb(_init)(size_t xdim, size_t ydim, size_t zdim) { \ - return cx_memb(_with_storage)(xdim, ydim, zdim, c_new_n(cx_value_t, xdim*ydim*zdim)); \ - } \ - STC_INLINE size_t cx_memb(_size)(Self arr) { return arr.xdim*arr.ydim*arr.zdim; } \ - STC_INLINE cx_value_t* cx_memb(_data)(Self* self) { return **self->data; } \ - STC_INLINE cx_value_t* cx_memb(_at)(Self* self, size_t x, size_t y, size_t z) { \ - return **self->data + self->zdim*(self->ydim*x + y) + z; \ - } \ -\ - STC_INLINE cx_value_t* cx_memb(_release)(Self* self) { \ - cx_value_t *values = **self->data; \ - c_free(self->data); self->data = NULL; \ - return values; \ - } \ -\ - STC_INLINE cx_iter_t cx_memb(_begin)(const Self* self) { \ - return c_make(cx_iter_t){**self->data}; \ - } \ - STC_INLINE cx_iter_t cx_memb(_end)(const Self* self) { \ - return c_make(cx_iter_t){**self->data + cx_memb(_size)(*self)}; \ - } \ - STC_INLINE void cx_memb(_next)(cx_iter_t* it) { ++it->ref; } \ -\ - _c_implement_carray3(Self, i_val, i_valdel, i_valfrom) \ - STC_API void cx_memb(_del)(Self* self) - -/* -------------------------- IMPLEMENTATION ------------------------- */ - -#if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION) - -#define _c_implement_carray2(Self, i_val, i_valdel, i_valfrom) \ -\ - STC_DEF Self cx_memb(_with_storage)(size_t xdim, size_t ydim, cx_value_t* block) { \ - Self _arr = {c_new_n(cx_value_t*, xdim), xdim, ydim}; \ - for (size_t x = 0; x < xdim; ++x, block += ydim) \ - _arr.data[x] = block; \ - return _arr; \ - } \ -\ - STC_DEF Self cx_memb(_with_values)(size_t xdim, size_t ydim, i_val value) { \ - Self _arr = cx_memb(_init)(xdim, ydim); \ - for (cx_value_t* p = _arr.data[0], *e = p + xdim*ydim; p != e; ++p) \ - *p = value; \ - return _arr; \ - } \ -\ - STC_DEF Self cx_memb(_clone)(Self src) { \ - Self _arr = cx_memb(_init)(src.xdim, src.ydim); \ - for (cx_value_t* p = _arr.data[0], *q = src.data[0], *e = p + cx_memb(_size)(src); p != e; ++p, ++q) \ - *p = i_valfrom(*q); \ - return _arr; \ - } \ -\ - STC_DEF void cx_memb(_del)(Self* self) { \ - if (!self->data) return; \ - for (cx_value_t* p = self->data[0], *e = p + cx_memb(_size)(*self); p != e; ++p) \ - i_valdel(p); \ - c_free(self->data[0]); /* data */ \ - c_free(self->data); /* pointers */ \ - } - -// carray3 impl. - -#define _c_implement_carray3(Self, i_val, i_valdel, i_valfrom) \ -\ - STC_DEF Self cx_memb(_with_storage)(size_t xdim, size_t ydim, size_t zdim, cx_value_t* block) { \ - Self _arr = {c_new_n(cx_value_t**, xdim*(ydim + 1)), xdim, ydim, zdim}; \ - cx_value_t** p = (cx_value_t**) &_arr.data[xdim]; \ - for (size_t x = 0, y; x < xdim; ++x, p += ydim) \ - for (_arr.data[x] = p, y = 0; y < ydim; ++y, block += zdim) \ - _arr.data[x][y] = block; \ - return _arr; \ - } \ -\ - STC_DEF Self cx_memb(_with_values)(size_t xdim, size_t ydim, size_t zdim, i_val value) { \ - Self _arr = cx_memb(_init)(xdim, ydim, zdim); \ - for (cx_value_t* p = **_arr.data, *e = p + xdim*ydim*zdim; p != e; ++p) \ - *p = value; \ - return _arr; \ - } \ -\ - STC_DEF Self cx_memb(_clone)(Self src) { \ - Self _arr = cx_memb(_init)(src.xdim, src.ydim, src.zdim); \ - for (cx_value_t* p = **_arr.data, *q = **src.data, *e = p + cx_memb(_size)(src); p != e; ++p, ++q) \ - *p = i_valfrom(*q); \ - return _arr; \ - } \ -\ - STC_DEF void cx_memb(_del)(Self* self) { \ - if (!self->data) return; \ - for (cx_value_t* p = **self->data, *e = p + cx_memb(_size)(*self); p != e; ++p) \ - i_valdel(p); \ - c_free(self->data[0][0]); /* data */ \ - c_free(self->data); /* pointers */ \ - } - -#endif - -#endif diff --git a/include/stc/forward.h b/include/stc/forward.h index d524d088..70c21440 100644 --- a/include/stc/forward.h +++ b/include/stc/forward.h @@ -25,8 +25,8 @@ #include -#define forward_carray2(TAG, VAL) _c_carray2_types(carray2##TAG, VAL) -#define forward_carray3(TAG, VAL) _c_carray3_types(carray2##TAG, VAL) +#define forward_carr2(TAG, VAL) _c_carr2_types(carr2_##TAG, VAL) +#define forward_carr3(TAG, VAL) _c_carr3_types(carr3_##TAG, VAL) #define forward_cdeq(TAG, VAL) _c_cdeq_types(cdeq_##TAG, VAL) #define forward_clist(TAG, VAL) _c_clist_types(clist_##TAG, VAL) #define forward_cmap(TAG, KEY, VAL) _c_chash_types(cmap_##TAG, KEY, VAL, c_true, c_false) @@ -45,12 +45,12 @@ #define c_true(...) __VA_ARGS__ #define c_false(...) -#define _c_carray2_types(SELF, VAL) \ +#define _c_carr2_types(SELF, VAL) \ typedef VAL SELF##_value_t; \ typedef struct { SELF##_value_t *ref; } SELF##_iter_t; \ typedef struct { SELF##_value_t **data; size_t xdim, ydim; } SELF -#define _c_carray3_types(SELF, VAL) \ +#define _c_carr3_types(SELF, VAL) \ typedef VAL SELF##_value_t; \ typedef struct { SELF##_value_t *ref; } SELF##_iter_t; \ typedef struct { SELF##_value_t ***data; size_t xdim, ydim, zdim; } SELF @@ -153,4 +153,4 @@ typedef struct { SELF##_value_t *ref; } SELF##_iter_t; \ typedef struct { SELF##_value_t *data; } SELF -#endif // STC_FORWARD_H_INCLUDED \ No newline at end of file +#endif // STC_FORWARD_H_INCLUDED diff --git a/include/stc/test_new_carr.c b/include/stc/test_new_carr.c new file mode 100644 index 00000000..7513903f --- /dev/null +++ b/include/stc/test_new_carr.c @@ -0,0 +1,47 @@ +#include + +#define i_tag i +#define i_val int +#include + +#define i_val int +#include +#include + +int main() +{ + int w = 7, h = 5, d = 3; + + c_forvar (carr2_i image = carr2_i_init(w, h), carr2_i_del(&image)) + { + int *dat = carr2_i_data(&image); + for (int i = 0; i < carr2_i_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(""); + + c_foreach (i, carr2_i, image) + printf(" %d", *i.ref); + } + puts("\n"); + + c_forvar (carr3_int image = carr3_int_init(w, h, d), carr3_int_del(&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(""); + + c_foreach (i, carr3_int, image) + printf(" %d", *i.ref); + } + puts(""); +} -- cgit v1.2.3