From 27a6860137499e71eb23c1930ebe6e992baece12 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Wed, 3 Nov 2021 13:11:56 +0100 Subject: Changed carrN_X_at() to return const value*. Added mutable carrN_X_elem(). --- docs/carray_api.md | 10 ++++++---- examples/complex.c | 8 ++++---- include/stc/carr2.h | 5 ++++- include/stc/carr3.h | 5 ++++- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/docs/carray_api.md b/docs/carray_api.md index a6870b91..65c4588f 100644 --- a/docs/carray_api.md +++ b/docs/carray_api.md @@ -33,7 +33,8 @@ void carr2_X_del(carr2_X* self); size_t carr2_X_size(carr2_X arr); i_val* carr2_X_data(carr2_X* self); // access storage data -i_val* carr2_X_at(carr2_X* self, size_t x, size_t y); +i_val* carr2_X_elem(carr2_X* self, size_t x, size_t y); +const i_val* carr2_X_at(const carr2_X* self, size_t x, size_t y); carr2_X_iter carr2_X_begin(const carr2_X* self); carr2_X_iter carr2_X_end(const carr2_X* self); @@ -46,12 +47,13 @@ carr3_X carr3_X_with_values(size_t xdim, size_t ydim, size_t zdim, i carr3_X carr3_X_with_storage(size_t xdim, size_t ydim, size_t zdim, i_val* array); carr3_X carr3_X_clone(carr3_X arr); void carr3_X_copy(carr3_X* self, carr3_X other); -i_val* carr3_X_release(carr3_X* self); // release storage (not freed) +i_val* carr3_X_release(carr3_X* self); // release storage (not freed) void carr3_X_del(carr3_X* self); size_t carr3_X_size(carr3_X arr); -i_val* carr3_X_data(carr3_X* self); // access storage data -i_val* carr3_X_at(carr3_X* self, size_t x, size_t y, size_t z); +i_val* carr3_X_data(carr3_X* self); // storage data +i_val* carr3_X_elem(carr3_X* self, size_t x, size_t y, size_t z); +const i_val* carr3_X_at(const carr3_X* self, size_t x, size_t y, size_t z); carr3_X_iter carr3_X_begin(const carr3_X* self); carr3_X_iter carr3_X_end(const carr3_X* self); diff --git a/examples/complex.c b/examples/complex.c index 69900bba..32dd82eb 100644 --- a/examples/complex.c +++ b/examples/complex.c @@ -53,10 +53,10 @@ int main() { cmap_map_insert(&myMap, cstr_from(strKey), listMap); // Access the data entry - cmap_lst* mapL = &cmap_map_find(&myMap, strKey).ref->second; - clist_arr* lstA = &cmap_lst_find(mapL, tableKey).ref->second; - cstack_f arr = *clist_arr_back(lstA); - printf("value (%d) is: %f\n", x, arr.data[x]); + const cmap_lst* mapL = cmap_map_at(&myMap, strKey); + const clist_arr* lstA = cmap_lst_at(mapL, tableKey); + const cstack_f* arr = clist_arr_back(lstA); + printf("value (%d) is: %f\n", x, *cstack_f_at(arr, x)); stk.data[x] = 1.41421356f; // change the value in array } diff --git a/include/stc/carr2.h b/include/stc/carr2.h index 3f9d1540..a1dfc3ce 100644 --- a/include/stc/carr2.h +++ b/include/stc/carr2.h @@ -76,7 +76,10 @@ STC_INLINE size_t _cx_memb(_size)(_cx_self arr) STC_INLINE _cx_value *_cx_memb(_data)(_cx_self* self) { return *self->data; } -STC_INLINE _cx_value *_cx_memb(_at)(_cx_self* self, size_t x, size_t y) +STC_INLINE _cx_value *_cx_memb(_elem)(_cx_self* self, size_t x, size_t y) + { return *self->data + self->ydim*x + y; } + +STC_INLINE const _cx_value *_cx_memb(_at)(const _cx_self* self, size_t x, size_t y) { return *self->data + self->ydim*x + y; } STC_INLINE void _cx_memb(_copy)(_cx_self *self, _cx_self other) { diff --git a/include/stc/carr3.h b/include/stc/carr3.h index 1133dcc3..4d4fc84e 100644 --- a/include/stc/carr3.h +++ b/include/stc/carr3.h @@ -78,7 +78,10 @@ STC_INLINE size_t _cx_memb(_size)(_cx_self arr) STC_INLINE _cx_value* _cx_memb(_data)(_cx_self* self) { return **self->data; } -STC_INLINE _cx_value* _cx_memb(_at)(_cx_self* self, size_t x, size_t y, size_t z) +STC_INLINE _cx_value* _cx_memb(_elem)(_cx_self* self, size_t x, size_t y, size_t z) + { return **self->data + self->zdim*(self->ydim*x + y) + z; } + +STC_INLINE const _cx_value* _cx_memb(_at)(const _cx_self* self, size_t x, size_t y, size_t z) { return **self->data + self->zdim*(self->ydim*x + y) + z; } STC_INLINE void _cx_memb(_copy)(_cx_self *self, _cx_self other) { -- cgit v1.2.3