diff --git a/src/utilities.h b/src/utilities.h index ddacaac499f33bff6a6fb7aa4cdef5b0f841d8c1..28d6d28eae3eaafa6cdbe698fc6c1bee7d3b78ff 100644 --- a/src/utilities.h +++ b/src/utilities.h @@ -21,73 +21,39 @@ #define SWIFT_UTILITIES_H /** - * @brief Search for a value in a monotonic array to find the index such that - * array[index] < value < array[index + 1] + * @brief Search for a value in a monotonically increasing array to find the + * index such that array[index] < value < array[index + 1] * * @param x The value to find - * @param arr The array to search + * @param array The array to search * @param n The length of the array - * @param i The found index + * @param index The found index * * Set -1 and n for x below and above the array edge values respectively. */ -INLINE static void find_value_in_monotonic_array(float x, float *arr, int n, - int *i) { +INLINE static void find_value_in_monotonic_array( + const float x, const float *array, const int n, int *index) { - int is_incr = (arr[n-1] > arr[0]); // Increasing or decreasing? - int i_mid, i_low = 0, i_high = n; + int index_mid, index_low = 0, index_high = n; - // Until arr[i_low] < x < arr[i_high=i_low+1] - while (i_high - i_low > 1) { - i_mid = (i_high + i_low) >> 1; // Middle index + // Until array[index_low] < x < array[index_high=index_low+1] + while (index_high - index_low > 1) { + index_mid = (index_high + index_low) / 2.f; // Middle index - // If mid < x and increasing or x < mid and decreasing - if ((arr[i_mid] <= x) == is_incr) - i_low = i_mid; + // Replace the low or high index with the middle + if (array[index_mid] <= x) + index_low = index_mid; else - i_high = i_mid; + index_high = index_mid; } - // Set i with the found i_low, or an error value if outside the array - if (x < arr[0]) - *i = -1; - else if (arr[n-1] <= x) - *i = n; + // Set index with the found index_low or an error value if outside the array + if (x < array[0]) + *index = -1; + else if (array[n-1] <= x) + *index = n; else - *i = i_low; + *index = index_low; } #endif /* SWIFT_UTILITIES_H */ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/tests/testUtilities.c b/tests/testUtilities.c index d0bd8258e6ee3dbaad2dbc24b40d007e0649cda7..1f2f2cb57cd11b8637b3179e3ed40e8b5797bd8c 100644 --- a/tests/testUtilities.c +++ b/tests/testUtilities.c @@ -26,54 +26,49 @@ int main() { /// Test find_value_in_monotonic_array() int n = 100; - float arr[n]; - int i; + float array[n]; + int index; float x; // Initialise test array for (int j = 0; j < n; j++) { - arr[j] = j; + array[j] = j; } // Typical value x = 42.42f; - find_value_in_monotonic_array(x, arr, n, &i); - if (i != 42) { - printf("Failed with a typical value \n"); - return 1; + find_value_in_monotonic_array(x, array, n, &index); + if (index != 42) { + error("Failed with a typical value "); } // Value on array element x = 33.f; - find_value_in_monotonic_array(x, arr, n, &i); - if (i != 33) { - printf("Failed with an array element \n"); - return 1; + find_value_in_monotonic_array(x, array, n, &index); + if (index != 33) { + error("Failed with an array element "); } // Value below array x = -123.f; - find_value_in_monotonic_array(x, arr, n, &i); - if (i != -1) { - printf("Failed with a value below the array \n"); - return 1; + find_value_in_monotonic_array(x, array, n, &index); + if (index != -1) { + error("Failed with a value below the array "); } // Value above array x = 123.f; - find_value_in_monotonic_array(x, arr, n, &i); - if (i != n) { - printf("Failed with a value above the array \n"); - return 1; + find_value_in_monotonic_array(x, array, n, &index); + if (index != n) { + error("Failed with a value above the array "); } // Array slice with typical value x = 9.81f; n = 10; - find_value_in_monotonic_array(x, arr + 5, n, &i); - if (i != 4) { - printf("Failed with an array slice \n"); - return 1; + find_value_in_monotonic_array(x, array + 5, n, &index); + if (index != 4) { + error("Failed with an array slice "); } return 0;