Skip to content

Commit e860b9f

Browse files
committed
Corrected bug with Buffer
1 parent be7e44d commit e860b9f

File tree

7 files changed

+71
-17
lines changed

7 files changed

+71
-17
lines changed

include/buffer.hpp

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ template <class T, class D> class Buffer {
3737
SPData<T, D> topPointer(int k);
3838

3939
// Setters
40-
void setStart();
41-
void setData(SPData<T, D> elt, int index);
40+
void setStart(int k = 1);
41+
void setData(SPData<T, D> elt, int id = 1);
4242

4343
// Push and Pop
4444
void push(SPData<T, D> elt);
@@ -87,8 +87,8 @@ template <class T, class D> SPData<T, D> Buffer<T, D>::topPointer(int k) {
8787
/*==============================================================================
8888
Setters
8989
==============================================================================*/
90-
template <class T, class D> void Buffer<T, D>::setStart() {
91-
mStart = (mStart + 1) % mSize;
90+
template <class T, class D> void Buffer<T, D>::setStart(int k) {
91+
mStart = (mStart + k) % mSize;
9292
}
9393
template <class T, class D>
9494
void Buffer<T, D>::setData(SPData<T, D> elt, int id) {
@@ -104,14 +104,15 @@ template <class T, class D> void Buffer<T, D>::push(SPData<T, D> elt) {
104104
if ((int) mExplicit.size() < mSize) {
105105
mExplicit.push_back(elt);
106106
} else {
107-
setData(elt, mStart + 1);
107+
setData(elt);
108+
setStart();
108109
}
109110
}
110111
}
111112

112113
template <class T, class D> void Buffer<T, D>::pop(SPData<T, D> elt) {
113-
setStart();
114-
setData(elt, mSize);
114+
setStart(mSize - 1);
115+
setData(elt);
115116
}
116117

117118
/*==============================================================================
@@ -120,9 +121,21 @@ template <class T, class D> void Buffer<T, D>::pop(SPData<T, D> elt) {
120121
template <class T, class D> std::string Buffer<T, D>::toString() {
121122
std::string str = "";
122123
if (mSize > 0) {
123-
str = "\t\tBuffer size is " + std::to_string(mSize);
124+
str = "\n\t\tBuffer size is " + std::to_string(mSize);
124125
str += " and start at index " + std::to_string(mStart) + "\n";
125-
str += "\t\t\t" + explicitPointerToString(mExplicit);
126+
str += "\t\t\t";
127+
std::string aux = "";
128+
for (int i = mStart; i < mSize; i++) {
129+
if (aux == "") {
130+
aux += "{" + mExplicit[i]->toString();
131+
} else {
132+
aux += "," + mExplicit[i]->toString();
133+
}
134+
}
135+
for (int i = 0; i < mStart; i++) {
136+
aux += "," + mExplicit[i]->toString();
137+
}
138+
str += aux + "}";
126139
}
127140
return str;
128141
}

include/compare.hpp

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ template <class T, class D> class CompareStacks : public Problem<T, D> {
2929
std::cout << str << std::endl;
3030
}
3131

32+
// Redefinition
33+
void readPush(int iter = 1);
34+
3235
private:
3336
// Stack Functions: defined by user
3437
virtual D readInput(std::vector<std::string> line) = 0;
@@ -67,8 +70,7 @@ template <class T, class D> void CompareStacks<T, D>::runCompare(int buffer) {
6770
std::streampos position = Problem<T, D>::mInput.tellg();
6871
(*Problem<T, D>::mStack).setPosition(position);
6972
for (int i = 1; i <= buffer; i++) {
70-
bool bIndex =
71-
Problem<T, D>::top(i).mIndex == mNormalStack->top(i).mIndex;
73+
bool bIndex = Problem<T, D>::top(i).mIndex == mNormalStack->top(i).mIndex;
7274
bool bData = Problem<T, D>::top(i).mData == mNormalStack->top(i).mData;
7375
if (!bIndex || !bData) {
7476
Problem<T, D>::println();
@@ -81,7 +83,7 @@ template <class T, class D> void CompareStacks<T, D>::runCompare(int buffer) {
8183
break;
8284
}
8385
D data = readInput(line);
84-
Problem<T, D>::mIndex++; // Might have to move
86+
Problem<T, D>::mIndex++;
8587
if ((*Problem<T, D>::mStack).empty() != mNormalStack->empty()) {
8688
(*Problem<T, D>::mStack).empty();
8789
Problem<T, D>::println();
@@ -97,8 +99,7 @@ template <class T, class D> void CompareStacks<T, D>::runCompare(int buffer) {
9799
bool bData = elt.mData == eltNormal.mData;
98100
if (!bIndex || !bData) {
99101
Problem<T, D>::println();
100-
std::cout << *Problem<T, D>::mContext << std::endl;
101-
;
102+
//std::cout << *Problem<T, D>::mContext << std::endl;
102103
std::cout << mNormalStack->toString() << std::endl;
103104
throw "The two elements popped are different";
104105
}
@@ -109,10 +110,35 @@ template <class T, class D> void CompareStacks<T, D>::runCompare(int buffer) {
109110
Problem<T, D>::push(elt);
110111
mNormalStack->push(elt);
111112
}
113+
if (Problem<T, D>::mStack->getBufferSize() > 0) {
114+
std::cout << "Is it working" << std::endl;
115+
for (int k = 1; k <= Problem<T, D>::mStack->getBufferSize(); k++) {
116+
if (Problem<T, D>::mStack->top(k).mIndex == mNormalStack->top(k).mIndex) {
117+
Problem<T, D>::println();
118+
//std::cout << *Problem<T, D>::mContext << std::endl;
119+
std::cout << mNormalStack->toString() << std::endl;
120+
throw "The two elements at the k = $(k) position are different";
121+
}
122+
}
123+
}
112124
}
113125
} catch (char const *e) {
114126
std::cout << e << std::endl;
115127
}
116128
}
117129

130+
template <class T, class D> void CompareStacks<T, D>::readPush(int iter) {
131+
for (int i = 0; i < iter; i++) {
132+
std::streampos position = Problem<T, D>::mInput.tellg();
133+
Problem<T, D>::mStack->setPosition(position);
134+
std::vector<std::string> line = Problem<T, D>::readLine();
135+
D data = readInput(line);
136+
Problem<T, D>::mIndex++;
137+
Data<T, D> elt(Problem<T, D>::mIndex, data);
138+
pushAction(elt);
139+
Problem<T, D>::push(elt);
140+
mNormalStack->push(elt);
141+
}
142+
}
143+
118144
#endif /* COMPARE */

include/compressedStack.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ template <class T, class D> class CompressedStack : public Stack<T, D> {
5959
Block<T, D> getCompressed();
6060
ExplicitPointer<T, D> getFirstExplicit();
6161
Signature<T, D> getFirstSign();
62+
int getBufferSize ();
6263

6364
// IO
6465
std::string toString();
@@ -423,6 +424,11 @@ Signature<T, D> CompressedStack<T, D>::getBottomSign() {
423424
}
424425
}
425426

427+
template <class T, class D>
428+
int CompressedStack<T, D>::getBufferSize() {
429+
return mBuffer.mSize;
430+
}
431+
426432
/*==============================================================================
427433
IO : toString
428434
==============================================================================*/

include/normalStack.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ template <class T, class D> class NormalStack : public Stack<T, D> {
5252
Block<T, D> getCompressed();
5353
ExplicitPointer<T, D> getFirstExplicit();
5454
Signature<T, D> getFirstSign();
55+
int getBufferSize ();
5556
};
5657

5758
/*==============================================================================
@@ -112,6 +113,11 @@ ExplicitPointer<T, D> NormalStack<T, D>::getFirstExplicit() {
112113
return initExplicitPointer<T, D>();
113114
}
114115

116+
template <class T, class D>
117+
int NormalStack<T, D>::getBufferSize() {
118+
return 0;
119+
}
120+
115121
template <class T, class D> Signature<T, D> NormalStack<T, D>::getFirstSign() {
116122
Signature<T, D> sign(0, std::streampos(0), std::shared_ptr<T>(nullptr),
117123
Buffer<T, D>(0));

include/stack.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ template <class T, class D> class Stack {
3636
virtual Block<T, D> getCompressed() = 0;
3737
virtual ExplicitPointer<T, D> getFirstExplicit() = 0;
3838
virtual Signature<T, D> getFirstSign() = 0;
39+
virtual int getBufferSize() = 0;
3940

4041
// Setters
4142
virtual void setContext(std::shared_ptr<T> context) = 0;

src/convexHull.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ bool convexHull::popCondition(Point2D last) {
3838
minus1=top(1).getData();
3939
minus2=top(2).getData();
4040

41-
std::cout << last << " <<<< pop condition read two befoer "<<minus1<<minus2 << std::endl;
41+
std::cout << last << " <<<< pop condition read two before "<<minus1<<minus2 << std::endl;
4242

4343

4444
if (Point2D::orientation(minus2,minus1,last)==2 ) {
@@ -83,6 +83,8 @@ std::shared_ptr<emptyContext> comparisonConvexHull::initStack() {
8383
//first, read and push two values
8484
readPush(2);
8585

86+
printCompare();
87+
8688
std::cout<<"done reading two values "<<std::endl;
8789

8890
// then initialize context (which in this case is NULL everything
@@ -99,7 +101,7 @@ bool comparisonConvexHull::popCondition(Point2D last) {
99101
minus1=top(1).getData();
100102
minus2=top(2).getData();
101103

102-
std::cout << last << " <<<< pop condition read two befoer "<<minus1<<minus2 << std::endl;
104+
std::cout << last << " <<<< pop condition read two before "<<minus1<<minus2 << std::endl;
103105

104106

105107
if (Point2D::orientation(minus2,minus1,last)==2 ) {

src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ void test(std::string filePath,int code2)
112112
}
113113
case 2: {
114114
comparisonConvexHull chComp(filePath);
115-
chComp.run();
115+
chComp.runCompare();
116116
chComp.println();
117117
break;
118118
}

0 commit comments

Comments
 (0)