Skip to content

Commit ef7fb7e

Browse files
committed
Functional Normal Stack template. Example of an instance. First README.md file
1 parent 56f0852 commit ef7fb7e

File tree

7 files changed

+193
-75
lines changed

7 files changed

+193
-75
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,32 @@
11
# CompressedStacks.cpp
2+
```cpp
3+
// Class for test instance
4+
// T is the type of the context and D is the type of the input data. Please change it on 1st and 3rd lines.
5+
class Instance: public Problem<T,D>{
6+
public:
7+
Instance(std::string filePath, int size):Problem<T,D>(filePath, size){}
8+
private:
9+
// Functions to implement according to the problem and input
10+
int mReadInput(){
11+
std::cout << "Implement mReadInput for your instance" << std::endl;
12+
return 0;
13+
}
14+
void mInitStack(){
15+
std::cout << "Implement mInitStack for your instance" << std::endl;
16+
}
17+
bool mPopCondition(int elt){
18+
std::cout << "Implement mPopCondition for your instance" << std::endl;
19+
return false;
20+
}
21+
void mPopAction(Data<D> elt){
22+
std::cout << "Implement mPopAction for your instance" << std::endl;
23+
}
24+
bool mPushCondition(int elt){
25+
std::cout << "Implement mPushCondition for your instance" << std::endl;
26+
return true;
27+
}
28+
void mPushAction(Data<D> elt){
29+
std::cout << "Implement mPushAction for your instance" << std::endl;
30+
}
31+
};
32+
```

include/compressedStack.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class CompressedStack: public Stack<D>
1919
Data<D> top(int k);
2020
void push(Data<D> data);
2121
Data<D> pop();
22+
bool isempty();
2223

2324
// IO
2425
std::string toString();
@@ -86,6 +87,11 @@ void CompressedStack<T,D>::println()
8687
}
8788

8889
/** Pushes, pops and accesses **/
90+
template <class T, class D>
91+
bool CompressedStack<T,D>::isempty(){
92+
return true;
93+
}
94+
8995
template <class T, class D>
9096
void CompressedStack<T,D>::push(Data<D> data)
9197
{

include/data.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ template <class D>
5555
std::string Data<D>::toString()
5656
{
5757
std::string str;
58-
str = std::to_string(mIndex);
58+
str = std::to_string(mIndex) + "<-" + std::to_string(mData);
5959
return str;
6060
}
6161
template <class D>

include/normalStack.hpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class NormalStack: public Stack<D>
1515
Data<D> pop();
1616
void push(Data<D> data);
1717
Data<D> top(int k);
18+
bool isempty();
1819

1920
// IO
2021
std::string toString();
@@ -35,6 +36,12 @@ NormalStack<D>::NormalStack(int size)
3536
}
3637

3738
/** Stack internal methods **/
39+
template <class D>
40+
bool NormalStack<D>::isempty()
41+
{
42+
return mDatas.empty();
43+
}
44+
3845
template <class D>
3946
Data<D> NormalStack<D>::pop()
4047
{
@@ -44,9 +51,12 @@ template <class D>
4451
}
4552

4653
template <class D>
47-
void NormalStack<D>::push(Data<D> data)
54+
void NormalStack<D>::push(Data<D> elt)
4855
{
49-
mDatas.push_back(data);
56+
std::cout << "Ultra Push! : data = " << (elt.getData()) << std::endl;
57+
mDatas.push_back(elt);
58+
std::cout << "Size of the stack : " << (mDatas.size()) << std::endl;
59+
println();
5060
}
5161

5262
template <class D>

include/problem.hpp

Lines changed: 91 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -5,57 +5,58 @@
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

1213
template <class T, class D>
1314
class 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-
2016
public:
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+
4344
private:
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
6566
template <class T, class D>
6667
Problem<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

8778
template <class T, class D>
8879
Problem<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 **/
132138
template <class T, class D>
133139
void 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 **/
154165
template <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
}
158169
template <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
}
162173
template <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
}
166177
template <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 **/
171183
template <class T, class D>
172184
void 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 */

include/stack.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Stack
1212
virtual Data<D> pop() = 0;
1313
virtual void push(Data<D> data) = 0;
1414
virtual Data<D> top(int k) = 0;
15+
virtual bool isempty() = 0;
1516

1617
// IO
1718
virtual std::string toString() = 0;

0 commit comments

Comments
 (0)