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
2728public:
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{
5456protected:
5557 void initStackIntern ();
5658 std::vector<std::string> readLine ();
59+ std::vector<std::string> readHeader ();
5760 int mIndex ;
5861
5962private:
@@ -80,6 +83,44 @@ class Problem{
8083 Constructors : with NormalStack or CompressedStack
8184==============================================================================*/
8285template <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>
83124Problem<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
0 commit comments