-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedian.h
58 lines (49 loc) · 1.35 KB
/
median.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
50
51
52
53
54
55
56
57
58
// Project identifier: 9504853406CBAC39EE89AA3AD238AA12CA198043
#include <vector>
#include <queue>
#include "zomb.h"
using namespace std;
class medianZombQueue { // median specifically for zomb rds active
private:
priority_queue<zomb*, vector<zomb*>, revZombMedCMP> uppHalf;
priority_queue<zomb*, vector<zomb*>, zombMedCMP> lowHalf;
public:
void push(zomb* newZomb) {
if (uppHalf.empty() && lowHalf.empty()) {
uppHalf.push(newZomb);
}
else if (newZomb->rdsAct > uppHalf.top()->rdsAct) {
uppHalf.push(newZomb);
}
else {
lowHalf.push(newZomb);
}
// Rebalancing queue sizes if need be
if ((uppHalf.size() - lowHalf.size()) == 2) {
lowHalf.push(uppHalf.top());
uppHalf.pop();
}
else if ((lowHalf.size() - uppHalf.size()) == 2) {
uppHalf.push(lowHalf.top());
lowHalf.pop();
}
}
uint32_t getMed() const {
if (uppHalf.size() == lowHalf.size()) {
return ((uppHalf.top()->rdsAct + lowHalf.top()->rdsAct) / 2);
}
else if (uppHalf.size() > lowHalf.size()) {
return uppHalf.top()->rdsAct;
}
else {
return lowHalf.top()->rdsAct;
}
}
bool empty() const {
return (uppHalf.empty() && lowHalf.empty());
}
size_t size() const {
return (uppHalf.size() + lowHalf.size());
}
};
// Edit above class for p2b medianQueue part. (edit getMed and template it)