Skip to content

Commit 29deaa4

Browse files
committed
Merge New Instance format
2 parents c39838d + cef4758 commit 29deaa4

File tree

9 files changed

+120
-2094
lines changed

9 files changed

+120
-2094
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/compare.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ template <class T, class D>
1818
class CompareStacks: public Problem<T,D>{
1919
public:
2020
// Members functions
21-
CompareStacks<T,D>(std::string fileName, int size, int space, int buffer);
21+
CompareStacks<T,D>(std::string fileName);
2222

2323
// Compare the stacks
2424
void runCompare(int buffer = 0);
@@ -51,9 +51,9 @@ class CompareStacks: public Problem<T,D>{
5151
==============================================================================*/
5252

5353
template <class T, class D>
54-
CompareStacks<T,D>::CompareStacks(std::string fileName, int size, int space, int buffer)
55-
: Problem<T,D>(fileName, size, space, buffer)
56-
, mNormalStack(new NormalStack<T,D> (size)){
54+
CompareStacks<T,D>::CompareStacks(std::string fileName)
55+
: Problem<T,D>(fileName)
56+
, mNormalStack(new NormalStack<T,D> ()){
5757
}
5858

5959
/*==============================================================================

include/normalStack.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class NormalStack: public Stack<T,D>{
2525

2626
public:
2727
// Constructors
28-
NormalStack<T,D>(int size);
28+
NormalStack<T,D>();
2929

3030
// IO
3131
std::string toString();
@@ -62,7 +62,7 @@ class NormalStack: public Stack<T,D>{
6262
Constructors
6363
==============================================================================*/
6464
template <class T, class D>
65-
NormalStack<T,D>::NormalStack(int size){
65+
NormalStack<T,D>::NormalStack(){
6666
Explicit<T,D> datas;
6767
datas = initExplicit<T,D>();
6868
mDatas = datas;

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)
@@ -30,8 +31,9 @@ class Problem{
3031

3132
public:
3233
// Members functions
33-
Problem<T,D>(std::string fileName, int size);
34-
Problem<T,D>(std::string fileName, int size, int space, int buffer);
34+
Problem<T,D>(std::string fileName);
35+
//Problem<T,D>(std::string fileName, int size);
36+
//Problem<T,D>(std::string fileName, int size, int space, int buffer);
3537
virtual ~Problem<T,D>() {}
3638

3739
// Running the stack
@@ -58,6 +60,7 @@ class Problem{
5860
protected:
5961
void initStackIntern();
6062
std::vector<std::string> readLine();
63+
std::vector<std::string> readHeader();
6164
int mIndex;
6265

6366
private:
@@ -84,6 +87,44 @@ class Problem{
8487
Constructors : with NormalStack or CompressedStack
8588
==============================================================================*/
8689
template <class T, class D>
90+
Problem<T,D>::Problem(std::string fileName)
91+
: mIndex(0)
92+
, mContext(nullptr) {
93+
mInput.open(fileName, std::ifstream::in);
94+
95+
std::vector<std::string> parameters=readHeader();
96+
97+
int n,p,b;
98+
bool foundP=false;
99+
bool foundBuffer=false;
100+
101+
for(unsigned int i=0;i<parameters.size();i=i+2)
102+
{
103+
if(parameters[i].compare("n")==0) n=stoi(parameters[i+1]);
104+
if(parameters[i].compare("p")==0)
105+
{
106+
foundP=true;
107+
p=stoi(parameters[i+1]);
108+
}
109+
if(parameters[i].compare("b")==0)
110+
{
111+
foundBuffer=true;
112+
b=stoi(parameters[i+1]);
113+
}
114+
}
115+
116+
// std::cout << "reading all shit n,p,b,foundP,foundBuffer "<<n<<" "<<p<<" "<<b<<" "<<foundP<<" "<<foundBuffer<<std::endl;
117+
118+
if(foundBuffer&&!foundP)throw("Problem<T,D>::Problem(std::string fileName), wrong header format ");
119+
if(!foundP) mStack= std::shared_ptr<Stack<T,D>> (new NormalStack<T,D> ()); //space not provided, normal stack
120+
else // space was provided, compressed stack
121+
{
122+
if(!foundBuffer) b=0;
123+
mStack = std::shared_ptr<Stack<T,D>> (new CompressedStack<T,D> (n, p, b, mContext));
124+
}
125+
}
126+
127+
/*template <class T, class D>
87128
Problem<T,D>::Problem(std::string fileName, int size)
88129
: mIndex(0)
89130
, mContext(nullptr)
@@ -98,7 +139,8 @@ Problem<T,D>::Problem(std::string fileName, int size, int space, int buffer)
98139
mInput.open(fileName, std::ifstream::in);
99140
std::streampos position = mInput.tellg();
100141
mStack = std::shared_ptr<Stack<T,D>> (new CompressedStack<T,D> (size, space, buffer, mContext));
101-
}
142+
143+
}*/
102144

103145
/*==============================================================================
104146
IO : toString, print, println
@@ -142,6 +184,25 @@ std::vector<std::string> Problem<T,D>::readLine(){
142184
return line;
143185
}
144186

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

146207
/*==============================================================================
147208
Stack Functions: run, push, pop, top
@@ -156,7 +217,7 @@ void Problem<T,D>::run(){
156217
if ( (line.front()== "-1") || (line.front()=="") ) {
157218
break;
158219
}
159-
D data = readInput(line);
220+
D data = readInput(line);
160221
mIndex++; // Might have to move
161222
while ((!emptystack()) && (popCondition(data))) {
162223
Data<T,D> elt = pop();
@@ -167,6 +228,7 @@ void Problem<T,D>::run(){
167228
pushAction(elt);
168229
push(elt);
169230
}
231+
170232
}
171233
}
172234

0 commit comments

Comments
 (0)