-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
177 lines (146 loc) · 5.05 KB
/
main.cpp
File metadata and controls
177 lines (146 loc) · 5.05 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
#include <iostream>
#include <fstream>
#include <utility>
#include <vector>
#include <queue>
using namespace std;
const clock_t begin_time = clock();
struct node {
string sentence;
int index;
node() {};
node(string a, int b) : sentence(move(a)), index(b) {};
bool operator<(const node &other) const {
return (sentence.compare(other.sentence) > 0);
}
};
void writeToDisk(const string &outputFileName, const vector<string> &data) {
ofstream outputStream;
outputStream.open(outputFileName);
if (!outputStream) {
cout << "Can't open output file " << outputFileName << '\n';
exit(EXIT_FAILURE);
}
for (int i = 0; i < data.size() - 1; ++i) {
outputStream << data[i] << '\n';
}
outputStream << data[data.size() - 1];
outputStream.close();
}
int inputHandle(const string &inputFileName, long long availableMemory) {
ifstream inputStream;
inputStream.open(inputFileName);
if (!inputStream) {
cout << "Can't open input file\n";
exit(EXIT_FAILURE);
}
inputStream.seekg(0, inputStream.end);
long long inputSize = inputStream.tellg();
inputStream.seekg(0, inputStream.beg);
vector<string> data;
cout << "File " << inputFileName << " with size " << inputSize << " bytes is being read\n";
int outputFilesCount{0};
long long currentUsedMemory{0};
while (!inputStream.eof()) {
string sentence;
getline(inputStream, sentence);
if (sentence.size() + currentUsedMemory < availableMemory) {
data.push_back(sentence);
currentUsedMemory += sentence.size();
} else {
sort(data.begin(), data.end());
++outputFilesCount;
writeToDisk("Output_file_" + to_string(outputFilesCount), data);
data.clear();
currentUsedMemory = sentence.size();
data.push_back(sentence);
}
}
//handle leftover data if exist
if (!data.empty()) {
sort(data.begin(), data.end());
++outputFilesCount;
writeToDisk("Output_file_" + to_string(outputFilesCount), data);
}
cout << "Input reading is done!\n";
return outputFilesCount;
}
void deleteRedundantOutputFiles(int totalFiles) {
cout << "-------------------------------------------------------\n";
cout << "Removing output files!" << endl;
for (int i = 1; i <= totalFiles; ++i) {
string file{"Output_file_" + to_string(i)};
cout << "Removing Output_file_" << i << '\n';
remove(file.c_str());
}
cout << "-------------------------------------------------------\n\n\n";
}
void kWayMerge(int totalFiles, const string &outputFileName, long long availableMemory) {
cout << "Merging " << totalFiles << " into " << outputFileName << '\n';
ifstream inputStream[totalFiles + 1];
for (int i = 1; i <= totalFiles; ++i) {
inputStream[i].open("Output_file_" + to_string(i));
if (!inputStream[i]) {
cout << "Can't open file Output_file_" << i << '\n';
}
}
ofstream outputStream;
outputStream.open(outputFileName);
if (!outputStream) {
cout << "Can't open output file " << outputFileName << '\n';
}
priority_queue<node, vector<node> > minHeap;
for (int i = 1; i <= totalFiles; ++i) {
string sentence;
getline(inputStream[i], sentence);
minHeap.push(node{sentence, i});
}
//numbers of input files and a output file
long long sizeOfEachPart{availableMemory / (totalFiles + 1)};
long long curUsedMemory{0};
vector<string> data;
node cur;
while (!minHeap.empty()) {
cur = minHeap.top();
minHeap.pop();
if (curUsedMemory + cur.sentence.size() >= sizeOfEachPart) {
for (int i = 0; i < data.size(); ++i) {
outputStream << data[i] << '\n';
}
outputStream.flush();
data.clear();
curUsedMemory = cur.sentence.size();
}
if (!inputStream[cur.index].eof()) {
string sentence;
getline(inputStream[cur.index], sentence);
minHeap.push(node{sentence, cur.index});
}
data.push_back(cur.sentence);
}
if (!data.empty()) {
for (int i = 0; i < data.size(); ++i) {
outputStream << data[i] << '\n';
}
}
for (int i = 1; i <= totalFiles; ++i) {
inputStream[i].close();
}
outputStream.close();
deleteRedundantOutputFiles(totalFiles);
}
int main(int argc, char *argv[]) {
if (argc < 4) {
cout << "Please provide input file name, output file name and memory\n";
return 0;
}
string inputFileName{argv[1]};
string outputFileName{argv[2]};
//memory in bytes
long long availableMemory{strtoll(argv[3], nullptr, 10)};
int totalOutputFiles{inputHandle(inputFileName, availableMemory)};
kWayMerge(totalOutputFiles, outputFileName, availableMemory);
cout << "DONE!\n";
cout << "Executing time: " << float(clock() - begin_time) / CLOCKS_PER_SEC * 1000 << " msec." << endl;
return 0;
}