Skip to content

Commit d96d0c0

Browse files
committed
Solved end of file problem + reconstruction
1 parent 8f7c7cd commit d96d0c0

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

include/compressedStack.hpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ void CompressedStack<T,D>::pushExplicit(SPData<T,D> elt){
368368
mFirst.mSign.mLast = index;
369369
} else {
370370
if ((mDepth == 1) && (!(mSecond.isExplicitEmpty()))) {
371-
int distCompressed = mSpace*mSpace;
371+
int distCompressed = mSpace;
372372
int startCompressed = headIndex - (headIndex - 1) % distCompressed;
373373
if (index - startCompressed < distCompressed) {
374374
mCompressed.back().mLast = headIndex;
@@ -414,7 +414,7 @@ void CompressedStack<T,D>::pushCompressed(SPData<T,D> elt, int lvl, int headInde
414414
}
415415
} else {
416416
if (lvl == 0 && (!mSecond.mPartial[0].empty())) {
417-
int distCompressed = std::pow(mSpace, mDepth + 1);
417+
int distCompressed = std::pow(mSpace, mDepth);
418418
int startCompressed = headIndex - (headIndex - 1) % distCompressed;
419419
int gamma = index - startCompressed;
420420
if (gamma < distCompressed && !mCompressed.empty()) {
@@ -458,11 +458,16 @@ void CompressedStack<T,D>::reconstruct(Problem<T,D> &problem){
458458
// Reconstruct an auxiliary stack based on the signature found above
459459
template <class T, class D>
460460
void CompressedStack<T,D>::reconstruct(Problem<T,D> &problem, const Signature<T,D> &sign, int lvl){
461+
Signature<T,D> sign2 = sign;
462+
std::cout << "Reconstruct on " << sign2.toString() << std::endl;
463+
461464
std::streampos posReminder = problem.mInput.tellg();
462465
int indexReminder = problem.mIndex;
466+
std::ios_base::iostate eofbitReminder = problem.mInput.rdstate();
463467

464468
problem.mContext = std::make_shared<T>(*sign.mContext);
465469
problem.mIndex = sign.mFirst - 1;
470+
problem.mInput.clear();
466471
problem.mInput.seekg(sign.mPosition);
467472

468473
int auxSize = std::pow(mSpace, mDepth - lvl + 1);
@@ -490,13 +495,14 @@ void CompressedStack<T,D>::reconstruct(Problem<T,D> &problem, const Signature<T,
490495
problem.mIndex = indexReminder;
491496
problem.mInput.seekg(posReminder);
492497
problem.mContext = mContext;
498+
problem.mInput.setstate(eofbitReminder);
493499
}
494500

495501
template <class T, class D>
496502
void CompressedStack<T,D>::copyContent(CompressedStack<T,D> &stack) {
497503
stack.mFirst = mFirst;
498504
stack.mSecond = mSecond;
499-
stack.mCompressed = mCompressed;
505+
stack.mCompressed.pop_back();
500506
stack.mBuffer = mBuffer;
501507
}
502508

@@ -570,6 +576,10 @@ template <class T, class D>
570576
void CompressedStack<T,D>::propagateFirst(int index, int lvl){
571577
for (int i = 0; i <= lvl; i++) {
572578
int temp = std::min(mFirst.mPartial[i].back().mLast, index);
579+
if (temp < mFirst.mPartial[i].back().mFirst) {
580+
std::cout << "Debug Prop1" << std::endl << toString() << std::endl;
581+
exit(0);
582+
}
573583
mFirst.mPartial[i].back().mLast = temp;
574584
}
575585
}
@@ -635,12 +645,15 @@ Data<T,D> CompressedStack<T,D>::pop(Problem<T,D> &problem){
635645
SPData<T,D> elt;
636646
if (mFirst.mExplicit.empty() && mSecond.mExplicit.empty()) {
637647
// Reconstruct the compressed stack with the first available signature
648+
std::cout << "Begin reconstruct with mDepth = " << mDepth << std::endl;
638649
reconstruct(problem);
650+
std::cout << "End reconstruct with mDepth = " << mDepth << std::endl;
639651
}
640652
if (mFirst.mExplicit.empty()) {
641653
if (mSecond.mExplicit.empty()) {
654+
std::cout << "Empty after reconstruct" << std::endl;
642655
problem.println();
643-
// exit(0);
656+
exit(0);
644657
}
645658
elt = mSecond.mExplicit.back();
646659
popSecond((*elt).mIndex);

include/problem.hpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ void Problem<T,D>::run(){
214214
std::streampos position = mInput.tellg();
215215
(*mStack).setPosition(position);
216216
std::vector<std::string> line = readLine();
217-
if ( (line.front()== "-1") || (line.front()=="") ) {
217+
if (line.front()=="") {
218218
break;
219219
}
220220
D data = readInput(line);
@@ -228,20 +228,17 @@ void Problem<T,D>::run(){
228228
pushAction(elt);
229229
push(elt);
230230
}
231-
232231
}
233232
}
234233

235234
template <class T, class D>
236235
void Problem<T,D>::run(int limit){
237236
int testIndex = mIndex;
238-
while (mInput.good() && (mIndex < limit)) {
237+
while (mInput.good() && mIndex < limit) {
239238
std::streampos position = mInput.tellg();
240239
(*mStack).setPosition(position);
241240
std::vector<std::string> line = readLine();
242-
if ( (line.front()== "-1") || (line.front()=="") ) {
243-
break;
244-
}
241+
245242
D data = readInput(line);
246243
mIndex++; // Might have to move
247244

src/main.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class Instance: public Problem<int,int>{
3636
}
3737
void popAction(Data<int,int> elt){
3838
std::cout << elt.toString() << " <<<< Pop!" << std::endl;
39+
println();
3940
setContext(getContext() - 1);
4041
}
4142
bool pushCondition(int data){
@@ -46,6 +47,7 @@ class Instance: public Problem<int,int>{
4647
}
4748
void pushAction(Data<int,int> elt){
4849
std::cout << "Push >>>> " << elt.toString() << std::endl;
50+
println();
4951
}
5052
};
5153

@@ -94,13 +96,16 @@ class Comparison: public CompareStacks<int,int>{
9496
void testProblem(std::string filePath)
9597
{
9698
// Test on normal stack
97-
Instance testNS(filePath);
98-
testNS.run();
99-
testNS.println();
99+
// Instance testNS(filePath);
100+
// testNS.run();
101+
// testNS.println();
100102

101103
// Test on CompressedStack
104+
std::cout << "Debug testProblem 1" << std::endl;
102105
Instance testCS(filePath);
106+
std::cout << "Debug testProblem 2" << std::endl;
103107
testCS.run();
108+
std::cout << "Debug testProblem 3" << std::endl;
104109
testCS.println();
105110
}
106111

@@ -123,7 +128,7 @@ int main(int argc, char *argv[]) {
123128
{
124129
case 0:
125130
testProblem(filename);
126-
testCompare(filename);
131+
// testCompare(filename);
127132
break;
128133
case 1:
129134
ct.createTestInputFiles(0,filename,atoi(argv[3]),atoi(argv[4]), atoi(argv[5]), atoi(argv[6]), atof(argv[7]) );

0 commit comments

Comments
 (0)