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
3132public:
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{
5860protected:
5961 void initStackIntern ();
6062 std::vector<std::string> readLine ();
63+ std::vector<std::string> readHeader ();
6164 int mIndex ;
6265
6366private:
@@ -84,6 +87,44 @@ class Problem{
8487 Constructors : with NormalStack or CompressedStack
8588==============================================================================*/
8689template <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>
87128Problem<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