-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
vptree.hpp
224 lines (181 loc) · 5.1 KB
/
vptree.hpp
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
/* This software is distributed under BSD 3-clause license (see LICENSE file).
*
* Copyright (c) 2012-2013 Laurens van der Maaten, Sergey Lisitsyn
*/
#ifndef TAPKEE_VPTREE_H_
#define TAPKEE_VPTREE_H_
/* Tapkee includes */
#include <shogun/lib/tapkee/defines.hpp>
/* End of Tapkee includes */
#include <vector>
#include <queue>
#include <algorithm>
#include <limits>
namespace tapkee
{
namespace tapkee_internal
{
template<class Type, class RandomAccessIterator, class DistanceCallback>
struct compare_impl;
template<class RandomAccessIterator, class DistanceCallback>
struct DistanceComparator
{
DistanceCallback callback;
const RandomAccessIterator item;
DistanceComparator(const DistanceCallback& c, const RandomAccessIterator& i) :
callback(c), item(i) {}
inline bool operator()(const RandomAccessIterator& a, const RandomAccessIterator& b)
{
return compare_impl<typename DistanceCallback::type,RandomAccessIterator,DistanceCallback>()
(callback,item,a,b);
}
};
struct KernelType;
template<class RandomAccessIterator, class DistanceCallback>
struct compare_impl<KernelType,RandomAccessIterator,DistanceCallback>
{
inline bool operator()(DistanceCallback& callback, const RandomAccessIterator& item,
const RandomAccessIterator& a, const RandomAccessIterator& b)
{
return (-2*callback(item,a) + callback(a,a)) < (-2*callback(item,b) + callback(b,b));
}
};
struct DistanceType;
template<class RandomAccessIterator, class DistanceCallback>
struct compare_impl<DistanceType,RandomAccessIterator,DistanceCallback>
{
inline bool operator()(DistanceCallback& callback, const RandomAccessIterator& item,
const RandomAccessIterator& a, const RandomAccessIterator& b)
{
return callback(item,a) < callback(item,b);
}
};
template<class RandomAccessIterator, class DistanceCallback>
class VantagePointTree
{
public:
// Default constructor
VantagePointTree(RandomAccessIterator b, RandomAccessIterator e, DistanceCallback c) :
begin(b), items(), callback(c), tau(0.0), root(0)
{
items.reserve(e-b);
for (RandomAccessIterator i=b; i!=e; ++i)
items.push_back(i);
root = buildFromPoints(0, items.size());
}
// Destructor
~VantagePointTree()
{
delete root;
}
// Function that uses the tree to find the k nearest neighbors of target
std::vector<IndexType> search(const RandomAccessIterator& target, int k)
{
std::vector<IndexType> results;
// Use a priority queue to store intermediate results on
std::priority_queue<HeapItem> heap;
// Variable that tracks the distance to the farthest point in our results
tau = std::numeric_limits<double>::max();
// Perform the searcg
search(root, target, k, heap);
// Gather final results
results.reserve(k);
while(!heap.empty()) {
results.push_back(items[heap.top().index]-begin);
heap.pop();
}
return results;
}
private:
VantagePointTree(const VantagePointTree&);
VantagePointTree& operator=(const VantagePointTree&);
RandomAccessIterator begin;
std::vector<RandomAccessIterator> items;
DistanceCallback callback;
double tau;
struct Node
{
int index;
double threshold;
Node* left;
Node* right;
Node() :
index(0), threshold(0.),
left(0), right(0)
{
}
~Node()
{
delete left;
delete right;
}
Node(const Node&);
Node& operator=(const Node&);
}* root;
struct HeapItem {
HeapItem(int i, double d) :
index(i), distance(d) {}
int index;
double distance;
bool operator<(const HeapItem& o) const {
return distance < o.distance;
}
};
Node* buildFromPoints(int lower, int upper)
{
if (upper == lower)
{
return NULL;
}
Node* node = new Node();
node->index = lower;
if (upper - lower > 1)
{
int i = (int) (tapkee::uniform_random() * (upper - lower - 1)) + lower;
std::swap(items[lower], items[i]);
int median = (upper + lower) / 2;
std::nth_element(items.begin() + lower + 1, items.begin() + median, items.begin() + upper,
DistanceComparator<RandomAccessIterator,DistanceCallback>(callback,items[lower]));
node->threshold = callback.distance(items[lower], items[median]);
node->index = lower;
node->left = buildFromPoints(lower + 1, median);
node->right = buildFromPoints(median, upper);
}
return node;
}
void search(Node* node, const RandomAccessIterator& target, int k, std::priority_queue<HeapItem>& heap)
{
if (node == NULL)
return;
double distance = callback.distance(items[node->index], target);
if (distance < tau)
{
if (heap.size() == static_cast<size_t>(k))
heap.pop();
heap.push(HeapItem(node->index, distance));
if (heap.size() == static_cast<size_t>(k))
tau = heap.top().distance;
}
if (node->left == NULL && node->right == NULL)
{
return;
}
if (distance < node->threshold)
{
if ((distance - tau) <= node->threshold)
search(node->left, target, k, heap);
if ((distance + tau) >= node->threshold)
search(node->right, target, k, heap);
}
else
{
if ((distance + tau) >= node->threshold)
search(node->right, target, k, heap);
if ((distance - tau) <= node->threshold)
search(node->left, target, k, heap);
}
}
};
}
}
#endif