11#ifndef COMPONENT
22#define COMPONENT
33
4- /* *** Compressed Stack: declarations ****/
4+ /* ==============================================================================
5+ Includes
6+ ==============================================================================*/
57#include " sign.hpp"
68#include " stack.hpp"
79#include < string>
10+ #include < memory>
811
9- /* Component of a Compressed Stack */
12+ /* ==============================================================================
13+ Class : template (T context, D datas)
14+ Extensions :
15+ Aliases :
16+ Friends -> CompressedStack
17+ <- Data, Signature
18+ ==============================================================================*/
19+ template <class T , class D > class CompressedStack ; // Required for the friendship
1020template <class T , class D >
11- class Component
12- {
13- public:
21+ class Component {
22+ friend class CompressedStack <T,D>;
23+
24+ private:
1425 Component<T,D>(int space, int depth);
1526
1627 // Setters
17- void setSignature (Signature<T> sign);
18- void setLastSign (int index);
28+ void clearExplicit (int space);
29+
30+ // Push and pop
31+ void pushExplicit (std::shared_ptr<Data<T,D>> elt);
32+ void push (Signature<T,D> sign, int lvl);
33+ Data<T,D> top ();
34+ Signature<T,D> top (int lvl);
35+ int topIndex (int lvl = 0 );
1936
2037 // IO
2138 std::string toString ();
22- void print ();
23- void println ();
2439
25- private:
26- Levels<T> mPartial ;
27- Explicit<D> mExplicit ;
28- Signature<T>* mSign ;
40+ // State
41+ bool isempty ();
42+ bool isempty (int lvl);
43+ bool isExplicitEmpty ();
44+
45+ Levels<T,D> mPartial ;
46+ ExplicitPointer<T,D> mExplicit ;
47+ Signature<T,D> mSign ;
2948};
3049
31- /* * Constructors **/
50+ /* ==============================================================================
51+ Constructors
52+ ==============================================================================*/
3253template <class T , class D >
3354Component<T,D>::Component(int space, int depth)
34- {
35- mSign = nullptr ;
55+ :mSign (0 , std::streampos (0 ), std::shared_ptr<T>(nullptr )){
3656
37- Levels<T> partial = initLevels<T>(space, depth);
57+ Levels<T,D > partial = initLevels<T,D >(space, depth);
3858 mPartial = partial;
3959
40- Explicit<D> xplicit;
41- xplicit = initExplicit<D>();
60+ ExplicitPointer<T,D> xplicit = initExplicitPointer<T,D>();
4261 xplicit.reserve (space);
4362 mExplicit = xplicit;
4463}
4564
46- /* * IO **/
47- template <class T >
48- std::string levelsToStringInComponent (Levels<T> levels)
49- {
65+ /* ==============================================================================
66+ IO : levelsToStringInComponent, toString
67+ ==============================================================================*/
68+ template <class T , class D >
69+ std::string levelsToString (Levels<T,D> levels){
5070 std::string str;
5171 str = " " ;
5272 int index = 0 ;
53- for (typename Levels<T>::iterator it = levels.begin () ; it != levels.end (); ++it)
73+ for (typename Levels<T,D >::iterator it = levels.begin () ; it != levels.end (); ++it)
5474 {
5575 index++;
5676 str += " \t\t\t Level" + std::to_string (index) + " ->\n " ;
@@ -60,15 +80,75 @@ std::string levelsToStringInComponent(Levels<T> levels)
6080}
6181
6282template <class T , class D >
63- std::string Component<T,D>::toString()
64- {
83+ std::string Component<T,D>::toString(){
6584 std::string str;
66- str = levelsToStringInComponent (mPartial );
85+ str = levelsToString (mPartial );
6786 str += " \t\t\t Explicit->\n " ;
68- str += explicitToString (mExplicit );
87+ str += explicitPointerToString (mExplicit );
6988 str += " \t\t\t Signature->\n " ;
7089 // str += (&mSign).toString() + "\n";
7190 return str;
7291}
7392
93+ /* ==============================================================================
94+ Stack Functions: push, pop, top, topIndex, isempty, isExplicitEmpty
95+ ==============================================================================*/
96+ template <class T , class D >
97+ bool Component<T,D>::isempty(){
98+ bool b = bool (mSign .mContext );
99+ return !b;
100+ }
101+
102+ template <class T , class D >
103+ bool Component<T,D>::isempty(int lvl){
104+ bool b;
105+ if (lvl > int (mPartial .size ())) {
106+ b = isExplicitEmpty ();
107+ } else {
108+ b = (mPartial [lvl]).empty ();
109+ }
110+ return b;
111+ }
112+
113+ template <class T , class D >
114+ bool Component<T,D>::isExplicitEmpty(){
115+ return (mExplicit .empty ());
116+ }
117+
118+ template <class T , class D >
119+ void Component<T,D>::pushExplicit(std::shared_ptr<Data<T,D>> elt){
120+ mExplicit .push_back (elt);
121+ }
122+ template <class T , class D >
123+ void Component<T,D>::push(Signature<T,D> sign, int lvl){
124+ mPartial [lvl].push_back (sign);
125+ }
126+
127+ template <class T , class D >
128+ Data<T,D> Component<T,D>::top(){
129+ return *(mExplicit .back ());
130+ }
131+ template <class T , class D >
132+ Signature<T,D> Component<T,D>::top(int lvl){
133+ return mPartial [lvl].back ();
134+ }
135+
136+ template <class T , class D >
137+ int Component<T,D>::topIndex(int lvl){
138+ if (lvl == 0 ) {
139+ return top ().mIndex ;
140+ } else {
141+ return top (lvl).mLast ;
142+ }
143+ }
144+
145+ /* ==============================================================================
146+ Setters
147+ ==============================================================================*/
148+ template <class T , class D >
149+ void Component<T,D>::clearExplicit(int space){
150+ mExplicit .clear ();
151+ mExplicit .reserve (space);
152+ }
153+
74154#endif /* COMPONENT */
0 commit comments