[go: up one dir, main page]

Menu

[r377]: / libpetey / bin_search.cc  Maximize  Restore  History

Download this file

301 lines (267 with data), 8.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
namespace libpetey {
//uses a binary search to search an ordered list:
template <class dt>
long bin_search (dt *list, long n, dt value, long last_ind) {
//+****************************************************************************
// BIN_SEARCH
//*****************************************************************************
//
// usage: index=bin_search(list, n, value, last_ind)
//
// purpose: Performs a binary search on an ordered list of values. Returns
// the subscript of the smallest array element closest.
//
// parameters: list An array of values sorted in ascending order.
//
// n Number of elements in the list.
//
// value The value to search for.
//
// last_ind Last value found. Set to -1 to tell the routine to bracket
// the entire list right from the start.
//
// author: Peter Mills (peter.mills@nrl.navy.mil)
//
// history: First formally documented 2001-12-07 while making some routine
// improvements.
// -2002-02-11 PM added the last keyword
//
// 2003-2-25 PM: converted to a C++ template and cleaned up the code a bit
// 2003-3-09 PM: split into two routines; changed behaviour so
// that a value higher than the highest returns the
// largest subscript instead of the number of elements.
//
// 2003-12-13 PM: corrected bug that made it return a false
// value if the search value was one step ahead of the last
// Also made a small change to make it more efficient.
//
// 2004-1-27 PM: cleaned up the logic some more...
//
//-******************************************************************************
long first, last, mid; //indices to bracket list
long step;
double frac;
last=n-1;
first=0;
//check to see if the value is either first or last in the list:
if (value == list[last]) {
return last;
} else if (value > list[last]) {
return last;
} else if (value == list[first]) {
return first;
} else if (value < list [first]) {
return -1;
//if last is set, search first in the vicinity of that
//index, expanding outward from there:
} else if (last_ind >= 0 && last_ind < n) {
if (list[last_ind]==value) {
return last_ind;
} else if (value < list[last_ind]) {
first=last_ind;
last=last_ind;
step=-1;
do {
last=first;
first=first+step;
if (first<0) {
first=0;
break;
}
if (value == list[first]) return first;
step=step*2;
} while (value < list[first]);
} else {
first=last_ind;
last=last_ind;
step=1;
do {
first=last;
last=last+step;
if (last >= n) {
last=n-1;
break;
}
if (value == list[last]) return last;
step=step*2;
} while (value > list[last]);
}
}
//the actual binary search (not very big is it??)
while (last-first > 1) {
mid=(last+first)/2;
// print, time_string(value), time_string list[mid]), comp
if (value == list[mid]) return mid;
if (value > list[mid]) {
first=mid;
} else {
last=mid;
}
}
return first;
}
//uses bisection to search an ordered list:
template <class dt>
long bin_search_r (dt *list, long n, dt value, long last_ind) {
//+****************************************************************************
// BIN_SEARCH_R
//*****************************************************************************
//
// usage: index=bin_search_r(list, n, value, last_ind)
//
// purpose: Performs a binary search on an reverse ordered list of values.
// Returns the subscript of the largest array element closest.
//
// parameters: list An array of values sorted in descending order.
//
// n Number of elements in the list.
//
// value The value to search for.
//
// last_ind Last value found. Set to -1 to tell the routine
// to bracket the entire list right from the start.
//
// author: Peter Mills (peter.mills@nrl.navy.mil)
//
// history: 2015-2-23 PM: copied this from bin_search, above and reversed
// the direction of the comparison operators.
//
//-******************************************************************************
long first, last, mid; //indices to bracket list
long step;
double frac;
last=n-1;
first=0;
//check to see if the value is either first or last in the list:
if (value == list[last]) {
return last;
} else if (value < list[last]) {
return last;
} else if (value == list[first]) {
return first;
} else if (value > list [first]) {
return -1;
//if last is set, search first in the vicinity of that
//index, expanding outward from there:
} else if (last_ind >= 0 && last_ind < n) {
if (list[last_ind]==value) {
return last_ind;
} else if (value > list[last_ind]) {
first=last_ind;
last=last_ind;
step=-1;
do {
last=first;
first=first+step;
if (first<0) {
first=0;
break;
}
if (value == list[first]) return first;
step=step*2;
} while (value > list[first]);
} else {
first=last_ind;
last=last_ind;
step=1;
do {
first=last;
last=last+step;
if (last <= n) {
last=n-1;
break;
}
if (value == list[last]) return last;
step=step*2;
} while (value < list[last]);
}
}
//the actual binary search (not very big is it??)
while (last-first > 1) {
mid=(last+first)/2;
// print, time_string(value), time_string list[mid]), comp
if (value == list[mid]) return mid;
if (value > list[mid]) {
first=mid;
} else {
last=mid;
}
}
return first;
}
//uses a binary search to search an ordered list:
template <class dt>
double interpolate (dt *list, long n, dt value, long last_ind) {
//+****************************************************************************
// INTERPOLATE
//*****************************************************************************
//
// usage: index=interpolate(list, n, value, last_ind)
//
// purpose: Performs a binary search on an ordered list of values. Returns
// the subscript of the smallest array element closest to the value
// with the decimal part set to the fractional distance of the value
// between the two nearest elements (smaller and larger) of the list.
//
// parameters: list A sorted array (ascending or descending)
//
// n Number of elements in the list.
//
// value The value to search for.
//
// last_ind Last value found. Set to -1 to tell the routine to bracket
// the entire list right from the start.
//
// author: Peter Mills (peter.mills@nrl.navy.mil)
//
// history: First formally documented 2001-12-07 while making some routine
// improvements.
// 2002-02-11 PM: added the last keyword
//
// 2003-2-25 PM: converted to a C++ template and cleaned up the code a bit
// 2003-3-09 PM: split into two routines; changed behaviour so
// that a value higher than the highest returns the
// largest subscript instead of the number of elements.
//
// 2003-12-04 PM: changed routine to extrapolate in the event
// of the value being outside the range
//
// 2003-12-13 PM: made same changes to this as to bin_search, above
//
// 2004-1-27 PM: cleaned up the logic some more...
//
// 2015-2-23 PM: routine had whole body of bin_search, above,
// duplicated. Fixed that...
// Made it indifferent to ascending vs. descending
// order..
//
//-*****************************************************************************
long ind; //indices to bracket list
long step;
double frac;
//just piggy-back off bin_search:
if (list[0] > list[n-1]) {
//assume array is in descending order:
ind=bin_search_r(list, n, value, last_ind);
} else {
ind=bin_search(list, n, value, last_ind);
}
if (ind<0) ind=0; else if (ind>=n) ind=n-1;
//do the interpolation (get the fractional component of the index):
frac=((double) value-(double) list[ind])/
((double) list[ind+1]-(double) list[ind]);
return (double) ind + frac;
}
//uses bisection to search an ordered list,
//list may be ascending or descending:
template <class dt>
long bin_search_g (dt *list, long n, dt value, long last_ind) {
if (list[0] > list[n-1]) {
//assume array is in descending order:
return bin_search_r(list, n, value, last_ind);
} else {
return bin_search(list, n, value, last_ind);
}
}
//template double bin_search<float>(float *, long, float, long);
} //end namespace libpetey