-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOutwardAlternatingSearch.h
More file actions
53 lines (46 loc) · 1009 Bytes
/
OutwardAlternatingSearch.h
File metadata and controls
53 lines (46 loc) · 1009 Bytes
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
class OutwardAlternatingSearch {
public:
OutwardAlternatingSearch(int middle, int height, int window)
: m_middle(middle), m_height(height), m_window(window),
m_negative(true), m_distance(0), m_done(false)
{}
const OutwardAlternatingSearch & operator++() {
if (m_negative) {
m_distance++;
if (m_middle + m_distance < m_height) {
m_negative = false;
} else if (m_middle - m_distance < 0) {
m_done = true;
}
} else {
if (m_middle - m_distance < 0) {
m_distance++;
if (m_middle + m_distance >= m_height) {
m_done = true;
}
} else {
m_negative = true;
}
}
if (m_distance > m_window) {
m_done = true;
}
return *this;
}
operator bool() {
return !m_done;
}
int offset() const {
return m_negative ? - m_distance : m_distance;
}
int pos() const {
return m_middle + offset();
}
private:
int m_middle;
int m_height;
int m_window;
bool m_negative;
int m_distance;
bool m_done;
};