search_vec Class Template Reference

Searching class for monotonic data with caching. More...

#include <search_vec.h>

Inheritance diagram for search_vec:

search_vec_ext

Detailed Description

template<class vec_t>
class search_vec< vec_t >

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(). If you happen to know in advance that the vector is increasing or decreasing, then you can use find_inc() or find_dec() instead. To ignore the caching and just use straight binary search, you can use the functions in vector.h .

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.

Note:
The behavior of these functions is undefined if some of the user-specified data is not finite or not strictly monotonic. Two adjacent data points should not be equal. This class does not verify that the user-specified data has these properties.

Definition at line 70 of file search_vec.h.


Public Member Functions

 search_vec (size_t nn, vec_t &x)
 Create a searching object with vector x of size nn.
size_t find (const double x0) const
 Search an increasing or decreasing vector for the interval containing x0.
size_t find_inc (const double x0) const
 Search an increasing vector for the interval containing x0.
size_t find_dec (const double x0) const
 Search a decreasing vector for the interval containing x0.
size_t ordered_lookup (const double x0) const
 Find the index of x0 in the ordered array x.

Protected Attributes

size_t cache
 Storage for the most recent index.
vec_t * v
 Desc.
size_t n
 Desc.

Member Function Documentation

size_t find ( const double  x0  )  const [inline]

This function is identical to find_inc() if the data is increasing, and find_dec() if the data is decreasing.

Reimplemented in search_vec_ext.

Definition at line 110 of file search_vec.h.

size_t find_dec ( const double  x0  )  const [inline]

This function is a cached version of vector_bsearch_dec() . The operation of this function is undefined if the data is not strictly monotonic, i.e. if some of the data elements are equal. This function will call the error handler if n is less than 1.

Reimplemented in search_vec_ext.

Definition at line 149 of file search_vec.h.

size_t find_inc ( const double  x0  )  const [inline]

This function is a cached version of vector_bsearch_inc() .

This function is not quite the same as gsl_interp_accel_find(), because it will return the index of the last element in the array if x0 is larger than the last element. One can call this function with the parameter n as one less than the vector size in order to go back to the behavior of the GSL function. This function, unlike the GSL function, does not record cache hits and misses.

Reimplemented in search_vec_ext.

Definition at line 131 of file search_vec.h.

size_t ordered_lookup ( const double  x0  )  const [inline]

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 you have a non-monotonic vector, you can use vector_lookup() instead, or if you 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() ).

Idea for future:
This function just uses the find functions and then adjusts the answer at the end if necessary. It might be possible to improve the speed by rewriting this from scratch.

Definition at line 181 of file search_vec.h.


Field Documentation

size_t cache [mutable, protected]

Note:
This is marked mutable to ensure const-correctness is straightforward.

Definition at line 81 of file search_vec.h.


The documentation for this class was generated from the following file:

Documentation generated with Doxygen and provided under the GNU Free Documentation License. See License Information for details.

Project hosting provided by SourceForge.net Logo, O2scl Sourceforge Project Page