From 6d6bbcb18c604e35f9d29ed06e2c9f11eb7974bb Mon Sep 17 00:00:00 2001 From: Jacob Kegerreis <jacob.kegerreis@durham.ac.uk> Date: Mon, 21 May 2018 13:08:52 +0100 Subject: [PATCH] Return the found index instead of using the pointer --- src/utilities.h | 13 ++++++------- tests/testUtilities.c | 10 +++++----- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/utilities.h b/src/utilities.h index 28d6d28eae..02124682eb 100644 --- a/src/utilities.h +++ b/src/utilities.h @@ -27,12 +27,11 @@ * @param x The value to find * @param array The array to search * @param n The length of the array - * @param index The found index * - * Set -1 and n for x below and above the array edge values respectively. + * Return -1 and n for x below and above the array edge values respectively. */ -INLINE static void find_value_in_monotonic_array( - const float x, const float *array, const int n, int *index) { +INLINE static int find_value_in_monotonic_array( + const float x, const float *array, const int n) { int index_mid, index_low = 0, index_high = n; @@ -49,11 +48,11 @@ INLINE static void find_value_in_monotonic_array( // Set index with the found index_low or an error value if outside the array if (x < array[0]) - *index = -1; + return -1; else if (array[n-1] <= x) - *index = n; + return n; else - *index = index_low; + return index_low; } #endif /* SWIFT_UTILITIES_H */ diff --git a/tests/testUtilities.c b/tests/testUtilities.c index 1f2f2cb57c..a08eec37ad 100644 --- a/tests/testUtilities.c +++ b/tests/testUtilities.c @@ -37,28 +37,28 @@ int main() { // Typical value x = 42.42f; - find_value_in_monotonic_array(x, array, n, &index); + index = find_value_in_monotonic_array(x, array, n); if (index != 42) { error("Failed with a typical value "); } // Value on array element x = 33.f; - find_value_in_monotonic_array(x, array, n, &index); + index = find_value_in_monotonic_array(x, array, n); if (index != 33) { error("Failed with an array element "); } // Value below array x = -123.f; - find_value_in_monotonic_array(x, array, n, &index); + index = find_value_in_monotonic_array(x, array, n); if (index != -1) { error("Failed with a value below the array "); } // Value above array x = 123.f; - find_value_in_monotonic_array(x, array, n, &index); + index = find_value_in_monotonic_array(x, array, n); if (index != n) { error("Failed with a value above the array "); } @@ -66,7 +66,7 @@ int main() { // Array slice with typical value x = 9.81f; n = 10; - find_value_in_monotonic_array(x, array + 5, n, &index); + index = find_value_in_monotonic_array(x, array + 5, n); if (index != 4) { error("Failed with an array slice "); } -- GitLab