-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface.cpp
More file actions
110 lines (80 loc) · 2.55 KB
/
interface.cpp
File metadata and controls
110 lines (80 loc) · 2.55 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
#include "interface.h"
std::vector<std::unique_ptr<Tape>>
Interface::createBlockSort(Tape &inputFile, uint blockSize, uint blockCount) {
std::vector<T> arr(blockSize);
std::vector<std::unique_ptr<Tape>> tmpFiles;
uint i, j;
for (i = 0; i < blockCount; i++) {
for (j = 0; j < blockSize && !inputFile.isEnd();
j++, inputFile.moveRight()) {
arr[j] = inputFile.getData();
}
std::sort(arr.begin(), arr.begin() + j);
tmpFiles.emplace_back(std::make_unique<Tape>(
j * sizeof(T), "tmpFile" + std::to_string(i + 1)));
for (uint t = 0; t < j; t++, tmpFiles[i]->moveRight()) {
tmpFiles[i]->writeData(arr[t]);
}
tmpFiles[i]->headBegin();
}
return tmpFiles;
}
void Interface::merge(Tape &a, Tape &b, const std::string &path) {
Tape result(a.size() + b.size(), path);
a.headBegin();
b.headBegin();
for (uint i = 0; i < result.count(); i++) {
if (!a.isEnd() && (b.isEnd() || b.getData() > a.getData())) {
result.writeData(a.getData());
a.moveRight();
} else {
result.writeData(b.getData());
b.moveRight();
}
result.moveRight();
}
}
void Interface::Extern_sort(const std::string &path_in,
const std::string &path_out) {
Tape inputFile(path_in);
uint blockCount = inputFile.size() / RAM;
uint blockSize = RAM / sizeof(T);
std::vector<std::unique_ptr<Tape>> tmpFiles =
createBlockSort(inputFile, blockSize, blockCount);
for (uint i = 0; i + 1 < blockCount; i++) {
Tape tape("tmpFilesMerge" + std::to_string(i));
merge(tape, *tmpFiles[i], "tmpFilesMerge" + std::to_string(i + 1));
tape.deleteTape();
}
Tape tape("tmpFilesMerge" + std::to_string(blockCount - 1));
merge(tape, *tmpFiles[blockCount - 1], path_out);
tape.deleteTape();
for (auto &v : tmpFiles) {
v->deleteTape();
}
}
void Interface::show(Tape &tape) {
tape.headBegin();
while (!tape.isEnd()) {
std::cout << tape.getData() << ' ';
tape.moveRight();
}
std::cout << std::endl;
tape.headBegin();
}
bool Interface::isSorted(Tape &tape) {
if (tape.count() <= 1) {
return true;
}
tape.headBegin();
T prevValue = tape.getData();
tape.moveRight();
while (!tape.isEnd()) {
if (tape.getData() < prevValue) {
return false;
}
prevValue = tape.getData();
tape.moveRight();
}
return true;
}