-
Notifications
You must be signed in to change notification settings - Fork 0
/
statistics.C
173 lines (149 loc) · 5.54 KB
/
statistics.C
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# include <statistics.H>
Statistics * Statistics::instance = NULL;
Statistics::Statistics()
: num_iterations(0), generated_packages(0), deliveried_packages(0),
package_in_queue(0), max_deflections(0), deflections_counter(NULL)
{
// Empty
}
Statistics::~Statistics()
{
if (instance != NULL)
{
delete instance;
if (deflections_counter != NULL)
delete []deflections_counter;
}
}
Statistics * Statistics::get_instance()
{
if (instance == NULL)
instance = new Statistics();
return instance;
}
void Statistics::inc_iterations()
{
++num_iterations;
events_list_per_iteration.append(Pair_Of_List());
}
void Statistics::inc_generated_packages()
{
++generated_packages;
}
void Statistics::inc_deliveried_packages()
{
++deliveried_packages;
}
void Statistics::inc_packages_in_queue()
{
++package_in_queue;
}
void Statistics::dec_packages_in_queue()
{
--package_in_queue;
}
void Statistics::add_statistic_to_node(std::string & node_name, std::string & statistic)
{
Statistics_Per_Element * s = search_node_statistic(node_name);
if (s == NULL)
s = &(events_list_per_iteration.get_last().statistics_per_node.append(Statistics_Per_Element(node_name)));
s->add_statistic(statistic);
}
void Statistics::add_statistic_to_package(std::string & package_name, std::string & statistic)
{
Statistics_Per_Element * s = search_package_statistic(package_name);
if (s == NULL)
s = &(events_list_per_iteration.get_last().statistics_per_package.append(Statistics_Per_Element(package_name)));
s->add_statistic(statistic);
}
Statistics_Per_Element * Statistics::search_package_statistic(const std::string & name)
{
DynDlist<Statistics_Per_Element> & statistics_per_package = events_list_per_iteration.get_last().statistics_per_package;
for (DynDlist<Statistics_Per_Element>::Iterator it(statistics_per_package); it.has_current(); it.next())
{
Statistics_Per_Element & s = it.get_current();
if (s.get_element_name() == name)
return &s;
}
return NULL;
}
Statistics_Per_Element * Statistics::search_node_statistic(const std::string & name)
{
DynDlist<Statistics_Per_Element> & statistics_per_node = events_list_per_iteration.get_last().statistics_per_node;
for (DynDlist<Statistics_Per_Element>::Iterator it(statistics_per_node); it.has_current(); it.next())
{
Statistics_Per_Element & s = it.get_current();
if (s.get_element_name() == name)
return &s;
}
return NULL;
}
# include <iostream>
void Statistics::print()
{
std::cout << "Resultados de la Simulacion" << std::endl << std::endl;
std::cout << "Numero de iteraciones: " << get_num_iterations() << std::endl;
std::cout << "Numero de paquetes generados: " << get_generated_packages() << std::endl;
std::cout << "Numero de paquetes en cola: " << get_num_packages_in_queue() << std::endl;
std::cout << "Numero de paquetes entregados: " << get_deliveried_packages() << std::endl;
std::cout << "Numero de paquetes en movimiento: " << get_generated_packages() - get_deliveried_packages() - get_num_packages_in_queue() << std::endl << std::endl;
std::cout << "Tiempo promedio de un paquete en cola: " << tiq_avg << " iteraciones" << std::endl;
std::cout << "Tiempo promedio de un paquete en movimiento: " << ttl_avg << " iteraciones" << std::endl << std::endl;
std::cout << "Resultados por paquete" << std::endl << std::endl;
unsigned long i = 0;
for (DynDlist<Pair_Of_List>::Iterator it(events_list_per_iteration); it.has_current(); it.next(), ++i)
{
if (i > 0)
std::cout << "Iteracion numero " << i << std::endl << std::endl;
else
std::cout << "Estado inicial" << std::endl << std::endl;
DynDlist<Statistics_Per_Element> & statistics_per_package = it.get_current().statistics_per_package;
for (DynDlist<Statistics_Per_Element>::Iterator it(statistics_per_package); it.has_current(); it.next())
{
Statistics_Per_Element & s = it.get_current();
std::cout << "Paquete: " << s.get_element_name() << std::endl;
DynDlist<std::string> & h = s.get_history();
for (DynDlist<std::string>::Iterator it(h); it.has_current(); it.next())
{
std::string & st = it.get_current();
std::cout << "Evento: " << st << std::endl;
}
std::cout << std::endl;
}
}
std::cout << "Numero maximo de deflexiones: " << max_deflections << std::endl;
for (int i = 0; i < max_deflections + 1; ++i)
cout << "Numero de paquetes deflectados " << i
<< (i == 1 ? " vez: " : " veces: ") << deflections_counter[i] << endl;
}
void Statistics::clear()
{
num_iterations = generated_packages = deliveried_packages = package_in_queue = 0;
events_list_per_iteration.empty();
delete []deflections_counter;
max_deflections = 0;
}
void Statistics::init()
{
events_list_per_iteration.append(Pair_Of_List());
}
void Statistics::set_max_deflections(const unsigned long & m)
{
max_deflections = MAX(m, max_deflections);
}
void Statistics::count_deflections(DynDlist<Package> & packages)
{
unsigned long long acum_ttl = 0, acum_tiq = 0;
deflections_counter = new unsigned long[max_deflections + 1];
for (int i = 0; i < max_deflections + 1; ++i)
deflections_counter[i] = 0;
for (DynDlist<Package>::Iterator it(packages); it.has_current(); it.next())
{
Package & p = it.get_current();
++deflections_counter[p.get_num_deflections()];
acum_ttl += p.get_ttl();
acum_tiq += p.get_tiq();
}
ttl_avg = double (acum_ttl / packages.size());
tiq_avg = double (acum_tiq / packages.size());
}