#if !defined  HAVE_BSEARCHAPPROX_H__
#define       HAVE_BSEARCHAPPROX_H__

#include "fxttypes.h"

#include "sort/bsearch.h"
#include "sort/bsearchfunc.h"


template <typename Type>
ulong bsearch_approx(const Type *f, ulong n, const Type v, Type da)
// Return index of first element x in f[] for which  |(x-v)| <= da
// Return ~0 if there is no such element.
// f[] must be sorted in ascending order.
// da must be positive.
//
// Makes sense only with inexact types (float or double).
// Must have  n!=0
{
    ulong k = bsearch_ge(f, n, v-da);
    if ( k<n )  k = bsearch_le(f+k, n-k, v+da);
    return k;
}
// -------------------------


template <typename Type>
ulong bsearch_approx(const Type *f, ulong n, const Type v, Type da,
                     int (*cmp)(const Type &, const Type &))
//
// Return index of first element x in f[] for which  |(x-v)| <= da
//    with respect to comparison function cmp().
// Return ~0 if there is no such element.
// f[] must be sorted in ascending order.
// da must be positive.
//
// Makes sense only with inexact types (float or double).
// Must have  n!=0
{
    ulong k = bsearch_ge(f, n, v-da, cmp);
    if ( k<n )  k = bsearch_le(f+k, n-k, v+da, cmp);
    return k;
}
// -------------------------


#endif  // !defined HAVE_BSEARCHAPPROX_H__
