-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.cpp
179 lines (154 loc) · 5.48 KB
/
matrix.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include "matrix.h"
Matrix::Matrix()
{
}
Matrix::~Matrix()
{
for(unsigned int x = 0; x < getDataMatrixWidth(); x++){
for(unsigned int y=0; y < getDataMatrixHeigth(); y++){
delete dataMatrix.at(x)->at(y);
}
delete dataMatrix.at(x);
dataMatrix.at(x)->clear();
}
dataMatrix.clear();
titles.clear();
}
unsigned int Matrix::getDataMatrixWidth() const {return dataMatrix.size();}
unsigned int Matrix::getDataMatrixHeigth() const
{
if(dataMatrix.empty()) return 0;
return dataMatrix.at(0)->size();
}
QString Matrix::getTitle(unsigned int col) const {return titles[col];}
void Matrix::updateTitle(QString text, unsigned int col){titles[col] = text;}
void Matrix::updateDataMatrixValue(QString text, unsigned int x, unsigned int y){dataMatrix.at(x)->at(y)->setData(text);}
Data* Matrix::getDataAt(unsigned int x, unsigned int y) const{return dataMatrix.at(x)->at(y);}
void Matrix::loadData(const QJsonArray& json)
{
for(int x = 0; x < json.size(); x++){
dataMatrix.append(new QVector<Data*>);
QJsonArray data = json.at(x).toObject().value("data").toArray();
if(json.at(x).toObject().value("type").toString() == "Numeric"){
for(int y = 0; y < data.size(); y++)
dataMatrix.at(x)->append(new NumericData(data.at(y).toDouble()));
}
else{
for(int y = 0; y < data.size(); y++)
dataMatrix.at(x)->append(new TextData(data.at(y).toString()));
}
titles.append(json.at(x).toObject().value("title").toString());
}
}
bool Matrix::isNumeric(unsigned int col) const
{
if(dynamic_cast<NumericData*>(getDataAt(col))) return true;
else return false;
}
//void Matrix::printDebug() const
//{
// for(unsigned int y = 0; y < getDataMatrixHeigth(); y++){
// for(unsigned int x = 0; x < getDataMatrixWidth(); x++){
// QString aux;
// if(dynamic_cast<NumericData*>(getDataAt(x))){
// aux = (QString::number(static_cast<NumericData*>(getDataAt(x,y))->getData()));
// }
// else{
// aux = static_cast<TextData*>(getDataAt(x,y))->getData();
// }
// QTextStream(stdout) << aux << " ";
// }
// QTextStream(stdout) << "" << endl;
// }
// for(unsigned int x = 0; x < getDataMatrixWidth(); x++){
// QTextStream(stdout) << getTitle(x) + " ";
// }
// QTextStream(stdout) << "" << endl;
//}
void Matrix::addRowMatrix(){
for(unsigned int x = 0; x < getDataMatrixWidth(); x++){
if(isNumeric(x))
dataMatrix.at(x)->append(new NumericData);
else
dataMatrix.at(x)->append(new TextData);
}
}
void Matrix::deleteRowMatrix(unsigned int row)
{
for(unsigned int x = 0; x < getDataMatrixWidth(); x++){ //removes the data from the selected row
Data* tmp = dataMatrix.at(x)->at(row);
delete tmp;
dataMatrix.at(x)->erase(dataMatrix.at(x)->begin() + row); //this automatically reallocates all the data in the following rows to keep the Matrix compact
}
if(dataMatrix.at(0)->isEmpty()){ //handles the final removal of all columns
for(int x = getDataMatrixWidth()-1; x >= 0; x--){
deleteColumnMatrix(x);
}
}
}
void Matrix::addColumnMatrix(bool isNumeric)
{
titles.push_back("");
if(isNumeric){
if(dataMatrix.isEmpty())
dataMatrix.append(new QVector<Data*>(1, new NumericData)); //adds new QVector<Data*> with 1 default Data
else{
dataMatrix.append(new QVector<Data*>); //adds new QVector<Data*> with default NumericDatas, as many as matrix heigth
for(unsigned int y = 0; y < getDataMatrixHeigth(); y++)
dataMatrix.at(getDataMatrixWidth()-1)->push_back(new NumericData);
}
}
else{
if(dataMatrix.isEmpty())
dataMatrix.append(new QVector<Data*>(1, new TextData)); //adds new QVector<Data*> with 1 default Data
else{
dataMatrix.append(new QVector<Data*>); //adds new QVector<Data*> with default TextDatas, as many as matrix heigth
for(unsigned int y = 0; y < getDataMatrixHeigth(); y++)
dataMatrix.at(getDataMatrixWidth()-1)->push_back(new TextData);
}
}
}
void Matrix::deleteColumnMatrix(unsigned int col){
QVector<Data*>* tmp = dataMatrix.at(col);
delete tmp;
dataMatrix.erase(dataMatrix.begin() + col);
titles.erase(titles.begin() + col);
}
unsigned int Matrix::getNumberOfNumerics() const
{
unsigned int count = 0;
for(unsigned int col = 0; col < getDataMatrixWidth(); col++){
if(dynamic_cast<NumericData*>(getDataAt(col, 0)))
count++;
}
return count;
}
unsigned int Matrix::getNumberOfTexts() const
{
unsigned int count = 0;
for(unsigned int col = 0; col < getDataMatrixWidth(); col++){
if(dynamic_cast<TextData*>(getDataAt(col, 0)))
count++;
}
return count;
}
QVector<int>* Matrix::getNumericDataIndexes() const
{
QVector<int>* numericColumns = new QVector<int>;
for(unsigned int col = 0; col < getDataMatrixWidth(); col++){
if(isNumeric(col)){
numericColumns->append(col);
}
}
return numericColumns;
}
QVector<int>* Matrix::getTextDataIndexes() const
{
QVector<int>* textColumns = new QVector<int>;
for(unsigned int col = 0; col < getDataMatrixWidth(); col++){
if(!isNumeric(col)){
textColumns->append(col);
}
}
return textColumns;
}