Skip to content

Commit f67c66e

Browse files
committed
Implementation of push for Compressed Stack
1 parent 72c8c75 commit f67c66e

File tree

8 files changed

+98
-66
lines changed

8 files changed

+98
-66
lines changed

include/component.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ class Component{
4444

4545
Levels<T,D> mPartial;
4646
ExplicitPointer<T,D> mExplicit;
47-
std::shared_ptr<Signature<T,D>> mSign;
47+
Signature<T,D> mSign;
4848
};
4949

5050
/*==============================================================================
5151
Constructors
5252
==============================================================================*/
5353
template <class T, class D>
54-
Component<T,D>::Component(int space, int depth){
55-
mSign = std::shared_ptr<Signature<T,D>> (nullptr);
54+
Component<T,D>::Component(int space, int depth)
55+
:mSign(0, std::streampos (0), std::shared_ptr<T>(nullptr)){
5656

5757
Levels<T,D> partial = initLevels<T,D>(space, depth);
5858
mPartial = partial;
@@ -95,14 +95,14 @@ std::string Component<T,D>::toString(){
9595
==============================================================================*/
9696
template <class T, class D>
9797
bool Component<T,D>::isempty(){
98-
bool b = bool (mSign);
98+
bool b = bool (mSign.mContext);
9999
return !b;
100100
}
101101

102102
template <class T, class D>
103103
bool Component<T,D>::isempty(int lvl){
104104
bool b;
105-
if (lvl > mPartial.size()) {
105+
if (lvl > int(mPartial.size())) {
106106
b = isExplicitEmpty();
107107
} else {
108108
b = (mPartial[lvl]).empty();

include/compressedStack.hpp

Lines changed: 59 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,17 @@ class CompressedStack: public Stack<T,D>{
2626
friend class Problem<T,D>;
2727

2828
private:
29-
CompressedStack<T,D>(int size, int space, int buffer, std::shared_ptr<T> context, std::streampos position);
29+
CompressedStack<T,D>(int size, int space, int buffer, std::shared_ptr<T> context, std::streampos position = std::streampos(0));
3030

3131
// Internals
3232
Data<T,D> top(int k);
33-
void push(Data<T,D> data);
33+
void push(const Data<T,D> &data);
3434
Data<T,D> pop();
3535
bool isempty();
3636

37+
// Setters
38+
void setContext(std::shared_ptr<T> context);
39+
3740
// IO
3841
std::string toString();
3942

@@ -42,6 +45,8 @@ class CompressedStack: public Stack<T,D>{
4245
void pushCompressed(std::shared_ptr<Data<T,D>> elt, int lvl);
4346
Data<T,D> top();
4447
int topIndex();
48+
void compress();
49+
void resetBlock(Signature<T,D> sign, int lvl);
4550

4651
// Structure constraints
4752
int mSize; // (Expected) size of the input in #elements
@@ -68,15 +73,22 @@ class CompressedStack: public Stack<T,D>{
6873
==============================================================================*/
6974
template <class T, class D>
7075
CompressedStack<T,D>::CompressedStack(int size, int space, int buffer, std::shared_ptr<T> context, std::streampos position)
71-
:mFirst(size,space), mSecond(size,space), mBuffer(buffer){
76+
: mFirst(space, int( ceil(log(size)/log(space)-.1)))
77+
, mSecond(space, int( ceil(log(size)/log(space)-.1)))
78+
, mBuffer(buffer){
7279
mSize = size;
7380
mSpace = space;
74-
mDepth = (int) ceil(log(size)/log(space)-.1); // - 1;
75-
81+
mDepth = int( ceil(log(size)/log(space)-.1));
7682
mPosition = position;
77-
7883
mCompressed = initBlock<T,D>(mSpace);
84+
mContext = context;
85+
}
7986

87+
/*==============================================================================
88+
Setters : setContext
89+
==============================================================================*/
90+
template <class T, class D>
91+
void CompressedStack<T,D>::setContext(std::shared_ptr<T> context){
8092
mContext = context;
8193
}
8294

@@ -100,23 +112,39 @@ std::string CompressedStack<T,D>::toString(){
100112
}
101113

102114
/*==============================================================================
103-
Stack Functions: push, pop, isempty
115+
Stack Functions: push, pop, isempty, compress
104116
==============================================================================*/
117+
// Function that compress the top block of mSecond to a sign in mCompressed
118+
template <class T, class D>
119+
void CompressedStack<T,D>::compress(){
120+
if (!mSecond.isempty()) {
121+
mCompressed.back().mLast = mSecond.topIndex();
122+
}
123+
}
124+
105125
template <class T, class D>
106126
bool CompressedStack<T,D>::isempty(){
107127
return (mFirst.isempty() && mSecond.isempty());
108128
}
109129

130+
template <class T, class D>
131+
void CompressedStack<T,D>::resetBlock(Signature<T,D> sign, int lvl){
132+
mFirst.mPartial[lvl].clear();
133+
mFirst.mPartial[lvl].reserve(std::pow(mSpace, mDepth + 1 - lvl));
134+
mFirst.mPartial[lvl].push_back(sign);
135+
}
136+
110137
// Function push that push the data in explicit and index in partial/compressed
111138
template <class T, class D>
112-
void CompressedStack<T,D>::push(Data<T,D> elt){
139+
void CompressedStack<T,D>::push(const Data<T,D> &elt){
113140
// update the buffer (if buffer size is bigger than 0)
114-
std::shared_ptr<Data<T,D>> ptr_elt (new Data<T,D>(elt));
141+
std::shared_ptr<Data<T,D>> ptr_elt = std::make_shared<Data<T,D>>(elt);
115142
mBuffer.push(ptr_elt);
116143
// update the explicit Blocks, with possibly shifting first to second
117144
pushExplicit(ptr_elt);
118145
// update the compressed Blocks at each levels (including fully compressed)
119-
for (int lvl = 1; lvl < mDepth - 1; lvl++) {
146+
for (int lvl = 0; lvl < mDepth - 1; lvl++) {
147+
std::cout << "Pushing on level " << lvl << std::endl;
120148
pushCompressed(ptr_elt, lvl);
121149
}
122150
}
@@ -131,28 +159,26 @@ void CompressedStack<T,D>::pushExplicit(std::shared_ptr<Data<T,D>> elt){
131159
// If the explicit datas of component 1 are empty we push
132160
if (mFirst.isExplicitEmpty()) {
133161
mFirst.pushExplicit(eltPtr);
134-
std::shared_ptr<Signature<T,D>> signPtr(&sign);
135-
mFirst.mSign = signPtr;
162+
mFirst.mSign = sign;
136163
}
137164
// We check if thoses explicit datas are full
138165
else {
139166
int headIndex = mFirst.topIndex();
140167
int startBlock = headIndex - (headIndex - 1) % mSpace;
141168
if (index - startBlock < mSpace) {
142169
mFirst.pushExplicit(eltPtr);
143-
(*mFirst.mSign).mLast = index;
170+
mFirst.mSign.mLast = index;
144171
} else {
145172
if ((mDepth == 1) && (!(mSecond.isExplicitEmpty()))) {
146173
if (mCompressed.empty()) {
147-
mCompressed.push_back(*mSecond.mSign);
174+
mCompressed.push_back(mSecond.mSign);
148175
} else {
149176
// Compress mSecond into mCompressed
150-
(mCompressed.back()).mLast = (*mSecond.mSign).mLast;
177+
(mCompressed.back()).mLast = mSecond.mSign.mLast;
151178
}
152179
}
153-
std::shared_ptr<Signature<T,D>> signPtr = mFirst.mSign;
154-
mSecond.mSign = signPtr;
155-
mFirst.mSign = signPtr;
180+
mSecond.mSign = sign;
181+
mFirst.mSign = sign;
156182
mSecond.mExplicit = mFirst.mExplicit;
157183
mFirst.clearExplicit(mSpace);
158184
mFirst.pushExplicit(eltPtr);
@@ -166,9 +192,9 @@ void CompressedStack<T,D>::pushCompressed(std::shared_ptr<Data<T,D>> elt, int lv
166192
int distSubBlock = std::pow(mSpace,(mDepth - lvl));
167193
int distBlock = distSubBlock * mSpace;
168194
int index = elt->mIndex;
195+
Signature<T,D> sign (index, mPosition, mContext);
169196

170197
if (mFirst.isempty(lvl)) {
171-
Signature<T,D> sign (index, mPosition, mContext);
172198
mFirst.push(sign, lvl);
173199
} else {
174200
int headIndex = mFirst.topIndex(lvl);
@@ -181,12 +207,23 @@ void CompressedStack<T,D>::pushCompressed(std::shared_ptr<Data<T,D>> elt, int lv
181207
int eta = index - startSubBlock + 1;
182208
// compress new element in the top of the current Block
183209
if (eta <= distSubBlock) {
184-
int lengthSubBlock = 0;
210+
mFirst.mPartial[lvl].back().mLast = index;
185211
} else {
186-
/* code */
212+
mFirst.push(sign, lvl);
187213
}
188214
} else {
189-
/* code */
215+
if (lvl == 0) {
216+
int distComponent = int(ceil(mSize/mSpace));
217+
int startComponent = headIndex - (headIndex - 1) % distComponent;
218+
int gamma = index - startComponent + 1;
219+
if (gamma <= distComponent) {
220+
compress();
221+
} else {
222+
mCompressed.push_back(Signature<T,D>(mSecond.mPartial[1]));
223+
}
224+
}
225+
mSecond.mPartial[lvl] = mFirst.mPartial[lvl];
226+
resetBlock(sign, lvl);
190227
}
191228
}
192229
}

include/data.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@
1313
Class : template (D datas)
1414
Extensions :
1515
Aliases : Explicit, ExplicitPointer
16-
Friends -> Component
16+
Friends -> Component, CompressedStack, Problem
1717
<-
1818
==============================================================================*/
1919
template <class T, class D> class Component; // Required for the friendship
2020
template <class T, class D> class CompressedStack; // Required for the friendship
21+
template <class T, class D> class Problem; // Required for the friendship
2122
template <class T, class D>
2223
class Data{
2324
friend class Component<T,D>;
2425
friend class CompressedStack<T,D>;
26+
friend class Problem<T,D>;
2527

2628
public:
2729
// IO

include/normalStack.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
Includes
66
==============================================================================*/
77
#include "stack.hpp"
8+
#include <memory>
89

910
/*==============================================================================
1011
Class : template (D datas)
@@ -30,9 +31,12 @@ class NormalStack: public Stack<T,D>{
3031

3132
// Stack common methods
3233
Data<T,D> pop();
33-
void push(Data<T,D> data);
34+
void push(const Data<T,D> &data);
3435
Data<T,D> top(int k);
3536
bool isempty();
37+
38+
// Setters
39+
void setContext(std::shared_ptr<T> context){}
3640
};
3741

3842
/*==============================================================================
@@ -61,7 +65,7 @@ Data<T,D> NormalStack<T,D>::pop(){
6165
}
6266

6367
template <class T, class D>
64-
void NormalStack<T,D>::push(Data<T,D> elt){
68+
void NormalStack<T,D>::push(const Data<T,D> &elt){
6569
mDatas.push_back(elt);
6670
}
6771

include/problem.hpp

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
Extensions : Instance
1818
Aliases :
1919
Friends ->
20-
<- NormalStack, CompressedStack
20+
<- NormalStack, CompressedStack, Data
2121
==============================================================================*/
2222
template <class T, class D>
2323
class Problem{
@@ -35,8 +35,7 @@ class Problem{
3535
bool emptystack();
3636

3737
// Setters
38-
void setOutput(std::string fileName);
39-
void setContext(T context);
38+
void setContext(const T &context);
4039

4140
//Getters
4241
T getContext();
@@ -54,7 +53,7 @@ class Problem{
5453
private:
5554
// Input/Ouput
5655
std::ifstream mInput;
57-
std::shared_ptr<std::ofstream> mOutput; // output file is optional
56+
std::ofstream mOutput; // output file is optional
5857

5958
// Stack Functions: defined by user
6059
virtual D readInput(std::vector<std::string> line) = 0;
@@ -75,27 +74,20 @@ class Problem{
7574
Constructors : with NormalStack or CompressedStack
7675
==============================================================================*/
7776
template <class T, class D>
78-
Problem<T,D>::Problem(std::string fileName, int size){
77+
Problem<T,D>::Problem(std::string fileName, int size)
78+
: mIndex(0)
79+
, mContext(nullptr)
80+
, mStack(new NormalStack<T,D> (size)){
7981
mInput.open(fileName, std::ifstream::in);
80-
mOutput = nullptr;
81-
82-
mContext = (nullptr);
83-
mIndex = 0;
84-
85-
mStack = std::shared_ptr<Stack<T,D>> (new NormalStack<T,D> (size));
8682
}
8783

8884
template <class T, class D>
89-
Problem<T,D>::Problem(std::string fileName, int size, int space, int buffer){
85+
Problem<T,D>::Problem(std::string fileName, int size, int space, int buffer)
86+
: mIndex(0)
87+
, mContext(nullptr){
9088
mInput.open(fileName, std::ifstream::in);
9189
std::streampos position = mInput.tellg();
92-
93-
mOutput = nullptr;
94-
95-
mContext = (nullptr);
96-
mIndex = 0;
97-
98-
mStack = std::shared_ptr<Stack<T,D>> (new CompressedStack<T,D> (size, space, buffer, mContext, position));
90+
mStack = std::shared_ptr<Stack<T,D>> (new CompressedStack<T,D> (size, space, buffer, mContext));
9991
}
10092

10193
/*==============================================================================
@@ -185,18 +177,9 @@ bool Problem<T,D>::emptystack(){
185177

186178
/** Setters **/
187179
template <class T, class D>
188-
void Problem<T,D>::setOutput(std::string fileName){
189-
std::ofstream ofstr;
190-
ofstr.open(fileName, std::ofstream::out);
191-
mOutput = &ofstr;
192-
}
193-
194-
template <class T, class D>
195-
void Problem<T,D>::setContext(T context){
196-
// std::cout << "setContext, T = " << context << std::endl;
197-
*mContext = context;
198-
// std::cout << "setContext, *mContext = " << (*mContext) << std::endl;
199-
// std::cout << "setContext, *mContext = " << getContext() << std::endl;
180+
void Problem<T,D>::setContext(const T &context){
181+
mContext = std::make_shared<T>(context);
182+
mStack->setContext(mContext);
200183
}
201184

202185
template <class T, class D>

include/sign.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ template <class T, class D>
6262
Signature<T,D>::Signature(Block<T,D> block){
6363
Signature<T,D> frontSign = block.front();
6464
Signature<T,D> backSign = block.back();
65-
mFirst = frontSign.getFirst();
66-
mLast = backSign.getLast();
67-
mPosition = frontSign.getPosition();
68-
mContext = frontSign.getContext();
65+
mFirst = frontSign.mFirst;
66+
mLast = backSign.mLast;
67+
mPosition = frontSign.mPosition;
68+
mContext = frontSign.mContext;
6969
}
7070

7171
template <class T, class D>

include/stack.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
Includes
66
==============================================================================*/
77
#include "data.hpp"
8+
#include <memory>
89

910
/*==============================================================================
1011
Class : abstract, template (T context, D datas)
@@ -22,10 +23,13 @@ class Stack{
2223

2324
// Common stack functions
2425
virtual Data<T,D> pop() = 0;
25-
virtual void push(Data<T,D> data) = 0;
26+
virtual void push(const Data<T,D> &data) = 0;
2627
virtual Data<T,D> top(int k) = 0;
2728
virtual bool isempty() = 0;
2829

30+
// Setters
31+
virtual void setContext(std::shared_ptr<T> context) = 0;
32+
2933
// IO
3034
virtual std::string toString() = 0;
3135
};

src/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ void testProblem()
6565
// Test on CompressedStack
6666
Instance testCS(filePath, 1000, 3, 0);
6767
testCS.println();
68+
testCS.run();
69+
testCS.println();
6870
}
6971

7072
/*==============================================================================

0 commit comments

Comments
 (0)