-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexternalSort.cpp
98 lines (91 loc) · 2.75 KB
/
externalSort.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
#include "externalSort.h"
#include <algorithm>
#include <sstream>
#include <map>
using namespace std;
typedef std::pair <string, string> stringPair;
bool next(Seq* seq) {
if (seq->source.good()) {
seq->source >> seq->value.first >> seq->value.second;
return true;
}
return false;
}
static int counter = 0;
void externalSort::sort1(){
int number = 0;
std::ifstream in(this->filename);
while(!in.eof()){
std::vector<stringPair> values = std::vector<stringPair>();
string s1, s2;
while(number<pages*8096 && !in.eof()){
std::map<string,string> mymap;
string line;
getline(in, line);
istringstream ss(line);
ss >> s1;
while(ss>>s2){
if(mymap.find(s2)==mymap.end()){
mymap[s2] = s1;
std::pair <string,string> foo;
foo = make_pair(s2, s1);
values.push_back(foo);
number+=16;
}
}
}
//cout << "vector size is " << values.size() <<endl;
number = 0;
std::sort(values.begin(),values.end());
string outputfile = "input" + to_string(counter) + ".txt";
counter++;
//cout << "counter is " << counter <<endl;
std::ofstream out;
out.open(outputfile, std::ofstream::out | std::ofstream::app);
if(!out) {cout << "Cannot open output file\n" ;}
for(int i=0; i<values.size(); i++) {
if(i==values.size()-1){
out << values[i].first << " " << values[i].second;
}
else{
out << values[i].first << " " << values[i].second <<endl;
}
}
out.close();
inputfile.push_back(outputfile);
}
in.close();
cout << "seprating file done!\n" ;
}
void externalSort::merge(std::vector<Seq*>& sequences, std::ofstream& output) {
auto comp = []( const Seq* lhs, const Seq* rhs ) { return lhs->value.first > rhs->value.first; };
std::priority_queue<Seq*, std::vector<Seq*>, decltype(comp)> pq(sequences.begin(), sequences.end(), comp);
string s1 = " ";
while(!pq.empty()) {
Seq* top = pq.top();
pq.pop();
if(top->value.first == s1)
output << " " <<top->value.second;
else
output<<endl << top->value.first <<" " << top->value.second;
s1 = top->value.first;
if(next(top)) {
pq.push(top);
}
}
}
void externalSort::externalMerge(){
sort1();
string outputFilename = "sortedIndex.txt";
std::ofstream output(outputFilename);
std::vector<Seq*> sequences = std::vector<Seq*>();
for(int i=0; i<inputfile.size(); i++){
//cout << inputfile[i] <<endl;
Seq* seq = new Seq(inputfile[i]);
if(next(seq)) {
sequences.push_back(seq);
}
}
merge(sequences, output);
output.close();
}