00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef O2SCL_SEARCH_VEC_H
00024 #define O2SCL_SEARCH_VEC_H
00025
00026 #include <iostream>
00027 #include <string>
00028 #include <gsl/gsl_spline.h>
00029 #include <o2scl/err_hnd.h>
00030 #include <o2scl/collection.h>
00031 #include <o2scl/ovector_tlate.h>
00032
00033
00034
00035
00036 #ifndef DOXYGENP
00037 namespace o2scl {
00038 #endif
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 template<class vec_t> class search_vec {
00053
00054 #ifndef DOXYGEN_INTERNAL
00055
00056 protected:
00057
00058
00059 size_t cache;
00060
00061 #endif
00062
00063 public:
00064
00065
00066
00067 size_t find(const double x0, size_t n, const vec_t &x) {
00068 if (x[0]<x[n-1]) return find_inc(x0,n,x);
00069 return find_dec(x0,n,x);
00070 }
00071
00072
00073
00074 size_t find_inc(const double x0, size_t n, const vec_t &x) {
00075 size_t x_index=cache;
00076
00077
00078 if (x_index>n-1) x_index=n/2;
00079
00080 if (x0<x[x_index]) {
00081 cache=bsearch_inc(x0,x,0,x_index);
00082 } else if (x0>x[x_index+1]) {
00083 cache=bsearch_inc(x0,x,x_index,n-1);
00084 }
00085
00086 return cache;
00087 }
00088
00089
00090
00091 size_t find_dec(const double x0, size_t n, const vec_t &x) {
00092 size_t x_index=cache;
00093
00094
00095 if (x_index>n-1) x_index=n/2;
00096
00097 if (x0>x[x_index]) {
00098 cache=bsearch_dec(x0,x,0,x_index);
00099 } else if (x0<x[x_index+1]) {
00100 cache=bsearch_dec(x0,x,x_index,n-1);
00101 }
00102
00103 return cache;
00104 }
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120 size_t ordered_lookup(const double x0, size_t n, const vec_t &x) {
00121 size_t row;
00122 err_hnd->reset();
00123
00124 if (x[0]<=x[n-1]) {
00125
00126
00127
00128 if (x0>=x[n-1]) {
00129 row=n-1;
00130 } else {
00131 row=find_inc(x0,n,x);
00132 if (row<n-1 && fabs(x[row+1]-x0)<fabs(x[row]-x0)) row++;
00133 }
00134
00135 } else {
00136
00137
00138
00139 if (x0<=x[n-1]) {
00140 row=n-1;
00141 } else {
00142 row=find_dec(x0,n,x);
00143 if (row<n-1 && fabs(x[row+1]-x0)<fabs(x[row]-x0)) row++;
00144 }
00145 }
00146
00147 return row;
00148 }
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168 size_t ordered_interval(const double x0, size_t n, const vec_t &x) {
00169 size_t row;
00170 err_hnd->reset();
00171
00172 if (x[0]<=x[n-1]) {
00173
00174
00175
00176 if (x0>=x[n-1]) row=n-1;
00177 else row=find_inc(x0,n,x);
00178
00179 } else {
00180
00181
00182 if (x0<=x[n-1]) row=n-1;
00183 else row=find_dec(x0,n,x);
00184 }
00185
00186 return row;
00187 }
00188
00189
00190
00191
00192
00193
00194 size_t bsearch_inc(const double x0, const vec_t &x,
00195 size_t lo, size_t hi) const {
00196 while (hi>lo+1) {
00197 size_t i=(hi+lo)/2;
00198 if (x[i]>x0)
00199 hi=i;
00200 else
00201 lo=i;
00202 }
00203
00204 return lo;
00205 }
00206
00207
00208
00209
00210
00211
00212 size_t bsearch_dec(const double x0, const vec_t &x,
00213 size_t lo, size_t hi) const {
00214 while (hi>lo+1) {
00215 size_t i=(hi+lo)/2;
00216 if (x[i]<x0)
00217 hi=i;
00218 else
00219 lo=i;
00220 }
00221
00222 return lo;
00223 }
00224
00225 };
00226
00227 #ifndef DOXYGENP
00228 }
00229 #endif
00230
00231 #endif