Skip to content

Commit 4589be5

Browse files
committed
Simplification of pop functions and code syntax improvement (clang based)
1 parent d96d0c0 commit 4589be5

File tree

14 files changed

+913
-934
lines changed

14 files changed

+913
-934
lines changed

include/Point2D.hpp

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,42 @@
33
//
44

55
#include <iostream>
6-
#include<string>
6+
#include <string>
77

88
#ifndef SMARTSTACK_POINT2D
99
#define SMARTSTACK_POINT2D
1010

11-
1211
class Point2D {
1312

1413
public:
15-
double x;
16-
double y;
17-
18-
Point2D();
19-
Point2D(double x, double y);
20-
// copy constructor
21-
Point2D(const Point2D &other);
14+
double x;
15+
double y;
2216

23-
~Point2D();
17+
Point2D();
18+
Point2D(double x, double y);
19+
// copy constructor
20+
Point2D(const Point2D &other);
2421

25-
void Set(const double x, const double y);
22+
~Point2D();
2623

24+
void Set(const double x, const double y);
2725

28-
void SetX(double x);
29-
void SetY(double y);
30-
double GetX() const;
31-
double GetY() const;
26+
void SetX(double x);
27+
void SetY(double y);
28+
double GetX() const;
29+
double GetY() const;
3230

33-
// std::string ToString() const;
31+
// std::string ToString() const;
3432

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;
33+
// assignment operator
34+
void operator=(const Point2D &other);
4435

36+
bool operator==(const Point2D &other) const;
37+
bool operator!=(const Point2D &other) const;
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;
4542
};
4643

47-
48-
#endif //SMARTSTACK_POINT2D
44+
#endif // SMARTSTACK_POINT2D

include/buffer.hpp

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
Includes
66
==============================================================================*/
77
#include "data.hpp"
8-
#include <vector>
9-
#include <string>
108
#include <iostream>
119
#include <memory>
10+
#include <string>
11+
#include <vector>
1212

