Skip to content

Commit be7e44d

Browse files
committed
Fix to Buffer and Opt of CH
1 parent dcf7c5e commit be7e44d

File tree

6 files changed

+97
-110
lines changed

6 files changed

+97
-110
lines changed

include/buffer.hpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@ template <class T, class D> Buffer<T, D>::Buffer(int size) {
6969
Getters
7070
==============================================================================*/
7171
template <class T, class D> Data<T, D> Buffer<T, D>::top(int k) {
72-
if (k < mSize) {
72+
if (k <= mSize) {
7373
int index = (k + mStart - 1) % mSize; // -1 match the start of vectors at 0
7474
return *(mExplicit[index]);
7575
}
7676
throw "Access to a top element bigger than the size of the buffer";
7777
}
7878

7979
template <class T, class D> SPData<T, D> Buffer<T, D>::topPointer(int k) {
80-
if (k < mSize) {
80+
if (k <= mSize) {
8181
int index = (k + mStart - 1) % mSize; // -1 match the start of vectors at 0
8282
return mExplicit[index];
8383
}
@@ -101,7 +101,11 @@ void Buffer<T, D>::setData(SPData<T, D> elt, int id) {
101101
==============================================================================*/
102102
template <class T, class D> void Buffer<T, D>::push(SPData<T, D> elt) {
103103
if (mSize > 0) {
104-
setData(elt, mStart + 1);
104+
if ((int) mExplicit.size() < mSize) {
105+
mExplicit.push_back(elt);
106+
} else {
107+
setData(elt, mStart + 1);
108+
}
105109
}
106110
}
107111

include/compressedStack.hpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ SPData<T, D> CompressedStack<T, D>::getExplicitData(int k) {
405405
if (k <= (int)mFirst.mExplicit.size()) {
406406
return mFirst.mExplicit[k - 1];
407407
} else {
408-
return mFirst.mSign.mBuffer.topPointer(k - 1 - mFirst.mExplicit.size());
408+
return mFirst.mSign.mBuffer.topPointer(k - mFirst.mExplicit.size());
409409
}
410410
}
411411

@@ -451,16 +451,12 @@ template <class T, class D> std::string CompressedStack<T, D>::toString() {
451451
// Function push that push the data in explicit and index in partial/compressed
452452
template <class T, class D>
453453
void CompressedStack<T, D>::push(const Data<T, D> &elt) {
454-
std::cout << " inside comp stack goinbg to push "<< std::endl;
455454
// update the buffer (if buffer size is bigger than 0)
456455
SPData<T, D> ptr_elt = std::make_shared<Data<T, D>>(elt);
457-
std::cout << " inside comp stack made buffer "<< std::endl;
458456
mBuffer.push(ptr_elt);
459457
// update the explicit Blocks, with possibly shifting first to second
460-
std::cout << " inside comp stack going to push explicit "<< std::endl;
461458
pushExplicit(ptr_elt);
462459
// update the compressed Blocks at each levels (including fully compressed)
463-
std::cout << " inside comp stack pushed explicit "<< std::endl;
464460
for (int lvl = mDepth - 1; lvl > 0; lvl--) {
465461
int headIndex = getLast(lvl);
466462
pushCompressed(ptr_elt, lvl, headIndex);
@@ -476,7 +472,10 @@ void CompressedStack<T, D>::pushExplicit(SPData<T, D> elt) {
476472
// If the explicit datas of component 1 are empty we push
477473
if (empty(mDepth, 1)) {
478474
mFirst.pushExplicit(eltPtr);
479-
std::shared_ptr<T> context = std::make_shared<T>(*mContext);
475+
std::shared_ptr<T> context;
476+
if (mContext != nullptr) {
477+
context = std::make_shared<T>(*mContext);
478+
}
480479
Signature<T, D> sign(index, mPosition, context, mBuffer);
481480
mFirst.mSign = sign;
482481
}

include/convexHull.hpp

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,52 @@
33
#include "Point2D.hpp"
44

55

6+
/*==============================================================================
7+
Empty type for the context (empty for the Convex Hull problem)
8+
==============================================================================*/
9+
class emptyContext{};
10+
611
/*==============================================================================
712
Instantiation of a problem
813
==============================================================================*/
9-
class convexHull : public Problem<std::nullptr_t, Point2D> {
14+
class convexHull : public Problem<emptyContext, Point2D> {
1015
public:
11-
convexHull(std::string filePath) : Problem<std::nullptr_t, Point2D>(filePath) {}
16+
convexHull(std::string filePath) : Problem<emptyContext, Point2D>(filePath) {}
1217

1318
private:
1419
// Functions to run the stack
1520
Point2D readInput(std::vector<std::string> line);
1621

17-
std::shared_ptr<std::nullptr_t> initStack();
22+
std::shared_ptr<emptyContext> initStack();
1823

1924
bool popCondition(Point2D data);
2025

21-
void popAction(Data<std::nullptr_t, Point2D> elt);
26+
void popAction(Data<emptyContext, Point2D> elt);
2227

2328
bool pushCondition(Point2D data);
2429

25-
void pushAction(Data<std::nullptr_t, Point2D> elt);
30+
void pushAction(Data<emptyContext, Point2D> elt);
2631
};
2732

2833
/*==============================================================================
2934
Instantiation of a comparison
3035
==============================================================================*/
3136

32-
class comparisonConvexHull : public CompareStacks<int, int> {
37+
class comparisonConvexHull : public CompareStacks<emptyContext, Point2D> {
3338
public:
34-
comparisonConvexHull(std::string filePath) : CompareStacks<int, int>(filePath) {}
39+
comparisonConvexHull(std::string filePath) : CompareStacks<emptyContext, Point2D>(filePath) {}
3540

3641
private:
3742
// Functions to run the stack
38-
int readInput(std::vector<std::string> line);
39-
40-
std::shared_ptr<int> initStack();
43+
Point2D readInput(std::vector<std::string> line);
4144

42-
bool popCondition(int data);
45+
std::shared_ptr<emptyContext> initStack();
4346

44-
void popAction(Data<int, int> elt);
47+
bool popCondition(Point2D data);
4548

46-
bool pushCondition(int data);
49+
void popAction(Data<emptyContext, Point2D> elt);
4750

48-
void pushAction(Data<int, int> elt);
51+
bool pushCondition(Point2D data);
4952

50-
};
53+
void pushAction(Data<emptyContext, Point2D> elt);
54+
};

include/problem.hpp

Lines changed: 4 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ template <class T, class D> class Problem {
3939
// Running the stack
4040
void run();
4141
void run(int limit);
42-
void readPush(int iter = 1);
4342
void push(Data<T, D> elt);
4443
Data<T, D> pop();
4544
Data<T, D> top(int k);
@@ -109,13 +108,9 @@ Problem<T, D>::Problem(std::string fileName) : mIndex(0), mContext(nullptr) {
109108
if (parameters[i].compare("b") == 0) {
110109
foundBuffer = true;
111110
b = stoi(parameters[i + 1]);
112-
//std::cout<<"found buffer "<<b<<std::endl;
113111
}
114112
}
115113

116-
// std::cout << "reading all shit n,p,b,foundP,foundBuffer "<<n<<" "<<p<<"
117-
// "<<b<<" "<<foundP<<" "<<foundBuffer<<std::endl;
118-
119114
if (foundBuffer && !foundP)
120115
throw("Problem<T,D>::Problem(std::string fileName), wrong header format ");
121116
if (!foundP)
@@ -130,31 +125,12 @@ Problem<T, D>::Problem(std::string fileName) : mIndex(0), mContext(nullptr) {
130125
}
131126
}
132127

133-
/*template <class T, class D>
134-
Problem<T,D>::Problem(std::string fileName, int size)
135-
: mIndex(0)
136-
, mContext(nullptr)
137-
, mStack(new NormalStack<T,D> (size)){
138-
mInput.open(fileName, std::ifstream::in);
139-
}
140-
141-
template <class T, class D>
142-
Problem<T,D>::Problem(std::string fileName, int size, int space, int buffer)
143-
: mIndex(0)
144-
, mContext(nullptr){
145-
mInput.open(fileName, std::ifstream::in);
146-
std::streampos position = mInput.tellg();
147-
mStack = std::shared_ptr<Stack<T,D>> (new CompressedStack<T,D> (size, space,
148-
buffer, mContext));
149-
150-
}*/
151-
152128
/*==============================================================================
153129
IO : toString, print, println
154130
==============================================================================*/
155131
template <class T, class D> std::string Problem<T, D>::toString() {
156132
std::string str;
157-
str = "Problem with an actual index of " + std::to_string(mIndex);
133+
str = "Instance with an actual index of " + std::to_string(mIndex);
158134
str += ", with a stack of type\n";
159135
str += mStack->toString();
160136
return str;
@@ -206,30 +182,23 @@ std::vector<std::string> Problem<T, D>::readHeader() {
206182
}
207183

208184
template <class T, class D> void Problem<T, D>::readPush(int iter) {
209-
std::cout << " read push start " << std::endl;
210-
211185
for (int i = 0; i < iter; i++) {
212186
std::streampos position = mInput.tellg();
213187
(*mStack).setPosition(position);
214188
std::vector<std::string> line = readLine();
215189
D data = readInput(line);
216190
mIndex++;
217-
std::cout << " read push read "<<data << std::endl;
218-
219191
Data<T, D> elt(mIndex, data);
220192
pushAction(elt);
221193
push(elt);
222-
std::cout << " pushed read "<<data << std::endl;
223-
224194
}
225-
std::cout << " read push end " << std::endl;
226-
227195
}
228196

229197

230198
/*==============================================================================
231199
Stack Functions: run, push, pop, top, readPush
232200
==============================================================================*/
201+
// TODO: Make popLoop, pushStep and so on functions
233202
template <class T, class D> void Problem<T, D>::run() {
234203
initStackIntern();
235204
while (mInput.good()) {
@@ -240,8 +209,7 @@ template <class T, class D> void Problem<T, D>::run() {
240209
break;
241210
}
242211
D data = readInput(line);
243-
mIndex++; // Might have to move
244-
std::cout << " Starting loop for "<<data << std::endl;
212+
mIndex++;
245213
while ((!emptystack()) && (popCondition(data))) {
246214
Data<T, D> elt = pop();
247215
popAction(elt);
@@ -262,7 +230,7 @@ template <class T, class D> void Problem<T, D>::run(int limit) {
262230
std::vector<std::string> line = readLine();
263231

264232
D data = readInput(line);
265-
mIndex++; // Might have to move
233+
mIndex++;
266234

267235
while ((!emptystack()) && (popCondition(data))) {
268236
Data<T, D> elt = pop();
@@ -276,25 +244,8 @@ template <class T, class D> void Problem<T, D>::run(int limit) {
276244
}
277245
}
278246

279-
template <class T, class D> void Problem<T, D>::readPush(int iter) {
280-
for (int i = 0; i < iter; i++) {
281-
std::streampos position = mInput.tellg();
282-
(*mStack).setPosition(position);
283-
std::vector<std::string> line = readLine();
284-
D data = readInput(line);
285-
mIndex++;
286-
Data<T, D> elt(mIndex, data);
287-
pushAction(elt);
288-
push(elt);
289-
}
290-
}
291-
292247
template <class T, class D> void Problem<T, D>::push(Data<T, D> elt) {
293-
std::cout << " goinbg to push "<<elt.getData() << std::endl;
294-
295248
mStack->push(elt);
296-
std::cout << " pushed "<<elt.getData() << std::endl;
297-
298249
}
299250
template <class T, class D> Data<T, D> Problem<T, D>::pop() {
300251
return mStack->pop(*this);

src/convexHull.cpp

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ double y = std::stof(line[1]);
1616
return p;
1717
}
1818

19-
std::shared_ptr<std::nullptr_t> convexHull::initStack() {
19+
std::shared_ptr<emptyContext> convexHull::initStack() {
2020

2121
std::cout<<"going to read two values "<<std::endl;
2222

@@ -26,7 +26,7 @@ std::shared_ptr<std::nullptr_t> convexHull::initStack() {
2626
std::cout<<"done reading two values "<<std::endl;
2727

2828
// then initialize context (which in this case is NULL everything
29-
std::shared_ptr<std::nullptr_t> context(new std::nullptr_t());
29+
std::shared_ptr<emptyContext> context;
3030
return context;
3131
}
3232
bool convexHull::popCondition(Point2D last) {
@@ -50,41 +50,76 @@ bool convexHull::popCondition(Point2D last) {
5050

5151
return false;
5252
}
53-
void convexHull::popAction(Data<std::nullptr_t, Point2D> elt) {
53+
void convexHull::popAction(Data<emptyContext, Point2D> elt) {
5454
std::cout << elt.getData() << " <<<< Pop!" << std::endl;
5555
// setContext(getContext() );
5656
}
5757
bool convexHull::pushCondition(Point2D data) {
5858
std::cout << data << " <<<< push condition returning true " << std::endl;
5959
return true;
6060
}
61-
void convexHull::pushAction(Data<std::nullptr_t, Point2D> elt) {
61+
void convexHull::pushAction(Data<emptyContext, Point2D> elt) {
6262
std::cout << "convexHullProblem::pushAction Nothing to see here "<<elt.getData()<< std::endl;
6363
}
6464

6565

6666
// copy the same functions again to be able to perform comparisons
67-
int comparisonConvexHull::readInput(std::vector<std::string> line) {
68-
int value = std::stoi(line[0]);
69-
setContext(std::stoi(line[1]));
70-
return value;
67+
Point2D comparisonConvexHull::readInput(std::vector<std::string> line) {
68+
double x = std::stof(line[0]);
69+
double y = std::stof(line[1]);
70+
71+
Point2D p(x,y);
72+
73+
std::cout<<"I JUST READ "<<p<<std::endl;
74+
75+
//setContext(std::stoi(line[1]));
76+
return p;
7177
}
72-
std::shared_ptr<int> comparisonConvexHull::initStack() {
73-
std::shared_ptr<int> context(new int(0));
78+
79+
std::shared_ptr<emptyContext> comparisonConvexHull::initStack() {
80+
81+
std::cout<<"going to read two values "<<std::endl;
82+
83+
//first, read and push two values
84+
readPush(2);
85+
86+
std::cout<<"done reading two values "<<std::endl;
87+
88+
// then initialize context (which in this case is NULL everything
89+
std::shared_ptr<emptyContext> context;
7490
return context;
7591
}
76-
bool comparisonConvexHull::popCondition(int data) {
77-
if (getContext() > 0) {
92+
93+
bool comparisonConvexHull::popCondition(Point2D last) {
94+
Point2D minus1,minus2;
95+
96+
std::cout << last << " <<<< pop condition enter " << std::endl;
97+
98+
// read the two previous elements
99+
minus1=top(1).getData();
100+
minus2=top(2).getData();
101+
102+
std::cout << last << " <<<< pop condition read two befoer "<<minus1<<minus2 << std::endl;
103+
104+
105+
if (Point2D::orientation(minus2,minus1,last)==2 ) {
106+
std::cout << last << " <<<< pop condition returning true " << std::endl;
107+
78108
return true;
79109
}
110+
std::cout << last << " <<<< pop condition returning false " << std::endl;
111+
80112
return false;
81113
}
82-
void comparisonConvexHull::popAction(Data<int, int> elt) {
83-
// std::cout << elt.toString() << " <<<< Pop!" << std::endl;
84-
setContext(getContext() - 1);
114+
115+
void comparisonConvexHull::popAction(Data<emptyContext, Point2D> elt) {
116+
std::cout << elt.getData() << " <<<< Pop!" << std::endl;
117+
// setContext(getContext() );
118+
}
119+
bool comparisonConvexHull::pushCondition(Point2D data) {
120+
std::cout << data << " <<<< push condition returning true " << std::endl;
121+
return true;
85122
}
86-
bool comparisonConvexHull::pushCondition(int data) {
87-
return true;
123+
void comparisonConvexHull::pushAction(Data<emptyContext, Point2D> elt) {
124+
std::cout << "convexHullProblem::pushAction Nothing to see here "<<elt.getData()<< std::endl;
88125
}
89-
void comparisonConvexHull::pushAction(Data<int, int> elt) {
90-
}

0 commit comments

Comments
 (0)