commit 51b4335ca377c50e1d17807fda3d3fc69c0bee81
parent 9fc33cbfdf570abfb761f91cd10b0a816137fba9
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Thu, 25 Aug 2022 17:10:27 +0200
Implement the sars_find_overlaped_bands function
Diffstat:
| M | src/sars.c | | | 63 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| M | src/sars.h | | | 7 | +++++++ |
2 files changed, 70 insertions(+), 0 deletions(-)
diff --git a/src/sars.c b/src/sars.c
@@ -21,6 +21,8 @@
#include "sars_c.h"
#include "sars_log.h"
+#include <rsys/algorithm.h>
+
#include <unistd.h> /* sysconf support */
#include <errno.h>
@@ -206,6 +208,23 @@ error:
goto exit;
}
+static INLINE int
+cmp_band(const void* key, const void* item)
+{
+ const struct band* band = item;
+ double wnum;
+ ASSERT(key && item);
+ wnum = *(double*)key;
+
+ if(wnum < band->low) {
+ return -1;
+ } else if(wnum > band->upp) {
+ return +1;
+ } else {
+ return 0;
+ }
+}
+
static void
release_sars(ref_T* ref)
{
@@ -369,3 +388,47 @@ exit:
error:
goto exit;
}
+
+res_T
+sars_find_overlaped_bands
+ (const struct sars* sars,
+ const double range[2],
+ size_t ibands[2])
+{
+ const struct band* bands = NULL;
+ const struct band* low = NULL;
+ const struct band* upp = NULL;
+ size_t nbands = 0;
+ res_T res = RES_OK;
+
+ if(!sars || !range || !bands || range[0] > range[1]) {
+ res = RES_BAD_ARG;
+ goto error;
+ }
+
+ bands = darray_band_cdata_get(&sars->bands);
+ nbands = darray_band_size_get(&sars->bands);
+
+ low = search_lower_bound(range+0, bands, nbands, sizeof(*bands), cmp_band);
+ if(low) {
+ ibands[0] = (size_t)(low - bands);
+ } else {
+ /* The submitted range does not overlap any band */
+ ibands[0] = SIZE_MAX;
+ ibands[1] = 0;
+ goto exit;
+ }
+
+ upp = search_lower_bound(range+1, bands, nbands, sizeof(*bands), cmp_band);
+ if(upp) {
+ ibands[1] = (size_t)(upp - bands);
+ } else {
+ /* The submitted range overlaps the remaining bands */
+ ibands[1] = nbands - 1;
+ }
+
+exit:
+ return res;
+error:
+ goto exit;
+}
diff --git a/src/sars.h b/src/sars.h
@@ -104,4 +104,11 @@ sars_get_band
const size_t iband,
struct sars_band* band);
+/* Returns a degenerated if no band is found */
+SARS_API res_T
+sars_find_overlaped_bands
+ (const struct sars* sars,
+ const double range[2], /* in cm^-1 */
+ size_t ibands[2]); /* Range of overlaped bands. Limits are inclusive */
+
#endif /* SARS_H */