Skip to content

Commit 7a345fe

Browse files
committed
Added pushcompressed
1 parent ab69008 commit 7a345fe

File tree

5 files changed

+160
-28
lines changed

5 files changed

+160
-28
lines changed

include/component.hpp

Lines changed: 84 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,25 @@ class Component
1414
public:
1515
Component<T,D>(int space, int depth);
1616

17+
// Getters
18+
Signature<T> getSign();
19+
std::shared_ptr<Signature<T>> getSignPtr();
20+
int getLastSign();
21+
ExplicitPointer<D> getExplicit();
22+
1723
// Setters
1824
void setSignature(std::shared_ptr<Signature<T>> sign);
1925
void setLastSign(int index);
26+
void setExplicit(ExplicitPointer<D> xplicit);
27+
void clearExplicit(int space);
2028

2129
// Push and pop
22-
void push(std::shared_ptr<Data<D>> elt);
30+
void pushExplicit(std::shared_ptr<Data<D>> elt);
31+
void push(Signature<T> sign, int lvl);
2332
Data<D> top();
33+
Signature<T> top(int lvl);
34+
int topIndex(int lvl);
35+
int topIndex();
2436

2537
// IO
2638
std::string toString();
@@ -29,6 +41,7 @@ class Component
2941

3042
// State
3143
bool isempty();
44+
bool isempty(int lvl);
3245
bool isExplicitEmpty();
3346

