Skip to content

Commit cd5136f

Browse files
committed
printing method
1 parent b6afe0b commit cd5136f

File tree

11 files changed

+608
-0
lines changed

11 files changed

+608
-0
lines changed

CMakeLists.txt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Recent version of CMake required
2+
cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR)
3+
4+
# Project Name
5+
project(smartstack)
6+
7+
include(CheckCXXCompilerFlag)
8+
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
9+
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
10+
if(COMPILER_SUPPORTS_CXX11)
11+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
12+
elseif(COMPILER_SUPPORTS_CXX0X)
13+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
14+
else()
15+
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
16+
endif()
17+
18+
#Bring the headers into the project
19+
include_directories(${PROJECT_SOURCE_DIR}/include)
20+
21+
#Bring the sources
22+
file(GLOB SOURCES "src/*.cpp")
23+
24+
# Test directories
25+
get_property(inc_dirs DIRECTORY PROPERTY INCLUDE_DIRECTORIES)
26+
message("inc_dirs = ${inc_dirs}")
27+
28+
# Executable
29+
add_executable(smartstack ${SOURCES})

build/smartstack

72.3 KB
Binary file not shown.

include/compressedStack.hpp

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#ifndef COMPRESSEDSTACK
2+
#define COMPRESSEDSTACK
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>(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+
31+
32+
/* Compressed Stack itself */
33+
template <class T, class D>
34+
class CompressedStack:Stack<D>
35+
{
36+
public:
37+
CompressedStack<T,D>(int size, int space);
38+
39+
// IO
40+
std::string toString();
41+
42+
private:
43+
// Structure constraints
44+
int mSize; // (Expected) size of the input in #elements
45+
int mSpace; // Maximum space order of the compressed stack
46+
int mDepth; // Depth (#levels of compression) based on size and space
47+
48+
// Position of previous input (before reading)
49+
int mPosition;
50+
51+
// First, Second, and Compressed components
52+
Component<T,D> mFirst;
53+
Component<T,D> mSecond;
54+
Block<T> mCompressed;
55+
};
56+
57+
/** 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+
72+
template <class T, class D>
73+
CompressedStack<T,D>::CompressedStack(int size, int space)
74+
{
75+
mSize = size;
76+
mSpace = space;
77+
mDepth = 0;
78+
}
79+
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+
}
95+
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;
106+
}
107+
108+
template <class T, class D>
109+
std::string CompressedStack<T,D>::toString()
110+
{
111+
std::string str;
112+
str = "\tCompressed Stack with " + std::to_string(mSize) + " elements, ";
113+
str += std::to_string(mSpace) + " space order, ";
114+
str += std::to_string(mDepth) + " depth.\n";
115+
str += "\t\tFirst Component\n";
116+
str += mFirst.toString();
117+
str += "\t\tSecond Component\n";
118+
str += mSecond.toString();
119+
str += "\t\tFully Compressed\n";
120+
str += toString();
121+
return str;
122+
}
123+
124+
#endif /* COMPRESSEDSTACK */

include/data.hpp

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#ifndef DATA
2+
#define DATA
3+
4+
/** Data class : declaration **/
5+
#include <vector>
6+
#include <string>
7+
#include <iostream>
8+
9+
template <class D>
10+
class Data
11+
{
12+
public:
13+
// Constructor
14+
Data<D>(int index, D data);
15+
16+
// Getters
17+
int getIndex();
18+
D getData();
19+
20+
// IO
21+
std::string toString();
22+
void print();
23+
void println();
24+
25+
private:
26+
int mIndex;
27+
D mData;
28+
};
29+
30+
template<class D> using Explicit = std::vector<Data<D>>;
31+
template<class D> Explicit<D> initExplicit();
32+
33+
/** Constructors **/
34+
template <class D>
35+
Data<D>::Data(int index, D data)
36+
{
37+
mIndex = index;
38+
mData = data;
39+
}
40+
41+
/** Getters **/
42+
template <class D>
43+
int Data<D>::getIndex()
44+
{
45+
return mIndex;
46+
}
47+
template <class D>
48+
D Data<D>::getData()
49+
{
50+
return mData;
51+
}
52+
53+
/** IO **/
54+
template <class D>
55+
std::string Data<D>::toString()
56+
{
57+
std::string str;
58+
str = std::to_string(mIndex);
59+
return str;
60+
}
61+
template <class D>
62+
void Data<D>::print()
63+
{
64+
std::string str;
65+
str = this->toString();
66+
std::cout << str;
67+
}
68+
template <class D>
69+
void Data<D>::println()
70+
{
71+
this->print();
72+
std::cout << "\n";
73+
}
74+
75+
template <class D>
76+
std::string toString(Explicit<D> xplicit)
77+
{
78+
std::string str;
79+
str = "{";
80+
for (typename Explicit<D>::iterator it = xplicit.begin() ; it != xplicit.end(); ++it)
81+
str += (*it).toString() + ",";
82+
str.back() = '}';
83+
return str;
84+
}
85+
86+
template <class D>
87+
void printExplicit(Explicit<D> xplicit)
88+
{
89+
std::string str;
90+
str = toString(xplicit);
91+
std::cout << str;
92+
}
93+
template <class D>
94+
void printlnExplicit(Explicit<D> xplicit)
95+
{
96+
printExplicit(xplicit);
97+
std::cout << "\n";
98+
}
99+
100+
// Explicit constructor
101+
template <class D>
102+
Explicit<D> initExplicit()
103+
{
104+
Explicit<D> xplicit;
105+
return xplicit;
106+
}
107+
108+
#endif /* DATA */

include/normalStack.hpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#ifndef NORMALSTACK
2+
#define NORMALSTACK
3+
4+
/**** Normal Stack: declaration ****/
5+
#include "stack.hpp"
6+
7+
template <class D>
8+
class NormalStack:Stack<D>
9+
{
10+
public:
11+
// Constructors
12+
NormalStack<D>(int size);
13+
14+
// Stack common methods
15+
Data<D> pop();
16+
void push(Data<D> data);
17+
Data<D> top(int k);
18+
19+
// IO
20+
void print();
21+
void println();
22+
23+
private:
24+
Explicit<D> mDatas; // vector of Data
25+
};
26+
27+
/** Constructors **/
28+
template <class D>
29+
NormalStack<D>::NormalStack(int size)
30+
{
31+
Explicit<D> datas;
32+
datas = initExplicit<D>();
33+
mDatas = datas;
34+
}
35+
36+
/** Stack internal methods **/
37+
template <class D>
38+
Data<D> NormalStack<D>::pop()
39+
{
40+
Data<D> data = mDatas.back();
41+
mDatas.pop_back();
42+
return data;
43+
}
44+
45+
template <class D>
46+
void NormalStack<D>::push(Data<D> data)
47+
{
48+
mDatas.push_back(data);
49+
}
50+
51+
template <class D>
52+
Data<D> NormalStack<D>::top(int k)
53+
{
54+
int index = mDatas.size() - k;
55+
return mDatas.at(index);
56+
}
57+
58+
/** IO **/
59+
template <class D>
60+
void NormalStack<D>::print()
61+
{
62+
std::cout << "\tNormal Stack (Explicit Datas)\n\t\t";
63+
printExplicit(mDatas);
64+
}
65+
template <class D>
66+
void NormalStack<D>::println()
67+
{
68+
this->print();
69+
std::cout << std::endl;
70+
}
71+
72+
#endif /* NORMALSTACK */

include/problem.hpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#ifndef PROBLEM
2+
#define PROBLEM
3+
4+
/**** Problem : declaration ****/
5+
#include "stack.hpp"
6+
#include <stdbool.h>
7+
#include <fstream>
8+
#include <string>
9+
10+
template <class T, class D>
11+
class Problem
12+
{
13+
public:
14+
Problem<T,D>(std::string fileName);
15+
16+
// Functions of the problem
17+
//void init();
18+
//D read();
19+
20+
private:
21+
// Input/Ouput
22+
std::ifstream mInput;
23+
std::ofstream* mOutput; // output file is optional
24+
25+
// Stack Functions: defined by user
26+
void (*mInitStack)();
27+
D (*mReadInput)();
28+
bool (*mPopCondition)();
29+
void (*mPopAction)();
30+
bool (*mPushCondition)();
31+
void (*mPushAction)();
32+
33+
// Problem internal during run
34+
T* mContext;
35+
int mIndex;
36+
37+
// Stack: Normal or Compressed
38+
Stack<D> mStack;
39+
};
40+
41+
#endif /* PROBLEM */

0 commit comments

Comments
 (0)