-
Notifications
You must be signed in to change notification settings - Fork 0
/
SATree.h
73 lines (53 loc) · 1.63 KB
/
SATree.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
64
65
66
67
68
69
70
71
72
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <optional>
#include <queue>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <vector>
struct Point
{
int node_id{-1};
std::vector<float> v;
std::string to_string();
friend bool operator==(const Point & self, const Point & other);
};
using Points = std::vector<Point>;
struct SATreeNode
{
Point point;
std::vector<SATreeNode *> neighbours;
float covering_radius{0};
SATreeNode(Point p) : point(p) { }
~SATreeNode();
};
struct kNNResultTuple
{
Point p;
float distance; // Distance from query
std::string to_string() { return "{" + p.to_string() + ", " + std::to_string(distance) + "}"; }
friend bool operator<(const kNNResultTuple & self, const kNNResultTuple & other);
friend bool operator==(const kNNResultTuple & self, const kNNResultTuple & other);
};
using kNNResult = std::vector<kNNResultTuple>;
class SATree
{
SATreeNode * root;
std::unordered_map<std::string, float> distance_cache;
void build(SATreeNode * a, Points & S);
void print(SATreeNode * node, int space);
std::string to_string(SATreeNode * node);
float cached_distance(Point p, Point q);
std::optional<Point> range_search(SATreeNode * node, Point query, float radius, float digression);
public:
SATree(std::vector<Point> S);
~SATree();
void print() { return print(root, 0); }
std::string to_string() { return to_string(root); }
std::optional<Point> range_search(Point query, float radius);
kNNResult nearest_neighbour_search(Point query, int k);
};