-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
229 lines (183 loc) · 6.58 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
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <ctime>
#include <queue>
using namespace std;
const clock_t begin_time = clock();
struct HeapNode {
string sentence;
int index;
HeapNode(string a, int b): sentence(a), index(b) {}
bool operator<(const HeapNode& rhs) const {
return (sentence.compare(rhs.sentence) > 0);
}
};
int input(string input_name, int TOTAL_MEM) {
ifstream input;
input.open(input_name.c_str());
if (!input.good()) {
cout << "File input is not found!" << endl << "Exit program!" << endl;
exit(-1);
}
int input_size;
input.seekg(0, input.end);
input_size = input.tellg();
input.seekg(0, input.beg);
cout << "-------------------------------------------------------\n";
cout << "The size of the file chosen is (in bytes): " << input_size << endl;
int run_count = 0;
int total_mem_so_far = 0;
ofstream output;
vector<string> data; data.clear();
cout << "File " << input_name << " is being read!" << endl;
cout << "-------------------------------------------------------\n\n\n";
cout << "-------------------------------------------------------\n";
while (!input.eof()) {
string sentence;
getline(input, sentence);
if (total_mem_so_far + sentence.size() < TOTAL_MEM) {
total_mem_so_far += sentence.size() + 1;
data.push_back(sentence);
} else {
sort(data.begin(), data.end());
run_count++;
stringstream ss;
ss << "run_" << run_count << ".txt";
cout << "Writing " << ss.str() << endl;
output.open(ss.str());
int data_size = data.size();
for (int i = 0; i < data_size-1; i++) {
output << data[i];
output << endl;
}
if (data_size > 0) {
output << data[data_size-1];
}
output.close();
data.clear();
total_mem_so_far = sentence.size();
data.push_back(sentence);
}
}
if (data.size() > 0) {
sort(data.begin(), data.end());
run_count++;
stringstream ss;
ss << "run_" << run_count << ".txt";
cout << "Writing " << ss.str() << endl;
output.open(ss.str());
int data_size = data.size();
for (int i = 0; i < data_size-1; i++) {
output << data[i];
output << endl;
}
input.close();
output << data[data_size-1];
output.close();
}
cout << "Read input is done!" << endl;
cout << "Entire process so far took a total of: " << float(clock() - begin_time) / CLOCKS_PER_SEC * 1000 << " msec." << endl;
cout << "-------------------------------------------------------\n\n\n";
return run_count;
}
void merge(int start, int end, int location) {
int runs_count = end - start + 1;
ifstream input[runs_count];
for (int i = 0; i < runs_count; i++) {
stringstream ss;
ss << "run_" << start + i << ".txt";
input[i].open(ss.str());
}
priority_queue<HeapNode, vector<HeapNode> > heap;
ofstream output;
stringstream ss;
ss << "run_" << location << ".txt";
output.open(ss.str());
for (int i = 0; i < runs_count; i++) {
string sentence;
if (!input[i].eof()) {
getline(input[i], sentence);
// cout << sentence << ' ' << i << ' ' << sentence.length() << endl;
// if (sentence.length() == 0) {
// cout << "Making heap: " << i << ' ' << sentence << endl;
// }
heap.push(HeapNode(sentence, i));
}
}
cout << "-------------------------------------------------------\n";
cout << endl << "Merging from run_" << start << " to run_" << end << " into run_" << location << " file" << endl;
while (!heap.empty()) {
string sentence = heap.top().sentence;
int index = heap.top().index;
heap.pop();
// cout << sentence << ' ' << index << ' ' << sentence.length() << endl;
output << sentence << endl;
if (!input[index].eof()) {
getline(input[index], sentence);
heap.push(HeapNode(sentence, index));
}
}
cout << "Merge done!" << endl << endl;
cout << "-------------------------------------------------------\n\n\n";
for (int i = 0; i < runs_count; i++) {
input[i].close();
}
output.close();
}
void merge(int runs_count, string output_name) {
cout << "-------------------------------------------------------\n";
cout << "Merging " << runs_count << " files into output (" << output_name << " file)" << endl;
cout << "-------------------------------------------------------\n\n\n";
//int distance = 100;
int start = 1;
int end = runs_count;
while (start < end) {
int location = end;
int distance = 100;
int time = (end - start + 1) / distance + 1;
if ((end - start + 1) / time < distance) {
distance = (end - start + 1) / time + 1;
}
while (start <= end) {
int mid = min(start + distance, end);
location++;
merge(start, mid, location);
start = mid + 1;
}
end = location;
}
stringstream ss;
ss << "run_" << start << ".txt";
rename(ss.str().c_str(), output_name.c_str());
cout << "-------------------------------------------------------\n";
cout << "Removing chucks files!" << endl;
for (int i = 1; i < end; i++) {
stringstream ss;
ss << "run_" << i << ".txt";
cout << "Removing " << ss.str() << endl;
remove(ss.str().c_str());
}
cout << "-------------------------------------------------------\n\n\n";
}
int main(int argc, char* argv[]) {
if (argc < 4) {
cout << "input_file output_file mem_size" << endl << "Exit program!" << endl;
return 0;
}
cout << "-------------------------------------------------------\n";
cout << "Welcome to Chiendo97's External Sort Program!\n";
cout << "-------------------------------------------------------\n\n\n";
string input_name = argv[1];
string output_name = argv[2];
int TOTAL_MEM = strtol(argv[3], nullptr, 0); // bytes
int runs_count = input(input_name, TOTAL_MEM);
merge(runs_count, output_name);
cout << "Entire process took a total of: " << float(clock() - begin_time) / CLOCKS_PER_SEC * 1000 << " msec." << endl;
return 0;
}