Skip to content

Commit 4e4ade6

Browse files
committed
Corrected bug on the headIndex in pushCompressed (compressedStack.hpp)
1 parent d787d9e commit 4e4ade6

File tree

2 files changed

+29
-32
lines changed

2 files changed

+29
-32
lines changed

include/compressedStack.hpp

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -455,14 +455,14 @@ template <class T, class D> std::string CompressedStack<T, D>::toString() {
455455
// Function push that push the data in explicit and index in partial/compressed
456456
template <class T, class D>
457457
void CompressedStack<T, D>::push(const Data<T, D> &elt) {
458-
int headIndex = getLast(mDepth);
459458
// update the buffer (if buffer size is bigger than 0)
460459
SPData<T, D> ptr_elt = std::make_shared<Data<T, D>>(elt);
461460
mBuffer.push(ptr_elt);
462461
// update the explicit Blocks, with possibly shifting first to second
463462
pushExplicit(ptr_elt);
464463
// update the compressed Blocks at each levels (including fully compressed)
465464
for (int lvl = mDepth - 1; lvl > 0; lvl--) {
465+
int headIndex = getLast(lvl);
466466
pushCompressed(ptr_elt, lvl, headIndex);
467467
}
468468
}
@@ -525,8 +525,7 @@ void CompressedStack<T, D>::pushCompressed(SPData<T, D> elt, int lvl,
525525
int delta = index - startBlock;
526526
if (delta < distBlock) {
527527
// Distance with the current subblock
528-
int headSubBlock = getLast(lvl);
529-
int startSubBlock = headSubBlock - (headSubBlock - 1) % distSubBlock;
528+
int startSubBlock = headIndex - (headIndex - 1) % distSubBlock;
530529
int eta = index - startSubBlock;
531530
// compress new element in the top of the current Block
532531
if (eta < distSubBlock) {
@@ -612,11 +611,12 @@ void CompressedStack<T, D>::compress(Block<T, D> block) {
612611
==============================================================================*/
613612
template <class T, class D>
614613
void CompressedStack<T, D>::reconstruct(Problem<T, D> &problem) {
615-
Signature<T, D> *sign;
614+
Signature<T, D> *sign = new Signature<T,D>();
616615
int lvl;
617616
for (lvl = mDepth; lvl >= 0; lvl--) {
618617
if (lvl == 0) {
619618
sign = getSign(0);
619+
break;
620620
} else {
621621
if (!empty(lvl, 1)) {
622622
sign = getSign(1, lvl);
@@ -627,7 +627,6 @@ void CompressedStack<T, D>::reconstruct(Problem<T, D> &problem) {
627627
}
628628
}
629629
}
630-
std::cout << "Reconstruct on " << sign->toString() << std::endl;
631630

632631
std::streampos posReminder = problem.mInput.tellg();
633632
int indexReminder = problem.mIndex;
@@ -638,7 +637,10 @@ void CompressedStack<T, D>::reconstruct(Problem<T, D> &problem) {
638637
problem.mInput.clear();
639638
problem.mInput.seekg(sign->mPosition);
640639

641-
int auxSize = std::pow(mSpace, mDepth - lvl + 1);
640+
int auxSize = mSize;
641+
if (lvl > 0) {
642+
auxSize = std::pow(mSpace, mDepth - lvl + 1);
643+
}
642644
std::shared_ptr<Stack<T, D>> auxStack =
643645
std::make_shared<CompressedStack<T, D>>(
644646
auxSize, mSpace, mBuffer.mSize, problem.mContext, sign->mPosition);
@@ -649,17 +651,16 @@ void CompressedStack<T, D>::reconstruct(Problem<T, D> &problem) {
649651
swap(problem.mStack, auxStack);
650652

651653
if (lvl == 0) {
652-
(*auxStack).copyContent(*this);
654+
auxStack->copyContent(*this);
653655
} else {
654656
// Copy the First component of the reconstructed stack into the main stack
655-
int auxDepth = int(ceil(log(auxSize) / log(mSpace) - .001)) - 1;
657+
int auxDepth = int(ceil(log(auxSize) / log(mSpace) - .001) - 1);
656658
int delta = mDepth - auxDepth;
657-
for (int i = 0; i < auxDepth - 1; i++) {
658-
mSecond.mPartial[delta + i] = (*auxStack).getFirstPartial(i);
659+
for (int i = 1; i < auxDepth; i++) {
660+
mSecond.mPartial[delta + i - 1] = auxStack->getFirstPartial(i);
659661
}
660-
661-
mSecond.mExplicit = (*auxStack).getFirstExplicit();
662-
mSecond.mSign = (*auxStack).getFirstSign();
662+
mSecond.mExplicit = auxStack->getFirstExplicit();
663+
mSecond.mSign = auxStack->getFirstSign();
663664
}
664665

665666
problem.mIndex = indexReminder;
@@ -721,7 +722,7 @@ void CompressedStack<T, D>::popComponent(int index, int component) {
721722
setSign(component);
722723
} else {
723724
int newLast = getLast(mDepth);
724-
setLast(newLast, component);
725+
setLast(component, newLast);
725726
}
726727
if (mDepth > 1) {
727728
clear(index, mDepth - 1, component);
@@ -732,17 +733,18 @@ template <class T, class D>
732733
void CompressedStack<T, D>::clear(int index, int lvl, int component) {
733734
if (component == 2 && !empty(lvl, 1)) {
734735
clear(index, lvl, 1);
735-
}
736-
if (single(lvl, component)) {
737-
pop_back(lvl, component);
738-
if (lvl > 1) {
739-
clear(index, lvl - 1, component);
740-
} else if (empty(1, component)) {
741-
setSign(component);
742-
}
743736
} else {
744-
int newLast = getLast(lvl + 1);
745-
propagate(newLast, lvl, component);
737+
if (single(lvl, component)) {
738+
pop_back(lvl, component);
739+
if (lvl > 1) {
740+
clear(index, lvl - 1, component);
741+
} else if (empty(1, component)) {
742+
setSign(component);
743+
}
744+
} else {
745+
int newLast = getLast(lvl + 1);
746+
propagate(newLast, lvl, component);
747+
}
746748
}
747749
}
748750

src/main.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ class Instance : public Problem<int, int> {
3636
}
3737
void popAction(Data<int, int> elt) {
3838
std::cout << elt.toString() << " <<<< Pop!" << std::endl;
39-
println();
4039
setContext(getContext() - 1);
4140
}
4241
bool pushCondition(int data) {
@@ -47,7 +46,6 @@ class Instance : public Problem<int, int> {
4746
}
4847
void pushAction(Data<int, int> elt) {
4948
std::cout << "Push >>>> " << elt.toString() << std::endl;
50-
println();
5149
}
5250
};
5351

@@ -101,19 +99,16 @@ void testProblem(std::string filePath) {
10199
// testNS.println();
102100

103101
// Test on CompressedStack
104-
std::cout << "Debug testProblem 1" << std::endl;
105102
Instance testCS(filePath);
106-
std::cout << "Debug testProblem 2" << std::endl;
107103
testCS.run();
108-
std::cout << "Debug testProblem 3" << std::endl;
109104
testCS.println();
110105
}
111106

112107
void testCompare(std::string filePath) {
113108

114109
Comparison comp(filePath);
115110
comp.runCompare();
116-
comp.printCompare();
111+
comp.println();
117112
}
118113
/*==============================================================================
119114
Main function
@@ -128,8 +123,8 @@ int main(int argc, char *argv[]) {
128123

129124
switch (atoi(argv[2])) {
130125
case 0:
131-
testProblem(filename);
132-
// testCompare(filename);
126+
// testProblem(filename);
127+
testCompare(filename);
133128
break;
134129
case 1:
135130
ct.createTestInputFiles(0, filename, atoi(argv[3]), atoi(argv[4]),

0 commit comments

Comments
 (0)