Skip to content

Commit 342007f

Browse files
committed
2 parents c901177 + 194184c commit 342007f

File tree

6 files changed

+185
-19
lines changed

6 files changed

+185
-19
lines changed

.idea/CompressedStacks.cpp.iml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

include/createTestInput.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
#include <fstream>
77
#include <sstream>
88
#include <string>
9-
10-
9+
#include "Point2D.h"
10+
#include <vector>
11+
#include <algorithm>
1112

1213
#ifndef SMARTSTACK_CREATETESTINPUT_H
1314
#define SMARTSTACK_CREATETESTINPUT_H

src/Point2D.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//
2+
// Created by yago on 16/07/05.
3+
//
4+
5+
#include "Point2D.h"
6+
7+
Point2D::Point2D() {}
8+
9+
Point2D::Point2D(double xi, double yi) {
10+
this->x=xi;
11+
this->y=yi;
12+
}
13+
14+
Point2D::Point2D(const Point2D &other) {
15+
this->x=other.x;
16+
this->y=other.y;
17+
}
18+
19+
Point2D::~Point2D() {
20+
}
21+
22+
void Point2D::Set(const double xi, const double yi) {
23+
this->x=xi;
24+
this->y=yi;
25+
}
26+
27+
void Point2D::SetX(double xi) {
28+
this->x=xi;
29+
}
30+
31+
void Point2D::SetY(double yi) {
32+
this->y=yi;
33+
}
34+
35+
double Point2D::GetX() const {
36+
return x;
37+
}
38+
39+
double Point2D::GetY() const {
40+
return y;
41+
}
42+
43+
//std::string Point2D::ToString() const {
44+
// return std::string("%f",x)
45+
//}
46+
47+
void Point2D::operator=(const Point2D &other) {
48+
x=other.x;
49+
y=other.y;
50+
}
51+
52+
bool Point2D::operator==(const Point2D &other) const {
53+
return (x==other.x)&&(y==other.y);
54+
}
55+
56+
bool Point2D::operator!=(const Point2D &other) const {
57+
return !(*this==other);
58+
}
59+
60+
// x order
61+
bool Point2D::operator>(const Point2D &other) const {
62+
if(x!=other.x )return x>other.x;
63+
else return y>other.y;//equal x
64+
}
65+
66+
bool Point2D::operator < (const Point2D &other) const {
67+
//std::cout<<" comparing "<<x<<" and "<<other.x<<" output "<<(x < other.x)<<std::endl;
68+
if(x!=other.x ) return x < other.x;
69+
else return y<other.y;//equal x
70+
}
71+
72+
bool Point2D::operator>=(const Point2D &other) const {
73+
return (*this==other)||(*this>other);
74+
}
75+
76+
bool Point2D::operator<=(const Point2D &other) const {
77+
return (*this==other)||(*this<other);
78+
}

src/Point2D.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//
2+
// Created by yago on 16/07/05.
3+
//
4+
5+
#include <iostream>
6+
#include<string>
7+
8+
#ifndef SMARTSTACK_POINT2D_H
9+
#define SMARTSTACK_POINT2D_H
10+
11+
12+
class Point2D {
13+
14+
public:
15+
double x;
16+
double y;
17+
18+
Point2D();
19+
Point2D(double x, double y);
20+
// copy constructor
21+
Point2D(const Point2D &other);
22+
23+
~Point2D();
24+
25+
void Set(const double x, const double y);
26+
27+
28+
void SetX(double x);
29+
void SetY(double y);
30+
double GetX() const;
31+
double GetY() const;
32+
33+
// std::string ToString() const;
34+
35+
// assignment operator
36+
void operator= (const Point2D &other);
37+
38+
bool operator== (const Point2D &other) const;
39+
bool operator!= (const Point2D &other) const;
40+
bool operator> (const Point2D &other) const;
41+
bool operator< (const Point2D &other) const;
42+
bool operator>= (const Point2D &other) const;
43+
bool operator<= (const Point2D &other) const;
44+
45+
};
46+
47+
48+
#endif //SMARTSTACK_POINT2D_H

