-
Notifications
You must be signed in to change notification settings - Fork 0
/
header.h
277 lines (225 loc) · 6.68 KB
/
header.h
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// =================================================================
//
// File: header.h
// Author: Pedro Perez
// Description: This file contains the interface and implementation
// of the Chronometer class, as well as the
// implementation of some support functions. This class
// is used to record the execution time of a program.
//
// Copyright (c) 2020 by Tecnologico de Monterrey.
// All Rights Reserved. May be reproduced for any non-commercial
// purpose.
// =================================================================
#ifndef HEADER_H
#define HEADER_H
#include <fstream>
#include <iostream>
#include <sstream>
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <sys/time.h>
#include <sys/types.h>
#include <string>
#include <vector>
using namespace std;
vector<string> search(vector <string> datos1, string search1){
vector<string> datosFiltrados;
for(int i=0;i < datos1.size();i++){
int found = datos1[i].find(search1);
if(found!=datos1[i].npos){
datosFiltrados.push_back(datos1[i]);
}
}
return datosFiltrados;
}
vector<string> selectionSort(vector<int> &dayValue, vector <string> &datos1){
vector <int> x = dayValue;
vector <string> y = datos1;
for (int i = 0; i < x.size()-1; i++) {
for(int j = i + 1; j < x.size(); j++){
if (x[j] < x[i]){
swap(y[i],y[j]);
swap(x[i],x[j]);
}
}
}
return y;
}
vector<int> date2dayvalue(vector<string> &dates){
vector <int> dayValue;
string tempDatum;
int day,month;
for(int i = 0; i<dates.size();i+=3){
tempDatum = dates[i];
stringstream cinDay(tempDatum);
cinDay >> day;
tempDatum = dates[i+1];
stringstream cinMonth(tempDatum);
cinMonth >> month;
tempDatum = dates[i+2];
int tempDayValue = ((month-1)*30)+day;
dayValue.push_back(tempDayValue);
}
return dayValue;
}
vector<int> string2date(vector<string> &datePlusHour){
//Obtiene el string de fecha y hora de la entrada de
//datos
vector<string> dates;
string tempDatum;
for(int i = 0;i<datePlusHour.size(); i+=4){
//cout << datePlusHour[i] << "\t" <<datePlusHour[i+1] <<endl;
tempDatum = datePlusHour[i];
stringstream cinDate(tempDatum);
while(getline(cinDate,tempDatum,'-')){
dates.push_back(tempDatum);
}
}
vector <int> dayValue = date2dayvalue(dates);
return dayValue;
}
vector<int> hour2sec(vector<string> &hours){
vector <int> secValue;
string tempDatum;
int hour,min;
for(int i = 0; i<hours.size();i+=2){
tempDatum = hours[i];
stringstream cinHour(tempDatum);
cinHour >> hour;
tempDatum = hours[i+1];
stringstream cinMin(tempDatum);
cinMin >> min;
int tempSecValue = (hour*3600)+min*60;
secValue.push_back(tempSecValue);
}
return secValue;
}
vector<int> string2hour(vector<string> &datePlusHour){
//Obtiene el string de fecha y hora de la entrada de
//datos
vector<string> hours;
string tempDatum;
for(int i = 1;i<datePlusHour.size(); i+=4){
//cout << datePlusHour[i] << "\t" <<datePlusHour[i+1] <<endl;
tempDatum = datePlusHour[i];
stringstream cinHour(tempDatum);
while(getline(cinHour,tempDatum,':')){
hours.push_back(tempDatum);
}
}
vector <int> secValue = hour2sec(hours);
return secValue;
}
vector<int> date2secs(vector<string> &datePlusHour){
vector <int> dayValue = string2date(datePlusHour);
vector <int> secValue = string2hour(datePlusHour);
vector <int> totalValue;
for(int i = 0; i < dayValue.size(); i++){
int secs = (dayValue[i]*3600*24) + (secValue[i]);
totalValue.push_back(secs);
}
return totalValue;
}
vector<int> line2string(vector<string> &data){
//Desgloza la entrada de datos, asigna una posición
//de un vector a cada string entre espacio y espacio
vector<string> datePlusHour;
for(int i=0; i<data.size(); i++){
string tempLine = data[i];
stringstream cinData(tempLine);
while(getline(cinData,tempLine,' ')){
datePlusHour.push_back(tempLine);
}
}
vector <int> totalValue = date2secs(datePlusHour);
return totalValue;
}
// =================================================================
// This class allows us to calculate the time that elapses between
// one execution line and another.
// =================================================================
class Chronometer {
private:
timeval startTime;
bool started;
public:
Chronometer() :started(false) {}
void start(){
started = true;
gettimeofday(&startTime, NULL);
}
double stop(){
timeval endTime;
long seconds, useconds;
double duration = -1;
if (started) {
gettimeofday(&endTime, NULL);
seconds = endTime.tv_sec - startTime.tv_sec;
useconds = endTime.tv_usec - startTime.tv_usec;
duration = (seconds * 1000.0) + (useconds / 1000.0);
started = false;
}
return duration;
}
};
// =================================================================
// Swap the content of two localities (i, j) in array A.
//
// @param A, an array of T elements.
// @param i, an index in the array.
// @param j, an index in the array.
// =================================================================
template <class T>
void swap(T *A, int i, int j) {
T aux = A[i];
A[i] = A[j];
A[j] = aux;
}
// =================================================================
// Swap the content of two localities (i, j) in vector v.
//
// @param v, a vector of T elements.
// @param i, an index in the vector.
// @param j, an index in the vector.
// =================================================================
template <class T>
void swap(vector<T> &v, int i, int j) {
T aux = v[i];
v[i] = v[j];
v[j] = aux;
}
// =================================================================
// Converts the content of an array to a string.
//
// @param A, an array of T elements.
// @param size, the number of elements in the array.
// =================================================================
template <class T>
string arr2str(T *A, int size) {
stringstream aux;
aux << "[" << A[0];
for (int i = 1; i < size; i++) {
aux << ", " << A[i];
}
aux << "]";
return aux.str();
}
// =================================================================
// Converts the content of a vector to a string.
//
// @param v, a vector of T elements.
// @param size, the number of elements in the array.
// =================================================================
template <class T>
string vec2str(const std::vector<T> &v) {
stringstream aux;
aux << "[" << v[0];
for (int i = 1; i < v.size(); i++) {
aux << ", " << v[i];
}
aux << "]";
return aux.str();
}
#endif