@@ -26,14 +26,17 @@ class CompressedStack: public Stack<T,D>{
2626 friend class Problem <T,D>;
2727
2828private:
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==============================================================================*/
6974template <class T , class D >
7075CompressedStack<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+
105125template <class T , class D >
106126bool 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
111138template <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}
0 commit comments