-
Notifications
You must be signed in to change notification settings - Fork 4
/
Multiway.h
49 lines (44 loc) · 1.31 KB
/
Multiway.h
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
#ifndef CH2_MULTIWAY_H
#define CH2_MULTIWAY_H
#include <fstream>
#include <iostream>
#include "../head/IndexMinPQ.h"
using std::fstream;
using std::string;
using std::cout;
using std::endl;
/**
* The {@code Multiway} class provides a client for reading in several
* sorted text files and merging them together into a single sorted
* text stream.
* This implementation uses a {@link IndexMinPQ} to perform the multiway
* merge.
* <p>
* For additional documentation, see <a href="https://algs4.cs.princeton.edu/24pq">Section 2.4</a>
* of <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
class Multiway {
public:
// This class should not be instantiated.
Multiway() = delete;
// merge together the sorted input streams and write the sorted result to standard output
static void merge(vector<fstream> &vec) {
int N = vec.size();
IndexMinPQ<string> pq(N);
string tmp;
for (int i = 0; i < vec.size(); ++i) {
if (vec[i] >> tmp)
pq.insert(i, tmp);
}
while (!pq.isEmpty()) {
cout << pq.minKey() << " ";
int i = pq.delMin();
if (vec[i] >> tmp)
pq.insert(i, tmp);
}
}
};
#endif //CH2_MULTIWAY_H