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 |
--- a/src/array.c Thu Jul 04 22:23:15 2019 +0200 +++ b/src/array.c Thu Jul 04 22:32:03 2019 +0200 @@ -143,6 +143,24 @@ return 0; } +int ucx_array_set(UcxArray *array, size_t index, void *data) { + if (index >= array->size) { + if (ucx_array_reserve(array, index+1)) { + return 1; + } + array->size = index+1; + } + + void *dest = ucx_array_at(*array, index); + if (data) { + memcpy(dest, data, array->elemsize); + } else { + memset(dest, 0, array->elemsize); + } + + return 0; +} + int ucx_array_concat(UcxArray *array1, const UcxArray *array2) { if (array1->elemsize != array2->elemsize)
--- a/src/ucx/array.h Thu Jul 04 22:23:15 2019 +0200 +++ b/src/ucx/array.h Thu Jul 04 22:32:03 2019 +0200 @@ -158,6 +158,20 @@ */ int ucx_array_prepend(UcxArray *array, void *data); + +/** + * Sets an element at the specified index. + * + * If the index is out of bounds, the array automatically grows. + * The pointer to the data may be NULL, in which case the element is zeroed. + * + * @param array a pointer the array where to set the data + * @param index the index of the element to set + * @param data a pointer to the data to insert (may be <code>NULL</code>) + * @return zero on success, non-zero if a reallocation was necessary but failed + */ +int ucx_array_set(UcxArray *array, size_t index, void *data); + /** * Concatenates two arrays. *
--- a/test/array_tests.c Thu Jul 04 22:23:15 2019 +0200 +++ b/test/array_tests.c Thu Jul 04 22:32:03 2019 +0200 @@ -118,6 +118,34 @@ ucx_array_free(&array); } +UCX_TEST(test_ucx_array_set) { + UcxArray array = ucx_array_new(16, sizeof(int)); + + int x = 42; + + UCX_TEST_BEGIN + + ucx_array_set(&array, 7, &x); + UCX_TEST_ASSERT(ucx_array_at_int(array, 7) == 42, "failed"); + UCX_TEST_ASSERT(array.size >= 8, "array not resized on set"); + UCX_TEST_ASSERT(array.capacity == 16, "capacity changed unnecessarily"); + + x = 13; + ucx_array_set(&array, 27, &x); + + UCX_TEST_ASSERT(ucx_array_at_int(array, 27) == 13, "failed"); + UCX_TEST_ASSERT(array.size == 28, "array not resized on set"); + UCX_TEST_ASSERT(array.capacity == 28, "capacity not grown"); + + ucx_array_set(&array, 7, NULL); + + UCX_TEST_ASSERT(ucx_array_at_int(array, 7) == 0, "not zeroed on NULL set"); + + UCX_TEST_END + + ucx_array_free(&array); +} + UCX_TEST(test_ucx_array_equals) { UcxArray a1 = ucx_array_new(16, sizeof(int)); UcxArray a2 = ucx_array_new(16, sizeof(int));
--- a/test/array_tests.h Thu Jul 04 22:23:15 2019 +0200 +++ b/test/array_tests.h Thu Jul 04 22:32:03 2019 +0200 @@ -41,6 +41,7 @@ UCX_TEST(test_ucx_array_at); UCX_TEST(test_ucx_array_append); UCX_TEST(test_ucx_array_prepend); +UCX_TEST(test_ucx_array_set); UCX_TEST(test_ucx_array_autogrow); UCX_TEST(test_ucx_array_equals); UCX_TEST(test_ucx_array_concat);
--- a/test/main.c Thu Jul 04 22:23:15 2019 +0200 +++ b/test/main.c Thu Jul 04 22:32:03 2019 +0200 @@ -148,6 +148,7 @@ ucx_test_register(suite, test_ucx_array_at); ucx_test_register(suite, test_ucx_array_append); ucx_test_register(suite, test_ucx_array_prepend); + ucx_test_register(suite, test_ucx_array_set); ucx_test_register(suite, test_ucx_array_autogrow); ucx_test_register(suite, test_ucx_array_equals); ucx_test_register(suite, test_ucx_array_concat);