#include <search_vec.h>
A searching class for monotonic vectors. A caching system similar to gsl_interp_accel
is used.
To find the interval containing a value, use find_interval(). If you happen to know in advance that the vector is increasing or decreasing, then you can use find_interval_inc() or find_interval_dec() instead. To ignore the caching and just use straight binary search, you can use bsearch_inc() or bsearch_dec() for increasing or decreasing arrays respectively.
If you want to allow the function to return n-1
, to indicate the value is larger than or equal to the last element, use find_interval_uncons().
Alternatively, if you just want to find the index with the element closest to a specified value, use ordered_lookup(). Note that ordered_lookup() is slightly different than ovector_tlate::lookup(), since the latter does not assume the data is monotonic.
Definition at line 69 of file search_vec.h.
Public Member Functions | |
size_t | find_interval (const double x0, size_t n, const vec_t &x) |
Search an increasing or decreasing vector for the interval containing x0 . | |
size_t | find_interval_inc (const double x0, size_t n, const vec_t &x) |
Search an increasing vector for the interval containing x0 . | |
size_t | find_interval_dec (const double x0, size_t n, const vec_t &x) |
Search a decreasing vector for the interval containing x0 . | |
size_t | ordered_lookup (const double x0, size_t n, const vec_t &x) |
Find the index of x0 in the ordered array x . | |
size_t | find_interval_uncons (const double x0, size_t n, const vec_t &x) |
Search an increasing or decreasing vector for the interval containing x0 including the open interval at the end. | |
size_t | bsearch_inc (const double x0, const vec_t &x, size_t lo, size_t hi) const |
Binary search a part of an increasing vector for the interval containing x0 . | |
size_t | bsearch_dec (const double x0, const vec_t &x, size_t lo, size_t hi) const |
Binary search a part of an decreasing vector for the interval containing x0 . | |
Protected Attributes | |
size_t | cache |
Storage for the most recent index. |
size_t bsearch_dec | ( | const double | x0, | |
const vec_t & | x, | |||
size_t | lo, | |||
size_t | hi | |||
) | const [inline] |
Binary search a part of an decreasing vector for the interval containing x0
.
This function performs a binary search of between x[lo]
and x[hi]
(inclusive). It returns
lo
if x0
> x[lo]
i
if x[i]
>= x0
> x[i+1]
for lo
<= i
< hi
hi-1
if x0
<= x
[hi]
The cache is not used for this function.
hi
for an n
element array is typically n-1
, e.g. For double x[10]
one would use bsearch_dec(1.0,x,0,9)
. Definition at line 272 of file search_vec.h.
size_t bsearch_inc | ( | const double | x0, | |
const vec_t & | x, | |||
size_t | lo, | |||
size_t | hi | |||
) | const [inline] |
Binary search a part of an increasing vector for the interval containing x0
.
This function performs a binary search of between x[lo]
and x[hi]
(inclusive). It returns
lo
if x0
< x[lo]
i
if x[i]
<= x0
< x[i+1]
for lo
<= i
< hi
hi-1
if x0
>= x
[hi]
This function operates in the same way as gsl_interp_bsearch()
.
The cache is not used for this function.
hi
for an n
element array is typically n-1
, e.g. For double x[10]
one would use bsearch_inc(1.0,x,0,9)
. Definition at line 241 of file search_vec.h.
size_t find_interval_inc | ( | const double | x0, | |
size_t | n, | |||
const vec_t & | x | |||
) | [inline] |
Search an increasing vector for the interval containing x0
.
This function operates in the same way as gsl_interp_accel_find()
, except that it does not record cache hits and misses.
Definition at line 102 of file search_vec.h.
size_t find_interval_uncons | ( | const double | x0, | |
size_t | n, | |||
const vec_t & | x | |||
) | [inline] |
Search an increasing or decreasing vector for the interval containing x0
including the open interval at the end.
This returns the index i for which x[i]<=x0<x[i+1].
This function operates just as find_interval(), except that in the case of increasing arrays, it will return n-1
if x0
is greater than the last element in the array. The decreasing case is handled analogously.
If some of the values in the vector are not finite, then the output of this function is not defined.
Definition at line 201 of file search_vec.h.
size_t ordered_lookup | ( | const double | x0, | |
size_t | n, | |||
const vec_t & | x | |||
) | [inline] |
Find the index of x0 in the ordered array x
.
This returns the index i for which x[i] is as close as possible to x0 if x[i] is either increasing or decreasing.
If some of the values in the ovector are not finite, then the output of this function is not defined.
If you have a non-monotonic ovector or uvector object, consider using ovector_view_tlate::lookup() or uvector_view_tlate::lookup() instead of this function.
Generally, if there are two adjacent entries with the same value, this function will return the entry with the smaller index. (This is the same as the convention in ovector_view_tlate::lookup() and uvector_view_tlate::lookup() ).
find_interval
functions and then adjusts the answer at the end if necessary. Definition at line 158 of file search_vec.h.
Documentation generated with Doxygen and provided under the GNU Free Documentation License. See License Information for details.
Project hosting provided by
,
O2scl Sourceforge Project Page