44/* ==============================================================================
55 Includes
66==============================================================================*/
7- #include " compressedStack.hpp"
87#include " classicStack.hpp"
8+ #include " compressedStack.hpp"
99#include " stack.hpp"
1010#include < exception>
1111#include < fstream>
@@ -34,6 +34,12 @@ template <class T, class D> class StackAlgo {
3434 StackAlgo<T, D>(std::string fileName);
3535 virtual ~StackAlgo<T, D>() {}
3636
37+ // Walking the stack
38+ void walk (int steps = 1 );
39+ int step ();
40+ void popLoop (D data);
41+ void pushStep (D data);
42+
3743 // Running the stack
3844 void run ();
3945 void run (int limit);
@@ -69,12 +75,20 @@ template <class T, class D> class StackAlgo {
6975 std::ofstream mOutput ; // output file is optional
7076
7177 // Stack Functions: defined by user
72- virtual D readInput (std::vector<std::string> line) = 0;
7378 virtual std::shared_ptr<T> initStack () = 0;
79+ virtual D readInput (std::vector<std::string> line) = 0;
80+
7481 virtual bool popCondition (D data) = 0;
75- virtual void popAction (Data<T, D> elt){};
82+ virtual void prePop (D data){};
83+ virtual void postPop (D data, Data<T, D> elt){};
84+ virtual void noPop (D data){};
85+
7686 virtual bool pushCondition (D data) = 0;
77- virtual void pushAction (Data<T, D> elt){};
87+ virtual void prePush (Data<T, D> elt){};
88+ virtual void postPush (Data<T, D> elt){};
89+ virtual void noPush (D data){};
90+
91+ virtual void reportStack () = 0;
7892
7993 // StackAlgo internal during run
8094 std::shared_ptr<T> mContext ;
@@ -87,7 +101,8 @@ template <class T, class D> class StackAlgo {
87101 Constructors : with ClassicStack or CompressedStack
88102==============================================================================*/
89103template <class T , class D >
90- StackAlgo<T, D>::StackAlgo(std::string fileName) : mIndex (0 ), mContext (nullptr ) {
104+ StackAlgo<T, D>::StackAlgo(std::string fileName)
105+ : mIndex (0 ), mContext (nullptr ) {
91106 mInput .open (fileName, std::ifstream::in);
92107
93108 std::vector<std::string> parameters = readHeader ();
@@ -110,11 +125,12 @@ StackAlgo<T, D>::StackAlgo(std::string fileName) : mIndex(0), mContext(nullptr)
110125 }
111126
112127 if (foundBuffer && !foundP)
113- throw (" StackAlgo<T,D>::StackAlgo(std::string fileName), wrong header format " );
128+ throw (" StackAlgo<T,D>::StackAlgo(std::string fileName), wrong header "
129+ " format " );
114130 if (!foundP)
115131 mStack = std::shared_ptr<Stack<T, D>>(
116132 new ClassicStack<T, D>()); // space not provided, classic stack
117- else // space was provided, compressed stack
133+ else // space was provided, compressed stack
118134 {
119135 if (!foundBuffer)
120136 b = 0 ;
@@ -143,7 +159,8 @@ template <class T, class D> void StackAlgo<T, D>::println() {
143159 std::cout << std::endl;
144160}
145161
146- template <class T , class D > std::vector<std::string> StackAlgo<T, D>::readLine() {
162+ template <class T , class D >
163+ std::vector<std::string> StackAlgo<T, D>::readLine() {
147164 std::string str;
148165 std::vector<std::string> line;
149166 size_t pos = std::string::npos;
@@ -187,57 +204,88 @@ template <class T, class D> void StackAlgo<T, D>::readPush(int iter) {
187204 D data = readInput (line);
188205 mIndex ++;
189206 Data<T, D> elt (mIndex , data);
190- pushAction (elt);
207+ prePush (elt);
191208 push (elt);
192209 }
193210}
194211
195-
196212/* ==============================================================================
197213 Stack Functions: run, push, pop, top, readPush
198214==============================================================================*/
199- // TODO: Make popLoop, pushStep and so on functions
200- template <class T , class D > void StackAlgo<T, D>::run() {
201- initStackIntern ();
202- while (mInput .good ()) {
203- std::streampos position = mInput .tellg ();
204- (*mStack ).setPosition (position);
205- std::vector<std::string> line = readLine ();
206- if (line.front () == " " ) {
215+ template <class T , class D > void StackAlgo<T, D>::walk(int steps) {
216+ for (int i = 0 ; i < steps; i++) {
217+ if (step () == 0 || mInput .good ()) {
207218 break ;
208219 }
209- D data = readInput (line);
210- mIndex ++;
211- while ((!emptystack ()) && (popCondition (data))) {
220+ }
221+ }
222+
223+ template <class T , class D > void StackAlgo<T, D>::popLoop(D data) {
224+ while (!emptystack ()) {
225+ if (popCondition (data)) {
226+ prePop (data);
212227 Data<T, D> elt = pop ();
213- popAction (elt);
228+ postPop (data, elt);
229+ } else {
230+ noPop (data);
231+ break ;
214232 }
215- if (pushCondition (data)) {
216- Data<T, D> elt (mIndex , data);
217- pushAction (elt);
218- push (elt);
233+ }
234+ }
235+
236+ template <class T , class D > void StackAlgo<T, D>::pushStep(D data) {
237+ if (pushCondition (data)) {
238+ Data<T, D> elt (mIndex , data);
239+ prePush (elt);
240+ push (elt);
241+ postPush (elt);
242+ } else {
243+ noPush (data);
244+ }
245+ }
246+
247+ template <class T , class D > int StackAlgo<T, D>::step() {
248+ // Storing the position in the input file
249+ std::streampos position = mInput .tellg ();
250+ (*mStack ).setPosition (position);
251+
252+ // Reading a new element
253+ std::vector<std::string> line = readLine ();
254+ if (line.front () == " " ) {
255+ // Return 0 if it has to stop (EOF)
256+ return 0 ;
257+ }
258+ D data = readInput (line);
259+
260+ // Increasing index of the number of elements
261+ mIndex ++;
262+
263+ // Call the pop loop
264+ popLoop (data);
265+
266+ // Call the conditional push
267+ pushStep (data);
268+
269+ // Return 1 in case of success
270+ return 1 ;
271+ }
272+
273+ template <class T , class D > void StackAlgo<T, D>::run() {
274+ initStackIntern ();
275+ while (mInput .good ()) {
276+ if (step () == 0 ) {
277+ break ;
219278 }
220279 }
280+
281+ reportStack ();
221282}
222283
223284template <class T , class D > void StackAlgo<T, D>::run(int limit) {
224285 // int testIndex = mIndex;
225286 while (mInput .good () && mIndex < limit) {
226- std::streampos position = mInput .tellg ();
227- (*mStack ).setPosition (position);
228- std::vector<std::string> line = readLine ();
229-
230- D data = readInput (line);
231- mIndex ++;
232-
233- while ((!emptystack ()) && (popCondition (data))) {
234- Data<T, D> elt = pop ();
235- popAction (elt);
236- }
237- if (pushCondition (data)) {
238- Data<T, D> elt (mIndex , data);
239- pushAction (elt);
240- push (elt);
287+ if (step () == 0 ) {
288+ break ;
241289 }
242290 }
243291}
@@ -274,7 +322,9 @@ template <class T, class D> void StackAlgo<T, D>::initStackIntern() {
274322/* ==============================================================================
275323 Getters
276324==============================================================================*/
277- template <class T , class D > T StackAlgo<T, D>::getContext() { return *mContext ; }
325+ template <class T , class D > T StackAlgo<T, D>::getContext() {
326+ return *mContext ;
327+ }
278328
279329template <class T , class D > int StackAlgo<T, D>::getIndex() { return mIndex ; }
280330
0 commit comments