Skip to content

Commit cef4758

Browse files
committed
Changed input so it is always read from file, modified constructors
1 parent 9bb6bc9 commit cef4758

File tree

4 files changed

+118
-21
lines changed

4 files changed

+118
-21
lines changed

.idea/CompressedStacks.cpp.iml

Lines changed: 2 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

include/problem.hpp

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <vector>
1212
#include <string>
1313
#include <memory>
14+
#include <exception>
1415

1516
/*==============================================================================
1617
Class : abstract, template (T context, D datas)
@@ -26,8 +27,9 @@ class Problem{
2627

2728
public:
2829
// Members functions
29-
Problem<T,D>(std::string fileName, int size);
30-
Problem<T,D>(std::string fileName, int size, int space, int buffer);
30+
Problem<T,D>(std::string fileName);
31+
//Problem<T,D>(std::string fileName, int size);
32+
//Problem<T,D>(std::string fileName, int size, int space, int buffer);
3133
virtual ~Problem<T,D>() {}
3234

3335
// Running the stack
@@ -54,6 +56,7 @@ class Problem{
5456
protected:
5557
void initStackIntern();
5658
std::vector<std::string> readLine();
59+
std::vector<std::string> readHeader();
5760
int mIndex;
5861

5962
private:
@@ -80,6 +83,44 @@ class Problem{
8083
Constructors : with NormalStack or CompressedStack
8184
==============================================================================*/
8285
template <class T, class D>
86+
Problem<T,D>::Problem(std::string fileName)
87+
: mIndex(0)
88+
, mContext(nullptr) {
89+
mInput.open(fileName, std::ifstream::in);
90+
91+
std::vector<std::string> parameters=readHeader();
92+
93+
int n,p,b;
94+
bool foundP=false;
95+
bool foundBuffer=false;
96+
97+
for(unsigned int i=0;i<parameters.size();i=i+2)
98+
{
99+
if(parameters[i].compare("n")==0) n=stoi(parameters[i+1]);
100+
if(parameters[i].compare("p")==0)
101+
{
102+
foundP=true;
103+
p=stoi(parameters[i+1]);
104+
}
105+
if(parameters[i].compare("b")==0)
106+
{
107+
foundBuffer=true;
108+
b=stoi(parameters[i+1]);
109+
}
110+
}
111+
112+
// std::cout << "reading all shit n,p,b,foundP,foundBuffer "<<n<<" "<<p<<" "<<b<<" "<<foundP<<" "<<foundBuffer<<std::endl;
113+
114+
if(foundBuffer&&!foundP)throw("Problem<T,D>::Problem(std::string fileName), wrong header format ");
115+
if(!foundP) mStack= std::shared_ptr<Stack<T,D>> (new NormalStack<T,D> (n)); //space not provided, normal stack
116+
else // space was provided, compressed stack
117+
{
118+
if(!foundBuffer) b=0;
119+
mStack = std::shared_ptr<Stack<T,D>> (new CompressedStack<T,D> (n, p, b, mContext));
120+
}
121+
}
122+
123+
/*template <class T, class D>
83124
Problem<T,D>::Problem(std::string fileName, int size)
84125
: mIndex(0)
85126
, mContext(nullptr)
@@ -94,7 +135,8 @@ Problem<T,D>::Problem(std::string fileName, int size, int space, int buffer)
94135
mInput.open(fileName, std::ifstream::in);
95136
std::streampos position = mInput.tellg();
96137
mStack = std::shared_ptr<Stack<T,D>> (new CompressedStack<T,D> (size, space, buffer, mContext));
97-
}
138+
139+
}*/
98140

