|
| 1 | +#ifndef COMPARE |
| 2 | +#define COMPARE |
| 3 | + |
| 4 | +/*============================================================================== |
| 5 | + Includes |
| 6 | +==============================================================================*/ |
| 7 | +#include "problem.hpp" |
| 8 | +#include "data.hpp" |
| 9 | + |
| 10 | +/*============================================================================== |
| 11 | + Class : abstract, template (T context, D datas) |
| 12 | + Extensions : |
| 13 | + Aliases : |
| 14 | + Friends -> |
| 15 | + <- Problem |
| 16 | +==============================================================================*/ |
| 17 | +template <class T, class D> |
| 18 | +class CompareStacks: public Problem<T,D>{ |
| 19 | +public: |
| 20 | + // Members functions |
| 21 | + CompareStacks<T,D>(std::string fileName, int size, int space, int buffer); |
| 22 | + |
| 23 | + // Compare the stacks |
| 24 | + void runCompare(int buffer = 0); |
| 25 | + |
| 26 | +private: |
| 27 | + |
| 28 | + // Stack Functions: defined by user |
| 29 | + virtual D readInput(std::vector<std::string> line) = 0; |
| 30 | + virtual std::shared_ptr<T> initStack() = 0; |
| 31 | + virtual bool popCondition(D data) = 0; |
| 32 | + virtual void popAction(Data<T,D> elt) {}; |
| 33 | + virtual bool pushCondition(D data) = 0; |
| 34 | + virtual void pushAction(Data<T,D> elt) {}; |
| 35 | + |
| 36 | + // Stack: Normal Stack for comparison |
| 37 | + std::shared_ptr<NormalStack<T,D>> mNormalStack; |
| 38 | +}; |
| 39 | + |
| 40 | +/*============================================================================== |
| 41 | + Constructors : |
| 42 | +==============================================================================*/ |
| 43 | + |
| 44 | +template <class T, class D> |
| 45 | +CompareStacks<T,D>::CompareStacks(std::string fileName, int size, int space, int buffer) |
| 46 | +: Problem<T,D>(fileName, size, space, buffer) |
| 47 | +, mNormalStack(new NormalStack<T,D> (size)){ |
| 48 | +} |
| 49 | + |
| 50 | +/*============================================================================== |
| 51 | + Stack Functions: run, push, pop, top |
| 52 | +==============================================================================*/ |
| 53 | +template <class T, class D> |
| 54 | +void CompareStacks<T,D>::runCompare(int buffer){ |
| 55 | + Problem<T,D>::initStackIntern(); |
| 56 | + while ((Problem<T,D>::mInput.good())) { |
| 57 | + for (int i = 1; i <= buffer; i++) { |
| 58 | + bool bIndex = Problem<T,D>::top(i).mIndex == mNormalStack->top(i).mIndex; |
| 59 | + bool bData = Problem<T,D>::top(i).mData == mNormalStack->top(i).mData; |
| 60 | + if (!bIndex || !bData) { |
| 61 | + throw "The top $(i)st elements are different"; |
| 62 | + } |
| 63 | + } |
| 64 | + std::vector<std::string> line = Problem<T,D>::readLine(); |
| 65 | + if ( (line.front()== "-1") || (line.front()=="") ) { |
| 66 | + break; |
| 67 | + } |
| 68 | + D data = readInput(line); |
| 69 | + Problem<T,D>::mIndex++; // Might have to move |
| 70 | + if (Problem<T,D>::emptystack() != mNormalStack->isempty()) { |
| 71 | + throw "One stack is empty and not the other"; |
| 72 | + } |
| 73 | + while ( !(Problem<T,D>::emptystack()) && (popCondition(data)) ) { |
| 74 | + Data<T,D> elt = Problem<T,D>::pop(); |
| 75 | + Data<T,D> eltNormal = mNormalStack->pop(); |
| 76 | + bool bIndex = elt.mIndex == eltNormal.mIndex; |
| 77 | + bool bData = elt.mData == eltNormal.mData; |
| 78 | + if (!bIndex || bData) { |
| 79 | + throw "The two elements popped are different"; |
| 80 | + } |
| 81 | + popAction(elt); |
| 82 | + } |
| 83 | + if (pushCondition(data)) { |
| 84 | + Data<T,D> elt (Problem<T,D>::mIndex,data); |
| 85 | + pushAction(elt); |
| 86 | + Problem<T,D>::push(elt); |
| 87 | + mNormalStack->push(elt); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +#endif /* COMPARE */ |
0 commit comments