Skip to content

Commit ccc7020

Browse files
committed
Added step, walk, factorization of code, pre and post push/pop, report
1 parent 259e092 commit ccc7020

File tree

4 files changed

+269
-177
lines changed

4 files changed

+269
-177
lines changed

include/convexHull.hpp

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,36 @@
1-
#include "stackAlgo.hpp"
21
#include "compare.hpp"
32
#include "point2D.hpp"
4-
3+
#include "stackAlgo.hpp"
54

65
/*==============================================================================
76
Empty type for the context (empty for the Convex Hull problem)
87
==============================================================================*/
9-
class emptyContext{};
8+
class emptyContext {};
109

1110
/*==============================================================================
1211
Instantiation of a problem
1312
==============================================================================*/
1413
class convexHull : public StackAlgo<emptyContext, Point2D> {
1514
public:
16-
convexHull(std::string filePath) : StackAlgo<emptyContext, Point2D>(filePath) {}
15+
convexHull(std::string filePath)
16+
: StackAlgo<emptyContext, Point2D>(filePath) {}
1717

1818
private:
19-
// Functions to run the stack
20-
Point2D readInput(std::vector<std::string> line);
21-
22-
std::shared_ptr<emptyContext> initStack();
19+
// Functions to run the stack
20+
Point2D readInput(std::vector<std::string> line);
21+
std::shared_ptr<emptyContext> initStack();
2322

24-
bool popCondition(Point2D data);
23+
bool popCondition(Point2D data);
24+
void prePop(Point2D data);
25+
void postPop(Point2D data, Data<emptyContext, Point2D> elt);
26+
void noPop(Point2D data);
2527

26-
void popAction(Data<emptyContext, Point2D> elt);
28+
bool pushCondition(Point2D data);
29+
void prePush(Data<emptyContext, Point2D> elt);
30+
void postPush(Data<emptyContext, Point2D> elt);
31+
void noPush(Point2D data);
2732

28-
bool pushCondition(Point2D data);
29-
30-
void pushAction(Data<emptyContext, Point2D> elt);
33+
void reportStack();
3134
};
3235

3336
/*==============================================================================
@@ -36,19 +39,23 @@ class convexHull : public StackAlgo<emptyContext, Point2D> {
3639

3740
class comparisonConvexHull : public CompareStacks<emptyContext, Point2D> {
3841
public:
39-
comparisonConvexHull(std::string filePath) : CompareStacks<emptyContext, Point2D>(filePath) {}
42+
comparisonConvexHull(std::string filePath)
43+
: CompareStacks<emptyContext, Point2D>(filePath) {}
4044

4145
private:
42-
// Functions to run the stack
43-
Point2D readInput(std::vector<std::string> line);
44-
45-
std::shared_ptr<emptyContext> initStack();
46-
47-
bool popCondition(Point2D data);
46+
// Functions to run the stack
47+
Point2D readInput(std::vector<std::string> line);
48+
std::shared_ptr<emptyContext> initStack();
4849

49-
void popAction(Data<emptyContext, Point2D> elt);
50+
bool popCondition(Point2D data);
51+
void prePop(Point2D data);
52+
void postPop(Point2D data, Data<emptyContext, Point2D> elt);
53+
void noPop(Point2D data);
5054

51-
bool pushCondition(Point2D data);
55+
bool pushCondition(Point2D data);
56+
void prePush(Data<emptyContext, Point2D> elt);
57+
void postPush(Data<emptyContext, Point2D> elt);
58+
void noPush(Point2D data);
5259

53-
void pushAction(Data<emptyContext, Point2D> elt);
60+
void reportStack();
5461
};

include/stackAlgo.hpp

Lines changed: 92 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
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
==============================================================================*/
89103
template <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

223284
template <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

279329
template <class T, class D> int StackAlgo<T, D>::getIndex() { return mIndex; }
280330

0 commit comments

Comments
 (0)