# HG changeset patch # User Mike Becker # Date 1562272323 -7200 # Node ID f695ae118460eb9b6d8d903d6ff1ac30ddeb7c20 # Parent 6d7aa8a1a3b300d42112d57a7248d899b648cf6e adds ucx_array_set() diff -r 6d7aa8a1a3b3 -r f695ae118460 src/array.c --- 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) diff -r 6d7aa8a1a3b3 -r f695ae118460 src/ucx/array.h --- 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 NULL) + * @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. * diff -r 6d7aa8a1a3b3 -r f695ae118460 test/array_tests.c --- 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)); diff -r 6d7aa8a1a3b3 -r f695ae118460 test/array_tests.h --- 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); diff -r 6d7aa8a1a3b3 -r f695ae118460 test/main.c --- 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);