|
1 |
| -// UpperHull : Definition |
2 |
| -#ifndef UPPERHULL |
3 |
| -#define UPPERHULL |
4 |
| - |
5 |
| -/*============================================================================== |
6 |
| - Includes |
7 |
| -==============================================================================*/ |
8 |
| -#include "../../../include/stackAlgo.hpp" |
9 |
| -#include "point2D.hpp" |
10 |
| - |
11 |
| -double pops = 0; |
12 |
| -double total = 0; |
13 |
| - |
14 |
| -/*============================================================================== |
15 |
| - Empty type for the context (empty for the Convex Hull problem) |
16 |
| -==============================================================================*/ |
17 |
| -class emptyContext {}; |
18 |
| - |
19 |
| -/*============================================================================== |
20 |
| - Instantiation of a problem |
21 |
| -==============================================================================*/ |
22 |
| -template <class I> |
23 |
| -class UpperHull : public StackAlgo<emptyContext, Point2D, I> { |
24 |
| -public: |
25 |
| - UpperHull(std::string filePath) |
26 |
| - : StackAlgo<emptyContext, Point2D, I>(filePath) {} |
27 |
| - |
28 |
| -private: |
29 |
| - // Functions to run the stack |
30 |
| - Point2D readInput(std::vector<std::string> line); |
31 |
| - std::shared_ptr<emptyContext> initStack(); |
32 |
| - |
33 |
| - bool popCondition(Point2D data); |
34 |
| - void prePop(Point2D data); |
35 |
| - void postPop(Point2D data, Data<emptyContext, Point2D, I> elt); |
36 |
| - void noPop(Point2D data); |
37 |
| - |
38 |
| - bool pushCondition(Point2D data); |
39 |
| - void prePush(Data<emptyContext, Point2D, I> elt); |
40 |
| - void postPush(Data<emptyContext, Point2D, I> elt); |
41 |
| - void noPush(Point2D data); |
42 |
| - |
43 |
| - void reportStack(); |
44 |
| -}; |
45 |
| - |
46 |
| -/*============================================================================== |
47 |
| - Stack functions to define the Convex Hull algorithm with Compressed Stack |
48 |
| - * readInput |
49 |
| - * initStack |
50 |
| - * popCondition |
51 |
| - * prePop |
52 |
| - * postPop |
53 |
| - * noPop |
54 |
| - * pushCondition |
55 |
| - * prePush |
56 |
| - * postPush |
57 |
| - * noPush |
58 |
| - * reportStack |
59 |
| -==============================================================================*/ |
60 |
| -template <class I> |
61 |
| -Point2D UpperHull<I>::readInput(std::vector<std::string> line) { |
62 |
| - double x = std::stof(line[0]); |
63 |
| - double y = std::stof(line[1]); |
64 |
| - |
65 |
| - Point2D p(x, y); |
66 |
| - |
67 |
| - // std::cout << "I JUST READ " << p << std::endl; |
68 |
| - return p; |
69 |
| -} |
70 |
| - |
71 |
| -template <class I> std::shared_ptr<emptyContext> UpperHull<I>::initStack() { |
72 |
| - |
73 |
| - std::cout << "going to read two values " << std::endl; |
74 |
| - |
75 |
| - // first, read and push two values |
76 |
| - StackAlgo<emptyContext, Point2D, I>::readPush(2); |
77 |
| - |
78 |
| - std::cout << "done reading two values " << std::endl; |
79 |
| - |
80 |
| - // then initialize context (which in this case is NULL everything |
81 |
| - std::shared_ptr<emptyContext> context; |
82 |
| - return context; |
83 |
| -} |
84 |
| - |
85 |
| -template <class I> bool UpperHull<I>::popCondition(Point2D last) { |
86 |
| - Point2D minus1, minus2; |
87 |
| - total++; |
88 |
| - std::cout << std::endl << last << " <<<< pop condition enter " << std::endl; |
89 |
| - StackAlgo<emptyContext, Point2D, I>::println(); |
90 |
| - |
91 |
| - // read the two previous elements |
92 |
| - minus1 = StackAlgo<emptyContext, Point2D, I>::top(1).getData(); |
93 |
| - if (StackAlgo<emptyContext, Point2D, I>::mStack->getBufferLength() < 2) { |
94 |
| - return true; |
95 |
| - } |
96 |
| - |
97 |
| - minus2 = StackAlgo<emptyContext, Point2D, I>::top(2).getData(); |
98 |
| - |
99 |
| - std::cout << last << " <<<< pop condition read two before " << minus2 |
100 |
| - << minus1 << std::endl; |
101 |
| - |
102 |
| - if (Point2D::orientation(minus2, minus1, last) == 1) { |
103 |
| - pops++; |
104 |
| - std::cout << last |
105 |
| - << " <<<< " |
106 |
| - " pop condition returning true " |
107 |
| - << pops / total << " tot " << total << std::endl; |
108 |
| - |
109 |
| - return true; |
110 |
| - } |
111 |
| - std::cout << last << " " |
112 |
| - " <<<< pop condition returning false " |
113 |
| - << pops / total << " tot " << total << std::endl; |
114 |
| - |
115 |
| - return false; |
116 |
| -} |
117 |
| -template <class I> void UpperHull<I>::prePop(Point2D data) {} |
118 |
| -template <class I> |
119 |
| -void UpperHull<I>::postPop(Point2D data, Data<emptyContext, Point2D, I> elt) { |
120 |
| - std::cout << elt.getData() << " <<<< (post-)Pop!" << std::endl; |
121 |
| -} |
122 |
| -template <class I> void UpperHull<I>::noPop(Point2D data) {} |
123 |
| - |
124 |
| -template <class I> bool UpperHull<I>::pushCondition(Point2D data) { |
125 |
| - std::cout << data << " <<<< push condition returning true " << std::endl; |
126 |
| - return true; |
127 |
| -} |
128 |
| -template <class I> |
129 |
| -void UpperHull<I>::prePush(Data<emptyContext, Point2D, I> elt) {} |
130 |
| -template <class I> |
131 |
| -void UpperHull<I>::postPush(Data<emptyContext, Point2D, I> elt) { |
132 |
| - std::cout << "UpperHullStackAlgo::pushAction Nothing to see here " |
133 |
| - << elt.getData() << std::endl; |
134 |
| -} |
135 |
| -template <class I> void UpperHull<I>::noPush(Point2D data) {} |
136 |
| - |
137 |
| -template <class I> void UpperHull<I>::reportStack() {} |
138 |
| - |
139 |
| -#endif // UPPERHULL |
| 1 | +// UpperHull : Definition |
| 2 | +#ifndef UPPERHULL |
| 3 | +#define UPPERHULL |
| 4 | + |
| 5 | +/*============================================================================== |
| 6 | + Includes |
| 7 | +==============================================================================*/ |
| 8 | +#include "../../../include/stackAlgo.hpp" |
| 9 | +#include "point2D.hpp" |
| 10 | + |
| 11 | +double pops = 0; |
| 12 | +double total = 0; |
| 13 | + |
| 14 | +/*============================================================================== |
| 15 | + Empty type for the context (empty for the Convex Hull problem) |
| 16 | +==============================================================================*/ |
| 17 | +class emptyContext {}; |
| 18 | + |
| 19 | +/*============================================================================== |
| 20 | + Instantiation of a problem |
| 21 | +==============================================================================*/ |
| 22 | +template <class I> |
| 23 | +class UpperHull : public StackAlgo<emptyContext, Point2D, I> { |
| 24 | +public: |
| 25 | + UpperHull(std::string filePath) |
| 26 | + : StackAlgo<emptyContext, Point2D, I>(filePath) {} |
| 27 | + |
| 28 | +private: |
| 29 | + // Functions to run the stack |
| 30 | + Point2D readInput(std::vector<std::string> line); |
| 31 | + std::shared_ptr<emptyContext> initStack(); |
| 32 | + |
| 33 | + bool popCondition(Point2D data); |
| 34 | + void prePop(Point2D data); |
| 35 | + void postPop(Point2D data, Data<emptyContext, Point2D, I> elt); |
| 36 | + void noPop(Point2D data); |
| 37 | + |
| 38 | + bool pushCondition(Point2D data); |
| 39 | + void prePush(Data<emptyContext, Point2D, I> elt); |
| 40 | + void postPush(Data<emptyContext, Point2D, I> elt); |
| 41 | + void noPush(Point2D data); |
| 42 | + |
| 43 | + void reportStack(); |
| 44 | +}; |
| 45 | + |
| 46 | +/*============================================================================== |
| 47 | + Stack functions to define the Convex Hull algorithm with Compressed Stack |
| 48 | + * readInput |
| 49 | + * initStack |
| 50 | + * popCondition |
| 51 | + * prePop |
| 52 | + * postPop |
| 53 | + * noPop |
| 54 | + * pushCondition |
| 55 | + * prePush |
| 56 | + * postPush |
| 57 | + * noPush |
| 58 | + * reportStack |
| 59 | +==============================================================================*/ |
| 60 | +template <class I> |
| 61 | +Point2D UpperHull<I>::readInput(std::vector<std::string> line) { |
| 62 | + double x = std::stof(line[0]); |
| 63 | + double y = std::stof(line[1]); |
| 64 | + |
| 65 | + Point2D p(x, y); |
| 66 | + |
| 67 | + // std::cout << "I JUST READ " << p << std::endl; |
| 68 | + return p; |
| 69 | +} |
| 70 | + |
| 71 | +template <class I> std::shared_ptr<emptyContext> UpperHull<I>::initStack() { |
| 72 | + |
| 73 | + std::cout << "going to read two values " << std::endl; |
| 74 | + |
| 75 | + // first, read and push two values |
| 76 | + StackAlgo<emptyContext, Point2D, I>::readPush(2); |
| 77 | + |
| 78 | + std::cout << "done reading two values " << std::endl; |
| 79 | + |
| 80 | + // then initialize context (which in this case is NULL everything |
| 81 | + std::shared_ptr<emptyContext> context; |
| 82 | + return context; |
| 83 | +} |
| 84 | + |
| 85 | +template <class I> bool UpperHull<I>::popCondition(Point2D last) { |
| 86 | + Point2D minus1, minus2; |
| 87 | + total++; |
| 88 | + std::cout << std::endl << last << " <<<< pop condition enter " << std::endl; |
| 89 | + StackAlgo<emptyContext, Point2D, I>::println(); |
| 90 | + |
| 91 | + // read the two previous elements |
| 92 | + minus1 = StackAlgo<emptyContext, Point2D, I>::top(1).getData(); |
| 93 | + if (StackAlgo<emptyContext, Point2D, I>::mStack->getBufferLength() < 2) { |
| 94 | + return true; |
| 95 | + } |
| 96 | + |
| 97 | + minus2 = StackAlgo<emptyContext, Point2D, I>::top(2).getData(); |
| 98 | + |
| 99 | + std::cout << last << " <<<< pop condition read two before " << minus2 |
| 100 | + << minus1 << std::endl; |
| 101 | + |
| 102 | + if (Point2D::orientation(minus2, minus1, last) == 1) { |
| 103 | + pops++; |
| 104 | + std::cout << last |
| 105 | + << " <<<< " |
| 106 | + " pop condition returning true " |
| 107 | + << pops / total << " tot " << total << std::endl; |
| 108 | + |
| 109 | + return true; |
| 110 | + } |
| 111 | + std::cout << last << " " |
| 112 | + " <<<< pop condition returning false " |
| 113 | + << pops / total << " tot " << total << std::endl; |
| 114 | + |
| 115 | + return false; |
| 116 | +} |
| 117 | +template <class I> void UpperHull<I>::prePop(Point2D data) {} |
| 118 | +template <class I> |
| 119 | +void UpperHull<I>::postPop(Point2D data, Data<emptyContext, Point2D, I> elt) { |
| 120 | + std::cout << elt.getData() << " <<<< (post-)Pop!" << std::endl; |
| 121 | +} |
| 122 | +template <class I> void UpperHull<I>::noPop(Point2D data) {} |
| 123 | + |
| 124 | +template <class I> bool UpperHull<I>::pushCondition(Point2D data) { |
| 125 | + std::cout << data << " <<<< push condition returning true " << std::endl; |
| 126 | + return true; |
| 127 | +} |
| 128 | +template <class I> |
| 129 | +void UpperHull<I>::prePush(Data<emptyContext, Point2D, I> elt) {} |
| 130 | +template <class I> |
| 131 | +void UpperHull<I>::postPush(Data<emptyContext, Point2D, I> elt) { |
| 132 | + std::cout << "UpperHullStackAlgo::pushAction Nothing to see here " |
| 133 | + << elt.getData() << std::endl; |
| 134 | +} |
| 135 | +template <class I> void UpperHull<I>::noPush(Point2D data) {} |
| 136 | + |
| 137 | +template <class I> void UpperHull<I>::reportStack() {} |
| 138 | + |
| 139 | +#endif // UPPERHULL |
0 commit comments