-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
60 lines (57 loc) · 1.3 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
#include "heap.h"
#include "m3sketch.h"
#include <iostream>
#include <fstream>
#include <cstring>
#include <stdexcept>
template <typename Functor>
void processIntFile(char* fname, Functor functor) {
std::ifstream in(fname);
if (in.fail()) {
throw std::runtime_error("file open failed.\n");
}
int x;
while (in >> x) {
functor(x);
}
in.close();
}
bool intLessThan(const int& l, const int& r) {
return l < r;
}
bool intGreaterThan(const int& l, const int& r) {
return l > r;
}
int main(int argc, char* argv[]) {
if (argc != 4) {
std::cerr << "wrong number of args [data struct] [insert] [remove]\n";
} else {
if (strcmp(argv[1], "heap") == 0) {
Heap<int> minH(&intLessThan, true);
Heap<int> maxH(&intGreaterThan, false);
processIntFile(argv[2], [&](int x) {
minH.insert(x);
maxH.insert(x);
});
processIntFile(argv[3], [&](int x) {
minH.remove(x);
maxH.remove(x);
});
std::cout << "Min Heap:\n";
minH.report();
std::cout << "Max Heap:\n";
maxH.report();
} else if (strcmp(argv[1], "minmedianmax") == 0) {
M3Sketch<int> m3(&intLessThan, &intGreaterThan);
processIntFile(argv[2], [&](int x) {
m3.insert(x);
});
processIntFile(argv[3], [&](int x) {
m3.remove(x);
});
std::cout << "MinMedianMaxSketch:\n";
m3.report();
}
}
return 0;
}