-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatientPriorityQueue.cpp
More file actions
88 lines (72 loc) · 2.15 KB
/
Copy pathPatientPriorityQueue.cpp
File metadata and controls
88 lines (72 loc) · 2.15 KB
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
//
// Created by Nichlos Ho on 10/30/20.
//
#include <algorithm>
#include "PatientPriorityQueue.h"
PatientPriorityQueue::PatientPriorityQueue() = default;
void PatientPriorityQueue::enqueue(const Patient& newItem) {
data.push_back(newItem);
percolateUp(data.size() - 1);
}
const Patient &PatientPriorityQueue::peek() const {
return data.front();
}
int PatientPriorityQueue::size() {
return data.size();
}
void PatientPriorityQueue::percolateUp(int index) {
if (index > 0) {
int p = getParent(index); // getParent index
Patient temp = Patient::compareTo(data[index], data[p]);
if (temp.toString() == data[index].toString()) {
std::swap(data[index], data[p]);
percolateUp(p);
}
}
}
int PatientPriorityQueue::getParent(int child) {
return (child - 1) / 2;
}
Patient PatientPriorityQueue::getData(int index) {
return data.at(index);
}
Patient PatientPriorityQueue::dequeue() {
Patient ret = peek();
data[0] = data.back();
data.pop_back();
percolateDown(0);
return ret;
}
void PatientPriorityQueue::percolateDown(int index) {
int l = getLeft(index);
int r = getRight(index);
if (size() == 2) {
Patient temp = Patient::compareTo(data[l], data[index]); // returns the high priority of l or r
if (temp.toString() == data[l].toString()) {
std::swap(data[index], data[l]);
return;
}
return;
}
if (l >= size())
return;
Patient temp = data[l];
if (l < size() && r < size()) {
temp = Patient::compareTo(data[l], data[r]);
}// returns the high priority of l or r
Patient temp2 = Patient::compareTo(temp, data[index]); // return the higher priority between temp and index (parent)
if (temp2.toString() == data[l].toString()) {
std::swap(data[index], data[l]);
percolateDown(l);
}
if (temp2.toString() == data[r].toString()) {
std::swap(data[index], data[r]);
percolateDown(r);
}
}
int PatientPriorityQueue::getRight(int index) {
return 2 * index + 2;
}
int PatientPriorityQueue::getLeft(int index) {
return 2 * index + 1;
}