-
Notifications
You must be signed in to change notification settings - Fork 1
/
Matrix.cpp
85 lines (75 loc) · 2.15 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
#include "Matrix.h"
/***
*
* @param matrix_vec is a vector that holds ths input of the matrix.
*/
Matrix::Matrix(vector<string> matrix_vec) {
if (build_matrix(matrix_vec) == false) {
n = -1;
}
}
// this function bulds for each index a vertex in the vertexMap
bool Matrix::mapBuilder(vector<string> line_vec, int i) {
int j;
int vec_length = line_vec.size();
if(vec_length == n) {
State *temp_state;
string vertex_index;
for (j = 0; j < vec_length; ++j) {
vertex_index = to_string(i) + "," + to_string(j);
temp_state = new State(vertex_index, stod(line_vec.at(j)));
vertexMap.insert(pair<string, State *>(vertex_index, temp_state));
}
return true;
} else {
return false;
}
}
//the builder of the matrix, uses a friend functions
bool Matrix::build_matrix(vector<string> matrix_vec) {
int i;
int sizeOfVector = matrix_vec.size() - 2;
vector<string> line;
this->n = sizeOfVector;
for (i = 0; i < sizeOfVector; i++) {
line = buildLine(matrix_vec.at(i));
if (mapBuilder(line, i) == false) {
return false;
}
}
line = buildLine(matrix_vec.at(sizeOfVector));
initialVertex = vertexMap[line.at(0) + "," + line.at(1)];
line = buildLine(matrix_vec.at(++sizeOfVector));
goalVertex = vertexMap[line.at(0) + "," + line.at(1)];
return true;
}
//this function gets a line and puts it into a vector of strings
vector<string> Matrix::buildLine(string line) {
int endNum = 0;
int start = 0;
vector<string> line_vec;
if (line[line.length() - 1] != ',') {
line = line + ",";
}
while (true) {
endNum = line.find(',', start);
if (endNum != -1) {
line_vec.push_back(line.substr(start, endNum-start));
start = endNum + 1;
} else {
return line_vec;
}
}
}
int Matrix::getN() {
return n;
}
unordered_map<string, State*>* Matrix::getVertexMap() {
return &this->vertexMap;
}
State *Matrix::getInitialState() {
return this->initialVertex;
}
State *Matrix::getGoalState() {
return this->goalVertex;
}