Skip to content

Commit 72c8c75

Browse files
committed
POO optimal
1 parent 026fe04 commit 72c8c75

File tree

9 files changed

+212
-345
lines changed

9 files changed

+212
-345
lines changed

include/buffer.hpp

Lines changed: 28 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -14,67 +14,57 @@
1414
Class : template (D datas)
1515
Extensions :
1616
Aliases :
17-
Friends ->
17+
Friends -> CompressedStack
1818
<-
1919
==============================================================================*/
20-
template <class D>
20+
template <class T, class D> class CompressedStack; // Required for the friendship
21+
template <class T, class D>
2122
class Buffer{
22-
public:
23+
friend class CompressedStack<T,D>;
24+
25+
private:
2326
// Constructor
24-
Buffer<D>(int size = 0);
27+
Buffer<T,D>(int size = 0);
2528

2629
// Getters
27-
Data<D> top(int k);
28-
int getSize();
29-
int getStart();
30+
Data<T,D> top(int k);
3031

3132
// Setters
3233
void setStart();
33-
void setStart(int start);
34-
void setData(std::shared_ptr<Data<D>> elt, int index);
34+
void setData(std::shared_ptr<Data<T,D>> elt, int index);
3535

3636
// Push and Pop
37-
void push(std::shared_ptr<Data<D>> elt);
37+
void push(std::shared_ptr<Data<T,D>> elt);
3838
void pop();
3939

4040
// IO
4141
std::string toString();
42-
void print();
43-
void println();
4442

45-
private:
43+
// Members
4644
int mSize;
4745
int mStart;
48-
ExplicitPointer<D> mExplicit;
46+
ExplicitPointer<T,D> mExplicit;
4947
};
5048

