Skip to content

Commit 9df37ba

Browse files
committed
Completed the structure for Compressed Stack (with minimal tests)
1 parent cd5136f commit 9df37ba

File tree

7 files changed

+176
-97
lines changed

7 files changed

+176
-97
lines changed

build/smartstack

-72.3 KB
Binary file not shown.

include/component.hpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#ifndef COMPONENT
2+
#define COMPONENT
3+
4+
/**** Compressed Stack: declarations ****/
5+
#include "sign.hpp"
6+
#include "stack.hpp"
7+
#include <string>
8+
9+
/* Component of a Compressed Stack */
10+
template <class T, class D>
11+
class Component
12+
{
13+
public:
14+
Component<T,D>();
15+
Component<T,D>(int space, int depth);
16+
17+
// Setters
18+
void setSignature(Signature<T> sign);
19+
void setLastSign(int index);
20+
21+
// IO
22+
std::string toString();
23+
void print();
24+
void println();
25+
26+
private:
27+
Levels<T> mPartial;
28+
Explicit<D> mExplicit;
29+
Signature<T>* mSign;
30+
};
31+
32+
/** Constructors **/
33+
template <class T, class D>
34+
Component<T,D>::Component()
35+
{
36+
std::cout << "Using default constructor for Component" << std::endl;
37+
}
38+
39+
template <class T, class D>
40+
Component<T,D>::Component(int space, int depth)
41+
{
42+
mSign = nullptr;
43+
44+
Levels<T> partial = initLevels<T>(space, depth);
45+
mPartial = partial;
46+
47+
Explicit<D> xplicit;
48+
xplicit = initExplicit<D>();
49+
xplicit.reserve(space);
50+
mExplicit = xplicit;
51+
}
52+
53+
/** IO **/
54+
template <class T>
55+
std::string levelsToStringInComponent(Levels<T> levels)
56+
{
57+
std::string str;
58+
str = "";
59+
int index = 0;
60+
for (typename Levels<T>::iterator it = levels.begin() ; it != levels.end(); ++it)
61+
{
62+
index++;
63+
str += "\t\t\tLevel" + std::to_string(index) + "->\n";
64+
str += "\t\t\t\t" + blockToString(*it) + "\n";
65+
}
66+
return str;
67+
}
68+
69+
template <class T, class D>
70+
std::string Component<T,D>::toString()
71+
{
72+
std::string str;
73+
str = levelsToStringInComponent(mPartial);
74+
str += "\t\t\tExplicit->\n";
75+
str += explicitToString(mExplicit);
76+
str += "\t\t\tSignature->\n";
77+
//str += (&mSign).toString() + "\n";
78+
return str;
79+
}
80+
81+
#endif /* COMPONENT */

include/compressedStack.hpp

Lines changed: 49 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,9 @@
44
/**** Compressed Stack: declarations ****/
55
#include "sign.hpp"
66
#include "stack.hpp"
7+
#include "component.hpp"
78
#include <string>
8-
9-
/* Component of a Compressed Stack */
10-
template <class T, class D>
11-
class Component
12-
{
13-
public:
14-
Component<T,D>(int space, int depth);
15-
16-
// Setters
17-
void setSignature(Signature<T> sign);
18-
void setLastSign(int index);
19-
20-
// IO
21-
std::string toString();
22-
void print();
23-
void println();
24-
25-
private:
26-
Levels<T> mPartial;
27-
Explicit<D> mExplicit;
28-
Signature<T>* mSign;
29-
};
30-
9+
#include <cmath>
3110

3211
/* Compressed Stack itself */
3312
template <class T, class D>
@@ -36,8 +15,15 @@ class CompressedStack:Stack<D>
3615
public:
3716
CompressedStack<T,D>(int size, int space);
3817

18+
// Internals
19+
Data<D> top(int k);
20+
void push(Data<D> data);
21+
Data<D> pop();
22+
3923
// IO
4024
std::string toString();
25+
void print();
26+
void println();
4127

4228
private:
4329
// Structure constraints
@@ -55,56 +41,21 @@ class CompressedStack:Stack<D>
5541
};
5642

5743
/** Constructors **/
58-
template <class T, class D>
59-
Component<T,D>::Component(int space, int depth)
60-
{
61-
mSign = nullptr;
62-
63-
Levels<T> partial = initLevels<T>(space, depth);
64-
mPartial = partial;
65-
66-
Explicit<D> xplicit;
67-
xplicit = initExplicit<D>();
68-
xplicit.resize(space);
69-
mExplicit = xplicit;
70-
}
71-
7244
template <class T, class D>
7345
CompressedStack<T,D>::CompressedStack(int size, int space)
7446
{
75-
mSize = size;
76-
mSpace = space;
77-
mDepth = 0;
78-
}
47+
mSize = size;
48+
mSpace = space;
49+
mDepth = (int) ceil(log(size)/log(space)-.1); // - 1;
7950

80-
/** IO **/
81-
template <class T>
82-
std::string toString(Levels<T> levels)
83-
{
84-
std::string str;
85-
str = "";
86-
int index = 0;
87-
for (typename Levels<T>::iterator it = levels.begin() ; it != levels.end(); ++it)
88-
{
89-
index++;
90-
str += "\t\t\tLevel" + std::to_string(index) + "->\n";
91-
str += "\t\t\t\t" + toString(*it) + "\n";
92-
}
93-
return str;
94-
}
51+
mPosition = 0;
9552

