-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector_test.cpp
More file actions
412 lines (314 loc) · 13.2 KB
/
vector_test.cpp
File metadata and controls
412 lines (314 loc) · 13.2 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#define VECTOR_TEST
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <ctime>
// #include <chrono>
#include "quicksort.cpp"
#include "mergesort.cpp"
#include "bubblesort.cpp"
#include "heapsort.cpp"
#include "insertionsort.cpp"
#include "selectionsort.cpp"
#include <algorithm>
#include "vector_utility.cpp"
#include <sstream>
#include <iomanip>
using namespace std;
// This part only works for windows, I didn't searched for linux and mac variations
#ifdef WIN32
#include <Windows.h>
// Inspecting folder for a list of all files
vector<string> getFileListWithinFolder(std::string folder) {
vector<string> names;
string search_path = folder + "/*.*";
WIN32_FIND_DATA fd;
HANDLE hFind = ::FindFirstFile(search_path.c_str(), &fd);
if(hFind != INVALID_HANDLE_VALUE) {
do {
// read all (real) files in current folder
// , delete '!' read other 2 default folder . and ..
if(! (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ) {
names.push_back(folder+"/"+fd.cFileName);
}
}while(::FindNextFile(hFind, &fd));
::FindClose(hFind);
}
return names;
}
#endif
// Function for creating data with a size and size_step
void create_data_set() {
int n = 1000000;
int step = 100000;
string name;
string folder;
void *v;
// int a=23;
// int size_v[] = {100,500,1000,2000,3000,4000,5000,10000,20000,30000,40000,50000,100000,200000,300000,400000,500000,1000000,2000000,3000000,4000000,5000000,10000000};
// delete
name = "intV_";
folder = "Datasets/vectorInt special/";
for (int size=step; size<=n; size+=step) {
std::stringstream ss;
ss << std::setw(8) << std::setfill('0') << size;
v = malloc(size*sizeof(int));
initRandomVWithUniqueValues((int*)v, size, size, 0);
quicksortRandomPivot(((int*)v)+size/3, size/3);
writeFileWithVector<int>((int*)v, size, name+ss.str()+".bin", folder);
free(v);
}
/////////////////////////////////////////////
// name = "intV_";
// folder = "Datasets/vectorInt Random 100 max value/";
// for (int size=step; size<=n; size+=step) {
// std::stringstream ss;
// ss << std::setw(8) << std::setfill('0') << size;
// v = malloc(size*sizeof(int));
// initRandomV((int*)v, size, 101, 0);
// writeFileWithVector<int>((int*)v, size, name+ss.str()+".bin", folder);
// free(v);
// }
// for (int i=0; i<23; i++) {
// std::stringstream ss;
// ss << std::setw(8) << std::setfill('0') << size_v[i];
// v = malloc(size_v[i]*sizeof(int));
// // initRandomV((int*)v, size_v[i], size_v[i], 0);
// // initSortedV((int*)v, size_v[i], size_v[i], 0);
// initRandomVWithUniqueValues((int*)v, size_v[i], size_v[i], 0);
// quicksortRandomPivot(((int*)v)+size_v[i]/3, size_v[i]/3);
// writeFileWithVector<int>((int*)v, size_v[i], name+ss.str()+".bin", folder);
// free(v);
// }
// name = "floatV_";
// folder = "Datasets/vectorFloat/";
// for (int size=step; size<=n; size+=step) {
// v = malloc(size*sizeof(float));
// initRandomV((float*)v, size, (float)1000.0, (float)0.0);
// if (size==step) printV((float*)v,size);
// writeFileWithVector<float>((float*)v, size, name+to_string(size)+".bin", folder);
// free(v);
// }
// name = "doubleV_";
// folder = "Datasets/vectorDouble/";
// for (int size=step; size<=n; size+=step) {
// v = malloc(size*sizeof(double));
// initRandomV((double*)v, size, (double)1000.0, (double)0.0);
// writeFileWithVector<double>((double*)v, size, name+to_string(size)+".bin", folder);
// free(v);
// }
}
// Function for runing all the implemented sorts with all the differents data created inside folder
template <typename T>
void runTVectorTests(string folder) {
// We define the vector variables
T *v, *v_tmp;
int size;
double t;
// list of sorting functions
vector<void (*)(T*,int)> sortingFuncs;
vector<string> f_names;
//// uncomment for sorting integer vectors created by create_data_set
sortingFuncs.push_back(quicksort);
f_names.push_back("quicksort");
sortingFuncs.push_back(quicksortRandomPivot);
f_names.push_back("quicksortRandomPivot");
f_names.push_back("mergesort");
sortingFuncs.push_back(mergesort);
f_names.push_back("mergesortInPlace");
sortingFuncs.push_back(mergesortInPlace);
f_names.push_back("heapsort");
sortingFuncs.push_back(heapsort);
sortingFuncs.push_back(bubblesort);
f_names.push_back("bubblesort");
sortingFuncs.push_back(insertionsort);
f_names.push_back("insertionsort");
sortingFuncs.push_back(selectionsort);
f_names.push_back("selectionsort");
//
// we search the file list for the datasets and iterate over them
vector<string> files = getFileListWithinFolder(folder);
cout.precision(6);
for (vector<string>::iterator i = files.begin(); i<files.end(); i++) {
cout << "Reading file: " << *i << endl;
// We read the file as vector and return it
v = readVectorFromFile<T>(&size, *i);
v_tmp = (T*) malloc(size*sizeof(T));
// cout << "checkSorting input: " << checkSorting(v,size) << endl;
for (int j=0; j<f_names.size(); j++) {
// we copie v into the temporal vector, so we can run all the sortings with the same vector
memcpy(v_tmp, v, size*sizeof(T));
// we measure the time
t = measureTimeOf(sortingFuncs[j], v_tmp, size);
cout << f_names[j] << " in " << t << "sec for " << size << " elements." << endl;
if (checkSorting(v_tmp,size)) cout << "Error en " << f_names[j] << " sorting\n";
}
memcpy(v_tmp, v, size*sizeof(T));
// we measure the time of std::sort
double t = measureTimeOf(std::sort, v_tmp, v_tmp+size);
cout << "std::sort in " << t << "sec " << endl;
// we free de memory that the vector and temporal vector were using
free(v);
free(v_tmp);
}
}
template <typename T>
void runTVectorTestsWriteDataTo(void (*s)(T*,int), string folder, string output_file) {
// We define the vector variables
T *v, *v_tmp;
int size;
double t;
int n = 10;
float t_avr;
// we search the file list for the datasets and iterate over them
vector<string> files = getFileListWithinFolder(folder);
cout.precision(6);
ofstream output;
output.open(output_file, ios::out | ios::trunc);
for (vector<string>::iterator i = files.begin(); i<files.end(); i++) {
t_avr = 0;
cout << "Reading file: " << *i << endl;
// We read the file as vector and return it
v = readVectorFromFile<T>(&size, *i);
v_tmp = (T*) malloc(size*sizeof(T));
// cout << "checkSorting input: " << checkSorting(v,size) << endl;
for (int j=0; j<n; j++) {
// we copie v into the temporal vector, so we can run all the sortings with the same vector
memcpy(v_tmp, v, size*sizeof(T));
// we measure the time
t = measureTimeOf(s, v_tmp, size);
// cout << f_names[j] << " in " << t << "sec for " << size << " elements." << endl;
if (checkSorting(v_tmp,size)) cout << "Error in sorting\n";
t_avr += t/n;
}
memcpy(v_tmp, v, size*sizeof(T));
output << t_avr << ";";
// we free de memory that the vector and temporal vector were using
free(v);
free(v_tmp);
}
// we close the file
output.close();
}
template <typename T>
void runTVectorTestsWriteDataTo(void (*s)(T*,T*), string folder, string output_file) {
// We define the vector variables
T *v, *v_tmp;
int size;
double t;
int n = 10;
float t_avr;
// we search the file list for the datasets and iterate over them
vector<string> files = getFileListWithinFolder(folder);
cout.precision(6);
ofstream output;
output.open(output_file, ios::out | ios::trunc);
for (vector<string>::iterator i = files.begin(); i<files.end(); i++) {
t_avr = 0;
cout << "Reading file: " << *i << endl;
// We read the file as vector and return it
v = readVectorFromFile<T>(&size, *i);
v_tmp = (T*) malloc(size*sizeof(T));
// cout << "checkSorting input: " << checkSorting(v,size) << endl;
for (int j=0; j<n; j++) {
// we copie v into the temporal vector, so we can run all the sortings with the same vector
memcpy(v_tmp, v, size*sizeof(T));
// we measure the time
t = measureTimeOf(s, v_tmp, v_tmp+size);
// cout << f_names[j] << " in " << t << "sec for " << size << " elements." << endl;
if (checkSorting(v_tmp,size)) cout << "Error in sorting\n";
t_avr += t/n;
}
memcpy(v_tmp, v, size*sizeof(T));
output << t_avr << ";";
// we free de memory that the vector and temporal vector were using
free(v);
free(v_tmp);
}
output.close();
}
int main() {
// srand(time(0)); // I dont know were to put this one, but its needed for the quicksortRandomPivot and create data sets
// create_data_set(); // uncomment for creating data
// runTVectorTests<int>("Datasets/vectorInt"); // uncomment for sorting integer vectors created by create_data_set
// runTVectorTests<float>("Datasets/vectorFloat/"); // uncomment for sorting floating points vectors created by create_data_set
// runTVectorTests<double>("Datasets/vectorDouble/"); // uncomment for sorting floating points with double precision vectors created by create_data_set
// // This section is for sorting specific files with a specific sorting, and write the result into a file
// typedef int data_type; // Must match vector type for the input file
// string input_file = "Datasets/vectorFloat/floatV_100.bin";
// string output_file = "Datasets/vectorFloat/Sorted/floatV_100.bin";
// auto eval = [] (data_type a, data_type b) { return a<b; }; // for ascending order
// // auto eval = [] (data_type a, data_type b) { return a>b; }; // for descending order
// sortBinFileToBinFile<data_type>(input_file, output_file, quicksort, eval);
// string output_file = "Datasets/vectorInt/intV_10000000_sorted.bin";
// string output_file = "Datasets/vectorInt semi sorted With Unique Values/intV_00000100.bin";
// int size;
// data_type * v = readVectorFromFile<data_type>(&size, output_file);
// printV(v, size);
// if (!checkSorting(v, size)) cout << "Correct sorted order" << endl;
// else cout << "Incorrect sorted order" << endl;
/*
SORTINGS:
quicksort
quicksortRandomPivot
mergesort
mergesortInPlace
heapsort
bubblesort
insertionsort
selectionsort
std::sort
*/
vector<void (*)(int*,int)> sortingFuncs;
vector<string> f_names;
// uncomment for sorting integer vectors created by create_data_set
// sortingFuncs.push_back(quicksort);
// f_names.push_back("quicksort");
// sortingFuncs.push_back(quicksortRandomPivot);
// f_names.push_back("quicksortRandomPivot");
// f_names.push_back("mergesort");
// sortingFuncs.push_back(mergesort);
// f_names.push_back("mergesortInPlace");
// sortingFuncs.push_back(mergesortInPlace);
// f_names.push_back("heapsort");
// sortingFuncs.push_back(heapsort);
// sortingFuncs.push_back(bubblesort);
// f_names.push_back("bubblesort");
// sortingFuncs.push_back(insertionsort);
// f_names.push_back("insertionsort");
// sortingFuncs.push_back(selectionsort);
// f_names.push_back("selectionsort");
// for (int i=0; i<sortingFuncs.size(); i++) {
// runTVectorTestsWriteDataTo<int>(sortingFuncs[i], "Datasets/vectorInt", "Datasets/vectorInt/Sorted/"+f_names[i]+".txt");
// runTVectorTestsWriteDataTo<int>(sortingFuncs[i], "Datasets/vectorInt random With Unique Values", "Datasets/vectorInt random With Unique Values/Sorted/"+f_names[i]+".txt");
// runTVectorTestsWriteDataTo<int>(sortingFuncs[i], "Datasets/vectorInt sorted", "Datasets/vectorInt sorted/Sorted/"+f_names[i]+".txt");
// runTVectorTestsWriteDataTo<int>(sortingFuncs[i], "Datasets/vectorInt semi sorted With Unique Values", "Datasets/vectorInt semi sorted With Unique Values/Sorted/"+f_names[i]+".txt");
// runTVectorTestsWriteDataTo<int>(sortingFuncs[i], "Datasets/vectorInt Random 100 max value", "Datasets/vectorInt Random 100 max value/Sorted/"+f_names[i]+".txt");
// runTVectorTestsWriteDataTo<int>(sortingFuncs[i], "Datasets/vectorInt special", "Datasets/vectorInt special/Sorted/"+f_names[i]+".txt");
// }
// runTVectorTestsWriteDataTo<int>(std::sort, "Datasets/vectorInt", "Datasets/vectorInt/Sorted/std_sort.txt");
// runTVectorTestsWriteDataTo<int>(std::sort, "Datasets/vectorInt random With Unique Values", "Datasets/vectorInt random With Unique Values/Sorted/std_sort.txt");
// runTVectorTestsWriteDataTo<int>(std::sort, "Datasets/vectorInt sorted", "Datasets/vectorInt sorted/Sorted/std_sort.txt");
// runTVectorTestsWriteDataTo<int>(std::sort, "Datasets/vectorInt semi sorted With Unique Values", "Datasets/vectorInt semi sorted With Unique Values/Sorted/std_sort.txt");
// runTVectorTestsWriteDataTo<int>(std::sort, "Datasets/vectorInt Random 100 max value", "Datasets/vectorInt Random 100 max value/Sorted/std_sort.txt");
// // Single test
int *v;
int size;
v = readVectorFromFile<int>(&size, "Datasets/vectorInt random With Unique Values/intV_10000000.bin");
double t = measureTimeOf(quicksort, v, size);
cout << "heapsort in " << t << "sec for " << size << " elements." << endl;
if (!checkSorting(v, size)) cout << "Correct sorted order" << endl;
else cout << "Incorrect sorted order" << endl;
cout << v[0] << v[size-1] << endl;
// free(v);
initSortedV(v, size, size, 0);
shuffle(v,size);
t = measureTimeOf(quicksort, v, size);
cout << "heapsort in " << t << "sec for " << size << " elements." << endl;
if (!checkSorting(v, size)) cout << "Correct sorted order" << endl;
else cout << "Incorrect sorted order" << endl;
cout << v[0] << v[size-1] << endl;
return 0;
}