-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtility.h
70 lines (62 loc) · 1.52 KB
/
Utility.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
#ifndef __UTILITY_H
#define __UTILITY_H
#include <iostream>
#include <vector>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
#define L 256
static bool read_data( // 读取数据专用函数,同学们不要修改
int n,
int d,
float** &data,
const char* file_name)
{
FILE* fin = fopen(file_name, "r");
if (!fin) {
printf("%s doesn't exist!\n", file_name);
return false;
}
int id;
data = new float*[n];
for (int i = 0; i < n; i++) {
data[i] = new float[d + 1];
fscanf(fin, "%d", &id);
data[i][d] = id; // d号位置用来存储此数据点在源文件中的编号
for (int j = 0; j < d; j++) {
fscanf(fin, "%f", &data[i][j]);
}
}
printf("Finish reading %s\n", file_name);
fclose(fin);
return true;
}
/*
这里可以添加代码
*/
static float getDist(float* query, float* data){
float tempDist=0;
for(int i=0; i<50; ++i)
tempDist += (query[i]-data[i])*(query[i]-data[i]);
return sqrt(tempDist);
}
static float Distance(vector<float> query, vector<float> node, int d){
float dis = 0;
for (int i = 0; i < d; i++){
dis += (query[i] - node[i])*(query[i] - node[i]);
}
return sqrt(dis);
}
static float getDistance(vector<float> query, vector<float> node){
if (query.size() != node.size()){
cout << "Dimension not match" << endl;
exit(1);
}
float dis = 0;
for (int i = 0; i < query.size()-1; i++){
dis += (query[i] - node[i])*(query[i] - node[i]);
}
return sqrt(dis);
}
#endif