5149
/*==============================================================================
5250
Constructors
5351
==============================================================================*/
54-
template <class D>
55-
Buffer<D>::Buffer(int size){
52+
template <class T, class D>
53+
Buffer<T,D>::Buffer(int size){
5654
mSize = size;
5755
mStart = 0;
5856

59-
ExplicitPointer<D> xplicit = initExplicitPointer<D>();
57+
ExplicitPointer<T,D> xplicit = initExplicitPointer<T,D>();
6058
xplicit.reserve(size);
6159
mExplicit = xplicit;
6260
}
6361

6462
/*==============================================================================
6563
Getters
6664
==============================================================================*/
67-
template <class D>
68-
int Buffer<D>::getSize(){
69-
return mSize;
70-
}
71-
template <class D>
72-
int Buffer<D>::getStart(){
73-
return mStart;
74-
}
75-
template <class D>
76-
Data<D> Buffer<D>::top(int k){
77-
if (k < getSize()) {
65+
template <class T, class D>
66+
Data<T,D> Buffer<T,D>::top(int k){
67+
if (k < mSize) {
7868
int index = (k + mStart - 1) % mSize; // -1 match the start of vectors at 0
7969
return *(mExplicit[index]);
8070
}
@@ -84,56 +74,41 @@ Data<D> Buffer<D>::top(int k){
8474
/*==============================================================================
8575
Setters
8676
==============================================================================*/
87-
template <class D>
88-
void Buffer<D>::setStart(int start){
89-
mStart = start;
90-
}
91-
template <class D>
92-
void Buffer<D>::setStart(){
77+
template <class T, class D>
78+
void Buffer<T,D>::setStart(){
9379
mStart = (mStart + 1) % mSize;
9480
}
95-
template <class D>
96-
void Buffer<D>::setData(std::shared_ptr<Data<D>> elt, int id){
81+
template <class T, class D>
82+
void Buffer<T,D>::setData(std::shared_ptr<Data<T,D>> elt, int id){
9783
int index = (id + mStart - 1) % mSize; // -1 match the start of vectors at 0
9884
mExplicit[index] = elt;
9985
}
10086

10187
/*==============================================================================
10288
Stack Functions: push, pop
10389
==============================================================================*/
104-
template <class D>
105-
void Buffer<D>::push(std::shared_ptr<Data<D>> elt){
90+
template <class T, class D>
91+
void Buffer<T,D>::push(std::shared_ptr<Data<T,D>> elt){
10692
if (mSize > 0) {
10793
setData(elt, mStart+1);
10894
}
10995
}
11096

111-
template <class D>
112-
void Buffer<D>::pop(){
97+
template <class T, class D>
98+
void Buffer<T,D>::pop(){
11399
setStart();
114100
}
115101

116102
/*==============================================================================
117103
IO : toString
118104
==============================================================================*/
119-
template <class D>
120-
std::string Buffer<D>::toString(){
105+
template <class T, class D>
106+
std::string Buffer<T,D>::toString(){
121107
std::string str;
122108
str = "\t\tBuffer size is " + std::to_string(mSize);
123109
str += " and start at index " + std::to_string(mStart) + "\n";
124110
str += explicitPointerToString(mExplicit);
125111
return str;
126112
}
127-
template <class D>
128-
void Buffer<D>::print(){
129-
std::string str;
130-
str = this->toString();
131-
std::cout << str;
132-
}
133-
template <class D>
134-
void Buffer<D>::println(){
135-
this->print();
136-
std::cout << "\n";
137-
}
138113

139114
#endif /* BUFFER */

include/component.hpp

Lines changed: 34 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -13,74 +13,64 @@
1313
Class : template (T context, D datas)
1414
Extensions :
1515
Aliases :
16-
Friends ->
17-
<-
16+
Friends -> CompressedStack
17+
<- Data, Signature
1818
==============================================================================*/
19+
template <class T, class D> class CompressedStack; // Required for the friendship
1920
template <class T, class D>
2021
class Component{
21-
public:
22-
Component<T,D>(int space, int depth);
22+
friend class CompressedStack<T,D>;
2323

24-
// Getters
25-
Signature<T> getSign();
26-
std::shared_ptr<Signature<T>> getSignPtr();
27-
int getLastSign();
28-
ExplicitPointer<D> getExplicit();
24+
private:
25+
Component<T,D>(int space, int depth);
2926

3027
// Setters
31-
void setSignature(std::shared_ptr<Signature<T>> sign);
32-
void setLastSign(int index);
33-
void setExplicit(ExplicitPointer<D> xplicit);
3428
void clearExplicit(int space);
3529

3630
// Push and pop
37-
void pushExplicit(std::shared_ptr<Data<D>> elt);
38-
void push(Signature<T> sign, int lvl);
39-
Data<D> top();
40-
Signature<T> top(int lvl);
41-
int topIndex(int lvl);
42-
int topIndex();
31+
void pushExplicit(std::shared_ptr<Data<T,D>> elt);
32+
void push(Signature<T,D> sign, int lvl);
33+
Data<T,D> top();
34+
Signature<T,D> top(int lvl);
35+
int topIndex(int lvl = 0);
4336

4437
// IO
4538
std::string toString();
46-
void print();
47-
void println();
4839

4940
// State
5041
bool isempty();
5142
bool isempty(int lvl);
5243
bool isExplicitEmpty();
5344

54-
private:
55-
Levels<T> mPartial;
56-
ExplicitPointer<D> mExplicit;
57-
std::shared_ptr<Signature<T>> mSign;
45+
Levels<T,D> mPartial;
46+
ExplicitPointer<T,D> mExplicit;
47+
std::shared_ptr<Signature<T,D>> mSign;
5848
};
5949

6050
/*==============================================================================
6151
Constructors
6252
==============================================================================*/
6353
template <class T, class D>
6454
Component<T,D>::Component(int space, int depth){
65-
mSign = std::shared_ptr<Signature<T>> (nullptr);
55+
mSign = std::shared_ptr<Signature<T,D>> (nullptr);
6656

67-
Levels<T> partial = initLevels<T>(space, depth);
57+
Levels<T,D> partial = initLevels<T,D>(space, depth);
6858
mPartial = partial;
6959

70-
ExplicitPointer<D> xplicit = initExplicitPointer<D>();
60+
ExplicitPointer<T,D> xplicit = initExplicitPointer<T,D>();
7161
xplicit.reserve(space);
7262
mExplicit = xplicit;
7363
}
7464

7565
/*==============================================================================
76-
IO
66+
IO : levelsToStringInComponent, toString
7767
==============================================================================*/
78-
template <class T>
79-
std::string levelsToStringInComponent(Levels<T> levels){
68+
template <class T, class D>
69+
std::string levelsToString(Levels<T,D> levels){
8070
std::string str;
8171
str = "";
8272
int index = 0;
83-
for (typename Levels<T>::iterator it = levels.begin() ; it != levels.end(); ++it)
73+
for (typename Levels<T,D>::iterator it = levels.begin() ; it != levels.end(); ++it)
8474
{
8575
index++;
8676
str += "\t\t\tLevel" + std::to_string(index) + "->\n";
@@ -92,15 +82,17 @@ std::string levelsToStringInComponent(Levels<T> levels){
9282
template <class T, class D>
9383
std::string Component<T,D>::toString(){
9484
std::string str;
95-
str = levelsToStringInComponent(mPartial);
85+
str = levelsToString(mPartial);
9686
str += "\t\t\tExplicit->\n";
9787
str += explicitPointerToString(mExplicit);
9888
str += "\t\t\tSignature->\n";
9989
//str += (&mSign).toString() + "\n";
10090
return str;
10191
}
10292

103-
/* State of the component */
93+
/*==============================================================================
94+
Stack Functions: push, pop, top, topIndex, isempty, isExplicitEmpty
95+
==============================================================================*/
10496
template <class T, class D>
10597
bool Component<T,D>::isempty(){
10698
bool b = bool (mSign);
@@ -123,85 +115,40 @@ bool Component<T,D>::isExplicitEmpty(){
123115
return (mExplicit.empty());
124116
}
125117

126-
/*==============================================================================
127-
Stack Functions: push, pop, top, topIndex
128-
==============================================================================*/
129118
template <class T, class D>
130-
void Component<T,D>::pushExplicit(std::shared_ptr<Data<D>> elt){
119+
void Component<T,D>::pushExplicit(std::shared_ptr<Data<T,D>> elt){
131120
mExplicit.push_back(elt);
132121
}
133122
template <class T, class D>
134-
void Component<T,D>::push(Signature<T> sign, int lvl){
123+
void Component<T,D>::push(Signature<T,D> sign, int lvl){
135124
mPartial[lvl].push_back(sign);
136125
}
137126

138127
template <class T, class D>
139-
Data<D> Component<T,D>::top(){
128+
Data<T,D> Component<T,D>::top(){
140129
return *(mExplicit.back());
141130
}
142131
template <class T, class D>
143-
Signature<T> Component<T,D>::top(int lvl){
132+
Signature<T,D> Component<T,D>::top(int lvl){
144133
return mPartial[lvl].back();
145134
}
146135

147-
template <class T, class D>
148-
int Component<T,D>::topIndex(){
149-
Data<D> elt = top();
150-
int index = elt.getIndex();
151-
return index;
152-
}
153136
template <class T, class D>
154137
int Component<T,D>::topIndex(int lvl){
155-
Signature<T> sign = top(lvl);
156-
int index = sign.getLast();
157-
return index;
138+
if (lvl == 0) {
139+
return top().mIndex;
140+
} else {
141+
return top(lvl).mLast;
142+
}
158143
}
159144

160145
/*==============================================================================
161146
Setters
162147
==============================================================================*/
163-
template <class T, class D>
164-
void Component<T,D>::setSignature(std::shared_ptr<Signature<T>> sign){
165-
mSign = sign;
166-
}
167-
168-
template <class T, class D>
169-
void Component<T,D>::setLastSign(int index){
170-
mSign->setLast(index);
171-
}
172-
173-
template <class T, class D>
174-
void Component<T,D>::setExplicit(ExplicitPointer<D> xplicit){
175-
mExplicit = xplicit;
176-
}
177-
178148
template <class T, class D>
179149
void Component<T,D>::clearExplicit(int space){
180150
mExplicit.clear();
181151
mExplicit.reserve(space);
182152
}
183153

184-
/*==============================================================================
185-
Getters
186-
==============================================================================*/
187-
template <class T, class D>
188-
Signature<T> Component<T,D>::getSign(){
189-
return *mSign;
190-
}
191-
192-
template <class T, class D>
193-
std::shared_ptr<Signature<T>> Component<T,D>::getSignPtr(){
194-
return mSign;
195-
}
196-
197-
template <class T, class D>
198-
int Component<T,D>::getLastSign(){
199-
return mSign->getLast();
200-
}
201-
202-
template <class T, class D>
203-
ExplicitPointer<D> Component<T,D>::getExplicit(){
204-
return mExplicit;
205-
}
206-
207154
#endif /* COMPONENT */

0 commit comments

Comments
 (0)