-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ3.cpp
205 lines (189 loc) · 5.86 KB
/
Q3.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
#include <iostream>
#include <fstream>
#include <sstream>
#include <sys/stat.h>
#include <vector>
#include <cstdio>
#include "mergesort.h"
#include "priorityQ.h"
#define ll long long int
using namespace std;
#define MAXBUFFSIZE 1024*1024*2
class FileElement {
public:
ll data;
ifstream *iFile;
FileElement() {
}
FileElement(ll num, ifstream *file) {
data = num;
iFile = file;
}
};
class myComparator {
public:
int operator()(const FileElement a, const FileElement b) const{
if(a.data > b.data)
return 1;
else if(a.data < b.data)
return -1;
else
return 0;
}
};
class KwayMergeSort {
private:
public:
string inputFile;
string outputFile;
vector<string> tempFilesVec;
vector<ifstream*> openTempFilesVec;
KwayMergeSort(string inFile, string outFile) {
inputFile = inFile;
outputFile = outFile;
}
void sortFile() {
divide_sort();
if(!tempFilesVec.empty())
conquer_merge();
}
void divide_sort() {
struct stat filestatus;
stat(inputFile.c_str(), &filestatus);
ifstream file(inputFile,ifstream::binary);
ll chunk_size = MAXBUFFSIZE;
ll total_size = filestatus.st_size;
ll total_chunks = total_size / chunk_size;
ll last_chunk_size = total_size % chunk_size;
if (last_chunk_size != 0) {
++total_chunks; /* add an unfilled final chunk */
}
else {
last_chunk_size = chunk_size;
}
string prevnum;
bool intbroken = false;
for (ll chunk = 0; chunk < total_chunks; ++chunk) {
vector<ll> num_vec;
ll this_chunk_size = chunk == total_chunks - 1 ? last_chunk_size : chunk_size;
char chunk_data[this_chunk_size];
file.read(&chunk_data[0], this_chunk_size);
string chunk_str = string(chunk_data);
stringstream ss(chunk_str);
while (ss.good()) {
string substr;
getline(ss, substr, ',');
if(intbroken) {
prevnum += substr;
substr = prevnum;
intbroken = false;
prevnum = "";
}
stringstream toInt(substr);
ll num = 0;
toInt >> num;
num_vec.push_back(num);
prevnum = substr;
}
if(chunk < total_chunks-1 && chunk_data[this_chunk_size-1]!=',') {
intbroken = true;
prevnum = prevnum.substr(0,prevnum.length()-1);
num_vec.pop_back();
}
else if(chunk<=total_chunks-1 && chunk_data[this_chunk_size-1]==',') {
num_vec.pop_back();
}
mergeSort(num_vec,0,num_vec.size()-1);
if(total_chunks>1) {
write_temp_file(num_vec, chunk);
}
else {
write_single_output_file(num_vec);
}
}
}
void write_single_output_file(vector<ll> &num_vec) {
ofstream *tempop;
tempop = new ofstream(outputFile.c_str(), ios::out);
for (ll i = 0; i < num_vec.size(); ++i) {
if(i==num_vec.size()-1)
*tempop << num_vec[i];
else
*tempop << to_string(num_vec[i]) << ",";
}
tempop->close();
delete tempop;
}
void write_temp_file(vector<ll> &num_vec,ll chunk_no) {
int pos = inputFile.find_last_of('.');
string tempfile;
if (pos != string::npos)
tempfile = inputFile.substr(0,pos);
tempfile += "."+to_string(chunk_no)+".tmp";
ofstream *tempop;
tempop = new ofstream(tempfile.c_str(), ios::out);
for (ll i = 0; i < num_vec.size(); ++i) {
if(i==num_vec.size()-1)
*tempop << num_vec[i];
else
*tempop << num_vec[i] << "\n";
}
tempop->close();
delete tempop;
tempFilesVec.push_back(tempfile);
}
void conquer_merge() {
open_temp_files();
int pos = inputFile.find_last_of('/');
string path;
if (pos != string::npos )
path = inputFile.substr(0,pos);
string outPath = path+"/output.txt";
cout << outPath << endl;
ofstream *outputFile = new ofstream(outPath.c_str(), ios::out);
PriorityQ<FileElement,myComparator> minHeap;
ll line;
for (ll i = 0; i < openTempFilesVec.size(); ++i) {
*openTempFilesVec[i] >> line;
minHeap.push(FileElement(line,openTempFilesVec[i]));
}
while (minHeap.empty() == false) {
FileElement minElement = minHeap.top();
if(minHeap.size()==1) {
*outputFile << minElement.data;
}
else {
*outputFile << minElement.data << ",";
}
minHeap.pop();
*(minElement.iFile) >> line;
if(*(minElement.iFile)) {
minHeap.push(FileElement(line,minElement.iFile));
}
}
close_temp_files();
}
void open_temp_files() {
for (ll i = 0; i < tempFilesVec.size(); ++i) {
ifstream *file;
file = new ifstream(tempFilesVec[i].c_str(), ios::in);
openTempFilesVec.push_back(file);
}
}
void close_temp_files() {
for (ll i=0; i < openTempFilesVec.size(); ++i) {
openTempFilesVec[i]->close();
remove(tempFilesVec[i].c_str());
delete openTempFilesVec[i];
}
}
};
int main(int argc,char ** argv) {
if(argc<3) {
cout << "Usage : main.cpp <input_file> <output_file>" << endl;
return 0;
}
KwayMergeSort *kwms = new KwayMergeSort(argv[1],argv[2]);
kwms->sortFile();
return 0;
}