Skip to content
Snippets Groups Projects
Commit 3572f993 authored by Jacob Kegerreis's avatar Jacob Kegerreis
Browse files

Do tweaks for merging

parent 9da3668a
Branches
Tags
1 merge request!547Add generic utility function to find a value in an array
...@@ -21,73 +21,39 @@ ...@@ -21,73 +21,39 @@
#define SWIFT_UTILITIES_H #define SWIFT_UTILITIES_H
/** /**
* @brief Search for a value in a monotonic array to find the index such that * @brief Search for a value in a monotonically increasing array to find the
* array[index] < value < array[index + 1] * index such that array[index] < value < array[index + 1]
* *
* @param x The value to find * @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 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. * 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, INLINE static void find_value_in_monotonic_array(
int *i) { const float x, const float *array, const int n, int *index) {
int is_incr = (arr[n-1] > arr[0]); // Increasing or decreasing? int index_mid, index_low = 0, index_high = n;
int i_mid, i_low = 0, i_high = n;
// Until arr[i_low] < x < arr[i_high=i_low+1] // Until array[index_low] < x < array[index_high=index_low+1]
while (i_high - i_low > 1) { while (index_high - index_low > 1) {
i_mid = (i_high + i_low) >> 1; // Middle index index_mid = (index_high + index_low) / 2.f; // Middle index
// If mid < x and increasing or x < mid and decreasing // Replace the low or high index with the middle
if ((arr[i_mid] <= x) == is_incr) if (array[index_mid] <= x)
i_low = i_mid; index_low = index_mid;
else else
i_high = i_mid; index_high = index_mid;
} }
// Set i with the found i_low, or an error value if outside the array // Set index with the found index_low or an error value if outside the array
if (x < arr[0]) if (x < array[0])
*i = -1; *index = -1;
else if (arr[n-1] <= x) else if (array[n-1] <= x)
*i = n; *index = n;
else else
*i = i_low; *index = index_low;
} }
#endif /* SWIFT_UTILITIES_H */ #endif /* SWIFT_UTILITIES_H */
...@@ -26,54 +26,49 @@ ...@@ -26,54 +26,49 @@
int main() { int main() {
/// Test find_value_in_monotonic_array() /// Test find_value_in_monotonic_array()
int n = 100; int n = 100;
float arr[n]; float array[n];
int i; int index;
float x; float x;
// Initialise test array // Initialise test array
for (int j = 0; j < n; j++) { for (int j = 0; j < n; j++) {
arr[j] = j; array[j] = j;
} }
// Typical value // Typical value
x = 42.42f; x = 42.42f;
find_value_in_monotonic_array(x, arr, n, &i); find_value_in_monotonic_array(x, array, n, &index);
if (i != 42) { if (index != 42) {
printf("Failed with a typical value \n"); error("Failed with a typical value ");
return 1;
} }
// Value on array element // Value on array element
x = 33.f; x = 33.f;
find_value_in_monotonic_array(x, arr, n, &i); find_value_in_monotonic_array(x, array, n, &index);
if (i != 33) { if (index != 33) {
printf("Failed with an array element \n"); error("Failed with an array element ");
return 1;
} }
// Value below array // Value below array
x = -123.f; x = -123.f;
find_value_in_monotonic_array(x, arr, n, &i); find_value_in_monotonic_array(x, array, n, &index);
if (i != -1) { if (index != -1) {
printf("Failed with a value below the array \n"); error("Failed with a value below the array ");
return 1;
} }
// Value above array // Value above array
x = 123.f; x = 123.f;
find_value_in_monotonic_array(x, arr, n, &i); find_value_in_monotonic_array(x, array, n, &index);
if (i != n) { if (index != n) {
printf("Failed with a value above the array \n"); error("Failed with a value above the array ");
return 1;
} }
// Array slice with typical value // Array slice with typical value
x = 9.81f; x = 9.81f;
n = 10; n = 10;
find_value_in_monotonic_array(x, arr + 5, n, &i); find_value_in_monotonic_array(x, array + 5, n, &index);
if (i != 4) { if (index != 4) {
printf("Failed with an array slice \n"); error("Failed with an array slice ");
return 1;
} }
return 0; return 0;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment