23 #ifndef O2SCL_SEARCH_VEC_H
24 #define O2SCL_SEARCH_VEC_H
32 #include <o2scl/err_hnd.h>
33 #include <o2scl/vector.h>
35 #ifndef DOXYGEN_NO_O2NS
80 #ifndef DOXYGEN_INTERNAL
110 std::string str=((std::string)
"Vector too small (size=")+
121 std::string str=((std::string)
"Vector too small (size=")+
136 size_t find(
const double x0)
const {
137 #if !O2SCL_NO_RANGE_CHECK
139 O2SCL_ERR(
"Cache mis-alignment in search_vec::find().",
158 cache=vector_bsearch_inc<vec_t,double>(x0,*
v,0,
cache);
159 }
else if (x0>=(*
v)[cache+1]) {
160 cache=vector_bsearch_inc<vec_t,double>(x0,*
v,
cache,
n-1);
162 #if !O2SCL_NO_RANGE_CHECK
164 O2SCL_ERR(
"Cache mis-alignment in search_vec::find_inc().",
181 cache=vector_bsearch_dec<vec_t,double>(x0,*
v,0,
cache);
182 }
else if (x0<=(*
v)[cache+1]) {
183 cache=vector_bsearch_dec<vec_t,double>(x0,*
v,
cache,
n-1);
185 #if !O2SCL_NO_RANGE_CHECK
187 O2SCL_ERR(
"Cache mis-alignment in search_vec::find_dec().",
213 std::string str=((std::string)
"Not enough data (n=")+
220 if ((*
v)[0]<=(*v)[
n-1]) {
228 if (row<
n-1 && fabs((*
v)[row+1]-x0)<fabs((*
v)[row]-x0)) row++;
239 if (row<
n-1 && fabs((*
v)[row+1]-x0)<fabs((*
v)[row]-x0)) row++;
246 #ifndef DOXYGEN_INTERNAL
282 std::string str=((std::string)
"Vector too small (n=")+
291 size_t find(
const double x0)
const {
292 #if !O2SCL_NO_RANGE_CHECK
293 if (this->
cache>=this->
n) {
294 O2SCL_ERR(
"Cache mis-alignment in search_vec_ext::find().",
298 if ((*this->
v)[0]<(*this->
v)[this->
n-1])
return find_inc(x0);
306 if (x0<(*this->
v)[this->
cache]) {
307 this->cache=vector_bsearch_inc<vec_t,double>
308 (x0,*this->
v,0,this->
cache);
309 }
else if (this->cache<this->
n-1 && x0>=(*this->
v)[this->cache+1]) {
310 this->cache=vector_bsearch_inc<vec_t,double>
311 (x0,*this->
v,this->
cache,this->
n);
313 #if !O2SCL_NO_RANGE_CHECK
314 if (this->cache>=this->
n) {
315 O2SCL_ERR(
"Cache mis-alignment in search_vec_ext::find_inc().",
326 if (x0>(*this->
v)[this->
cache]) {
327 this->cache=vector_bsearch_dec<vec_t,double>
328 (x0,*this->
v,0,this->
cache);
329 }
else if (this->cache<this->
n-1 && x0<=(*this->
v)[this->cache+1]) {
330 this->cache=vector_bsearch_dec<vec_t,double>
331 (x0,*this->
v,this->
cache,this->
n);
333 #if !O2SCL_NO_RANGE_CHECK
334 if (this->cache>=this->
n) {
335 O2SCL_ERR(
"Cache mis-alignment in search_vec_ext::find_dec().",
342 #ifndef DOXYGEN_INTERNAL
353 #ifndef DOXYGEN_NO_O2NS
size_t cache
Storage for the most recent index.
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
sanity check failed - shouldn't happen
invalid argument supplied by user
search_vec_ext()
Create a blank searching object.
search_vec()
Create a blank searching object.
search_vec(size_t nn, const 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
const vec_t * v
The vector to be searched.
size_t find(const double x0) const
Search an increasing or decreasing vector for the interval containing x0
#define O2SCL_ERR(d, n)
Set an error with message d and code n.
Searching class for monotonic data with caching.
size_t find_dec(const double x0) const
Search a decreasing vector for the interval containing x0
An extended search_vec which is allowed to return the last element.
size_t find_inc(const double x0) const
Search an increasing vector for the interval containing x0
void set_vec(size_t nn, const vec_t &x)
Set the vector to be searched.
search_vec_ext(size_t nn, const vec_t &x)
Create a searching object for vector x of size nn.
std::string szttos(size_t x)
Convert a size_t to a string.
size_t ordered_lookup(const double x0) const
Find the index of x0 in the ordered array x.