3447
private:
@@ -71,12 +84,9 @@ template <class T, class D>
7184
std::string Component<T,D>::toString()
7285
{
7386
std::string str;
74-
std::cout << "Debug Component::toString 1" << std::endl;
7587
str = levelsToStringInComponent(mPartial);
76-
std::cout << "Debug Component::toString 2" << std::endl;
7788
str += "\t\t\tExplicit->\n";
7889
str += explicitPointerToString(mExplicit);
79-
std::cout << "Debug Component::toString 3" << std::endl;
8090
str += "\t\t\tSignature->\n";
8191
//str += (&mSign).toString() + "\n";
8292
return str;
@@ -89,26 +99,95 @@ bool Component<T,D>::isempty(){
8999
return !b;
90100
}
91101

102+
template <class T, class D>
103+
bool Component<T,D>::isempty(int lvl){
104+
bool b;
105+
if (lvl > mPartial.size()) {
106+
b = isExplicitEmpty();
107+
} else {
108+
b = (mPartial[lvl]).empty();
109+
}
110+
return b;
111+
}
112+
92113
template <class T, class D>
93114
bool Component<T,D>::isExplicitEmpty(){
94115
return (mExplicit.empty());
95116
}
96117

97118
/** Push and pop **/
98119
template <class T, class D>
99-
void Component<T,D>::push(std::shared_ptr<Data<D>> elt){
120+
void Component<T,D>::pushExplicit(std::shared_ptr<Data<D>> elt){
100121
mExplicit.push_back(elt);
101122
}
123+
template <class T, class D>
124+
void Component<T,D>::push(Signature<T> sign, int lvl){
125+
mPartial[lvl].push_back(sign);
126+
}
102127

103128
template <class T, class D>
104129
Data<D> Component<T,D>::top(){
105130
return *(mExplicit.back());
106131
}
132+
template <class T, class D>
133+
Signature<T> Component<T,D>::top(int lvl){
134+
return mPartial[lvl].back();
135+
}
136+
137+
template <class T, class D>
138+
int Component<T,D>::topIndex(){
139+
Data<D> elt = top();
140+
int index = elt.getIndex();
141+
return index;
142+
}
143+
template <class T, class D>
144+
int Component<T,D>::topIndex(int lvl){
145+
Signature<T> sign = top(lvl);
146+
int index = sign.getLast();
147+
return index;
148+
}
107149

108150
/** Setters **/
109151
template <class T, class D>
110152
void Component<T,D>::setSignature(std::shared_ptr<Signature<T>> sign){
111153
mSign = sign;
112154
}
113155

156+
template <class T, class D>
157+
void Component<T,D>::setLastSign(int index){
158+
mSign->setLast(index);
159+
}
160+
161+
template <class T, class D>
162+
void Component<T,D>::setExplicit(ExplicitPointer<D> xplicit){
163+
mExplicit = xplicit;
164+
}
165+
166+
template <class T, class D>
167+
void Component<T,D>::clearExplicit(int space){
168+
mExplicit.clear();
169+
mExplicit.reserve(space);
170+
}
171+
172+
// Getters
173+
template <class T, class D>
174+
Signature<T> Component<T,D>::getSign(){
175+
return *mSign;
176+
}
177+
178+
template <class T, class D>
179+
std::shared_ptr<Signature<T>> Component<T,D>::getSignPtr(){
180+
return mSign;
181+
}
182+
183+
template <class T, class D>
184+
int Component<T,D>::getLastSign(){
185+
return mSign->getLast();
186+
}
187+
188+
template <class T, class D>
189+
ExplicitPointer<D> Component<T,D>::getExplicit(){
190+
return mExplicit;
191+
}
192+
114193
#endif /* COMPONENT */

include/compressedStack.hpp

Lines changed: 59 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ template <class T, class D>
1616
class CompressedStack: public Stack<D>
1717
{
1818
public:
19-
CompressedStack<T,D>(int size, int space, int buffer, std::shared_ptr<T> context);
19+
CompressedStack<T,D>(int size, int space, int buffer, std::shared_ptr<T> context, std::streampos position);
2020

2121
// Internals
2222
Data<D> top(int k);
@@ -58,7 +58,7 @@ class CompressedStack: public Stack<D>
5858

5959
/** Constructors **/
6060
template <class T, class D>
61-
CompressedStack<T,D>::CompressedStack(int size, int space, int buffer, std::shared_ptr<T> context)
61+
CompressedStack<T,D>::CompressedStack(int size, int space, int buffer, std::shared_ptr<T> context, std::streampos position)
6262
: mFirst(size,space)
6363
, mSecond(size,space)
6464
, mBuffer(buffer)
@@ -67,7 +67,7 @@ CompressedStack<T,D>::CompressedStack(int size, int space, int buffer, std::shar
6767
mSpace = space;
6868
mDepth = (int) ceil(log(size)/log(space)-.1); // - 1;
6969

70-
mPosition = 0;
70+
mPosition = position;
7171

7272
mCompressed = initBlock<T>(mSpace);
7373

@@ -79,22 +79,16 @@ template <class T, class D>
7979
std::string CompressedStack<T,D>::toString()
8080
{
8181
std::string str;
82-
std::cout << "Debug CompressedStack::toString 1" << std::endl;
8382
str = "\tCompressed Stack with " + std::to_string(mSize) + " elements, ";
8483
str += std::to_string(mSpace) + " space order, ";
8584
str += std::to_string(mDepth) + " depth.\n";
8685
str += "\t\tFirst Component\n";
87-
std::cout << "Debug CompressedStack::toString 2" << std::endl;
8886
str += mFirst.toString();
8987
str += "\t\tSecond Component\n";
90-
std::cout << "Debug CompressedStack::toString 3" << std::endl;
9188
str += mSecond.toString();
9289
str += "\t\tFully Compressed\n";
93-
std::cout << "Debug CompressedStack::toString 4" << std::endl;
9490
str += blockToString(mCompressed);
95-
std::cout << "Debug CompressedStack::toString 5" << std::endl;
9691
str += mBuffer.toString();
97-
std::cout << "Debug CompressedStack::toString 6" << std::endl;
9892
return str;
9993
}
10094

@@ -134,23 +128,70 @@ void CompressedStack<T,D>::push(Data<D> elt){
134128
// Function push for the Explicit members of the stack
135129
template <class T, class D>
136130
void CompressedStack<T,D>::pushExplicit(std::shared_ptr<Data<D>> elt){
137-
std::shared_ptr<Data<D>> ptr = elt;
138-
if (mFirst.isempty()) {
139-
mFirst.push(ptr);
140-
std::shared_ptr<Signature<T>> sign (new Signature<T> (ptr->getIndex(), mPosition, mContext));
141-
mFirst.setSignature(sign);
131+
int index = elt->getIndex();
132+
std::shared_ptr<Data<D>> eltPtr = elt;
133+
Signature<T> sign (index, mPosition, mContext);
134+
135+
// If the explicit datas of component 1 are empty we push
136+
if (mFirst.isExplicitEmpty()) {
137+
mFirst.pushExplicit(eltPtr);
138+
std::shared_ptr<Signature<T>> signPtr(&sign);
139+
mFirst.setSignature(signPtr);
142140
}
141+
// We check if thoses explicit datas are full
143142
else {
144-
int headIndex = 0;
143+
int headIndex = mFirst.topIndex();
144+
int startBlock = headIndex - (headIndex - 1) % mSpace;
145+
if (index - startBlock < mSpace) {
146+
mFirst.pushExplicit(eltPtr);
147+
mFirst.setLastSign(index);
148+
} else {
149+
if ((mDepth == 1) && (!(mSecond.isExplicitEmpty()))) {
150+
if (mCompressed.empty()) {
151+
mCompressed.push_back(mSecond.getSign());
152+
} else {
153+
(mCompressed.back()).setLast(mSecond.getLastSign());
154+
}
155+
}
156+
std::shared_ptr<Signature<T>> signPtr = mFirst.getSignPtr();
157+
mSecond.setSignature(signPtr);
158+
mFirst.setSignature(signPtr);
159+
mSecond.setExplicit(mFirst.getExplicit());
160+
mFirst.clearExplicit(mSpace);
161+
mFirst.pushExplicit(eltPtr);
162+
}
145163
}
146-
147-
148164
}
149165

150166
// Function push for the part. and fully compressed members of the stack
151167
template <class T, class D>
152168
void CompressedStack<T,D>::pushCompressed(std::shared_ptr<Data<D>> elt, int lvl){
153-
169+
int distSubBlock = std::pow(mSpace,(mDepth - lvl));
170+
int distBlock = distSubBlock * mSpace;
171+
int index = elt->getIndex();
172+
173+
if (mFirst.isempty(lvl)) {
174+
Signature<T> sign (index, mPosition, mContext);
175+
mFirst.push(sign, lvl);
176+
} else {
177+
int headIndex = mFirst.topIndex(lvl);
178+
int startBlock = headIndex - (headIndex - 1) % distBlock;
179+
// distance of the new index and current block
180+
int delta = index - startBlock + 1;
181+
if (delta <= distBlock) {
182+
// Distance with the current subblock
183+
int startSubBlock = headIndex - (headIndex - 1) % distSubBlock;
184+
int eta = index - startSubBlock + 1;
185+
// compress new element in the top of the current Block
186+
if (eta <= distSubBlock) {
187+
int lengthSubBlock = 0;
188+
} else {
189+
/* code */
190+
}
191+
} else {
192+
/* code */
193+
}
194+
}
154195
}
155196

156197
template <class T, class D>

include/problem.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,24 +80,23 @@ template <class T, class D>
8080
Problem<T,D>::Problem(std::string fileName, int size, int space, int buffer)
8181
{
8282
mInput.open(fileName, std::ifstream::in);
83+
std::streampos position = mInput.tellg();
84+
8385
mOutput = nullptr;
8486

8587
mContext = (nullptr);
8688
mIndex = 0;
8789

88-
mStack = std::shared_ptr<Stack<D>> (new CompressedStack<T,D> (size, space, buffer, mContext));
90+
mStack = std::shared_ptr<Stack<D>> (new CompressedStack<T,D> (size, space, buffer, mContext, position));
8991
}
9092

9193
/** IO **/
9294
template <class T, class D>
9395
std::string Problem<T,D>::toString(){
9496
std::string str;
95-
std::cout << "Debug Problem::toString 1" << std::endl;
9697
str = "Problem with an actual index of " + std::to_string(mIndex);
97-
std::cout << "Debug Problem::toString 2" << std::endl;
9898
str += ", with a stack of type\n";
9999
str += mStack->toString();
100-
std::cout << "Debug Problem::toString 3" << std::endl;
101100
return str;
102101
}
103102

include/sign.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Signature
1313
public:
1414
// Constructors
1515
Signature<T>(int index, std::streampos position, std::shared_ptr<T> context);
16+
Signature<T>(std::vector<Signature<T>> block);
1617

1718
// Setters
1819
void setLast(int index);
@@ -55,6 +56,17 @@ Signature<T>::Signature(int index, std::streampos position, std::shared_ptr<T> c
5556
mContext = context;
5657
}
5758

59+
template <class T>
60+
Signature<T>::Signature(Block<T> block)
61+
{
62+
Signature<T> frontSign = block.front();
63+
Signature<T> backSign = block.back();
64+
mFirst = frontSign.getFirst();
65+
mLast = backSign.getLast();
66+
mPosition = frontSign.getPosition();
67+
mContext = frontSign.getContext();
68+
}
69+
5870
template <class T>
5971
Block<T> initBlock(int space)
6072
{

src/main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ void testCompressedStack()
6060
Levels<int> lvls = initLevels<int>(3, 3);
6161
Component<int, int> comp(3,5);
6262
std::shared_ptr<int> c (new int(10));
63-
CompressedStack<int, int> stack (81,3,0,c);
63+
std::streampos p (0);
64+
CompressedStack<int, int> stack (81,3,0,c,p);
6465
stack.println();
6566
}
6667

0 commit comments

Comments
 (0)