-
Notifications
You must be signed in to change notification settings - Fork 1
/
DataInstance.cpp
54 lines (43 loc) · 1.09 KB
/
DataInstance.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
#include "DataInstance.h"
using namespace std;
DataInstance::DataInstance(int _id, const std::vector<int>& _data, int _class) : id(_id), data(_data), label(_class), distance(-1){
}
void DataInstance::set_distance(const DataInstance& query){
distance = 0;
unsigned int P = data.size();
for (unsigned int i = 0; i < P; i++){
distance += (query[i] - data[i]) * (query[i] - data[i]);
}
}
int DataInstance::get_distance() const {
return distance;
}
int DataInstance::get_id() const{
return id;
}
int DataInstance::get_class() const{
return label;
}
int DataInstance::size() const{
return data.size();
}
bool DataInstance::operator<(const DataInstance& other) const {
return distance < other.get_distance();
}
const int& DataInstance::operator[](const int& i) const{
return data[i];
}
std::ostream& operator<<(std::ostream& os, const DataInstance& v){
os << v.get_id() << " --> [";
unsigned int P = v.size();
if (0 == P){
os << "]";
}else{
for (unsigned int i = 0; i < P - 1; i++){
os << v[i] << ", ";
}
os << v[P - 1] << "]";
}
os << " class: " << v.get_class() << endl;
return os;
}