From d1f2b62f66b5fed1e2550dbd8f8644164cf1df26 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Sun, 1 Jul 2018 22:35:05 +0200 Subject: [PATCH] Replace _set_at2 by _get_at for array. (Break API) --- doc/API.txt | 5 +++++ example/ex-multi01.c | 4 ++-- m-array.h | 13 +++++++------ tests/test-marray.c | 2 +- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/doc/API.txt b/doc/API.txt index 213f60c3..1dea4f45 100644 --- a/doc/API.txt +++ b/doc/API.txt @@ -8,3 +8,8 @@ V0.2.*: * worker_sync takes a 'worker_t' as an additional parameter * worker_init takes an int as numWorker instead of an unsingned int. * RBTREE pop method has been renamed into _pop_at * SNAPSHOT_DEF has been removed. use SNAPSHOT_SPSC_DEF + * array's _set_at2 has been replaced by array's _get_at. + The interface is similar to dict _get_at, making the API cleaner + and more generic. + _set_at2(a,i,x) ==> set(*_get_at(a,i),x) + diff --git a/example/ex-multi01.c b/example/ex-multi01.c index c98149f5..f1deeaa7 100644 --- a/example/ex-multi01.c +++ b/example/ex-multi01.c @@ -47,8 +47,8 @@ static void program_init(program_t p, string_t text) { int left; array_int_pop_back(&left, leftstack); int right = pc; - array_int_set_at2 (p->bracket_map, left, right); - array_int_set_at2 (p->bracket_map, right, left); + *array_int_get_at (p->bracket_map, left) = right; + *array_int_get_at (p->bracket_map, right) = left; } pc++; diff --git a/m-array.h b/m-array.h index 3f0d62d6..24728f31 100644 --- a/m-array.h +++ b/m-array.h @@ -353,24 +353,25 @@ ARRAYI_CONTRACT(v); \ } \ \ - static inline void \ - M_C(name, _set_at2)(array_t v, size_t idx, type x) \ + static inline type * \ + M_C(name, _get_at)(array_t v, size_t idx) \ { \ ARRAYI_CONTRACT(v); \ const size_t size = idx + 1; \ - /* NOTE: set at and resize if needed */ \ + /* resize if needed */ \ if (v->size <= size) { \ /* Increase size of array */ \ if (M_UNLIKELY (size > v->alloc) ) { \ size_t alloc = M_GET_INC_ALLOC oplist (size) ; \ + /* In case of overflow of size_t */ \ if (M_UNLIKELY (alloc <= v->alloc)) { \ M_MEMORY_FULL(sizeof (type) * alloc); \ - return ; \ + return NULL; \ } \ type *ptr = M_GET_REALLOC oplist (type, v->ptr, alloc); \ if (M_UNLIKELY (ptr == NULL) ) { \ M_MEMORY_FULL(sizeof (type) * alloc); \ - return; \ + return NULL; \ } \ v->ptr = ptr; \ v->alloc = alloc; \ @@ -380,8 +381,8 @@ v->size = size; \ } \ assert (idx < v->size); \ - M_GET_SET oplist(v->ptr[idx], x); \ ARRAYI_CONTRACT(v); \ + return &v->ptr[idx]; \ } \ \ static inline void \ diff --git a/tests/test-marray.c b/tests/test-marray.c index 855a7532..31e5897c 100644 --- a/tests/test-marray.c +++ b/tests/test-marray.c @@ -281,7 +281,7 @@ static void test_d(void) for(int i = 9; i < 19; i++) assert (*array_uint_get (a1, i) == 18U-i); - array_uint_set_at2(a1, 100, 100); + *array_uint_get_at(a1, 100) = 100; assert (*array_uint_back(a1) == 100); assert (array_uint_size(a1) == 101); array_uint_pop_back (NULL, a1);