96-
template <class T, class D>
97-
std::string Component<T,D>::toString()
98-
{
99-
std::string str;
100-
str = toString(mPartial);
101-
str += "\t\t\tExplicit->\n";
102-
str += toString(mExplicit);
103-
str += "\t\t\tSignature->\n";
104-
str += (&mSign).toString() + "\n";
105-
return str;
53+
mFirst = Component<T,D>(mSpace, mDepth);
54+
mSecond = Component<T,D>(mSpace, mDepth);
55+
mCompressed = initBlock<T>(mSpace);
10656
}
10757

58+
/** IO **/
10859
template <class T, class D>
10960
std::string CompressedStack<T,D>::toString()
11061
{
@@ -121,4 +72,36 @@ std::string CompressedStack<T,D>::toString()
12172
return str;
12273
}
12374

75+
template <class T, class D>
76+
void CompressedStack<T,D>::print()
77+
{
78+
std::cout << this->toString();
79+
}
80+
81+
template <class T, class D>
82+
void CompressedStack<T,D>::println()
83+
{
84+
this->print();
85+
std::cout << std::endl;
86+
}
87+
88+
/** Pushes, pops and accesses **/
89+
template <class T, class D>
90+
void CompressedStack<T,D>::push(Data<D> data)
91+
{
92+
93+
}
94+
95+
template <class T, class D>
96+
Data<D> CompressedStack<T,D>::pop()
97+
{
98+
99+
}
100+
101+
template <class T, class D>
102+
Data<D> CompressedStack<T,D>::top(int k)
103+
{
104+
105+
}
106+
124107
#endif /* COMPRESSEDSTACK */

include/data.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,21 +73,21 @@ void Data<D>::println()
7373
}
7474

7575
template <class D>
76-
std::string toString(Explicit<D> xplicit)
76+
std::string explicitToString(Explicit<D> xplicit)
7777
{
7878
std::string str;
7979
str = "{";
8080
for (typename Explicit<D>::iterator it = xplicit.begin() ; it != xplicit.end(); ++it)
8181
str += (*it).toString() + ",";
82-
str.back() = '}';
82+
str.back() = ')';
8383
return str;
8484
}
8585

8686
template <class D>
8787
void printExplicit(Explicit<D> xplicit)
8888
{
8989
std::string str;
90-
str = toString(xplicit);
90+
str = explicitToString(xplicit);
9191
std::cout << str;
9292
}
9393
template <class D>

include/sign.hpp

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ class Signature
3636

3737
/* Derived types: Block and Levels */
3838
// A Partially Compressed Block is composed of the signatures of its SubBlocks
39-
template<class T> using Block = std::vector<T,Signature<T>>;
39+
template<class T> using Block = std::vector<Signature<T>>;
4040
template<class T> Block<T> initBlock(int space);
4141

4242
// Each level of compressed Blocks (first and second) are stored in Levels
43-
template<class T> using Levels = std::vector<T,Block<T>>;
43+
template<class T> using Levels = std::vector<Block<T>>;
4444
template<class T> Levels<T> initLevels(int space, int depth);
4545

4646
/** Constructors **/
@@ -54,6 +54,28 @@ Signature<T>::Signature(int index, int position, T context)
5454
mContext = context;
5555
}
5656

57+
template <class T>
58+
Block<T> initBlock(int space)
59+
{
60+
Block<T> block;
61+
block.reserve(space);
62+
return block;
63+
}
64+
65+
template <class T>
66+
Levels<T> initLevels(int space, int depth)
67+
{
68+
Levels<T> levels;
69+
for (int lvl = 1; lvl < depth; lvl++)
70+
{
71+
Block<T> block = initBlock<T>(space);
72+
levels.push_back(block);
73+
}
74+
levels.reserve(depth-1);
75+
76+
return levels;
77+
}
78+
5779
/** Setters **/
5880
template <class T>
5981
void Signature<T>::setLast(int index)
@@ -107,7 +129,7 @@ void Signature<T>::println()
107129
}
108130

109131
template<class T>
110-
std::string toString(Block<T> block)
132+
std::string blockToString(Block<T> block)
111133
{
112134
std::string str;
113135
str = "[";
@@ -119,4 +141,11 @@ std::string toString(Block<T> block)
119141
return str;
120142
}
121143

144+
template<class T>
145+
std::string levelsToString(Levels<T> levels)
146+
{
147+
std::string str;
148+
return str;
149+
}
150+
122151
#endif /* SIGN */

src/main.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "../include/sign.hpp"
33
#include "../include/data.hpp"
44
#include "../include/normalStack.hpp"
5+
#include "../include/compressedStack.hpp"
56

67
// Test Signature
78
void testSign()
@@ -45,10 +46,20 @@ void testNormalStack()
4546
stack.println();
4647
}
4748

49+
// Test normal stack
50+
void testCompressedStack()
51+
{
52+
Block<int> block = initBlock<int>(5);
53+
Levels<int> lvls = initLevels<int>(3, 3);
54+
Component<int, int> comp(3,5);
55+
CompressedStack<int, int> stack = CompressedStack<int, int>(81,3);
56+
}
57+
4858
// Main
4959
int main(int argc, char const *argv[]) {
5060
testSign();
5161
testData();
5262
testNormalStack();
63+
testCompressedStack();
5364
return 0;
5465
}

src/sign.cpp

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)