-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
173 lines (146 loc) · 3.67 KB
/
main.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
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
#include <iostream>
#include <array>
#include <vector>
#include <fstream>
#include <sstream>
#include <string>
#include <cstring>
#include <regex>
#include "kdtree.h"
using namespace std;
struct Descriptor {
//info about a given location
string name;
int price;
};
// user-defined point type
// inherits array in order to use operator[]
class Location: public array <double, 2> {
public:
// dimension of space (or "k" of k-d tree)
// KDTree class accesses this member
static const int DIM = 2;
Descriptor d;
// the constructors
Location() {}
Location(double lon, double lat) {
(*this)[0] = lon;
(*this)[1] = lat;
}
Location(double lon, double lat, Descriptor &d) {
(*this)[0] = lon;
(*this)[1] = lat;
this->d = d;
}
};
struct Filter {
regex query;
int prices;
bool operator()(Location l){
if(!regex_search(l.d.name, query))
return false;
if(!((1 << (l.d.price - 1)) & prices))
return false;
return true;
}
};
void usage(string programname, int exitcode){
cout << "Usage: " << programname << " flags... longitude latitude\n"
<< "Flags: \n"
<< "\t-h : shows this prompt\n"
<< "\t-f FILE : chooses the file to load locations from\n"
<< "\t-k NUMBER : select how many to return\n"
<< "\t-r RADIUS : find within this radius (miles)\n"
<< "\t-q QUERY : find similar to this (regex)\n"
<< "\t-p PRICES : find with selected prices (bitwise mask from 0 to 15, 1 = $, 8 = $$$$)\n";
//TODO additional args for when FILTER is implemented
exit(exitcode);
}
static const char SEP = ',';
int main(int argc, char** argv) {
double r = -1;
int k = 20;
int prices = 15;
regex reg(".*");
// PARSE
int argind = 1;
string filename = "locations.csv";
string programname = string(argv[0]);
while (argind < argc && strlen(argv[argind]) > 1 && argv[argind][0] == '-' && strlen(argv[argind]) < 3) {
char *arg = argv[argind++];
switch (arg[1]) {
case 'h':
usage(programname, 0);
break;
case 'f': {
char* format = argv[argind++];
filename = string(format);
}
break;
case 'k': {
char* num = argv[argind++];
k = atoi(num);
}
break;
case 'r': {
char* dist = argv[argind++];
r = atof(dist);
}
break;
case 'q': {
reg = regex(argv[argind++], regex_constants::icase | regex_constants::ECMAScript);
}
break;
case 'p': {
char* num = argv[argind++];
prices = atoi(num);
}
break;
default:
usage(programname, 1);
break;
}
}
if(argc - argind < 2) usage(programname, 1);
double longSearch = atof(argv[argind++]);
double latSearch = atof(argv[argind]);
// READ DATA FILE
ifstream str(filename);
vector<Location> locs;
string line;
while(getline(str, line)){
stringstream ss(line);
string word;
getline(ss, word, SEP);
double lon = stod(word);
getline(ss, word, SEP);
double lat = stod(word);
getline(ss, word, SEP);
string name = word;
getline(ss, word);
int price = stoi(word);
Descriptor d = {name, price}; //TODO add price here
Location l(lon, lat, d);//TODO fix long and lat switchup
locs.push_back(l);
}
// SETUP
kdt::KDTree <Location> kdtree(locs);
Location query(longSearch, latSearch);//passed in params
vector<int> result;
Filter f = {reg, prices};
// SEARCH
if(r < 0){
// k-nearest neigbors search
result = kdtree.knnSearch(query, k, f);
} else {
// radius search
result = kdtree.radiusSearch(query, r, f);
result = kdtree.knnSearch(query, (int) result.size() < k ? result.size() : k, f);//reduces number to k and hacky orders them
}
// OUTPUT
for(auto it = result.begin(); it != result.end(); it++){
cout << *it << "\n";//outputs the line number
}
return 0;
}