55#include " stack.hpp"
66#include " compressedStack.hpp"
77#include " normalStack.hpp"
8- #include < stdbool.h>
8+ // #include <stdbool.h>
99#include < fstream>
10+ #include < vector>
1011#include < string>
1112
1213template <class T , class D >
1314class Problem
1415{
15- /* * Typedefs for handling pointer-to-function **/
16- typedef void (Problem::*ProblemVoidFn)();
17- typedef D (Problem::*ProblemDataFn)();
18- typedef bool (Problem::*ProblemBoolFn)();
19-
2016public:
2117 // Problem<T,D>(std::string fileName);
2218 Problem<T,D>(std::string fileName, int size);
2319 Problem<T,D>(std::string fileName, int size, int space);
2420
2521 // Running the stack
2622 void run ();
23+ void push (Data<D>);
24+ Data<D> pop ();
25+ Data<D> top (int k);
26+ bool emptystack ();
2727
2828 // Setters
29- void setReadInput (D (*functionPointer)());
30- void setInitStack (void (*functionPointer)());
31- void setPopCondition (ProblemBoolFn functionPointer);
32- void setPopAction (void (*functionPointer)());
33- void setPushCondition (bool (*functionPointer)());
34- void setPushAction (void (*functionPointer)());
35-
3629 void setOutput (std::string fileName);
30+ void setContext (T context);
31+
32+ // Getters
33+ T getContext ();
3734
3835 // IO
3936 std::string toString ();
4037 void print ();
4138 void println ();
4239
40+ protected:
41+ std::vector<std::string> readLine ();
42+ int mIndex ;
43+
4344private:
4445 // Input/Ouput
45- std::ifstream* mInput ;
46+ std::ifstream mInput ;
4647 std::ofstream* mOutput ; // output file is optional
4748
4849 // Stack Functions: defined by user
49- D (*mReadInput )();
50- void (*mInitStack )();
51- bool (*mPopCondition )();
52- void (*mPopAction )();
53- bool (*mPushCondition )();
54- void (*mPushAction )();
50+ virtual D readInput (std::vector<std::string> line) = 0;
51+ virtual void initStack () = 0;
52+ virtual bool popCondition (D data) = 0;
53+ virtual void popAction (Data<D> elt) {};
54+ virtual bool pushCondition (D data) = 0;
55+ // virtual void pushAction() = 0;
56+ virtual void pushAction (Data<D> elt) {};
5557
5658 // Problem internal during run
5759 T* mContext ;
58- int mIndex ;
5960
6061 // Stack: Normal or Compressed
6162 Stack<D>* mStack ;
@@ -65,40 +66,21 @@ class Problem
6566template <class T , class D >
6667Problem<T,D>::Problem(std::string fileName, int size)
6768{
68- std::ifstream ifstr;
69- ifstr.open (fileName, std::ifstream::in);
70- mInput = &ifstr;
69+ mInput .open (fileName, std::ifstream::in);
7170 mOutput = nullptr ;
7271
73- mInitStack = nullptr ;
74- mReadInput = nullptr ;
75- mPopCondition = nullptr ;
76- mPopAction = nullptr ;
77- mPushCondition = nullptr ;
78- mPushAction = nullptr ;
79-
8072 mContext = nullptr ;
8173 mIndex = 0 ;
8274
83-
8475 mStack = new NormalStack<T> (size);
8576}
8677
8778template <class T , class D >
8879Problem<T,D>::Problem(std::string fileName, int size, int space)
8980{
90- std::ifstream ifstr;
91- ifstr.open (fileName, std::ifstream::in);
92- mInput = &ifstr;
81+ mInput .open (fileName, std::ifstream::in);
9382 mOutput = nullptr ;
9483
95- mInitStack = nullptr ;
96- mReadInput = nullptr ;
97- mPopCondition = nullptr ;
98- mPopAction = nullptr ;
99- mPushCondition = nullptr ;
100- mPushAction = nullptr ;
101-
10284 mContext = nullptr ;
10385 mIndex = 0 ;
10486
@@ -128,51 +110,95 @@ void Problem<T,D>::println()
128110 std::cout << std::endl;
129111}
130112
113+ template <class T , class D >
114+ std::vector<std::string> Problem<T,D>::readLine()
115+ {
116+ std::string str;
117+ std::vector<std::string> line;
118+ size_t pos=std::string::npos;
119+ getline (mInput ,str);
120+ std::cout << " Debug 4 : " << str << std::endl;
121+ while (true ){
122+ pos=str.find_first_of (" ," );
123+ line.push_back (str.substr (0 ,pos));
124+ std::cout << " Debug 4.2 : " << str << " and pos = " << pos << std::endl;
125+ str.erase (0 ,pos+1 );
126+ std::cout << " Debug 4.1 : " << str << " and pos = " << pos << std::endl;
127+ if (pos=std::string::npos){
128+ line.push_back (str.substr (0 ,pos));
129+ str.erase (0 ,pos);
130+ break ;
131+ }
132+ }
133+ return line;
134+ }
135+
136+
131137/* * Running the stack **/
132138template <class T , class D >
133139void Problem<T,D>::run() {
134- while (true ) {
135- D elt = mInput ();
136- while (true && mPopConditon (elt)) {
137- (*mStack ).pop ();
140+ initStack ();
141+ while ((mInput .good ())) {
142+ std::cout << " Debug 1" << std::endl;
143+ std::vector<std::string> line = readLine ();
144+ if ( (line.front ()== " -1" ) || (line.front ()==" " ) ) {
145+ break ;
138146 }
139- if (mPushCondition ()) {
140- (*mStack ).push (elt);
147+ std::cout << " Debug 2 : size = " << line.size () << std::endl;
148+ std::cout << " Context = " << (line.back ()) << std::endl;
149+ D data = readInput (line);
150+ mIndex ++; // Might have to move
151+ while ( (emptystack ()) && (popCondition (data)) ) {
152+ Data<D> elt = pop ();
153+ popAction (elt);
154+ }
155+ if (pushCondition (data)) {
156+ Data<D> elt (mIndex ,data);
157+ pushAction (elt);
158+ push (elt);
159+ println ();
141160 }
142161 }
143162}
144163
145- /* * Setters **/
146- template <class T , class D >
147- void Problem<T,D>::setReadInput(D (*functionPointer)()){
148- mReadInput = functionPointer;
149- }
150- template <class T , class D >
151- void Problem<T,D>::setInitStack(void (*functionPointer)()){
152- mInitStack = functionPointer;
153- }
164+ /* * Push, pop, and top **/
154165template <class T , class D >
155- void Problem<T,D>::setPopAction( void (*functionPointer)() ){
156- mPopAction = functionPointer ;
166+ void Problem<T,D>::push(Data<D> elt ){
167+ (* mStack ). push (elt) ;
157168}
158169template <class T , class D >
159- void Problem<T,D>::setPopCondition(ProblemBoolFn functionPointer ){
160- mPopConditon = functionPointer ;
170+ Data<D> Problem<T,D>::pop( ){
171+ return (* mStack ). pop () ;
161172}
162173template <class T , class D >
163- void Problem<T,D>::setPushAction( void (*functionPointer)() ){
164- mPushAction = functionPointer ;
174+ Data<D> Problem<T,D>::top( int k ){
175+ return (* mStack ). top (k) ;
165176}
166177template <class T , class D >
167- void Problem<T,D>::setPushCondition( bool (*functionPointer)() ){
168- mPushConditon = functionPointer ;
178+ bool Problem<T,D>::emptystack( ){
179+ return (* mStack ). isempty () ;
169180}
170181
182+ /* * Setters **/
171183template <class T , class D >
172184void Problem<T,D>::setOutput(std::string fileName){
173185 std::ofstream ofstr;
174186 ofstr.open (fileName, std::ofstream::out);
175187 mOutput = &ofstr;
176188}
177189
190+ template <class T , class D >
191+ void Problem<T,D>::setContext(T context){
192+ std::cout << " setContext, T = " << context << std::endl;
193+ mContext = &context;
194+ std::cout << " setContext, *mContext = " << (*mContext ) << std::endl;
195+ std::cout << " setContext, *mContext = " << getContext () << std::endl;
196+ }
197+
198+ /* * Getters **/
199+ template <class T , class D >
200+ T Problem<T,D>::getContext(){
201+ return *mContext ;
202+ }
203+
178204#endif /* PROBLEM */
0 commit comments