A
Alan Schroeder
The following code produces the expected results on a PC using gcc, but I
need to port it (or least something similar) to a different
platform/compiler. I don't think I've introduced any undefined behavior but
would like another set of eyes to check.
#include <math.h>
#include <stdlib.h>
extern float window;
/*
* NOTE:
* This function will return the lowest index into 'data' which contains
* a value such that data[index]>=value-window, or -1 if a value can not
* be found such that data[index]>=value-window and
value+window<=data[index].
*/
int
find_first_index(const float *value, const float *data, size_t size, size_t
width)
{
int index;
float *fptr;
float minValue;
index = -1;
fptr = bsearch(value, data, size, width, compare_window);
if (fptr != NULL) {
minValue = *value - window;
index = (int)(fptr-data);
while (index > 0 && data[index] > minValue) {
index--;
}
if (index) index++; /* We may have gone one too far */
}
return index;
}
/*
* NOTE:
* **ONLY** use this function to sort an array of floats; it won't work
* as expected if searching for a arbitrary value.
*/
int
compare_exact(const void *e1, const void *e2)
{
const float *f1 = e1;
const float *f2 = e2;
float delta;
delta = *f1 - *f2;
if (delta < 0.0) return -1 ;
if (delta > 0.0) return 1;
return 0;
}
/*
* This function can be use with 'bsearch' to find a float value within
* a given +/- window
*/
int
compare_window(const void *e1, const void *e2)
{
const float *f1 = e1;
const float *f2 = e2;
float delta;
delta = *f1 - *f2;
if (fabs(delta) <= window) return 0;
if (delta < 0.0) return -1;
if (delta > 0.0) return 1;
/* NOTE: We should **NEVER** get here */
return 0;
}
Thanks,
ALS
need to port it (or least something similar) to a different
platform/compiler. I don't think I've introduced any undefined behavior but
would like another set of eyes to check.
#include <math.h>
#include <stdlib.h>
extern float window;
/*
* NOTE:
* This function will return the lowest index into 'data' which contains
* a value such that data[index]>=value-window, or -1 if a value can not
* be found such that data[index]>=value-window and
value+window<=data[index].
*/
int
find_first_index(const float *value, const float *data, size_t size, size_t
width)
{
int index;
float *fptr;
float minValue;
index = -1;
fptr = bsearch(value, data, size, width, compare_window);
if (fptr != NULL) {
minValue = *value - window;
index = (int)(fptr-data);
while (index > 0 && data[index] > minValue) {
index--;
}
if (index) index++; /* We may have gone one too far */
}
return index;
}
/*
* NOTE:
* **ONLY** use this function to sort an array of floats; it won't work
* as expected if searching for a arbitrary value.
*/
int
compare_exact(const void *e1, const void *e2)
{
const float *f1 = e1;
const float *f2 = e2;
float delta;
delta = *f1 - *f2;
if (delta < 0.0) return -1 ;
if (delta > 0.0) return 1;
return 0;
}
/*
* This function can be use with 'bsearch' to find a float value within
* a given +/- window
*/
int
compare_window(const void *e1, const void *e2)
{
const float *f1 = e1;
const float *f2 = e2;
float delta;
delta = *f1 - *f2;
if (fabs(delta) <= window) return 0;
if (delta < 0.0) return -1;
if (delta > 0.0) return 1;
/* NOTE: We should **NEVER** get here */
return 0;
}
Thanks,
ALS