1313
/*==============================================================================
1414
Class : template (D datas)
@@ -17,69 +17,66 @@
1717
Friends -> CompressedStack
1818
<-
1919
==============================================================================*/
20-
template <class T, class D> class CompressedStack; // Required for the friendship
21-
template <class T, class D> class Signature; // Required for the friendship
22-
template <class T, class D> class Component; // Required for the friendship
23-
template <class T, class D> class NormalStack; // Required for the friendship
2420
template <class T, class D>
25-
class Buffer{
26-
friend class CompressedStack<T,D>;
27-
friend class NormalStack<T,D>;
28-
friend class Signature<T,D>;
29-
friend class Component<T,D>;
21+
class CompressedStack; // Required for the friendship
22+
template <class T, class D> class Signature; // Required for the friendship
23+
template <class T, class D> class Component; // Required for the friendship
24+
template <class T, class D> class NormalStack; // Required for the friendship
25+
template <class T, class D> class Buffer {
26+
friend class CompressedStack<T, D>;
27+
friend class NormalStack<T, D>;
28+
friend class Signature<T, D>;
29+
friend class Component<T, D>;
3030

3131
private:
3232
// Constructor
33-
Buffer<T,D>(int size = 0);
33+
Buffer<T, D>(int size = 0);
3434

3535
// Getters
36-
Data<T,D> top(int k);
37-
SPData<T,D> getPointer(int k);
36+
Data<T, D> top(int k);
37+
SPData<T, D> topPointer(int k);
3838

3939
// Setters
4040
void setStart();
41-
void setData(SPData<T,D> elt, int index);
41+
void setData(SPData<T, D> elt, int index);
4242

4343
// Push and Pop
44-
void push(SPData<T,D> elt);
45-
void pop(SPData<T,D> elt);
44+
void push(SPData<T, D> elt);
45+
void pop(SPData<T, D> elt);
4646

4747
// IO
4848
std::string toString();
4949

5050
// Members
5151
int mSize;
5252
int mStart;
53-
ExplicitPointer<T,D> mExplicit;
53+
ExplicitPointer<T, D> mExplicit;
5454
};
5555

5656
/*==============================================================================
5757
Constructors
5858
==============================================================================*/
59-
template <class T, class D>
60-
Buffer<T,D>::Buffer(int size){
59+
template <class T, class D> Buffer<T, D>::Buffer(int size) {
6160
mSize = size;
6261
mStart = 0;
6362

64-
ExplicitPointer<T,D> xplicit = initExplicitPointer<T,D>();
63+
ExplicitPointer<T, D> xplicit = initExplicitPointer<T, D>();
6564
xplicit.reserve(size);
6665
mExplicit = xplicit;
6766
}
6867

6968
/*==============================================================================
7069
Getters
7170
==============================================================================*/
72-
template <class T, class D>
73-
Data<T,D> Buffer<T,D>::top(int k){
71+
template <class T, class D> Data<T, D> Buffer<T, D>::top(int k) {
7472
if (k < mSize) {
7573
int index = (k + mStart - 1) % mSize; // -1 match the start of vectors at 0
7674
return *(mExplicit[index]);
7775
}
7876
throw "Access to a top element bigger than the size of the buffer";
7977
}
8078

81-
template <class T, class D>
82-
SPData<T,D> Buffer<T,D>::getPointer(int k){
79+
template <class T, class D> SPData<T, D> Buffer<T, D>::topPointer(int k) {
8380
if (k < mSize) {
8481
int index = (k + mStart - 1) % mSize; // -1 match the start of vectors at 0
8582
return mExplicit[index];
@@ -90,37 +87,33 @@ SPData<T,D> Buffer<T,D>::getPointer(int k){
9087
/*==============================================================================
9188
Setters
9289
==============================================================================*/
93-
template <class T, class D>
94-
void Buffer<T,D>::setStart(){
90+
template <class T, class D> void Buffer<T, D>::setStart() {
9591
mStart = (mStart + 1) % mSize;
9692
}
9793
template <class T, class D>
98-
void Buffer<T,D>::setData(SPData<T,D> elt, int id){
94+
void Buffer<T, D>::setData(SPData<T, D> elt, int id) {
9995
int index = (id + mStart - 1) % mSize; // -1 match the start of vectors at 0
10096
mExplicit[index] = elt;
10197
}
10298

10399
/*==============================================================================
104100
Stack Functions: push, pop
105101
==============================================================================*/
106-
template <class T, class D>
107-
void Buffer<T,D>::push(SPData<T,D> elt){
102+
template <class T, class D> void Buffer<T, D>::push(SPData<T, D> elt) {
108103
if (mSize > 0) {
109-
setData(elt, mStart+1);
104+
setData(elt, mStart + 1);
110105
}
111106
}
112107

113-
template <class T, class D>
114-
void Buffer<T,D>::pop(SPData<T,D> elt){
108+
template <class T, class D> void Buffer<T, D>::pop(SPData<T, D> elt) {
115109
setStart();
116110
setData(elt, mSize);
117111
}
118112

119113
/*==============================================================================
120114
IO : toString
121115
==============================================================================*/
122-
template <class T, class D>
123-
std::string Buffer<T,D>::toString(){
116+
template <class T, class D> std::string Buffer<T, D>::toString() {
124117
std::string str = "";
125118
if (mSize > 0) {
126119
str = "\t\tBuffer size is " + std::to_string(mSize);

include/compare.hpp

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
/*==============================================================================
55
Includes
66
==============================================================================*/
7-
#include "problem.hpp"
87
#include "data.hpp"
8+
#include "problem.hpp"
99

1010
/*==============================================================================
1111
Class : abstract, template (T context, D datas)
@@ -14,18 +14,17 @@
1414
Friends ->
1515
<- Problem
1616
==============================================================================*/
17-
template <class T, class D>
18-
class CompareStacks: public Problem<T,D>{
17+
template <class T, class D> class CompareStacks : public Problem<T, D> {
1918
public:
2019
// Members functions
21-
CompareStacks<T,D>(std::string fileName);
20+
CompareStacks<T, D>(std::string fileName);
2221

2322
// Compare the stacks
2423
void runCompare(int buffer = 0);
2524

2625
// IO
27-
void printCompare(){
28-
std::string str = Problem<T,D>::toString();
26+
void printCompare() {
27+
std::string str = Problem<T, D>::toString();
2928
str += "\n" + (*mNormalStack).toString();
3029
std::cout << str << std::endl;
3130
}
@@ -35,85 +34,83 @@ class CompareStacks: public Problem<T,D>{
3534
virtual D readInput(std::vector<std::string> line) = 0;
3635
virtual std::shared_ptr<T> initStack() = 0;
3736
virtual bool popCondition(D data) = 0;
38-
virtual void popAction(Data<T,D> elt) {};
37+
virtual void popAction(Data<T, D> elt){};
3938
virtual bool pushCondition(D data) = 0;
40-
virtual void pushAction(Data<T,D> elt) {};
39+
virtual void pushAction(Data<T, D> elt){};
4140

4241
// pop
43-
Data<T,D> popCompare();
42+
Data<T, D> popCompare();
4443

4544
// Stack: Normal Stack for comparison
46-
std::shared_ptr<NormalStack<T,D>> mNormalStack;
45+
std::shared_ptr<NormalStack<T, D>> mNormalStack;
4746
};
4847

4948
/*==============================================================================
5049
Constructors :
5150
==============================================================================*/
5251

5352
template <class T, class D>
54-
CompareStacks<T,D>::CompareStacks(std::string fileName)
55-
: Problem<T,D>(fileName)
56-
, mNormalStack(new NormalStack<T,D> ()){
57-
}
53+
CompareStacks<T, D>::CompareStacks(std::string fileName)
54+
: Problem<T, D>(fileName), mNormalStack(new NormalStack<T, D>()) {}
5855

5956
/*==============================================================================
6057
Stack Functions: run, push, pop, top
6158
==============================================================================*/
62-
template <class T, class D>
63-
Data<T,D> CompareStacks<T,D>::popCompare(){
64-
return Problem<T,D>::mStack->pop(*this);
59+
template <class T, class D> Data<T, D> CompareStacks<T, D>::popCompare() {
60+
return Problem<T, D>::mStack->pop(*this);
6561
}
6662

67-
template <class T, class D>
68-
void CompareStacks<T,D>::runCompare(int buffer){
63+
template <class T, class D> void CompareStacks<T, D>::runCompare(int buffer) {
6964
try {
70-
Problem<T,D>::initStackIntern();
71-
while ((Problem<T,D>::mInput.good())) {
72-
std::streampos position = Problem<T,D>::mInput.tellg();
73-
(*Problem<T,D>::mStack).setPosition(position);
65+
Problem<T, D>::initStackIntern();
66+
while ((Problem<T, D>::mInput.good())) {
67+
std::streampos position = Problem<T, D>::mInput.tellg();
68+
(*Problem<T, D>::mStack).setPosition(position);
7469
for (int i = 1; i <= buffer; i++) {
75-
bool bIndex = Problem<T,D>::top(i).mIndex == mNormalStack->top(i).mIndex;
76-
bool bData = Problem<T,D>::top(i).mData == mNormalStack->top(i).mData;
70+
bool bIndex =
71+
Problem<T, D>::top(i).mIndex == mNormalStack->top(i).mIndex;
72+
bool bData = Problem<T, D>::top(i).mData == mNormalStack->top(i).mData;
7773
if (!bIndex || !bData) {
78-
Problem<T,D>::println();
74+
Problem<T, D>::println();
7975
std::cout << mNormalStack->toString() << std::endl;
8076
throw "The top $(i)st elements are different";
8177
}
8278
}
83-
std::vector<std::string> line = Problem<T,D>::readLine();
84-
if ( (line.front()== "-1") || (line.front()=="") ) {
79+
std::vector<std::string> line = Problem<T, D>::readLine();
80+
if ((line.front() == "-1") || (line.front() == "")) {
8581
break;
8682
}
8783
D data = readInput(line);
88-
Problem<T,D>::mIndex++; // Might have to move
89-
if ((*Problem<T,D>::mStack).isempty() != mNormalStack->isempty()) {
90-
(*Problem<T,D>::mStack).isempty();
91-
Problem<T,D>::println();
84+
Problem<T, D>::mIndex++; // Might have to move
85+
if ((*Problem<T, D>::mStack).empty() != mNormalStack->empty()) {
86+
(*Problem<T, D>::mStack).empty();
87+
Problem<T, D>::println();
9288
std::cout << mNormalStack->toString() << std::endl;
93-
(*Problem<T,D>::mStack).isempty();
89+
(*Problem<T, D>::mStack).empty();
9490
throw "One stack is empty and not the other";
9591
}
96-
while ( (!(Problem<T,D>::emptystack())) && (popCondition(data)) ) {
97-
Data<T,D> elt = Problem<T,D>::pop();
98-
Data<T,D> eltNormal = mNormalStack->pop();
92+
while ((!(Problem<T, D>::emptystack())) && (popCondition(data))) {
93+
Data<T, D> elt = Problem<T, D>::pop();
94+
Data<T, D> eltNormal = mNormalStack->pop();
9995
popAction(elt);
10096
bool bIndex = elt.mIndex == eltNormal.mIndex;
10197
bool bData = elt.mData == eltNormal.mData;
10298
if (!bIndex || !bData) {
103-
Problem<T,D>::println();
104-
std::cout << *Problem<T,D>::mContext << std::endl;;
99+
Problem<T, D>::println();
100+
std::cout << *Problem<T, D>::mContext << std::endl;
101+
;
105102
std::cout << mNormalStack->toString() << std::endl;
106103
throw "The two elements popped are different";
107104
}
108105
}
109106
if (pushCondition(data)) {
110-
Data<T,D> elt (Problem<T,D>::mIndex,data);
107+
Data<T, D> elt(Problem<T, D>::mIndex, data);
111108
pushAction(elt);
112-
Problem<T,D>::push(elt);
109+
Problem<T, D>::push(elt);
113110
mNormalStack->push(elt);
114111
}
115112
}
116-
} catch (char const * e) {
113+
} catch (char const *e) {
117114
std::cout << e << std::endl;
118115
}
119116
}

0 commit comments

Comments
 (0)