-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathData.cpp
More file actions
127 lines (112 loc) · 2.52 KB
/
Data.cpp
File metadata and controls
127 lines (112 loc) · 2.52 KB
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
#include "Data.h"
// There are three labels: 'L', 'R', 'B'
const string Data::LABEL = "LRB";
// Default constructor.
Data::Data()
{
//label = '';
attribute.clear();
}
// Constructor with full params.
Data::Data(char _label, vector<int> _att, int _index) : label(_label), attribute(_att), index(_index) {}
// Constructor with one line string.
Data::Data(string line)
{
stringstream ss(line);
ss >> label;
int num;
attribute.clear();
char temp;
while (ss >> temp)
{
ss >> num;
attribute.push_back(num);
}
}
// Destructor.
Data::~Data()
{
attribute.clear();
}
void Data::getAttFromString(string line)
{
stringstream ss(line);
int num;
attribute.clear();
char temp;
while (ss >> num)
{
ss >> temp;
attribute.push_back(num);
}
}
// toString function returns a string that contains
// label and all the values of attribute vector.
string Data::toString()
{
stringstream ss;
ss << label;
for (int num : attribute)
{
ss << ',' << num;
}
string str;
getline(ss, str);
return str;
}
// loadDataSet function return vector of Data that read from file.
DataSet *loadDataSet(string fileName)
{
DataSet *dataSet = new DataSet();
ifstream file(fileName);
string line;
while (getline(file, line))
{
Data* temp = new Data(line);
temp->index = dataSet->size();
dataSet->push_back(temp);
}
return dataSet;
}
DataSet *loadDataSetBuff(string fileName, char label, int scale)
{
DataSet *dataSet = new DataSet();
ifstream file(fileName);
string line;
while (getline(file, line))
{
Data* temp = new Data(line);
temp->index = dataSet->size();
if (line[0] == label)
{
for (int i = 1; i < scale; i++)
{
dataSet->push_back(temp);
}
}
dataSet->push_back(temp);
}
return dataSet;
}
DataSet *loadDataTest(string filename)
{
DataSet *dataset = new DataSet();
ifstream file(filename);
string line;
while (getline(file, line))
{
dataset->push_back(new Data());
dataset->back()->getAttFromString(line);
}
return dataset;
}
Data* Data::clone() {
return new Data(this->label, this->attribute, this->index);
}
DataSet *cloneDataSet(DataSet *dataSet) {
DataSet *newDataSet = new DataSet();
for(int i = 0; i < dataSet->size(); i++) {
newDataSet->push_back(dataSet->at(i)->clone());
}
return newDataSet;
}