This repository has been archived by the owner on Jun 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
lib-dolphinn.cpp
133 lines (114 loc) · 2.88 KB
/
lib-dolphinn.cpp
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
#include <cstdint>
#include <cstdlib>
#include <cstdbool>
#include <string>
#include <cstring>
#include <vector>
#include <sstream>
#include <fstream>
#include <iostream>
#include "lib-dolphinn/IO.h"
#include "lib-dolphinn/hypercube.h"
extern "C" {
static int entry_length;
static int max_number_of_points = 0;
static int r = 0;
static int dimModifier = 0;
bool configure(const char* var, const char* val) {
if (strcmp(var, "r") == 0) {
char* end;
errno = 0;
long k = strtol(val, &end, 10);
if (errno != 0 || *val == 0 || *end != 0 || k < 0) {
return false;
} else {
r = k;
return true;
}
} else if (strcmp(var, "modifier") == 0) {
char* end;
errno = 0;
long k = strtol(val, &end, 10);
if (errno != 0 || *val == 0 || *end != 0 || k < 0) {
return false;
} else {
dimModifier = k;
return true;
}
} else if (strcmp(var, "numpoints") == 0) {
char* end;
errno = 0;
long k = strtol(val, &end, 10);
if (errno != 0 || *val == 0 || *end != 0 || k < 0) {
return false;
} else {
max_number_of_points = k;
return true;
}
} else return false;
}
static std::vector<double>* pointset = nullptr;
bool end_configure(void) {
pointset = new std::vector<double>();
return true;
}
static size_t entry_count = 0;
std::vector<double> parseEntry(const char* entry) {
std::vector<double> e;
std::string line(entry);
double x;
auto sstr = std::istringstream(line);
while (sstr >> x) {
e.push_back(x);
}
return e;
}
bool train(const char* entry) {
auto parsed_entry = parseEntry(entry);
pointset->insert(pointset->end(), parsed_entry.begin(), parsed_entry.end());
entry_count++;
return true;
}
static Dolphinn::Hypercube<double, char>* hypercube = nullptr;
static std::vector<std::vector<std::pair<int,float>>> results_distances(1);
static std::vector<double> parsed_entry;
static size_t position = 0;
void end_train(void) {
entry_length = pointset->size() / entry_count;
hypercube = new Dolphinn::Hypercube<double, char>(
*pointset,
entry_count,
entry_length,
/* hypercube_dimension */ floor(log2(entry_count)) - 2,
1,
r);
}
bool prepare_query(const char* entry) {
parsed_entry = parseEntry(entry);
position = 0;
results_distances.clear();
}
size_t query(const char* entry, size_t k) {
results_distances[0].resize(k);
hypercube->m_nearest_neighbors_query(
parsed_entry,
1,
/* Number of nearest neighbors*/ k,
/* max_candidate_count */ max_number_of_points,
results_distances,
1);
int i = 1;
while (i <= k && std::get<0>(results_distances[0][i]) != -1) {
i++;
}
return i - 1;
}
size_t query_result(void) {
if (position < results_distances[0].size()) {
auto elem = std::get<0>(results_distances[0][position++]);
return elem;
} else return SIZE_MAX;
}
void end_query(void) {
}
}