Skip to content

Commit 443f13f

Browse files
committed
Improved README.md
1 parent 867659b commit 443f13f

File tree

3 files changed

+98
-27
lines changed

3 files changed

+98
-27
lines changed

README.md

Lines changed: 97 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,118 @@
11
# CompressedStacks.cpp
2+
3+
The CompressedStacks.cpp module/library implements a time-space trade-off structure for stack's algorithms.
4+
5+
<!-- TODO: Write down a better description -->
6+
7+
## Category of algorithms
8+
<p>
9+
This compressed stack structure works correctly as a normal stack for any problems that read input from a file. However, the running time is optimal when the input would be read sequentially with a classical stack structure. For his reason, the only function implemented in the Problem template to solve it (to do a run) is the one presented below in a simplified version.
10+
</p>
11+
212
```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, 3rd, 4th, 18th, and 25th lines.
13+
template <class T, class D> void Problem<T, D>::run() {
14+
initStackIntern();
15+
while (notEndOfFile()) {
16+
D data = readInput(line);
17+
while (notEmptystack() && popCondition(data)) {
18+
elt = pop();
19+
popAction(elt);
20+
}
21+
if (pushCondition(data)) {
22+
pushAction(data);
23+
push(data);
24+
}
25+
}
26+
}
27+
```
28+
29+
## Characterization of a problem
30+
<p>In the followwing examples, implementations of the Problem interface are given.</p>
31+
32+
### General example : Instance<T,D>
33+
34+
```cpp
35+
#include <string>
36+
#include <vector>
37+
#include <memory>
38+
39+
// T is the type of the context and D is the type of the input data.
540
class Instance: public Problem<T,D>{
641
public:
7-
Instance(std::string filePath, int size):Problem<T,D>(filePath, size){}
8-
Instance(std::string filePath, int size, int space, int buffer):Problem<T,D>(filePath, size, space, buffer){}
42+
Instance(std::string filePath) : Problem<T, D>(filePath) {}
943
private:
1044
// Functions to implement according to the problem and input
11-
int mReadInput(){
12-
std::cout << "Implement mReadInput for your instance" << std::endl;
45+
D readInput(std::vector<std::string> line){
46+
std::cout << "Implement readInput for your instance" << std::endl;
1347
return 0;
1448
}
15-
void mInitStack(){
16-
std::cout << "Implement mInitStack for your instance" << std::endl;
49+
std::shared_ptr<T> initStack(){
50+
std::cout << "Implement initStack for your instance" << std::endl;
51+
std::shared_ptr<T> context(nullptr);
52+
return context;
1753
}
18-
bool mPopCondition(int elt){
54+
bool popCondition(D data){
1955
std::cout << "Implement mPopCondition for your instance" << std::endl;
2056
return false;
2157
}
22-
void mPopAction(Data<D> elt){
58+
void popAction(Data<T, D> elt){
2359
std::cout << "Implement mPopAction for your instance" << std::endl;
2460
}
25-
bool mPushCondition(int elt){
61+
bool pushCondition(D data){
2662
std::cout << "Implement mPushCondition for your instance" << std::endl;
2763
return true;
2864
}
29-
void mPushAction(Data<D> elt){
65+
void pushAction(Data<T, D> elt){
3066
std::cout << "Implement mPushAction for your instance" << std::endl;
3167
}
3268
};
3369
```
70+
71+
### Example with T = int and D = int : Instance<int,int>
72+
The context is initialized at 0. The data (in cvs format) is read as a pair of string such that the first string is the data and the second is used to update the context. While the context is more than 0, the stack is poped and the context decreased by 1. If the data is more than 0 then it is pushed.
73+
```cpp
74+
class Instance : public Problem<int, int> {
75+
public:
76+
Instance(std::string filePath) : Problem<int, int>(filePath) {}
77+
78+
private:
79+
// Functions to run the stack
80+
int readInput(std::vector<std::string> line) {
81+
int value = std::stoi(line[0]);
82+
setContext(std::stoi(line[1]));
83+
return value;
84+
}
85+
std::shared_ptr<int> initStack() {
86+
std::shared_ptr<int> context(new int(0));
87+
return context;
88+
}
89+
bool popCondition(int data) {
90+
if ((getContext() > 0)) {
91+
return true;
92+
}
93+
return false;
94+
}
95+
void popAction(Data<int, int> elt) {
96+
std::cout << elt.toString() << " <<<< Pop!" << std::endl;
97+
setContext(getContext() - 1);
98+
}
99+
bool pushCondition(int data) {
100+
if (data > 0) {
101+
return true;
102+
}
103+
return false;
104+
}
105+
void pushAction(Data<int, int> elt) {
106+
std::cout << "Push >>>> " << elt.toString() << std::endl;
107+
}
108+
};
109+
```
110+
111+
## How to run your problem
112+
Suppose the class Instance implement the interface Problem<T,D> (as in some examples above). You can run an instance of your problem described in the input located at <i>filepath</i>. The last command just print an output in th console of your compressed stack after the run.
113+
114+
```cpp
115+
Instance stack(filePath);
116+
stack.run();
117+
stack.println();
118+
```

include/compressedStack.hpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ CompressedStack<T, D>::CompressedStack(int size, int space, int buffer,
149149
}
150150

151151
/*==============================================================================
152-
New Internals
152+
Internals
153153
1) emptyExplicit (int lvl, int component) : test if the explicit part of
154154
component is empty. If component = 0, test on all component.
155155
2) back(int lvl, int component) : if lvl = 0 give the back of mCompressed,
@@ -165,8 +165,6 @@ component
165165
9) getSign(int component, int lvl) : get the last sign in component at lvl.
166166
If lvl = 0, look in the fully compressed part
167167
==============================================================================*/
168-
// TODO: Move new internals once finished
169-
170168
template <class T, class D>
171169
bool CompressedStack<T, D>::empty(int lvl, int component) {
172170
bool b = true;
@@ -450,8 +448,6 @@ template <class T, class D> std::string CompressedStack<T, D>::toString() {
450448
3) pushCompressed
451449
4) resetBlock
452450
==============================================================================*/
453-
// TODO: reorganize the push functions
454-
455451
// Function push that push the data in explicit and index in partial/compressed
456452
template <class T, class D>
457453
void CompressedStack<T, D>::push(const Data<T, D> &elt) {

src/main.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,5 @@ int main(int argc, char *argv[]) {
145145
exit(-1);
146146
break;
147147
}
148-
149-
// Intances generation, please comment when not in use
150-
/* createTestInput ct=createTestInput();
151-
ct.createTestInputFiles(0,"/home/yago/code/CompressedStacks.cpp/instances/pushOnlyInput.csv",1000,3,
152-
5, 100, 0.2 );
153-
ct.createTestInputFiles(1,"/home/yago/code/CompressedStacks.cpp/instances/CTInput.csv",1000,3);
154-
ct.createTestInputFiles(2,"/home/yago/code/CompressedStacks.cpp/instances/CHInput.csv",100,3,-1,
155-
11);
156-
*/
157-
158148
return 0;
159149
}

0 commit comments

Comments
 (0)