1 #if !defined  HAVE_BSEARCHAPPROX_H__
2 #define       HAVE_BSEARCHAPPROX_H__
3 // This file is part of the FXT library.
4 // Copyright (C) 2010 Joerg Arndt
5 // License: GNU General Public License version 3 or later,
6 // see the file COPYING.txt in the main directory.
7 
8 #include "fxttypes.h"
9 
10 #include "sort/bsearch.h"
11 #include "sort/bsearchfunc.h"
12 
13 
14 template <typename Type>
bsearch_approx(const Type * f,ulong n,const Type v,Type da)15 ulong bsearch_approx(const Type *f, ulong n, const Type v, Type da)
16 // Return index of first element x in f[] for which  |(x-v)| <= da
17 // Return n if there is no such element.
18 // f[] must be sorted in ascending order.
19 // da must be positive.
20 //
21 // Makes sense only with inexact types (float or double).
22 // Must have  n!=0
23 {
24     ulong k = bsearch_geq(f, n, v-da);
25     if ( k<n )  k = bsearch_leq(f+k, n-k, v+da);
26     return k;
27 }
28 // -------------------------
29 
30 
31 template <typename Type>
bsearch_approx(const Type * f,ulong n,const Type v,Type da,int (* cmp)(const Type &,const Type &))32 ulong bsearch_approx(const Type *f, ulong n, const Type v, Type da,
33                      int (*cmp)(const Type &, const Type &))
34 //
35 // Return index of first element x in f[] for which  |(x-v)| <= da
36 //    with respect to comparison function cmp().
37 // Return n if there is no such element.
38 // f[] must be sorted in ascending order.
39 // da must be positive.
40 //
41 // Makes sense only with inexact types (float or double).
42 // Must have  n!=0
43 {
44     ulong k = bsearch_geq(f, n, v-da, cmp);
45     if ( k<n )  k = bsearch_leq(f+k, n-k, v+da, cmp);
46     return k;
47 }
48 // -------------------------
49 
50 
51 #endif  // !defined HAVE_BSEARCHAPPROX_H__
52