Skip to content

Commit b9b4f2d

Browse files
committed
pushonly and CT seem to work, CH has some problems with push (maybe because of buffer?)
1 parent 6c44ac8 commit b9b4f2d

File tree

11 files changed

+304
-60
lines changed

11 files changed

+304
-60
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ get_property(inc_dirs DIRECTORY PROPERTY INCLUDE_DIRECTORIES)
5353
message("inc_dirs = ${inc_dirs}")
5454

5555
# Executable
56-
add_executable(smartstack ${SOURCES})
56+
add_executable(smartstack ${SOURCES} src/convexHull.cpp)
5757

5858

5959
# Description of the different builds

include/Point2D.hpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <iostream>
66
#include <string>
77

8+
89
#ifndef SMARTSTACK_POINT2D
910
#define SMARTSTACK_POINT2D
1011

@@ -28,7 +29,9 @@ class Point2D {
2829
double GetX() const;
2930
double GetY() const;
3031

31-
// std::string ToString() const;
32+
//std::string toString(Point2D&) ;
33+
34+
static int orientation(Point2D p1, Point2D p2, Point2D p3);
3235

3336
// assignment operator
3437
void operator=(const Point2D &other);
@@ -39,6 +42,15 @@ class Point2D {
3942
bool operator<(const Point2D &other) const;
4043
bool operator>=(const Point2D &other) const;
4144
bool operator<=(const Point2D &other) const;
45+
46+
void write(std::ostream& os);
47+
48+
friend std::ostream& operator<<(std::ostream& os,Point2D p)
49+
{
50+
p.write(os);
51+
return os;
52+
}
53+
4254
};
4355

4456
#endif // SMARTSTACK_POINT2D

include/compressedStack.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,12 +451,16 @@ 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;
454455
// update the buffer (if buffer size is bigger than 0)
455456
SPData<T, D> ptr_elt = std::make_shared<Data<T, D>>(elt);
457+
std::cout << " inside comp stack made buffer "<< std::endl;
456458
mBuffer.push(ptr_elt);
457459
// update the explicit Blocks, with possibly shifting first to second
460+
std::cout << " inside comp stack going to push explicit "<< std::endl;
458461
pushExplicit(ptr_elt);
459462
// update the compressed Blocks at each levels (including fully compressed)
463+
std::cout << " inside comp stack pushed explicit "<< std::endl;
460464
for (int lvl = mDepth - 1; lvl > 0; lvl--) {
461465
int headIndex = getLast(lvl);
462466
pushCompressed(ptr_elt, lvl, headIndex);

include/convexHull.hpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "problem.hpp"
2+
#include "compare.hpp"
3+
#include "Point2D.hpp"
4+
5+
6+
/*==============================================================================
7+
Instantiation of a problem
8+
==============================================================================*/
9+
class convexHull : public Problem<std::nullptr_t, Point2D> {
10+
public:
11+
convexHull(std::string filePath) : Problem<std::nullptr_t, Point2D>(filePath) {}
12+
13+
private:
14+
// Functions to run the stack
15+
Point2D readInput(std::vector<std::string> line);
16+
17+
std::shared_ptr<std::nullptr_t> initStack();
18+
19+
bool popCondition(Point2D data);
20+
21+
void popAction(Data<std::nullptr_t, Point2D> elt);
22+
23+
bool pushCondition(Point2D data);
24+
25+
void pushAction(Data<std::nullptr_t, Point2D> elt);
26+
};
27+
28+
/*==============================================================================
29+
Instantiation of a comparison
30+
==============================================================================*/
31+
32+
class comparisonConvexHull : public CompareStacks<int, int> {
33+
public:
34+
comparisonConvexHull(std::string filePath) : CompareStacks<int, int>(filePath) {}
35+
36+
private:
37+
// Functions to run the stack
38+
int readInput(std::vector<std::string> line);
39+
40+
std::shared_ptr<int> initStack();
41+
42+
bool popCondition(int data);
43+
44+
void popAction(Data<int, int> elt);
45+
46+
bool pushCondition(int data);
47+
48+
void pushAction(Data<int, int> elt);
49+
50+
};

include/createTestInput.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class createTestInput {
1818
public:
1919
createTestInput() {}
2020

21-
void createTestInputFiles(int code, std::string fileName, int n, int p,
21+
void createTestInputFiles(int code, int stacktype, std::string fileName, int n, int p,
2222
int min = 0, int max = 100, double prob = 0);
2323
};
2424

include/data.hpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ template <class T, class D> class Data {
3232
public:
3333
// IO
3434
std::string toString();
35+
D getData();
36+
3537

3638
private:
3739
// Constructor
@@ -72,7 +74,7 @@ template <class T, class D> ExplicitPointer<T, D> initExplicitPointer() {
7274
==============================================================================*/
7375
template <class T, class D> std::string Data<T, D>::toString() {
7476
std::string str;
75-
str = std::to_string(mIndex) + "<-" + std::to_string(mData);
77+
str = std::to_string(mIndex) + "<-" /*+ std::to_string(mData)*/;
7678
return str;
7779
}
7880

@@ -103,4 +105,9 @@ std::string explicitPointerToString(ExplicitPointer<T, D> xpointer) {
103105
return str;
104106
}
105107

108+
template <class T, class D> D Data<T, D>::getData() {
109+
return mData;
110+
}
111+
112+
106113
#endif /* DATA */

include/problem.hpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,14 @@ template <class T, class D> class Problem {
5050

5151
// Getters
5252
T getContext();
53+
5354
int getIndex();
5455

5556
// IO
5657
std::string toString();
5758
void print();
5859
void println();
60+
void readPush(int iter = 1);
5961

6062
protected:
6163
void initStackIntern();
@@ -106,6 +108,7 @@ Problem<T, D>::Problem(std::string fileName) : mIndex(0), mContext(nullptr) {
106108
if (parameters[i].compare("b") == 0) {
107109
foundBuffer = true;
108110
b = stoi(parameters[i + 1]);
111+
//std::cout<<"found buffer "<<b<<std::endl;
109112
}
110113
}
111114

@@ -201,6 +204,28 @@ std::vector<std::string> Problem<T, D>::readHeader() {
201204
return line;
202205
}
203206

207+
template <class T, class D> void Problem<T, D>::readPush(int iter) {
208+
std::cout << " read push start " << std::endl;
209+
210+
for (int i = 0; i < iter; i++) {
211+
std::streampos position = mInput.tellg();
212+
(*mStack).setPosition(position);
213+
std::vector<std::string> line = readLine();
214+
D data = readInput(line);
215+
mIndex++;
216+
std::cout << " read push read "<<data << std::endl;
217+
218+
Data<T, D> elt(mIndex, data);
219+
pushAction(elt);
220+
push(elt);
221+
std::cout << " pushed read "<<data << std::endl;
222+
223+
}
224+
std::cout << " read push end " << std::endl;
225+
226+
}
227+
228+
204229
/*==============================================================================
205230
Stack Functions: run, push, pop, top
206231
==============================================================================*/
@@ -215,6 +240,7 @@ template <class T, class D> void Problem<T, D>::run() {
215240
}
216241
D data = readInput(line);
217242
mIndex++; // Might have to move
243+
std::cout << " Starting loop for "<<data << std::endl;
218244
while ((!emptystack()) && (popCondition(data))) {
219245
Data<T, D> elt = pop();
220246
popAction(elt);
@@ -250,7 +276,11 @@ template <class T, class D> void Problem<T, D>::run(int limit) {
250276
}
251277

252278
template <class T, class D> void Problem<T, D>::push(Data<T, D> elt) {
279+
std::cout << " goinbg to push "<<elt.getData() << std::endl;
280+
253281
mStack->push(elt);
282+
std::cout << " pushed "<<elt.getData() << std::endl;
283+
254284
}
255285
template <class T, class D> Data<T, D> Problem<T, D>::pop() {
256286
return mStack->pop(*this);

src/Point2D.cpp

Lines changed: 58 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,68 +7,97 @@
77
Point2D::Point2D() {}
88

99
Point2D::Point2D(double xi, double yi) {
10-
this->x = xi;
11-
this->y = yi;
10+
this->x=xi;
11+
this->y=yi;
1212
}
1313

1414
Point2D::Point2D(const Point2D &other) {
15-
this->x = other.x;
16-
this->y = other.y;
15+
this->x=other.x;
16+
this->y=other.y;
1717
}
1818

19-
Point2D::~Point2D() {}
19+
Point2D::~Point2D() {
20+
}
2021

2122
void Point2D::Set(const double xi, const double yi) {
22-
this->x = xi;
23-
this->y = yi;
23+
this->x=xi;
24+
this->y=yi;
25+
}
26+
27+
void Point2D::SetX(double xi) {
28+
this->x=xi;
2429
}
2530

26-
void Point2D::SetX(double xi) { this->x = xi; }
31+
void Point2D::SetY(double yi) {
32+
this->y=yi;
33+
}
2734

28-
void Point2D::SetY(double yi) { this->y = yi; }
35+
double Point2D::GetX() const {
36+
return x;
37+
}
2938

30-
double Point2D::GetX() const { return x; }
39+
double Point2D::GetY() const {
40+
return y;
41+
}
3142

32-
double Point2D::GetY() const { return y; }
43+
/*std::string Point2D::toString(Point2D &p) {
44+
return "( "+ std::to_string(p.x) + " , " +std::to_string(p.y)+" )";
45+
}*/
3346

34-
// std::string Point2D::ToString() const {
47+
48+
// To find orientation of ordered triplet (p1, p2, p3).
49+
// The function returns following values
50+
// 0 --> p, q and r are colinear
51+
// 1 --> Clockwise
52+
// 2 --> Counterclockwise
53+
// taken from http://www.geeksforgeeks.org/orientation-3-ordered-points/
54+
int Point2D::orientation(Point2D p1, Point2D p2, Point2D p3)
55+
{
56+
int val = (p2.y - p1.y) * (p3.x - p2.x) -
57+
(p2.x - p1.x) * (p3.y - p2.y);
58+
59+
if (val == 0) return 0; // colinear
60+
return (val > 0)? 1: 2; // clock or counterclock wise
61+
}
62+
63+
//std::string Point2D::ToString() const {
3564
// return std::string("%f",x)
3665
//}
3766

3867
void Point2D::operator=(const Point2D &other) {
39-
x = other.x;
40-
y = other.y;
68+
x=other.x;
69+
y=other.y;
4170
}
4271

4372
bool Point2D::operator==(const Point2D &other) const {
44-
return (x == other.x) && (y == other.y);
73+
return (x==other.x)&&(y==other.y);
4574
}
4675

4776
bool Point2D::operator!=(const Point2D &other) const {
48-
return !(*this == other);
77+
return !(*this==other);
4978
}
5079

5180
// x order
5281
bool Point2D::operator>(const Point2D &other) const {
53-
if (x != other.x)
54-
return x > other.x;
55-
else
56-
return y > other.y; // equal x
82+
if(x!=other.x )return x>other.x;
83+
else return y>other.y;//equal x
5784
}
5885

59-
bool Point2D::operator<(const Point2D &other) const {
60-
// std::cout<<" comparing "<<x<<" and "<<other.x<<" output "<<(x <
61-
// other.x)<<std::endl;
62-
if (x != other.x)
63-
return x < other.x;
64-
else
65-
return y < other.y; // equal x
86+
bool Point2D::operator < (const Point2D &other) const {
87+
//std::cout<<" comparing "<<x<<" and "<<other.x<<" output "<<(x < other.x)<<std::endl;
88+
if(x!=other.x ) return x < other.x;
89+
else return y<other.y;//equal x
6690
}
6791

6892
bool Point2D::operator>=(const Point2D &other) const {
69-
return (*this == other) || (*this > other);
93+
return (*this==other)||(*this>other);
7094
}
7195

7296
bool Point2D::operator<=(const Point2D &other) const {
73-
return (*this == other) || (*this < other);
97+
return (*this==other)||(*this<other);
7498
}
99+
100+
void Point2D::write(std::ostream& os)
101+
{
102+
os << "( "<<x<<" , "<<y<<" )"<<std::endl;
103+
}

0 commit comments

Comments
 (0)