-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
334 lines (310 loc) · 14.6 KB
/
main.cpp
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
#include <iostream>
#include <fstream>
#include <chrono>
#include <cstdint>
#include <random>
#include "headers\ArrayList.hpp"
#include "headers\SinglyLinkedList.hpp"
#include "headers\SinglyLinkedListTail.hpp"
#include "headers\DoublyLinkedList.hpp"
#include "headers\TimerClock.hpp"
int dataSetsSize[8] = {5000, 8000, 10000, 16000, 20000, 40000, 60000, 100000}; // Wielkosci zestawow danych do zmierzenia
enum Function {
ADD_LAST,
ADD_FIRST,
ADD_AT,
REMOVE_LAST,
REMOVE_FIRST,
REMOVE_FROM,
FIND_ELEMENT
};
// Funkcja generujaca pseudolosowa liczbe z zakresu 0 do max_value
uint32_t generateRandomNumberInRange(uint32_t seed, uint32_t max_value) {
std::mt19937 gen(seed);
std::uniform_int_distribution<uint32_t> dis(0, max_value);
return dis(gen);
}
// Funkcja generujaca pseudolosowa liczbe z seeda
uint32_t generateRandomNumberFromSeed(uint32_t seed) {
std::mt19937 gen(seed);
std::uniform_int_distribution<uint32_t> dis(0, std::numeric_limits<uint32_t>::max());
return dis(gen);
}
uint32_t generateRandomNumber(std::mt19937 &gen) {
std::uniform_int_distribution<uint32_t> dis(0, std::numeric_limits<uint32_t>::max());
return dis(gen);
}
// Funkcja generujaca pseudolosowy zestaw liczb (o romiarze dataSetSize)
template<typename T>
void generateRandomData(T &container, int dataSetSize, uint32_t seed) {
std::mt19937 gen(seed);
container.reset();
for (int i = 0; i < dataSetSize; i++) {
container.addLast(generateRandomNumber(gen));
}
}
// Funkcja mierzaca czas dzialania operacji (func) struktury (container).
// number - element ktory ma zostac dodany,
// index - index elementu na ktorym beda wykonywane operacje,
// elementToFind - element ktory ma byc szukany w strukturze
template<typename T>
double measure(T& container, int32_t number, int index, int32_t elementToFind, Function func) {
TimerClock timer;
switch (func) {
case ADD_LAST:
container.addLast(number);
break;
case ADD_FIRST:
container.addFirst(number);
break;
case ADD_AT:
container.addAt(index, number);
break;
case REMOVE_LAST:
container.removeLast();
break;
case REMOVE_FIRST:
container.removeFirst();
break;
case REMOVE_FROM:
container.removeFrom(index);
break;
case FIND_ELEMENT:
container.findElement(elementToFind);
break;
}
return timer.elapsed();
}
// Funkcja mierzaca sredni czas operacji
// container - struktura ktora ma byc mierzona
// func - mierzona operacja
// seed[] - tablica seedow zasilajaca elementy generowana losowo
// repetitions - liczba powtorzen, funkcji potrzebna do zmierzenia sredniej
// size - rozmiar struktury potrzebny do wygenerowania indeksu na ktory ma zostac dodany element
template<typename T>
double measureAverage(T &container, Function func, uint32_t seed[], int repetitions, int size) {
double avg;
int sum = 0;
for (int i = 0; i < repetitions; i++) {
uint32_t number = generateRandomNumberFromSeed(seed[i]);
int index = generateRandomNumberInRange(seed[i], size);
int indexofElementToFind = generateRandomNumberInRange(seed[i], size);
generateRandomData(container, size, seed[i]);
uint32_t elementToFind = container.getElement(indexofElementToFind);
double averageTime = measure(container, number, index, elementToFind, func);
sum = sum + averageTime;
}
avg = sum / repetitions;
return avg;
}
// Funkcja ktora mierzy czasy i zapisuje je do pliku. Argumenty te same co wyzej + nazwa pliku (fileName)
template<typename T>
void measureAndSave(T& container, Function func, uint32_t seed[], int dataSetsSize[], int repetitions, std::string fileName)
{
std::ofstream output;
output.open("measured_times/" + fileName);
for (int i = 0; i < 8; i++) {
double average = measureAverage(container, func, seed, repetitions, dataSetsSize[i]);
output << dataSetsSize[i] << ", " << average << std::endl;
}
output.close();
}
// Menu do wybrania operacji ktora chcemy przeprowadzic na wybranej wczesniej strukturze
template<typename T>
void performOperations(T& dataStructure) {
int operationMenu;
int element, index;
while (true) {
std::cout << std::endl << "Wybierz operacje:" << std::endl;
std::cout << "1. Dodaj na koncu." << std::endl;
std::cout << "2. Dodaj na poczatku." << std::endl;
std::cout << "3. Dodaj na indeksie." << std::endl;
std::cout << "4. Usun z konca." << std::endl;
std::cout << "5. Usun z poczatku." << std::endl;
std::cout << "6. Usun z indeksu." << std::endl;
std::cout << "7. Wyczysc strukture." << std::endl;
std::cout << "8. Znajdz element." << std::endl;
std::cout << "9. Wyswietl dane." << std::endl;
std::cout << "10. Zapisz strukture do pliku." << std::endl;
std::cout << "11. Wczytaj strukture z pliku." << std::endl;
std::cout << "12. Wygeneruj losowe dane. " << std::endl;
std::cout << "13. Powrot do menu glownego." << std::endl;
std::cout << "Wybierz: ";
std::cin >> operationMenu;
switch (operationMenu) {
case 1:
std::cout << "Podaj element do dodania na koncu: ";
std::cin >> element;
dataStructure.addLast(element);
break;
case 2:
std::cout << "Podaj element do dodania na poczatku: ";
std::cin >> element;
dataStructure.addFirst(element);
break;
case 3:
std::cout << "Podaj indeks: ";
std::cin >> index;
std::cout << "Podaj element do dodania na indeksie " << index << ": ";
std::cin >> element;
dataStructure.addAt(index, element);
break;
case 4:
dataStructure.removeLast();
break;
case 5:
dataStructure.removeFirst();
break;
case 6:
std::cout << "Podaj indeks do usuniecia: ";
std::cin >> index;
dataStructure.removeFrom(index);
break;
case 7:
dataStructure.reset();
std::cout << "Struktura wyczyszczona!";
break;
case 8: {
std::cout << "Podaj element do znalezienia: ";
std::cin >> element;
int indexOfElement = dataStructure.findElement(element);
if (indexOfElement != -1) {
std::cout << "Indeks pierwszego wystapienia elementu: " << indexOfElement << std::endl;
} else {
std::cout << "Nie znaleziono elementu." << std::endl;
}
break;
}
case 9:
std::cout << std::endl << "Wyswietlanie danych:" << std::endl;
dataStructure.display();
break;
case 10:{
std::ofstream output;
std::string fileName;
std::cout << "Podaj nazwe pliku w ktorym chcesz zapisac strukture: ";
std::cin >> fileName;
output.open("saved/" + fileName + ".txt");
for(int i = 0; i < dataStructure.getSize(); i++) {
output << dataStructure.getElement(i) << std::endl;
}
output.close();
break;
}
case 11: {
std::ifstream input;
std::string fileName;
while(!input.is_open()) {
std::cout << "Podaj nazwe pliku ktory chcesz wczytac: ";
std::cin >> fileName;
input.open("saved/" + fileName + ".txt");
if (!input.is_open()) std::cout << "Nie udalo sie otworzyc pliku!" << std::endl;
}
std::string line;
while (std::getline(input, line)) {
dataStructure.addLast(std::stoi(line));
}
break;
}
case 12: {
int seed = rand();
int numberOfData;
std::cout << "Podaj ile danych chcesz wygenerowac: ";
std::cin >> numberOfData;
generateRandomData(dataStructure, numberOfData, seed);
break;
}
case 13:
return;
default:
std::cout << "Niepoprawny wybor operacji." << std::endl;
break;
}
}
}
int main() {
srand((unsigned)time(NULL));
// Menu wyboru struktury
while (true) {
int structureMenu;
std::cout << std::endl << "Wybierz strukture danych:" << std::endl;
std::cout << "1. ArrayList" << std::endl;
std::cout << "2. Singly Linked List" << std::endl;
std::cout << "3. Singly Linked List (tail)" << std::endl;
std::cout << "4. Doubly Linked List" << std::endl;
std::cout << "5. Wykonaj pomiary dla wszystkich struktur i zapisz wyniki do plikow." << std::endl;
std::cout << "6. Wyjscie" << std::endl;
std::cout << "Wybierz: ";
std::cin >> structureMenu;
switch (structureMenu) {
case 1: {
ArrayList arrayList;
performOperations(arrayList);
break;
}
case 2: {
SinglyLinkedList singlyLinkedList;
performOperations(singlyLinkedList);
break;
}
case 3: {
SinglyLinkedListTail singlyLinkedListTail;
performOperations(singlyLinkedListTail);
break;
}
case 4: {
DoublyLinkedList doublyLinkedList;
performOperations(doublyLinkedList);
break;
}
case 5: {
ArrayList arrayList;
SinglyLinkedList singlyLinkedList;
SinglyLinkedListTail singlyLinkedListTail;
DoublyLinkedList doublyLinkedList;
int numberOfIterations;
std::cout << "Ile chcesz wykonac iteracji pomiaru kazdej wielkosci? ";
std::cin >> numberOfIterations;
uint32_t seed[numberOfIterations]; // Tablica losowo generowanych seedow - potrzebna do tego, by kazda z operacji byla mierzona na tym samym zestawie danych
for (int i = 0; i < numberOfIterations; i++) {
seed[i] = rand();
}
measureAndSave(arrayList, ADD_FIRST, seed, dataSetsSize, numberOfIterations, "ArrayList_AddFirst.txt");
measureAndSave(arrayList, ADD_LAST, seed, dataSetsSize, numberOfIterations, "ArrayList_AddLast.txt");
measureAndSave(arrayList, ADD_AT, seed, dataSetsSize, numberOfIterations, "ArrayList_AddAt.txt");
measureAndSave(arrayList, REMOVE_FIRST, seed, dataSetsSize, numberOfIterations, "ArrayList_RemoveFirst.txt");
measureAndSave(arrayList, REMOVE_FROM, seed, dataSetsSize, numberOfIterations, "ArrayList_RemoveFrom.txt");
measureAndSave(arrayList, REMOVE_LAST, seed, dataSetsSize, numberOfIterations, "ArrayList_RemoveLast.txt");
measureAndSave(arrayList, FIND_ELEMENT, seed, dataSetsSize, numberOfIterations, "ArrayList_Find");
measureAndSave(singlyLinkedList, ADD_FIRST, seed, dataSetsSize, numberOfIterations, "SinglyLinkedList_AddFirst.txt");
measureAndSave(singlyLinkedList, ADD_LAST, seed, dataSetsSize, numberOfIterations, "SinglyLinkedList_AddLast.txt");
measureAndSave(singlyLinkedList, ADD_AT, seed, dataSetsSize, numberOfIterations, "SinglyLinkedList_AddAt.txt");
measureAndSave(singlyLinkedList, REMOVE_FIRST, seed, dataSetsSize, numberOfIterations, "SinglyLinkedList_RemoveFirst.txt");
measureAndSave(singlyLinkedList, REMOVE_FROM, seed, dataSetsSize, numberOfIterations, "SinglyLinkedList_RemoveFrom.txt");
measureAndSave(singlyLinkedList, REMOVE_LAST, seed, dataSetsSize, numberOfIterations, "SinglyLinkedList_RemoveLast.txt");
measureAndSave(singlyLinkedList, FIND_ELEMENT, seed, dataSetsSize, numberOfIterations, "SinglyLinkedList_Find.txt");
measureAndSave(singlyLinkedListTail, ADD_FIRST, seed, dataSetsSize, numberOfIterations, "SinglyLinkedListTail_AddFirst.txt");
measureAndSave(singlyLinkedListTail, ADD_LAST, seed, dataSetsSize, numberOfIterations, "SinglyLinkedListTail_AddLast.txt");
measureAndSave(singlyLinkedListTail, ADD_AT, seed, dataSetsSize, numberOfIterations, "SinglyLinkedListTail_AddAt.txt");
measureAndSave(singlyLinkedListTail, REMOVE_FIRST, seed, dataSetsSize, numberOfIterations, "SinglyLinkedListTail_RemoveFirst.txt");
measureAndSave(singlyLinkedListTail, REMOVE_FROM, seed, dataSetsSize, numberOfIterations, "SinglyLinkedListTail_RemoveFrom.txt");
measureAndSave(singlyLinkedListTail, REMOVE_LAST, seed, dataSetsSize, numberOfIterations, "SinglyLinkedListTail_RemoveLast.txt");
measureAndSave(singlyLinkedListTail, FIND_ELEMENT, seed, dataSetsSize, numberOfIterations, "SinglyLinkedListTail_Find.txt");
measureAndSave(doublyLinkedList, ADD_FIRST, seed, dataSetsSize, numberOfIterations, "DoublyLinkedList_AddFirst.txt");
measureAndSave(doublyLinkedList, ADD_LAST, seed, dataSetsSize, numberOfIterations, "DoublyLinkedList_AddLast.txt");
measureAndSave(doublyLinkedList, ADD_AT, seed, dataSetsSize, numberOfIterations, "DoublyLinkedList_AddAt.txt");
measureAndSave(doublyLinkedList, REMOVE_FIRST, seed, dataSetsSize, numberOfIterations, "DoublyLinkedList_RemoveFirst.txt");
measureAndSave(doublyLinkedList, REMOVE_FROM, seed, dataSetsSize, numberOfIterations, "DoublyLinkedList_RemoveFrom.txt");
measureAndSave(doublyLinkedList, REMOVE_LAST, seed, dataSetsSize, numberOfIterations, "DoublyLinkedList_RemoveLast.txt");
measureAndSave(doublyLinkedList, FIND_ELEMENT, seed, dataSetsSize, numberOfIterations, "DoublyLinkedList_Find.txt");
std::cout << std::endl << "Pomiary zostaly wykonane i zapisane w pliku!";
break;
}
case 6:
return 0; // Wyjscie z programu
default:
std::cout << "Niepoprawny wybor struktury danych." << std::endl;
break;
}
}
}