-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.cpp
155 lines (132 loc) · 3.57 KB
/
data.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
/*
* Fengji Hou
* New York University
*
* In data.cpp, Member functions of data class are defined. The functions
* include collecting data from data files and identify various sizes of the
* data.
*
*/
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
#include <vector>
#include "data.h"
using namespace std;
// This routine counts how many rows there are in the data file, whether or not
// some of the rows are not data.
int64_t Data::count_rows() {
string data_file_name("./data/" + data_name + ".dat");
ifstream input(data_file_name.c_str());
if(input.fail()) {
throw DataException("Data constructor could not open file " + data_file_name + " !");
return -1;
}
int64_t num_rows = 0;
string temporary; // temporary string used to store the line read by getline()
while ( !input.eof() ) {
getline( input, temporary ); // get and count a line until the line is EOF
if( temporary[0] != 0 ) ++ num_rows;
}
input.close();
return num_rows;
}
// This function reads the data into 2-d vector 'data'.
void Data::collect_data() {
string data_file_name("./data/" + data_name + ".dat");
ifstream input(data_file_name.c_str());
string temporary;
for (int64_t i = 0; i < skip_rows; ++i) {
getline( input, temporary );
}
vector<double> input_array; // used to store the data in an array
double temp;
while(!input.eof()) {
input >> temp;
input_array.push_back(temp);
}
num_col = input_array.size() / data_size; // determining number of columns
data.resize(data_size, vector<double>(num_col, 0));
for (int64_t i = 0; i < data_size; ++i) {
for (int64_t j = 0; j < num_col; ++j) {
data[i][j] = input_array[i*num_col + j];
}
}
input.close();
}
Data::Data() {
}
// constructor
Data::Data(string name): // name of the data
data_name(name)
{
skip_rows = 0;
num_row = count_rows();
data_size = num_row - skip_rows;
if (data_size > 0) {
collect_data();
}
else {
//throw DataException("Data constructor: data file is empty!");
}
}
// constructor
Data::Data(string name, // name of the data
int64_t sr): // how many rows to skip
data_name(name),
skip_rows(sr)
{
num_row = count_rows();
data_size = num_row - skip_rows;
if (data_size > 0) {
collect_data();
}
else {
//throw DataException("Data constructor: data file is empty!");
}
}
// copy constructor
Data::Data(const Data& original):
data_name(original.data_name),
num_col(original.num_col),
num_row(original.num_row),
skip_rows(original.skip_rows),
data_size(original.data_size)
{
if (data_size > 0) {
data.resize(data_size, vector<double>(num_col, 0));
for (int64_t i = 0; i < data_size; ++i) {
for (int64_t j = 0; j < num_col; ++j) {
data[i][j] = original.data[i][j];
}
}
}
}
Data& Data::operator=(const Data& original)
{
data_name = original.data_name;
num_col = original.num_col;
num_row = original.num_row;
skip_rows = original.skip_rows;
data_size = original.data_size;
if (data_size > 0) {
data.resize(data_size, vector<double>(num_col, 0));
for (int64_t i = 0; i < data_size; ++i) {
for (int64_t j = 0; j < num_col; ++j) {
data[i][j] = original.data[i][j];
}
}
}
return *this;
}
// Code for throwing an exception with an error message and catching it outside
DataException::DataException(string ErrorMessage){ // Set the exception message
MessageText = ErrorMessage;
}
string DataException::ExceptionMessage(){
return MessageText;
}