Skip to content

Commit af0905f

Browse files
committed
Added comparison class and function.
1 parent 9bb6bc9 commit af0905f

File tree

5 files changed

+159
-1
lines changed

5 files changed

+159
-1
lines changed

include/compare.hpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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 */

include/data.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919
template <class T, class D> class Component; // Required for the friendship
2020
template <class T, class D> class CompressedStack; // Required for the friendship
2121
template <class T, class D> class Problem; // Required for the friendship
22+
template <class T, class D> class CompareStacks; // Required for the friendship
2223
template <class T, class D>
2324
class Data{
2425
friend class Component<T,D>;
2526
friend class CompressedStack<T,D>;
2627
friend class Problem<T,D>;
28+
friend class CompareStacks<T,D>;
2729

2830
public:
2931
// IO

include/normalStack.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
<-
1616
==============================================================================*/
1717
template <class T, class D> class Problem; // Required for the friendship
18+
template <class T, class D> class CompareStacks; // Required for the friendship
1819
template <class T, class D>
1920
class NormalStack: public Stack<T,D>{
2021
friend class Problem<T,D>;
22+
friend class CompareStacks<T,D>;
2123

2224
public:
2325
// Constructors
@@ -31,7 +33,9 @@ class NormalStack: public Stack<T,D>{
3133

3234
// Stack common methods
3335
Data<T,D> pop(Problem<T,D> &problem);
36+
Data<T,D> pop();
3437
void push(const Data<T,D> &data, std::streampos position);
38+
void push(const Data<T,D> &data);
3539
Data<T,D> top(int k);
3640
bool isempty();
3741

@@ -72,11 +76,21 @@ Data<T,D> NormalStack<T,D>::pop(Problem<T,D> &problem){
7276
mDatas.pop_back();
7377
return data;
7478
}
79+
template <class T, class D>
80+
Data<T,D> NormalStack<T,D>::pop(){
81+
Data<T,D> data = mDatas.back();
82+
mDatas.pop_back();
83+
return data;
84+
}
7585

7686
template <class T, class D>
7787
void NormalStack<T,D>::push(const Data<T,D> &elt, std::streampos position){
7888
mDatas.push_back(elt);
7989
}
90+
template <class T, class D>
91+
void NormalStack<T,D>::push(const Data<T,D> &elt){
92+
mDatas.push_back(elt);
93+
}
8094

8195
template <class T, class D>
8296
Data<T,D> NormalStack<T,D>::top(int k){

include/problem.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@
2020
<- NormalStack, CompressedStack, Data
2121
==============================================================================*/
2222
template <class T, class D> class CompressedStack; // Required for the friendship
23+
template <class T, class D> class CompareStacks; // Required for the friendship
2324
template <class T, class D>
2425
class Problem{
2526
friend class CompressedStack<T,D>;
27+
friend class CompareStacks<T,D>;
2628

2729
public:
2830
// Members functions

src/main.cpp

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
Includes
55
==============================================================================*/
66
#include "../include/problem.hpp"
7+
#include "../include/compare.hpp"
78
#include "../include/createTestInput.hpp"
89
#include <string>
910
#include <vector>
@@ -49,6 +50,45 @@ class Instance: public Problem<int,int>{
4950
}
5051
};
5152

53+
/*==============================================================================
54+
Instantiation of a comparison
55+
==============================================================================*/
56+
class Comparison: public CompareStacks<int,int>{
57+
public:
58+
Comparison(std::string filePath, int size, int space, int buffer):CompareStacks<int,int>(filePath, size, space, buffer){}
59+
private:
60+
// Functions to run the stack
61+
int readInput(std::vector<std::string> line){
62+
int value = std::stoi(line[0]);
63+
setContext(std::stoi(line[1]));
64+
return value;
65+
66+
}
67+
std::shared_ptr<int> initStack(){
68+
std::shared_ptr<int> context (new int (10));
69+
std::vector<std::string> line = readLine(); // Temporary measure to get rid of the first line
70+
return context;
71+
}
72+
bool popCondition(int data){
73+
if (getContext() > 0) {
74+
return true;
75+
}
76+
return false;
77+
}
78+
void popAction(Data<int,int> elt){
79+
setContext(getContext() - 1);
80+
}
81+
bool pushCondition(int data){
82+
if (data > 0) {
83+
return true;
84+
}
85+
return false;
86+
}
87+
void pushAction(Data<int,int> elt){
88+
std::cout << "Implement mPushAction for your instance" << std::endl;
89+
}
90+
};
91+
5292
/*==============================================================================
5393
Test functions
5494
==============================================================================*/
@@ -69,12 +109,20 @@ void testProblem()
69109
testCS.println();
70110
}
71111

112+
void testCompare(){
113+
std::string filePath = "../instances/pushOnlyInput.csv";
114+
115+
Comparison comp(filePath, 1000, 3, 0);
116+
comp.runCompare();
117+
}
72118
/*==============================================================================
73119
Main function
74120
==============================================================================*/
75121
// Main //int main(int argc, char const *argv[]) {
76122
int main() {
77-
testProblem();
123+
// testProblem();
124+
125+
testCompare();
78126

79127
// Intances generation, please comment when not in use
80128
/* createTestInput ct=createTestInput();

0 commit comments

Comments
 (0)