-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyClientHandler.h
115 lines (97 loc) · 3.01 KB
/
MyClientHandler.h
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
#ifndef PROJB_MYCLIENTHANDLER_H
#define PROJB_MYCLIENTHANDLER_H
#include "./interface/ClientHandler.h"
#include <vector>
#include "searchable/Matrix.h"
#include <string>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include "Point.h"
#include "searchAlg/Bestfs.h"
using std::string;
using std::ofstream;
using std::ifstream;
using project::Pointm;
using std::vector;
class MyClientHandler : public ClientHandler<Matrix<Pointm> *, list<State<Pointm> *>> {
vector<Pointm> *parserLine(int line, string s);
Pointm *specifigPoint(string line) {
string x, y;
int delimiter = line.find(",");
x = line.substr(0, delimiter);
y = line.substr(delimiter + 1, line.size());
return new Pointm(stoi(x), stoi(y), 0);
}
Matrix<Pointm> *getMatrix(ifstream &input);
public:
MyClientHandler(Solver<Matrix<Pointm> *, list<State<Pointm> *>> *solver, CacheManager *cacheManager)
: ClientHandler(solver, cacheManager) {
}
//Solver<Matrix<Pointm>, list<State<Pointm> *> *>
void handleClient(string inputFile, string outputFile) override;
};
void MyClientHandler::handleClient(string inputFile, string outputFile) {
vector<Matrix<Pointm> *> matrixes;
string s;
ifstream input;
input.open(inputFile);
if (!input.is_open()) {
throw "Error in input file";
}
ofstream output;
output.open(outputFile);
if (!output.is_open()) {
throw "Error in output file";
}
input >> s;
int numOfGraphs = stoi(s);
//load all graphs from inputStream
for (int graphIndex = 0; graphIndex < numOfGraphs; graphIndex++) {
Matrix<Pointm> *m = this->getMatrix(input);
matrixes.push_back(m);
}
for (auto *tmpMatrix:matrixes) {
auto ans = this->solver->solve(tmpMatrix);//todo save the answer
}
}
vector<Pointm> *MyClientHandler::parserLine(int line, string s) {
vector<Pointm> *vector = new ::vector<Pointm>;
string buffer;
int col = 0;
for (char c:s) {
if (c == ',') {
vector->push_back(Pointm(col, line, stoi(buffer)));
col++;
buffer = "";
continue;
}
buffer += c;
}
vector->push_back(Pointm(col, line, stoi(buffer)));
return vector;
}
Matrix<Pointm> *MyClientHandler::getMatrix(ifstream &istream) {
string s = "";
istream >> s;//todo heg,width of h=w?
int size = stoi(s);
s = "";
istream >> s;
auto *inition = this->specifigPoint(s);
s = "";
istream >> s;
auto *goal = this->specifigPoint(s);
auto matrix = new State<Pointm> **[size];
for (int i = 0; i < size; i++) {//get the graph
auto vector = this->parserLine(i, s);
matrix[i] = new State<Pointm> *[size];//initon line
for (int j = 0; j < size; j++) {//get line of matrix
matrix[i][j] = new State<Pointm>(vector->at(j));
}
s = "";
istream >> s;
}
return new Matrix<Pointm>(matrix, size, size, *inition, *goal);
}
//
#endif //PROJB_MYCLIENTHANDLER_H