2 #include "bbtkSimpleUtilities.h"
3 #define LOG_FILE_NAME "log.txt"
9 //template <typename T>
18 std::string kind(int*);
19 std::string is(int*, bool withkind=false);
20 std::string kind(double* a);
21 std::string is(double* a, bool withkind=false);
22 std::string kind(float* a);
23 std::string is(float* a, bool withkind=false);
24 std::string kind(char* a);
25 std::string is(char* a, bool withkind=false);
26 std::string kind(long* a);
27 std::string is(long* a, bool withkind=false);
28 std::string kind(short* a);
29 std::string is(short* a, bool withkind=false);
30 std::string kind(bool* a);
31 std::string is(bool* a, bool withkind=false);
32 std::string kind(void* a);
33 std::string is(void* a, bool withkind=false);
35 std::string kind(unsigned int* a);
36 std::string is(unsigned int* a, bool withkind=false);
37 std::string kind(unsigned char* a);
38 std::string is(unsigned char* a, bool withkind=false);
39 std::string kind(unsigned long* a);
40 std::string is(unsigned long* a, bool withkind=false);
41 std::string kind(unsigned short* a);
42 std::string is(unsigned short* a, bool withkind=false);
43 std::string kind(__int64* a);
44 std::string is(__int64* a, bool withkind=false);
45 std::string kind(unsigned __int64* a);
46 std::string is(unsigned __int64* a, bool withkind=false);
47 std::string kind(long double* a);
48 std::string is(long double* a, bool withkind=false);
50 std::string kind(int a);
51 std::string is(int a, bool withkind=false);
52 std::string kind(double a);
53 std::string is(double a, bool withkind=false);
54 std::string kind(float a);
55 std::string is(float a, bool withkind=false);
56 std::string kind(short a);
57 std::string is(short a, bool withkind=false);
58 std::string kind(char a);
59 std::string is(char a, bool withkind=false);
60 std::string kind(long a);
61 std::string is(long a, bool withkind=false);
62 std::string kind(bool a);
63 std::string is(bool a, bool withkind=false);
64 std::string kind(unsigned int a);
65 std::string is(unsigned int a, bool withkind=false);
66 std::string kind(unsigned char a);
67 std::string is(unsigned char a, bool withkind=false);
68 std::string kind(unsigned long a);
69 std::string is(unsigned long a, bool withkind=false);
70 std::string kind(unsigned short a);
71 std::string is(unsigned short a, bool withkind=false);
72 std::string kind(__int64 a);
73 std::string is(__int64 a, bool withkind=false);
74 std::string kind(unsigned __int64 a);
75 std::string is(unsigned __int64 a, bool withkind=false);
76 std::string kind(long double a);
77 std::string is(long double a, bool withkind=false);
79 std::string kind(std::vector<int> a);
80 std::string is(std::vector<int> a, bool withkind=false);
81 std::string kind(std::vector<double> a);
82 std::string is(std::vector<double> a, bool withkind=false);
83 std::string kind(std::vector<float> a);
84 std::string is(std::vector<float> a, bool withkind=false);
85 std::string kind(std::vector<short> a);
86 std::string is(std::vector<short> a, bool withkind=false);
87 std::string kind(std::vector<char> a);
88 std::string is(std::vector<char> a, bool withkind=false);
89 std::string kind(std::vector<long> a);
90 std::string is(std::vector<long> a, bool withkind=false);
91 std::string kind(std::vector<bool> a);
92 std::string is(std::vector<bool> a, bool withkind=false);
93 std::string kind(std::vector<__int64> a);
94 std::string is(std::vector<__int64> a, bool withkind=false);
95 std::string kind(std::vector<unsigned int> a);
96 std::string is(std::vector<unsigned int> a, bool withkind=false);
97 std::string kind(std::vector<unsigned char> a);
98 std::string is(std::vector<unsigned char> a, bool withkind=false);
99 std::string kind(std::vector<unsigned long> a);
100 std::string is(std::vector<unsigned long> a, bool withkind=false);
101 std::string kind(std::vector<unsigned short> a);
102 std::string is(std::vector<unsigned short> a, bool withkind=false);
103 std::string kind(std::vector<long double> a);
104 std::string is(std::vector<long double> a, bool withkind=false);
106 std::string is(double* a,int len=1, bool withkind=false);
107 std::string is(int* a,int len=1, bool withkind=false);
108 std::string is(float* a,int len=1, bool withkind=false);
109 std::string is(char* a,int len=1, bool withkind=false);
116 bool persistence::writeFile(std::string stm, std::string content)
118 std::ofstream myfile (stm.c_str());
119 if (myfile.is_open())
131 std::string persistence::readFile(std::string path)
134 std::stringstream stm;
135 std::ifstream myfile (path.c_str());
136 if (myfile.is_open())
138 while (! myfile.eof() )
140 getline (myfile,line);
141 stm << line << std::endl;
155 std::string logging::fecha()
158 seconds = time (NULL);
161 std::stringstream out;
163 std::string la_fecha = out.str();
164 //_strtime( timeStr );
165 //std::string la_fecha(timeStr);
166 return "["+la_fecha+"] ";
169 void logging::out(std::string texto)
171 std::ofstream outFile;
172 outFile.open(LOG_FILE_NAME, std::ios::app);
175 outFile << fecha() << texto << std::endl;
179 void logging::erase()
181 std::ofstream outFile;
182 outFile.open(LOG_FILE_NAME, std::ios::out);
185 outFile << fecha() << "INICIO" << std::endl;
190 std::vector<std::string> translate::StringSplit(std::string str, std::string delim)
192 std::vector<std::string> results;
194 while( (cutAt = (int)str.find_first_of(delim)) != str.npos )
198 results.push_back(str.substr(0,cutAt));
200 str = str.substr(cutAt+1);
204 results.push_back(str);
209 std::vector<double> translate::stringTovector(std::string texto, std::string start, std::string end)
211 std::vector<double> el_vector;
212 std::vector<std::string> partes;
215 int inicial = (int)texto.find( start, 0 );
216 int final = (int)texto.find( end, 0 );
218 if (inicial == std::string::npos || final == std::string::npos)
223 std::string contenido = texto.substr(inicial+1, final-inicial-1);
225 partes = StringSplit(contenido, ",");
226 if (partes.size() != -1){
227 for (int i=0; i<(int)partes.size(); i++)
229 if (partes.at(i).size() > 0)
231 double uno = atof(partes.at(i).data());
232 el_vector.push_back(uno);
239 std::vector<double> translate::stringTovectorDelimited(std::string texto, std::string delimitador)
241 std::vector<double> el_vector;
242 std::vector<std::string> partes;
244 std::string contenido = texto;
246 partes = StringSplit(contenido, delimitador);
247 if (partes.size() != -1){
248 for (int i=0; i<(int)partes.size(); i++)
250 double uno = atof(partes.at(i).data());
251 el_vector.push_back(uno);
257 double* translate::stringToArray(std::string texto)
259 std::vector<double> el_vector = stringTovector(texto);
260 double* array = new double[el_vector.size()];
261 for (int i=0; i<(int)el_vector.size(); i++)
263 array[i]=el_vector[i];
268 std::string translate::getInnerInfo(std::string texto)
270 int first_par = (int)texto.find("[", 0);
271 int last_par = (int)texto.rfind("]", texto.size());
273 return texto.substr(first_par+1,last_par-first_par-1);
276 std::vector<double*> translate::stringToVectorArray(std::string texto)
278 std::vector<double*> el_vector;
279 std::string innerInfo = getInnerInfo(texto);
285 while (lastComma != std::string::npos)
287 first_par = (int)innerInfo.find("[", lastComma);
288 last_par = (int)innerInfo.find("]", lastComma);
289 lastComma = (int)innerInfo.find(",", last_par);
290 if (first_par == std::string::npos || last_par == std::string::npos)
292 std::string subArreglo = innerInfo.substr(first_par,last_par-first_par+1);
293 double* subArray = stringToArray(subArreglo);
294 el_vector.push_back(subArray);
299 std::string translate::vectorToStringDelimited(std::vector<double> a, std::string delim)
301 std::stringstream out;
304 for (i = 0; i < a.size(); i++)
315 //Recorre el vector de manera ciclica
316 double vectores::vectorInfinite(std::vector<double> in, int index)
318 //std::cout << "index = " << index;
320 int tamanio = in.size();
321 bool inverso = false;
329 value = tamanio - index%tamanio;
330 if (value == tamanio)
333 value = index%tamanio;
335 //std::cout << ", value=" << value << std::endl;
343 Ordenamiento busbuja, array[0] es el maximo
344 http://mathbits.com/mathbits/compsci/Arrays/Bubble.htm
346 void sorts::bubble(std::vector<double> &array)
348 double i, j, flag = 1; // set flag to 1 to begin initial pass
349 double temp; // holding variable
350 double arrayLength = array.size();
351 for(i = 1; (i <= arrayLength) && flag; i++)
354 for (j=0; j < (arrayLength -1); j++)
356 if (array[(int)(j+1)] > array[(int)j]) // ascending order simply changes to <
358 temp = array[(int)j]; // swap elements
359 array[(int)j] = array[(int)(j+1)];
360 array[(int)(j+1)] = temp;
361 flag = 1; // indicates that a swap occurred.