23 #ifndef O2SCL_SEARCH_VEC_H 24 #define O2SCL_SEARCH_VEC_H 33 #include <o2scl/err_hnd.h> 34 #include <o2scl/vector.h> 36 #ifndef DOXYGEN_NO_O2NS 76 #ifndef DOXYGEN_INTERNAL 99 std::string str=((std::string)
"Vector too small (size=")+
109 std::string str=((std::string)
"Vector too small (size=")+
125 #if !O2SCL_NO_RANGE_CHECK 127 O2SCL_ERR(
"Cache mis-alignment in search_vec::find().",
131 if ((*v)[0]<(*v)[n-1])
return find_inc(x0);
135 size_t find_const(
const double x0,
size_t &cache)
const {
136 #if !O2SCL_NO_RANGE_CHECK 138 O2SCL_ERR(
"Cache mis-alignment in search_vec::find().",
142 if ((*v)[0]<(*v)[n-1])
return find_inc_const(x0,cache);
143 return find_dec_const(x0,cache);
157 if (x0<(*v)[cache]) {
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().",
171 size_t find_inc_const(
const double x0,
size_t &cache)
const {
172 if (x0<(*v)[cache]) {
173 cache=vector_bsearch_inc<vec_t,double>(x0,*
v,0,cache);
174 }
else if (x0>=(*v)[cache+1]) {
175 cache=vector_bsearch_inc<vec_t,double>(x0,*
v,cache,n-1);
177 #if !O2SCL_NO_RANGE_CHECK 179 O2SCL_ERR(
"Cache mis-alignment in search_vec::find_inc().",
196 if (x0>(*v)[cache]) {
197 cache=vector_bsearch_dec<vec_t,double>(x0,*
v,0,cache);
198 }
else if (x0<=(*v)[cache+1]) {
199 cache=vector_bsearch_dec<vec_t,double>(x0,*
v,cache,n-1);
201 #if !O2SCL_NO_RANGE_CHECK 203 O2SCL_ERR(
"Cache mis-alignment in search_vec::find_dec().",
210 size_t find_dec_const(
const double x0,
size_t &cache)
const {
211 if (x0>(*v)[cache]) {
212 cache=vector_bsearch_dec<vec_t,double>(x0,*
v,0,cache);
213 }
else if (x0<=(*v)[cache+1]) {
214 cache=vector_bsearch_dec<vec_t,double>(x0,*
v,cache,n-1);
216 #if !O2SCL_NO_RANGE_CHECK 218 O2SCL_ERR(
"Cache mis-alignment in search_vec::find_dec().",
244 std::string str=((std::string)
"Not enough data (n=")+
251 if ((*v)[0]<=(*v)[n-1]) {
259 row=find_inc_const(x0,cache);
260 if (row<n-1 && fabs((*v)[row+1]-x0)<fabs((*v)[row]-x0)) row++;
271 row=find_dec_const(x0,cache);
272 if (row<n-1 && fabs((*v)[row+1]-x0)<fabs((*v)[row]-x0)) row++;
279 #ifndef DOXYGEN_INTERNAL 295 #ifndef DOXYGEN_INTERNAL 332 std::string str=((std::string)
"Vector too small (n=")+
342 #if !O2SCL_NO_RANGE_CHECK 343 if (this->cache>=this->n) {
344 O2SCL_ERR(
"Cache mis-alignment in search_vec_ext::find().",
348 if ((*this->v)[0]<(*this->v)[this->n-1])
return find_inc(x0);
356 if (x0<(*this->v)[this->cache]) {
357 this->cache=vector_bsearch_inc<vec_t,double>
358 (x0,*this->
v,0,this->cache);
359 }
else if (this->cache<this->n-1 && x0>=(*this->v)[this->cache+1]) {
360 this->cache=vector_bsearch_inc<vec_t,double>
361 (x0,*this->
v,this->cache,this->
n);
363 #if !O2SCL_NO_RANGE_CHECK 364 if (this->cache>=this->n) {
365 O2SCL_ERR(
"Cache mis-alignment in search_vec_ext::find_inc().",
376 if (x0>(*this->v)[this->cache]) {
377 this->cache=vector_bsearch_dec<vec_t,double>
378 (x0,*this->
v,0,this->cache);
379 }
else if (this->cache<this->n-1 && x0<=(*this->v)[this->cache+1]) {
380 this->cache=vector_bsearch_dec<vec_t,double>
381 (x0,*this->
v,this->cache,this->
n);
383 #if !O2SCL_NO_RANGE_CHECK 384 if (this->cache>=this->n) {
385 O2SCL_ERR(
"Cache mis-alignment in search_vec_ext::find_dec().",
392 #ifndef DOXYGEN_INTERNAL 403 #ifndef DOXYGEN_NO_O2NS The main O<span style='position: relative; top: 0.3em; font-size: 0.8em'>2</span>scl O$_2$scl names...
size_t find_inc(const double x0)
Search an increasing vector for the interval containing x0
size_t find(const double x0)
Search an increasing or 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.
size_t cache
Storage for the most recent index.
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.
const vec_t * v
The vector to be searched.
size_t find_inc(const double x0)
Search an increasing vector for the interval containing x0
#define O2SCL_ERR(d, n)
Set an error with message d and code n.
size_t find(const double x0)
Search an increasing or decreasing vector for the interval containing x0
size_t find_dec(const double x0)
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.
Searching class for monotonic data with caching.
An extended search_vec which is allowed to return the last element.
size_t find_dec(const double x0)
Search a decreasing 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.
const vec_t * v
The vector to be searched.
std::string szttos(size_t x)
Convert a size_t to a string.