|
1 | 1 | # 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 | + |
2 | 12 | ```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. |
5 | 40 | class Instance: public Problem<T,D>{ |
6 | 41 | 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) {} |
9 | 43 | private: |
10 | 44 | // 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; |
13 | 47 | return 0; |
14 | 48 | } |
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; |
17 | 53 | } |
18 | | - bool mPopCondition(int elt){ |
| 54 | + bool popCondition(D data){ |
19 | 55 | std::cout << "Implement mPopCondition for your instance" << std::endl; |
20 | 56 | return false; |
21 | 57 | } |
22 | | - void mPopAction(Data<D> elt){ |
| 58 | + void popAction(Data<T, D> elt){ |
23 | 59 | std::cout << "Implement mPopAction for your instance" << std::endl; |
24 | 60 | } |
25 | | - bool mPushCondition(int elt){ |
| 61 | + bool pushCondition(D data){ |
26 | 62 | std::cout << "Implement mPushCondition for your instance" << std::endl; |
27 | 63 | return true; |
28 | 64 | } |
29 | | - void mPushAction(Data<D> elt){ |
| 65 | + void pushAction(Data<T, D> elt){ |
30 | 66 | std::cout << "Implement mPushAction for your instance" << std::endl; |
31 | 67 | } |
32 | 68 | }; |
33 | 69 | ``` |
| 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 | +``` |
0 commit comments