-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.cpp
More file actions
84 lines (81 loc) · 2.77 KB
/
helpers.cpp
File metadata and controls
84 lines (81 loc) · 2.77 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
#include "CommonHeader.hpp"
#include <sstream>
static void GenerateTape(const std::string &path, int size) {
std::fstream newFile;
newFile.open(path, std::ios::out | std::ios::binary);
std::default_random_engine gen(
static_cast<unsigned int>(clock())); // clock()
std::uniform_real_distribution<double> dist(DIST_LOWER_LIMIT,
DIST_UPPER_LIMIT);
std::uniform_int_distribution<int> distInt(RECORD_MIN_SIZE,
RECORD_MAX_SIZE);
auto generator = [&]() { return dist(gen); };
auto generatorInt = [&]() { return distInt(gen); };
for (int i = 0; i < size; i++) {
int setSize = generatorInt();
while (setSize--) {
double num = generator();
newFile.write(reinterpret_cast<char *>(&num), sizeof(double));
}
double separator = SEPARATOR_VALUE;
newFile.write(reinterpret_cast<char *>(&separator), sizeof(double));
}
newFile.close();
}
static void IsSorted(std::string filename) {
std::cout << std::endl << "======== SORT CHECK ========" << std::endl;
std::cout << "==== CHECKING: " << filename << " ====" << std::endl;
auto tape =
std::make_shared<Tape>(filename, std::ios::in | std::ios::binary);
#if PRINT_TAPES == 1
std::cout << *tape << std::endl;
#endif
long long counter = 1;
bool isSorted = true;
auto prev = tape->GetNext();
while (tape->HasNext()) {
counter++;
auto next = tape->GetNext();
if (prev > next) {
isSorted = false;
break;
}
prev = next;
}
if (!isSorted) {
std::cout << "NOT SORTED!" << std::endl;
} else
std::cout << "SORTED! " << counter << " RECORDS TOTAL" << std::endl;
}
static int InputFromConsole(std::string path) {
std::fstream file;
std::remove(path.c_str());
int size = 0;
file.open(path, std::ios::out | std::ios::binary | std::ios::app);
std::cout << "======== INSERTING DATA FROM CONSOLE ========" << std::endl;
while (true) {
std::cin.clear();
std::vector<double> data;
std::string line;
std::getline(std::cin, line);
std::istringstream iss(line);
double input;
while (iss >> input) {
data.push_back(input);
}
if (data.empty())
break;
data.push_back(SEPARATOR_VALUE);
std::cout << "RECORD ADDED: ";
for (auto item : data)
if (!std::isnan(item))
std::cout << item << ' ';
std::cout << std::endl;
size++;
double *toWrite = &data[0];
file.write(reinterpret_cast<char *>(toWrite),
data.size() * sizeof(double));
file.flush();
}
return size;
}