Thu, 04 Jul 2019 22:32:03 +0200
adds ucx_array_set()
src/array.c | file | annotate | diff | comparison | revisions | |
src/ucx/array.h | file | annotate | diff | comparison | revisions | |
test/array_tests.c | file | annotate | diff | comparison | revisions | |
test/array_tests.h | file | annotate | diff | comparison | revisions | |
test/main.c | file | annotate | diff | comparison | revisions |
1.1 --- a/src/array.c Thu Jul 04 22:23:15 2019 +0200 1.2 +++ b/src/array.c Thu Jul 04 22:32:03 2019 +0200 1.3 @@ -143,6 +143,24 @@ 1.4 return 0; 1.5 } 1.6 1.7 +int ucx_array_set(UcxArray *array, size_t index, void *data) { 1.8 + if (index >= array->size) { 1.9 + if (ucx_array_reserve(array, index+1)) { 1.10 + return 1; 1.11 + } 1.12 + array->size = index+1; 1.13 + } 1.14 + 1.15 + void *dest = ucx_array_at(*array, index); 1.16 + if (data) { 1.17 + memcpy(dest, data, array->elemsize); 1.18 + } else { 1.19 + memset(dest, 0, array->elemsize); 1.20 + } 1.21 + 1.22 + return 0; 1.23 +} 1.24 + 1.25 int ucx_array_concat(UcxArray *array1, const UcxArray *array2) { 1.26 1.27 if (array1->elemsize != array2->elemsize)
2.1 --- a/src/ucx/array.h Thu Jul 04 22:23:15 2019 +0200 2.2 +++ b/src/ucx/array.h Thu Jul 04 22:32:03 2019 +0200 2.3 @@ -158,6 +158,20 @@ 2.4 */ 2.5 int ucx_array_prepend(UcxArray *array, void *data); 2.6 2.7 + 2.8 +/** 2.9 + * Sets an element at the specified index. 2.10 + * 2.11 + * If the index is out of bounds, the array automatically grows. 2.12 + * The pointer to the data may be NULL, in which case the element is zeroed. 2.13 + * 2.14 + * @param array a pointer the array where to set the data 2.15 + * @param index the index of the element to set 2.16 + * @param data a pointer to the data to insert (may be <code>NULL</code>) 2.17 + * @return zero on success, non-zero if a reallocation was necessary but failed 2.18 + */ 2.19 +int ucx_array_set(UcxArray *array, size_t index, void *data); 2.20 + 2.21 /** 2.22 * Concatenates two arrays. 2.23 *
3.1 --- a/test/array_tests.c Thu Jul 04 22:23:15 2019 +0200 3.2 +++ b/test/array_tests.c Thu Jul 04 22:32:03 2019 +0200 3.3 @@ -118,6 +118,34 @@ 3.4 ucx_array_free(&array); 3.5 } 3.6 3.7 +UCX_TEST(test_ucx_array_set) { 3.8 + UcxArray array = ucx_array_new(16, sizeof(int)); 3.9 + 3.10 + int x = 42; 3.11 + 3.12 + UCX_TEST_BEGIN 3.13 + 3.14 + ucx_array_set(&array, 7, &x); 3.15 + UCX_TEST_ASSERT(ucx_array_at_int(array, 7) == 42, "failed"); 3.16 + UCX_TEST_ASSERT(array.size >= 8, "array not resized on set"); 3.17 + UCX_TEST_ASSERT(array.capacity == 16, "capacity changed unnecessarily"); 3.18 + 3.19 + x = 13; 3.20 + ucx_array_set(&array, 27, &x); 3.21 + 3.22 + UCX_TEST_ASSERT(ucx_array_at_int(array, 27) == 13, "failed"); 3.23 + UCX_TEST_ASSERT(array.size == 28, "array not resized on set"); 3.24 + UCX_TEST_ASSERT(array.capacity == 28, "capacity not grown"); 3.25 + 3.26 + ucx_array_set(&array, 7, NULL); 3.27 + 3.28 + UCX_TEST_ASSERT(ucx_array_at_int(array, 7) == 0, "not zeroed on NULL set"); 3.29 + 3.30 + UCX_TEST_END 3.31 + 3.32 + ucx_array_free(&array); 3.33 +} 3.34 + 3.35 UCX_TEST(test_ucx_array_equals) { 3.36 UcxArray a1 = ucx_array_new(16, sizeof(int)); 3.37 UcxArray a2 = ucx_array_new(16, sizeof(int));
4.1 --- a/test/array_tests.h Thu Jul 04 22:23:15 2019 +0200 4.2 +++ b/test/array_tests.h Thu Jul 04 22:32:03 2019 +0200 4.3 @@ -41,6 +41,7 @@ 4.4 UCX_TEST(test_ucx_array_at); 4.5 UCX_TEST(test_ucx_array_append); 4.6 UCX_TEST(test_ucx_array_prepend); 4.7 +UCX_TEST(test_ucx_array_set); 4.8 UCX_TEST(test_ucx_array_autogrow); 4.9 UCX_TEST(test_ucx_array_equals); 4.10 UCX_TEST(test_ucx_array_concat);
5.1 --- a/test/main.c Thu Jul 04 22:23:15 2019 +0200 5.2 +++ b/test/main.c Thu Jul 04 22:32:03 2019 +0200 5.3 @@ -148,6 +148,7 @@ 5.4 ucx_test_register(suite, test_ucx_array_at); 5.5 ucx_test_register(suite, test_ucx_array_append); 5.6 ucx_test_register(suite, test_ucx_array_prepend); 5.7 + ucx_test_register(suite, test_ucx_array_set); 5.8 ucx_test_register(suite, test_ucx_array_autogrow); 5.9 ucx_test_register(suite, test_ucx_array_equals); 5.10 ucx_test_register(suite, test_ucx_array_concat);