-
Notifications
You must be signed in to change notification settings - Fork 0
/
stl_seq_knn.cpp
65 lines (56 loc) · 1.75 KB
/
stl_seq_knn.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
/*
C++ STL implementation of sequential knn.
*/
// Author : Yohannis Kifle Telila.
// Date : 17/02/2022
// Desc : This file contain code for calculating KNN of a 2D points using sequantial
// patterns using only C++ STL.
// include necessary libraries.
#include <iostream>
#include <thread>
#include <cstring>
#include "src/utimer.cpp"
#include "src/utils.h"
using namespace std;
int main(int argc, char const *argv[]) {
if (argc < 3) {
cerr << "use: " << argv[0] << " k-value input_file_path -d [optional -d flag to output only nw and running time]\n";
return -1;
}
int k = atoi(argv[1]); // k value.
string filepath = argv[2]; // input file path.];
string d = ""; // parameter for outputing only nw and running time.
if(argv[3] != NULL){
d = string(argv[3]);
}
vector<knn_result> knn_seq_result; // what will store the sequential result.
vector<point> points;// where the points are going to be stored [the one read from]
int points_size;
long seq_read;
{
utimer t_seq("File read", &seq_read);
// Reading the file and storing the points in the vector.
points = read2dpoints(filepath);
points_size = points.size();
}
long seq_time;
// Computing knn in sequantial.
{
utimer t_seq("STL Sequential KNN", &seq_time);
// sequential knn
vector<int> res;
for(int i=0;i<points.size();i++){
knn_result res;res.index = i;
res.knn_index = get_knn(points, points.size(), i, k);
knn_seq_result.push_back(res);
}
}
// printing the result.
long write_ftime;
{
utimer t_seq("File write took: ", &write_ftime);
// write the result to file.
print_knn_result(knn_seq_result,k,seq_time,1, argv[0], d); // 1 is nw.
}
return 0;
}