-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadaptivesearch.h
63 lines (48 loc) · 1.27 KB
/
adaptivesearch.h
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
#ifndef ADAPTIVESEARCH_H
#define ADAPTIVESEARCH_H
#include <cinttypes>
#include <assert.h>
#include "lin.h"
#include "util.h"
#include "padded_vector.h"
#include <limits>
template <int record_bytes = 8, typename Index = unsigned long>
class adaptivesearch {
using Vector = PaddedVector<record_bytes>;
using Linear = LinearUnroll<Vector>;
const Vector& A;
public:
adaptivesearch(const Vector &_a) : A(_a) {}
__attribute__((always_inline)) Key operator()(const Key x) {
Index bot, top, next, med, position;
bool finished = false;
bot = 0;
top = A.size() - 1;
position = -1;
while((position == -1) && (bot < top)) {
med = (top - bot) / 2;
next = (int) ((x - A[bot]) / (A[top] - A[bot]) * (top - bot) + bot);
if ((x < A[next]) && ((next - bot) > med)) {
top = next - 1;
next = (bot + top) / 2;
} else if ((x > A[next]) && ((top - next) > med)) {
bot = next + 1;
next = (bot + top) / 2;
}
if (x > A[next]) {
bot = next + 1;
} else if (x < A[next]) {
top = next - 1;
} else {
position = next;
}
}
if(A[top] == x) {
position = top;
} else {
position = -top - 1;
}
return A[position];
}
};
#endif //ADAPTIVESEARCH_H