-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathGolomb.cpp
More file actions
109 lines (91 loc) · 2.07 KB
/
Golomb.cpp
File metadata and controls
109 lines (91 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <Golomb.h>
#include <vector>
#include <sstream>
Golomb::Golomb(int inicio, unsigned int tamanho, Interceptador *interceptador) :
Calculo(inicio, tamanho, interceptador)
{
this->resultados.reserve(tamanho);
}
void Golomb::calcula()
{
std::stringstream ss;
unsigned int gV;
for (int i = inicio; i < (int)(inicio + tamanho); ++i) {
gV = this->golombValue(i);
for (unsigned int j = 0; j < gV; ++j) {
ss << i;
}
this->resultados.push_back(ss.str());
ss.str("");
}
}
unsigned int Golomb::golombValue(const unsigned int n)
{
if (n < 1) return 0;
std::vector<unsigned int> g;
g.push_back(0);
for (unsigned int i = 1; i <= n; ++i) {
if (i == 1) {
g.push_back(1);
continue;
}
g.push_back(1 + g[(i - 1) + 1 - g[g[i-1]]]);
}
return g[n];
}
void Golomb::limpaCalculo()
{
this->resultados.clear();
}
unsigned int Golomb::numeroResultados()
{
return this->resultados.size();
}
int Golomb::resultado(unsigned int indice)
{
int rtn;
std::stringstream ss;
if (indice >= this->resultados.size()) {
return 0; //?
}
ss << this->resultados[indice];
ss >> rtn;
return this->interceptador->intercepta(rtn);
}
bool Golomb::resultado(unsigned int id, std::string& value)
{
int rtn;
std::stringstream ss;
if (id >= this->resultados.size()) {
return false;
}
value = this->resultados[id];
ss << value;
ss >> rtn;
this->interceptador->intercepta(rtn);
return true;
}
string Golomb::toString(char sep)
{
std::stringstream ss;
unsigned int i = 0;
for(vector<std::string>::iterator it = this->resultados.begin(); it != this->resultados.end(); it++){
ss << *it;
if(i < (this->resultados.size() - 1)){
ss << sep;
}
i++;
}
return ss.str();
}
string Golomb::nome() const
{
return "Golomb";
}
Golomb::~Golomb() {
this->inicio = 0;
if (interceptador != 0){
delete interceptador;
interceptador = 0;
}
}