99141
/*==============================================================================
100142
IO : toString, print, println
@@ -138,6 +180,25 @@ std::vector<std::string> Problem<T,D>::readLine(){
138180
return line;
139181
}
140182

183+
template <class T, class D>
184+
std::vector<std::string> Problem<T,D>::readHeader(){
185+
std::string str;
186+
std::vector<std::string> line;
187+
size_t pos=std::string::npos;
188+
189+
getline(mInput,str); // to read the first HEADER LINE
190+
191+
getline(mInput,str);
192+
while (str.compare("/HEADER ")!=0){
193+
pos=str.find_first_of(" ");
194+
line.push_back(str.substr(0,pos));
195+
line.push_back(str.substr(pos+1,str.size()-pos-1));
196+
getline(mInput,str);
197+
198+
}
199+
return line;
200+
}
201+
141202

142203
/*==============================================================================
143204
Stack Functions: run, push, pop, top
@@ -150,7 +211,7 @@ void Problem<T,D>::run(){
150211
if ( (line.front()== "-1") || (line.front()=="") ) {
151212
break;
152213
}
153-
D data = readInput(line);
214+
D data = readInput(line);
154215
mIndex++; // Might have to move
155216
while ( (emptystack()) && (popCondition(data)) ) {
156217
Data<T,D> elt = pop();
@@ -161,6 +222,7 @@ void Problem<T,D>::run(){
161222
pushAction(elt);
162223
push(elt);
163224
}
225+
164226
}
165227
}
166228

src/createTestInput.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ void createTestInput::createTestInputFiles(int code, string fileName,int n,int p
1414
srand(time(NULL));
1515

1616
// First write the problem parameters
17-
outfile << n << "," << p << endl;
17+
outfile << "HEADER " << endl;
18+
outfile <<"n "<< n<< endl;
19+
outfile <<"p "<< p<< endl;
20+
outfile << "/HEADER " << endl;
1821

1922
switch(code){
2023
case 0 : // push only test

src/main.cpp

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,28 @@
1414
==============================================================================*/
1515
class Instance: public Problem<int,int>{
1616
public:
17-
Instance(std::string filePath, int size):Problem<int,int>(filePath, size){}
18-
Instance(std::string filePath, int size, int space, int buffer):Problem<int,int>(filePath, size, space, buffer){}
17+
Instance(std::string filePath):Problem<int,int>(filePath){ }
18+
/* Instance(std::string filePath, int size):Problem<int,int>(filePath, size){
19+
20+
}
21+
Instance(std::string filePath, int size, int space, int buffer):Problem<int,int>(filePath, size, space, buffer){
22+
}*/
1923
private:
2024
// Functions to run the stack
2125
int readInput(std::vector<std::string> line){
2226
int value = std::stoi(line[0]);
2327
setContext(std::stoi(line[1]));
28+
2429
return value;
2530

2631
}
2732
std::shared_ptr<int> initStack(){
2833
std::shared_ptr<int> context (new int (10));
29-
std::vector<std::string> line = readLine(); // Temporary measure to get rid of the first line
34+
3035
return context;
3136
}
3237
bool popCondition(int data){
33-
if (getContext() > 0) {
38+
if ((getContext() > 0) && !emptystack() ){
3439
return true;
3540
}
3641
return false;
@@ -40,41 +45,69 @@ class Instance: public Problem<int,int>{
4045
}
4146
bool pushCondition(int data){
4247
if (data > 0) {
48+
4349
return true;
4450
}
4551
return false;
4652
}
4753
void pushAction(Data<int,int> elt){
48-
std::cout << "Implement mPushAction for your instance" << std::endl;
54+
//std::cout << "Implement mPushAction for your instance" << std::endl;
4955
}
5056
};
5157

5258
/*==============================================================================
5359
Test functions
5460
==============================================================================*/
5561
// Test problem
56-
void testProblem()
62+
void testProblem(std::string filePath)
5763
{
58-
std::string filePath = "../instances/pushOnlyInput.csv";
5964

6065
// Test on normal stack
6166
// Instance testNS(filePath, 1000);
6267
// testNS.run();
6368
// testNS.println();
6469

6570
// Test on CompressedStack
66-
Instance testCS(filePath, 1000, 3, 0);
67-
testCS.println();
71+
Instance testCS(filePath);
72+
// testCS.println();
6873
testCS.run();
69-
testCS.println();
74+
75+
// testCS.println();
7076
}
7177

7278
/*==============================================================================
7379
Main function
7480
==============================================================================*/
7581
// Main //int main(int argc, char const *argv[]) {
76-
int main() {
77-
testProblem();
82+
int main(int argc, char *argv[]) {
83+
84+
// argv[0] is the file name, argv[1] is the code of what is to be done, 0 run the example contained in the file, others are create input: 1 push only, 2 CT, 3 CH
85+
createTestInput ct=createTestInput();
86+
std::string filename=argv[1];
87+
88+
switch(atoi(argv[2]))
89+
{
90+
case 0:
91+
testProblem(filename);
92+
break;
93+
case 1:
94+
ct.createTestInputFiles(0,filename,atoi(argv[3]),atoi(argv[4]), atoi(argv[5]), atoi(argv[6]), atof(argv[7]) );
95+
96+
break;
97+
case 2:
98+
ct.createTestInputFiles(1,filename,atoi(argv[3]),atoi(argv[4]));
99+
100+
break;
101+
case 3:
102+
ct.createTestInputFiles(2,filename,atoi(argv[3]),atoi(argv[4]), atoi(argv[5]), atoi(argv[6]));
103+
104+
break;
105+
default:
106+
std::cout << "WRONG PROGRAM CODE";
107+
exit(-1);
108+
break;
109+
}
110+
78111

79112
// Intances generation, please comment when not in use
80113
/* createTestInput ct=createTestInput();
@@ -83,5 +116,6 @@ int main() {
83116
ct.createTestInputFiles(2,"/home/yago/code/CompressedStacks.cpp/instances/CHInput.csv",100,3,-1, 11);
84117
*/
85118

119+
86120
return 0;
87121
}

0 commit comments

Comments
 (0)