src/createTestInput.cpp

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ void createTestInput::createTestInputFiles(int code, string fileName,int n,int p
1313
ofstream outfile (fileName.c_str());
1414
srand(time(NULL));
1515

16+
// First write the problem parameters
17+
outfile << n << "," << p << endl;
18+
1619
switch(code){
1720
case 0 : // push only test
1821
{
19-
// First write the problem parameters
20-
outfile << n << "," << p << endl;
2122
// now create the actual file
2223
// pairs of elements (x,0 means push ) (0,1) means pop one (-1,-1) means pop the rest of the stack
2324

@@ -42,13 +43,10 @@ void createTestInput::createTestInputFiles(int code, string fileName,int n,int p
4243
i++;
4344
}
4445
outfile << -1 << "," << -1 << endl;
45-
break; //optional
46+
break;
4647
}
4748
case 1 :
4849
{
49-
// First write the problem parameters
50-
outfile << n << "," << p << endl;
51-
5250
// now create the actual file
5351
// pairs of elements (x,0 means push ) (0,y) means pop y times (-1,-1) means pop the rest of the stack
5452

@@ -82,11 +80,54 @@ void createTestInput::createTestInputFiles(int code, string fileName,int n,int p
8280
outfile << number << "," << numPops << endl;
8381
i++;
8482
}
83+
break;
84+
}
85+
case 2 :
86+
{
87+
// now create the actual file
88+
// pairs of elements (x,0 means push ) (0,1) means pop one (-1,-1) means pop the rest of the stack
8589

86-
outfile << -1 << "," << -1 << endl;
87-
break; //optional
90+
std::vector<Point2D> pointsToSort = vector<Point2D>();
91+
92+
int i = 0;
93+
while (i < n) {
94+
95+
// create output for the convex hull problem.
96+
// in this case, max and min stand for the maximum and minimum values of x and y
97+
// generate a random point in the (min,max)2 range
98+
99+
double randomx = (double) rand() / RAND_MAX;
100+
randomx=min+(max-min)*randomx;
101+
double randomy = (double) rand() / RAND_MAX;
102+
randomy=min+(max-min)*randomy;
103+
104+
// cout<<"generated point "<<randomx<<" "<<randomy<<" "<<pointsToSort.size()<<endl;
105+
106+
pointsToSort.push_back(Point2D(randomx,randomy));
107+
i++;
108+
}
109+
110+
111+
//sort the vector
112+
std::sort(pointsToSort.begin(),pointsToSort.end());
113+
114+
115+
116+
// add first the point (min,min)
117+
outfile << min << "," << min << endl;
118+
119+
// add the sorted points
120+
for(int i=0;i<pointsToSort.size();i++)
121+
{
122+
outfile << pointsToSort[i].GetX() << "," << pointsToSort[i].GetY() << endl;
123+
}
124+
125+
// add finally (max,min)
126+
outfile << max << "," << min << endl;
127+
128+
break;
88129
}
89-
default : //Optional
130+
default : //Optional
90131
{throw (" createTestInput::createTestInputFiles WRONG CODE ");}
91132
}
92133

src/main.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,14 @@ void testProblem()
114114
}
115115

116116
// Main
117-
int main(int argc, char const *argv[]) {
118-
// Test of different classes composing the stacks
119-
// testSign();
120-
// testData();
121-
// testNormalStack();
122-
// testCompressedStack();
117+
int main() {
123118
testProblem();
124119

125120
// Intances generation, please comment when not in use
126121
/* createTestInput ct=createTestInput();
127-
ct.createTestInputFiles(0,"../instances/pushOnlyInput.csv",1000,3, 5, 100, 0.2 );
128-
ct.createTestInputFiles(1,"../instances/CTInput.csv",1000,3);
122+
ct.createTestInputFiles(0,"/home/yago/code/CompressedStacks.cpp/instances/pushOnlyInput.csv",1000,3, 5, 100, 0.2 );
123+
ct.createTestInputFiles(1,"/home/yago/code/CompressedStacks.cpp/instances/CTInput.csv",1000,3);
124+
ct.createTestInputFiles(2,"/home/yago/code/CompressedStacks.cpp/instances/CHInput.csv",100,3,-1, 11);
129125
*/
130126

131127
return 0;

0 commit comments

Comments
 (0)